Home > Mobile >  Inserting string to string regularly ( 1234567891234 -> 1,2345,6789,1234 )
Inserting string to string regularly ( 1234567891234 -> 1,2345,6789,1234 )

Time:04-29

How to insert ' # ' for each n index from backward?
ex) n=4
evil = '01234567891234oooooooooooooooo321'
to
stan = '0#1234#5678#9123#4ooo#oooo#oooo#oooo#o321'
i tried using list with for,if statement, got stuck. something shameful like this

a = 1234567891234
b = [ a[-i] for i in range(1,len(a) 1)]
for i in range(len(b)):
    c  = b[i]
    if i%4==0: #stuck
        c  = ',' 
c.reverse()

What is the optimum way?

CodePudding user response:

cut the string into chunks (backwards) and then concat them using the seperator

evil = '01234567891234oooooooooooooooo321'
l = 4
sep = '#'
sep.join([evil[max(i-l,0):i] for i in range(len(evil), 0, -l)][::-1])
'0#1234#5678#9123#4ooo#oooo#oooo#oooo#o321'

CodePudding user response:

chunks function as in this answer

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i   n]

evil = '01234567891234oooooooooooooooo321'
n = 4
stan = "#".join(chunks(evil[::-1], n))[::-1]
print(stan) # Output: 0#1234#5678#9123#4ooo#oooo#oooo#oooo#o321

Input string is reversed ([::-1]), split into chunks, joined by "#" and then reversed back again. (It's possible to skip reverses if you calculate how many characters there will be in the first set of characters)

CodePudding user response:

A naive solution would be using parts of evil string:

evil = '01234567891234oooooooooooooooo321'
n = 4
start = len(evil) % n
insert = '#'

stan = evil[:start]   insert
for i in range(start, len(evil) - n, n):
    stan  = evil[i:i n]   insert
stan  = evil[-n:]

CodePudding user response:

You might use a pattern asserting optional repetitions of 4 characters to the right, and replace that position with #

import re

pattern = r"(?=(?:.{4})*$)"
s = "01234567891234oooooooooooooooo321"
print(re.sub(pattern, "#", s))

Output

0#1234#5678#9123#4ooo#oooo#oooo#oooo#o321#

Python demo

CodePudding user response:

For this, I would go backwards through your string evil by reversing the string and iterating through it in a for loop. Then I set a count variable to keep track of how many loops it's done, and reset to 0 when it equals 4. All of this looks like the below:

count = 0
for char in evil[::-1]:
    if count == 4:
        count = 0
    count  = 1

You can then establish a new empty string (new_str), and append each character of evil to, each time checking if count is 4, and adding a # to the string as well before resetting the count. Full code:

count = 0
new_str = ''
for char in evil[::-1]:
    if count == 4:
        new_str  = '#'
        count = 0
    count  = 1
    new_str  = char

This will produce the new string reversed, so you need to reverse it again to get the desired result:

new_str = new_str[::-1]

Output:

'123o#oooo#oooo#oooo#ooo4#3219#8765#4321#0'

CodePudding user response:

You can do it like this:

evil = '01234567891234oooooooooooooooo321'
''.join(j if i%4  else f'#{j}' for i, j in enumerate(evil[::-1]))[::-1][:-1]

Output:

'0#1234#5678#9123#4ooo#oooo#oooo#oooo#o321'
  • Related