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(5): 1 22 333 4444 55555 pattern(11): 1 22 333 4444 55555 666666 7777777 88888888 999999999 10101010101010101010 1111111111111111111111
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 email@example.com with your suggestions!
Top comments (17)
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 the
aparameter, which is the response array (initialized as
and updated at each iteration), and the
lparameter which is the current index.
aarray an item which is described as follow : Build a
l+1-long array, fill it with
l+1(hence, if we are at
l=1, the built item will be equal to
[2, 2]). Then, use the
joinmethod to convert it to a string (
'22'in this case)
joinmethod one last time to add line breaks between lines
Dart, didn't bother to deal with the float and use
I think there is no new-line at the end.
Here is C++ solution:
Python one liner :
Python ... slightly more verbose :)
My Swift solution :
Here is my solution with Elixir
here is the output:
You could use
intercalate "\n"to join the lines. Also, it looks like you have to support decimals.
Shortest snippet among all! That's why I ❤️ Ruby.
Python one-line solution