Implement a function pattern, which returns the following pattern for up to n number of rows. If n < 1 then it should return " " i.e. empty string. There are no whitespaces in the pattern.
Pattern:
1
22
333
....
.....
nnnnnn
Examples
pattern(5): 1 22 333 4444 55555 pattern(11): 1 22 333 4444 55555 666666 7777777 88888888 999999999 10101010101010101010 1111111111111111111111
Tests
pattern(4)
pattern(8)
pattern(0.5)
Good luck!
This challenge comes from curious_db97 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (16)
Well, here we go for some JS oneliner:
It works like this :
0|ilength (it will floor the number)reducemethod to loop through the built array. Use theaparameter, which is the response array (initialized as[]and updated at each iteration), and thelparameter which is the current index.aarray an item which is described as follow : Build al+1-long array, fill it withl+1(hence, if we are atl=1, the built item will be equal to[2, 2]). Then, use thejoinmethod to convert it to a string ('22'in this case)joinmethod one last time to add line breaks between linesDart, didn't bother to deal with the float and use
dynamicJavaScript
C#
I think there is no new-line at the end.
Here is C++ solution:
Python one liner :
Python ... slightly more verbose :)
Rust :)
Playground link
Python
Haskell
Output:
My Swift solution :
swift