Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Get time in milli secs
- import time
- cur_time_milli_sec = int(time.time() * 1000)
- # Read file
- with open("1.txt") as f:
- for line in f.xreadlines():
- print line.rstrip('\r\n')
- # Write file ('a' for append, 'w' for write)
- with open("2.txt", 'a') as f:
- f.write("123\n")
- # Read from stdin (the input stream has to end with EOF)
- import sys
- for line in sys.stdin.xreadlines():
- print line.rstrip('\r\n')
- # Read from stdin (prompt the user for input)
- name = raw_input("type your name: ")
- print name, len(name)
- # bash redirect file to stdin: python 1.py 0<1.txt
- # bash redirect stdout to file: python 1.py 0<1.txt 1>3.txt
- # random numbers
- from random import seed
- from random import random
- from random import randint
- seed(1)
- # random float number between 0-1
- rand_float = random()
- print rand_float
- # random int number between [0, 1] (inclusive)
- rand_int = randint(0, 1)
- print rand_int
- # iterator (note that xrange is not a iterator)
- a = iter(range(5))
- print next(a, None) # get the next with a default value so we can check if there is not more values
- print next(a, None)
- # Find the first index in an array where array[index] >= target
- a = [1,2,3,3,3,4,5,6,6,6]
- def findIndex(array, target):
- if not array:
- return 0
- if array[0] >= target:
- return 0
- if array[-1] < target:
- return len(array)
- left = 0
- right = len(array) - 1
- while left < right:
- mid = (left + right) / 2
- if array[mid] >= target:
- if (mid - 1) >= 0 and array[mid-1] < target:
- return mid
- right = mid - 1
- elif array[mid] < target:
- left = mid + 1
- return left
- for i in range(8):
- print "find " + str(i) + " = " + str(findIndex(a, i))
Add Comment
Please, Sign In to add comment