## DEV Community is a community of 892,765 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Daily Challenge #202 - Complete the Pattern II

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!

## Discussion (12) Avalander • Edited on

I'm writing this on my phone, so there are probably errors, I'll check it when I have access to a compiler.

``````pattern :: Int -> String
pattern n
| n < 1     = " "
| otherwise = pattern' n
where
pattern' n = unlines \$ foldl folder [] \$ map show [n,(n-1)..1]
folder :: [String] -> String -> [String]
folder [] x     = [x]
folder (p:xs) x = (p ++ x) : p : xs
``````

Edit: fixed issues after running it through a compiler. SavagePixie

Elixir

I based my solution on the examples, rather than the explanation.

``````  def pattern(n) when n < 1, do: " "
def pattern(n) do
list = for x <- 1..n, do: n..x
|> Enum.to_list
|> Integer.undigits
|> Integer.to_string
Enum.join(list, "\n")
end
`````` Rafael Acioly

Python solution 🐍

``````EMPTY_RESPONSE = " "
MIN_DIGIT = 1

def pattern(value: int) -> str:
if not value:
return EMPTY_RESPONSE

items = [str(i) for i in reversed(range(MIN_DIGIT, n + MIN_DIGIT))]

return "".join(items)
`````` Vidit Sarkar

No new-line at the end.
C++

``````string pattern(int n){
string pat = "";
for(int i=1;i<n;i++){
for(int j=n;j>=i;j--)
pat += to_string(j);
pat += "\n";
}
if(n > 0)
pat += to_string(n);
return pat;
}
`````` Java, with the assumption that the function is actually taking an integer, not a float.

``````        private static String pattern(int x) {

StringBuffer sb = new StringBuffer("\n"),
result = new StringBuffer();

for (int i = 1; i <= x; i++) {
sb.insert(0,i);
result.insert(0,sb.toString());
}

return result.toString();
}
`````` Vidit Sarkar

Here is a recursive approach using Python

``````def pattern(number, end=1):
res = ''.join([str(i) for i in range(int(number), end-1, -1)])
if end >= int(number):
return res
return res + '\n' + pattern(number, end+1)
``````

One liner for the above

``````pattern = lambda number, end=1 : ''.join([str(i) for i in range(int(number), end-1, -1)]) + ('' if end >= int(number) else '\n' + pattern(number, end+1))
`````` Vidit Sarkar

Python one-liner

``````pattern = lambda number : '\n'.join([''.join([str(j) for j in range(int(number),i-1,-1)]) for i in range(1,int(number)+1)])
`````` Kumaravel_Viswam

Python

def pattern(num):
for i in range(num,0,-1):
print(''.join([str(x) for x in range(num,(num-i),-1)])) Amit Patel

Ruby

``````def pattern(n)
return "" if n < 1
(1..n).to_a.reverse.each_with_index.map { |_, index| ((index+1)..n).to_a.reverse.join('') }.join("\n")
end
``````