Print the pattern | Set-1 | Practice | GeeksforGeeks

You a given a number N. You need to print the pattern for the given value of N.
for N = 2 the pattern will be 
2 2 1 1
2 1
for N = 3 the pattern will be 
3 3 3 2 2 2 1 1 1
3 3 2 2 1 1
3 2 1
Example 1:
Input: 2
Output:
2 2 1 1 $2 1 $


Example 2:
Input: 3
Output:
3 3 3 2 2 2 1 1 1 $3 3 2 2 1 1 $3 2 1 $

 
Your Task:
Since this is a function problem, you don't need to worry about the testcases. Your task is to complete the function printPat which takes one argument 'N' denoting the length of the pattern.
Note : Instead of printing new line print a "$" without quotes.
Constraints:
1 <= N <= 40

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
vishalsinghrajput21 minutes ago
def printPat(n):

    k=0
    temp=n
    result = ""
    for i in range(0,n,1):
        for j in range(0,(n*(n-i)),1):
            if k< (n-i):
                result+=str(temp)+" "
                k+=1
            else:
                temp-=1 
                if temp>0:
                    result+=str(temp)+" "
                    k=1
        k=0
        temp=n
        result+="$"
    return result
0
aahshis1613 hours ago
def printPat(n):   ret = ''   for i in range(n,0,-1):       for j in range(n,0,-1):           ret += i*(str(j) + ' ')                  ret += '$'
   return ret
+1
radhakrishna9121rk6 days ago
python solution:
def printPat(n):
    #Code here
    s=""
    for i in range(n,0,-1):
          for j in range(n,0,-1):
                s+=(str(j)+' ')*i
          s+="$"
    return s
 print(printPat(3)) #i passed3 value
0
ejlungay6 days ago
Python solution
def printPat(n):
    result = ""
    nn = n
    for i in range(n):
        for k in range(n, 0, -1):
            for j in range(nn, 0, -1):
                result += str(k) + " "
        result += "$"
        nn -= 1
        
    return result
0
sarajadhav120520091 week ago
C++ Solution:
void printPat(int n)
{
    const int input = n;

    for (int i = 1; i <= input; i++)
    {
        int copied = input;
        while (copied > 0)
        {
            for (int i = n; i > 0; i--) //3
            {
                cout << copied << " ";
            }
            copied--;
        }
        cout << "$";
        n--;
    }
}
0
sharmahariom342881 week ago
C solution :
 
void printPat(int n)
{
    int x, y, z;
    
    for (x = n; x >= 1; x--)
    {
        for (y = n; y >= 1; y--)
        {
            for (z = 1; z <= x; z++)
            {
                printf("%d ", y);
            }
        }
        printf("$");
    }
}
0
monsotal2 weeks ago
   void printPat(int n) {

        for(int t =0 ; t<n ; t++){
            for(int k =n ; k>0 ;k--){
                for(int i =n ; i>t ; i--){
                    System.out.print(k+" ");
                }
            }
            System.out.print("$");

        }
    }
+1
monsotal2 weeks ago
   void printPat(int n) {

        for(int t =0 ; t<n ; t++){
            for(int k =n ; k>0 ;k--){
                for(int i =n ; i>t ; i--){
                    System.out.print(k+" ");
                }
            }
            System.out.print("$");

        }
    }
0
monsotal2 weeks ago
class GfG{   void printPat(int n)   {                for(int t =0 ; t<n ; t++){           for(int k =n ; k>0 ;k--){               for(int i =n ; i>t ; i--){                   System.out.print(k+" ");               }           }           System.out.print("$");
       }   }}
0
monsotal2 weeks ago
Java:
 
class GfG
{
    void printPat(int n)
    {
         
         for(int t =0 ; t<n ; t++){
            for(int k =n ; k>0 ;k--){
                for(int i =n ; i>t ; i--){
                    System.out.print(k+" ");
                }
            }
            System.out.print("$");

        }
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.