+ 5

Compress text

The code blew secssed to perform compress to taxt but the output print repeated char how can i fixed i word=input() string="" new_string="" count=0 for i in (word): if word.count(i)>1: B=i+str(word.count(i)) string=string+B else: k=1 n=1 for j in range (len(word)): if word.count(word[j])==1: A=word[j]+str(1) new_string=new_string+A else: o=1 print(string+new_string)

31st Dec 2023, 7:44 AM
Mohammed Hassan
Mohammed Hassan - avatar
15 Respuestas
+ 4
Mohammed Hassan Unfortunately your code don‘t work really good and has different results as required. For example on input abbccc your code outputs b2c3a1 and this is not correct. The right output here should be a1b2c3.
31st Dec 2023, 2:43 PM
JaScript
JaScript - avatar
+ 3
Mohammed Hassan Here, I've fixed that, now it gives the expected output. https://sololearn.com/compiler-playground/c7kC1tmJN90h/?ref=app
31st Dec 2023, 2:04 PM
卂ㄚㄩ丂卄
卂ㄚㄩ丂卄 - avatar
+ 3
卂ㄚㄩ丂卄, JaScript Thanks i fixed with different way word=input() string="" new_string="" count=1 for i in (word): if word.count(i)>1: B=i+str(word.count(i)) string=string+B else: k=1 n=1 for j in range (len(word)): if word.count(word[j])==1: A=word[j]+str(1) new_string=new_string+A else: o=1 Q=(string+new_string) list=[] for char in Q: if char not in list: list.append(char) st="".join(list) print(st)
31st Dec 2023, 2:25 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 2
Mohammed Hassan , Without giving you the whole code, I will say that word.count(i) gives the total of all occurrences of i in word, even if they are not next to each other. For example, it cannot distinguish meet from mete. >>> "meet".count("e") 2 >>> "mete".count("e") 2 For compression, I think you need to count repeated letters that are next to each other. Also, even though it's safe to have a variable named count and a method named count, it makes it harder to read.
31st Dec 2023, 11:29 AM
Rain
Rain - avatar
+ 2
Mohammed Hassan, 卂ㄚㄩ丂卄 My solution: code and decode with 2 variations. (string can include numerals or string cannot include numerals): https://sololearn.com/compiler-playground/cp6cY8KXUa8U/?ref=app
1st Jan 2024, 4:45 AM
Bob_Li
Bob_Li - avatar
+ 1
JaScript i think will appera an error
31st Dec 2023, 8:49 AM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
卂ㄚㄩ丂卄 the output still the same Input: kkkkkkbbbb The current output is :k6k6k6k6k6k6b4b4b4b4 The output must be :k6b4
31st Dec 2023, 9:08 AM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
Do you know about dict? Dict works better than list in this problem.
31st Dec 2023, 10:33 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Rain I think after that use while loop For condition while
31st Dec 2023, 11:55 AM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
JaScript my code cost 25 line but . Give the same result
31st Dec 2023, 2:34 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
You know Because the i make combination between two strings JaScript
31st Dec 2023, 2:46 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
JaScript word=input() I fixed string="" new_string="" count=1 for i in (word): if word.count(i)>1: i+str(word.count(i)) string=string+i+str(word.count(i)) new_string=new_string+string else: i+str(1) string=string+i+str(1) new_string=new_string+string list=[] for char in new_string: if char not in list: list.append(char) st="".join(list) print(st)
31st Dec 2023, 3:07 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
Assume you take it as a challenge only use list but no other functions, here's my solution. https://sololearn.com/compiler-playground/cpG5UpG424RW/?ref=app
1st Jan 2024, 7:49 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Mohammed Hassan , Are you trying to do the opposite of this Code Coach? Text Decompressor https://www.sololearn.com/coach/85?ref=app
1st Jan 2024, 1:39 PM
Rain
Rain - avatar