NazmusAshrafi/apps_probs huggingface dataset:
def encrypt(text: str) -> str:
    """
    For encrypting strings this region of chars is given (in this order!):
    
    * all letters (ascending, first all UpperCase, then all LowerCase)
    * all digits (ascending)
    * the following chars: `.,:;-?! '()$%&"`
    
    These are 77 chars! (This region is zero-based.)
    
    Write two methods:
    ```python
    def encrypt(text)
    def decrypt(encrypted_text)
    ```
    
    Prechecks:
    1. If the input-string has chars, that are not in the region, throw an Exception(C#, Python) or Error(JavaScript).
    2. If the input-string is null or empty return exactly this value!
    
    For building the encrypted string:
    1. For every second char do a switch of the case.
    2. For every char take the index from the region. Take the difference from the region-index of the char before (from the input text! Not from the fresh encrypted char before!). (Char2 = Char1-Char2)
    Replace the original char by the char of the difference-value from the region. In this step the first letter of the text is unchanged.
    3. Replace the first char by the mirror in the given region. (`'A' -> '"'`, `'B' -> '&'`, ...)
    
    Simple example:
    
    * Input:  `"Business"`
    * Step 1: `"BUsInEsS"`
    * Step 2: `"B61kujla"`
    * `B -> U`
    * `B (1) - U (20) = -19`
    * `-19 + 77 = 58`
    * `Region[58] = "6"`
    * `U -> s`
    * `U (20) - s (44) = -24`
    * `-24 + 77 = 53`
    * `Region[53] = "1"`
    * Step 3: `"&61kujla"`
    
    This kata is part of the Simple Encryption Series:
    Simple Encryption #1 - Alternating Split
    Simple Encryption #2 - Index-Difference
    Simple Encryption #3 - Turn The Bits Around
    Simple Encryption #4 - Qwerty
    
    Have fun coding it and please don't forget to vote and rank this kata! :-)
    """


def decrypt(encrypted_text: str) -> str:
    """
    The reverse process of the encryption described above.
    """


# Tests

def check(candidate):
    assert candidate('$-Wy,dM79H\'i\'o$n0C&I.ZTcMJw5vPlZc Hn!krhlaa:khV mkL;gvtP-S7Rt1Vp2RV:wV9VuhO Iz3dqb.U0w') == 'Do the kata "Kobayashi-Maru-Test!" Endless fun and excitement when finding a solution!'
    assert candidate('5MyQa9p0riYplZc') == 'This is a test!'
    assert candidate('5MyQa79H\'ijQaw!Ns6jVtpmnlZ.V6p') == 'This kata is very interesting!'
    assert candidate('') == ''
    assert candidate(None) == None
check(encrypt)
def find_unique_k_diff_pairs(arr: list[int], k: int) -> int:
    """
    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.
    
    
    
    Example 1:
    
    Input: [3, 1, 4, 1, 5], k = 2
    Output: 2
    Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).Although we have two 1s in the input, we should only return the number of unique pairs.
    
    
    
    Example 2:
    
    Input:[1, 2, 3, 4, 5], k = 1
    Output: 4
    Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
    
    
    
    Example 3:
    
    Input: [1, 3, 1, 5, 4], k = 0
    Output: 1
    Explanation: There is one 0-diff pair in the array, (1, 1).
    
    
    
    Note:
    
    The pairs (i, j) and (j, i) count as the same pair.
    The length of the array won't exceed 10,000.
    All the integers in the given input belong to the range: [-1e7, 1e7].
    """


# Tests

def check(candidate):
    assert candidate([3, 1, 4, 1, 5], 2) == 2
    assert candidate([1, 2, 3, 4, 5], 1) == 4
    assert candidate([1, 3, 1, 5, 4], 0) == 1
check(find_unique_k_diff_pairs)
def check_parity_QC(n: int, cases: List[Tuple[int, int]]) -> List[int]:
    """
    The Quark Codejam's number QC(n, m) represents the number of ways to partition a set of n things into m nonempty subsets. For example, there are seven ways to split a four-element set into two parts:
    
    {1, 2, 3} ∪ {4}, {1, 2, 4} ∪ {3}, {1, 3, 4} ∪ {2}, {2, 3, 4} ∪ {1},
    
    {1, 2} ∪ {3, 4}, {1, 3} ∪ {2, 4}, {1, 4} ∪ {2, 3}.
    
    We can compute QC(n, m) using the recurrence,
    
    QC(n, m) = mQC(n − 1, m) + QC(n − 1, m − 1), for integers 1 < m < n.
    
    but your task is a somewhat different: given integers n and m, compute the parity of QC(n, m), i.e. QC(n, m) mod 2.
    
    Example :
    
    QC(4, 2) mod 2 = 1.
    Write a program that reads two positive integers n and m, computes QC(n, m) mod 2, and writes the
    
    result.
    
    -----Input-----
    The input begins with a single positive integer on a line by itself indicating the number of the cases. This line is followed by the input cases.
    
    The input consists two integers n and m separated by a space, with 1 ≤ m ≤ n ≤ 1000000000.
    
    -----Output-----
    For each test case, print the output.
    
    The output should be the integer S(n, m) mod 2.
    Sample Input
    1
    
    4 2
    Sample Output
    1
    """


# Tests

def check(candidate):
    assert candidate(1, [(4, 2)]) == [1]
check(check_parity_QC)
def pawn_chess(T: int, test_cases: List[str]) -> List[str]:
    """
    Ada is playing pawn chess with Suzumo.
    Pawn chess is played on a long board with N$N$ squares in one row. Initially, some of the squares contain pawns.
    Note that the colours of the squares and pawns do not matter in this game, but otherwise, the standard chess rules apply:
    - no two pawns can occupy the same square at the same time
    - a pawn cannot jump over another pawn (they are no knights!), i.e. if there is a pawn at square i$i$, then it can only be moved to square i−2$i-2$ if squares i−1$i-1$ and i−2$i-2$ are empty
    - pawns cannot move outside of the board (outs are forbidden)
    The players alternate turns; as usual, Ada plays first. In each turn, the current player must choose a pawn and move it either one or two squares to the left of its current position. The player that cannot make a move loses.
    Can Ada always beat Suzumo? Remember that Ada is a chess grandmaster, so she always plays optimally.
    
    -----Input-----
    - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows.
    - The first and only line of each test case contains a single string S$S$ with length N$N$ describing the initial board from left to right. An empty square and a square containing a pawn are denoted by the characters '.' and 'P' respectively.
    
    -----Output-----
    For each test case, print a single line containing the string "Yes" if Ada wins the game or "No" otherwise (without quotes).
    
    -----Constraints-----
    - 1≤T≤500$1 \le T \le 500$
    - 2≤N≤128$2 \le N \le 128$
    - S$S$ contains only characters '.' and 'P'
    
    -----Example Input-----
    1
    ..P.P
    
    -----Example Output-----
    Yes
    
    -----Explanation-----
    Example case 1: Ada can move the first pawn two squares to the left; the board after this move looks like
    P...P
    
    and now, Suzumo can only move the second pawn. If he moves it one square to the left, Ada will move it two squares to the left on her next move, and if he moves it two squares to the left, Ada will move it one square to the left, so the board after Ada's next move will look like
    PP...
    
    and Suzumo cannot make any move here.
    """


# Tests

def check(candidate):
    assert candidate(1, ['..P.P']) == ['Yes']
check(pawn_chess)
def minimize_m(T: int, test_cases: List[Tuple[int, List[int]]]) -> List[int]:
    """
    "I'm a fan of anything that tries to replace actual human contact." - Sheldon.
    After years of hard work, Sheldon was finally able to develop a formula which would diminish the real human contact.
    He found k$k$ integers n1,n2...nk$n_1,n_2...n_k$ . Also he found that if he could minimize the value of m$m$ such that  ∑ki=1$\sum_{i=1}^k$n$n$i$i$C$C$m$m$i$i$ is even, where m$m$ = ∑ki=1$\sum_{i=1}^k$mi$m_i$, he would finish the real human contact.
    Since Sheldon is busy choosing between PS-4 and XBOX-ONE, he want you to help him to calculate the minimum value of m$m$.
    
    -----Input:-----
    - The first line of the input contains a single integer T$T$ denoting the number of test cases. The
    description of T$T$ test cases follows.
    - The first line of each test case contains a single integer k$k$.
    - Next line contains k space separated integers n1,n2...nk$n_1,n_2...n_k$ .
    
    -----Output:-----
    For each test case output the minimum value of m for which ∑ki=1$\sum_{i=1}^k$n$n$i$i$C$C$m$m$i$i$ is even, where  m$m$=m1$m_1$+m2$m_2$+. . . mk$m_k$   and  0$0$ <= mi$m_i$<= ni$n_i$ .  If no such answer exists print -1.
    
    -----Constraints-----
    - 1≤T≤1000$1 \leq T \leq 1000$
    - 1≤k≤1000$1 \leq k \leq 1000$
    - 1≤ni≤10$1 \leq n_i \leq 10$18$18$
    
    -----Sample Input:-----
    1
    1
    5
    
    -----Sample Output:-----
    2
    
    -----EXPLANATION:-----
    5$5$C$C$2$2$ = 10 which is even and m is minimum.
    """


# Tests

def check(candidate):
    assert candidate(1, [(1, [5])]) == [2]
check(minimize_m)
def count_bubbly_words(M: int, words: List[str]) -> int:
    """
    -----Problem-----
    
    Nikki's latest work is writing a story of letters. However, she finds writing story so boring that, after working for three hours, she realized that all she has written are M long words consisting entirely of letters A and B. Having accepted that she will never finish the story in time, Nikki has decided to at least have some fun with it by counting bubbly words.
    
    
    Now Nikki is connecting pairs of identical letters (A with A, B with B) by drawing lines above the word. A given word is bubbly if each letter can be connected to exactly one other letter in such a way that no two lines intersect. So here is your task. Help Nikki count how many words are bubbly.
    
    -----Input-----
    -
    The first line of input contains the positive integer M, the number of words written down by Nikki.
    
    -
    Each of the following M lines contains a single word consisting of letters A and B, with length
    
    between 2 and 10^5, inclusive. The sum of lengths of all words doesn't exceed 10^6.
    
    
    -----Output-----
    
    The first and only line of output must contain the number of bubbly words.
    
    
    -----Constraints-----
    -
    1 ≤ M ≤ 100
    
    
    -----Sample Input-----
    
    3
    
    ABAB
    
    AABB
    
    ABBA
    
    -----Sample Output-----
    2
    
    -----Explanation-----
    -
    ABAB - It is not bubbly as A(indexed 1) will connect to A(indexed 3) by a line and when we try to connect B(indexed 2) with B(indexed 4) by a line then it will intersect with the line b/w A and A.
    
    -
    AABB - It is bubbly as line b/w A and A will not intersect with the line b/w B and B.
    
    -
    ABBA -It is also bubbly as lines will not intersect. We can draw line b/w A and A above the line b/w B and B.
    p { text-align:justify }
    """


# Tests

def check(candidate):
    assert candidate(3, ['ABAB', 'AABB', 'ABBA']) == 2
check(count_bubbly_words)
def sum_of_multiples(T: int, test_cases: List[int]) -> List[int]:
    """
    Find sum of all the numbers that are multiples of 10 and are less than or equal to a given number "N". (quotes for clarity and be careful of integer overflow)
    
    -----Input-----
    Input will start with an integer T the count of test cases, each case will have an integer N.
    
    -----Output-----
    Output each values, on a newline.
    
    -----Constraints-----
    - 1 ≤ T ≤ 10
    - 1 ≤ N ≤1000000000
    
    -----Example-----
    Input:
    1
    10
    
    Output:
    10
    
    -----Explanation-----
    Example case 1. Only integer that is multiple 10 that is less than or equal to 10 is 10
    """


# Tests

def check(candidate):
    assert candidate(1, [10]) == [10]
    assert candidate(2, [10, 20]) == [10, 30]
    assert candidate(1, [1]) == [0]
    assert candidate(3, [30, 40, 50]) == [60, 100, 150]
check(sum_of_multiples)
def max_nice_bouquet(T: int, test_cases: List[Tuple[List[int], List[int], List[int]]]) -> List[int]:
    """
    It's autumn now, the time of the leaf fall.
    Sergey likes to collect fallen leaves in autumn. In his city, he can find fallen leaves of maple, oak and poplar. These leaves can be of three different colors: green, yellow or red.
    Sergey has collected some leaves of each type and color. Now he wants to create the biggest nice bouquet from them. He considers the bouquet nice iff all the leaves in it are either from the same type of tree or of the same color (or both). Moreover, he doesn't want to create a bouquet with even number of leaves in it, since this kind of bouquets are considered to attract bad luck. However, if it's impossible to make any nice bouquet, he won't do anything, thus, obtaining a bouquet with zero leaves.
    Please help Sergey to find the maximal number of leaves he can have in a nice bouquet, which satisfies all the above mentioned requirements.
    Please note that Sergey doesn't have to use all the leaves of the same color or of the same type. For example, if he has 20 maple leaves, he can still create a bouquet of 19 leaves.
    
    -----Input-----
    IThe first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows."
    The first line of each test case contains three space-separated integers MG MY MR denoting the number of green, yellow and red maple leaves respectively.
    The second line contains three space-separated integers OG OY OR denoting the number of green, yellow and red oak leaves respectively.
    The third line of each test case contains three space-separated integers PG PY PR denoting the number of green, yellow and red poplar leaves respectively.
    
    -----Output-----
    For each test case, output a single line containing the maximal amount of flowers in nice bouquet, satisfying all conditions or 0 if it's impossible to create any bouquet, satisfying the conditions.
    
    -----Constraints-----
    
    - 1 ≤ T ≤ 10000
    - Subtask 1 (50 points): 0 ≤ MG, MY, MR, OG, OY, OR, PG, PY, PR ≤ 5
    - Subtask 2 (50 points): 0 ≤ MG, MY, MR, OG, OY, OR, PG, PY, PR ≤ 109
    
    -----Example-----
    Input:1
    1 2 3
    3 2 1
    1 3 4
    
    Output:7
    
    -----Explanation-----
    Example case 1. We can create a bouquet with 7 leaves, for example, by collecting all yellow leaves. This is not the only way to create the nice bouquet with 7 leaves (for example, Sergey can use all but one red leaves), but it is impossible to create a nice bouquet with more than 7 leaves.
    """


# Tests

def check(candidate):
    assert candidate(1, [([1, 2, 3], [3, 2, 1], [1, 3, 4])]) == [7]
check(max_nice_bouquet)
def min_moves_to_sort(N: int, books: List[int]) -> int:
    """
    Indraneel has to sort the books in his library. His library has one long shelf. His books are numbered $1$ through $N$ and he wants to rearrange the books so that they appear in the sequence $1,2, ..., N$.
    He intends to do this by a sequence of moves. In each move he can pick up any book from the shelf and insert it at a different place in the shelf. Suppose Indraneel has $5$ books and they are initially arranged in the order
    21453214532 \quad 1 \quad 4 \quad 5 \quad 3
    Indraneel will rearrange this in ascending order by first moving book $1$ to the beginning of the shelf to get
    12453124531 \quad 2 \quad 4 \quad 5 \quad 3
    Then, moving book $3$ to position $3$, he gets
    12345123451 \quad 2 \quad 3 \quad 4 \quad 5
    Your task is to write a program to help Indraneel determine the minimum number of moves that are necessary to sort his book shelf.
    
    -----Input:-----
    The first line of the input will contain a single integer $N$ indicating the number of books in Indraneel's library. This is followed by a line containing a permutation of $1, 2, ..., N$ indicating the intial state of Indraneel's book-shelf.
    
    -----Output:-----
    A single integer indicating the minimum number of moves necessary to sort Indraneel's book-shelf.
    
    -----Constraints:-----
    - $1 \leq N \leq 200000$.
    - You may also assume that in $50 \%$ of the inputs, $1 \leq N \leq 5000$.
    
    -----Sample Input-----
    5
    2 1 4 5 3
    
    -----Sample Output-----
    2
    """


# Tests

def check(candidate):
    assert candidate(5, [2, 1, 4, 5, 3]) == 2
check(min_moves_to_sort)
def score_of_parentheses(S: str) -> int:
    """
    Given a balanced parentheses string S, compute the score of the string based on the following rule:
    
    () has score 1
    AB has score A + B, where A and B are balanced parentheses strings.
    (A) has score 2 * A, where A is a balanced parentheses string.
    
    
    
    Example 1:
    Input: "()"
    Output: 1
    
    
    Example 2:
    Input: "(())"
    Output: 2
    
    
    Example 3:
    Input: "()()"
    Output: 2
    
    
    Example 4:
    Input: "(()(()))"
    Output: 6
    
    
    Note:
    
    S is a balanced parentheses string, containing only ( and ).
    2 <= S.length <= 50
    """


# Tests

def check(candidate):
    assert candidate('()') == 1
    assert candidate('(())') == 2
    assert candidate('()()') == 2
    assert candidate('(()(()))') == 6
check(score_of_parentheses)
def factorial_modulo(T: int, numbers: List[int]) -> List[int]:
    """
    Master Oogway has forseen that a panda named Po will be the dragon warrior, and the master of Chi. But he did not tell anyone about the spell that would make him the master of Chi, and has left Po confused. Now Po has to defeat Kai, who is the super villian, the strongest of them all. Po needs to master Chi, and he finds a spell which unlocks his powerful Chi. But the spell is rather strange. It asks Po to calculate the factorial of a number! Po is very good at mathematics, and thinks that this is very easy. So he leaves the spell, thinking it's a hoax. But little does he know that this can give him the ultimate power of Chi. Help Po by solving the spell and proving that it's not a hoax.
    
    -----Input-----
    First line of input contains an integer T denoting the number of test cases.
    The next T lines contain an integer N.
    
    -----Output-----
    For each test case, print a single line containing the solution to the spell which is equal to factorial of N, i.e. N!. Since the output could be large, output it modulo 1589540031(Grand Master Oogway's current age).
    
    -----Constraints-----
    - 1 ≤ T ≤ 100000
    - 1 ≤ N ≤ 100000
    
    -----Example-----
    Input:
    4
    1
    2
    3
    4
    
    Output:
    1
    2
    6
    24
    """


# Tests

def check(candidate):
    assert candidate(4, [1, 2, 3, 4]) == [1, 2, 6, 24]
