Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8765217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:13:38+00:00 2026-06-13T16:13:38+00:00

I’m new to Python, so maybe I’m asking for something very easy but I

  • 0

I’m new to Python, so maybe I’m asking for something very easy but I can’t think of the problem in a Python way.

I have a compressed string. The idea is, if a character gets repeated 4-15 times, I make this change:

'0000' ---> '0|4'

If more than 15 times, I use a slash and two digits to represent the amount (working with hexadecimal values):

'00...(16 times)..0' ---> '0/10'

So, accustomed to other languages, my approach is the following:

def uncompress(line):
    verticalBarIndex = line.index('|')
    while verticalBarIndex!=-1:
        repeatedChar = line[verticalBarIndex-1:verticalBarIndex]
        timesRepeated = int(line[verticalBarIndex+1:verticalBarIndex+2], 16)
        uncompressedChars = [repeatedChar]
        for i in range(timesRepeated):
            uncompressedChars.append(repeatedChar)
        uncompressedString = uncompressedChars.join()
        line = line[:verticalBarIndex-1] + uncompressedString + line[verticalBarIndex+2:]
        verticalBarIndex = line.index('|') #next one

    slashIndex = line.index('/')
    while slashIndex!=-1:
        repeatedChar = line[slashIndex-1:slashIndex]
        timesRepeated = int(line[slashIndex+1:verticalBarIndex+3], 16)
        uncompressedChars = [repeatedChar]
        for i in range(timesRepeated):
            uncompressedChars.append(repeatedChar)
        uncompressedString = uncompressedChars.join()
        line = line[:slashIndex-1] + uncompressedString + line[slashIndex+3:]
        slashIndex = line.index('/') #next one
    return line

Which I know it is wrong, since strings are inmutable in Python, and I am changing line contents all the time until no ‘|’ or ‘/’ are present.

I know UserString exists, but I guess there is an easier and more Pythonish way of doing it, which would be great to learn.

Any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:13:39+00:00Added an answer on June 13, 2026 at 4:13 pm

    The changes necessary to get your code running with the sample strings:

    Change .index() to .find(). .index() raises an exception if the substring isn’t found, .find() returns -1.

    Change uncompressedChars.join() to ''.join(uncompressedChars).

    Change timesRepeated = int(line[slashIndex+1:verticalBarIndex+3], 16) to timesRepeated = int(line[slashIndex+1:slashIndex+3], 16)

    Set uncompressedChars = [] to start with, instead of uncompressedChars = [repeatedChar].

    This should get it working properly. There are a lot of places where the code an be tidied and otpimised, but this works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.