def pattern(n):
s = ""
for i in range(1, n+2):
count = i-1
for k in range(n-i+1):
s += " "
for j in range(2*i):
if count > 0:
s += str(count)
count -= 1
print(s + "".join(map(str, list(range(0, i)))))
s = ""
Problem of the day
Ninja’s younger sister got a project to print the pattern for the given number of rows, say ‘N’. She finds it difficult to write such a pattern for every ‘N’.
Ninja decided to help his sister and decided to write a code for this but wasn't able to write the code for the same. Ninja wants help to write a code and asks you for help. Help Ninja.
Example:
Pattern for N = 2
0
101
21012
The first line of input contains an integer ‘T’ denoting the number of test cases. And each test case follows:
Each test case contains an integer ‘N’ representing the number of rows.
Output format :
Each test case print 'N' strings denoting the required pattern of integers for a given integer ‘N’.
The output of each test case will be printed on a separate line
1 <= T <= 5
1 <= N <= 100
Time Limit: 1 sec.
2
1
2
0
101
0
101
21012
Test Case 1:
For ‘N’ = 1 , we need to print 2 rows - in first row 0, and second row 1 0 1.
Test Case 2:
The Number of rows for ‘N’ = 2 will be 3.
2
3
4
0
101
21012
3210123
432101234
0
101
21012
3210123
432101234
54321012345
Observe the pattern and try using nested iterations.
The idea is to use nested iterations for generating spaces and the digits inside the pattern.
O(N ^ 2), where ‘N’ is the given integer.
As, we are making an iteration ‘N’ number of times to print different rows in the pattern and at max (2 * (N + 1)) columns for the pattern with nested iterations. Therefore, overall time complexity will be O(N ^ 2).
O(1)
As we are using constant space. Therefore, space complexity will be O(1).
Interview problems
Easy python solution
def pattern(n):
s = ""
for i in range(1, n+2):
count = i-1
for k in range(n-i+1):
s += " "
for j in range(2*i):
if count > 0:
s += str(count)
count -= 1
print(s + "".join(map(str, list(range(0, i)))))
s = ""
Interview problems
python
def pattern(n):
for i in range(n+1):
for j in range(i,n):
print(" ",end="")
for j in range(i,-1,-1):
print(j,end="")
for j in range(1,i+1):
print(j,end="")
print()
print()
Interview problems
Why i cant see any of my output being returned
def pattern(n):
triangle = []
row = [0]
triangle.append(row)
for i in range(1, n + 1):
row2 = [0] * ((2 * i) + 1)
temp=i
increment=True
for j in range(0, len(row2)):
if temp>=0:
# print(temp)
row2[j]=temp
if temp==0:
increment=False
if increment:
temp-=1
else:
temp+=1
triangle.append(row2)
return triangle
Interview problems
Mirror image of triangle
#include <bits/stdc++.h>
void pattern(int n)
{
int space=n;
string a="0";
int i=0;
while(n-->=0)
{
int k=space;
while(k--)
cout<<" ";
if(i!=0)
{
a=to_string(i)+a+to_string(i);
}
cout<<a<<" ";
cout<<endl;
i++;
space--;
}
}
Interview problems
Using Java Programming Language.All test cases passed
import java.util.* ;
import java.io.*;
public class Solution {
public static void NumberPattern(int n) {
// Write your code here
for(int i=0;i<=n;i++)
{
for(int j=n-i-1;j>=0;j--)
{
System.out.print(" ");
}
for(int j=0;j<=n-(n-i);j++)
{
System.out.print(Math.abs(j-i));
}
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
Interview problems
Number Pattern
import java.util.* ;
import java.io.*;
public class Solution {
public static void NumberPattern(int n) {
// Write your code here
int count =0;
for(int i=0;i<=n;i++){
for(int j=0;j<n-i;j++){
System.out.print(" ");
}
if(i==0){
System.out.print('0');
}else{
for(int k=i;k>=0;k--){
System.out.print(k);
}
for(int k =1;k<=i;k++){
System.out.print(k);
}
}
System.out.println();
}
}
}
Interview problems
Mirror image of triangle
#include <bits/stdc++.h>
void pattern(int n)
{
for(int i=0;i<=n;i++){
for(int j=i;j<n;j++){
cout<<" ";
}
for(int j=i;j>=0;j--){
cout<<j;
}
for(int j=1;j<=i;j++){
cout<<j;
}
cout<<endl;
}
}
Discussion thread on Interview Problem | Mirror image of triangle
Hey everyone, creating this thread to discuss the interview problem - Mirror image of triangle.
Practise this interview question on Coding Ninjas Studio (hyperlinked with the following link): Mirror image of triangle