Is Unstable Pair?

Description:

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

  • [time limit] 3000ms (cs)
  • [input] string filename1

    A non-empty string of letters and digits.

    Constraints:
    1 ≤ filename1.length ≤ 10.

  • [input] string filename2

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

    Constraints:
    1 ≤ filename2.length ≤ 10.

  • [output] boolean

    true if filename1 and filename2 form an unstable pair, false otherwise.

Tests:
Solution:

bool isUnstablePair(string filename1, string filename2) {
    if(string.Compare(filename2,filename1, StringComparison.Ordinal)>0) {
        return string.Compare(filename2.ToUpper(),filename1.ToUpper(),StringComparison.Ordinal)<0;
    }
    else if(string.Compare(filename2,filename1, StringComparison.Ordinal)<0) {
        return string.Compare(filename2.ToLower(),filename1.ToLower(), StringComparison.Ordinal)>0;
    }
    else 
        return false;
}

Metro Card

Description:

You just bought a public transit card that allows you to ride the Metro for a certain number of days.

Here is how it works: upon first receiving the card, the system allocates you a31-day pass, which equals the number of days in January. The second time you pay for the card, your pass is extended by 28 days, i.e. the number of days in February (note that leap years are not considered), and so on. The 13th time you extend the pass, you get 31 days again.

You just ran out of days on the card, and unfortunately you’ve forgotten how many times your pass has been extended so far. However, you do remember the number of days you were able to ride the Metro during this most recent month. Figure out the number of days by which your pass will now be extended, and return all the options as an array sorted in increasing order.

Example

For lastNumberOfDays = 30, the output should be
metroCard(lastNumberOfDays) = [31].

There are 30 days in April, June, September and November, so the next months to consider are May, July, October or December. All of them have exactly 31days, which means that you will definitely get a 31-days pass the next time you extend your card.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer lastNumberOfDays

    A positive integer, the number of days for which the card was extended the last time. This number can be equal to 28, 30 or 31.

  • [output] array.integer

    An array of positive integers, the possible number of days for which you will extend your pass. The elements of the array can only be equal to 28, 30or 31 and must be sorted in increasing order.

Tests:
Solution:

int[] metroCard(int lastNumberOfDays) {
    if(lastNumberOfDays==28 || lastNumberOfDays==30 || lastNumberOfDays==31) {
        if(lastNumberOfDays==28)
            return new []{31};
        else if(lastNumberOfDays==30)
            return new []{31};
        else 
            return new []{28,30,31};
    }
    else {
        throw new ArgumentOutOfRangeException();
    }
}

Will You?

Description:

Once Mary heard a famous song, and a line from it stuck in her head. That line was “Will you still love me when I’m no longer young and beautiful?”. Mary believes that a person is loved if and only if he/she is both young and beautiful, but this is quite a depressing thought, so she wants to put her belief to the test.

Knowing whether a person is young, beautiful and loved, find out if they contradict Mary’s belief.

Example

  • For young = true, beautiful = true and loved = true, the output should be
    willYou(young, beautiful, loved) = false.

    Young and beautiful people are loved according to Mary’s belief.

  • For young = true, beautiful = false and loved = true, the output should be
    willYou(young, beautiful, loved) = true.

    Mary doesn’t believe that not beautiful people can be loved.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] boolean young
  • [input] boolean beautiful
  • [input] boolean loved
  • [output] boolean

    true if the person contradicts Mary’s belief, false otherwise.

Tests:
Solution:

bool willYou(bool young, bool beautiful, bool loved) {
    return !(young && beautiful) && loved||(young && beautiful) && !loved;
}

Tennis Set

Description:

In tennis, a set is finished when one of the players wins 6 games and the other one wins less than 5, or, if both players win at least 5 games, until one of the players win 7 games.

Determine if it is possible for a tennis set to be finished with the score score1 :score2.

