First, build an array with a 0|i length (it will floor the number)
Use the reduce method to loop through the built array. Use the a parameter, which is the response array (initialized as [] and updated at each iteration), and the l parameter which is the current index.
For each iteration, add to the a array 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 join method to convert it to a string ('22'in this case)
Finally, once the array is built, use the join method one last time to add line breaks between lines
var = float(input("Enter the number: "))
def pattern(var):
if var < 1:
print("")
else:
var = int(var)
for i in range(var+1):
for j in range(i):
print(i,end = '')
print('\n',end='')
pattern(var)
I think there is no new-line at the end.
Here is C++ solution:
stringpattern(intnumber){stringpat="";// there is no new-line at the end so loop end is number-1for(inti=1;i<=number-1;i++){for(intj=0;j<i;j++)pat+=to_string(i);// add the string version of numberpat+="\n";// add a new-line after adding the numbers each time }// add the case of number at the end with no new-linefor(inti=0;i<number;i++)pat+=to_string(number);returnpat;}
def pattern(n):
pat = ''
if n < 1:
return pat
else:
for index,value in enumerate(range(n),start=1):
if value:
pat += '\n'
for i in range(index):
pat += str(index)
return pat
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 linesPython
Rust :)
Playground link
JavaScript
Haskell
Output:
I think there is no new-line at the end.
Here is C++ solution:
Python one liner :
C#
Dart, didn't bother to deal with the float and use
dynamicPython ... slightly more verbose :)
My Swift solution :
swift
Here is my solution with Elixir
here is the output:
Shortest snippet among all! That's why I ❤️ Ruby.
Haskell
You could use
unlinesinstead ofintercalate "\n"to join the lines. Also, it looks like you have to support decimals.Python one-line solution