This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| a,b,c = [random.randint(-100,100) for i in range(3)] | |
| print a,b,c | |
| #the actual solution | |
| mx = max(a,max(b,c)) # max of a,b,c | |
| mn = -max(-a,max(-b,-c)) # min of a,b,c, lol! :D | |
| mid = a+b+c-mn-mx # middle term | |
| print mn,mid,mx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import fileinput | |
| # add_edge (u -> v) with weight w to graph g | |
| def add_edge(graph,u,v,w): | |
| if u in graph: #vertex u already in graph? | |
| l = graph[u] #get list of neighbours | |
| else: | |
| graph[u] = l = list() #insert u into graph and assign an empty list of neighbours | |
| l.append((v,w)) #append tuple (v,w) into the neighbour list | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def find_first_gap_linear(lst): | |
| for i in xrange(len(r)-1): | |
| if r[i] + 1 != r[i+1]: | |
| return r[i] + 1 | |
| return -1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def find_first_gap(lst, low , high): | |
| if high - low == lst[high] - lst[low]: return -1 | |
| if low + 1 == high: return lst[low] + 1 | |
| mid = (low + high) / 2 | |
| if mid - low < lst[mid] - lst[low]: #gap in first half | |
| return find_first_gap(lst,low,mid) | |
| else: | |
| return find_first_gap(lst,mid,high) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fermatPass(n): | |
| return n > 1 and ( n == 2 or ( n % 2 != 0 and pow( random.randint( 1, n - 1 ), n - 1, n ) == 1 ) ) | |
| def fermatTest(n,k = 10): | |
| return all( fermatPass( n ) for i in xrange(k) ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def millerRabin(n, k = 5): | |
| if n <= 3 or n % 2 == 0: return n == 2 or n == 3 | |
| d = n - 1 | |
| s = 0 | |
| while d > 0 and d % 2 == 0: | |
| d = d / 2 | |
| s = s + 1 | |
| def millerRabinPass(a, s, d, n): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def lucasLehmerTest(p): #checks if 2^p - 1 is a prime (also called a mersenne prime) | |
| if p % 2 == 0 and not isPrime(p): return False | |
| s = 4 | |
| M = (1 << p) - 1 | |
| for _ in xrange(p - 2): | |
| s = ((s * s) - 2) % M | |
| return s == 0 | |
| def isPrime(n): | |
| if n <= 3 or n % 2 == 0 or n % 3 == 0: return n == 2 or n == 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class StackMin extends Stack { | |
| private Stack minStack; | |
| public StackMin(int size) { | |
| super(size); | |
| minStack = new Stack(size); | |
| } | |
| @Override |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random,Queue | |
| pq = Queue.PriorityQueue() | |
| for q in range(10): | |
| p = random.randint(0,100) | |
| print "Inserting %d with priority %d" % (q,p) | |
| pq.put((p,q)) | |
| while not pq.empty(): | |
| print pq.get() | |
| raw_input() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import heapq as hp | |
| class pqueue: | |
| def __init__(self, minheap = True): | |
| self.heap = [] | |
| self.mul = 1 if minheap else -1 | |
| self.count = 0 | |
| def size(self): |
OlderNewer