NazmusAshrafi/apps_probs huggingface dataset:
def shortest_subsequence_length(T: int, test_cases: List[Tuple[int, int, List[int]]]) -> List[int]:
    """
    You are given a sequence of n integers a1, a2, ..., an and an integer d.
    Find the length of the shortest non-empty contiguous subsequence with sum of elements at least d. Formally, you should find the smallest positive integer k with the following property: there is an integer s (1 ≤ s ≤ N-k+1) such that as + as+1 + ... + as+k-1 ≥ d.
    
    -----Input-----
    
    - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
    - The first line of each test case contains two space-separated integers n and d.
    - The second line contains n space-separated integers a1, a2, ..., an.
    
    -----Output-----
    For each test case, print a single line containing one integer — the length of the shortest contiguous subsequence with sum of elements ≥ d. If there is no such subsequence, print -1 instead.
    
    -----Constraints-----
    - 1 ≤ T ≤ 105
    - 1 ≤ n ≤ 105
    - -109 ≤ d ≤ 109
    - -104 ≤ ai ≤ 104
    - 1 ≤ sum of n over all test cases ≤ 2 · 105
    
    -----Example-----
    Input:
    
    2
    5 5
    1 2 3 1 -5
    5 1
    1 2 3 1 -5
    
    Output:
    
    2
    1
    """


# Tests

def check(candidate):
    assert candidate(2, [(5, 5, [1, 2, 3, 1, -5]), (5, 1, [1, 2, 3, 1, -5])]) == [2, 1]
check(shortest_subsequence_length)
def find_path_costs(N: int, parents: List[int], values: List[int]) -> List[int]:
    """
    You are given a rooted tree on N vertices. The nodes are numbered from 1 to N, and Node 1 is the root. Each node u has an associated value attached to it: Au.
    For each vertex v, we consider the path going upwards from v to the root. Suppose that path is v1, v2, .., vk, where v1 = v and vk = 1. The cost of any node on this path is equal to the minimum value among all the nodes to its left in the path sequence, including itself. That is, cost(vi) = min1 <= j <= i{Avj}. And the cost of the path is the sum of costs of all the nodes in it.
    For every node in the tree, find the cost of the path from that node to the root.
    
    -----Input-----
    - The first line of the input contains a single integer, N, denoting the number of nodes in the tree.
    - The next line contains N-1 integers, the i-th of which denotes the parent of node i+1.
    - The next line contains N integers, the i-th of which denotes Ai.
    
    -----Output-----
    Output a single line containing N integers, the i-th of which should be the cost of the path from node i to the root.
    
    -----Constraints-----
    - 1 ≤ N ≤ 100,000
    - -1,000,000,000 ≤ Av ≤ 1,000,000,000
    
    -----Subtasks-----
    - Subtask #1 (30 points): 1 ≤ N ≤ 2000
    - Subtask #2 (70 points): Original constraints.
    
    -----Example-----
    Input:
    8
    1 1 1 1 5 8 6
    1 2 3 4 5 15 70 10
    
    Output:
    1 3 4 5 6 21 96 26
    
    -----Explanation-----
    For example, take a look at the path from vertex 7: The path is 7 -> 8 -> 6 -> 5 -> 1.
    Cost(7) has no choice but to be A7. So Cost(7) = 70.
    Cost(8) will be minimum of A7 and A8, which turns out to be A8. So Cost(8) = 10.
    Cost(6) = minimum {A7, A8, A6} = minimum {70, 10, 15} = 10.
    Cost(5) = minimum {70, 10, 15, 5} = 5.
    Cost(1) = minimum {70, 10, 15, 5, 1} = 1.
    So, the cost of the path from 7 to the root is 70 + 10 + 10 + 5 + 1 = 96.
    """


# Tests

def check(candidate):
    assert candidate(8, [1, 1, 1, 1, 5, 8, 6], [1, 2, 3, 4, 5, 15, 70, 10]) == [1, 3, 4, 5, 6, 21, 96, 26]
check(find_path_costs)
def min_lies(t: int, test_cases: List[Tuple[int, List[Tuple[str, int, str]]]]) -> List[int]:
    """
    Alice and Johnny are playing a simple guessing game. Johnny picks an arbitrary positive integer n (1<=n<=109) and gives Alice exactly k hints about the value of n. It is Alice's task to guess n, based on the received hints.
    
    Alice often has a serious problem guessing the value of n, and she's beginning to suspect that Johnny occasionally cheats, that is, gives her incorrect hints.
    After the last game, they had the following little conversation:
    
    - [Alice] Johnny, you keep cheating!
    
    - [Johnny] Indeed? You cannot prove it.
    
    - [Alice] Oh yes I can. In fact, I can tell you with the utmost certainty that in the last game you lied to me at least *** times.
    
    So, how many times at least did Johnny lie to Alice? Try to determine this, knowing only the hints Johnny gave to Alice.
    
    -----Input-----
    The first line of input contains t, the number of test cases (about 20). Exactly t test cases follow.
    
    Each test case starts with a line containing a single integer k, denoting the number of hints given by Johnny (1<=k<=100000). Each of the next k lines contains exactly one hint. The i-th hint is of the form:
    operator li logical_value
    where operator denotes one of the symbols < , > , or =; li is an integer (1<=li<=109), while logical_value is one of the words: Yes or No. The hint is considered correct if logical_value is the correct reply to the question: "Does the relation: n operator li hold?", and is considered to be false (a lie) otherwise.
    
    -----Output-----
    For each test case output a line containing a single integer, equal to the minimal possible number of Johnny's lies during the game.
    
    -----Example-----
    Input:
    3
    2
    < 100 No
    > 100 No
    3
    < 2 Yes
    > 4 Yes
    = 3 No
    6
    < 2 Yes
    > 1 Yes
    = 1 Yes
    = 1 Yes
    > 1 Yes
    = 1 Yes
    
    Output:
    0
    1
    2
    
    Explanation: for the respective test cases, the number picked by Johnny could have been e.g. n=100, n=5, and n=1.
    """


# Tests

def check(candidate):
    assert candidate(3, [(2, [('<', 100, 'No'), ('>', 100, 'No')]), (3, [('<', 2, 'Yes'), ('>', 4, 'Yes'), ('=', 3, 'No')]), (6, [('<', 2, 'Yes'), ('>', 1, 'Yes'), ('=', 1, 'Yes'), ('=', 1, 'Yes'), ('>', 1, 'Yes'), ('=', 1, 'Yes')])]) == [0, 1, 2]
check(min_lies)
def min_string_length(T: int, test_cases: List[str]) -> List[int]:
    """
    Given a string $s$.
    You can perform the following operation on given string any number of time.
    Delete two successive elements of the string if they are same.
    After performing the above operation you have to return the least possible length of the string.
    
    -----Input:-----
    - First line will contain $T$, number of testcases. Then the testcases follow.
    - Each testcase contains of a single line of input, a string $s$.
    
    -----Output:-----
    For each testcase, output in a single line answer- minimum length of string possible after performing given operations.
    
    -----Constraints-----
    - $1 \leq T \leq 1000$
    - $2 \leq length of string \leq 10^5$
    $s$ contains only lowercase letters.
    
    -----Sample Input:-----
    3
    abccd
    abbac
    aaaa
    
    -----Sample Output:-----
    3
    1
    0
    
    -----EXPLANATION:-----
    - In first case, $"abd"$ will be final string.
    - in second case, $"c"$ will be final string
    """


# Tests

def check(candidate):
    assert candidate(3, ['abccd', 'abbac', 'aaaa']) == [3, 1, 0]
