compress text | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

compress text

How can make code to decompress text like this e.g =k2b2 out put=kkbb

25th Dec 2023, 2:11 PM
Mohammed Hassan
Mohammed Hassan - avatar
15 Answers
+ 8
Mohammed Hassan , this is my description: (we need 2 lines of code only + the input procedure) > use a for loop that generates numbers starting from 0 upto n => length of input string. to do so, we can use *range()* to get the indexes we need. arguments for range(): > first argument is 0 (indexes) > second argument is length of input string > third argument is the step-width, we use *2*, so we will get each 2nd character from input string inside the for loop we can do the processing and output: use print with these *3 arguments*: > inp[ind] => is the character like `k` at index > inp[ind+1] => is the number like `2` of repetitions of the character, has to be converted to int > end='' => ensures that the output is done in the same line inside the print function we ca now *multiply* the character by the corresponding count. do a try by yourself. if you get stuck, post your code here.
25th Dec 2023, 4:37 PM
Lothar
Lothar - avatar
+ 4
Mohammed Hassan , 2 nested loops will not work as expected. > we have to do the task in single *for loop*
25th Dec 2023, 7:20 PM
Lothar
Lothar - avatar
+ 3
Try to make it yourself Tip: for example you can take the characters 2 by 2 (lets calls the character "c" and the number "n"). Next you can print the character "c" "n" times
25th Dec 2023, 2:42 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 2
Mohammed Hassan not really. Just make a loop and you'll be able to read all the String. its easy! whats your problem???? show your attempt to solve the problem!
25th Dec 2023, 4:24 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 2
Thanks Lothar You know the my brain thinking for this task currently 2 for loops One for char The second of number found in word
25th Dec 2023, 4:44 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
Ugulberto Sánchez this simple but problem when became full word
25th Dec 2023, 3:18 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
Mohammed Hassan , Clarification needed. Your example shows a repeating pattern of one letter and one digit, but your reply says "problem when became full word". Show examples of all the different types of input you expect. Do you need to process something like this? "rabbit999frog1" Can the input encode spaces? Punctuation? "Hello1,1 1World1!1"
27th Dec 2023, 4:55 AM
Rain
Rain - avatar
+ 1
Rain There is different between that found in code couch This is compress text That challenge is Decompress text
1st Jan 2024, 1:56 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
Rain 😂😂😂😂😂😂😂😂😂😅
1st Jan 2024, 2:02 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
Rain bro it is mistake Yes i change the mistake not for lying but to be correct!
1st Jan 2024, 3:31 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
Mohammed Hassan , After I learned it is an existing Code Coach, I solved it. Here's my solution. There can be other solutions too. inp = input() for i in range(0, len(inp), 2): print(inp[i] * int(inp[i+1]))
1st Jan 2024, 3:40 PM
Rain
Rain - avatar
0
Mohammed Hassan , Are you trying to solve this Code Coach? You should just say that from the beginning and share the link. https://www.sololearn.com/coach/85?ref=app
1st Jan 2024, 1:37 PM
Rain
Rain - avatar
0
Mohammed Hassan , Read your thread title above!
1st Jan 2024, 1:57 PM
Rain
Rain - avatar
0
Mohammed Hassan , Now you edited the title from "decompress text" to "compress text", but your question body is still how to decompress, just like the title was. Don't be a liar. You have two threads. One for compress and one for decompress. This one we are talking in right now is for decompress text. And this one linked below is for compress text. https://www.sololearn.com/Discuss/3258686/?ref=app
1st Jan 2024, 2:06 PM
Rain
Rain - avatar
0
Mohammed Hassan , I think you are confused which thread you're writing in. You should edit the title back to the original "decompress text" to match your question body: "How can make code to decompress text like this e.g =k2b2 out put=kkbb"
1st Jan 2024, 3:34 PM
Rain
Rain - avatar