PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 18, 2022

[FIXED] How to group a section of numbers in a list together Python

 October 18, 2022     function, python, python-3.x     No comments   

Issue

I am currently working on a program in which I must take a string as input and then reverse all of the numbers in that string, leaving all other characters the same. I managed to do this, however it seems that I must reverse sections of numbers at a time, and not reverse every number. I am not sure how I can do this with my solution. I would prefer not to use any libraries.

For example:

For input abc123abc456abc7891

My result: abc198abc765abc4321

Target Result: abc321abc654abc1987

Here is what I have:

#Fucntion just reverses the numbers that getn gives to it
def reverse(t):
    t = t[::-1]
    return t

def getn(w):
    w = list(w)
    Li = []
#Going through each character of w(the inputted string) and adding any numbers to the list Li
    for i in w:
        if i.isdigit():
            Li.append(i)
#Turn Li back into a string so I can then reverse it using the above function
#after reversing, I turn it back into a list
    Li = ''.join(Li)
    Li = reverse(Li)
    Li = list(Li)
#I use t only for the purpose of the for loop below,
#to get the len of the string,
#a is used to increment the position in Li
    t = ''.join(w)
    a = 0
#This goes through each position of the string again,
#and replaces each of the original numbers with the reversed sequence
    for i in range(0,len(t)):
        if w[i].isdigit():
            w[i] = Li[a]
            a+=1
#Turn w back into a string to print
    w = ''.join(w)
    print('New String:\n'+w)

x = input('Enter String:\n')
getn(x)

Solution

Solution outline:

  • Break the string into a list of substrings. Each substring is defined by the division between digits and non-digits. Your result at othe end of this stage should be ["abc", "123", "abc", "456", "abc", "7891"]
  • Go through this list; replace each digit string with its reverse.
  • join this list into a single string.

The final step is simply ''.join(substring_list).

The middle step is contained in what you're already doing.

The first step isn't trivial, but well within the coding ability in your original post.

Can you take it from here?


UPDATE

Here's the logic to break the string into groups as needed. Check the "digit-ness" of each char. If it's different from that of the previous char, then you have to start a new substring.

instr = "abc123abc456abc7891"

substr = ""
sub_list = []
prev_digit = instr[0].isdigit()

for char in instr:
    # if the character's digit-ness is different from the last one,
    #    then "tie off" the current substring and start a new one.
    this_digit = char.isdigit()
    if this_digit != prev_digit:
        sub_list.append(substr)
        substr = ""
        prev_digit = this_digit

    substr += char

# Add the last substr to the list
sub_list.append(substr)

print(sub_list)

Output:

['abc', '123', 'abc', '456', 'abc', '7891']


Answered By - Prune
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing