Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. Homework 2
  2. Derek Moy
  3. CS 110-03
  4. 10/23/2017
  5. ID: 20397155
  6.  
  7.  
  8. 1. Suppose the value of b is False and the value of x is 0. What is the value of each of the following expressions? (1 point each)
  9. (a) b and x == 0
  10. F ^ T = False
  11.  
  12. (b) b or x == 0
  13. F v T = True
  14.  
  15. (c) not b and x == 0
  16. ¬F ^ T = T ^ T = True
  17.  
  18. (d) not b or x == 0
  19. ¬F v T = T v T = True
  20.  
  21. (e) b and x != 0
  22. F ^ F = False
  23.  
  24. (f) b or x != 0
  25. F v F = False
  26.  
  27. (g) not b and x != 0
  28. ¬F ^ F = T ^ F = False
  29.  
  30. (h) not b or x != 0
  31. ¬F v F = T v F = True
  32.  
  33.  
  34. 2. Write a while loop that prints: (4 points each)
  35. (a) all squares less than n. For example, if n is 100, print 0 1 4 9 16 25 36 49 64 81
  36. x = 0
  37. while (x**2) < n:
  38. print (x ** 2, end =" ")
  39. x += 1
  40.  
  41. (b) all positive numbers that are divisible by 10 and less than n.
  42. For example, if n is 100, print 10 20 30 40 50 60 70 80 90
  43. x = 1
  44. while (x < n):
  45. if (x%10 == 0):
  46. print(x, end = " ")
  47. x+=1
  48. else:
  49. x+=1
  50.  
  51. (c) all powers of two less than n. For example, if n is 100, print 1 2 4 8 16 32 64
  52. x = 0
  53. while (2**x) < n:
  54. print(2**x, end = " ")
  55. x += 1
  56.  
  57.  
  58. 3. What do these loops print? (1 point each)
  59. (a) for i in range(1, 10):
  60. print(i)
  61. 1
  62. 2
  63. 3
  64. 4
  65. 5
  66. 6
  67. 7
  68. 8
  69. 9
  70.  
  71. (b) for i in range (1, 10, 2):
  72. print(i)
  73. 1
  74. 3
  75. 5
  76. 7
  77. 9
  78.  
  79. (c) for i in range (10, 1, 1):
  80. print(i)
  81. [prints nothing]
  82.  
  83. (d) for i in range(10):
  84. print(i)
  85. 0
  86. 1
  87. 2
  88. 3
  89. 4
  90. 5
  91. 6
  92. 7
  93. 8
  94. 9
  95.  
  96. (e) for i in range(1,10):
  97. if i % 2 == 0:
  98. print(i)
  99. 2
  100. 4
  101. 6
  102. 8
  103.  
  104.  
  105. 4. What do the following loops print? Work out the asnwer by tracing the code, not by using the computer. (1 point each)
  106. (a) s = 1
  107. for n in range (1, 6):
  108. s = s + n
  109. print(s)
  110. 2
  111. 4
  112. 7
  113. 11
  114. 16
  115.  
  116. (b) s = 1
  117. for n in range(1,11):
  118. n = n + 2
  119. s = s + n
  120. print (s)
  121.  
  122. 4
  123. 8
  124. 13
  125. 19
  126. 26
  127. 34
  128. 43
  129. 53
  130. 64
  131. 76
  132.  
  133. (c) s = 1
  134. for n in range(1, 6): n = 1, 2, 3, 4, 5
  135. s = s + n
  136. n = n + 1
  137. print(s, n)
  138.  
  139. 16 6
  140.  
  141.  
  142.  
  143. 5. What do the following loops print? Work out the asnwer by tracing the code, not by using the computer (1 point each)
  144. (a) n = 2
  145. while n < 100:
  146. print(n)
  147. n = 2 * n
  148. print (n)
  149.  
  150. 2
  151. 4
  152. 8
  153. 16
  154. 32
  155. 64
  156. 128
  157.  
  158. (b) n = 2
  159. while n < 10:
  160. n = 2 * n
  161. print(n)
  162. print(n)
  163.  
  164. 4
  165. 8
  166. 16
  167. 16
  168.  
  169. (c) n = 1
  170. while n < 100:
  171. print(n)
  172. n = 10 * n
  173.  
  174. 1
  175. 10
  176.  
  177. (d) n = 1
  178. while n <= 100:
  179. print(n)
  180. n = 10 * n
  181.  
  182. 1
  183. 10
  184. 100
  185.  
  186. (e) n = 1
  187. while n >= 100:
  188. print(n)
  189. n = 10 * n
  190. print (n)
  191.  
  192. 1
  193.  
  194.  
  195. 6. Consider the following list a = [1, 2, 3, 4, 5, 4, 3, 2, 1, 0]. What are the ocntents of the list a after each of the following loops complete? (For each part, assume the list a contains the original list of values.) (1 point each)
  196. (a) for i in range (1, 10):
  197. a[i] = a[i-1]
  198.  
  199. a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  200.  
  201. (b) for i in range (9, 0, -1):
  202. a[i] = a[i-1]
  203.  
  204. a = [1, 1, 2, 3, 4, 5, 4, 3, 2, 1]
  205.  
  206. (c) for i in range(9):
  207. a[i] = a[i+1]
  208.  
  209. a = [2, 3, 4, 5, 4, 3, 2, 1, 0, 0]
  210.  
  211. (d) for i in range (8, -8, -1):
  212. a[i] = a[i+1]
  213.  
  214. a = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  215.  
  216. (e) for i in range (1, 10):
  217. a[i] = a[i] + a[i-1]
  218.  
  219. a = [1, 3, 6, 10, 15, 19, 22, 24, 25, 25]
  220.  
  221. (f) for i in range (1, 10, 2):
  222. a[i] = 0
  223.  
  224. a = [1, 0, 3, 0, 5, 0, 3, 0, 1, 0]
  225.  
  226. (g) for i in range(5):
  227. a[i + 5] = a[i]
  228.  
  229. a = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
  230.  
  231. (h) for i in range(1, 5):
  232. a[i] = a[9 - i]
  233.  
  234. a = [1, 1, 2, 3, 4, 4, 3, 2, 1, 0]
  235.  
  236.  
  237. 7. What is wrong with the following function that aims to fill a list with random numbers? (5 points)
  238.  
  239. def fillWithRandomNumbers(values):
  240. numbers = []
  241. for i in range(len(values)):
  242. numbers[i] = random.random()
  243. values = numbers
  244.  
  245. The function will not work because in the for loop, the function tries to overwrite what is in the list numbers by using indices. But, because numbers is assigned as an empty list, there aren't any indices. The for loop tries to put a random number at numbers[0], but there isnt anythin in numbers at index 0. So, the list assignment index is out of range. The function also doesn't return any values.
  246.  
  247.  
  248. 8. Write code to do the following tasks with lsits in Python. (1 point each)
  249. (a) Test that two lists contain the same elements in the same order.
  250. def compare(list1,list2):
  251. if len(list1) == len(list2):
  252. for i in range(len(list1)):
  253. if list1[i] == list2[i]:
  254. return True
  255. else:
  256. return False
  257. else:
  258. return False
  259.  
  260. (b) Copy one list to another.
  261. def copy(list1, list2):
  262. for i in range(len(list1)):
  263. list2.append(list1[i])
  264. return list2
  265.  
  266. (c) Fill a list with zeroes, overwriting all elements in it.
  267. def replace(list1):
  268. for i in range(len(list1)):
  269. list1[i] = 0
  270. return list1
  271.  
  272. (d) Remove all elements from a list.
  273. def remove(list1):
  274. for i in range(len(list1)-1, -1, -1):
  275. list1.remove(list1[i])
  276. return list1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement