Python Forum
Need to upgrade this code.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to upgrade this code.
#1
Hey everyone. For my project i need to extract single characters from a given string which are separated from each other by the increasing number of places. Each extracted character must be exactly one place more apart from its predecessor than the one before. We need to use a string formed from the extracted characters as an input for the next phase of the project.
Example1:
Output:
Input string: "OTORHINOLARYNGOLOGY". Outring: "OTRNRL".
Example2:
Output:
Input string: "A clown has a red nose.". Output string: "A lnsee".
This is the code i have for it now:
def index_gen(end):
     
    current = 0
    step = 1
     
    while current < end:
        yield current
        current += step
        step += 1
 
def task(text):
    
    return "".join(text[idx] for idx in index_gen(len(text)))
 
 
if __name__ == "__main__":
    
    text = "A clown has a red nose."
    print(text)
    print(task(text))

    return chars_extracted
I need to upgrade this code with this:

I now need to have a single class with two methods: 'shrink' and 'expand'.

Implement a class whose method 'shrink' matches the function you created for the extracting of characters.

The method 'expand' should place all the characters in their original positions, filling the space between each character with the 'space' character and return the result as a string.

Example1:
Output:
Input string: "OTRNRL". Output string: "OT R N R L".
Example2:
Output:
Input string: "A lnsee". Output string: "A l n s e e".
Anyone knows how to do this, im not as proficient to do this.
buran write Feb-19-2024, 04:32 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You become proficient by doing. You have the code for shrink. Write it in the form of a class. expand is easier than shrink, so it should be no challenge.
Reply
#3
If I understand correctly -
1. Create a class and move your shrink routine to that.
2. Change the shrink routine to record the location of each removed letter.
3. Create a function, expand, that puts the spaces into the string.
4. You will need to have the list that holds the removed positions as a class variable, and would have the result strings as class variables.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Coding upgrade MoreMoney 4 453 Mar-22-2024, 04:30 PM
Last Post: MoreMoney

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020