Repository navigation
Expand file tree
/
Copy pathexpanded_form.py
More file actions
52 lines (39 loc) · 1.73 KB
/
Copy pathexpanded_form.py
File metadata and controls
52 lines (39 loc) · 1.73 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 Number in Expanded Form
You will be given a number and you will need to return it as a string in Expanded Form. For example:
expanded_form(12) # Should return '10 + 2'
expanded_form(42) # Should return '40 + 2'
expanded_form(70304) # Should return '70000 + 300 + 4'
NOTE: All numbers will be whole numbers greater than 0.
def expanded_form(num):
pass
"""
# expanded_form(70304) # Should return '70000 + 300 + 4'
def expanded_form(num):
if num == 0:
return '0' # Si el número es cero, la forma es simplemente '0'
num_str = str(num) # Convertir el número en una cadena para trabajar con los dígitos
result = [] # Lista para almacenar los términos
for i, digit in enumerate(num_str):
if digit != '0': # Solo considera los dígitos diferentes de cero
# Crear un término multiplicando el dígito por la potencia de 10 correspondiente a su posición
term = digit + '0' * (len(num_str) - i - 1)
result.append(term) # Agregar el término a la lista de resultados
return ' + '.join(result) # Unir los términos con ' + ' para obtener la forma expandida
# Pruebas
def test_expanded_form():
assert expanded_form(12) == '10 + 2'
assert expanded_form(42) == '40 + 2'
assert expanded_form(70304) == '70000 + 300 + 4'
assert expanded_form(5000) == '5000'
assert expanded_form(0) == '0'
print("Todas las pruebas pasaron con éxito!")
# Prueba no pasa -> AssertionError
test_expanded_form()
def test_expanded_form():
assert expanded_form(12) == '10 + 2'
assert expanded_form(42) == '40 + 2'
assert expanded_form(70304) == '70000 + 300 + 4'
assert expanded_form(5000) == '5000'
assert expanded_form(0) == '0'
test_expanded_form()