-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanced number inefficient code
More file actions
47 lines (38 loc) · 1.26 KB
/
Balanced number inefficient code
File metadata and controls
47 lines (38 loc) · 1.26 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
def balanced_num(number):
odd = False
if len(str(number)) % 2 == 0:
odd = False
else:
odd = True
if odd:
number_list = []
for i in str(number):
number_list.append(i)
length = (len(str(number)) -1) // 2
odd_middle_num = number_list[length]
first_part = number_list[:length]
second_part = number_list[length+1:]
for i in range(len(first_part)):
first_part[i] = int(first_part[i])
second_part[i] = int(second_part[i])
if sum(first_part) == sum(second_part):
return "Balanced"
else:
return "Not Balanced"
elif not odd:
number_list = []
for i in str(number):
number_list.append(i)
length = ((len(str(number))) // 2) - 1
# odd_middle_num = number_list[length], number_list[length+1]
first_part = number_list[:length]
second_part = number_list[length + 2:]
for i in range(len(first_part)):
first_part[i] = int(first_part[i])
second_part[i] = int(second_part[i])
if sum(first_part) == sum(second_part):
return "Balanced"
else:
return "Not Balanced"
number = 2232
print(balanced_num(number))