Showing posts with label Pattern. Show all posts
Showing posts with label Pattern. Show all posts

Printing patterns

You are required to form a matrix of size r×c where r is the number of rows and c is the number of columns. You are required to form the waves of numbers around the provided center, (Ci, Cj) (0-based indexing).

Example:

  • Size of the matrix: r=9 and c=9
  • Center coordinates: Ci=4 and Cj=4 (0 based indexing)
  • Pattern:

            4 4 4 4 4 4 4 4 4 
            4 3 3 3 3 3 3 3 4 
            4 3 2 2 2 2 2 3 4 
            4 3 2 1 1 1 2 3 4 
            4 3 2 1 0 1 2 3 4 
            4 3 2 1 1 1 2 3 4 
            4 3 2 2 2 2 2 3 4 
            4 3 3 3 3 3 3 3 4 
            4 4 4 4 4 4 4 4 4

You are given the values of r, c, Ci, Cj (the values of Ci and Cj is 0-based indexed). Your task is to print the provided pattern.

Example:

Input:  r=10, c=10, x=5, y=5
Output: 
5 5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 4 4 
5 4 3 3 3 3 3 3 3 4 
5 4 3 2 2 2 2 2 3 4 
5 4 3 2 1 1 1 2 3 4 
5 4 3 2 1 0 1 2 3 4 
5 4 3 2 1 1 1 2 3 4 
5 4 3 2 2 2 2 2 3 4 
5 4 3 3 3 3 3 3 3 4 
5 4 4 4 4 4 4 4 4 4 

Approach

Java

public class PrintingPatterns {
    public static void main(String args[]) {
        int r = 10;
        int c = 10;
        int x = 5;
        int y = 5;
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                str.append(Math.max(Math.abs(x - i), Math.abs(y - j))).append(" ");
            }
            str.append("\n");
        }
        System.out.println(str);
    }
}


ques 6

Given an integer n. Print a pyramid made up of symbol '*' of height n.

Example:

Input:  n = 3

Output:

* *** *****

Approach:

C++

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n = 3;

    for (int i = 1i <= ni++)
    {
        for (int j = 1j <= n - ij++)
            cout << " ";
        for (int k = 0k < 2 * i - 1k++)
            cout << "*";
        cout << "\n";
    }
    cout << "\n";

    return 0;
}


The Pattern

Joey is always fond of playing with patterns. So on his birthday, his uncle gave him a mat.  Mat is a rectangular grid

(NM). i.e. there are N rows and M columns are drawn on the mat.  Each cell of this grid is filled with either dot(.) or star(). Now Joey starts to fold this mat.  Firstly he folds the mat along the rows (top to down) until it transforms into a (1M) grid (Neglect width of mat on each fold). After that, he starts folding this along the columns (left to right) and finally transforms into a single cell. In the end Joey wants to know what will be on the top of that cell (dot (.)or star()).

When star() come over dot(.) it converted to dot(.)
When star() come over star(), it remains the star()
When dot(.) come over dot(.), it remains dot(.)
When dot(.) come over star(), it converted to star()


Example:

Input:  n = 5, m = 5 , pattern={{"*.***"},{".**.."},{".*.*."},{"*...*"},{"..*.*"}}
Output: *

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void thePattern(vector<stringpatternint nint m)
{
    for (int i = 0i < n - 1i++)
    {
        for (int j = 0j < mj++)
        {
            if (pattern[i][j] == '.' && 
pattern[i + 1][j] == '*')
                pattern[i + 1][j] = '*';
            else if (pattern[i][j] == '*' &&
 pattern[i + 1][j] == '.')
                pattern[i + 1][j] = '.';
        }
    }

    for (int j = 0j < m - 1j++)
    {
        if (pattern[n - 1][j] == '.' && 
pattern[n - 1][j + 1] == '*')
            pattern[n - 1][j + 1] = '*';
        else if (pattern[n - 1][j] == '*' &&
 pattern[n - 1][j + 1] == '.')
            pattern[n - 1][j + 1] = '.';
    }
    cout << pattern[n - 1][m - 1] << "\n";
}
int main()
{
    int n = 5m = 5;

    vector<stringpattern = {{"*.***"},
                              {".**.."},
                              {".*.*."},
                              {"*...*"},
                              {"..*.*"}};

    thePattern(patternnm);

    return 0;
}