Medium

Your friend wants to become a professional tour guide and travel all around the world. In pursuit of this dream, she enrolled in tour guide school. The professors in this school turned out to be very demanding, and one of them gave your friend a difficult assignment that she has to finish over the weekend.

Here's the assignment: Given a list of countries, your friend should identify all the countries that are in Africa. To help her, you have decided to write a function that will find all such countries from any set of countries. The countries table in which the countries are stored has the following structure:

  • name: the name of the country;
  • continent: the continent on which the country is situated;
  • population: the country's population.

Your task is to return a new table that has the same columns, but that only contains the countries from Africa. The countries should be sorted alphabetically by their names.

Example

For the following table countries

name continent population
Austria Europe 8767919
Belize North America 375909
Botswana Africa 2230905
Cambodia Asia 15626444
Cameroon Africa 22709892

the output should be

name continent population
Botswana Africa 2230905
Cameroon Africa 22709892
  • [execution time limit] 10 seconds (mysql)

더보기

Solution

CREATE PROCEDURE countriesSelection()
BEGIN
	/* Write your SQL here. Terminate each statement with a semicolon. */
	SELECT name, continent, population FROM countries WHERE continent="Africa";
END
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> projectList  (0) 2020.12.30
<Codesignal> Alphanumeric Less  (0) 2020.12.30
<Codesignal> Construct Square  (0) 2020.12.30
<Codesignal> Crossword Formation  (0) 2020.12.30
<Codesignal> Decipher  (0) 2020.06.27

Medium

Your boss wants to identify the successful projects running in your company, so he asked you to prepare a list of all the currently active projects and their average monthly income.

You have stored the information about these projects in a simple database with a single Projects table that has five columns:

  • internal_id: the company's internal identifier for the project;
  • project_name: the official name of the project;
  • team_size: the number of employees working on the project;
  • team_lead: the name of the project manager;
  • income: the average monthly income of the project.

Your boss says that internal project ids are irrelevant to him and that he isn't interested in how big the teams are. Since that's the case, he wants you to create another table by removing the internal_id and team_sizecolumns from the existing Projects table. Return it sorted by internal_id in ascending order.

Example

For the following table Projects

internal_id project_name team_size team_lead income
1384 MapReduce 100 Jeffrey Dean 0
2855 Windows 1000 Bill Gates 100500
5961 Snapchat 3 Evan Spiegel 2000

the resulting table should be

 

project_name team_lead income
MapReduce Jeffrey Dean 0
Windows Bill Gates 100500
Snapchat Evan Spiegel 2000
  • [execution time limit] 10 seconds (mysql)

더보기

Solution

CREATE PROCEDURE projectList()
BEGIN
	/* Write your SQL here. Terminate each statement with a semicolon. */
	SELECT project_name, team_lead, income FROM Projects;
END
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> countriesSelection  (0) 2021.02.11
<Codesignal> Alphanumeric Less  (0) 2020.12.30
<Codesignal> Construct Square  (0) 2020.12.30
<Codesignal> Crossword Formation  (0) 2020.12.30
<Codesignal> Decipher  (0) 2020.06.27

Medium

An alphanumeric ordering of strings is defined as follows: each string is considered as a sequence of tokens, where each token is a letter or a number (as opposed to an isolated digit, as is the case in lexicographic ordering). For example, the tokens of the string "ab01c004" are [a, b, 01, c, 004]. In order to compare two strings, we'll first break them down into tokens and then compare the corresponding pairs of tokens with each other (i.e. compare the first token of the first string with the first token of the second string, etc).

Here is how tokens are compared:

  • If a letter is compared with another letter, the usual alphabetical order applies.
  • A number is always considered less than a letter.
  • When two numbers are compared, their values are compared. Leading zeros, if any, are ignored.

If at some point one string has no more tokens left while the other one still does, the one with fewer tokens is considered smaller.

If the two strings s1 and s2 appear to be equal, consider the smallest index i such that tokens(s1)[i] and tokens(s2)[i] (where tokens(s)[i] is the ith token of string s) differ only by the number of leading zeros. If no such i exists, the strings are indeed equal. Otherwise, the string whose ith token has more leading zeros is considered smaller.

