How do I iterate over every character in a string and multiply it with the place in its string?

1.1k Views Asked by At

I'm doing this coding exercise on codewars.com, and I'm stuck. I'm not looking for a complete solution. Instead, I'm looking to see if my reasoning is correct and if what I'm trying to do is possible.

INPUT: so I have a string: "string." And I want to print the first character in this string ("s") once, The second character in this string ("t") twice, The third character ("r") in this string three times, And so on, until the end of the string. OUTPUT: s-tt-rrr-iiii-nnnnn-gggggg

The only thing I have so far is to iterate over every character in this string.

string = "string"
for char in string: 
print(char, end="-")

output: s-t-r-i-n-g-

The solution I have in my mind is to Iterate over every character in the string Print every character * (times) his place in the string (its index number) +1

Meaning: "S" is at index 0, +1 becomes the first letter, So "s" is printed once. "T" is at index 1, +1 becomes the second letter So "t" is printed twice ...

Then I tried this:

string = "string"
for char in string: 
print(char * string[char], end="-")

But this doesn't work because I get a TypeError: string indices must be integers.

Then I was thinking: Is it possible to iterate over every character in the string, assign it to a variable and then use the variables to do operations? Meaning string[0] or "s" would become variable_1 = 1 (first letter in the string), and I multiply that by the first letter in the string ("s").

What do you think? Could this work or is my solution way off?

PS: I read through StackOverflow's guide on asking questions and tried my best to formulate my problem as clearly as possible. If there are any tips on doing this better, I would appreciate it if you would share them with me. I'm learning how to program and how to ask good questions that benefit my learning.

Thank you for taking the time to read my question. Stan

2

There are 2 best solutions below

1
On BEST ANSWER

enumerate is convenient for getting an integer index and an element for each iteration.

def f(x):
    size = len(x)
    for i, char in enumerate(x):
        num = i+1       # number of characters
        if num == size: # don't print - after last character
            ending = ""
        else:
            ending = "-"
        print(num*char, end = ending)

f("string")

Your logic was only a bit off. If we didn't use enumerate and just indexed a string with integers:

def g(x):
    size = len(x)
    for i in range(size):
        num = i+1       # number of characters
        if num == size: # don't print - after last character
            ending = ""
        else:
            ending = "-"
        print(num*x[i], end = ending)
0
On

The same question was asked by an interviewer to me. I write this code like this

code

string = "string"
final_data = []
count = 1
for i in list(string):
    final_data.append(i*count)
    count += 1

output = "-".join(final_data)
print(output)