check(min_string_length)
def generate_tiles(T: int, cases: List[int]) -> List[str]:
    """
    Chef Tobby asked Bhuvan to brush up his knowledge of statistics for a test. While studying some distributions, Bhuvan learns the fact that for symmetric distributions, the mean and the median are always the same.
    Chef Tobby asks Bhuvan out for a game and tells him that it will utilize his new found knowledge. He lays out a total of 109 small tiles in front of Bhuvan. Each tile has a distinct number written on it from 1 to 109.
    Chef Tobby gives Bhuvan an integer N and asks him to choose N distinct tiles and arrange them in a line such that the mean of median of all subarrays lies between [N-1, N+1], both inclusive. The median of subarray of even length is the mean of the two numbers in the middle after the subarray is sorted
    Bhuvan realizes that his book didn’t teach him how to solve this and asks for your help. Can you solve the problem for him?
    In case, no solution exists, print -1.
    
    -----Input section-----
    First line contains, T, denoting the number of test cases.
    Each of the next T lines, contain a single integer N.
    
    -----Output section-----
    If no solution, exists print -1.
    If the solution exists, output N space separated integers denoting the elements of the array A such that above conditions are satisfied. In case, multiple answers exist, you can output any one them.
    
    -----Input constraints-----
    1 ≤ T ≤ 20
    1 ≤ N ≤ 100
    
    -----Sample Input-----
    3
    1
    2
    3
    
    -----Sample Output-----
    1
    1 2
    1 2 3
    
    -----Explanation-----
    For test case 3, the subarrays and their median are as follows:
    - {1}, median = 1
    - {2}, median = 2
    - {3}, median = 3
    - {1, 2}, median = 1.5
    - {2, 3}, median = 2.5
    - {1, 2, 3}, median = 2
    The mean of the medians is 2 which lies in the range [2, 4]
    """


# Tests

def check(candidate):
    assert candidate(3, [1, 2, 3]) == ['1', '1 2', '1 2 3']
check(generate_tiles)
def shortest_average_path(T: int, test_cases: List[Tuple[int, int, List[Tuple[int, int, int]], Tuple[int, int]]]) -> List[float]:
    """
    There are a lot of problems related to the shortest paths. Nevertheless, there are not much problems, related to the shortest paths in average.
    Consider a directed graph G, consisting of N nodes and M edges. Consider a walk from the node A to the node B in this graph. The average length of this walk will be total sum of weight of its' edges divided by number of edges. Every edge counts as many times as it appears in this path.
    Now, your problem is quite simple. For the given graph and two given nodes, find out the shortest average length of the walk between these nodes. Please note, that the length of the walk need not to be finite, but average walk length will be.
    
    -----Input-----
    The 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 a pair of space-separated integers N and M denoting the number of nodes and the number of edges in the graph.
    Each of the following M lines contains a triple of space-separated integers Xi Yi Zi, denoting the arc, connecting the node Xi to the node Yi (but not vice-versa!) having the weight of Zi.
    The next line contains a pair of space separated integers A and B, denoting the first and the last node of the path.
    
    -----Output-----
    For each test case, output a single line containing the length of the shortest path in average.
    If there is no path at all, output just -1 on the corresponding line of the output.
    
    -----Constraints-----
    - 1 ≤ N ≤ 500
    - 1 ≤ M ≤ 1000
    - A is not equal to B
    - 1 ≤ A, B, Xi, Yi ≤ N
    - 1 ≤ Zi ≤ 100
    - There are no self-loops and multiple edges in the graph.
    - 1 ≤ sum of N over all test cases ≤ 10000
    - 1 ≤ sum of M over all test cases ≤ 20000
    
    -----Subtasks-----
    - Subtask #1 (45 points): 1 ≤ N ≤ 10, 1 ≤ M ≤ 20; Your answer will be considered correct in case it has an absolute or relative error of no more than 10-2.
    - Subtask #2 (55 points): no additional constraints; Your answer will be considered correct in case it has an absolute or relative error of no more than 10-6.
    
    -----Example-----
    Input:2
    3 3
    1 2 1
    2 3 2
    3 2 3
    1 3
    3 3
    1 2 10
    2 3 1
    3 2 1
    1 3
    
    Output:1.5
    1.0
    
    -----Explanation-----
    Example case 1. The walk 1 -> 2 and 2 -> 3 has average length of 3/2 = 1.5. Any other walks in the graph will have more or equal average length than this.
    """


