Guest User

Untitled

a guest
Jul 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import numpy as np
  2. """
  3. # 1)
  4.  
  5. Define a function MaxOfThree() that takes three numbers as arguments
  6. and returns the largest of them (Use If, elif and else).
  7. For example, MaxOfThree(1, 2, 3) should return 3
  8.  
  9. def MaxOfThree(x1, x2, x3):
  10. if (type(x1) == int and type(x2) == int and type(x3) == int):
  11. if(x1 > x2 and x1> x3):
  12. return x1
  13. elif(x2 > x1 and x2> x3):
  14. return x2
  15. else:
  16. return x3
  17. else:
  18. print("Dambass, input numbers only!")
  19.  
  20. print(MaxOfThree(1,"2",3))
  21.  
  22.  
  23. # 2)
  24.  
  25. Define a function MySum() and a function MyMultiply() that sums
  26. and multiplies (respectively) all the numbers in a list of
  27. numbers (Use for). For example, s MySum([1, 2, 3, 4]) should return 10,
  28. and MyMultiply([1, 2, 3, 4]) should return 24.
  29.  
  30. n = range(1, 6)
  31. def MySum(list1):
  32. sum =0
  33. for l in list1:
  34. sum += l
  35. return sum
  36.  
  37. def MyMultiply(list2):
  38. mult = 1
  39. for l in list2:
  40. mult *= l
  41. return mult
  42.  
  43. print(MySum(n))
  44. print(MyMultiply(n))
  45.  
  46.  
  47. # 3)
  48.  
  49. Define a function MyMean() that receives an unknown amount
  50. of input values from a user and returns the mean value of all the
  51. input values (Use while). For example: if the user provides [1, 2, 3, 4],
  52. the MyMean(), should return 2.5
  53.  
  54. def MyMean(l):
  55. print(np.mean(l))
  56.  
  57. final = []
  58. while True:
  59. means = input('Please enter a number: ')
  60. if not means:
  61. break
  62. final.append(int(means))
  63.  
  64. MyMean(final)
  65.  
  66. Define a function MyStars() that takes a list of integers and prints
  67. a string of stars which has the length of a value of an integer to
  68. the screen. For example, MyStars([3, 9, 7]) should print the following:
  69. ***
  70. *********
  71. *******
  72.  
  73.  
  74. numbers = []
  75.  
  76. while True:
  77. means = input('Please enter a number: ')
  78. if not means:
  79. break
  80. numbers.append(int(means))
  81.  
  82.  
  83. def MyStars(l):
  84. for i in l:
  85. print("*"*i)
  86.  
  87. MyStars(numbers)
  88.  
  89. # 5)
  90. Define a function SecondBest() that receives a list of numbers and
  91. returns the second largest and second smallest numbers. For example,
  92. SecondBest([1, 2, 3, 4]) should return [3, 2] (do not use built in
  93. functions such as max or sort).
  94. best_list = []
  95.  
  96. while True:
  97. means = input('Please enter a number: ')
  98. if not means:
  99. break
  100. best_list.append(int(means))
  101.  
  102. def SecondBest(best_list):
  103. if best_list[0] > best_list[1]:
  104. first_max = best_list[0]
  105. first_min = best_list[1]
  106. else:
  107. first_max = best_list[1]
  108. first_min = best_list[0]
  109. second_min = first_max
  110. second_max = first_min
  111.  
  112. for i in range(2, len(best_list)):
  113. if best_list[i] > first_max:
  114. second_max = first_max
  115. first_max = best_list[i]
  116. elif best_list[i] < first_min:
  117. second_min = first_min
  118. first_min = best_list[i]
  119. return second_min, second_max
  120.  
  121. x, y = SecondBest(best_list)
  122. print("Second largest number is " + str(y))
  123. print("Second smallest number is " + str(x))
  124.  
  125. """
  126.  
  127. #6)
  128. letters = []
  129. while True:
  130. input_l = input('Please enter a letter: ')
  131. if not input_l:
  132. break
  133. letters.append(input_l)
  134.  
  135. def MySort(char_list):
  136. sorted_l = [range(ord("a"), ord("z"))]
  137. for i in char_list:
  138. sorted_l[chr(ord(i))] += 1
  139. for j in sorted_l:
  140. while j>0:
  141. char_list.append(chr(ord("a")+j))
  142. j -= 1
  143. return char_list
  144.  
  145. print(MySort(letters))
Add Comment
Please, Sign In to add comment