Python Forum
Trying to write func("abcd") -> "abbcccdddd" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trying to write func("abcd") -> "abbcccdddd" (/thread-30472.html)



Trying to write func("abcd") -> "abbcccdddd" - omm - Oct-22-2020

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


RE: Trying to write func("abcd") -> "abbcccdddd" - Axel_Erfurt - Oct-22-2020

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"))



RE: Trying to write func("abcd") -> "abbcccdddd" - perfringo - Oct-22-2020

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'



RE: Trying to write func("abcd") -> "abbcccdddd" - omm - Oct-22-2020

(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:]



RE: Trying to write func("abcd") -> "abbcccdddd" - omm - Oct-22-2020

(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 !


RE: Trying to write func("abcd") -> "abbcccdddd" - ebolisa - Oct-23-2020

(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


RE: Trying to write func("abcd") -> "abbcccdddd" - bowlofred - Oct-23-2020

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.


RE: Trying to write func("abcd") -> "abbcccdddd" - ebolisa - Oct-24-2020

(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'))



RE: Trying to write func("abcd") -> "abbcccdddd" - bowlofred - Oct-24-2020

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.