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

Time:10-31

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

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

CodePudding user response:

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)

CodePudding user response:

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)

CodePudding user response:

I understand that you've read the guide on asking questions, my only suggestion would be to use code blocks.

I tried to solve your issue, here's my code

x = ["string"]

for i in x:
    p = 0
    while p < 6:
        print(i[p]*p)
        p  = 1

This outputs:

t
rr
iii
nnnn
ggggg

I'm sure you can improve on this, have a great day!

  • Related