Medium

Sudoku is a number-placement puzzle. The objective is to fill a 9 × 9 grid with digits so that each column, each row, and each of the nine 3 × 3 sub-grids that compose the grid contains all of the digits from 1 to 9.

This algorithm should check if the given grid of numbers represents a correct solution to Sudoku.

Example

  • For

grid = [[1, 3, 2, 5, 4, 6, 9, 8, 7],

              [4, 6, 5, 8, 7, 9, 3, 2, 1],

              [7, 9, 8, 2, 1, 3, 6, 5, 4],

              [9, 2, 1, 4, 3, 5, 8, 7, 6],

              [3, 5, 4, 7, 6, 8, 2, 1, 9],

              [6, 8, 7, 1, 9, 2, 5, 4, 3],

              [5, 7, 6, 9, 8, 1, 4, 3, 2],

              [2, 4, 3, 6, 5, 7, 1, 9, 8],

              [8, 1, 9, 3, 2, 4, 7, 6, 5]]

the output should be
sudoku(grid) = true;

  • For

grid = [[1, 3, 2, 5, 4, 6, 9, 2, 7],

              [4, 6, 5, 8, 7, 9, 3, 8, 1],

              [7, 9, 8, 2, 1, 3, 6, 5, 4],

              [9, 2, 1, 4, 3, 5, 8, 7, 6],

              [3, 5, 4, 7, 6, 8, 2, 1, 9],

              [6, 8, 7, 1, 9, 2, 5, 4, 3],

              [5, 7, 6, 9, 8, 1, 4, 3, 2],

              [2, 4, 3, 6, 5, 7, 1, 9, 8],

              [8, 1, 9, 3, 2, 4, 7, 6, 5]]

the output should be
sudoku(grid) = false.

The output should be false: each of the nine 3 × 3 sub-grids should contain all of the digits from 1 to 9.
These examples are represented on the image below.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.array.integer grid

    A matrix representing 9 × 9 grid already filled with numbers from 1 to 9.

    Guaranteed constraints:
    grid.length = 9,
    grid[i].length = 9,
    1 ≤ grid[i][j] ≤ 9.

  • [output] boolean

    • true if the given grid represents a correct solution to Sudoku, false otherwise.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