# Tests

def check(candidate):
    assert candidate(2, [(3, 3, [(1, 2, 1), (2, 3, 2), (3, 2, 3)], (1, 3)), (3, 3, [(1, 2, 10), (2, 3, 1), (3, 2, 1)], (1, 3))]) == [1.5, 1.0]
check(shortest_average_path)
def check_reducible_anagrams(S: str, T: str) -> bool:
    """
    Given two strings S and T, each of which represents a non-negative rational number, return True if and only if they represent the same number. The strings may use parentheses to denote the repeating part of the rational number.
    In general a rational number can be represented using up to three parts: an integer part, a non-repeating part, and a repeating part. The number will be represented in one of the following three ways:
    
    <IntegerPart> (e.g. 0, 12, 123)
    <IntegerPart><.><NonRepeatingPart>  (e.g. 0.5, 1., 2.12, 2.0001)
    <IntegerPart><.><NonRepeatingPart><(><RepeatingPart><)> (e.g. 0.1(6), 0.9(9), 0.00(1212))
    
    The repeating portion of a decimal expansion is conventionally denoted within a pair of round brackets.  For example:
    1 / 6 = 0.16666666... = 0.1(6) = 0.1666(6) = 0.166(66)
    Both 0.1(6) or 0.1666(6) or 0.166(66) are correct representations of 1 / 6.
    
    Example 1:
    Input: S = "0.(52)", T = "0.5(25)"
    Output: true
    Explanation:
    Because "0.(52)" represents 0.52525252..., and "0.5(25)" represents 0.52525252525..... , the strings represent the same number.
    
    
    Example 2:
    Input: S = "0.1666(6)", T = "0.166(66)"
    Output: true
    
    
    Example 3:
    Input: S = "0.9(9)", T = "1."
    Output: true
    Explanation:
    "0.9(9)" represents 0.999999999... repeated forever, which equals 1.  [See this link for an explanation.]
    "1." represents the number 1, which is formed correctly: (IntegerPart) = "1" and (NonRepeatingPart) = "".
    
    
    
    Note:
    
    Each part consists only of digits.
    The <IntegerPart> will not begin with 2 or more zeros.  (There is no other restriction on the digits of each part.)
    1 <= <IntegerPart>.length <= 4
    0 <= <NonRepeatingPart>.length <= 4
    1 <= <RepeatingPart>.length <= 4
    """


# Tests

def check(candidate):
    assert candidate("0.(52)", "0.5(25)") == True
    assert candidate("0.1666(6)", "0.166(66)") == True
    assert candidate("0.9(9)", "1.") == True
check(check_reducible_anagrams)
def check_reducible_anagrams(T: int, N: List[int]) -> List[str]:
    """
    Problem description.
    Winston and Royce love sharing memes with each other. They express the amount of seconds they laughed ar a meme as the number of ‘XD’ subsequences in their messages. Being optimization freaks, they wanted to find the string with minimum possible length and having exactly the given number of ‘XD’ subsequences.
    
    -----Input-----
    - The first line of the input contains an integer T denoting the number of test cases.
    - Next T lines contains a single integer N, the no of seconds laughed.
    
    -----Output-----
    -
    For each input, print the corresponding string having minimum length. If there are multiple possible answers, print any.
    
    -----Constraints-----
    - 1 ≤ T ≤ 1000
    - 1 ≤ N ≤ 109
    - 1 ≤ Sum of length of output over all testcases ≤ 5*105
    
    -----Example-----
    Input:
    1
    9
    
    Output:
    XXXDDD
    
    -----Explanation-----
    Some of the possible strings are - XXDDDXD,XXXDDD,XDXXXDD,XDXDXDD etc. Of these, XXXDDD is the smallest.
    """


# Tests

def check(candidate):
    assert candidate(1, [9]) == ['XXXDDD']
check(check_reducible_anagrams)
def min_cost(grid: List[List[int]]) -> int:
    """
    Given a m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of grid[i][j] can be:
    
    1 which means go to the cell to the right. (i.e go from grid[i][j] to grid[i][j + 1])
    2 which means go to the cell to the left. (i.e go from grid[i][j] to grid[i][j - 1])
    3 which means go to the lower cell. (i.e go from grid[i][j] to grid[i + 1][j])
    4 which means go to the upper cell. (i.e go from grid[i][j] to grid[i - 1][j])
    
    Notice that there could be some invalid signs on the cells of the grid which points outside the grid.
    You will initially start at the upper left cell (0,0). A valid path in the grid is a path which starts from the upper left cell (0,0) and ends at the bottom-right cell (m - 1, n - 1) following the signs on the grid. The valid path doesn't have to be the shortest.
    You can modify the sign on a cell with cost = 1. You can modify the sign on a cell one time only.
    Return the minimum cost to make the grid have at least one valid path.
    
    Example 1:
    
    Input: grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
    Output: 3
    Explanation: You will start at point (0, 0).
    The path to (3, 3) is as follows. (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) change the arrow to down with cost = 1 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) change the arrow to down with cost = 1 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) change the arrow to down with cost = 1 --> (3, 3)
    The total cost = 3.
    
    Example 2:
    
    Input: grid = [[1,1,3],[3,2,2],[1,1,4]]
    Output: 0
    Explanation: You can follow the path from (0, 0) to (2, 2).
    
    Example 3:
    
    Input: grid = [[1,2],[4,3]]
    Output: 1
    
    Example 4:
    Input: grid = [[2,2,2],[2,2,2]]
    Output: 3
    
    Example 5:
    Input: grid = [[4]]
    Output: 0
    
    
    Constraints:
    
    m == grid.length
    n == grid[i].length
    1 <= m, n <= 100
    """


# Tests

