NazmusAshrafi/apps_probs huggingface dataset:
def decrypt(s: str) -> str:
    """
    # Task
    Smartphones software security has become a growing concern related to mobile telephony. It is particularly important as it relates to the security of available personal information.
    
    For this reason, Ahmed decided to encrypt phone numbers of contacts in such a way that nobody can decrypt them. At first he tried encryption algorithms very complex, but the decryption process is tedious, especially when he needed to dial a speed dial.
    
    He eventually found the algorithm following: instead of writing the number itself, Ahmed multiplied by 10, then adds the result to the original number.
    
    For example, if the phone number is `123`, after the transformation, it becomes `1353`. Ahmed truncates the result (from the left), so it has as many digits as the original phone number. In this example Ahmed wrote `353` instead of `123` in his smart phone.
    
    Ahmed needs a program to recover the original phone number from number stored on his phone. The program return "impossible" if the initial number can not be calculated.
    
    Note: There is no left leading zero in either the input or the output; Input `s` is given by string format, because it may be very huge ;-)
    
    # Example
    
    For `s="353"`, the result should be `"123"`
    
    ```
    1230
    +  123
    .......
    = 1353
    
    truncates the result to 3 digit -->"353"
    
    So the initial number is "123"
    ```
    For `s="123456"`, the result should be `"738496"`
    
    ```
    7384960
    +  738496
    .........
    = 8123456
    
    truncates the result to 6 digit -->"123456"
    
    So the initial number is "738496"
    ```
    For `s="4334"`, the result should be `"impossible"`
    
    Because no such a number can be encrypted to `"4334"`.
    
    # Input/Output
    
    
    - `[input]` string `s`
    
    string presentation of n with `1 <= n <= 10^100`
    
    
    - `[output]` a string
    
    The original phone number before encryption, or `"impossible"` if the initial number can not be calculated.
    """


# Tests

def check(candidate):
    assert candidate('353') == '123'
    assert candidate('444') == '404'
    assert candidate('123456') == '738496'
    assert candidate('147') == '377'
    assert candidate('4334') == 'impossible'
check(decrypt)
def reformat(s: str) -> str:
    """
    Given alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits).
    You have to find a permutation of the string where no letter is followed by another letter and no digit is followed by another digit. That is, no two adjacent characters have the same type.
    Return the reformatted string or return an empty string if it is impossible to reformat the string.
    
    Example 1:
    Input: s = "a0b1c2"
    Output: "0a1b2c"
    Explanation: No two adjacent characters have the same type in "0a1b2c". "a0b1c2", "0a1b2c", "0c2a1b" are also valid permutations.
    
    Example 2:
    Input: s = "leetcode"
    Output: ""
    Explanation: "leetcode" has only characters so we cannot separate them by digits.
    
    Example 3:
    Input: s = "1229857369"
    Output: ""
    Explanation: "1229857369" has only digits so we cannot separate them by characters.
    
    Example 4:
    Input: s = "covid2019"
    Output: "c2o0v1i9d"
    
    Example 5:
    Input: s = "ab123"
    Output: "1a2b3"
    
    
    Constraints:
    
    1 <= s.length <= 500
    s consists of only lowercase English letters and/or digits.
    """


# Tests

def check(candidate):
    assert candidate('a0b1c2') == '0a1b2c' or candidate('a0b1c2') == 'a0b1c2' or candidate('a0b1c2') == '0c2a1b'
    assert candidate('leetcode') == ''
    assert candidate('1229857369') == ''
    assert candidate('covid2019') == 'c2o0v1i9d' or candidate('covid2019') == '2c0o1v9i'
    assert candidate('ab123') == '1a2b3' or candidate('ab123') == 'a1b2c'
check(reformat)
def capitals_first(string: str) -> str:
    """
    Create a function that takes an input String and returns a String, where all the uppercase words of the input String are in front and all the lowercase words at the end.
    The order of the uppercase and lowercase words should be the order in which they occur.
    
    If a word starts with a number or special character, skip the word and leave it out of the result.
    
    Input String will not be empty.
    
    For an input String: "hey You, Sort me Already!"
    the function should return: "You, Sort Already! hey me"
    """


# Tests

def check(candidate):
    assert candidate('hey You, Sort me Already') == 'You, Sort Already hey me'
    assert candidate('sense Does to That Make you?') == 'Does That Make sense to you?'
    assert candidate('i First need Thing In coffee The Morning') == 'First Thing In The Morning i need coffee'
    assert candidate('123 baby You and Me') == 'You Me baby and'
    assert candidate('Life gets Sometimes pretty !Hard') == 'Life Sometimes gets pretty'
check(capitals_first)
def day_of_the_week(day: int, month: int, year: int) -> str:
    """
    Given a date, return the corresponding day of the week for that date.
    The input is given as three integers representing the day, month and year respectively.
    Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.
    
    Example 1:
    Input: day = 31, month = 8, year = 2019
    Output: "Saturday"
    
    Example 2:
    Input: day = 18, month = 7, year = 1999
    Output: "Sunday"
    
    Example 3:
    Input: day = 15, month = 8, year = 1993
    Output: "Sunday"
    
    
    Constraints:
    
    The given dates are valid dates between the years 1971 and 2100.
    """


# Tests

def check(candidate):
    assert candidate(31, 8, 2019) == 'Saturday'
    assert candidate(18, 7, 1999) == 'Sunday'
    assert candidate(15, 8, 1993) == 'Sunday'
check(day_of_the_week)
def max_length_three_blocks_palindrome(t: int, cases: List[Tuple[int, List[int]]]) -> List[int]:
    """
    The only difference between easy and hard versions is constraints.
    
    You are given a sequence $a$ consisting of $n$ positive integers.
    
    Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
    
    Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
    
    You have to answer $t$ independent test cases.
    
    Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
    
    
    -----Input-----
    
    The first line of the input contains one integer $t$ ($1 \le t \le 2000$) — the number of test cases. Then $t$ test cases follow.
    
    The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) — the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
    
    It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
    
    
    -----Output-----
    
    For each test case, print the answer — the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
    
    
    -----Example-----
    Input
    6
    8
    1 1 2 2 3 2 1 1
    3
    1 3 3
    4
    1 10 10 1
    1
    26
    2
    2 1
    3
    1 1 1
    
    Output
    7
    2
    4
    1
    1
    3
    """


# Tests

def check(candidate):
    assert candidate(6, [(8, [1, 1, 2, 2, 3, 2, 1, 1]), (3, [1, 3, 3]), (4, [1, 10, 10, 1]), (1, [26]), (2, [2, 1]), (3, [1, 1, 1])]) == [7, 2, 4, 1, 1, 3]
check(max_length_three_blocks_palindrome)
def moment_of_time_in_space(moment: str) -> List[bool]:
    """
    # Task
    You are given a `moment` in time and space. What you must do is break it down into time and space, to determine if that moment is from the past, present or future.
    
    `Time` is the sum of characters that increase time (i.e. numbers in range ['1'..'9'].
    
    `Space` in the number of characters which do not increase time (i.e. all characters but those that increase time).
    
    The moment of time is determined as follows:
    ```
    If time is greater than space, than the moment is from the future.
    If time is less than space, then the moment is from the past.
    Otherwise, it is the present moment.```
    
    You should return an array of three elements, two of which are false, and one is true. The true value should be at the `1st, 2nd or 3rd` place for `past, present and future` respectively.
    
    # Examples
    
    For `moment = "01:00 pm"`, the output should be `[true, false, false]`.
    
    time equals 1, and space equals 7, so the moment is from the past.
    
    For `moment = "12:02 pm"`, the output should be `[false, true, false]`.
    
    time equals 5, and space equals 5, which means that it's a present moment.
    
    For `moment = "12:30 pm"`, the output should be `[false, false, true]`.
    
    time equals 6, space equals 5, so the moment is from the future.
    
    # Input/Output
    
    
    - `[input]` string `moment`
    
    The moment of time and space that the input time came from.
    
    
    - `[output]` a boolean array
    
    Array of three elements, two of which are false, and one is true. The true value should be at the 1st, 2nd or 3rd place for past, present and future respectively.
    """


# Tests

def check(candidate):
    assert candidate('12:30 am') == [False, False, True]
    assert candidate('12:02 pm') == [False, True, False]
    assert candidate('01:00 pm') == [True, False, False]
    assert candidate('11:12 am') == [False, False, True]
    assert candidate('05:20 pm') == [False, False, True]
    assert candidate('04:20 am') == [False, True, False]
check(moment_of_time_in_space)
def max_distance_between_ones(n: int) -> int:
    """
    Given a positive integer n, find and return the longest distance between any two adjacent 1's in the binary representation of n. If there are no two adjacent 1's, return 0.
    Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. For example, the two 1's in "1001" have a distance of 3.
    
    Example 1:
    Input: n = 22
    Output: 2
    Explanation: 22 in binary is "10110".
    The first adjacent pair of 1's is "10110" with a distance of 2.
    The second adjacent pair of 1's is "10110" with a distance of 1.
    The answer is the largest of these two distances, which is 2.
    Note that "10110" is not a valid pair since there is a 1 separating the two 1's underlined.
    
    Example 2:
    Input: n = 5
    Output: 2
    Explanation: 5 in binary is "101".
    
    Example 3:
    Input: n = 6
    Output: 1
    Explanation: 6 in binary is "110".
    
    Example 4:
    Input: n = 8
    Output: 0
    Explanation: 8 in binary is "1000".
    There aren't any adjacent pairs of 1's in the binary representation of 8, so we return 0.
    
    Example 5:
    Input: n = 1
    Output: 0
    
    
    Constraints:
    
    1 <= n <= 109
    """


# Tests

def check(candidate):
    assert candidate(22) == 2
    assert candidate(5) == 2
    assert candidate(8) == 0
check(max_distance_between_ones)
def bin2gray(bits: list) -> list:
    """
    Gray code is a form of binary encoding where transitions between consecutive numbers differ by only one bit. This is a useful encoding for reducing hardware data hazards with values that change rapidly and/or connect to slower hardware as inputs. It is also useful for generating inputs for Karnaugh maps.
    
    Here is an exemple of what the code look like:
    
    ```
    0:    0000
    1:    0001
    2:    0011
    3:    0010
    4:    0110
    5:    0111
    6:    0101
    7:    0100
    8:    1100
    ```
    
    The goal of this kata is to build two function bin2gray and gray2bin wich will convert natural binary to Gray Code and vice-versa. We will use the "binary reflected Gray code". The input and output will be arrays of 0 and 1, MSB at index 0.
    
    There are "simple" formula to implement these functions. It is a very interesting exercise to find them by yourself.
    
    All input will be correct binary arrays.
    """


# Tests

def check(candidate):
    assert candidate([1, 0, 1]) == [1, 1, 1]
    assert candidate([1, 1]) == [1, 0]
    assert candidate([1]) == [1]
    assert candidate([0]) == [0]
    assert candidate([1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0]) == [1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1]
    assert candidate([1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0]) == [1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1]
check(bin2gray)
def pig_latin(s: str) -> str:
    """
    Pig Latin is an English language game where the goal is to hide the meaning of a word from people not aware of the rules.
    
    So, the goal of this kata is to wite a function that encodes a single word string to pig latin.
    
    The rules themselves are rather easy:
    
    1) The word starts with a vowel(a,e,i,o,u) -> return the original string plus "way".
    
    2) The word starts with a consonant -> move consonants from the beginning of the word to the end of the word until the first vowel, then return it plus "ay".
    
    3) The result must be lowercase, regardless of the case of the input. If the input string has any non-alpha characters, the function must return None, null, Nothing (depending on the language).
    
    4) The function must also handle simple random strings and not just English words.
    
    5) The input string has no vowels -> return the original string plus "ay".
    
    For example, the word "spaghetti" becomes "aghettispay" because the first two letters ("sp") are consonants, so they are moved to the end of the string and "ay" is appended.
    """


# Tests

def check(candidate):
    assert candidate('Hello') == 'ellohay'
    assert candidate('CCCC') == 'ccccay'
    assert candidate('tes3t5') == None
    assert candidate('ay') == 'ayway'
    assert candidate('') == None
    assert candidate('YA') == 'ayay'
    assert candidate('123') == None
    assert candidate('ya1') == None
    assert candidate('yaYAya') == 'ayayayay'
    assert candidate('YayayA') == 'ayayayay'
