Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Homework 2
- Derek Moy
- CS 110-03
- 10/23/2017
- ID: 20397155
- 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)
- (a) b and x == 0
- F ^ T = False
- (b) b or x == 0
- F v T = True
- (c) not b and x == 0
- ¬F ^ T = T ^ T = True
- (d) not b or x == 0
- ¬F v T = T v T = True
- (e) b and x != 0
- F ^ F = False
- (f) b or x != 0
- F v F = False
- (g) not b and x != 0
- ¬F ^ F = T ^ F = False
- (h) not b or x != 0
- ¬F v F = T v F = True
- 2. Write a while loop that prints: (4 points each)
- (a) all squares less than n. For example, if n is 100, print 0 1 4 9 16 25 36 49 64 81
- x = 0
- while (x**2) < n:
- print (x ** 2, end =" ")
- x += 1
- (b) all positive numbers that are divisible by 10 and less than n.
- For example, if n is 100, print 10 20 30 40 50 60 70 80 90
- x = 1
- while (x < n):
- if (x%10 == 0):
- print(x, end = " ")
- x+=1
- else:
- x+=1
- (c) all powers of two less than n. For example, if n is 100, print 1 2 4 8 16 32 64
- x = 0
- while (2**x) < n:
- print(2**x, end = " ")
- x += 1
- 3. What do these loops print? (1 point each)
- (a) for i in range(1, 10):
- print(i)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- (b) for i in range (1, 10, 2):
- print(i)
- 1
- 3
- 5
- 7
- 9
- (c) for i in range (10, 1, 1):
- print(i)
- [prints nothing]
- (d) for i in range(10):
- print(i)
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- (e) for i in range(1,10):
- if i % 2 == 0:
- print(i)
- 2
- 4
- 6
- 8
- 4. What do the following loops print? Work out the asnwer by tracing the code, not by using the computer. (1 point each)
- (a) s = 1
- for n in range (1, 6):
- s = s + n
- print(s)
- 2
- 4
- 7
- 11
- 16
- (b) s = 1
- for n in range(1,11):
- n = n + 2
- s = s + n
- print (s)
- 4
- 8
- 13
- 19
- 26
- 34
- 43
- 53
- 64
- 76
- (c) s = 1
- for n in range(1, 6): n = 1, 2, 3, 4, 5
- s = s + n
- n = n + 1
- print(s, n)
- 16 6
- 5. What do the following loops print? Work out the asnwer by tracing the code, not by using the computer (1 point each)
- (a) n = 2
- while n < 100:
- print(n)
- n = 2 * n
- print (n)
- 2
- 4
- 8
- 16
- 32
- 64
- 128
- (b) n = 2
- while n < 10:
- n = 2 * n
- print(n)
- print(n)
- 4
- 8
- 16
- 16
- (c) n = 1
- while n < 100:
- print(n)
- n = 10 * n
- 1
- 10
- (d) n = 1
- while n <= 100:
- print(n)
- n = 10 * n
- 1
- 10
- 100
- (e) n = 1
- while n >= 100:
- print(n)
- n = 10 * n
- print (n)
- 1
- 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)
- (a) for i in range (1, 10):
- a[i] = a[i-1]
- a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
- (b) for i in range (9, 0, -1):
- a[i] = a[i-1]
- a = [1, 1, 2, 3, 4, 5, 4, 3, 2, 1]
- (c) for i in range(9):
- a[i] = a[i+1]
- a = [2, 3, 4, 5, 4, 3, 2, 1, 0, 0]
- (d) for i in range (8, -8, -1):
- a[i] = a[i+1]
- a = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- (e) for i in range (1, 10):
- a[i] = a[i] + a[i-1]
- a = [1, 3, 6, 10, 15, 19, 22, 24, 25, 25]
- (f) for i in range (1, 10, 2):
- a[i] = 0
- a = [1, 0, 3, 0, 5, 0, 3, 0, 1, 0]
- (g) for i in range(5):
- a[i + 5] = a[i]
- a = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
- (h) for i in range(1, 5):
- a[i] = a[9 - i]
- a = [1, 1, 2, 3, 4, 4, 3, 2, 1, 0]
- 7. What is wrong with the following function that aims to fill a list with random numbers? (5 points)
- def fillWithRandomNumbers(values):
- numbers = []
- for i in range(len(values)):
- numbers[i] = random.random()
- values = numbers
- 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.
- 8. Write code to do the following tasks with lsits in Python. (1 point each)
- (a) Test that two lists contain the same elements in the same order.
- def compare(list1,list2):
- if len(list1) == len(list2):
- for i in range(len(list1)):
- if list1[i] == list2[i]:
- return True
- else:
- return False
- else:
- return False
- (b) Copy one list to another.
- def copy(list1, list2):
- for i in range(len(list1)):
- list2.append(list1[i])
- return list2
- (c) Fill a list with zeroes, overwriting all elements in it.
- def replace(list1):
- for i in range(len(list1)):
- list1[i] = 0
- return list1
- (d) Remove all elements from a list.
- def remove(list1):
- for i in range(len(list1)-1, -1, -1):
- list1.remove(list1[i])
- return list1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement