Medium

Consider two following representations of a non-negative integer:

  1. A simple decimal integer, constructed of a non-empty sequence of digits from 0 to 9;
  2. An integer with at least one digit in a base from 2 to 16 (inclusive), enclosed between # characters, and preceded by the base, which can only be a number between 2 and 16 in the first representation. For digits from 10 to 15 characters a, b, ..., f and A, B, ..., F are used.

Additionally, both representations may contain underscore (_) characters; they are used only as separators for improving legibility of numbers and can be ignored while processing a number.

Your task is to determine whether the given string is a valid integer representation.

Note: this is how integer numbers are represented in the programming language Ada.

Example

  • For line = "123_456_789", the output should be
    adaNumber(line) = true;
  • For line = "16#123abc#", the output should be
    adaNumber(line) = true;
  • For line = "10#123abc#", the output should be
    adaNumber(line) = false;
  • For line = "10#10#123ABC#", the output should be
    adaNumber(line) = false;
  • For line = "10#0#", the output should be
    adaNumber(line) = true;
  • For line = "10##", the output should be
    adaNumber(line) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string line

    A non-empty string.

    Guaranteed constraints:
    2 ≤ line.length ≤ 30.

  • [output] boolean

    • true if line is a valid integer representation, 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 adaNumber(char *line)
{
	int i, j, N=strlen(line), base=0, count=0;
	bool case1=true;

	for(i=N-1;i>=0;i--)
		if(line[i]=='_')
		{
			for(j=i;j<N;j++)
				line[j]=line[j+1];
			line[N--]='\0';
		}

	if(N==0)
		return false;

	for(i=0;i<N;i++)
		if(!(isdigit(line[i])))
		{
			case1=false;
			break;
		}

	if(case1)
		return true;

	if(line[0]=='#')
		return false;
	else if(line[N-1]!='#')
		return false;

	for(i=0;i<N;i++)
		if(line[i]>='A' && line[i]<='F')
			line[i]+=32;
		else if(line[i]=='#')
			count++;

	if(count!=2)
		return false;

	i=0;
	for(i=0;line[i]!='#';i++)
	{
		if(!isdigit(line[i]))
			return false;
		base*=10;
		base+=line[i]-'0';
	}
	if(base<2 || base>16)
		return false;

	if(N-i<=2)
		return false;

	while(!isalnum(line[++i]))
		if(!(isalnum(line[i]) || line[i]!='#'))
			return false;

	for(;i<N-1;i++)
	{
		if(line[i]>='a' && line[i]<='f')
			line[i]-='a'-10;
		else if(isdigit(line[i]))
			line[i]-='0';
		else
			return false;

		if(line[i]>=base)
			return false;
	}

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Character Parity  (0) 2020.05.24
<Codesignal> Three Split  (0) 2020.05.24
<Codesignal> Integer to String of Fixed width  (0) 2020.05.08
<Codesignal> Timed Reading  (0) 2020.05.04
<Codesignal> Switch Lights  (0) 2020.05.04

+ Recent posts