-
Notifications
You must be signed in to change notification settings - Fork 1
/
digit-operations.py
55 lines (37 loc) · 948 Bytes
/
digit-operations.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
"""
Operations involving digits of an integer
"""
import sys
def sum_of_digits(number):
total = 0
# For loop
# for num in str(number):
# total += int(num)
# Using map
# for num in map(int, str(number)):
# total += num
# Using list comprehension
for num in [int(i) for i in str(number)]:
total += num
return total
def product_of_digits(number):
product = 1
for num in str(number):
product *= int(num)
return product
def largest_digit(number):
largest = -sys.maxsize
for num in str(number):
if int(num) > largest:
largest = int(num)
return largest
def smallest_digit(number):
smallest = sys.maxsize
for num in str(number):
if int(num) < smallest:
smallest = int(num)
return smallest
print(sum_of_digits(456))
print(product_of_digits(456))
print(largest_digit(456))
print(smallest_digit(456))