Medium

Implement the missing code, denoted by ellipses. You may not modify the pre-existing code.

You're given two integers, n and m. Find position of the rightmost pair of equal bits in their binary representations (it is guaranteed that such a pair exists), counting from right to left.

Return the value of 2^position_of_the_found_pair (0-based).

Example

For n = 10 and m = 11, the output should be
equalPairOfBits(n, m) = 2.

10v10 = 1010v2, 11v10 = 1011v2, the position of the rightmost pair of equal bits is the bit at position 1 (0-based) from the right in the binary representations.
So the answer is 2^1 = 2.

Input/Output

  • [execution time limit] 0.5 seconds (cpp)

  • [input] integer n

    Guaranteed constraints:
    0 ≤ n ≤ 2^30.

  • [input] integer m

    Guaranteed constraints:
    0 ≤ m ≤ 2^30.

  • [output] integer

[C++] Syntax Tips

// Prints help message to the console
// Returns a string
std::string helloWorld(std::string name) {
    std::cout << "This prints to the console when you Run Tests" << std::endl;
    return "Hello, " + name;
}

더보기

Solution

int equalPairOfBits(int n, int m) {
  return n+m+1&~m-n;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Higher Version  (0) 2020.06.27
<Codesignal> Is Unstable Pair?  (0) 2020.06.26
<Codesignal> Different Rightmost Bit  (0) 2020.06.16
<Codesignal> Swap Adjacent Bits  (0) 2020.06.16
<Codesignal> Stolen Lunch  (0) 2020.05.29

Medium

Implement the missing code, denoted by ellipses. You may not modify the pre-existing code.

You're given two integers, n and m. Find position of the rightmost bit in which they differ in their binary representations (it is guaranteed that such a bit exists), counting from right to left.

Return the value of 2^position_of_the_found_bit (0-based).

Example

    • For n = 11 and m = 13, the output should be
      differentRightmostBit(n, m) = 2.

      11v10 = 1011v2, 13v10 = 1101v2, the rightmost bit in which they differ is the bit at position 1 (0-based) from the right in the binary representations.
      So the answer is 2^1 = 2.

    • For n = 7 and m = 23, the output should be
      differentRightmostBit(n, m) = 16.

      7v10 = 111v2, 23v10 = 10111v2, i.e.

    • 00111

            10111

So the answer is 2^4 = 16.

 

Input/Output

  • [execution time limit] 0.5 seconds (cpp)

  • [input] integer n

    Guaranteed constraints:
    0 ≤ n ≤ 2^30.

  • [input] integer m

    Guaranteed constraints:
    0 ≤ m ≤ 2^30,
    n ≠ m.

  • [output] integer

[C++] Syntax Tips

// Prints help message to the console
// Returns a string
std::string helloWorld(std::string name) {
    std::cout << "This prints to the console when you Run Tests" << std::endl;
    return "Hello, " + name;
}

더보기

Solution

int differentRightmostBit(int n, int m) {
  return (n^m)&-(n^m);
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Is Unstable Pair?  (0) 2020.06.26
<Codesignal> Equal Pair of Bits  (0) 2020.06.17
<Codesignal> Swap Adjacent Bits  (0) 2020.06.16
<Codesignal> Stolen Lunch  (0) 2020.05.29
<Codesignal> Cipher 26  (0) 2020.05.29

Easy

Implement the missing code, denoted by ellipses. You may not modify the pre-existing code.

You're given an arbitrary 32-bit integer n. Take its binary representation, split bits into it in pairs (bit number 0 and 1, bit number 2 and 3, etc.) and swap bits in each pair. Then return the result as a decimal number.

Example

  • For n = 13, the output should be
    swapAdjacentBits(n) = 14.

    1310 = 11012 ~> {11}{01}2 ~> 11102 = 1410.

  • For n = 74, the output should be
    swapAdjacentBits(n) = 133.

    7410 = 010010102 ~> {01}{00}{10}{10}2 ~> 100001012 = 13310.
    Note the preceding zero written in front of the initial number: since both numbers are 32-bit integers, they have 32 bits in their binary representation. The preceding zeros in other cases don't matter, so they are omitted. Here, however, it does make a difference.

Input/Output

  • [execution time limit] 0.5 seconds (cpp)

  • [input] integer n

    Guaranteed constraints:
    0 ≤ n < 2^30.

  • [output] integer

[C++] Syntax Tips

// Prints help message to the console
// Returns a string
std::string helloWorld(std::string name) {
    std::cout << "This prints to the console when you Run Tests" << std::endl;
    return "Hello, " + name;
}

더보기

Solution

int swapAdjacentBits(int n) {
  return (n & 0x55555555) << 1 | (n & 0xAAAAAAAA) >> 1;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Equal Pair of Bits  (0) 2020.06.17
<Codesignal> Different Rightmost Bit  (0) 2020.06.16
<Codesignal> Stolen Lunch  (0) 2020.05.29
<Codesignal> Cipher 26  (0) 2020.05.29
<Codesignal> New Numeral System  (0) 2020.05.24

Easy

When you recently visited your little nephew, he told you a sad story: there's a bully at school who steals his lunch every day, and locks it away in his locker. He also leaves a note with a strange, coded message. Your nephew gave you one of the notes in hope that you can decipher it for him. And you did: it looks like all the digits in it are replaced with letters and vice versa. Digit 0 is replaced with 'a', 1 is replaced with 'b' and so on, with digit 9 replaced by 'j'.

The note is different every day, so you decide to write a function that will decipher it for your nephew on an ongoing basis.

Example

For note = "you'll n4v4r 6u4ss 8t: cdja", the output should be
stolenLunch(note) = "you'll never guess it: 2390".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string note

    A string consisting of lowercase English letters, digits, punctuation marks and whitespace characters (' ').

    Guaranteed constraints:
    0 ≤ note.length ≤ 500.

  • [output] string

    • The deciphered note.

[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 *stolenLunch(char *note)
{
	for(int i=0;i<strlen(note);i++)
		if(isdigit(note[i]))
			note[i]+=49;
		else if(isalpha(note[i])&&note[i]<107)
			note[i]-=49;

	return note;
}

 

728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Different Rightmost Bit  (0) 2020.06.16
<Codesignal> Swap Adjacent Bits  (0) 2020.06.16
<Codesignal> Cipher 26  (0) 2020.05.29
<Codesignal> New Numeral System  (0) 2020.05.24
<Codesignal> Reflect String  (0) 2020.05.24

Easy

You've intercepted an encrypted message, and you are really curious about its contents. You were able to find out that the message initially contained only lowercase English letters, and was encrypted with the following cipher:

  • Let all letters from 'a' to 'z' correspond to the numbers from 0 to 25, respectively.
  • The number corresponding to the ith letter of the encrypted message is then equal to the sum of numbers corresponding to the first i letters of the initial unencrypted message modulo 26.

Now that you know how the message was encrypted, implement the algorithm to decipher it.

Example

For message = "taiaiaertkixquxjnfxxdh", the output should be
cipher26(message) = "thisisencryptedmessage".

The initial message "thisisencryptedmessage" was encrypted as follows:

  • letter 0: t -> 19 -> t;
  • letter 1: th -> (19 + 7) % 26 -> 0 -> a;
  • letter 2: thi -> (19 + 7 + 8) % 26 -> 8 -> i;
  • etc.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string message

    An encrypted string containing only lowercase English letters.

    Guaranteed constraints:
    1 ≤ message.length ≤ 200.

  • [output] string

    • A decrypted message.

[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 *cipher26(char *message)
{
	int sum=0;

	for(int i=0;i<strlen(message);i++)
	{
		message[i]-='a';
		for(int j=0;j<26;j++)
			if((sum+j)%26==message[i])
			{
				sum+=j;
				message[i]=j+'a';
				break;
			}
	}

	return message;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Swap Adjacent Bits  (0) 2020.06.16
<Codesignal> Stolen Lunch  (0) 2020.05.29
<Codesignal> New Numeral System  (0) 2020.05.24
<Codesignal> Reflect String  (0) 2020.05.24
<Codesignal> Character Parity  (0) 2020.05.24

Easy

Your Informatics teacher at school likes coming up with new ways to help you understand the material. When you started studying numeral systems, he introduced his own numeral system, which he's convinced will help clarify things. His numeral system has base 26, and its digits are represented by English capital letters - A for 0, B for 1, and so on.

The teacher assigned you the following numeral system exercise: given a one-digit number, you should find all unordered pairs of one-digit numbers whose values add up to the number.

Example

For number = 'G', the output should be
newNumeralSystem(number) = ["A + G", "B + F", "C + E", "D + D"].

Translating this into the decimal numeral system we get: number = 6, so it is ["0 + 6", "1 + 5", "2 + 4", "3 + 3"].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] char number

    A character representing a correct one-digit number in the new numeral system.

    Guaranteed constraints:
    'A' ≤ number ≤ 'Z'.

  • [output] array.string

    • An array of strings in the format "letter1 + letter2", where "letter1" and "letter2" are correct one-digit numbers in the new numeral system. The strings should be sorted by "letter1".

    Note that "letter1 + letter2" and "letter2 + letter1" are equal pairs and we don't consider them to be different.

[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 newNumeralSystem(char number)
{
	arr_string str=alloc_arr_string((number-'A')/2+1);
	for(int i=0;i<str.size;i++)
	{
		str.arr[i]=(char *)malloc(6*sizeof(char));
		sprintf(str.arr[i],"%c + %c",'A'+i, number-i);
	}

	return str;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Stolen Lunch  (0) 2020.05.29
<Codesignal> Cipher 26  (0) 2020.05.29
<Codesignal> Reflect String  (0) 2020.05.24
<Codesignal> Character Parity  (0) 2020.05.24
<Codesignal> Three Split  (0) 2020.05.24

Easy

Define an alphabet reflection as follows: a turns into z, b turns into y, c turns into x, ..., n turns into m, m turns into n, ..., z turns into a.

Define a string reflection as the result of applying the alphabet reflection to each of its characters.

Reflect the given string.

Example

For inputString = "name", the output should be
reflectString(inputString) = "mznv".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A string of lowercase characters.

    Guaranteed constraints:
    3 ≤ inputString.length ≤ 1000.

  • [output] 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

char *reflectString(char *inputString)
{
	for(int i=0;i<strlen(inputString);i++)
		inputString[i]='a'+'z'-inputString[i];

	return inputString;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Cipher 26  (0) 2020.05.29
<Codesignal> New Numeral System  (0) 2020.05.24
<Codesignal> Character Parity  (0) 2020.05.24
<Codesignal> Three Split  (0) 2020.05.24
<Codesignal> Ada Number  (0) 2020.05.23

Easy

Given a character, check if it represents an odd digit, an even digit or not a digit at all.

Example

  • For symbol = '5', the output should be
    characterParity(symbol) = "odd";
  • For symbol = '8', the output should be
    characterParity(symbol) = "even";
  • For symbol = 'q', the output should be
    characterParity(symbol) = "not a digit".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] char symbol

    A symbol to check.

    Guaranteed constraints:
    symbol is guaranteed to be a UTF-8 symbol.

  • [output] 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

char *characterParity(char symbol)
{
	if(!isdigit(symbol))
		return "not a digit";
	else if((symbol-'0')%2==0)
		return "even";
	else
		return "odd";
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> New Numeral System  (0) 2020.05.24
<Codesignal> Reflect String  (0) 2020.05.24
<Codesignal> Three Split  (0) 2020.05.24
<Codesignal> Ada Number  (0) 2020.05.23
<Codesignal> Integer to String of Fixed width  (0) 2020.05.08

+ Recent posts