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(4): 4321 432 43 4 pattern(11): 1110987654321 111098765432 11109876543 1110987654 111098765 11109876 1110987 111098 11109 1110 11
Tests
pattern(5)
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 (11)
Haskell
I'm writing this on my phone, so there are probably errors, I'll check it when I have access to a compiler.
Edit: fixed issues after running it through a compiler.
I do a lot of these challgnes on my phone. I use rextester.com/l/haskell_online_com... to test my Haskell code a lot.
Thanks for the tip!
Elixir
I based my solution on the examples, rather than the explanation.
Python solution 🐍
No new-line at the end.
C++
Java, with the assumption that the function is actually taking an integer, not a float.
Here is a recursive approach using Python
One liner for the above
Python one-liner
Python
def pattern(num):
for i in range(num,0,-1):
print(''.join([str(x) for x in range(num,(num-i),-1)]))
Ruby