Here are some examples of comparing strings using alphanumeric ordering.

"a" < "a1" < "ab"

"ab42" < "ab000144" < "ab00144" < "ab144" < "ab000144x"

"x11y012" < "x011y13"

Your task is to return true if s1 is strictly less than s2, and false otherwise.

Example

  • For s1 = "a" and s2 = "a1", the output should be alphanumericLess(s1, s2) = true;

    These strings have equal first tokens, but since s1 has fewer tokens than s2, it's considered smaller.

  • For s1 = "ab" and s2 = "a1", the output should be alphanumericLess(s1, s2) = false;

    These strings also have equal first tokens, but since numbers are considered less than letters, s1 is larger.

  • For s1 = "b" and s2 = "a1", the output should be alphanumericLess(s1, s2) = false.

    Since b is greater than a, s1 is larger.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string s1

    A string consisting of English letters and digits.

    Guaranteed constraints:
    1 ≤ s1.length ≤ 20.

  • [input] string s2

    A string consisting of English letters and digits.

    Guaranteed constraints:
    1 ≤ s2.length ≤ 20.

  • [output] boolean

    true if s1 is alphanumerically strictly less than s2, 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 alphanumericLess(char *s1,char *s2)
{
	int *l1=(int *)calloc(sizeof(int),strlen(s1)), *l2=(int *)calloc(sizeof(int),strlen(s2)), *ls1=l1,*ls2=l2, i, j;

	while(*s1!=0 && *s2!=0)
	{
		if(isalpha(*s1)!=isalpha(*s2))
			return !isalpha(*s1);
		if(isalpha(*s1)&&isalpha(*s2))
		{
			if(*s1!=*s2)
				return *s1<*s2;
			else
			{
				s1++;
				s2++;
				continue;
			}
		}

		i=j=0;
		while(*s1!=0 && !isalpha(*s1))
		{
			i*=10;
			i+=*s1++-'0';
			if(i==0)
				(*l1)++;
		}

		while(*s2!=0&&!isalpha(*s2))
		{
			j*=10;
			j+=*s2++-'0';
			if(j==0)
				(*l2)++;
		}

		if(i!=j)
			return i<j;

		l1++;
		l2++;
	}

	if(*s1==0 && *s2==0)
	{
		while(ls1<=l1)
			if(*ls1!=*ls2)
				return *ls1>*ls2;
			else
			{
				ls1++;
				ls2++;
			}

		return false;
	}

	return *s1==0;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> countriesSelection  (0) 2021.02.11
<Codesignal> projectList  (0) 2020.12.30
<Codesignal> Construct Square  (0) 2020.12.30
<Codesignal> Crossword Formation  (0) 2020.12.30
<Codesignal> Decipher  (0) 2020.06.27

Medium

Given a string consisting of lowercase English letters, find the largest square number which can be obtained by reordering the string's characters and replacing them with any digits you need (leading zeros are not allowed) where same characters always map to the same digits and different characters always map to different digits.

If there is no solution, return -1.

Example

  • For s = "ab", the output should be
    constructSquare(s) = 81.
    The largest 2-digit square number with different digits is 81.
  • For s = "zzz", the output should be
    constructSquare(s) = -1.
    There are no 3-digit square numbers with identical digits.
  • For s = "aba", the output should be
    constructSquare(s) = 900.
    It can be obtained after reordering the initial string into "baa" and replacing "a" with 0 and "b" with 9.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string s

    Guaranteed constraints:
    1 ≤ s.length < 10.

  • [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 constructSquare(char *s)
{
	int l, N, end=sqrt(pow(10,strlen(s))-1), start=sqrt(pow(10,strlen(s)-1));
	bool solved;

	for(int i=end;i>=start;i--)
	{
		int A[128]={0, };
		int B[10]={0, };

		N=i*i;

		while(N!=0)
		{
			B[N%10]++;
			N/=10;
		}
		for(int j=0;j<strlen(s);j++)
			A[s[j]]++;

		solved=true;

		for(int k=0;k<128;k++)
		{
			if(A[k]==0)
				continue;

			for(l=0;l<10;l++)
				if(A[k]==B[l])
				{
					B[l]=0;
					break;
				}

			if(l==10)
			{
				solved=false;
				break;
			}
		}

		if(solved)
			return i*i;
	}

	return -1;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> projectList  (0) 2020.12.30
<Codesignal> Alphanumeric Less  (0) 2020.12.30
<Codesignal> Crossword Formation  (0) 2020.12.30
<Codesignal> Decipher  (0) 2020.06.27
<Codesignal> Higher Version  (0) 2020.06.27

Medium

You're a crossword fanatic, and have finally decided to try and create your own. However, you also love symmetry and good design, so you come up with a set of rules they should follow:

  • the crossword must contain exactly four words;
  • these four words should form four pairwise intersections;
  • all words must be written either left-to-right or top-to-bottom;
  • the area of the rectangle formed by empty cells inside the intersections isn't equal to zero.

Given 4 words, find the number of ways to make a crossword following the above-described rules. Note that two crosswords which differ by rotation are considered different.

Example

For words = ["crossword", "square", "formation", "something"], the output should be
crosswordFormation(words) = 6.

The six crosswords can be formed as shown below:

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.string words

    An array of distinct strings, the words you need to use in your crossword.

    Guaranteed constraints:
    words.length = 4,
    3 ≤ words[i].length < 15.

  • [output] integer

    The number of ways to make a correct crossword of the desired formation.

[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 crosswordFormation(arr_string words)
{
	int row[4], col[4], count=0;

	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
			if(j!=i)
				for(row[0]=0;row[0]<strlen(words.arr[i]);row[0]++)
					for(col[0]=0;col[0]<strlen(words.arr[j]);col[0]++)
						if(words.arr[i][row[0]]==words.arr[j][col[0]])
							for(int k=0;k<4;k++)
								if(k!=i&&k!=j)
									for(row[1]=0;row[1]<strlen(words.arr[k]);row[1]++)
										for(col[1]=col[0]+2;col[1]<strlen(words.arr[j]);col[1]++)
											if(words.arr[k][row[1]]==words.arr[j][col[1]])
												for(int l=0;l<4;l++)
													if(l!=i&&l!=j&&l!=k)
														for(col[2]=0;col[2]<strlen(words.arr[l]);col[2]++)
															for(row[2]=row[0]+2;row[2]<strlen(words.arr[i]);row[2]++)
																if(words.arr[i][row[2]]==words.arr[l][col[2]])
																{
																	col[3]=col[2]+col[1]-col[0];
																	row[3]=row[2]+row[1]-row[0];
																	if(col[3]<strlen(words.arr[l])&&row[3]<strlen(words.arr[k]))
																		if(words.arr[k][row[3]]==words.arr[l][col[3]])
																			count++;
																}

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Alphanumeric Less  (0) 2020.12.30
<Codesignal> Construct Square  (0) 2020.12.30
<Codesignal> Decipher  (0) 2020.06.27
<Codesignal> Higher Version  (0) 2020.06.27
<Codesignal> Is Unstable Pair?  (0) 2020.06.26

Easy

Consider the following ciphering algorithm:

  • For each character replace it with its code.
  • Concatenate all of the obtained numbers.

Given a ciphered string, return the initial one if it is known that it consists only of lowercase letters.

Note: here the character's code means its decimal ASCII code, the numerical representation of a character used by most modern programming languages.

Example

For cipher = "10197115121", the output should be
decipher(cipher) = "easy".

Explanation: charCode('e') = 101, charCode('a') = 97, charCode('s') = 115 and charCode('y') = 121.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string cipher

    A non-empty string which is guaranteed to be a cipher for some other string of lowercase letters.

    Guaranteed constraints:
    2 ≤ cipher.length ≤ 100.

  • [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 *decipher(char *cipher)
{
	char *string=calloc(51,sizeof(char));
	int temp=0, count=0;

	for(int i=0;i<strlen(cipher);i++)
	{
		temp*=10;
		temp+=cipher[i]-'0';

		if(temp>='a' && temp<='z')
		{
			string[count++]=temp;
			temp=0;
		}
	}

	return string;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Construct Square  (0) 2020.12.30
<Codesignal> Crossword Formation  (0) 2020.12.30
<Codesignal> Higher Version  (0) 2020.06.27
<Codesignal> Is Unstable Pair?  (0) 2020.06.26
<Codesignal> Equal Pair of Bits  (0) 2020.06.17

Medium

Given two version strings composed of several non-negative decimal fields separated by periods ("."), both strings contain equal number of numeric fields. Return true if the first version is higher than the second version and false otherwise.

The syntax follows the regular semver ordering rules:

1.0.5 < 1.1.0 < 1.1.5 < 1.1.10 < 1.2.0 < 1.2.2 < 1.2.10 < 1.10.2 < 2.0.0 < 10.0.0

There are no leading zeros in any of the numeric fields, i.e. you do not have to handle inputs like 100.020.003 (it would instead be given as 100.20.3).

Example

  • For ver1 = "1.2.2" and ver2 = "1.2.0", the output should be
    higherVersion(ver1, ver2) = true;
  • For ver1 = "1.0.5" and ver2 = "1.1.0", the output should be
    higherVersion(ver1, ver2) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string ver1

    Guaranteed constraints:
    1 ≤ ver1.length ≤ 15.

  • [input] string ver2

    Guaranteed constraints:
    1 ≤ ver2.length ≤ 15.

  • [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 higherVersion(char *ver1,char *ver2)
{
	int count_1=0, count_2=0;

	while(count_1<strlen(ver1) && count_2<strlen(ver2))
	{
		int v1=0, v2=0;

		while(isdigit(ver1[count_1]))
		{
			v1*=10;
			v1+=ver1[count_1++]-'0';
		}
		while(isdigit(ver2[count_2]))
		{
			v2*=10;
			v2+=ver2[count_2++]-'0';
		}
		count_1++;
		count_2++;
		if(v1>v2)
			return true;
		else if(v2>v1)
			return false;
		else if(count_1>=strlen(ver1) || count_2>=strlen(ver2))
			return false;
	}
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Crossword Formation  (0) 2020.12.30
<Codesignal> Decipher  (0) 2020.06.27
<Codesignal> Is Unstable Pair?  (0) 2020.06.26
<Codesignal> Equal Pair of Bits  (0) 2020.06.17
<Codesignal> Different Rightmost Bit  (0) 2020.06.16

Medium

Some file managers sort filenames taking into account case of the letters, others compare strings as if all of the letters are of the same case. That may lead to different ways of filename ordering.

Call two filenames an unstable pair if their ordering depends on the case.

To compare two filenames a and b, find the first position i at which a[i] ≠ b[i]. If a[i] < b[i], then a < b. Otherwise a > b. If such position doesn't exist, the shorter string goes first.

Given two filenames, check whether they form an unstable pair.

Example

  • For filename1 = "aa" and filename2 = "AAB", the output should be
    isUnstablePair(filename1, filename2) = true.

    Because "AAB" < "aa", but "AAB" > "AA".

  • For filename1 = "A" and filename2 = "z", the output should be
    isUnstablePair(filename1, filename2) = false.

    Both "A" < "z" and "a" < "z".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string filename1

    A non-empty string of English letters and digits.

    Guaranteed constraints:
    1 ≤ filename1.length ≤ 10.

  • [input] string filename2

    A non-empty string of English letters and digits. It's guaranteed that there is no ambiguity, i.e. even being considered in the same case strings are never equal.

    Guaranteed constraints:
    1 ≤ filename2.length ≤ 10.

  • [output] boolean

    • true if filename1 and filename2 form an unstable pair, 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 isUnstablePair(char *filename1,char *filename2)
{
	int check=strcmp(filename1,filename2);

	for(int i=0;i<strlen(filename1);i++)
		filename1[i]=tolower(filename1[i]);
	for(int i=0;i<strlen(filename2);i++)
		filename2[i]=tolower(filename2[i]);

	return check*strcmp(filename1,filename2)<0;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Decipher  (0) 2020.06.27
<Codesignal> Higher Version  (0) 2020.06.27
<Codesignal> Equal Pair of Bits  (0) 2020.06.17
<Codesignal> Different Rightmost Bit  (0) 2020.06.16
<Codesignal> Swap Adjacent Bits  (0) 2020.06.16

+ Recent posts