+ 2

Logically true , but python does not accept it !

Hi friends , plz help to find out what is wrong with my code .. https://pastebin.ubuntu.com/p/gk5NfJkS59/ Suppose that the input is 3[abc] , then y = int(encoded[open[op_sz]-1]) is logically equal to 3 . I want to convert to an integer but a strange error rises , that is "inavlid literal for int() with base 10: 'a' ... I think it is actually converting character 'a' to an int , instead of number 3 ... why?? It shouldn't work like this logically..

16th May 2021, 6:06 PM
Ali_combination
Ali_combination - avatar
14 Antworten
+ 5
Ali_combination , please post your code here, and also give a clear task description. otherwise we will spend too much time in guessing what you try to achieve thanks !
16th May 2021, 8:18 PM
Lothar
Lothar - avatar
+ 5
I would try like this: The problem seems to be solvable with an fsm. https://code.sololearn.com/crytnLLYFagv maybe this works.
17th May 2021, 2:39 PM
Oma Falk
Oma Falk - avatar
+ 3
It is hard to say much more than "yes, I see the errors" when I don't know the broader requirements you're trying to satisfy. What are you hoping to have for inputs and outputs? Can you give examples? Is "3[abc]" an expected input? If yes, what do you want as an output if the input was "3[abc]"? Can you give more examples of input and output pairs?
16th May 2021, 8:09 PM
Josh Greig
Josh Greig - avatar
+ 3
If you know the encoded tokens are surrounded by regular spaces instead of punctuation marks and you know the bracketed strings don't contain spaces, you could do something simpler like this: def processEncodedToken(token): index = token.find('[') repetition_count = int(token[:index]) string_part = token[index + 1:-1] return string_part * repetition_count encoded = input() result_tokens = [] for token in encoded.split(' '): if '[' in token and ']' in token: result_tokens.append(processEncodedToken(token)) else: result_tokens.append(token) print(' '.join(result_tokens))
17th May 2021, 2:49 PM
Josh Greig
Josh Greig - avatar
+ 2
a=pop(l) takes the last entry from list l and assignes it to a.
17th May 2021, 4:05 PM
Oma Falk
Oma Falk - avatar
+ 1
Split at "[" First part is to be converted to int.
17th May 2021, 6:39 AM
Oma Falk
Oma Falk - avatar
+ 1
If you expect inputs to be a mixture of your encoded tokens and regular strings such as "2[yo], hey man. How are 5[ya?]", you could do something like this: import re def processEncodedToken(token): index = token.find('[') repetition_count = int(token[:index]) string_part = token[index + 1:-1] return string_part * repetition_count encoded = input() encoded_token_pattern = '\\d+\\[[^\\]]*\\]' match_obj=re.finditer(encoded_token_pattern, encoded) result_tokens = [] last_processed_index = 0 for match in match_obj: start_index = match.span()[0] if start_index > last_processed_index: result_tokens.append(encoded[last_processed_index:start_index]) result_tokens.append(processEncodedToken(match.group())) last_processed_index = match.span()[1] if last_processed_index < len(encoded): result_tokens.append(encoded[last_processed_index:]) print(''.join(result_tokens))
17th May 2021, 2:44 PM
Josh Greig
Josh Greig - avatar
0
Lothar Let's say k[□] represents repeating a string in the brackets for k times . For instance , 3[abc] = abcabcabc , 2[hijack3[cop]]=2[hijcakcopcopcop] = hijcakcopcopcophijcakcopcopcop
17th May 2021, 6:17 AM
Ali_combination
Ali_combination - avatar
0
Josh Greig Im trying to create a program to decode an encoded string . An encoded string is the one that includes some open and close brackets with a string in them , and a number behind each open bracket ... so 3[abc] is equal to abcabcabc . The problem is that vraiable y does not convert 3 to an int ... it is actually trying to convert 'a' to an int . However , I use VSCode community compiler and the error might be different , compared to other compilers ...
17th May 2021, 6:23 AM
Ali_combination
Ali_combination - avatar
0
Frogged can you clear it up a little more ? What do you mean by "split at" ?
17th May 2021, 8:19 AM
Ali_combination
Ali_combination - avatar
0
Frogged thank you Frogged , of course I just discovered the main bug , the condition of while loop should be 1<=op_sz ... Im not familiar to python errors and proper way of writing code in python , thats why im dealing with such simple problems..
17th May 2021, 2:05 PM
Ali_combination
Ali_combination - avatar
0
Frogged Bingo ! amazing gentleman😉! Thanks for your help , and plz let me know how pop() works ... am still a newbie in Python .. is it related to data structures ? Like C++ ... (by the way , my program does now always work properly , exactly when the input includes nested encoded strings..)
17th May 2021, 4:04 PM
Ali_combination
Ali_combination - avatar
0
Frogged Super , danke 😊
17th May 2021, 4:08 PM
Ali_combination
Ali_combination - avatar
0
Josh Greig thank you so much👍
17th May 2021, 4:10 PM
Ali_combination
Ali_combination - avatar