Adding commas to my integer

Sairam Penjarla
1 min readJun 28, 2021

I have a very large integer value such as 2874. I wrote a function that can return me a list containing all possible outcomes if I start placing commas between this large integer value. How can i reduce the complexity of this function

Input:

2874

Output:

[[2874],[2,874],[28,74],[287,4],[2,8,74],[2,87,4],[28,7,4],[2,8,7,4]]

Explanation:

#zero commas
[2874]
#one comma
[2,874]
[28,74]
[287,4]
#two commas
[2,8,74]
[2,87,4]
[28,7,4]
#three commas
[2,8,7,4]

This is my code:

a=2874def splitter(a):
empty_list = []
temp = str(a)
if len(temp) ==1:
return [[temp]]
else:
j=1
for i in range(len(temp)-1):
res = [temp[:j], temp[j:]]
empty_list.append(res)
j+=1
return empty_list
def add_commas(a):
final_list = [[str(a)]]
for i in splitter(a):
final_list.append(i)
level=1
n = 0
for z in range(2):
for m in range(len(final_list) - 1):
m+=n
temp = final_list[m + 1]
unchanged = temp[:level]
changed = temp[level:]
if len(changed)>0 and len(temp)!= len(str(a)):
after_part = splitter(changed[0])
for i in after_part:
res = unchanged + i
final_list.append(res)
n += 3
level += 1
unique_list = []
for x in final_list:
x = [int(i) for i in x]
if x not in unique_list:
unique_list.append(x)
return unique_list
print(add_commas(a))

--

--

Sairam Penjarla

Looking for my next opportunity to make change in a BIG way