bool sudoku(arr_arr_integer grid)
{
	for(int i=0;i<9;i++)
	{
		int row[9]={0, }, col[9]={0, };
		for(int j=0;j<9;j++)
		{
			if(i%3==0 && j%3==0)
			{
				int square[9]={0, };
				for(int x=0;x<3;x++)
					for(int y=0;y<3;y++)
					{
						if(square[grid.arr[i+y].arr[j+x]-1]!=0)
							return false;
						square[grid.arr[i+y].arr[j+x]-1]=1;
					}
			}

			if(row[grid.arr[i].arr[j]-1]!=0 || col[grid.arr[j].arr[i]-1]!=0)
				return false;
			row[grid.arr[i].arr[j]-1]=1;
			col[grid.arr[j].arr[i]-1]=1;
		}
	}

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Largest Number  (0) 2020.04.07
<Codesignal> Add Two Digits  (0) 2020.04.07
<Codesignal> spiralNumbers  (0) 2020.04.06
<Codesignal> messageFromBinaryCode  (0) 2020.04.06
<Codesignal> File Naming  (0) 2020.04.06

Medium

Construct a square matrix with a size N × N containing integers from 1 to N * N in a spiral order, starting from top-left and in clockwise direction.

Example

For n = 3, the output should be

spiralNumbers(n) = [[1, 2, 3],

                                          [8, 9, 4],

                                          [7, 6, 5]]

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    Matrix size, a positive integer.

    Guaranteed constraints:
    3 ≤ n ≤ 100.

  • [output] array.array.integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
arr_arr_integer spiralNumbers(int n)
{
	int count=1, x=0, y=n-1;
	arr_arr_integer matrix=alloc_arr_arr_integer(n);
	for(int i=0;i<n;i++)
		matrix.arr[i]=alloc_arr_integer(n);

	while(count<=n*n)
	{
		for(int i=x;i<=y;i++)
			matrix.arr[x].arr[i]=count++;
		for(int i=x+1;i<=y;i++)
			matrix.arr[i].arr[y]=count++;
		for(int i=y-1;i>=x;i--)
			matrix.arr[y].arr[i]=count++;
		for(int i=y-1;i>=x+1;i--)
			matrix.arr[i].arr[x]=count++;
		x++;
		y--;
	}

	return matrix;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Add Two Digits  (0) 2020.04.07
<Codesignal> Sudoku  (0) 2020.04.06
<Codesignal> messageFromBinaryCode  (0) 2020.04.06
<Codesignal> File Naming  (0) 2020.04.06
<Codesignal> digitsProduct  (0) 2020.04.06

Medium

You are taking part in an Escape Room challenge designed specifically for programmers. In your efforts to find a clue, you've found a binary code written on the wall behind a vase, and realized that it must be an encrypted message. After some thought, your first guess is that each consecutive 8 bits of the code stand for the character with the corresponding extended ASCII code.

Assuming that your hunch is correct, decode the message.

Example

For code = "010010000110010101101100011011000110111100100001", the output should be
messageFromBinaryCode(code) = "Hello!".

The first 8 characters of the code are 01001000, which is 72 in the binary numeral system. 72 stands for H in the ASCII-table, so the first letter is H.
Other letters can be obtained in the same manner.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string code

    A string, the encrypted message consisting of characters '0' and '1'.

    Guaranteed constraints:
    0 < code.length < 800.

  • [output] string

    • The decrypted message.
// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

char *messageFromBinaryCode(char *code)
{
	char *decimalCode=(char *)calloc(strlen(code)/8+1,sizeof(char));

	for(int i=0;i<strlen(code);i+=8)
	{
		int N=0;

		for(int j=0;j<8;j++)
			N+=pow(2,7-j)*(code[i+j]=='1');
		decimalCode[i/8]=N;
	}

	return decimalCode;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Sudoku  (0) 2020.04.06
<Codesignal> spiralNumbers  (0) 2020.04.06
<Codesignal> File Naming  (0) 2020.04.06
<Codesignal> digitsProduct  (0) 2020.04.06
<Codesignal> Different Squares  (0) 2020.04.06

Medium

You are given an array of desired filenames in the order of their creation. Since two files cannot have equal names, the one which comes later will have an addition to its name in a form of (k), where k is the smallest positive integer such that the obtained name is not used yet.

Return an array of names that will be given to the files.

Example

For names = ["doc", "doc", "image", "doc(1)", "doc"], the output should be
fileNaming(names) = ["doc", "doc(1)", "image", "doc(1)(1)", "doc(2)"].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.string names

    Guaranteed constraints:
    5 ≤ names.length ≤ 1000,
    1 ≤ names[i].length ≤ 15.

  • [output] array.string

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
arr_string fileNaming(arr_string names)
{
	for(int i=0;i<names.size-1;i++)
	{
		int count=1;

		for(int j=i+1;j<names.size;j++)
			if(strcmp(names.arr[i],names.arr[j])==0)
			{
				sprintf(names.arr[j],"%s%c%d%c", names.arr[i],'(',count++,')');

				for(int k=0;k<j;k++)
					if(strcmp(names.arr[k],names.arr[j])==0)
						sprintf(names.arr[j],"%s%c%d%c", names.arr[i],'(',count++,')');
			}
	}

	return names;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> spiralNumbers  (0) 2020.04.06
<Codesignal> messageFromBinaryCode  (0) 2020.04.06
<Codesignal> digitsProduct  (0) 2020.04.06
<Codesignal> Different Squares  (0) 2020.04.06
<Codesignal> sumUpNumbers  (0) 2020.04.06

Medium

Given an integer product, find the smallest positive (i.e. greater than 0) integer the product of whose digits is equal to product. If there is no such integer, return -1 instead.

Example

  • For product = 12, the output should be
    digitsProduct(product) = 26;
  • For product = 19, the output should be
    digitsProduct(product) = -1.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer product

    Guaranteed constraints:
    0 ≤ product ≤ 600.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int digitsProduct(int product)
{
	for(int i=product;i<=5555;i++)
	{
		int temp=i, mul=1;

		while(temp>0)
		{
			mul*=temp%10;
			temp/=10;
		}

		if(mul==product)
			return i;
	}

	return -1;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> messageFromBinaryCode  (0) 2020.04.06
<Codesignal> File Naming  (0) 2020.04.06
<Codesignal> Different Squares  (0) 2020.04.06
<Codesignal> sumUpNumbers  (0) 2020.04.06
<Codesignal> Valid Time  (0) 2020.04.06

Medium

Given a rectangular matrix containing only digits, calculate the number of different 2 × 2 squares in it.

Example

For

matrix = [[1, 2, 1],

                   [2, 2, 2],

                   [2, 2, 2],

                   [1, 2, 3],

                   [2, 2, 1]]

the output should be
differentSquares(matrix) = 6.

Here are all 6 different 2 × 2 squares:

  • 1 2
    2 2
  • 2 1
    2 2
  • 2 2
    2 2
  • 2 2
    1 2
  • 2 2
    2 3
  • 2 3
    2 1

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.array.integer matrix

    Guaranteed constraints:
    1 ≤ matrix.length ≤ 100,
    1 ≤ matrix[i].length ≤ 100,
    0 ≤ matrix[i][j] ≤ 9.

  • [output] integer

    • The number of different 2 × 2 squares in matrix.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
int differentSquares(arr_arr_integer matrix)
{
	int square[10000]={0, }, count=0;

	for(int i=0;i<matrix.size-1;i++)
		for(int j=0;j<matrix.arr[0].size-1;j++)
			square[1000*matrix.arr[i].arr[j]+100*matrix.arr[i].arr[j+1]+10*matrix.arr[i+1].arr[j]+matrix.arr[i+1].arr[j+1]]=1;

	for(int i=0;i<10000;i++)
		count+=square[i];

	return count;
}

 

728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> File Naming  (0) 2020.04.06
<Codesignal> digitsProduct  (0) 2020.04.06
<Codesignal> sumUpNumbers  (0) 2020.04.06
<Codesignal> Valid Time  (0) 2020.04.06
<Codesignal> longestWord  (0) 2020.04.06

Medium

CodeMaster has just returned from shopping. He scanned the check of the items he bought and gave the resulting string to Ratiorg to figure out the total number of purchased items. Since Ratiorg is a bot he is definitely going to automate it, so he needs a program that sums up all the numbers which appear in the given input.

Help Ratiorg by writing a function that returns the sum of numbers that appear in the given inputString.

Example

For inputString = "2 apples, 12 oranges", the output should be
sumUpNumbers(inputString) = 14.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    Guaranteed constraints:
    0 ≤ inputString.length ≤ 10^5.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int sumUpNumbers(char *inputString)
{
	int sum=0;

	for(int i=0;i<strlen(inputString);i++)
	{
		int number=0;

		while(isdigit(inputString[i]) && i<strlen(inputString))
		{
			number*=10;
			number+=inputString[i]-'0';
			i++;
		}

		sum+=number;
	}

	return sum;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> digitsProduct  (0) 2020.04.06
<Codesignal> Different Squares  (0) 2020.04.06
<Codesignal> Valid Time  (0) 2020.04.06
<Codesignal> longestWord  (0) 2020.04.06
<Codesignal> deleteDigit  (0) 2020.04.06

Easy

Check if the given string is a correct time representation of the 24-hour clock.

Example

  • For time = "13:58", the output should be
    validTime(time) = true;
  • For time = "25:51", the output should be
    validTime(time) = false;
  • For time = "02:76", the output should be
    validTime(time) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string time

    A string representing time in HH:MM format. It is guaranteed that the first two characters, as well as the last two characters, are digits.

  • [output] boolean

    • true if the given representation is correct, false otherwise.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool validTime(char *time)
{
	return strlen(time)==5 && 10*(time[0]-'0')+time[1]-'0'<24 && time[2]==':' && 10*(time[3]-'0')+time[4]-'0'<60;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Different Squares  (0) 2020.04.06
<Codesignal> sumUpNumbers  (0) 2020.04.06
<Codesignal> longestWord  (0) 2020.04.06
<Codesignal> deleteDigit  (0) 2020.04.06
<Codesignal> chessKnight  (0) 2020.04.06

+ Recent posts