Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do this two ways. Either with strings, or with integers.</p> <pre><code>aUserNum = [] # Make testing easier debug = True if debug: aUserNum = [55, 3303, 565, 55665, 565789] else: for i in range(10): iNum = int(input("Please enter your number: ")) aUserNum.append(iNum) </code></pre> <p>With strings, we turn all of the integers into a big string, and then count how many occurances of '0' there are, and then how many '1's there are, etc.</p> <pre><code>def string_count(nums): # Make a long string with all the numbers stuck together s = ''.join(map(str, nums)) # Make all of the digits into strings n = ''.join(map(str, range(10))) aDigits = [0,0,0,0,0,0,0,0,0,0] for i, x in enumerate(n): aDigits[i] = s.count(x) return aDigits </code></pre> <p>With integers we can use the lovely trick of integer division. This code is written for Python 2.7, and won't work on 3.x because of the "assume float" change. To get around that change the <code>x /= 10</code> to <code>x //= 10</code> and change the print statements to print functions.</p> <pre><code>def num_count(nums): aDigits = [0,0,0,0,0,0,0,0,0,0] for x in nums: while x: # Add a count for the digit in the ones place aDigits[x % 10] += 1 # Then chop off the ones place, until integer division results in 0 # and the loop ends x /= 10 return aDigits </code></pre> <p>These output the same. </p> <pre><code>print string_count(aUserNum) print num_count(aUserNum) # [1, 0, 0, 3, 0, 9, 4, 1, 1, 1] </code></pre> <p>For prettier output, write it like this.</p> <pre><code>print list(enumerate(string_count(aUserNum))) print list(enumerate(num_count(aUserNum))) # [(0, 1), (1, 0), (2, 0), (3, 3), (4, 0), (5, 9), (6, 4), (7, 1), (8, 1), (9, 1)] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload