Advertisement
Guest User

Ajoy

a guest
May 29th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.93 KB | None | 0 0
  1. #takes n integers as input, stores them in a list and returns the list
  2. def input_list(n):
  3. lst=[]
  4. while n>0:
  5. lst.append(int(input()))
  6. n-=1
  7. return lst
  8.  
  9.  
  10. #returns the sum of the numbers in list a[]
  11. #version 1: iteration by item
  12. def sum_list_1(a):
  13. _sum=0
  14. for num in a:
  15. _sum+=num
  16. return _sum
  17.  
  18.  
  19. #returns the sum of the numbers in list a[]
  20. #version 2: iteration by index
  21. def sum_list_2(a):
  22. _sum=0
  23. for i in range(len(a)):
  24. _sum+=a[i]
  25. return _sum
  26.  
  27.  
  28. #write it yourself
  29. #return the sum of the squares of the numbers in list a[]
  30. #for example, sum_square([1,2,3]) should return 14
  31. #def sum_square(a):
  32.  
  33.  
  34.  
  35.  
  36. #write it yourself
  37. #return the average of the numbers in list a[]
  38. #for example, average([1,2,3]) should return 2.0
  39. #def average(a):
  40.  
  41.  
  42.  
  43.  
  44. #write it yourself
  45. #return the minimum number in list a[]
  46. #for example, minimum([1,2,3]) should return 1
  47. #def minimum(a):
  48.  
  49.  
  50.  
  51.  
  52. #write it yourself
  53. #return the number of odd numbers in list a[]
  54. #for example, count_odd([1,2,3]) should return 2
  55. #def count_odd(a):
  56.  
  57.  
  58.  
  59.  
  60. #write it yourself
  61. #return the sum of the even numbers in list a[]
  62. #for example, sum_even([1,2,3,4]) should return 6
  63. #def sum_even(a):
  64.  
  65.  
  66.  
  67.  
  68. #write it yourself
  69. #return the sum of the numbers in list a[] until the first negative number is found (excluding the negative number)
  70. #for example, sum_until_negative([1,2,3,-4,5,6]) should return 6
  71. #def sum_until_negative(a):
  72.  
  73.  
  74.  
  75.  
  76. #returns a list of consecutive sums
  77. #ret[i] contains the sum of the first i+1 elements of list a[]
  78. #for example consecutive_sum_1([1,2,3,4]) returns [1,3,6,10]
  79. #version 1 uses the sum_list_1() function and slicing operator
  80. def consecutive_sum_1(a):
  81. ret=[]
  82. for i in range(len(a)):
  83. ret.append(sum_list_1(a[0:i+1]))
  84. return ret
  85.  
  86.  
  87. #returns a list of consecutive sums
  88. #ret[i] contains the sum of the first i+1 elements of list a[]
  89. #for example consecutive_sum_2([1,2,3,4]) returns [1,3,6,10]
  90. #version 2 builds ret[] incrementally and thus is more efficient
  91. def consecutive_sum_2(a):
  92. ret=[]
  93. _sum=0
  94. for num in a:
  95. _sum+=num
  96. ret.append(_sum)
  97. return ret
  98.  
  99.  
  100. #returns a list of first n fibonacci numbers
  101. def fibonacci(n):
  102. fib=[]
  103. fib.append(0)
  104. fib.append(1)
  105. for i in range(2,n):
  106. fib.append(fib[i-1]+fib[i-2])
  107. return fib
  108.  
  109.  
  110. #write it yourself
  111. #return a list of first n prime numbers
  112. #def prime(n):
  113.  
  114.  
  115.  
  116.  
  117. #write it yourself
  118. #return the largest (having maximum length) sublist of a[] having only nonnegative elements(sublist of a list is similar to substring of a string)
  119. #for example, largest_nonnegative_sublist([2,-2,-1,1,2,3,-2,1,1,1,2,-1,-2,1]) should return [1,1,1,2]
  120. #def largest_nonnegative_sublist(a):
  121.  
  122.  
  123.  
  124.  
  125. #write it yourself
  126. #return the sublist of a[] having maximum sum
  127. #for example, maximum_sum_sublist([2,-3,1,2,3,-5,1,1,1,1]) should return [1,2,3]
  128. #def maximum_sum_sublist(a):
  129.  
  130.  
  131.  
  132.  
  133. #sorts a list of integers in ascending order using bubble sort algorithm
  134. #bubble_sort() returns nothing, it just modifies the argument list a[]
  135. #see the animation in the wiki page to have a clearer idea: http://en.wikipedia.org/wiki/Bubble_sort
  136. def bubble_sort(a):
  137. for i in range(len(a)-2,-1,-1):
  138. swapped=False
  139. for j in range(0,i+1):
  140. if a[j]>a[j+1]:
  141. a[j],a[j+1]=a[j+1],a[j]
  142. swapped=True
  143. #print(a) #comment in this line to see a simulation of the bubble sort algorithm
  144. if not swapped:
  145. return
  146.  
  147.  
  148. #sorts a list of integers in ascending order using selection sort algorithm
  149. #selection_sort() returns nothing, it just modifies the argument list a[]
  150. #see the animation in the wiki page to have a clearer idea: http://en.wikipedia.org/wiki/Selection_sort
  151. def selection_sort(a):
  152. for i in range(len(a)-1):
  153. min_index=i
  154. for j in range(i+1,len(a)):
  155. if a[j]<a[min_index]:
  156. min_index=j
  157. a[i],a[min_index]=a[min_index],a[i]
  158. #print(a) #comment in this line to see a simulation of the selection sort algorithm
  159.  
  160.  
  161. #write it yourself
  162. #sort a list of integers in ascending order using counting sort algorithm
  163. #assume that the elements of a[] are integers in range(10)
  164. #create a list count[] of length 10, where count[i] holds the number of occurrences of i in a[], for 0<=i<=9
  165. #for example, for a=[3,5,1,2,3,7,0,0,9,8,4,3,5,6,2], count=[2,1,2,3,1,2,1,1,1,1], because 0 appears twice in a[], 1 appears once in a[] and so on
  166. #note that, sum_list_1(count) equals len(a) (why?)
  167. #now create another list ret[] by appending count[i] number of i's, for 0<=i<=9
  168. #for example, the first two elements of ret[] are 0, the next element is 1, the next two elements are 2 and so on
  169. #thus ret[] is the sorted version of a[]
  170. #note that, counting_sort() can not be used to sort non-integral values and the range of integers should be small and known in advance (why?)
  171. #def counting_sort(a):
  172.  
  173.  
  174.  
  175.  
  176. #takes n strings as input, stores them in a list and returns the list
  177. def input_str_list(n):
  178. lst=[]
  179. while n>0:
  180. lst.append(input())
  181. n-=1
  182. return lst
  183.  
  184.  
  185. #write it yourself
  186. #return the number of strings in list s[] which have length equal to c
  187. #for example, len_count(["abc","de","fghi","jk","l"],2) should return 2
  188. #you can use the input_str_list() function above to take a list of strings as input
  189. #def len_count(s,c):
  190.  
  191.  
  192.  
  193.  
  194. #s is a string containing words separated by a single space
  195. #returns a string containing the words of s in reverse order
  196. #for example, reverse_sentence("this is good") will return "good is this"
  197. def reverse_sentence(s):
  198. words=s.split()
  199. words.reverse()
  200. return " ".join(words)
  201.  
  202.  
  203. #write it yourself
  204. #take a string s as input, which contains words separated by a single space
  205. #return a string containing each word in its' position but individually reversed
  206. #for example, reverse_words("this is good") should return "siht si doog"
  207. #def reverse_words(s):
  208.  
  209.  
  210.  
  211.  
  212. #takes n*m integers as input, stores them in a 2D list of n rows and m columns and returns the list
  213. def input_2D_list(n,m):
  214. lst=[]
  215. while n>0:
  216. lst.append(input_list(m))
  217. n-=1
  218. return lst
  219.  
  220.  
  221. #adds two matrices and returns the resultant matrix
  222. def add_matrices(a,b):
  223. n=len(a)
  224. m=len(a[0])
  225. c=[]
  226. for i in range(n):
  227. tmp=[]
  228. for j in range(m):
  229. tmp.append(a[i][j]+b[i][j])
  230. c.append(tmp)
  231. return c
  232.  
  233.  
  234. #multiplies two matrices and returns the resultant matrix
  235. def multiply_matrices(a,b):
  236. p=len(a)
  237. q=len(a[0])
  238. r=len(b[0])
  239. c=[]
  240. for i in range(p):
  241. tmp=[]
  242. for j in range(r):
  243. _sum=0
  244. for k in range(q):
  245. _sum+=a[i][k]*b[k][j]
  246. tmp.append(_sum)
  247. c.append(tmp)
  248. return c
  249.  
  250.  
  251. #write it yourself
  252. #return a list containing the maximum number of each row of a 2D list a[][]
  253. #for example, row_maximum([3,1,2],[-3,-1,-2],[1,0,1],[0,0,0]) should return [3,-1,1,0]
  254. #def row_maximum(a):
  255.  
  256.  
  257.  
  258.  
  259. #write it yourself
  260. #a[] is a list of positive integers
  261. #return a 2D list of integers ret[][], where ret[i][j] holds the sum of the numbers in the sublist a[i:j] if i<j, 0 otherwise
  262. #for example, sublist_sum([1,2,3]) should return [[0,1,3,6],[0,0,2,5],[0,0,0,3]]
  263. #def sublist_sum(a):
  264.  
  265.  
  266.  
  267.  
  268. #write it yourself
  269. #flatten a list a[] of arbitrary dimension
  270. #for example, flatten([1,[2,3],[[[1]],3],[[[[[1]]]]],2,[1,[2,[3]]]]) should return [1,2,3,1,3,1,2,1,2,3]
  271. #you might need to use recursion and a global list
  272. #def flatten(a):
  273.  
  274.  
  275.  
  276.  
  277. #returns true if a[][] is an identity matrix, false otherwise
  278. # http://en.wikipedia.org/wiki/Identity_matrix
  279. def isIdentity(a):
  280. n=len(a)
  281. for i in range(n):
  282. for j in range(n):
  283. if i==j:
  284. if a[i][j]!=1:
  285. return False
  286. else:
  287. if a[i][j]!=0:
  288. return False
  289. return True
  290.  
  291.  
  292. #returns the transpose of matrix a[][]
  293. # http://en.wikipedia.org/wiki/Transpose
  294. def transpose(a):
  295. n=len(a)
  296. m=len(a[0])
  297. c=[]
  298. for i in range(m):
  299. tmp=[]
  300. for j in range(n):
  301. tmp.append(a[j][i])
  302. c.append(tmp)
  303. return c
  304.  
  305.  
  306. #write it yourself
  307. #return true if 2D matrix a[][] is symmetric, false otherwise
  308. # http://en.wikipedia.org/wiki/Symmetric_matrix
  309. #write 2 versions with and without using the transpose() function
  310. #def isSymmetric(a):
  311.  
  312.  
  313.  
  314.  
  315. #write it yourself
  316. #return true if 2D matrix a[][] is upper triangular, false otherwise
  317. # http://en.wikipedia.org/wiki/Triangular_matrix
  318. #def isUpperTriangular(a):
  319.  
  320.  
  321.  
  322.  
  323. #prints 2D list a[][]
  324. def print_2D_list(a):
  325. n=len(a)
  326. m=len(a[0])
  327. for i in range(n):
  328. for j in range(m):
  329. print(repr(a[i][j]).ljust(4),end="")
  330. print()
  331. print()
  332.  
  333.  
  334. #returns the number of distinct elements in a list of integers a[]
  335. def distinct_count(a):
  336. lst=[]
  337. for num in a:
  338. if num not in lst:
  339. lst.append(num)
  340. return len(lst)
  341.  
  342.  
  343. #write it yourself
  344. #return the number of distinct elements in a sorted list of integers a[]
  345. #write a more efficient version than the previous one
  346. #def distinct_count_sorted(a):
  347.  
  348.  
  349.  
  350.  
  351. #tester of some functions
  352. def main():
  353. #test sum_list_1() and sum_list_2()
  354. #n=int(input())
  355. #a=input_list(n)
  356. #print(a)
  357. #print(sum_list_1(a))
  358. #print(sum_list_2(a))
  359.  
  360. #test consecutive_sum_1() and consecutive_sum_2()
  361. #n=int(input())
  362. #a=input_list(n)
  363. #print(a)
  364. #print(consecutive_sum_1(a))
  365. #print(consecutive_sum_2(a))
  366.  
  367. #test bubble_sort()
  368. #n=int(input())
  369. #a=input_list(n)
  370. #print(a)
  371. #bubble_sort(a)
  372. #print(a)
  373.  
  374. #test selection_sort()
  375. #n=int(input())
  376. #a=input_list(n)
  377. #print(a)
  378. #selection_sort(a)
  379. #print(a)
  380.  
  381. #test reverse_sentence()
  382. #s=input()
  383. #print(reverse_sentence(s))
  384.  
  385. #test add_matrices()
  386. #n=int(input())
  387. #m=int(input())
  388. #a=input_2D_list(n,m)
  389. #b=input_2D_list(n,m)
  390. #print_2D_list(a)
  391. #print_2D_list(b)
  392. #c=add_matrices(a,b)
  393. #print_2D_list(c)
  394.  
  395. #test multiply_matrices()
  396. #p=int(input())
  397. #q=int(input())
  398. #r=int(input())
  399. #a=input_2D_list(p,q)
  400. #b=input_2D_list(q,r)
  401. #print_2D_list(a)
  402. #print_2D_list(b)
  403. #c=multiply_matrices(a,b)
  404. #print_2D_list(c)
  405.  
  406. print()
  407.  
  408.  
  409. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement