+ 1

I had to make a condition that the input sequence ends in a character (0) so that the program knows when to end. Avoidable?

https://code.sololearn.com/c3wo4IkKG96G/?ref=app

8th Apr 2018, 11:26 PM
db1070
db1070 - avatar
8 Answers
+ 3
You can do len(input) and do it without 0. Of course it is needed to rebuild this sample of code.
8th Apr 2018, 11:39 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
+ 2
The nested while is not working well. It want to have access to not existing element of string. You have to check length == 0 first (before getting to index of array). The if/while statements works from left to right: on the first statement it causes error and not check second. If you change them then it first check the length and later try check the index of array.
9th Apr 2018, 12:15 AM
Bartosz Pieszko
Bartosz Pieszko - avatar
+ 2
thanks
9th Apr 2018, 12:15 AM
db1070
db1070 - avatar
+ 2
By the way try to code: a != b Instead of not a == b
9th Apr 2018, 12:19 AM
Bartosz Pieszko
Bartosz Pieszko - avatar
+ 2
thanks a lot! I got it working!
9th Apr 2018, 12:24 AM
db1070
db1070 - avatar
+ 1
How it works: aaabbc0 becomes a3b2c
8th Apr 2018, 11:27 PM
db1070
db1070 - avatar
+ 1
input = input() #input any sequence of letters length = len(input) #initialize length print(length) adress = 0 #intialize adress compressed = str() #intialize compressed while not length == 0: #end loop when 0 letter = input[adress] #letter to check if it repeats compressed += input[adress] #add letter to output nletters = 0 #reset number of consecutive letters while input[adress] == letter: #loop for each consecutive letter adress += 1 #increment character of string read nletters += 1 #increment number of consecutive letters length -= 1 #decrement length print(length) if not nletters == 1: #skip add number of consecutive letters if only one letter nletters = str(nletters) compressed += nletters #add number of consecutive letters to output print(compressed) #print output
9th Apr 2018, 12:06 AM
db1070
db1070 - avatar
+ 1
thanks! ok so why isn't this working?
9th Apr 2018, 12:07 AM
db1070
db1070 - avatar