-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_List.py
69 lines (50 loc) · 2.09 KB
/
update_List.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Great! We now want to create a new list that contains the counts
# of all occurrances of every number seen in the randomly generated
# list. That means we want counts of all occurrences of all numbers
# from 0 through 10 in our randomly generated list.
# Let's store our counts in a list of length 11
# with zeros filled in.
# We can multiply a list construct to create a list with the same
# elements n number of times.
count_list = [0] * 11
# Check that we have a list of length 11 with all 0 elements
print count_list
# We use this list to store our count of numbers 0 to 10 - take note
# that total numbers 0 to 10 is 11. We can use the index number of
# each element to refer to the count of our target
# number. Our target number is actually the index of the list.
# For example, assume count_list looks like this:
count_list = [1,2,3,2,2,1,1,2,3,1,2]
# Let's print out the occurrences for the numbers 0, 4, 5, and 6
print count_list[0]
print count_list[4]
print count_list[5]
print count_list[6]
# Therefore, for our output, we want a count_list that looks like:
# [1,2,3,2,2,1,1,2,3,1,2]
# Here's our code that we coded before
import random
# Create random list of integers using while loop --------------------
random_list = []
list_length = 20
while len(random_list) < list_length:
random_list.append(random.randint(0,10))
# --------------------------------------------------------------------
# Initialize count_list for every integer between 0 and 10.
# A number will correspond to an index of this count_list
# Therefore if we see that there are 3 occurrences of the number 4,
# we assign count_list[4] = 3, if there are 5 occurrences of the
# number 6, we assign count_list[6] = 5
count_list = [0] * 11
index = 0
# Write code here to loop through every number in random_list and
# update count_list appropriately
while index<len(random_list):
number=random_list[index]
count_list[number]=count_list[number]+1
index+=1
# Check the list we created
print count_list
# If we coded everything correctly, the sum of all of the numbers
# in count_list should be 20
print sum(count_list)