Python Forum

Full Version: Trying to write func("abcd") -> "abbcccdddd"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
For passing a parameter 'abcd' to function tag, the following result is expected tag("abcd") -> "abbcccdddd"

Now I am trying to append these values to '' empty string but, not getting the result

def tag(s):
    count = 0
    res = ''
    for val in s:
        count += 1
        for i in range(count): 
            res.join(val) 
    return res # Expecting Toommm


print(tag("Tom"))
Am I doing something wrong here?
I have tried to print(i, val) and that seems to print correctly the expected characters.
But, could not append the same to empty string and output it.
            res.join(val) 
    return res # Expecting Toommm
Thanks,
Om
def tag(s):
    count = len(s)
    for i in range(count): 
        res = f"{s[:1]}{''.join([char*2 for char in s[1:]])}"
    return res
 
 
print(tag("Tom"))
Another way of doing it: 'multiply letter with it's queue number and join all letters to string'

>>> s = 'abcd'
>>> ''.join(i*char for i, char in enumerate(s, start=1))
'abbcccdddd'
(Oct-22-2020, 07:57 AM)Axel_Erfurt Wrote: [ -> ]
def tag(s):
    count = len(s)
    for i in range(count): 
        res = f"{s[:1]}{''.join([char*2 for char in s[1:]])}"
    return res
 
 
print(tag("Tom"))

Thanks for the quick response. The code you have suggested seems to print all letters twice other than the first letter. looks like we are joining the first character
{s[:1]}
with twice
char*2
every other character
for char in s[1:]
(Oct-22-2020, 08:43 AM)perfringo Wrote: [ -> ]Another way of doing it: 'multiply letter with it's queue number and join all letters to string'

>>> s = 'abcd'
>>> ''.join(i*char for i, char in enumerate(s, start=1))
'abbcccdddd'

Wow! thanks for the one liner code. Solved !
(Oct-22-2020, 08:43 AM)perfringo Wrote: [ -> ]Another way of doing it: 'multiply letter with it's queue number and join all letters to string'

>>> s = 'abcd'
>>> ''.join(i*char for i, char in enumerate(s, start=1))
'abbcccdddd'

Hi,

I understand overall your one liner but, if I may ask, how does the i in (i*char) contain a value before the loop is started?

TIA
Although it's at the front, it's still "inside the loop". This is just a syntax for a generator comprehension, similar to a list comprehension.

letter_generator = (i*char for i, char in enumerate(s, start=1))
Is similar to:

def gen_expression(iterable):
    for i,char in enumerate(iterable, start=1):
        yield i*char
letter_generator = gen_expression(s)
And then the generator will hand out the sequence as requested.
(Oct-23-2020, 09:57 PM)bowlofred Wrote: [ -> ]Although it's at the front, it's still "inside the loop". This is just a syntax for a generator comprehension, similar to a list comprehension.

letter_generator = (i*char for i, char in enumerate(s, start=1))
Is similar to:

def gen_expression(iterable):
    for i,char in enumerate(iterable, start=1):
        yield i*char
letter_generator = gen_expression(s)
And then the generator will hand out the sequence as requested.

Thanks. I came up with some similar which gets closer to my understanding of the function:

def tag(s):
    res = ''
    for i, char in enumerate(s, start=1):
        res += ''.join(i*char)
    return res

print(tag('tom'))
If you ignore the "generator" part of it, the flow is similar. But the join and concatenation are not shown in the correct places in this rewrite. The join you have is superfluous. It's only the addition operator that is building up the string.