"A book to train programming skills to fight in the world" Python code Solution example --1.6 String compression

"A book to train programming skills to fight in the world" Python code Solution example --1.6 String compression

table of contents

CHAP1. Arrays and strings

  1. Unique string
  2. Count the number of the same characters
  3. URLify
  4. Palindrome permutation
  5. One-shot conversion
  6. String compression
  7. Rotate matrix
  8. Matrix of "0"
  9. Rotate string

Python code answer example

def compressBad(input_str):

    compressedString = ""
    countConsecutive = 0

    for i in range(len(input_str)):

        countConsecutive = countConsecutive + 1

        if i+1 >= len(input_str) or input_str[i] != input_str[i+1]:

            compressedString = compressedString + input_str[i] + str(countConsecutive)
            countConsecutive = 0

    return compressedString if len(compressedString) < len(input_str) else input_str

input_str = "aabcccccaaa"

print(compressBad(input_str))

Recommended Posts

"A book to train programming skills to fight in the world" Python code Solution example --1.6 String compression
"A book to train programming skills to fight in the world" Python code solution example --1.5 One-shot conversion
"A book to train programming skills to fight in the world" Python code Solution example --1.7 Matrix rotation
"A book to train programming skills to fight in the world" Python code Solution example --2.8 Loop detection
"A book to train programming skills to fight in the world" Python code answer example --1.1 Duplicate character string
"A book to train programming skills to fight in the world" Python code answer example --1.8 "0" matrix
"A book to train programming skills to fight in the world" Python code answer example --3.1 Three stacks
"Book to train programming skills to fight in the world" Python code solution example --- Removed elements between 2.3
"Book to train programming skills to fight in the world" Python code Solution example --2.1 Remove duplicate elements
"Book to train programming skills to fight in the world" Python code answer example --1.3 URLify
"Book to train programming skills to fight in the world" Python code answer example --2.6 palindrome
"A book to train programming skills to fight in the world" Python code answer example --1.9 Rotation of strings
"A book to train programming skills to fight in the world" Python code Solution example --2.5 The sum of two numbers shown in the list
"Book to train programming skills to fight in the world" Python code answer example --2.7 intersecting nodes
"A book to train programming skills to fight in the world" Python code answer example --2.2 Returns the Kth from the back
"Book to train programming skills to fight in the world" Python code answer example --1.4 Permutation of sentences
"A book to train programming skills to fight in the world" Python code answer example --1.2 Count the number of the same characters
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
Programming to fight in the world ~ 5-2
Programming to fight in the world-Chapter 4
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
[Kenchon book to Python] "Train your problem-solving skills! Algorithms and data structures" I tried to rewrite the posted code in Python! -table of contents-
How to embed a variable in a python string
A solution to the problem that the Python version in Conda cannot be changed
I searched for the skills needed to become a web engineer in Python
[Kenchon book to Python]-Chapter 3- "Train your problem-solving skills! Algorithms and data structures" I rewrote the posted code to Python!
[Kenchon book to Python]-Chapter 2- "Train your problem-solving skills! Algorithms and data structures" I rewrote the posted code to Python!
[Kenchon book to Python]-Chapter 4-"Train your problem-solving skills! Algorithms and data structures" I rewrote the posted code to Python!
Run the output code with tkinter, saying "A, pretending to be B" in python
Check if the string is a number in python
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
Try to solve the programming challenge book with python3
[Python] How to expand variables in a character string
How to quickly count the frequency of appearance of characters from a character string in Python?
A Python program in "A book that gently teaches difficult programming"
Find out the apparent width of a string in python
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
How to get the last (last) value in a list in Python
Run the output code on the local web server as "A, pretending to be B" in python
In the python metaclass, pass the comparison operator as a string to the ORM method to assemble the where clause.
How to determine the existence of a selenium element in Python
How to make a string into an array or an array into a string in Python
[Introduction to Python] How to split a character string with the split function
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
[Introduction to Python] How to output a character string in a Print statement
How to check the memory size of a variable in Python
How to get a string from a command line argument in python
Timezone specification when converting a string to datetime type in python
I wrote the code to write the code of Brainf * ck in python
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
Get the formula in an excel file as a string in Python
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
In the python command python points to python3.8
6 ways to string objects in Python
Create a random string in Python