Easy

Correct variable names consist only of English letters, digits and underscores and they can't start with a digit.

Check if the given string is a correct variable name.

Example

  • For name = "var_1__Int", the output should be
    variableName(name) = true;
  • For name = "qq-q", the output should be
    variableName(name) = false;
  • For name = "2w2", the output should be
    variableName(name) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string name

    Guaranteed constraints:
    1 ≤ name.length ≤ 10.

  • [output] boolean

    • true if name is a correct variable name, 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 variableName(char *name)
{
	if(isdigit(name[0]))
		return false;
	for(int i=0;i<strlen(name);i++)
		if(!(isalnum(name[i]) || name[i]=='_'))
			return false;

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> chessBoardCellColor  (0) 2020.04.05
<Codesignal> alphabeticShift  (0) 2020.04.05
<Codesignal> evenDigitsOnly  (0) 2020.04.05
<Codesignal> Array Replace  (0) 2020.04.05
<Codesignal> Minesweeper  (0) 2020.04.05

Easy

Check if all digits of the given integer are even.

Example

  • For n = 248622, the output should be
    evenDigitsOnly(n) = true;
  • For n = 642386, the output should be
    evenDigitsOnly(n) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    Guaranteed constraints:
    1 ≤ n ≤ 10^9.

  • [output] boolean

    • true if all digits of n are even, 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 evenDigitsOnly(int n)
{
	while(n>0)
	{
		if(n%2==1)
			return false;
		n/=10;
	}

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> alphabeticShift  (0) 2020.04.05
<Codesignal> variableName  (0) 2020.04.05
<Codesignal> Array Replace  (0) 2020.04.05
<Codesignal> Minesweeper  (0) 2020.04.05
<Codesignal> Box Blur  (0) 2020.04.05

Easy

Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem.

Example

For inputArray = [1, 2, 1], elemToReplace = 1, and substitutionElem = 3, the output should be
arrayReplace(inputArray, elemToReplace, substitutionElem) = [3, 2, 3].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Guaranteed constraints:
    0 ≤ inputArray.length ≤ 10^4,
    0 ≤ inputArray[i] ≤ 10^9.

  • [input] integer elemToReplace

    Guaranteed constraints:
    0 ≤ elemToReplace ≤ 10^9.

  • [input] integer substitutionElem

    Guaranteed constraints:
    0 ≤ substitutionElem ≤ 10^9.

  • [output] 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_integer arrayReplace(arr_integer inputArray,int elemToReplace,int substitutionElem)
{
	for(int i=0;i<inputArray.size;i++)
		inputArray.arr[i]=inputArray.arr[i]==elemToReplace?substitutionElem:inputArray.arr[i];

	return inputArray;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> variableName  (0) 2020.04.05
<Codesignal> evenDigitsOnly  (0) 2020.04.05
<Codesignal> Minesweeper  (0) 2020.04.05
<Codesignal> Box Blur  (0) 2020.04.05
<Codesignal> avoidObstacles  (0) 2020.04.05

Medium

In the popular Minesweeper game you have a board with some mines and those cells that don't contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to create a Minesweeper game setup.

Example

For

matrix = [[true, false, false],

                   [false, true, false],

                   [false, false, false]]

the output should be

minesweeper(matrix) = [[1, 2, 1],

                                                [2, 1, 1],

                                                [1, 1, 1]]

Check out the image below for better understanding:

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.array.boolean matrix

    A non-empty rectangular matrix consisting of boolean values - true if the corresponding cell contains a mine, false otherwise.

    Guaranteed constraints:
    2 ≤ matrix.length ≤ 100,
    2 ≤ matrix[0].length ≤ 100.

  • [output] array.array.integer

    • Rectangular matrix of the same size as matrix each cell of which contains an integer equal to the number of mines in the neighboring cells. Two cells are called neighboring if they share at least one corner.

[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 minesweeper(arr_arr_boolean matrix)
{
	arr_arr_integer mine=alloc_arr_arr_integer(matrix.size);
	for(int i=0;i<mine.size;i++)
		mine.arr[i]=alloc_arr_integer(matrix.arr[0].size);

	for(int i=0;i<mine.size;i++)
		for(int j=0;j<mine.arr[0].size;j++)
		{
			mine.arr[i].arr[j]=0;

			for(int k=-1;k<=1;k++)
				for(int l=-1;l<=1;l++)
					if(!(i+k<0 || j+l<0 || (k==0&&l==0) || i+k>=mine.size || j+l>=mine.arr[0].size))
						mine.arr[i].arr[j]+=matrix.arr[i+k].arr[j+l]==true;
		}

	return mine;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> evenDigitsOnly  (0) 2020.04.05
<Codesignal> Array Replace  (0) 2020.04.05
<Codesignal> Box Blur  (0) 2020.04.05
<Codesignal> avoidObstacles  (0) 2020.04.05
<Codesignal> isIPv4Address  (0) 2020.04.05

Medium

Last night you partied a little too hard. Now there's a black and white photo of you that's about to go viral! You can't let this ruin your reputation, so you want to apply the box blur algorithm to the photo to hide its content.

The pixels in the input image are represented as integers. The algorithm distorts the input image in the following way: Every pixel x in the output image has a value equal to the average value of the pixel values from the 3 × 3 square that has its center at x, including x itself. All the pixels on the border of x are then removed.

Return the blurred image as an integer, with the fractions rounded down.

Example

For

image = [[1, 1, 1],

                   [1, 7, 1],

                   [1, 1, 1]]

the output should be boxBlur(image) = [[1]].

To get the value of the middle pixel in the input 3 × 3 square: (1 + 1 + 1 + 1 + 7 + 1 + 1 + 1 + 1) = 15 / 9 = 1.66666 = 1. The border pixels are cropped from the final result.

For

image = [[7, 4, 0, 1],

                   [5, 6, 2, 2],

                 [6, 10, 7, 8],

                   [1, 4, 2, 0]]

the output should be

boxBlur(image) = [[5, 4],

                                      [4, 4]]

There are four 3 × 3 squares in the input image, so there should be four integers in the blurred output. To get the first value: (7 + 4 + 0 + 5 + 6 + 2 + 6 + 10 + 7) = 47 / 9 = 5.2222 = 5. The other three integers are obtained the same way, then the surrounding integers are cropped from the final result.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.array.integer image

    An image, stored as a rectangular matrix of non-negative integers.

    Guaranteed constraints:
    3 ≤ image.length ≤ 100,
    3 ≤ image[0].length ≤ 100,
    0 ≤ image[i][j] ≤ 255.

  • [output] array.array.integer

    • A blurred image represented as integers, obtained through the process in the description.

[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 boxBlur(arr_arr_integer image)
{
	arr_arr_integer returnBox=alloc_arr_arr_integer(image.size-2);
	for(int i=0;i<returnBox.size;i++)
		returnBox.arr[i]=alloc_arr_integer(image.arr[0].size-2);

	for(int i=0;i<returnBox.size;i++)
		for(int j=0;j<returnBox.arr[0].size;j++)
			returnBox.arr[i].arr[j]=0;

	for(int i=1;i<image.size-1;i++)
		for(int j=1;j<image.arr[0].size-1;j++)
		{
			for(int k=-1;k<=1;k++)
				for(int l=-1;l<=1;l++)
					returnBox.arr[i-1].arr[j-1]+=image.arr[i+k].arr[j+l];
			returnBox.arr[i-1].arr[j-1]/=9;
		}

	return returnBox;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Array Replace  (0) 2020.04.05
<Codesignal> Minesweeper  (0) 2020.04.05
<Codesignal> avoidObstacles  (0) 2020.04.05
<Codesignal> isIPv4Address  (0) 2020.04.05
<Codesignal> arrayMaximalAdjacentDifference  (0) 2020.04.05

Easy

You are given an array of integers representing coordinates of obstacles situated on a straight line.

Assume that you are jumping from the point with coordinate 0 to the right. You are allowed only to make jumps of the same length represented by some integer.

Find the minimal length of the jump enough to avoid all the obstacles.

Example

For inputArray = [5, 3, 6, 7, 9], the output should be
avoidObstacles(inputArray) = 4.

Check out the image below for better understanding:

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Non-empty array of positive integers.

    Guaranteed constraints:
    2 ≤ inputArray.length ≤ 1000,
    1 ≤ inputArray[i] ≤ 1000.

  • [output] integer

    • The desired length.

[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 avoidObstacles(arr_integer inputArray)
{
	int max=0, N=1;

	for(int i=0;i<inputArray.size;i++)
		max=inputArray.arr[i]>max?inputArray.arr[i]:max;

	while(N++)
	{
		bool noObstacle=true;
		for(int i=0;i<=max;i+=N)
		{
			for(int j=0;j<inputArray.size;j++)
				if(i==inputArray.arr[j])
				{
					noObstacle=false;
					break;
				}
			if(!noObstacle)
				break;
		}
		if(noObstacle)
			return N;
	}
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Minesweeper  (0) 2020.04.05
<Codesignal> Box Blur  (0) 2020.04.05
<Codesignal> isIPv4Address  (0) 2020.04.05
<Codesignal> arrayMaximalAdjacentDifference  (0) 2020.04.05
<Codesignal> areEquallyStrong  (0) 2020.04.05

Medium

An IP address is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. There are two versions of the Internet protocol, and thus two versions of addresses. One of them is the IPv4 address.

Given a string, find out if it satisfies the IPv4 address naming rules.

Example

  • For inputString = "172.16.254.1", the output should be
    isIPv4Address(inputString) = true;

  • For inputString = "172.316.254.1", the output should be
    isIPv4Address(inputString) = false.

    316 is not in range [0, 255].

  • For inputString = ".254.255.0", the output should be
    isIPv4Address(inputString) = false.

    There is no first number.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A string consisting of digits, full stops and lowercase English letters.

    Guaranteed constraints:
    1 ≤ inputString.length ≤ 30.

  • [output] boolean

    • true if inputString satisfies the IPv4 address naming rules, 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 isIPv4Address(char *inputString)
{
	int dotCount=0, N=0;

	for(int i=0;i<strlen(inputString);i++)
	{
		if(!isdigit(inputString[i]) && inputString[i]!='.')
			return false;

		if(isdigit(inputString[i]))
		{
			N*=10;
			N+=inputString[i]-'0';

			if(N==0 && inputString[i]=='0' && inputString[i+1]!='.' && i!=strlen(inputString)-1)
				return false;
			if(N>255)
				return false;
		}

		if(inputString[i]=='.')
		{
			dotCount++;
			if(!isdigit(inputString[i-1]) || i==0 || N>255)
				return false;
			N=0;
		}

		if(i==strlen(inputString)-1 && inputString[i]=='.')
			return false;
	}

	return dotCount==3 && N<256;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Box Blur  (0) 2020.04.05
<Codesignal> avoidObstacles  (0) 2020.04.05
<Codesignal> arrayMaximalAdjacentDifference  (0) 2020.04.05
<Codesignal> areEquallyStrong  (0) 2020.04.05
<Codesignal> palindromeRearranging  (0) 2020.04.05

Easy

Given an array of integers, find the maximal absolute difference between any two of its adjacent elements.

Example

For inputArray = [2, 4, 1, 0], the output should be
arrayMaximalAdjacentDifference(inputArray) = 3.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Guaranteed constraints:
    3 ≤ inputArray.length ≤ 10,
    -15 ≤ inputArray[i] ≤ 15.

  • [output] integer

    • The maximal absolute difference.

[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 arrayMaximalAdjacentDifference(arr_integer inputArray)
{
	int max=0;

	for(int i=1;i<inputArray.size;i++)
		max=abs(inputArray.arr[i]-inputArray.arr[i-1])>max?abs(inputArray.arr[i]-inputArray.arr[i-1]):max;

	return max;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> avoidObstacles  (0) 2020.04.05
<Codesignal> isIPv4Address  (0) 2020.04.05
<Codesignal> areEquallyStrong  (0) 2020.04.05
<Codesignal> palindromeRearranging  (0) 2020.04.05
<Codesignal> arrayChange  (0) 2020.04.05

+ Recent posts