Stack Overflow Asked on November 18, 2021
I have a code for finding the possible combinations of a given string in the list. But facing an issue with leading zero getting error like SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers . How to overcome this issue as I wanted to pass values with leading zeros(Cannot edit the values manually all the time). Below is my code
def permute_string(str):
if len(str) == 0:
return ['']
prev_list = permute_string(str[1:len(str)])
next_list = []
for i in range(0,len(prev_list)):
for j in range(0,len(str)):
new_str = prev_list[i][0:j]+str[0]+prev_list[i][j:len(str)-1]
if new_str not in next_list:
next_list.append(new_str)
return next_list
list = [129, 831 ,014]
length = len(list)
i = 0
# Iterating using while loop
while i < length:
a = list[i]
print(permute_string(str(a)))
i += 1;
Finally, I got my answer. Below is the working code.
def permute_string(str):
if len(str) == 0:
return ['']
prev_list = permute_string(str[1:len(str)])
next_list = []
for i in range(0,len(prev_list)):
for j in range(0,len(str)):
new_str = prev_list[i][0:j]+str[0]+prev_list[i][j:len(str)-1]
if new_str not in next_list:
next_list.append(new_str)
return next_list
#Number should not be with leading Zero
actual_list = '129, 831 ,054, 845,376,970,074,345,175,965,068,287,164,230,250,983,064'
list = actual_list.split(',')
length = len(list)
i = 0
# Iterating using while loop
while i < length:
a = list[i]
print(permute_string(str(a)))
i += 1;
Answered by Midhun on November 18, 2021
Integer literals starting with 0 are illegal in python (other than zero itself, obviously), because of ambiguity. An integer literal starting with 0 has the following character which determines what number system it falls into: 0x
for hex, 0o
for octal, 0b
for binary.
As for the integers themselves, they're just numbers, and numbers will never begin with zero. If you have an integer represented as a string, and it happens to have a leading zero, then it'll get ignored when you turn it into an integer:
>>> print(int('014'))
14
Given what you're trying to do here, I'd just rewrite the list's initial definition:
lst = ['129', '831', '014']
...
while i < length:
a = lst[i]
print(permute_string(a)) # a is already a string, so no need to cast to str()
or, if you need lst
to be a list of integers, specifically, then you can change how you convert them to strings by using a format-string literal, instead of the str()
call, which allows you to pad the number with zeros:
lst = [129, 831, 14]
...
while i < length:
a = lst[i]
print(permute_string(f'{a:03}')) # three characters wide, add leading 0 if necessary
throughout this answer, I use lst
as the variable name, instead of the list
in your question. You shouldn't use list
as a variable name, because it's also the keyword that represents the built-in list datastructure, and if you name a variable that then you can no longer use the keyword.
Answered by Green Cloak Guy on November 18, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP