Interview Question
Country: India
// ZoomBA
def print_pattern( n ){
if ( n < 2 ) { println ( n ) ; return }
// create n * n matrix
m = list( [0:n] ) -> { list() }
// 0 , n-1, 1, n - 2 ...
row_indices = lfold ( [ 1:n-1], list(0,n-1) ) ->{
$.partial += ( $.partial[-2] + ( (-1) ** $.index) )
}
count = 1
for ( row : row_indices ){
for ( [0:n ] ){
m[row] += count
count +=1
}
}
lfold(m) -> { println ( str( $.item, ' * ' ) ) }
}
// use it
print_pattern ( 3 )
public static void printsq(int n) {
if (n <= 0) {
return;
}
if (n == 1) {
System.out.println("1");
return;
}
int[][] arr = new int[n][n];
int ctr = 1;
for (int i = 1; i <= n/2; i++) {
for (int j = 1; j<=n; j++) {
arr[i-1][j-1] = ctr;
ctr++;
}
for(int j = 1; j <= n; j++) {
arr[n-i][j-1] = ctr;
ctr++;
}
}
if (n%2 == 1) {
for (int j =0; j < n; j++) {
arr[n/2][j] = ctr;
ctr++;
}
}
for(int i=0; i<n; i++) {
for(int j=0;j<n;j++) {
if (j==0)
System.out.print(arr[i][j]);
else
System.out.print(" * " + arr[i][j]);
}
System.out.println();
}
}
#include <stdio.h>
void printMatrix(int matrice1[100][100], int n, int rem1, int rem2)
{
int i=0;
int j=0;
for( i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
if (i == 0) /* complete the first line o the matrix */
{
matrice1[0][j] = j + 1;
}
else if ((i > 0) && (i <= n/2)) /* complete the lines from 1 to n/2 */
{
matrice1[i][j] = rem1 + n + j + 1;
if (j==n-1)
{
rem1 = matrice1[i][j]; /* rem1 value set to the last element of the line */
}
}
else if (i == n-1) /* complete the last line of the matrix */
{
matrice1[i][j] = n+j+1;
}
else if ((i > n/2) && (i < n)) /* complete the lines between n/2 + 1 and n-1 */
{
matrice1[i][j] = rem2 + n + j + 1;
if (j==n-1)
{
rem2 = matrice1[i][j];
}
}
}
}
/* print the final matrix */
for (i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%d " , matrice1[i][j]);
}
printf("\n");
}
}
int main()
{
int nbofElements;
int matrice[100][100]={0};
int reminder1;
int reminder2;
printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);
if (nbofElements % 2 == 0)
{
printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);
if (nbofElements % 2 == 0)
{
exit(0); /* if the user enter a second wrong number -> exit the program */
}
}
else
{
}
reminder1 = nbofElements;
reminder2 = nbofElements*2; /*it has the value of the n*2 */
printMatrix(matrice, nbofElements, reminder1, reminder2);
getchar();
return 0;
}
#include <stdio.h>
void printMatrix(int matrice1[100][100], int n, int rem1, int rem2)
{
int i=0;
int j=0;
for( i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
if (i == 0) /* complete the first line o the matrix */
{
matrice1[0][j] = j + 1;
}
else if ((i > 0) && (i <= n/2)) /* complete the lines from 1 to n/2 */
{
matrice1[i][j] = rem1 + n + j + 1;
if (j==n-1)
{
rem1 = matrice1[i][j]; /* rem1 value set to the last element of the line */
}
}
else if (i == n-1) /* complete the last line of the matrix */
{
matrice1[i][j] = n+j+1;
}
else if ((i > n/2) && (i < n)) /* complete the lines between n/2 + 1 and n-1 */
{
matrice1[i][j] = rem2 + n + j + 1;
if (j==n-1)
{
rem2 = matrice1[i][j];
}
}
}
}
/* print the final matrix */
for (i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%d " , matrice1[i][j]);
}
printf("\n");
}
}
int main()
{
int nbofElements;
int matrice[100][100]={0};
int reminder1;
int reminder2;
printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);
if (nbofElements % 2 == 0)
{
printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);
if (nbofElements % 2 == 0)
{
exit(0); /* if the user enter a second wrong number -> exit the program */
}
}
else
{
}
reminder1 = nbofElements;
reminder2 = nbofElements*2; /*it has the value of the n*2 */
printMatrix(matrice, nbofElements, reminder1, reminder2);
getchar();
return 0;
#include <stdio.h>
void printMatrix(int matrice1[100][100], int n, int rem1, int rem2)
{
int i=0;
int j=0;
for( i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
if (i == 0) /* complete the first line o the matrix */
{
matrice1[0][j] = j + 1;
}
else if ((i > 0) && (i <= n/2)) /* complete the lines from 1 to n/2 */
{
matrice1[i][j] = rem1 + n + j + 1;
if (j==n-1)
{
rem1 = matrice1[i][j]; /* rem1 value set to the last element of the line */
}
}
else if (i == n-1) /* complete the last line of the matrix */
{
matrice1[i][j] = n+j+1;
}
else if ((i > n/2) && (i < n)) /* complete the lines between n/2 + 1 and n-1 */
{
matrice1[i][j] = rem2 + n + j + 1;
if (j==n-1)
{
rem2 = matrice1[i][j];
}
}
}
}
/* print the final matrix */
for (i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%d " , matrice1[i][j]);
}
printf("\n");
}
}
int main()
{
int nbofElements;
int matrice[100][100]={0};
int reminder1;
int reminder2;
printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);
if (nbofElements % 2 == 0)
{
printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);
if (nbofElements % 2 == 0)
{
exit(0); /* if the user enter a second wrong number -> exit the program */
}
}
else
{
}
reminder1 = nbofElements;
reminder2 = nbofElements*2; /*it has the value of the n*2 */
printMatrix(matrice, nbofElements, reminder1, reminder2);
getchar();
return 0;
}
#include <stdio.h>
void printMatrix(int matrice1[100][100], int n, int rem1, int rem2)
{
int i=0;
int j=0;
for( i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
if (i == 0) /* complete the first line o the matrix */
{
matrice1[0][j] = j + 1;
}
else if ((i > 0) && (i <= n/2)) /* complete the lines from 1 to n/2 */
{
matrice1[i][j] = rem1 + n + j + 1;
if (j==n-1)
{
rem1 = matrice1[i][j]; /* rem1 value set to the last element of the line */
}
}
else if (i == n-1) /* complete the last line of the matrix */
{
matrice1[i][j] = n+j+1;
}
else if ((i > n/2) && (i < n)) /* complete the lines between n/2 + 1 and n-1 */
{
matrice1[i][j] = rem2 + n + j + 1;
if (j==n-1)
{
rem2 = matrice1[i][j];
}
}
}
}
/* print the final matrix */
for (i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%d " , matrice1[i][j]);
}
printf("\n");
}
}
int main()
{
int nbofElements;
int matrice[100][100]={0};
int reminder1;
int reminder2;
printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);
if (nbofElements % 2 == 0)
{
printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);
if (nbofElements % 2 == 0)
{
exit(0); /* if the user enter a second wrong number -> exit the program */
}
}
else{}
reminder1 = nbofElements;
reminder2 = nbofElements*2; /*it has the value of the n*2 */
printMatrix(matrice, nbofElements, reminder1, reminder2);
getchar();
return 0;
}
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
in.close();
for (int j = 0, c = 2; j != -1;) {
if (j != N) {
for (int i = 1; i <= N; i++)
System.out.printf("%d*", j*N+i);
System.out.printf("\b\n");
}
j += c;
if (j == N || j == N + 1) {
j--;
c = -2;
}
}
}
}
- Swap October 10, 2016