+ 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..
14 Respuestas
+ 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 !
+ 5
I would try like this:
The problem seems to be solvable with an fsm.
https://code.sololearn.com/crytnLLYFagv
maybe this works.
+ 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?
+ 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))
+ 2
a=pop(l) takes the last entry from list l and assignes it to a.
+ 1
Split at "["
First part is to be converted to int.
+ 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))
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
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 ...
0
Frogged can you clear it up a little more ? What do you mean by "split at" ?
0
Frogged Super , danke 😊
0
Josh Greig thank you so much👍