How to solve this one kinda struggling now :( | Sololearn: Learn to code for FREE!

+4

How to solve this one kinda struggling now :(

Let's test your coding skills! Take a string as input and output each letter of the string on a new line, repeated N times, where N is the position of the letter in the string. Sample Input: data Sample Output: d aa ttt aaaa My try :x = input() for i in x: print(i*x.index(i))

9/26/2021 3:37:48 AM

Rajarshi

22 Answers

New Answer

+7

Rishi Are you using Python? There is no ++a in Python. Rajarshi What about this? x = input() for i in range(1, len(x) + 1): print(i * x[i - 1])

+5

kamran abi Your code works fine, but to improve your style, there should never be a space between a function's name and its parentheses. str(), input() and print() are functions, so don't say print (x), say print(x). Also, the input() function ALWAYS returns a string, so you never need to say str(input()). Just say input() :) See PEP 8. https://www.python.org/dev/peps/pep-0008/

+4

Use enumerate() to get the index of a character in the iterable for index, c in enumerate( input() ): print( c * ( index + 1 ) )

+4

One problem is the index of the first letter is 0 so you don't see that letter in the answer etc. This fixes that problem, but you still have the problem Rishi pointed out of only using the index of the first occurrence. I like the solution of Ipang as it is simplest. But don't put spaces after '(' or before ')' 😊 See PEP 8. https://www.python.org/dev/peps/pep-0008/

+3

The most pythonic way is that Ipang wrote, using the "enumerate". If you want to write in a more compressed form, you can use list comprehension additionally: [print(c * (index + 1)) for index, c in enumerate(input())]

+1

Ipang ah that's new for me actually I never used enumerate but thanks that's works as well

+1

Rajarshi your mistake is that, when a letter comes more than one time, for example in "data", x.index(i) only gives the "index" of the "first" occurrence. For example, for the fourth character 'a', it gives the index as 1

+1

David Ashton I meant to write something more like a pseudo code but similar to Python so he'll understand😅

+1

Thx Rishi

+1

David Ashton tnx

0

Use a variable (n) to keep count of the position of the character. Then, print the character n times inside the for loop. Like, a=1 for i in x: for j in range(0,a): print(i) ++a My solution is just an overview of my algorithm. See if this works ;)

0

Sorry but didn't get what u said Rishi

0

Rajarshi see the code I posted I declared a variable 'a' to keep count of the number of the character ie. first character or second character or... Then I loop through the string, character by character and have the variable I denote the character(just like in your code) Then I'm printing the character (i) 'a' times. If the character is first character, then a will be 1 and i will be printed 1 time. If it's 2, then i will be printed 2 times, etc... That's it. You got every charcter printed in new line, n times, where n is the position number of the character

0

Yea David Ashton that works thx Btw what I did wrong can u pls explain ?

0

Rishi thx got it now guess I need to practice more haha Btw any good site for practicing c and python questions?

0

Rajarshi try www.hackerrank.com It has many coding quizzes in varying difficulty levels. You can use any language of your choice in this platform

0

Ty so much ☺️

0

x = str (input()) i= 0 while x[i] in x: j = x[i] c = i+1 print (j*c) i += 1

0

r="data" count=1 for i in r: print(i*count) count+=1 OUTPUT I GOT : d aa ttt aaaa [Program finished]

0

a=input() i=0 b=len(a) for i in range(b): print(a[i]*(i+1)) i+=1