check(pig_latin)
def modifyString(s: str) -> str:
    """
    Given a string s containing only lower case English letters and the '?' character, convert all the '?' characters into lower case letters such that the final string does not contain any consecutive repeating characters. You cannot modify the non '?' characters.
    It is guaranteed that there are no consecutive repeating characters in the given string except for '?'.
    Return the final string after all the conversions (possibly zero) have been made. If there is more than one solution, return any of them. It can be shown that an answer is always possible with the given constraints.
    
    Example 1:
    Input: s = "?zs"
    Output: "azs"
    Explanation: There are 25 solutions for this problem. From "azs" to "yzs", all are valid. Only "z" is an invalid modification as the string will consist of consecutive repeating characters in "zzs".
    Example 2:
    Input: s = "ubv?w"
    Output: "ubvaw"
    Explanation: There are 24 solutions for this problem. Only "v" and "w" are invalid modifications as the strings will consist of consecutive repeating characters in "ubvvw" and "ubvww".
    
    Example 3:
    Input: s = "j?qg??b"
    Output: "jaqgacb"
    
    Example 4:
    Input: s = "??yw?ipkj?"
    Output: "acywaipkja"
    
    
    Constraints:
    
    1 <= s.length <= 100
    s contains only lower case English letters and '?'.
    """


# Tests

def check(candidate):
    assert candidate('?zs') == 'azs'
    assert candidate('ubv?w') == 'ubvaw'
    assert candidate('j?qg??b') == 'jaqgacb'
    assert candidate('??yw?ipkj?') == 'acywaipkja'
check(modifyString)
def blocks(s: str) -> str:
    """
    ## Task
    
    You will receive a string consisting of lowercase letters, uppercase letters and digits as input. Your task is to return this string as blocks separated by dashes (`"-"`). The elements of a block should be sorted with respect to the hierarchy listed below, and each block cannot contain multiple instances of the same character. Elements should be put into the first suitable block.
    
    The hierarchy is:
    1. lowercase letters (`a - z`), in alphabetical order
    2. uppercase letters (`A - Z`), in alphabetical order
    3. digits (`0 - 9`), in ascending order
    
    ## Examples
    
    * `"21AxBz" -> "xzAB12"` - since input does not contain repeating characters, you only need 1 block
    * `"abacad" -> "abcd-a-a"` - character "a" repeats 3 times, thus 3 blocks are needed
    * `"" -> ""` - an empty input should result in an empty output
    * `"hbh420sUUW222IWOxndjn93cdop69NICEep832" -> "bcdehjnopsxCEINOUW0234689-dhnpIUW239-2-2-2"` - a more sophisticated example
    
    Good luck!
    """


# Tests

def check(candidate):
    assert candidate('heyitssampletestkk') == 'aeiklmpsty-ehkst-s'
    assert candidate('dasf6ds65f45df65gdf651vdf5s1d6g5f65vqweAQWIDKsdds') == 'adefgqsvwADIKQW1456-dfgsv156-dfs56-dfs56-dfs56-df56-d5-d'
    assert candidate('SDF45648374RHF8BFVYg378rg3784rf87g3278bdqG') == 'bdfgqrBDFGHRSVY2345678-grF3478-gF3478-3478-78-8'
    assert candidate('') == ''
    assert candidate('aaaaaaaaaa') == 'a-a-a-a-a-a-a-a-a-a'
check(blocks)
def AlanAnnoyingKid(sentence: str) -> str:
    """
    Alan's child can be annoying at times.
    
    When Alan comes home and tells his kid what he has accomplished today, his kid never believes him.
    
    Be that kid.
    
    Your function 'AlanAnnoyingKid' takes as input a sentence spoken by Alan (a string). The sentence contains the following structure:
    
    "Today I " + [action_verb] + [object] + "."
    
    (e.g.: "Today I played football.")
    
    
    Your function will return Alan's kid response, which is another sentence with the following structure:
    
    "I don't think you " + [action_performed_by_alan] + " today, I think you " + ["did" OR "didn't"] + [verb_of _action_in_present_tense] + [" it!" OR " at all!"]
    
    (e.g.:"I don't think you played football today, I think you didn't play at all!")
    
    
    Note the different structure depending on the presence of a negation in Alan's first sentence (e.g., whether Alan says "I dind't play football", or "I played football").
    
    ! Also note: Alan's kid is young and only uses simple, regular verbs that use a simple "ed" to make past tense.
    There are random test cases.
    
    Some more examples:
    
    input  = "Today I played football."
    output = "I don't think you played football today, I think you didn't play at all!"
    
    input  = "Today I didn't attempt to hardcode this Kata."
    output = "I don't think you didn't attempt to hardcode this Kata today, I think you did attempt it!"
    
    input  = "Today I didn't play football."
    output = "I don't think you didn't play football today, I think you did play it!"
    
    input  = "Today I cleaned the kitchen."
    output = "I don't think you cleaned the kitchen today, I think you didn't clean at all!"
    """


# Tests

def check(candidate):
    assert candidate('Today I played football.') == "I don't think you played football today, I think you didn't play at all!"
    assert candidate("Today I didn't play football.") == "I don't think you didn't play football today, I think you did play it!"
    assert candidate("Today I didn't attempt to hardcode this Kata.") == "I don't think you didn't attempt to hardcode this Kata today, I think you did attempt it!"
    assert candidate('Today I cleaned the kitchen.') == "I don't think you cleaned the kitchen today, I think you didn't clean at all!"
    assert candidate('Today I learned to code like a pro.') == "I don't think you learned to code like a pro today, I think you didn't learn at all!"
check(AlanAnnoyingKid)
def T9(words: list, seq: str) -> list:
    """
    The T9 typing predictor helps with suggestions for possible word combinations on an old-style numeric keypad phone. Each digit in the keypad (2-9) represents a group of 3-4 letters. To type a letter, press once the key which corresponds to the letter group that contains the required letter. Typing words is done by typing letters of the word in sequence.
    
    The letter groups and corresponding digits are as follows:
    ```
    -----------------
    |  1  |  2  |  3  |
    |     | ABC | DEF |
    |-----|-----|-----|
    |  4  |  5  |  6  |
    | GHI | JKL | MNO |
    |-----|-----|-----|
    |  7  |  8  |  9  |
    | PQRS| TUV | WXYZ|
    -----------------
    ```
    
    The prediction algorithm tries to match the input sequence against a predefined dictionary of words. The combinations which appear in the dictionary are considered valid words and are shown as suggestions.
    
    Given a list of words as a reference dictionary, and a non-empty string (of digits 2-9) as input, complete the function which returns suggestions based on the string of digits, which are found in the reference dictionary.
    
    For example:
    ```python
    T9(['hello', 'world'], '43556') returns ['hello']
    T9(['good', 'home', 'new'], '4663') returns ['good', 'home']
    ```
    
    Note that the dictionary must be case-insensitive (`'hello'` and `'Hello'` are same entries). The list returned must contain the word as it appears in the dictionary (along with the case).
    
    Example:
    ```python
    T9(['Hello', 'world'], '43556') returns ['Hello']
    ```
    
    If there is no prediction available from the given dictionary, then return the string containing first letters of the letter groups, which correspond to the input digits.
    
    For example:
    ```python
    T9([], '43556') returns ['gdjjm']
    T9(['gold', 'word'], '4663') returns ['gmmd']
    ```
    """


# Tests

def check(candidate):
    assert candidate(['hello', 'world'], '43556') == ['hello']
    assert candidate(['good', 'home', 'new'], '4663') == ['good', 'home']
    assert candidate(['gone', 'hood', 'good', 'old'], '4663') == ['gone', 'hood', 'good']
    assert candidate(['Hello', 'world'], '43556') == ['Hello']
    assert candidate(['gOOD', 'hOmE', 'NeW'], '4663') == ['gOOD', 'hOmE']
    assert candidate(['goNe', 'hood', 'GOOD', 'old'], '4663') == ['goNe', 'hood', 'GOOD']
    assert candidate([], '43556') == ['gdjjm']
    assert candidate(['gold'], '4663') == ['gmmd']
    assert candidate(['gone', 'hood', 'good', 'old'], '729') == ['paw']
check(T9)