-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigitCount.py
More file actions
51 lines (47 loc) · 1.63 KB
/
Copy pathDigitCount.py
File metadata and controls
51 lines (47 loc) · 1.63 KB
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
# Write a function called digit_count. digit_count should
# take as input a number, which could be either a float or an
# integer. It should return a dictionary whose keys are digits,
# and whose values are the number of times that digit appears
# in the number.
#
# The dictionary should NOT contain any numerals that do not
# occur at all in the number, and it should also note contain
# the decimal point character if the number is a decimal.
#
# For example:
#
# digit_count(11223) -> {1: 2, 2: 2, 3: 1}
# digit_count(3.14159) -> {3: 1, 1: 2, 4: 1, 5: 1, 9: 1}
#
# Hint: You should probably convert the number to a string to
# count the digits, but convert the individual digits back to
# integers to use as keys to the dictionary.
# Write your function here!
def digit_count(num):
num_dict = {}
num_list = []
num = str(num)
num = num.replace(".", "")
num = num.replace("", " ")
num_list = [char for char in num]
num_list = num_list[1::]
num_list = num_list[:-1:]
new_list = [ele for ele in num_list if ele.strip()]
for item in new_list:
item = int(item)
if item in num_dict:
num_dict[item] += 1
else:
num_dict[item] = 1
return num_dict
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print (although the order of the keys may vary):
#
# {1: 2, 2: 2, 3: 1}
# {3: 1, 1: 2, 4: 1, 5: 1, 9: 1}
print(digit_count(11223))
print(digit_count(3.14159))