Character Parity

Description:

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

  • [time limit] 3000ms (cs)
  • [input] char symbol
  • [output] string

Tests:
Solution:

string characterParity(char symbol) {
    if(symbol>=48 && symbol<=57) {
        int number = int.Parse(""+symbol);
        if(number%2==0)
            return "even";
        else
            return "odd";
    }
    else
        return "not a digit";
}
Advertisements

Alphanumeric Less

Description:

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 of lexicographic ordering). For example, the tokens of a string "ab01c004" are [a, b, 01, c, 004]. In order to compare two strings, you break them down into tokens and compare corresponding pairs of tokens with each other (i.e. 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 order applies.
  • A number is always 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 isuch that tokens(s1)[i] and tokens(s2)[i] (where tokes(s)[i] is the ithtoken of string s) differ only by the number of leading zeros. If no such iexists, the strings are indeed equal. Otherwise, the string whose ith token has more leading zeros is considered less.

Here are some examples of comparing strings using alphanumeric ordering.

"a" < "a1" < "ab"
"ab42" < "ab000144" < "ab00144" < "ab144" < "ab000144x"
"x11y012" < "x011y13"

Example

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

Input/Output

  • [time limit] 3000ms (cs)
  • [input] string s1String, consisting of Latin letters and digits.

    Constraints:
    1 ≤ s1.length ≤ 10.

  • [input] string s2String, consisting of Latin letters and digits.

    Constraints:
    1 ≤ s2.length ≤ 10.

  • [output] booleantrue if s1 is alphanumerically strictly less than s2, false otherwise.

Tests:
Solution:

bool alphanumericLess(string s1, string s2) {
    string[] l1 = getTokens(s1);
    string[] l2 = getTokens(s2);
    for(int i=0;i<Math.Max(l1.Length,l2.Length);i++) {
        if(i==l1.Length)
            return true;
        if(i==l2.Length)
            return false;
        
        if(!isDigit(l1[i]) && !isDigit(l2[i])) {
            if(string.Compare(l1[i],l2[i],false)<0)
                return true;
            else if(string.Compare(l1[i],l2[i],false)>0)
                return false;
        }
        else if(isDigit(l1[i])||isDigit(l2[i])) {
            if(isDigit(l1[i])&&isDigit(l2[i])) {
                if(int.Parse(l1[i])==int.Parse(l2[i]))
                    return l1[i].Length>l2[i].Length;
                else
                    return int.Parse(l1[i])<int.Parse(l2[i]);
            }
            else if(isDigit(l1[i]))
                return true;
            else 
                return false;
            
        }
    }
    return false;
}
string[] getTokens(string s) {
    List<string> result = new List<string>();
    
    bool isDigit = false;
    
    char currentC;
    string strItem="";
    
    for(int index=0;index<s.Length;index++) {
        currentC = s[index];
        if(48<=currentC && currentC <=57) {
            if(isDigit)
                strItem+=currentC;
            else {
                strItem=""+currentC;                
            }
            isDigit= true;
        }
        else {
            result.Add(""+currentC);
            strItem="";
            isDigit = false;
        }
    }
    if(isDigit && !string.IsNullOrEmpty(strItem))
        result.Add(strItem);
    return result.ToArray();
}
bool isDigit(string s) {
    int test;
    return int.TryParse(s, out test);
}

Decipher

Description:

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') = 115and charCode('y') = 121.

Input/Output

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

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

    Constraints:
    2 ≤ cipher.length ≤ 100.

  • [output] string

Tests:
Solution:

string decipher(string cipher) {
    string result="";
    while(cipher.Length>0) {
        string s = cipher.Substring(0,2);
        int n = int.Parse(s);
        if(n>=97) {
            result+=(char)n;
            cipher = cipher.Substring(2);
        }else {
            s = cipher.Substring(0,3);
            n = int.Parse(s);
            result+=(char)n;
            cipher = cipher.Substring(3);
        }
    }
    return result;
}

Higher Version

Description:

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 falseotherwise.

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

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

    Constraints:
    1 ≤ ver1.length ≤ 14.

  • [input] string ver2

    Constraints:
    1 ≤ ver2.length ≤ 14.

  • [output] boolean

Tests:
Solution:

bool higherVersion(string ver1, string ver2) {
    string[] s1 = ver1.Split('.');
    string[] s2 = ver2.Split('.');
    for(int i=0;i<Math.Min(s1.Length,s2.Length);i++) {
        if(int.Parse(s1[i])>int.Parse(s2[i]))
            return true;
        else if(int.Parse(s1[i])<int.Parse(s2[i]))
            return false;
    }
    return false;
}

Stolen Lunch

Description:

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

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

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

    Constraints:
    0 ≤ note.length ≤ 500.

  • [output] string

    The deciphered note.

Tests:
Solution:

string stolenLunch(string note) {
    string result = "";
    for(int i=0;i<note.Length;i++) {
        if(note[i]>=48&&note[i]<=57) {
            char n = (char)(note[i] -48);
            result+=(char)(n+97);
        }
        else if(note[i]>=97&&note[i]<=106) {
            char n = (char)(note[i] -97);
            result+=(char)(n+48);
        }
        else {
            result+=note[i];
        }
    }
    return result;
}

Cipher 26

Description:

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

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

    An encrypted string containing only lowercase English letters.

    Constraints:
    1 ≤ message.length ≤ 200.

  • [output] string

    A decrypted message.

Tests:
Solution:

string cipher26(string message) {
    string result = string.Empty;
    int sub=0,prev=0;
    for(int i=0;i<message.Length;i++) {
        int t = message[i]-97;
        sub = t -prev;
        if(sub<0)
            sub=sub+26;
        result+=(char)(sub+97);
        prev = t;
    }
    return result;
}

New Numeral System

Description:

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

  • [time limit] 3000ms (cs)
  • [input] char number

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

    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.

Tests:
Solution:

string[] newNumeralSystem(char number) {
    List<string> result = new List<string>();
    if(48<=number&&number<=57) {
        int n = int.Parse(""+number);
        for(int i=0;i<=n/2;i++) {
            result.Add(i.ToString()+" + "+(n-i).ToString());
        }
        return result.ToArray();
    }else if(65<=number&&number<=91){
        int n = int.Parse(""+(number-65));
        for(int i=0;i<=n/2;i++) {
            result.Add((char)(65+i)+" + "+(char)(65+n-i));
        }
        return result.ToArray();
    }
    else
        throw new ArgumentOutOfRangeException();
}