pgupta6
BAN USER#input [2,1,20,3,6,5,4,8,11,12]
# N = 3
#output [20,11,12]
# O(n) = (n-x)x
###### create an top_array of n elements from input array,
###### find the minimum of top_array and compare the
###### all the elemnts from n array if the input array
###### elements is bigger than top_array, swap the value
def fill_n_elements(arr,n):
top_arr = []
for i in range(n):
top_arr.append(arr[i])
return top_arr
def find_min(top_arr):
min = top_arr[0]
index = 0
for i in range(1,len(top_arr)):
if min > top_arr[i]:
min = top_arr[i]
index = i
return index
def compare(i,min,arr,top_arr):
if top_arr[min] < arr[i]:
return 1
else:
return -1
def replace(i,min,arr,top_arr):
top_arr[min] = arr[i]
return top_arr
class Top:
def top_n(self,arr, n):
if arr == [] or n == 0:
return arr
top_arr = fill_n_elements(arr,n)
for i in range(n,len(arr)):
min = find_min(top_arr)
flag = compare(i,min,arr,top_arr)
if flag > 0:
top_arr = replace(i,min,arr,top_arr)
return top_arr
top = Top()
arr = [2,1,20,3,6,5,4,8,11,12]
top_arr = top.top_n(arr,3)
print top_arr
class fibo_sequence:
def fibonacci(self,n):
if n == 0 and n == 1:
return n
fibo = 0
temp1 = 1
temp2 = 1
arr = []
finalarr = []
count = 0
while(count < n ):
fibo = temp1 + temp2
temp2 = temp1
temp1 = fibo
arr.append(temp2%10)
arr.append(temp1%10)
finalarr.append(arr)
arr = []
count = count + 1
return finalarr
f = fibo_sequence()
print f.fibonacci(10)
We can not maintain the exact percentage at any given point, if we are adding the person 1 by 1 continuously in to the room.
We can make the queue of certain size after filling the queue we will move in the persons in the respective rooms based upon the percentage.. by this method we will maintain the exact percentage at any give point.
find the size of the queue = LCM of [% of a, % of b, % of c]
once the size is full, distribute person based upon a = (% of a)/LCM
b = (% of b )/ LCM
c = (% of c ) /LCM
- pgupta6 September 16, 2015