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|i
length (it will floor the number)reduce
method to loop through the built array. Use thea
parameter, which is the response array (initialized as[]
and updated at each iteration), and thel
parameter which is the current index.a
array 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 thejoin
method to convert it to a string ('22'
in this case)join
method one last time to add line breaks between linesHaskell
Output:
Dart, didn't bother to deal with the float and use
dynamic
JavaScript
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
My Swift solution :
swift