+ 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)
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.
+ 3
Mohammed Hassan Here, I've fixed that, now it gives the expected output.
https://sololearn.com/compiler-playground/c7kC1tmJN90h/?ref=app
+ 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)
+ 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.
+ 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
+ 1
JaScript i think will appera an error
+ 1
卂ㄚㄩ丂卄 the output still the same
Input: kkkkkkbbbb
The current output is :k6k6k6k6k6k6b4b4b4b4
The output must be :k6b4
+ 1
Do you know about dict?
Dict works better than list in this problem.
+ 1
Rain
I think after that use while loop
For condition while
+ 1
JaScript my code cost 25 line but .
Give the same result
+ 1
You know
Because the i make combination between two strings
JaScript
+ 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)
+ 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
+ 1
Mohammed Hassan ,
Are you trying to do the opposite of this Code Coach?
Text Decompressor
https://www.sololearn.com/coach/85?ref=app