Advertisement
biswasrohit20

pythonExercise

May 16th, 2021
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. # Exercise 11.1 – Iteration
  2.  
  3. # a. Write a program that will print your full name and student number 10 times.
  4.  
  5. for i in range(10):
  6. print("My name is Rohit Biswas")
  7. print("My student number is 001811301033")
  8.  
  9. # b. Write another program that will ask the user for a “wish” (eg. What do you wish for on
  10. # your birthday) and the number of time they want that wish to be displayed. Then print the
  11. # wish the number of times specified.
  12.  
  13. wish = input("What do you wish for on your birthday: ")
  14. num = int(input("How many times do you want wish to be displayed: "))
  15.  
  16. for j in range(num):
  17. print(wish)
  18.  
  19.  
  20.  
  21. # Exercise 11.2 – Sum
  22. # Given numLst = [1, 2, 3, 4, 5] we can compute the sumImperative in an imperative way. You have
  23. # to implement it such that it uses imperative way to compute the sum of the list.
  24.  
  25.  
  26. numLst = [1, 2, 3, 4, 5]
  27. sumImperative = 0
  28. for n in numLst:
  29. sumImperative += n
  30. print(sumImperative)
  31.  
  32.  
  33. # Exercise 11.3 – Even numbers
  34. # Using the imperative way of programming, find even numbers (printEvenNumbers) given a list of
  35. # natural numbers:
  36.  
  37. naturalNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  38.  
  39. for i in naturalNumbers:
  40. if i % 2 == 0:
  41. print(i)
  42.  
  43.  
  44. # Exercise 11.4 – Lists
  45. # Using the imperative coding style, implement a function groupList that given a list (list) and a
  46. # group length (glength), returns a list of lists with length glength.
  47.  
  48.  
  49. def groupList(l, s):
  50. main_output = []
  51. temp_list = []
  52. n = 0
  53. for i in l:
  54. temp_list.append(i)
  55. n = n + 1
  56. if n == s:
  57. main_output.append(temp_list.copy())
  58. temp_list = []
  59. n = 0
  60.  
  61. return main_output
  62.  
  63. #ab = [1,2,3,4,5,6,7,8,9]
  64. #print(groupList(ab,3))
  65.  
  66.  
  67.  
  68. # Exercise 11.5 – Character Lists
  69. # Write a program to check that a given character is in a given string. It will ask the user to enter a
  70. # string and a character to search. Use linear search for this program where you loop through all the
  71. # characters.
  72.  
  73. st = input("Enter a string: ")
  74. ch = input("Enter a character to search: ")
  75. found = False
  76. for c in st:
  77. if c == ch:
  78. found = True
  79.  
  80. if found:
  81. print("The character is present in the string")
  82. else:
  83. print("The character is not found in the string")
  84.  
  85.  
  86.  
  87. # Exercise 13.1
  88. # Given numLst = [1, 2, 3, 4, 5], define a functional sumFunctional to sum the list.
  89.  
  90.  
  91. def sumFunctional(l):
  92. sum = 0
  93. for i in l:
  94. sum += i
  95. return sum
  96.  
  97.  
  98. numLst = [1, 2, 3, 4, 5]
  99. print(sumFunctional(numLst))
  100.  
  101.  
  102. # Exercise 13.2 – Recursion
  103. # Recall from F# sessions, where the factorial of an integer number is defined by the following
  104.  
  105.  
  106. # a. Write a Python factorialR() function using recursion
  107.  
  108. def factorialR(n):
  109. if n == 0 or n == 1:
  110. return 1
  111. else:
  112. return n * factorialR(n-1)
  113.  
  114. #print(factorialR(5))
  115.  
  116.  
  117.  
  118. # b. Write a Python factorialI() function using iterative style:
  119.  
  120. def factorialI(n):
  121. factorial = 1
  122. if n > 0:
  123. for i in range(1, n+1):
  124. factorial = factorial * i
  125. return factorial
  126.  
  127. #print(factorialI(5))
  128.  
  129.  
  130.  
  131. # Exercise 13.3 – Higher-order, lambda
  132. # Write a function to find even numbers (printEvenNumbers) given a list of natural numbers:
  133. # naturalNumbers = [0,1,2,3,4,5,6,7,8,9]
  134. # Use lambda and filter() function.
  135.  
  136.  
  137. naturalNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  138.  
  139.  
  140. def printEvenNumbers(l):
  141. even = list(filter(lambda i: (i % 2 == 0), l))
  142. print("Even numbers in the list: ", even)
  143.  
  144. printEvenNumbers(naturalNumbers)
  145.  
  146.  
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement