mikeolteanu
BAN USER#You are given an integer array, where all numbers except for
#TWO numbers appear even number of times.
#Q: Find out the two numbers which appear odd number of times.
intarr = [1,2,3,4,5,6,7,8,9,10,1,2,4,5,6,7,8,9]
def find_two_ints_not_even(intlist):
odd_int={}
retlist=[]
for _int in intlist:
if _int in odd_int:
odd_int[_int]=odd_int[_int]+1
else:
odd_int[_int]=1
for val1 in odd_int:
if odd_int[val1] % 2 == 1:
retlist.append(val1)
if len(retlist)==2:
return retlist
return retlist
print find_two_ints_not_even(intarr)
python
#Conways game of life
#Any live cell with fewer than two live neighbours dies,
#as if caused by under-population.
#Any live cell with two or three live neighbours lives on to the next
#generation.
#Any live cell with more than three live neighbours dies, as if by
#overcrowding.
#Any dead cell with exactly three live neighbours becomes a live cell,
#as if by reproduction.
class Grid(object):
def __init__(self,size):
self.size=size
coordlist=[]
for count in range(size):
coordlist.append(0)
self.coordlist=[]
for count in range(size):
self.coordlist.append(coordlist[:])
def __str__(self):
retstringlist=[]
for row in self.coordlist:
for cell in row:
retstringlist.append("|" + str(cell))
retstringlist.append("\n")
return "".join(retstringlist)
def step_turn(self):
next_coordlist=[]
for count in range(self.size):
next_coordlist.append(0)
next_coordlist1=[]
for count in range(self.size):
next_coordlist1.append(next_coordlist[:])
for x in range(self.size):
for y in range(self.size):
next_coordlist2=self.calc_rules(x,y,next_coordlist1)
self.coordlist=next_coordlist2
def calc_rules(self,x,y,newcoords):
neighbours = self.calc_number_neighbours(x,y)
if neighbours < 2:
newcoords[x][y]=0
if neighbours == 2 and self.coordlist[x][y]==1:
newcoords[x][y]=1
if neighbours == 3:
newcoords[x][y]=1
if neighbours > 3:
newcoords[x][y]=0
return newcoords
def calc_number_neighbours(self,x,y):
neighbourcount=0
for a in range(3):
for b in range(3):
#print "a=" + str(a) + " b=" + str(b) + " coords checking: " + str(x+a-1) + " and " + str(y+b-1)
if not (a==1 and b==1):
checka=x+a-1
checkb=y+b-1
if checka==self.size:
checka=0
if checkb==self.size:
checkb=0
if self.coordlist[checka][checkb]==1:
#print "found a neighbour at " + str(checka) + "and" + str(checkb)
neighbourcount+=1
#print str(x) + "," + str(y) + ", " + str(neighbourcount)
return neighbourcount
grid1 = Grid(10)
grid1.coordlist[1][2]=1
grid1.coordlist[2][2]=1
grid1.coordlist[3][2]=1
grid1.coordlist[3][1]=1
grid1.coordlist[2][0]=1
print grid1
grid1.step_turn()
print grid1
grid1.step_turn()
print grid1
grid1.step_turn()
print grid1
grid1.step_turn()
print grid1
grid1.step_turn()
print grid1
grid1.step_turn()
print grid1
python
#Given an array, with positive and negative integers, arrange it in such a way that, positive numbers occupy even positions and negative numbers occupy odd position. All the remaining extra positive or negative integers should be stored at the end of the array. Remember, the elements of the array should remain in the same order.
#EG: Input array {1,-2,3,-4,-5,-6,-7,8,9,4,10,11,12}
#output array {1,-2,3,-4,8,-5,9,-6,4,-7,10,11,12}
def pos_neg_sort(inarray):
retlist = []
poscount = 0
negcount = 1
for x in range(len(inarray)*2):
retlist.append(0)
for val in inarray:
if val > 0:
retlist[poscount]=val
poscount+=2
if val < 0:
retlist[negcount]=val
negcount+=2
retlist2=[]
for val in retlist:
if val != 0:
retlist2.append(val)
return retlist2
inarray = [1,-2,3,-4,-5,-6,-7,8,9,4,10,11,12]
outarray = pos_neg_sort(inarray)
if outarray == [1,-2,3,-4,8,-5,9,-6,4,-7,10,11,12]:
print "Success!"
else:
print "Failure!"
print outarray
def find_all_pythag_triples(intlist):
retlist=[]
trip_iterator = triple_iterator(intlist)
for triple in trip_iterator:
test_result = test_triple(triple)
if test_result == True:
retlist.append(triple)
return retlist
def triple_iterator(testlist):
triplist=[]
num_items = len(testlist)
for item1 in range(num_items):
for item2 in range(num_items):
for item3 in range(num_items):
if item1!=item2 and item2!=item3 and item3!=item1:
yield (testlist[item1],testlist[item2],testlist[item3])
def test_triple(triple):
if triple[0]*triple[0] + triple[1]*triple[1] == triple[2]*triple[2]:
return True
else:
return False
testlist = [1,3,4,5,6,7,8,10,11]
print find_all_pythag_triples(testlist)
Python
Assumption:
if the 5 largest elements are all “100” and the 6th largest is “99”, then the 6th largest is “99”. 99 is not the “2nd largest”, 100 is.
class Matrix:
def __init__(self,rowlist):
self.rowlist = rowlist
def find_kth_largest_element2(matrix1, k):
queue1=[]
for row in matrix1.rowlist:
for cell in row:
if len(queue1) == 0:
queue1.append(cell)
elif len(queue1) > 0 and cell > queue1[0]:
queue1.append(cell)
queue1=sorted(queue1)
if len(queue1) > k:
queue1.pop(0)
return queue1[0]
x1 = Matrix([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]])
print find_kth_largest_element2(x1, 5)
>>>
11
python
- mikeolteanu September 12, 2013