NazmusAshrafi/apps_probs huggingface dataset:
def is_rectangle_overlap(rec1: list, rec2: list) -> bool:
    """
    An axis-aligned rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) is the coordinate of its bottom-left corner, and (x2, y2) is the coordinate of its top-right corner. Its top and bottom edges are parallel to the X-axis, and its left and right edges are parallel to the Y-axis.
    Two rectangles overlap if the area of their intersection is positive. To be clear, two rectangles that only touch at the corner or edges do not overlap.
    Given two axis-aligned rectangles rec1 and rec2, return true if they overlap, otherwise return false.
    
    Example 1:
    Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
    Output: true
    Example 2:
    Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
    Output: false
    Example 3:
    Input: rec1 = [0,0,1,1], rec2 = [2,2,3,3]
    Output: false
    
    
    Constraints:
    
    rect1.length == 4
    rect2.length == 4
    -109 <= rec1[i], rec2[i] <= 109
    rec1[0] <= rec1[2] and rec1[1] <= rec1[3]
    rec2[0] <= rec2[2] and rec2[1] <= rec2[3]
    """


# Tests

def check(candidate):
    assert candidate([0, 0, 2, 2], [1, 1, 3, 3]) == True
    assert candidate([0, 0, 1, 1], [1, 0, 2, 1]) == False
    assert candidate([0, 0, 1, 1], [2, 2, 3, 3]) == False
check(is_rectangle_overlap)
def buddy_strings(A: str, B: str) -> bool:
    """
    Given two strings A and B of lowercase letters, return true if you can swap two letters in A so the result is equal to B, otherwise, return false.
    Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at A[i] and A[j]. For example, swapping at indices 0 and 2 in "abcd" results in "cbad".
    
    Example 1:
    Input: A = "ab", B = "ba"
    Output: true
    Explanation: You can swap A[0] = 'a' and A[1] = 'b' to get "ba", which is equal to B.
    
    Example 2:
    Input: A = "ab", B = "ab"
    Output: false
    Explanation: The only letters you can swap are A[0] = 'a' and A[1] = 'b', which results in "ba" != B.
    
    Example 3:
    Input: A = "aa", B = "aa"
    Output: true
    Explanation: You can swap A[0] = 'a' and A[1] = 'a' to get "aa", which is equal to B.
    
    Example 4:
    Input: A = "aaaaaaabc", B = "aaaaaaacb"
    Output: true
    
    Example 5:
    Input: A = "", B = "aa"
    Output: false
    
    
    Constraints:
    
    0 <= A.length <= 20000
    0 <= B.length <= 20000
    A and B consist of lowercase letters.
    """


# Tests

def check(candidate):
    assert candidate('ab', 'ba') == True
    assert candidate('ab', 'ab') == False
    assert candidate('aa', 'aa') == True
    assert candidate('aaaaaaabc', 'aaaaaaacb') == True
    assert candidate('', 'aa') == False
check(buddy_strings)
def mystery_range(s: str, n: int) -> list[int]:
    """
    In this kata, your task is to write a function that returns the smallest and largest integers in an unsorted string. In this kata, a range is considered a finite sequence of consecutive integers.
    Input
    Your function will receive two arguments:
    
    A string comprised of integers in an unknown range; think of this string as the result when a range of integers is shuffled around in random order then joined together into a string
    An integer value representing the size of the range
    
    Output
    Your function should return the starting (minimum) and ending (maximum) numbers of the range in the form of an array/list comprised of two integers.
    
    Test Example
    
    ```python
    input_string = '1568141291110137'
    
    mystery_range(input_string, 10) # [6, 15]
    
    # The 10 numbers in this string are:
    # 15 6 8 14 12 9 11 10 13 7
    # Therefore the range of numbers is from 6 to 15
    ```
    
    
    Technical Details
    
    The maximum size of a range will be 100 integers
    The starting number of a range will be: 0 < n < 100
    Full Test Suite: 21 fixed tests, 100 random tests
    Use Python 3+ for the Python translation
    For JavaScript, require has been disabled and most built-in prototypes have been frozen (prototype methods can be added to Array and Function)
    All test cases will be valid
    
    If you enjoyed this kata, be sure to check out my other katas
    """


# Tests

def check(candidate):
    assert candidate('1568141291110137', 10) == [6, 15]
check(mystery_range)
def min_start_value(nums: list[int]) -> int:
    """
    Given an array of integers nums, you start with an initial positive value startValue.
    In each iteration, you calculate the step by step sum of startValue plus elements in nums (from left to right).
    Return the minimum positive value of startValue such that the step by step sum is never less than 1.
    
    Example 1:
    Input: nums = [-3,2,-3,4,2]
    Output: 5
    Explanation: If you choose startValue = 4, in the third iteration your step by step sum is less than 1.
    step by step sum
    startValue = 4 | startValue = 5 | nums
    (4 -3 ) = 1  | (5 -3 ) = 2    |  -3
    (1 +2 ) = 3  | (2 +2 ) = 4    |   2
    (3 -3 ) = 0  | (4 -3 ) = 1    |  -3
    (0 +4 ) = 4  | (1 +4 ) = 5    |   4
    (4 +2 ) = 6  | (5 +2 ) = 7    |   2
    
    Example 2:
    Input: nums = [1,2]
    Output: 1
    Explanation: Minimum start value should be positive.
    
    Example 3:
    Input: nums = [1,-2,-3]
    Output: 5
    
    
    Constraints:
    
    1 <= nums.length <= 100
    -100 <= nums[i] <= 100
    """


# Tests

def check(candidate):
    assert candidate([-3, 2, -3, 4, 2]) == 5
    assert candidate([1, 2]) == 1
    assert candidate([1, -2, -3]) == 5
check(min_start_value)
def min_cost_to_move_chips(position: list[int]) -> int:
    """
    We have n chips, where the position of the ith chip is position[i].
    We need to move all the chips to the same position. In one step, we can change the position of the ith chip from position[i] to:
    
    position[i] + 2 or position[i] - 2 with cost = 0.
    position[i] + 1 or position[i] - 1 with cost = 1.
    
    Return the minimum cost needed to move all the chips to the same position.
    
    Example 1:
    
    Input: position = [1,2,3]
    Output: 1
    Explanation: First step: Move the chip at position 3 to position 1 with cost = 0.
    Second step: Move the chip at position 2 to position 1 with cost = 1.
    Total cost is 1.
    
    Example 2:
    
    Input: position = [2,2,2,3,3]
    Output: 2
    Explanation: We can move the two chips at poistion 3 to position 2. Each move has cost = 1. The total cost = 2.
    
    Example 3:
    Input: position = [1,1000000000]
    Output: 1
    
    
    Constraints:
    
    1 <= position.length <= 100
    1 <= position[i] <= 10^9
    """


# Tests

def check(candidate):
    assert candidate([1, 2, 3]) == 1
    assert candidate([2, 2, 2, 3, 3]) == 2
    assert candidate([1, 1000000000]) == 1
check(min_cost_to_move_chips)
def build_palindrome(s: str) -> str:
    """
    ## Task
    
    Given a string, add the fewest number of characters possible from the front or back to make it a palindrome.
    
    ## Example
    
    For the input `cdcab`, the output should be `bacdcab`
    
    ## Input/Output
    
    Input is a string consisting of lowercase latin letters with length 3 <= str.length <= 10
    
    The output is a palindrome string satisfying the task.
    
    For s = `ab` either solution (`aba` or `bab`) will be accepted.
    """


# Tests

def check(candidate):
    assert candidate('abcdc') == 'abcdcba'
    assert candidate('ababa') == 'ababa'
check(build_palindrome)
def containsDuplicate(nums: list[int]) -> bool:
    """
    Given an array of integers, find if the array contains any duplicates.
    
    Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
    
    Example 1:
    
    
    Input: [1,2,3,1]
    Output: true
    
    Example 2:
    
    
    Input: [1,2,3,4]
    Output: false
    
    Example 3:
    
    
    Input: [1,1,1,3,3,4,3,2,4,2]
    Output: true
    """


# Tests

def check(candidate):
    assert candidate([1, 2, 3, 1]) == True
    assert candidate([1, 2, 3, 4]) == False
    assert candidate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2]) == True
check(containsDuplicate)
def vaccine_list(age: str, status: str, month: str) -> list[str]:
    """
    ### Vaccinations for children under 5
    You have been put in charge of administrating vaccinations for children in your local area. Write a function that will generate a list of vaccines for each child presented for vaccination, based on the child's age and vaccination history, and the month of the year.
    #### The function takes three parameters: age, status and month
    - The parameter 'age' will be given in weeks up to 16 weeks, and thereafter in months. You can assume that children presented will be scheduled for vaccination (eg '16 weeks', '12 months' etc).
    - The parameter 'status' indicates if the child has missed a scheduled vaccination, and the argument will be a string that says 'up-to-date', or a scheduled stage (eg '8 weeks') that has been missed, in which case you need to add any missing shots to the list. Only one missed vaccination stage will be passed in per function call.
    - If the month is 'september', 'october' or 'november' add 'offer fluVaccine' to the list.
    - Make sure there are no duplicates in the returned list, and sort it alphabetically.
    
    #### Example input and output
    ~~~~
    input     ('12 weeks', 'up-to-date', 'december')
    output    ['fiveInOne', 'rotavirus']
    
    input     ('12 months', '16 weeks', 'june')
    output     ['fiveInOne', 'hibMenC', 'measlesMumpsRubella', 'meningitisB', 'pneumococcal']
    
    input     ('40 months', '12 months', 'october')
    output    ['hibMenC', 'measlesMumpsRubella', 'meningitisB', 'offer fluVaccine', 'preSchoolBooster']
    ~~~~
    
    #### To save you typing it up, here is the vaccinations list
    ~~~~
    fiveInOne : ['8 weeks', '12 weeks', '16 weeks'],
    //Protects against: diphtheria, tetanus, whooping cough, polio and Hib (Haemophilus influenzae type b)
    pneumococcal : ['8 weeks', '16 weeks'],
    //Protects against: some types of pneumococcal infection
    rotavirus : ['8 weeks', '12 weeks'],
    //Protects against: rotavirus infection, a common cause of childhood diarrhoea and sickness
    meningitisB : ['8 weeks', '16 weeks', '12 months'],
    //Protects against: meningitis caused by meningococcal type B bacteria
    hibMenC : ['12 months'],
    //Protects against: Haemophilus influenzae type b (Hib), meningitis caused by meningococcal group C bacteria
    measlesMumpsRubella : ['12 months', '40 months'],
    //Protects against: measles, mumps and rubella
    fluVaccine : ['september','october','november'],
    //Given at: annually in Sept/Oct
    preSchoolBooster : ['40 months']
    //Protects against: diphtheria, tetanus, whooping cough and polio
    ~~~~
    """


# Tests

def check(candidate):
    assert candidate('12 weeks', 'up-to-date', 'december') == ['fiveInOne', 'rotavirus']
    assert candidate('12 months', '16 weeks', 'june') == ['fiveInOne', 'hibMenC', 'measlesMumpsRubella', 'meningitisB', 'pneumococcal']
    assert candidate('40 months', '12 months', 'october') == ['hibMenC', 'measlesMumpsRubella', 'meningitisB', 'offer fluVaccine', 'preSchoolBooster']
check(vaccine_list)
def max_burles_spent(t: int, test_cases: list[int]) -> list[int]:
    """
    Mishka wants to buy some food in the nearby shop. Initially, he has $s$ burles on his card.
    
    Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number $1 \le x \le s$, buy food that costs exactly $x$ burles and obtain $\lfloor\frac{x}{10}\rfloor$ burles as a cashback (in other words, Mishka spends $x$ burles and obtains $\lfloor\frac{x}{10}\rfloor$ back). The operation $\lfloor\frac{a}{b}\rfloor$ means $a$ divided by $b$ rounded down.
    
    It is guaranteed that you can always buy some food that costs $x$ for any possible value of $x$.
    
    Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.
    
    For example, if Mishka has $s=19$ burles then the maximum number of burles he can spend is $21$. Firstly, he can spend $x=10$ burles, obtain $1$ burle as a cashback. Now he has $s=10$ burles, so can spend $x=10$ burles, obtain $1$ burle as a cashback and spend it too.
    
    You have to answer $t$ independent test cases.
    
    
    -----Input-----
    
    The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
    
    The next $t$ lines describe test cases. Each test case is given on a separate line and consists of one integer $s$ ($1 \le s \le 10^9$) — the number of burles Mishka initially has.
    
    
    -----Output-----
    
    For each test case print the answer on it — the maximum number of burles Mishka can spend if he buys food optimally.
    
    
    -----Example-----
    Input
    6
    1
    10
    19
    9876
    12345
    1000000000
    
    Output
    1
    11
    21
    10973
    13716
    1111111111
    """


# Tests

def check(candidate):
    assert candidate(6, [1, 10, 19, 9876, 12345, 1000000000]) == [1, 11, 21, 10973, 13716, 1111111111]
check(max_burles_spent)
def pac_man(N: int, PM: list[int], enemies: list[list[int]]) -> int:
    """
    # Task
    Pac-Man got lucky today! Due to minor performance issue all his enemies have frozen. Too bad Pac-Man is not brave enough to face them right now, so he doesn't want any enemy to see him.
    
    Given a gamefield of size `N` x `N`, Pac-Man's position(`PM`) and his enemies' positions(`enemies`), your task is to count the number of coins he can collect without being seen.
    
    An enemy can see a Pac-Man if they are standing on the same row or column.
    
    It is guaranteed that no enemy can see Pac-Man on the starting position. There is a coin on each empty square (i.e. where there is no Pac-Man or enemy).
    
    # Example
    
    For `N = 4, PM = [3, 0], enemies = [[1, 2]]`, the result should be `3`.
    ```
    Let O represent coins, P - Pac-Man and E - enemy.
    OOOO
    OOEO
    OOOO
    POOO```
    Pac-Man cannot cross row 1 and column 2.
    
    He can only collect coins from points `(2, 0), (2, 1) and (3, 1)`, like this:
    ```
    x is the points that Pac-Man can collect the coins.
    OOOO
    OOEO
    xxOO
    PxOO
    ```
    
    # Input/Output
    
    
    - `[input]` integer `N`
    
    The field size.
    
    
    - `[input]` integer array `PM`
    
    Pac-Man's position (pair of integers)
    
    
    - `[input]` 2D integer array `enemies`
    
    Enemies' positions (array of pairs)
    
    
    - `[output]` an integer
    
    Number of coins Pac-Man can collect.
    
    
    # More PacMan Katas
    
    - [Play PacMan: Devour all](https://www.codewars.com/kata/575c29d5fcee86cb8b000136)
    
    - [Play PacMan 2: The way home](https://www.codewars.com/kata/575ed46e23891f67d90000d8)
    """


# Tests

def check(candidate):
    assert candidate(1, [0, 0], []) == 0
    assert candidate(2, [0, 0], []) == 3
    assert candidate(3, [0, 0], []) == 8
    assert candidate(3, [1, 1], []) == 8
    assert candidate(2, [0, 0], [[1, 1]]) == 0
    assert candidate(3, [2, 0], [[1, 1]]) == 0
    assert candidate(3, [2, 0], [[0, 2]]) == 3
    assert candidate(10, [4, 6], [[0, 2], [5, 2], [5, 5]]) == 15
    assert candidate(8, [1, 1], [[5, 4]]) == 19
    assert candidate(8, [1, 5], [[5, 4]]) == 14
    assert candidate(8, [6, 1], [[5, 4]]) == 7
check(pac_man)
def distanceBetweenBusStops(distance: List[int], start: int, destination: int) -> int:
    """
    A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n.
    The bus goes along both directions i.e. clockwise and counterclockwise.
    Return the shortest distance between the given start and destination stops.
    
    Example 1:
    
    Input: distance = [1,2,3,4], start = 0, destination = 1
    Output: 1
    Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1.
    
    Example 2:
    
    Input: distance = [1,2,3,4], start = 0, destination = 2
    Output: 3
    Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.
    
    
    Example 3:
    
    Input: distance = [1,2,3,4], start = 0, destination = 3
    Output: 4
    Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.
    
    
    Constraints:
    
    1 <= n <= 10^4
    distance.length == n
    0 <= start, destination < n
    0 <= distance[i] <= 10^4
    """


# Tests

def check(candidate):
    assert candidate([1, 2, 3, 4], 0, 1) == 1
    assert candidate([1, 2, 3, 4], 0, 2) == 3
    assert candidate([1, 2, 3, 4], 0, 3) == 4
check(distanceBetweenBusStops)
def reverse(x: int) -> int:
    """
    Given a 32-bit signed integer, reverse digits of an integer.
    
    Example 1:
    
    
    Input: 123
    Output: 321
    
    
    Example 2:
    
    
    Input: -123
    Output: -321
    
    
    Example 3:
    
    
    Input: 120
    Output: 21
    
    
    Note:
    Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
    """


# Tests

def check(candidate):
    assert candidate(123) == 321
    assert candidate(-123) == -321
    assert candidate(120) == 21
check(reverse)
def min_moves_to_regular_bracket_sequence(t: int, test_cases: list[tuple[int, str]]) -> list[int]:
    """
    You are given a bracket sequence $s$ of length $n$, where $n$ is even (divisible by two). The string $s$ consists of $\frac{n}{2}$ opening brackets '(' and $\frac{n}{2}$ closing brackets ')'.
    
    In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index $i$, remove the $i$-th character of $s$ and insert it before or after all remaining characters of $s$).
    
    Your task is to find the minimum number of moves required to obtain regular bracket sequence from $s$. It can be proved that the answer always exists under the given constraints.
    
    Recall what the regular bracket sequence is:
    
    "()" is regular bracket sequence;  if $s$ is regular bracket sequence then "(" + $s$ + ")" is regular bracket sequence;  if $s$ and $t$ are regular bracket sequences then $s$ + $t$ is regular bracket sequence.
    
    For example, "()()", "(())()", "(())" and "()" are regular bracket sequences, but ")(", "()(" and ")))" are not.
    
    You have to answer $t$ independent test cases.
    
    
    -----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$ ($2 \le n \le 50$) — the length of $s$. It is guaranteed that $n$ is even. The second line of the test case containg the string $s$ consisting of $\frac{n}{2}$ opening and $\frac{n}{2}$ closing brackets.
    
    
    -----Output-----
    
    For each test case, print the answer — the minimum number of moves required to obtain regular bracket sequence from $s$. It can be proved that the answer always exists under the given constraints.
    
    
    -----Example-----
    Input
    4
    2
    )(
    4
    ()()
    8
    ())()()(
    10
    )))((((())
    
    Output
    1
    0
    1
    3
    
    
    
    -----Note-----
    
    In the first test case of the example, it is sufficient to move the first bracket to the end of the string.
    
    In the third test case of the example, it is sufficient to move the last bracket to the beginning of the string.
    
    In the fourth test case of the example, we can choose last three openning brackets, move them to the beginning of the string and obtain "((()))(())".
    """


# Tests

def check(candidate):
    assert candidate(4, [(2, ')('), (4, '()()'), (8, '())()()('), (10, ')))((((())')]) == [1, 0, 1, 3]
check(min_moves_to_regular_bracket_sequence)
def checkIfExist(arr: list[int]) -> bool:
    """
    Given an array arr of integers, check if there exists two integers N and M such that N is the double of M ( i.e. N = 2 * M).
    More formally check if there exists two indices i and j such that :
    
    i != j
    0 <= i, j < arr.length
    arr[i] == 2 * arr[j]
    
    
    Example 1:
    Input: arr = [10,2,5,3]
    Output: true
    Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5.
    
    Example 2:
    Input: arr = [7,1,14,11]
    Output: true
    Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7.
    
    Example 3:
    Input: arr = [3,1,7,11]
    Output: false
    Explanation: In this case does not exist N and M, such that N = 2 * M.
    
    
    Constraints:
    
    2 <= arr.length <= 500
    -10^3 <= arr[i] <= 10^3
    """


# Tests

def check(candidate):
    assert candidate([10, 2, 5, 3]) == True
    assert candidate([7, 1, 14, 11]) == True
    assert candidate([3, 1, 7, 11]) == False
check(checkIfExist)
def min_num_subsequences(t: int, test_cases: list[tuple[int, str]]) -> list[tuple[int, list[int]]]:
    """
    You are given a binary string $s$ consisting of $n$ zeros and ones.
    
    Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
    
    Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
    
    You have to answer $t$ independent test cases.
    
    
    -----Input-----
    
    The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — 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 2 \cdot 10^5$) — the length of $s$. The second line of the test case contains $n$ characters '0' and '1' — the string $s$.
    
    It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
    
    
    -----Output-----
    
    For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) — the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
    
    If there are several answers, you can print any.
    
    
    -----Example-----
    Input
    4
    4
    0011
    6
    111111
    5
    10101
    8
    01010000
    
    Output
    2
    1 2 2 1
    6
    1 2 3 4 5 6
    1
    1 1 1 1 1
    4
    1 1 1 1 1 2 3 4
    """


# Tests

def check(candidate):
    assert candidate(4, [(4, '0011'), (6, '111111'), (5, '10101'), (8, '01010000')]) == [(2, [1, 2, 2, 1]), (6, [1, 2, 3, 4, 5, 6]), (1, [1, 1, 1, 1, 1]), (4, [1, 1, 1, 1, 1, 2, 3, 4])]
check(min_num_subsequences)
def make_upper_case(s: str) -> str:
    """
    Write a function which converts the input string to uppercase.
    
    ~~~if:bf
    For BF all inputs end with \0, all inputs are lowercases and there is no space between.
    ~~~
    """


# Tests

def check(candidate):
    assert candidate('hello') == 'HELLO'
    assert candidate('hello world') == 'HELLO WORLD'
    assert candidate('hello world !') == 'HELLO WORLD !'
    assert candidate('heLlO wORLd !') == 'HELLO WORLD !'
    assert candidate('1,2,3 hello world!') == '1,2,3 HELLO WORLD!'
check(make_upper_case)