check(factorial_modulo)
def expected_cost(T: int, test_cases: List[Tuple[int, int, List[Tuple[int, int]]]]) -> List[float]:
    """
    The Little Elephant from the Zoo of Lviv is going to the Birthday Party of  the Big Hippo tomorrow. Now he wants to prepare a gift for the Big Hippo.
    
    He has N balloons, numbered from 1 to N. The i-th balloon has the color Ci and it costs Pi dollars. The gift for the Big Hippo will be any subset (chosen randomly, possibly empty) of the balloons such that the number of different colors in that subset is at least M.
    
    Help Little Elephant to find the expected cost of the gift.
    
    -----Input-----
    The first line of the input contains a single integer T - the number of test cases. T test cases follow. The first line of each test case contains a pair of integers N and M. The next N lines contain N pairs of integers Ci and Pi, one pair per line.
    
    -----Output-----
    In T lines print T real numbers - the answers for the corresponding test cases. Your answer will considered correct if it has at most 10^-6 absolute or relative error.
    
    -----Constraints-----
    - 1 ≤ T ≤ 40
    - 1 ≤ N,  Ci≤ 40
    - 1 ≤ Pi ≤ 1000000
    - 0 ≤ M ≤ K, where K is the number of different colors in the test case.
    
    -----Example-----
    Input:
    2
    2 2
    1 4
    2 7
    2 1
    1 4
    2 7
    
    Output:
    11.000000000
    7.333333333
    """


# Tests

def check(candidate):
    assert abs(candidate(2, [(2, 2, [(1, 4), (2, 7)]), (2, 1, [(1, 4), (2, 7)])]) - [11.000000000, 7.333333333]) < 1e-6
check(expected_cost)
def calculate_distances(N: int, M: int, catchers: List[Tuple[int, int]], S: str) -> List[int]:
    """
    Today, puppy Tuzik is going to a new dog cinema. He has already left his home and just realised that he forgot his dog-collar! This is a real problem because the city is filled with catchers looking for stray dogs.
    A city where Tuzik lives in can be considered as an infinite grid, where each cell has exactly four neighbouring cells: those sharing a common side with the cell. Such a property of the city leads to the fact, that the distance between cells (xA, yA) and (xB, yB) equals |xA - xB| + |yA - yB|.
    Initially, the puppy started at the cell with coordinates (0, 0). There are N dog-catchers located at the cells with the coordinates (xi, yi), where 1 ≤ i ≤ N. Tuzik's path can be described as a string S of M characters, each of which belongs to the set {'D', 'U', 'L', 'R'} (corresponding to it moving down, up, left, and right, respectively). To estimate his level of safety, Tuzik wants to know the sum of the distances from each cell on his path to all the dog-catchers. You don't need to output this sum for the staring cell of the path (i.e. the cell with the coordinates (0, 0)).
    
    -----Input-----
    The first line of the input contains two integers N and M.
    The following N lines contain two integers xi and yi each, describing coordinates of the dog-catchers.
    The last line of the input contains string S of M characters on the set {'D', 'U', 'L', 'R'}.
    - 'D' - decrease y by 1
    - 'U' - increase y by 1
    - 'L' - decrease x by 1
    - 'R' - increase x by 1
    
    -----Output-----
    Output M lines: for each cell of the path (except the starting cell), output the required sum of the distances.
    
    -----Constraints-----
    - 1 ≤ N ≤ 3 ✕ 105
    - 1 ≤ M ≤ 3 ✕ 105
    - -106 ≤ xi, yi ≤ 106
    
    -----Example-----
    Input:
    2 3
    1 2
    0 1
    RDL
    
    Output:
    4
    6
    6
    
    -----Explanation-----
    
    Initially Tuzik stays at cell (0, 0). Let's consider his path:
    
    - Move 'R' to the cell (1, 0). Distance to the catcher (1, 2) equals 2, distance to the catcher (0, 1) equals 2, so the total distance equals 4
    - Move 'D' to the cell (1, -1). Distance to the catcher (1, 2) equals 3, distance to the catcher (0, 1) equals 3, so the total distance equals 6
    - Move 'L' to the cell (0, -1). Distance to the catcher (1, 2) equals 4, distance to the catcher (0, 1) equals 2, so the total distance equals 6
    """


# Tests

def check(candidate):
    assert candidate(2, 3, [(1, 2), (0, 1)], 'RDL') == [4, 6, 6]
check(calculate_distances)