Python Forum
Trying to write func("abcd") -> "abbcccdddd"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to write func("abcd") -> "abbcccdddd"
#1
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
Reply
#2
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"))
Reply
#3
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'
omm likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(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:]
Reply
#5
(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 !
Reply
#6
(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
Reply
#7
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.
Reply
#8
(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'))
Reply
#9
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.
omm likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to output one value per request of the CSV and print it in another func? Student44 3 1,277 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Func Animation not displaying my live graph jotalo 0 1,521 Nov-13-2020, 10:56 PM
Last Post: jotalo
  call func from dict mcmxl22 3 2,818 Jun-21-2019, 05:20 AM
Last Post: snippsat
  About [from FILE import FUNC] Nwb 7 3,520 Apr-21-2019, 02:46 PM
Last Post: snippsat
  Executing func() from a different folder ebolisa 2 2,305 Jan-14-2019, 10:18 AM
Last Post: ebolisa
  Correct number wrong position func. albry 5 5,956 Jan-11-2019, 04:01 PM
Last Post: Larz60+
  How can I return my list from a func? Mike Ru 3 3,040 Oct-22-2018, 01:15 PM
Last Post: buran
  list func in lambda mepyyeti 1 2,879 Mar-10-2018, 09:07 PM
Last Post: buran
  return variable in func mepyyeti 1 2,608 Mar-01-2018, 02:30 AM
Last Post: Larz60+
  Print func textbox instead of shell zykbee 1 5,217 Dec-06-2017, 08:22 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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