Example

  • For score1 = 3 and score2 = 6, the output should be
    tennisSet(score1, score2) = true;
  • For score1 = 8 and score2 = 5, the output should be
    tennisSet(score1, score2) = false;
  • For score1 = 6 and score2 = 5, the output should be
    tennisSet(score1, score2) = false.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer score1

    Number of games won by the 1st player, non-negative integer.

    Constraints:
    0 ≤ score1 ≤ 10.

  • [input] integer score2

    Number of games won by the 2nd player, non-negative integer.

    Constraints:
    0 ≤ score2 ≤ 10.

  • [output] boolean

    true if score1 : score2 represents a possible score for an ended set,false otherwise.

Tests:
Solution:

bool tennisSet(int score1, int score2) {
    //Validate input
    if(0<=score1 && score1 <= 10 && 0 <= score2 && score2 <= 10) {
        if((score1==5||score1==6) && score2==7||
           (score2==5||score2==6) && score1==7 ||
          score1<5 && score2==6||score2<5 && score1==6)
            return true;
        else
            return false;
    }
    else {
        throw new ArgumentOutOfRangeException();
    }
}

Arithmetic Expression

Description:

Consider an arithmetic expression of the form A#B=C. Check whether it is possible to replace # with one of the four signs: +, -, * or / to obtain a correct expression.

Example

  • For A = 2, B = 3 and C = 5, the output should be
    arithmeticExpression(A, B, C) = true.

    We can replace # with a + to obtain 2 + 3 = 5, so the answer is true.

  • For A = 8, B = 2 and C = 4, the output should be
    arithmeticExpression(A, B, C) = true.

    We can replace # with a / to obtain 8 / 2 = 4, so the answer is true.

  • For A = 8, B = 3 and C = 2, the output should be
    arithmeticExpression(A, B, C) = false.

    • 8 + 3 = 11 ≠ 2;
    • 8 - 3 = 5 ≠ 2;
    • 8 * 3 = 24 ≠ 2;
    • 8 / 3 = 2.(6) ≠ 2.

    So the answer is false.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer A

    Constraints:
    1 ≤ A ≤ 10.

  • [input] integer B

    Constraints:
    1 ≤ B ≤ 10.

  • [input] integer C

    Constraints:
    0 ≤ C ≤ 10.

  • [output] boolean

    true if the desired replacement is possible, false otherwise.

Tests:
Solution:

bool arithmeticExpression(int A, int B, int C) {
    return A + B == C || A * B == C || 1.0*A / B == C || A - B == C;
}

Is Infinite Process?

Description:

Given integers a and b, determine whether the following pseudocode results in an infinite loop

while a is not equal to b do
  increase a by 1
  decrease b by 1

Assume that the program is executed on a virtual machine which can store arbitrary long numbers and execute forever.

Example

  • For a = 2 and b = 6, the output should be
    isInfiniteProcess(a, b) = false;
  • For a = 2 and b = 3, the output should be
    isInfiniteProcess(a, b) = true.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer a

    Constraints:
    0 ≤ a ≤ 20.

  • [input] integer b

    Constraints:
    0 ≤ b ≤ 20.

  • [output] boolean

    true if the pseudocode will never stop, false otherwise.

Tests:
Solution:

bool isInfiniteProcess(int a, int b) {
    if(0<=a && a<=20 && 0<=b && b<=20){
        if(b>=a && (b-a)%2==0) {
            return false;
        }
        else {
            return true;
        }
    }
    else {
        throw new ArgumentOutOfRangeException();
    }
}

Extra Number

Description:

You’re given three integers, a, b and c. It is guaranteed that two of these integers are equal to each other. What is the value of the third integer?

Example

For a = 2, b = 7 and c = 2, the output should be
extraNumber(a, b, c) = 7.

The two equal numbers are a and c. The third number (b) equals 7, which is the answer.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer a

    Constraints:
    1 ≤ a ≤ 109.

  • [input] integer b

    Constraints:
    1 ≤ b ≤ 109.

  • [input] integer c

    Constraints:
    1 ≤ c ≤ 109.

  • [output] integer

Tests:

Solution:

int extraNumber(int a, int b, int c) {
    if(a==b || b==c || c==a){
        if(a==b && b==c){
            throw new ArgumentOutOfRangeException();
        }else{
            if(a==b)
                return c;
            else if(a==c)
                return b;
            else
                return a;
        }
    }else 
        throw new ArgumentOutOfRangeException();
}