loading...

Daily Challenge #202 - Complete the Pattern II

thepracticaldev profile image dev.to staff ・1 min read

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!

Posted on by:

thepracticaldev profile

dev.to staff

@thepracticaldev

The hardworking team behind dev.to ❤️

Discussion

pic
Editor guide
 

Haskell

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.

 

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.

  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
 

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)
 

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();
        }
 

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))
 

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)])
 

Python

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

 

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