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

26th Sep 2021, 3:37 AM
Asphalt
Asphalt - avatar
22 ответов
+ 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])
26th Sep 2021, 4:45 AM
David Ashton
David Ashton - avatar
+ 6
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/
27th Sep 2021, 2:59 PM
David Ashton
David Ashton - avatar
+ 4
Use enumerate() to get the index of a character in the iterable for index, c in enumerate( input() ): print( c * ( index + 1 ) )
26th Sep 2021, 4:31 AM
Ipang
+ 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/
26th Sep 2021, 10:40 AM
David Ashton
David Ashton - avatar
+ 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())]
27th Sep 2021, 7:14 AM
Gabriel
Gabriel - avatar
+ 2
David Ashton tnx
27th Sep 2021, 3:09 PM
kamran abi
+ 1
Ipang ah that's new for me actually I never used enumerate but thanks that's works as well
26th Sep 2021, 5:04 AM
Asphalt
Asphalt - avatar
+ 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
26th Sep 2021, 6:39 AM
Rishi
Rishi - avatar
+ 1
David Ashton I meant to write something more like a pseudo code but similar to Python so he'll understand😅
26th Sep 2021, 6:40 AM
Rishi
Rishi - avatar
+ 1
Thx Rishi
26th Sep 2021, 8:46 AM
Asphalt
Asphalt - avatar
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 ;)
26th Sep 2021, 3:55 AM
Rishi
Rishi - avatar
0
Sorry but didn't get what u said Rishi
26th Sep 2021, 4:19 AM
Asphalt
Asphalt - avatar
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
26th Sep 2021, 4:24 AM
Rishi
Rishi - avatar
0
Yea David Ashton that works thx Btw what I did wrong can u pls explain ?
26th Sep 2021, 4:59 AM
Asphalt
Asphalt - avatar
0
Rishi thx got it now guess I need to practice more haha Btw any good site for practicing c and python questions?
26th Sep 2021, 7:33 AM
Asphalt
Asphalt - avatar
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
26th Sep 2021, 8:34 AM
Rishi
Rishi - avatar
0
Ty so much ☺️
26th Sep 2021, 12:21 PM
Asphalt
Asphalt - avatar
0
x = str (input()) i= 0 while x[i] in x: j = x[i] c = i+1 print (j*c) i += 1
27th Sep 2021, 11:09 AM
kamran abi
0
r="data" count=1 for i in r: print(i*count) count+=1 OUTPUT I GOT : d aa ttt aaaa [Program finished]
27th Sep 2021, 4:45 PM
Ravi V
Ravi V - avatar
0
a=input() i=0 b=len(a) for i in range(b): print(a[i]*(i+1)) i+=1
27th Sep 2021, 6:15 PM
kamran abi