Medium

An email address such as "John.Smith@example.com" is made up of a local part ("John.Smith"), an "@" symbol, then a domain part ("example.com").

The domain name part of an email address may only consist of letters, digits, hyphens and dots. The local part, however, also allows a lot of different special characters. Here you can look at several examples of correct and incorrect email addresses.

Given a valid email address, find its domain part.

Example

  • For address = "prettyandsimple@example.com", the output should be
    findEmailDomain(address) = "example.com";
  • For address = "fully-qualified-domain@codesignal.com", the output should be
    findEmailDomain(address) = "codesignal.com".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string address

    Guaranteed constraints:
    10 ≤ address.length ≤ 50.

  • [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 *findEmailDomain(char *address)
{
	for(int i=strlen(address)-1;;i--)
		if(address[i-1]=='@')
			return address+i;
}
728x90

Easy

Given a string, check if it can become a palindrome through a case change of some (possibly, none) letters.

Example

  • For inputString = "AaBaa", the output should be
    isCaseInsensitivePalindrome(inputString) = true.

    "aabaa" is a palindrome as well as "AABAA", "aaBaa", etc.

  • For inputString = "abac", the output should be
    isCaseInsensitivePalindrome(inputString) = false.

    All the strings which can be obtained via changing case of some group of letters, i.e. "abac", "Abac", "aBAc" and 13 more, are not palindromes.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    Non-empty string consisting of English letters.

    Guaranteed constraints:
    4 ≤ inputString.length ≤ 10.

  • [output] boolean

[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 isCaseInsensitivePalindrome(char *inputString)
{
	for(int i=0;i<strlen(inputString)/2;i++)
		if(tolower(inputString[i])!=tolower(inputString[strlen(inputString)-i-1]))
			return false;

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> HTML End Tag By Start Tag  (0) 2020.04.19
<Codesignal> Find Email Domain  (0) 2020.04.19
<Codesignal> Is Tandem Repeat?  (0) 2020.04.19
<Codesignal> Proper Noun Correction  (0) 2020.04.18
<Codesignal> Enclose In Brackets  (0) 2020.04.18

Easy

Determine whether the given string can be obtained by one concatenation of some string to itself.

Example

  • For inputString = "tandemtandem", the output should be
    isTandemRepeat(inputString) = true;
  • For inputString = "qqq", the output should be
    isTandemRepeat(inputString) = false;
  • For inputString = "2w2ww", the output should be
    isTandemRepeat(inputString) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    Guaranteed constraints:
    2 ≤ inputString.length ≤ 20.

  • [output] boolean

    • true if inputString represents a string concatenated to itself, 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 isTandemRepeat(char *inputString)
{
	for(int i=0;i<strlen(inputString)/2;i++)
		if(inputString[i]!=inputString[strlen(inputString)/2+i])
			return false;

	return strlen(inputString)%2==0;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Find Email Domain  (0) 2020.04.19
<Codesignal> Is Case-Insensitive Palindrome?  (0) 2020.04.19
<Codesignal> Proper Noun Correction  (0) 2020.04.18
<Codesignal> Enclose In Brackets  (0) 2020.04.18
<Codesignal> Rectangle Rotation  (1) 2020.04.18

Easy

Proper nouns always begin with a capital letter, followed by small letters.

Correct a given proper noun so that it fits this statement.

Example

  • For noun = "pARiS", the output should be
    properNounCorrection(noun) = "Paris";
  • For noun = "John", the output should be
    properNounCorrection(noun) = "John".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string noun

    A string representing a proper noun with a mix of capital and small English letters.

    Guaranteed constraints:
    1 ≤ noun.length ≤ 10.

  • [output] string

    • Corrected (if needed) noun.

[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 *properNounCorrection(char *noun)
{
	noun[0]=toupper(noun[0]);
	for(int i=1;i<strlen(noun);i++)
		noun[i]=tolower(noun[i]);

	return noun;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Is Case-Insensitive Palindrome?  (0) 2020.04.19
<Codesignal> Is Tandem Repeat?  (0) 2020.04.19
<Codesignal> Enclose In Brackets  (0) 2020.04.18
<Codesignal> Rectangle Rotation  (1) 2020.04.18
<Codesignal> Weak Numbers  (0) 2020.04.18

Easy

Given a string, enclose it in round brackets.

Example

For inputString = "abacaba", the output should be
encloseInBrackets(inputString) = "(abacaba)".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    Guaranteed constraints:
    0 ≤ inputString.length ≤ 10.

  • [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 *encloseInBrackets(char *inputString)
{
	int N=strlen(inputString);

	for(int i=N;i>0;i--)
		inputString[i]=inputString[i-1];
	inputString[0]='(';
	inputString[N+1]=')';
	inputString[N+2]='\0';

	return inputString;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Is Tandem Repeat?  (0) 2020.04.19
<Codesignal> Proper Noun Correction  (0) 2020.04.18
<Codesignal> Rectangle Rotation  (1) 2020.04.18
<Codesignal> Weak Numbers  (0) 2020.04.18
<Codesignal> Comfortable Numbers  (0) 2020.04.17

Medium

A rectangle with sides equal to even integers a and b is drawn on the Cartesian plane. Its center (the intersection point of its diagonals) coincides with the point (0, 0), but the sides of the rectangle are not parallel to the axes; instead, they are forming 45 degree angles with the axes.

How many points with integer coordinates are located inside the given rectangle (including on its sides)?

Example

For a = 6 and b = 4, the output should be
rectangleRotation(a, b) = 23.

The following picture illustrates the example, and the 23 points are marked green.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer a

    A positive even integer.

    Guaranteed constraints:
    2 ≤ a ≤ 50.

  • [input] integer b

    A positive even integer.

    Guaranteed constraints:
    2 ≤ b ≤ 50.

  • [output] integer

    • The number of inner points with integer coordinates.

[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 rectangleRotation(int a, int b)
{
	a/=sqrt(2);
	b/=sqrt(2);

	return (2*a*b+a+b)|1;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Proper Noun Correction  (0) 2020.04.18
<Codesignal> Enclose In Brackets  (0) 2020.04.18
<Codesignal> Weak Numbers  (0) 2020.04.18
<Codesignal> Comfortable Numbers  (0) 2020.04.17
<Codesignal> Pages Numbering With Ink  (0) 2020.04.16

Easy

We define the weakness of number x as the number of positive integers smaller than x that have more divisors than x.

It follows that the weaker the number, the greater overall weakness it has. For the given integer n, you need to answer two questions:

  • what is the weakness of the weakest numbers in the range [1, n]?
  • how many numbers in the range [1, n] have this weakness?

Return the answer as an array of two elements, where the first element is the answer to the first question, and the second element is the answer to the second question.

Example

For n = 9, the output should be
weakNumbers(n) = [2, 2].

Here are the number of divisors and the specific weakness of each number in range [1, 9]:

  • 1: d(1) = 1, weakness(1) = 0;
  • 2: d(2) = 2, weakness(2) = 0;
  • 3: d(3) = 2, weakness(3) = 0;
  • 4: d(4) = 3, weakness(4) = 0;
  • 5: d(5) = 2, weakness(5) = 1;
  • 6: d(6) = 4, weakness(6) = 0;
  • 7: d(7) = 2, weakness(7) = 2;
  • 8: d(8) = 4, weakness(8) = 0;
  • 9: d(9) = 3, weakness(9) = 2.

As you can see, the maximal weakness is 2, and there are 2 numbers with that weakness level.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    Guaranteed constraints:
    1 ≤ n ≤ 1000.

  • [output] array.integer

    • Array of two elements: the weakness of the weakest number, and the number of integers in range [1, n] with this weakness.

[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 weakNumbers(int n)
{
	arr_integer ans=alloc_arr_integer(2);
	int d[1001]={0, }, weakness[1001]={0, };
	ans.arr[0]=ans.arr[1]=0;

	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=i;j++)
			d[i]+=i%j==0;
		for(int j=1;j<i;j++)
			weakness[i]+=d[i]<d[j];
		ans.arr[0]=weakness[i]>ans.arr[0]?weakness[i]:ans.arr[0];
	}

	for(int i=1;i<=n;i++)
		ans.arr[1]+=weakness[i]==ans.arr[0];

	return ans;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Enclose In Brackets  (0) 2020.04.18
<Codesignal> Rectangle Rotation  (1) 2020.04.18
<Codesignal> Comfortable Numbers  (0) 2020.04.17
<Codesignal> Pages Numbering With Ink  (0) 2020.04.16
<Codesignal> Square Digits Sequence  (0) 2020.04.15

Easy

Let's say that number a feels comfortable with number b if a ≠ b and b lies in the segment [a - s(a), a + s(a)], where s(x) is the sum of x's digits.

How many pairs (a, b) are there, such that a < b, both a and b lie on the segment [l, r], and each number feels comfortable with the other (so a feels comfortable with b and b feels comfortable with a)?

Example

For l = 10 and r = 12, the output should be
comfortableNumbers(l, r) = 2.

Here are all values of s(x) to consider:

  • s(10) = 1, so 10 is comfortable with 9 and 11;
  • s(11) = 2, so 11 is comfortable with 9, 10, 12 and 13;
  • s(12) = 3, so 12 is comfortable with 9, 10, 11, 13, 14 and 15.

Thus, there are 2 pairs of numbers comfortable with each other within the segment [10; 12]: (10, 11) and (11, 12).

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer l

    Guaranteed constraints:
    1 ≤ l ≤ r ≤ 1000.

  • [input] integer r

    Guaranteed constraints:
    1 ≤ l ≤ r ≤ 1000.

  • [output] integer

    • The number of pairs satisfying all the above conditions.

[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 s(int x)
{
	int sum=0;

	while(x>0)
	{
		sum+=x%10;
		x/=10;
	}

	return sum;
}

int comfortableNumbers(int l,int r)
{
	int count=0;

	for(int i=l;i<=r;i++)
		for(int j=i-s(i);j<=i+s(i);j++)
			count+=j!=i&&j>=l&&j<=r&&i>=j-s(j)&&i<=j+s(j);

	return count/2;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Rectangle Rotation  (1) 2020.04.18
<Codesignal> Weak Numbers  (0) 2020.04.18
<Codesignal> Pages Numbering With Ink  (0) 2020.04.16
<Codesignal> Square Digits Sequence  (0) 2020.04.15
<Codesignal> Is Sum of Consecutive 2  (0) 2020.04.15

+ Recent posts