def check(candidate):
    assert candidate([[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]) == 3
    assert candidate([[1,1,3],[3,2,2],[1,1,4]]) == 0
    assert candidate([[1,2],[4,3]]) == 1
    assert candidate([[2,2,2],[2,2,2]]) == 3
    assert candidate([[4]]) == 0
check(min_cost)
def find_A_from_B(T: int, test_cases: List[Tuple[int, int, int, List[List[List[int]]]]]) -> List[List[List[int]]]:
    """
    Suppose there is a X x Y x Z 3D matrix A of numbers having coordinates (i, j, k) where 0 ≤ i < X, 0 ≤ j < Y, 0 ≤ k < Z. Now another X x Y x Z matrix B is defined from A such that the (i, j, k) element of B is the sum of all the the numbers in A in the cuboid defined by the (0, 0, 0) and (i, j, k) elements as the diagonally opposite vertices. In other word (i, j, k) in B is the sum of numbers of A having coordinates (a, b, c) such that 0 ≤ a ≤ i, 0 ≤ b ≤ j, 0 ≤ c ≤ k. The problem is that given B, you have to find out A.
    
    -----Input-----
    The first line of input will contain the number of test cases ( ≤ 10). That many test cases will follow in subsequent lines. The first line of each test case will contain the numbers X Y Z (0 ≤ X, Y, Z ≤ 100). After that there will be X x Y lines each containing Z numbers of B. The first line contains the numbers (0, 0, 0), (0, 0, 1)..., (0, 0, Z-1). The second line has the numbers (0, 1, 0), (0, 1, 1)..., (0, 1, Z-1) and so on. The (Y+1)th line will have the numbers (1, 0, 0), (1, 0, 1)..., (1, 0, Z-1) and so on.
    
    -----Output-----
    For each test case print the numbers of A in exactly the same fashion as the input.
    
    -----Example-----
    Input:
    2
    3 1 1
    1
    8
    22
    1 2 3
    0 9 13
    18 45 51
    
    Output:
    1
    7
    14
    0 9 4
    18 18 2
    """


# Tests

def check(candidate):
    assert candidate(2, [(3, 1, 1, [[[1]], [[8]], [[22]]]), (1, 2, 3, [[[0, 9, 13], [18, 45, 51]]])]) == [[[1], [7], [14]], [[0, 9, 4], [18, 18, 2]]]
check(find_A_from_B)
def arrange_buildings(T: int, test_cases: List[Tuple[int, int]]) -> List[str]:
    """
    Problem Statement:Captain America and Iron Man are at WAR and the rage inside Iron Man is rising.
    
    But Iron Man faces a problem to identify the location of Captain America.
    
    There are N buildings situtaed adjacently to each other and Captain America can be at any building.
    
    Iron Man has to arrange the Buildings from 1 to N is such a way that Value(i.e abs(Building Number -Position of Building))=K for every building.
    
    Can You help Iron Man to Find The Arrangement of the Buildings?
    
    P.S- If no arrangement exist, then print
    “CAPTAIN AMERICA EVADES”.
    
    Input Format:
    The first line of input contains a single integer,T, denoting the number of test cases.
    
    Each of the T  subsequent lines contains 2 space-separated integers describing the respective N and K values for a test case.
    
    Output Format:
    On a new line for each test case,
    
    Print the lexicographically smallest arrangement;
    
    If no absolute arrangement exists, print “CAPTAIN AMERICA EVADES”.
    
    Constraints:
    SubTask#1
    1<=T<=10
    
    1<=N<=10^5
    
    0<=K<=N
    
    SubTask#2
    Original Constraints..
    
    SubTask#3
    Original Constraints..
    
    Sample Input:
    3
    
    2 1
    
    3 0
    
    3 2
    
    Sample Output:
    
    2 1
    
    1 2 3
    
    CAPTAIN AMERICA EVADES
    
    Explanation:
    Case 1:
    
    N=2 and K=1
    
    Therefore the arrangement is [2,1].
    
    Case 2:
    
    N=3 and K=0
    
    Therefore arrangement is [1,2,3].
    """


# Tests

def check(candidate):
    assert candidate(3, [(2, 1), (3, 0), (3, 2)]) == ['2 1', '1 2 3', 'CAPTAIN AMERICA EVADES']
check(arrange_buildings)
def min_cuts_sky_scrapers(n: int, heights: List[int]) -> int:
    """
    In a fictitious city of CODASLAM there were many skyscrapers. The mayor of the city decided to make the city beautiful and for this he decided to arrange the skyscrapers in descending order of their height, and the order must be strictly decreasing but he also didn’t want to waste much money so he decided to get the minimum cuts possible. Your job is to output the minimum value of cut that is possible to arrange the skyscrapers in descending order.
    
    -----Input-----
    
    *First line of input is the number of sky-scrappers in the city
    *Second line of input is the height of the respective sky-scrappers
    
    
    -----Output-----
    
    * Your output should be the minimum value of cut required to arrange these sky-scrappers in descending order.
    
    -----Example-----
    Input:
    5
    1 2 3 4 5
    
    Output:
    8
    
    By:
    Chintan,Asad,Ashayam,Akanksha
    """


# Tests

def check(candidate):
    assert candidate(5, [1, 2, 3, 4, 5]) == 8
check(min_cuts_sky_scrapers)
def sum_of_digits_of_powers(T: int, numbers: List[int]) -> List[int]:
    """
    The chef won a duet singing award at Techsurge & Mridang 2012. From that time he is obsessed with the number 2.
    
    He just started calculating the powers of two. And adding the digits of the results.
    
    But he got puzzled after a few calculations. So gave you the job to generate the solutions to 2^n and find their sum of digits.
    
    -----Input-----
    N : number of inputs N<=100
    
    then N lines with input T<=2000
    
    -----Output-----
    The output for the corresponding input T
    
    -----Example-----
    Input:
    3
    5
    10
    4
    
    Output:
    5
    7
    7
    
    Explanation:
    2^5=32
    3+2=5
    2^10=1024
    1+0+2+4=7
    2^4=16
    1+6=7
    """


# Tests

def check(candidate):
    assert candidate(3, [5, 10, 4]) == [5, 7, 7]
check(sum_of_digits_of_powers)