Easy

Find the leftmost digit that occurs in a given string.

Example

  • For inputString = "var_1__Int", the output should be
    firstDigit(inputString) = '1';
  • For inputString = "q2q-q", the output should be
    firstDigit(inputString) = '2';
  • For inputString = "0ss", the output should be
    firstDigit(inputString) = '0'.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A string containing at least one digit.

    Guaranteed constraints:
    3 ≤ inputString.length ≤ 10.

  • [output] char

[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

char firstDigit(char *inputString)
{
	for(int i=0;i<strlen(inputString);i++)
		if(isdigit(inputString[i]))
			return inputString[i];
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> arrayMaxConsecutiveSum  (0) 2020.04.05
<Codesignal> differentSymbolsNaive  (0) 2020.04.05
<Codesignal> extractEachKth  (0) 2020.04.05
<Codesignal> stringsRearrangement  (0) 2020.04.05
<Codesignal> absoluteValuesSumMinimization  (0) 2020.04.05

Easy

Given array of integers, remove each kth element from it.

Example

For inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and k = 3, the output should be
extractEachKth(inputArray, k) = [1, 2, 4, 5, 7, 8, 10].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Guaranteed constraints:
    5 ≤ inputArray.length ≤ 15,
    -20 ≤ inputArray[i] ≤ 20.

  • [input] integer k

    Guaranteed constraints:
    1 ≤ k ≤ 10.

  • [output] array.integer

    • inputArray without elements k - 1, 2k - 1, 3k - 1 etc.

[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 extractEachKth(arr_integer inputArray,int k)
{
	arr_integer returnArray=alloc_arr_integer(inputArray.size-inputArray.size/k);

	for(int i=0;i<inputArray.size;i++)
	{
		if((i+1)%k==0)
			continue;
		returnArray.arr[i-(i+1)/k]=inputArray.arr[i];
	}

	return returnArray;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> differentSymbolsNaive  (0) 2020.04.05
<Codesignal> firstDigit  (0) 2020.04.05
<Codesignal> stringsRearrangement  (0) 2020.04.05
<Codesignal> absoluteValuesSumMinimization  (0) 2020.04.05
<Codesignal> depositProfit  (0) 2020.04.05

Medium

Given an array of equal-length strings, you'd like to know if it's possible to rearrange the order of the elements in such a way that each consecutive pair of strings differ by exactly one character. Return true if it's possible, and false if not.

Note: You're only rearranging the order of the strings, not the order of the letters within the strings!

Example

  • For inputArray = ["aba", "bbb", "bab"], the output should be
    stringsRearrangement(inputArray) = false.

    There are 6 possible arrangements for these strings:

    • ["aba", "bbb", "bab"]
    • ["aba", "bab", "bbb"]
    • ["bbb", "aba", "bab"]
    • ["bbb", "bab", "aba"]
    • ["bab", "bbb", "aba"]
    • ["bab", "aba", "bbb"]

    None of these satisfy the condition of consecutive strings differing by 1 character, so the answer is false.

  • For inputArray = ["ab", "bb", "aa"], the output should be
    stringsRearrangement(inputArray) = true.

    It's possible to arrange these strings in a way that each consecutive pair of strings differ by 1 character (eg: "aa", "ab", "bb" or "bb", "ab", "aa"), so return true.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.string inputArray

    A non-empty array of strings of lowercase letters.

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

  • [output] boolean

    • Return true if the strings can be reordered in such a way that each neighbouring pair of strings differ by exactly one character (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 stringsRearrangement(arr_string inputArray)
{
	int N=0;

	for(int i=0;i<inputArray.size;i++) 
	{
		int count=0;
		bool isSame=false;

		for(int j=0;j<inputArray.size;j++)
		{
			if(i==j)
				continue;
			int difference=0;

			for(int k=0;k<strlen(inputArray.arr[0]);k++)
				difference+=inputArray.arr[i][k]!=inputArray.arr[j][k];

			count+=difference==1;
		}

		for(int j=0;j<i;j++)
			if(strcmp(inputArray.arr[i],inputArray.arr[j])==0)
			{
				isSame=true;
				break;
			}

		if(count==0)
			return false;

		N+=isSame?count%3==count:count==1;
	}

	return N<=2;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> firstDigit  (0) 2020.04.05
<Codesignal> extractEachKth  (0) 2020.04.05
<Codesignal> absoluteValuesSumMinimization  (0) 2020.04.05
<Codesignal> depositProfit  (0) 2020.04.05
<Codesignal> Circle of Numbers  (0) 2020.04.05

Medium

Given a sorted array of integers a, your task is to determine which element of a is closest to all other values of a. In other words, find the element x in a, which minimizes the following sum:

abs(a[0] - x) + abs(a[1] - x) + ... + abs(a[a.length - 1] - x)

(where abs denotes the absolute value)

If there are several possible answers, output the smallest one.

Example

  • For a = [2, 4, 7], the output should be absoluteValuesSumMinimization(a) = 4.

    • for x = 2, the value will be abs(2 - 2) + abs(4 - 2) + abs(7 - 2) = 7.
    • for x = 4, the value will be abs(2 - 4) + abs(4 - 4) + abs(7 - 4) = 5.
    • for x = 7, the value will be abs(2 - 7) + abs(4 - 7) + abs(7 - 7) = 8.

    The lowest possible value is when x = 4, so the answer is 4.

  • For a = [2, 3], the output should be absoluteValuesSumMinimization(a) = 2.

    • for x = 2, the value will be abs(2 - 2) + abs(3 - 2) = 1.
    • for x = 3, the value will be abs(2 - 3) + abs(3 - 3) = 1.

    Because there is a tie, the smallest x between x = 2 and x = 3 is the answer.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer a

    A non-empty array of integers, sorted in ascending order.

    Guaranteed constraints:
    1 ≤ a.length ≤ 1000,
    -10^6 ≤ a[i] ≤ 10^6.

  • [output] integer

    • An integer representing the element from a that minimizes the sum of its absolute differences with all other elements.

[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 absoluteValuesSumMinimization(arr_integer a)
{
	int min=1000000000, minValue;

	for(int i=0;i<a.size;i++)
	{
		int sum=0;

		for(int j=0;j<a.size;j++)
			sum+=abs(a.arr[i]-a.arr[j]);

		if(sum<min)
		{
			min=sum;
			minValue=a.arr[i];
		}
	}

	return minValue;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> extractEachKth  (0) 2020.04.05
<Codesignal> stringsRearrangement  (0) 2020.04.05
<Codesignal> depositProfit  (0) 2020.04.05
<Codesignal> Circle of Numbers  (0) 2020.04.05
<Codesignal> chessBoardCellColor  (0) 2020.04.05

Medium

You have deposited a specific amount of money into your bank account. Each year your balance increases at the same growth rate. With the assumption that you don't make any additional deposits, find out how long it would take for your balance to pass a specific threshold.

Example

For deposit = 100, rate = 20, and threshold = 170, the output should be
depositProfit(deposit, rate, threshold) = 3.

Each year the amount of money in your account increases by 20%. So throughout the years, your balance would be:

  • year 0: 100;
  • year 1: 120;
  • year 2: 144;
  • year 3: 172.8.

Thus, it will take 3 years for your balance to pass the threshold, so the answer is 3.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer deposit

    The initial deposit, guaranteed to be a positive integer.

    Guaranteed constraints:
    1 ≤ deposit ≤ 100.

  • [input] integer rate

    The rate of increase. Each year the balance increases by the rate percent of the current sum.

    Guaranteed constraints:
    1 ≤ rate ≤ 100.

  • [input] integer threshold

    The target balance.

    Guaranteed constraints:
    deposit < threshold ≤ 200.

  • [output] integer

    • The number of years it would take to hit the threshold.

[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형에서 double형으로 변환하여 풀었다.

int depositProfit(double deposit,double rate,double threshold)
{
	int i;

	for(i=0;deposit<threshold;i++)
	{
		deposit*=(rate+100.0);
		deposit/=100.0;
	}

	return i;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> stringsRearrangement  (0) 2020.04.05
<Codesignal> absoluteValuesSumMinimization  (0) 2020.04.05
<Codesignal> Circle of Numbers  (0) 2020.04.05
<Codesignal> chessBoardCellColor  (0) 2020.04.05
<Codesignal> alphabeticShift  (0) 2020.04.05

Easy

Consider integer numbers from 0 to n - 1 written down along the circle in such a way that the distance between any two neighboring numbers is equal (note that 0 and n - 1 are neighboring, too).

Given n and firstNumber, find the number which is written in the radially opposite position to firstNumber.

Example

For n = 10 and firstNumber = 2, the output should be
circleOfNumbers(n, firstNumber) = 7.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    A positive even integer.

    Guaranteed constraints:
    4 ≤ n ≤ 20.

  • [input] integer firstNumber

    Guaranteed constraints:
    0 ≤ firstNumber ≤ n - 1.

  • [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 circleOfNumbers(int n,int firstNumber)
{
	return firstNumber+n/2>=n?firstNumber-n/2:firstNumber+n/2;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> absoluteValuesSumMinimization  (0) 2020.04.05
<Codesignal> depositProfit  (0) 2020.04.05
<Codesignal> chessBoardCellColor  (0) 2020.04.05
<Codesignal> alphabeticShift  (0) 2020.04.05
<Codesignal> variableName  (0) 2020.04.05

Easy

Given two cells on the standard chess board, determine whether they have the same color or not.

Example

  • For cell1 = "A1" and cell2 = "C3", the output should be
    chessBoardCellColor(cell1, cell2) = true.

  • For cell1 = "A1" and cell2 = "H3", the output should be
    chessBoardCellColor(cell1, cell2) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string cell1

    Guaranteed constraints:
    cell1.length = 2,
    'A' ≤ cell1[0] ≤ 'H',
    1 ≤ cell1[1] ≤ 8.

  • [input] string cell2

    Guaranteed constraints:
    cell2.length = 2,
    'A' ≤ cell2[0] ≤ 'H',
    1 ≤ cell2[1] ≤ 8.

  • [output] boolean

    • true if both cells have the same color, 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 chessBoardCellColor(char *cell1,char *cell2)
{
	return abs(cell1[0]-cell2[0])%2==abs(cell1[1]-cell2[1])%2;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> depositProfit  (0) 2020.04.05
<Codesignal> Circle of Numbers  (0) 2020.04.05
<Codesignal> alphabeticShift  (0) 2020.04.05
<Codesignal> variableName  (0) 2020.04.05
<Codesignal> evenDigitsOnly  (0) 2020.04.05

Easy

Given a string, your task is to replace each of its characters by the next one in the English alphabet; i.e. replace a with b, replace b with c, etc (z would be replaced by a).

Example

For inputString = "crazy", the output should be alphabeticShift(inputString) = "dsbaz".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A non-empty string consisting of lowercase English characters.

    Guaranteed constraints:
    1 ≤ inputString.length ≤ 1000.

  • [output] string

    • The resulting string after replacing each of its characters.

[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

char *alphabeticShift(char *inputString)
{
	for(int i=0;i<strlen(inputString);i++)
	{
		inputString[i]++;
		if(!isalpha(inputString[i]))
			inputString[i]-=26;
	}

	return inputString;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Circle of Numbers  (0) 2020.04.05
<Codesignal> chessBoardCellColor  (0) 2020.04.05
<Codesignal> variableName  (0) 2020.04.05
<Codesignal> evenDigitsOnly  (0) 2020.04.05
<Codesignal> Array Replace  (0) 2020.04.05

+ Recent posts