Daily Challenge #202 - Complete the Pattern II dev.to staff on March 10, 2020 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 stri... Read full post Submit Preview Dismiss Collapse Expand Avalander Avalander Avalander Follow "Biography is irrelevant" - Seven of nine (probably) if she was asked that question. Joined Mar 12, 2018 • Mar 10 '20 • Edited on Mar 10 • Edited Dropdown menu Copy link Hide 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. Collapse Expand Craig McIlwrath Craig McIlwrath Craig McIlwrath Follow Joined Aug 25, 2018 • Mar 10 '20 Dropdown menu Copy link Hide 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. Collapse Expand Avalander Avalander Avalander Follow "Biography is irrelevant" - Seven of nine (probably) if she was asked that question. Joined Mar 12, 2018 • Mar 10 '20 Dropdown menu Copy link Hide Thanks for the tip! Collapse Expand SavagePixie SavagePixie SavagePixie Follow Always learning new things. I love web development and coding in general. Joined Jul 23, 2019 • Mar 10 '20 Dropdown menu Copy link Hide 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 Collapse Expand Rafael Acioly Rafael Acioly Rafael Acioly Follow backend developer; Go, Python and PHP Location São Paulo - Brazil Joined Sep 16, 2017 • Mar 11 '20 Dropdown menu Copy link Hide 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) Collapse Expand Vidit Sarkar Vidit Sarkar Vidit Sarkar Follow Studying Computer Science & Engineering. Prefer to write clean and easy-to-understand code. Languages I use Python, C++, C, Java Script and Java. Location India Education Bachelor of Computer Science & Engineering Joined Feb 10, 2020 • Mar 10 '20 Dropdown menu Copy link Hide 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; } Collapse Expand Vidit Sarkar Vidit Sarkar Vidit Sarkar Follow Studying Computer Science & Engineering. Prefer to write clean and easy-to-understand code. Languages I use Python, C++, C, Java Script and Java. Location India Education Bachelor of Computer Science & Engineering Joined Feb 10, 2020 • Mar 10 '20 Dropdown menu Copy link Hide 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)) Collapse Expand edh_developer edh_developer edh_developer Follow Joined Oct 18, 2017 • Mar 11 '20 Dropdown menu Copy link Hide 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(); } Collapse Expand Vidit Sarkar Vidit Sarkar Vidit Sarkar Follow Studying Computer Science & Engineering. Prefer to write clean and easy-to-understand code. Languages I use Python, C++, C, Java Script and Java. Location India Education Bachelor of Computer Science & Engineering Joined Feb 10, 2020 • Mar 10 '20 Dropdown menu Copy link Hide 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)]) Collapse Expand Amit Patel Amit Patel Amit Patel Follow Independent Software Developer, Primarily working with Ruby on Rails. Location Ahmedabad, India Education Masters in Computer Joined Feb 16, 2017 • Mar 11 '20 Dropdown menu Copy link Hide 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 Collapse Expand Kumaravel_Viswam Kumaravel_Viswam Kumaravel_Viswam Follow A Python beginner and a movie buff. Working on becoming a full stack developer Location India Education BE CSE Work Software Engineer at NTT Data Joined Nov 20, 2019 • Mar 10 '20 Dropdown menu Copy link Hide Python def pattern(num): for i in range(num,0,-1): print(''.join([str(x) for x in range(num,(num-i),-1)])) Code of Conduct • Report abuse
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++
Here is a recursive approach using Python
One liner for the above
Java, with the assumption that the function is actually taking an integer, not a float.
Python one-liner
Ruby
Python
def pattern(num):
for i in range(num,0,-1):
print(''.join([str(x) for x in range(num,(num-i),-1)]))