Last active
November 27, 2019 08:49
-
-
Save cowbert/75a904fd04764fc75138ba3bf2773df8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def countAllList(lst): | |
# define the list of accumulators: | |
counts = [0]*10 | |
# counts == [0,0,0...0] length of 10 | |
# iterate over lst to process each string at a time | |
for s in lst: | |
# lets turn the string into a list of letters | |
s_list = list(s) | |
# now count the number of occurances for each digit | |
# instead of typing out each digit lets have python | |
# do it for us by looping! | |
for i in range(10): | |
# this loops i from 0 to 9 | |
# str(i) turns the number into a string | |
counts[i] += s_list.count(str(i)) | |
# since when i == 0, we can kill 2 birds with 1 stone and refer to | |
# counts[i] as the first element of counts. | |
# At the same time we count the occurance of str(i) | |
# in our listified string | |
# increment i and repeat (i==1, add the result of s_list.count('1') to counts[1] ) | |
return counts | |
lres = countAllList(["111","2222","009"]) | |
print(lres) | |
assert countAllList(["111","2222","009"]) == [2, 3, 4, 0, 0, 0, 0, 0, 0, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment