task_type
stringclasses 1
value | problem
stringlengths 209
3.39k
| answer
stringlengths 35
6.15k
| problem_tokens
int64 60
774
| answer_tokens
int64 12
2.04k
|
|---|---|---|---|---|
coding
|
Solve the programming task below in a Python markdown code block.
You are given two sorted arrays that contain only integers. Your task is to find a way to merge them into a single one, sorted in **ascending order**. Complete the function `mergeArrays(arr1, arr2)`, where `arr1` and `arr2` are the original sorted arrays.
You don't need to worry about validation, since `arr1` and `arr2` must be arrays with 0 or more Integers. If both `arr1` and `arr2` are empty, then just return an empty array.
**Note:** `arr1` and `arr2` may be sorted in different orders. Also `arr1` and `arr2` may have same integers. Remove duplicated in the returned result.
## Examples
Happy coding!
Also feel free to reuse/extend the following starter code:
```python
def merge_arrays(arr1, arr2):
```
|
{"functional": "_inputs = [[[1, 2, 3, 4], [5, 6, 7, 8]], [[10, 8, 6, 4, 2], [9, 7, 5, 3, 1]], [[-20, 35, 36, 37, 39, 40], [-10, -5, 0, 6, 7, 8, 9, 10, 25, 38, 50, 62]], [[5, 6, 7, 8, 9, 10], [20, 18, 15, 14, 13, 12, 11, 4, 3, 2]], [[45, 30, 20, 15, 12, 5], [9, 10, 18, 25, 35, 50]], [[-8, -3, -2, 4, 5, 6, 7, 15, 42, 90, 134], [216, 102, 74, 32, 8, 2, 0, -9, -13]], [[-100, -27, -8, 5, 23, 56, 124, 325], [-34, -27, 6, 12, 25, 56, 213, 325, 601]], [[18, 7, 2, 0, -22, -46, -103, -293], [-300, -293, -46, -31, -5, 0, 18, 19, 74, 231]], [[105, 73, -4, -73, -201], [-201, -73, -4, 73, 105]]]\n_outputs = [[[1, 2, 3, 4, 5, 6, 7, 8]], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], [[-20, -10, -5, 0, 6, 7, 8, 9, 10, 25, 35, 36, 37, 38, 39, 40, 50, 62]], [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 20]], [[5, 9, 10, 12, 15, 18, 20, 25, 30, 35, 45, 50]], [[-13, -9, -8, -3, -2, 0, 2, 4, 5, 6, 7, 8, 15, 32, 42, 74, 90, 102, 134, 216]], [[-100, -34, -27, -8, 5, 6, 12, 23, 25, 56, 124, 213, 325, 601]], [[-300, -293, -103, -46, -31, -22, -5, 0, 2, 7, 18, 19, 74, 231]], [[-201, -73, -4, 73, 105]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(merge_arrays(*i), o[0])"}
| 198
| 1,067
|
coding
|
Solve the programming task below in a Python markdown code block.
Ania has a large integer $S$. Its decimal representation has length $n$ and doesn't contain any leading zeroes. Ania is allowed to change at most $k$ digits of $S$. She wants to do it in such a way that $S$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
-----Input-----
The first line contains two integers $n$ and $k$ ($1 \leq n \leq 200\,000$, $0 \leq k \leq n$) — the number of digits in the decimal representation of $S$ and the maximum allowed number of changed digits.
The second line contains the integer $S$. It's guaranteed that $S$ has exactly $n$ digits and doesn't contain any leading zeroes.
-----Output-----
Output the minimal possible value of $S$ which Ania can end with. Note that the resulting integer should also have $n$ digits.
-----Examples-----
Input
5 3
51528
Output
10028
Input
3 2
102
Output
100
Input
1 1
1
Output
0
-----Note-----
A number has leading zeroes if it consists of at least two digits and its first digit is $0$. For example, numbers $00$, $00069$ and $0101$ have leading zeroes, while $0$, $3000$ and $1010$ don't have leading zeroes.
|
{"inputs": ["1 1\n1\n", "1 0\n1\n", "1 1\n0\n", "1 0\n0\n", "1 0\n7\n", "1 1\n9\n", "1 1\n5\n", "1 1\n2\n"], "outputs": ["0\n", "1\n", "0\n", "0\n", "7\n", "0\n", "0\n", "0\n"]}
| 344
| 102
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
There is a bag that consists of items, each item has a number 1, 0, or -1 written on it.
You are given four non-negative integers numOnes, numZeros, numNegOnes, and k.
The bag initially contains:
numOnes items with 1s written on them.
numZeroes items with 0s written on them.
numNegOnes items with -1s written on them.
We want to pick exactly k items among the available items. Return the maximum possible sum of numbers written on the items.
Please complete the following python code precisely:
```python
class Solution:
def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2) == 2\n assert candidate(numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4) == 3\n\n\ncheck(Solution().kItemsWithMaximumSum)"}
| 186
| 90
|
coding
|
Solve the programming task below in a Python markdown code block.
Given are integers S and P.
Is there a pair of positive integers (N,M) such that N + M = S and N \times M = P?
-----Constraints-----
- All values in input are integers.
- 1 \leq S,P \leq 10^{12}
-----Input-----
Input is given from Standard Input in the following format:
S P
-----Output-----
If there is a pair of positive integers (N,M) such that N + M = S and N \times M = P, print Yes; otherwise, print No.
-----Sample Input-----
3 2
-----Sample Output-----
Yes
- For example, we have N+M=3 and N \times M =2 for N=1,M=2.
|
{"inputs": ["3 2\n", "2 1\n", "1060 67456\n", "1000000000000 1\n", "7449088 3336990720\n", "6064022 70812167400\n", "2000000 1000000000000\n", "3059787769 226424289430\n"], "outputs": ["Yes\n", "Yes\n", "Yes\n", "No\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n"]}
| 170
| 174
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two strings s1 and s2, both of length n, consisting of lowercase English letters.
You can apply the following operation on any of the two strings any number of times:
Choose any two indices i and j such that i < j and the difference j - i is even, then swap the two characters at those indices in the string.
Return true if you can make the strings s1 and s2 equal, and false otherwise.
Please complete the following python code precisely:
```python
class Solution:
def checkStrings(self, s1: str, s2: str) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(s1 = \"abcdba\", s2 = \"cabdab\") == True\n assert candidate(s1 = \"abe\", s2 = \"bea\") == False\n\n\ncheck(Solution().checkStrings)"}
| 145
| 59
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
You are given two integer arrays A and B each of size N. Let us define interaction of arrays A and B to be the sum of A[i] * B[i] for each i from 1 to N.
You want to maximize the value of interaction of the arrays. You are allowed to make at most K (possibly zero) operations of following kind.
In a single operation, you can increase or decrease any of the elements of array A by 1.
Find out the maximum value of interaction of the arrays that you can get.
------ Input ------
The first line of input contains a single integer T denoting number of test cases.
For each test case:
First line contains two space separated integers N, K.
Second line contains N space separated integers denoting array A.
Third line contains N space separated integers denoting array B.
------ Output ------
For each test case, output a single integer denoting the answer of the problem.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$0 ≤ |A[i]|, |B[i]| ≤ 10^{5}$
$0 ≤ K ≤ 10^{9}$
------ Subtasks ------
Subtask #1 : (25 points)
$1 ≤ N ≤ 10$
$0 ≤ |A[i]|, |B[i]| ≤ 10$
$0 ≤ K ≤ 10$
Subtask #2 : (35 points)
$1 ≤ N ≤ 1000$
$0 ≤ |A[i]|, |B[i]| ≤ 1000$
$0 ≤ K ≤ 10^{5}$
Subtask #3 : (40 points)
$No additional constraints$
----- Sample Input 1 ------
2
2 2
1 2
-2 3
3 5
1 2 -3
-2 3 -5
----- Sample Output 1 ------
10
44
----- explanation 1 ------
In the first example,
you can increase value A[2] using two two operations. Now, A would be [1, 4]. The value of interaction will be 1 * -2 + 4 * 3 = -2 + 12 = 10.
|
{"inputs": ["2\n2 2\n2 1\n0 6\n3 9\n1 2 -3\n0 -1 -4", "2\n2 2\n1 2\n-4 6\n3 9\n1 2 -3\n-2 0 0", "2\n2 1\n0 2\n-3 3\n3 2\n1 2 -3\n0 3 -5", "2\n2 2\n1 2\n-2 3\n3 5\n1 2 -3\n-2 3 -5", "2\n2 2\n1 2\n-2 3\n3 2\n1 2 -3\n-2 3 -5", "2\n2 2\n0 2\n-2 3\n3 2\n1 2 -3\n-2 3 -5", "2\n2 1\n0 2\n-2 3\n3 2\n1 2 -3\n-2 3 -5", "2\n2 2\n1 2\n-2 3\n3 5\n1 1 -3\n-2 3 -5"], "outputs": ["18\n46\n", "20\n16\n", "9\n31\n", "10\n44", "10\n29\n", "12\n29\n", "9\n29\n", "10\n41\n"]}
| 517
| 328
|
coding
|
Solve the programming task below in a Python markdown code block.
There are one cat, $k$ mice, and one hole on a coordinate line. The cat is located at the point $0$, the hole is located at the point $n$. All mice are located between the cat and the hole: the $i$-th mouse is located at the point $x_i$ ($0 < x_i < n$). At each point, many mice can be located.
In one second, the following happens. First, exactly one mouse moves to the right by $1$. If the mouse reaches the hole, it hides (i.e. the mouse will not any more move to any point and will not be eaten by the cat). Then (after that the mouse has finished its move) the cat moves to the right by $1$. If at the new cat's position, some mice are located, the cat eats them (they will not be able to move after that). The actions are performed until any mouse hasn't been hidden or isn't eaten.
In other words, the first move is made by a mouse. If the mouse has reached the hole, it's saved. Then the cat makes a move. The cat eats the mice located at the pointed the cat has reached (if the cat has reached the hole, it eats nobody).
Each second, you can select a mouse that will make a move. What is the maximum number of mice that can reach the hole without being eaten?
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.
Each test case consists of two lines. The first line contains two integers $n$ and $k$ ($2 \le n \le 10^9$, $1 \le k \le 4 \cdot 10^5$). The second line contains $k$ integers $x_1, x_2, \dots x_k$ ($1 \le x_i < n$) — the initial coordinates of the mice.
It is guaranteed that the sum of all $k$ given in the input doesn't exceed $4 \cdot 10^5$.
-----Output-----
For each test case output on a separate line an integer $m$ ($m \ge 0$) — the maximum number of mice that can reach the hole without being eaten.
-----Examples-----
Input
3
10 6
8 7 5 4 9 4
2 8
1 1 1 1 1 1 1 1
12 11
1 2 3 4 5 6 7 8 9 10 11
Output
3
1
4
-----Note-----
None
|
{"inputs": ["3\n10 6\n8 7 5 4 9 4\n2 8\n1 1 1 1 1 1 1 1\n12 11\n1 2 3 4 5 6 7 8 9 10 11\n"], "outputs": ["3\n1\n4\n"]}
| 591
| 85
|
coding
|
Solve the programming task below in a Python markdown code block.
Mothers arranged a dance party for the children in school. At that party, there are only mothers and their children. All are having great fun on the dance floor when suddenly all the lights went out. It's a dark night and no one can see each other. But you were flying nearby and you can see in the dark and have ability to teleport people anywhere you want.
Legend:
-Uppercase letters stands for mothers, lowercase stand for their children, i.e. "A" mother's children are "aaaa".
-Function input: String contains only letters, uppercase letters are unique.
Task:
Place all people in alphabetical order where Mothers are followed by their children, i.e. "aAbaBb" => "AaaBbb".
Also feel free to reuse/extend the following starter code:
```python
def find_children(dancing_brigade):
```
|
{"functional": "_inputs = [['abBA'], ['AaaaaZazzz'], ['CbcBcbaA'], ['xXfuUuuF'], ['']]\n_outputs = [['AaBb'], ['AaaaaaZzzz'], ['AaBbbCcc'], ['FfUuuuXx'], ['']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_children(*i), o[0])"}
| 191
| 209
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasya learned about integer subtraction in school. He is still not very good at it, so he is only able to subtract any single digit number from any other number (which is not necessarily single digit).
For practice, Vasya chose a positive integer n$n$ and wrote it on the first line in his notepad. After that, on the second line he wrote the result of subtraction of the first digit of n$n$ from itself. For example, if n=968$n = 968$, then the second line would contain 968−9=959$968 - 9 = 959$, while with n=5$n = 5$ the second number would be 5−5=0$5 - 5 = 0$. If the second number was still positive, then Vasya would write the result of the same operation on the following line, and so on. For example, if n=91$n = 91$, then the sequence of numbers Vasya would write starts as follows: 91,82,74,67,61,55,50,…$91, 82, 74, 67, 61, 55, 50, \ldots$. One can see that any such sequence eventually terminates with the number 0$0$.
Since then, Vasya lost his notepad. However, he remembered the total number k$k$ of integers he wrote down (including the first number n$n$ and the final number 0$0$). What was the largest possible value of n$n$ Vasya could have started with?
-----Input:-----
The first line contains T$T$ , number of test cases per file.
The only line in each testcase contains a single integer k−$k-$ the total number of integers in Vasya's notepad (2≤k≤1012$2 \leq k \leq 10^{12}$).
-----Output:-----
Print a single integer−$-$ the largest possible value of the starting number n$n$. It is guaranteed that at least one such number n$n$ exists, and the largest possible value is finite.
-----Constraints-----
- 1≤T≤34$1 \leq T \leq 34 $
- 2≤k≤1012$2 \leq k \leq 10^{12}$
-----Sample Input-----
3
2
3
100
-----Sample Output:-----
9
10
170
|
{"inputs": ["3\n2\n3\n100"], "outputs": ["9\n10\n170"]}
| 566
| 27
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer array nums, partition it into two (contiguous) subarrays left and right so that:
Every element in left is less than or equal to every element in right.
left and right are non-empty.
left has the smallest possible size.
Return the length of left after such a partitioning.
Test cases are generated such that partitioning exists.
Please complete the following python code precisely:
```python
class Solution:
def partitionDisjoint(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [5,0,3,8,6]) == 3\n assert candidate(nums = [1,1,1,0,6,12]) == 4\n\n\ncheck(Solution().partitionDisjoint)"}
| 121
| 63
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.
Please complete the following python code precisely:
```python
class Solution:
def judgeSquareSum(self, c: int) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(c = 5) == True\n assert candidate(c = 3) == False\n\n\ncheck(Solution().judgeSquareSum)"}
| 74
| 42
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given two strings $s$ and $t$ both of length $2$ and both consisting only of characters 'a', 'b' and 'c'.
Possible examples of strings $s$ and $t$: "ab", "ca", "bb".
You have to find a string $res$ consisting of $3n$ characters, $n$ characters should be 'a', $n$ characters should be 'b' and $n$ characters should be 'c' and $s$ and $t$ should not occur in $res$ as substrings.
A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc".
If there are multiple answers, you can print any of them.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 10^5$) — the number of characters 'a', 'b' and 'c' in the resulting string.
The second line of the input contains one string $s$ of length $2$ consisting of characters 'a', 'b' and 'c'.
The third line of the input contains one string $t$ of length $2$ consisting of characters 'a', 'b' and 'c'.
-----Output-----
If it is impossible to find the suitable string, print "NO" on the first line.
Otherwise print "YES" on the first line and string $res$ on the second line. $res$ should consist of $3n$ characters, $n$ characters should be 'a', $n$ characters should be 'b' and $n$ characters should be 'c' and $s$ and $t$ should not occur in $res$ as substrings.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2
ab
bc
Output
YES
acbbac
Input
3
aa
bc
Output
YES
cacbacbab
Input
1
cb
ac
Output
YES
abc
|
{"inputs": ["2\nab\nbc\n", "3\naa\nbc\n", "1\ncb\nac\n", "1\nab\ncb\n", "3\nbb\ncb\n", "4\naa\nbb\n", "4\naa\nbc\n", "4\nbc\nca\n"], "outputs": ["YES\nacbacb\n", "YES\nacbacbacb\n", "YES\nabc\n", "YES\nbac\n", "YES\nabcabcabc\n", "YES\nabcabcabcabc\n", "YES\nacbacbacbacb\n", "YES\nacbacbacbacb\n"]}
| 478
| 139
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed integer array nums.
We say that an integer x is expressible from nums if there exist some integers 0 <= index1 < index2 < ... < indexk < nums.length for which nums[index1] | nums[index2] | ... | nums[indexk] = x. In other words, an integer is expressible if it can be written as the bitwise OR of some subsequence of nums.
Return the minimum positive non-zero integer that is not expressible from nums.
Please complete the following python code precisely:
```python
class Solution:
def minImpossibleOR(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [2,1]) == 4\n assert candidate(nums = [5,3,2]) == 1\n\n\ncheck(Solution().minImpossibleOR)"}
| 156
| 50
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a string of length 5 called time, representing the current time on a digital clock in the format "hh:mm". The earliest possible time is "00:00" and the latest possible time is "23:59".
In the string time, the digits represented by the ? symbol are unknown, and must be replaced with a digit from 0 to 9.
Return an integer answer, the number of valid clock times that can be created by replacing every ? with a digit from 0 to 9.
Please complete the following python code precisely:
```python
class Solution:
def countTime(self, time: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(time = \"?5:00\") == 2\n assert candidate(time = \"0?:0?\") == 100\n assert candidate(time = \"??:??\") == 1440\n\n\ncheck(Solution().countTime)"}
| 159
| 69
|
coding
|
Solve the programming task below in a Python markdown code block.
You're on your way to the market when you hear beautiful music coming from a nearby street performer. The notes come together like you wouln't believe as the musician puts together patterns of tunes. As you wonder what kind of algorithm you could use to shift octaves by 8 pitches or something silly like that, it dawns on you that you have been watching the musician for some 10 odd minutes. You ask, "How much do people normally tip for something like this?" The artist looks up. "Its always gonna be about tree fiddy."
It was then that you realize the musician was a 400 foot tall beast from the paleolithic era. The Loch Ness Monster almost tricked you!
There are only 2 guaranteed ways to tell if you are speaking to The Loch Ness Monster: A.) It is a 400 foot tall beast from the paleolithic era B.) It will ask you for tree fiddy
Since Nessie is a master of disguise, the only way accurately tell is to look for the phrase "tree fiddy". Since you are tired of being grifted by this monster, the time has come to code a solution for finding The Loch Ness Monster.
Note: It can also be written as 3.50 or three fifty.
Also feel free to reuse/extend the following starter code:
```python
def is_lock_ness_monster(string):
```
|
{"functional": "_inputs = [['Your girlscout cookies are ready to ship. Your total comes to tree fiddy'], [\"Howdy Pardner. Name's Pete Lexington. I reckon you're the kinda stiff who carries about tree fiddy?\"], [\"I'm from Scottland. I moved here to be with my family sir. Please, $3.50 would go a long way to help me find them\"], ['Yo, I heard you were on the lookout for Nessie. Let me know if you need assistance.'], ['I will absolutely, positively, never give that darn Lock Ness Monster any of my three dollars and fifty cents'], [\"Did I ever tell you about my run with that paleolithic beast? He tried all sorts of ways to get at my three dolla and fitty cent? I told him 'this is MY 4 dolla!'. He just wouldn't listen.\"], ['Hello, I come from the year 3150 to bring you good news!'], [\"By 'tree fiddy' I mean 'three fifty'\"], ['I will be at the office by 3:50 or maybe a bit earlier, but definitely not before 3, to discuss with 50 clients'], ['']]\n_outputs = [[True], [True], [True], [False], [False], [False], [False], [True], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(is_lock_ness_monster(*i), o[0])"}
| 301
| 439
|
coding
|
Solve the programming task below in a Python markdown code block.
The Padovan sequence is the sequence of integers `P(n)` defined by the initial values
`P(0)=P(1)=P(2)=1`
and the recurrence relation
`P(n)=P(n-2)+P(n-3)`
The first few values of `P(n)` are
`1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151, 200, 265, ...`
### Task
The task is to write a method that returns i-th Padovan number
```python
> padovan(0) == 1
> padovan(1) == 1
> padovan(2) == 1
> padovan(n) = padovan(n-2) + padovan(n-3)
```
Also feel free to reuse/extend the following starter code:
```python
def padovan(n):
```
|
{"functional": "_inputs = [[100]]\n_outputs = [[1177482265857]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(padovan(*i), o[0])"}
| 255
| 168
|
coding
|
Solve the programming task below in a Python markdown code block.
[3R2 as DJ Mashiro - Happiness Breeze](https://open.spotify.com/track/2qGqK8GRS65Wlf20qUBEak)
[Ice - DJ Mashiro is dead or alive](https://soundcloud.com/iceloki/dj-mashiro-is-dead-or-alive)
NEKO#ΦωΦ has just got a new maze game on her PC!
The game's main puzzle is a maze, in the forms of a 2 × n rectangle grid. NEKO's task is to lead a Nekomimi girl from cell (1, 1) to the gate at (2, n) and escape the maze. The girl can only move between cells sharing a common side.
However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.
After hours of streaming, NEKO finally figured out there are only q such moments: the i-th moment toggles the state of cell (r_i, c_i) (either from ground to lava or vice versa).
Knowing this, NEKO wonders, after each of the q moments, whether it is still possible to move from cell (1, 1) to cell (2, n) without going through any lava cells.
Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her?
Input
The first line contains integers n, q (2 ≤ n ≤ 10^5, 1 ≤ q ≤ 10^5).
The i-th of q following lines contains two integers r_i, c_i (1 ≤ r_i ≤ 2, 1 ≤ c_i ≤ n), denoting the coordinates of the cell to be flipped at the i-th moment.
It is guaranteed that cells (1, 1) and (2, n) never appear in the query list.
Output
For each moment, if it is possible to travel from cell (1, 1) to cell (2, n), print "Yes", otherwise print "No". There should be exactly q answers, one after every update.
You can print the words in any case (either lowercase, uppercase or mixed).
Example
Input
5 5
2 3
1 4
2 4
2 3
1 4
Output
Yes
No
No
No
Yes
Note
We'll crack down the example test here:
* After the first query, the girl still able to reach the goal. One of the shortest path ways should be: (1,1) → (1,2) → (1,3) → (1,4) → (1,5) → (2,5).
* After the second query, it's impossible to move to the goal, since the farthest cell she could reach is (1, 3).
* After the fourth query, the (2, 3) is not blocked, but now all the 4-th column is blocked, so she still can't reach the goal.
* After the fifth query, the column barrier has been lifted, thus she can go to the final goal again.
|
{"inputs": ["4 1\n1 4\n", "5 1\n1 4\n", "2 2\n2 1\n1 2\n", "2 4\n2 1\n1 2\n1 2\n1 2\n", "4 4\n2 1\n1 2\n1 2\n1 2\n", "4 4\n2 1\n1 2\n1 2\n2 2\n", "5 5\n2 3\n2 4\n2 4\n2 3\n1 4\n", "5 5\n2 3\n1 4\n2 4\n2 3\n1 4\n"], "outputs": ["Yes\n", "Yes\n", "Yes\nNo\n", "Yes\nNo\nYes\nNo\n", "Yes\nNo\nYes\nNo\n", "Yes\nNo\nYes\nYes\n", "Yes\nYes\nYes\nYes\nYes\n", "Yes\nNo\nNo\nNo\nYes\n"]}
| 714
| 226
|
coding
|
Solve the programming task below in a Python markdown code block.
On Unix system type files can be identified with the ls -l command which displays the type of the file in the first alphabetic letter of the file system permissions field. You can find more information about file type on Unix system on the [wikipedia page](https://en.wikipedia.org/wiki/Unix_file_types).
- '-' A regular file ==> `file`.
- 'd' A directory ==> `directory`.
- 'l' A symbolic link ==> `symlink`.
- 'c' A character special file. It refers to a device that handles data as a stream of bytes (e.g: a terminal/modem) ==> `character_file`.
- 'b' A block special file. It refers to a device that handles data in blocks (e.g: such as a hard drive or CD-ROM drive) ==> `block_file`.
- 'p' a named pipe ==> `pipe`.
- 's' a socket ==> `socket`.
- 'D' a door ==> `door`.
In this kata you should complete a function that return the `filetype` as a string regarding the `file_attribute` given by the `ls -l` command.
For example if the function receive `-rwxr-xr-x` it should return `file`.
Also feel free to reuse/extend the following starter code:
```python
def linux_type(file_attribute):
```
|
{"functional": "_inputs = [['-rwxrwxrwx'], ['Drwxr-xr-x'], ['lrwxrw-rw-'], ['srwxrwxrwx']]\n_outputs = [['file'], ['door'], ['symlink'], ['socket']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(linux_type(*i), o[0])"}
| 289
| 195
|
coding
|
Solve the programming task below in a Python markdown code block.
Your algorithms have become so good at predicting the market that you now know what the share price of Wooden Orange Toothpicks Inc. (WOT) will be for the next number of days.
Each day, you can either buy one share of WOT, sell any number of shares of WOT that you own, or not make any transaction at all. What is the maximum profit you can obtain with an optimum trading strategy?
Example
$prices=[1,2]$
Buy one share day one, and sell it day two for a profit of $1$. Return $1$.
$prices=[2,1]$
No profit can be made so you do not buy or sell stock those days. Return $0$.
Function Description
Complete the stockmax function in the editor below.
stockmax has the following parameter(s):
prices: an array of integers that represent predicted daily stock prices
Returns
int: the maximum profit achievable
Input Format
The first line contains the number of test cases $t$.
Each of the next $t$ pairs of lines contain:
- The first line contains an integer $n$, the number of predicted prices for WOT.
- The next line contains n space-separated integers $prices [i]$, each a predicted stock price for day $i$.
Constraints
$1\leq t\leq10$
$1\le n\le50000$
$1\leq prices[i]\leq10000$
Output Format
Output $t$ lines, each containing the maximum profit which can be obtained for the corresponding test case.
Sample Input
STDIN Function
----- --------
3 q = 3
3 prices[] size n = 3
5 3 2 prices = [5, 3, 2]
3 prices[] size n = 3
1 2 100 prices = [1, 2, 100]
4 prices[] size n = 4
1 3 1 2 prices =[1, 3, 1, 2]
Sample Output
0
197
3
Explanation
For the first case, there is no profit because the share price never rises, return $0$.
For the second case, buy one share on the first two days and sell both of them on the third day for a profit of $197$.
For the third case, buy one share on day 1, sell one on day 2, buy one share on day 3, and sell one share on day 4. The overall profit is $3$.
|
{"inputs": ["3\n3\n5 3 2\n3\n1 2 100\n4\n1 3 1 2"], "outputs": ["0\n197\n3\n"]}
| 568
| 47
|
coding
|
Solve the programming task below in a Python markdown code block.
Sardar Singh has many men fighting for him, and he would like to calculate the power of each of them to better plan for his fight against Ramadhir.
The power of a string S of lowercase English alphabets is defined to be
\sum_{i = 1}^{|S|} i\cdot ord(S_{i})
where ord(S_{i}) is the position of S_{i} in the alphabet, i.e, ord('a') = 1, ord('b') = 2, \dots, ord('z') = 26.
Each of Sardar Singh's men has a name consisting of lowercase English alphabets. The power of a man is defined to be the maximum power over all possible rearrangements of this string.
Find the power of each of Sardar Singh's men.
------ Input Format ------
- The first line of input contains an integer T, denoting the total number of Sardar Singh's men.
- Each of the next T lines contains a single string S_{i}, the name of Sardar Singh's i-th man.
------ Output Format ------
- Output T lines, each containing a single integer. The i-th of these lines should have the power of the i-th of Sardar Singh's men.
------ Constraints ------
$1 ≤ T ≤ 60$
$1 ≤ |S_{i}| ≤ 100$
$S_{i}$ consists of lowercase english alphabets only.
----- Sample Input 1 ------
1
faizal
----- Sample Output 1 ------
273
----- explanation 1 ------
The rearrangement with maximum power is $aafilz$. Its power can be calculated as
$$
1\cdot ord('a') + 2\cdot ord('a') + 3\cdot ord('f') + 4\cdot ord('i') + 5\cdot ord('l') + 6\cdot ord('z')
$$
which equals $273$.
It can be verified that no rearrangement gives a larger power.
|
{"inputs": ["1\nfaizal"], "outputs": ["273"]}
| 450
| 18
|
coding
|
Solve the programming task below in a Python markdown code block.
Gildong is now developing a puzzle game. The puzzle consists of n platforms numbered from 1 to n. The player plays the game as a character that can stand on each platform and the goal of the game is to move the character from the 1-st platform to the n-th platform.
The i-th platform is labeled with an integer a_i (0 ≤ a_i ≤ n-i). When the character is standing on the i-th platform, the player can move the character to any of the j-th platforms where i+1 ≤ j ≤ i+a_i. If the character is on the i-th platform where a_i=0 and i ≠ n, the player loses the game.
Since Gildong thinks the current game is not hard enough, he wants to make it even harder. He wants to change some (possibly zero) labels to 0 so that there remains exactly one way to win. He wants to modify the game as little as possible, so he's asking you to find the minimum number of platforms that should have their labels changed. Two ways are different if and only if there exists a platform the character gets to in one way but not in the other way.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 500).
Each test case contains two lines. The first line of each test case consists of an integer n (2 ≤ n ≤ 3000) — the number of platforms of the game.
The second line of each test case contains n integers. The i-th integer is a_i (0 ≤ a_i ≤ n-i) — the integer of the i-th platform.
It is guaranteed that:
* For each test case, there is at least one way to win initially.
* The sum of n in all test cases doesn't exceed 3000.
Output
For each test case, print one integer — the minimum number of different labels that should be changed to 0 so that there remains exactly one way to win.
Example
Input
3
4
1 1 1 0
5
4 3 2 1 0
9
4 1 4 2 1 0 2 1 0
Output
0
3
2
Note
In the first case, the player can only move to the next platform until they get to the 4-th platform. Since there is already only one way to win, the answer is zero.
In the second case, Gildong can change a_2, a_3, and a_4 to 0 so that the game becomes 4 0 0 0 0. Now the only way the player can win is to move directly from the 1-st platform to the 5-th platform.
In the third case, Gildong can change a_2 and a_8 to 0, then the only way to win is to move in the following way: 1 – 3 – 7 – 9.
|
{"inputs": ["3\n4\n1 1 1 0\n5\n4 3 2 1 0\n9\n4 1 4 3 1 0 2 1 0\n", "3\n4\n1 1 1 0\n5\n4 3 2 1 0\n9\n4 1 4 2 1 0 2 0 0\n", "3\n4\n1 1 1 0\n5\n2 3 2 1 0\n9\n3 1 4 1 1 0 2 1 0\n", "3\n4\n1 1 1 0\n5\n4 3 2 1 0\n9\n4 2 4 3 1 0 2 1 0\n", "3\n4\n2 1 1 0\n5\n2 3 2 1 0\n9\n3 1 4 1 0 0 2 1 0\n", "3\n4\n1 1 1 0\n5\n4 2 2 1 0\n9\n3 0 4 1 1 0 2 1 0\n", "3\n4\n1 1 1 0\n5\n4 3 2 1 0\n9\n4 1 0 3 1 0 2 0 0\n", "3\n4\n2 1 1 0\n5\n4 3 2 1 0\n9\n4 2 4 3 1 0 2 1 0\n"], "outputs": ["0\n3\n2\n", "0\n3\n1\n", "0\n2\n2\n", "0\n3\n3\n", "1\n2\n2\n", "0\n2\n1\n", "0\n3\n0\n", "1\n3\n3\n"]}
| 647
| 438
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a 4-character string S consisting of uppercase English letters.
Determine if S consists of exactly two kinds of characters which both appear twice in S.
-----Constraints-----
- The length of S is 4.
- S consists of uppercase English letters.
-----Input-----
Input is given from Standard Input in the following format:
S
-----Output-----
If S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.
-----Sample Input-----
ASSA
-----Sample Output-----
Yes
S consists of A and S which both appear twice in S.
|
{"inputs": ["EEFF", "FQEE", "SPOT", "ASRA", "EEQF", "SPNT", "ARSA", "EEQG"], "outputs": ["Yes\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
| 135
| 73
|
coding
|
Solve the programming task below in a Python markdown code block.
Coolguy gives you a simple problem. Given a $1$-indexed array, $\mbox{A}$, containing $N$ elements, what will $\textit{and}$ be after this pseudocode is implemented and executed? Print $and\%(10^9+7)$.
//f(a, b) is a function that returns the minimum element in interval [a, b]
ans = 0
for a -> [1, n]
for b -> [a, n]
for c -> [b + 1, n]
for d -> [c, n]
ans = ans + min(f(a, b), f(c, d))
Input Format
The first line contains $N$ (the size of array $\mbox{A}$).
The second line contains $N$ space-separated integers describing $\mbox{A}$.
Constraints
$1$ ≤ $N$ ≤ $2\times10^5$
$1$ ≤ $A_i$ ≤ $\mathbf{10^{9}}$
Note: $\mbox{A}$ is $1$-indexed (i.e.: $A=\{A_1,A_2,\text{...},A_{N-1},A_N\}$).
Output Format
Print the integer result of $and\%(10^9+7)$.
Sample Input
3
3 2 1
Sample Output
6
Explanation
$min(~f(1,1),~f(2,2)~)=2$
$min(~f(1,1),~f(2,3)~)=1$
$min(~f(1,1),~f(3,3)~)=1$
$min(~f(1,2),~f(3,3)~)=1$
$min(~f(2,2),~f(3,3)~)=1$
We then sum these numbers ($2+1+1+1+1=6$) and print $6\%(10^9+7)$, which is $\boldsymbol{6}$.
|
{"inputs": ["3\n3 2 1\n"], "outputs": ["6\n"]}
| 459
| 20
|
coding
|
Solve the programming task below in a Python markdown code block.
Consider an N \times M binary matrix A, i.e. a grid consisting of N rows numbered 1 to N from top to bottom and M columns numbered 1 to M from left to right, whose cells take the value 0 or 1. Let (i, j) denote the cell on the i-th row and j-th column.
The matrix is said to be *special* if for every cell (i, j), the following condition holds simultaneously:
A_{i, j} = 1 if and only if the sum of row i is equal to the sum of column j. More formally, A_{i, j} = 1 iff \displaystyle\sum_{k=1}^{M} A_{i, k} = \sum_{k=1}^{N} A_{k, j}
The *specialty* of a *special* matrix is defined to be the sum of values of all cells in the matrix. Given N and M, find the minimum *specialty* of a *special* matrix among all *special* binary matrices consisting of N rows and M columns.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of a single line containing N and M — the number of rows and the number of columns.
------ Output Format ------
For each test case, output on a new line the minimum *specialty* of a *special* matrix among all *special* binary matrices consisting of N rows and M columns.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N, M ≤ 5000$
----- Sample Input 1 ------
2
3 3
4 5
----- Sample Output 1 ------
5
10
----- explanation 1 ------
Test case 1: One such *special* binary matrix with minimum *specialty* is:
Test case 2: One such *special* binary matrix with minimum *specialty* is:
|
{"inputs": ["2\n3 3\n4 5"], "outputs": ["5\n10"]}
| 439
| 23
|
coding
|
Solve the programming task below in a Python markdown code block.
# Summary:
Given a number, `num`, return the shortest amount of `steps` it would take from 1, to land exactly on that number.
# Description:
A `step` is defined as either:
- Adding 1 to the number: `num += 1`
- Doubling the number: `num *= 2`
You will always start from the number `1` and you will have to return the shortest count of steps it would take to land exactly on that number.
`1 <= num <= 10000`
Examples:
`num == 3` would return `2` steps:
```
1 -- +1 --> 2: 1 step
2 -- +1 --> 3: 2 steps
2 steps
```
`num == 12` would return `4` steps:
```
1 -- +1 --> 2: 1 step
2 -- +1 --> 3: 2 steps
3 -- x2 --> 6: 3 steps
6 -- x2 --> 12: 4 steps
4 steps
```
`num == 16` would return `4` steps:
```
1 -- +1 --> 2: 1 step
2 -- x2 --> 4: 2 steps
4 -- x2 --> 8: 3 steps
8 -- x2 --> 16: 4 steps
4 steps
```
Also feel free to reuse/extend the following starter code:
```python
def shortest_steps_to_num(num):
```
|
{"functional": "_inputs = [[2], [3], [4], [5], [6], [7], [8], [9], [10], [20], [30], [40], [50], [11], [24], [37], [19], [48], [59], [65], [73], [83], [64], [99], [100], [10000], [1500], [1534], [1978], [2763], [9999], [2673], [4578], [9876], [2659], [7777], [9364], [7280], [4998], [9283], [8234], [7622], [800], [782], [674], [4467], [1233], [3678], [7892], [5672]]\n_outputs = [[1], [2], [2], [3], [3], [4], [3], [4], [4], [5], [7], [6], [7], [5], [5], [7], [6], [6], [9], [7], [8], [9], [6], [9], [8], [17], [16], [18], [17], [17], [20], [16], [17], [18], [16], [18], [17], [17], [17], [17], [16], [19], [11], [13], [12], [18], [14], [18], [19], [16]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(shortest_steps_to_num(*i), o[0])"}
| 345
| 567
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two integers m and n representing the dimensions of a 0-indexed m x n grid.
You are also given a 0-indexed 2D integer matrix coordinates, where coordinates[i] = [x, y] indicates that the cell with coordinates [x, y] is colored black. All cells in the grid that do not appear in coordinates are white.
A block is defined as a 2 x 2 submatrix of the grid. More formally, a block with cell [x, y] as its top-left corner where 0 <= x < m - 1 and 0 <= y < n - 1 contains the coordinates [x, y], [x + 1, y], [x, y + 1], and [x + 1, y + 1].
Return a 0-indexed integer array arr of size 5 such that arr[i] is the number of blocks that contains exactly i black cells.
Please complete the following python code precisely:
```python
class Solution:
def countBlackBlocks(self, m: int, n: int, coordinates: List[List[int]]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(m = 3, n = 3, coordinates = [[0,0]]) == [3,1,0,0,0]\n assert candidate(m = 3, n = 3, coordinates = [[0,0],[1,1],[0,2]]) == [0,2,2,0,0]\n\n\ncheck(Solution().countBlackBlocks)"}
| 257
| 94
|
coding
|
Solve the programming task below in a Python markdown code block.
The city of Gridland is represented as an $n\times m$ matrix where the rows are numbered from $1$ to $n$ and the columns are numbered from $1$ to $m$.
Gridland has a network of train tracks that always run in straight horizontal lines along a row. In other words, the start and end points of a train track are $(r,c1)$ and $(r,c2)$, where $\textbf{r}$ represents the row number, $\mbox{c1}$ represents the starting column, and $c2$ represents the ending column of the train track.
The mayor of Gridland is surveying the city to determine the number of locations where lampposts can be placed. A lamppost can be placed in any cell that is not occupied by a train track.
Given a map of Gridland and its $\boldsymbol{\mbox{k}}$ train tracks, find and print the number of cells where the mayor can place lampposts.
Note: A train track may overlap other train tracks within the same row.
Example
If Gridland's data is the following (1-based indexing):
k = 3
r c1 c2
1 1 4
2 2 4
3 1 2
4 2 3
It yields the following map:
In this case, there are five open cells (red) where lampposts can be placed.
Function Description
Complete the gridlandMetro function in the editor below.
gridlandMetro has the following parameter(s):
int n:: the number of rows in Gridland
int m:: the number of columns in Gridland
int k:: the number of tracks
track[k][3]: each element contains $3$ integers that represent $\textbf{row},\textbf{column start},\textbf{column end}$, all 1-indexed
Returns
int: the number of cells where lampposts can be installed
Input Format
The first line contains three space-separated integers $n,m$ and $\boldsymbol{\mbox{k}}$, the number of rows, columns and tracks to be mapped.
Each of the next $\boldsymbol{\mbox{k}}$ lines contains three space-separated integers, $r,c1$ and $c2$, the row number and the track column start and end.
Constraints
$1\leq n,m\leq10^9$
$0\leq k\leq1000$
$1\leq r\leq n$
$1\leq c1\leq c2\leq m$
Sample Input
STDIN Function
----- --------
4 4 3 n = 4, m = 4, k = 3
2 2 3 track = [[2, 2, 3], [3, 1, 4], [4, 4, 4]]
3 1 4
4 4 4
Sample Output
9
Explanation
In the diagram above, the yellow cells denote the first train track, green denotes the second, and blue denotes the third. Lampposts can be placed in any of the nine red cells.
|
{"inputs": ["4 4 3\n2 2 3\n3 1 4\n4 4 4\n"], "outputs": ["9\n"]}
| 702
| 36
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
A substring is a contiguous (non-empty) sequence of characters within a string.
A vowel substring is a substring that only consists of vowels ('a', 'e', 'i', 'o', and 'u') and has all five vowels present in it.
Given a string word, return the number of vowel substrings in word.
Please complete the following python code precisely:
```python
class Solution:
def countVowelSubstrings(self, word: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(word = \"aeiouu\") == 2\n assert candidate(word = \"unicornarihan\") == 0\n assert candidate(word = \"cuaieuouac\") == 7\n assert candidate(word = \"bbaeixoubb\") == 0\n\n\ncheck(Solution().countVowelSubstrings)"}
| 117
| 83
|
coding
|
Solve the programming task below in a Python markdown code block.
During the hypnosis session, Nicholas suddenly remembered a positive integer $n$, which doesn't contain zeros in decimal notation.
Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or equal to one?
For some numbers doing so is impossible: for example, for number $53$ it's impossible to delete some of its digits to obtain a not prime integer. However, for all $n$ in the test cases of this problem, it's guaranteed that it's possible to delete some of their digits to obtain a not prime number.
Note that you cannot remove all the digits from the number.
A prime number is a number that has no divisors except one and itself. A composite is a number that has more than two divisors. $1$ is neither a prime nor a composite number.
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains one positive integer $k$ ($1 \le k \le 50$) — the number of digits in the number.
The second line of each test case contains a positive integer $n$, which doesn't contain zeros in decimal notation ($10^{k-1} \le n < 10^{k}$). It is guaranteed that it is always possible to remove less than $k$ digits to make the number not prime.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10^4$.
-----Output-----
For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all delitions.
If there are multiple solutions, print any.
-----Examples-----
Input
7
3
237
5
44444
3
221
2
35
3
773
1
4
30
626221626221626221626221626221
Output
2
27
1
4
1
1
2
35
2
77
1
4
1
6
-----Note-----
In the first test case, you can't delete $2$ digits from the number $237$, as all the numbers $2$, $3$, and $7$ are prime. However, you can delete $1$ digit, obtaining a number $27 = 3^3$.
In the second test case, you can delete all digits except one, as $4 = 2^2$ is a composite number.
|
{"inputs": ["1\n1\n9\n", "1\n1\n1\n", "1\n1\n8\n", "1\n1\n4\n", "1\n1\n6\n", "1\n2\n77\n", "1\n2\n77\n", "1\n2\n83\n"], "outputs": ["1\n9\n", "1\n1\n", "1\n8\n", "1\n4\n", "1\n6\n", "2\n77\n", "2\n77\n", "1\n8\n"]}
| 630
| 123
|
coding
|
Solve the programming task below in a Python markdown code block.
Exceptions
Errors detected during execution are called exceptions.
Examples:
ZeroDivisionError
This error is raised when the second argument of a division or modulo operation is zero.
>>> a = '1'
>>> b = '0'
>>> print int(a) / int(b)
>>> ZeroDivisionError: integer division or modulo by zero
ValueError
This error is raised when a built-in operation or function receives an argument that has the right type but an inappropriate value.
>>> a = '1'
>>> b = '#'
>>> print int(a) / int(b)
>>> ValueError: invalid literal for int() with base 10: '#'
To learn more about different built-in exceptions click here.
Handling Exceptions
The statements try and except can be used to handle selected exceptions. A try statement may have more than one except clause to specify handlers for different exceptions.
#Code
try:
print 1/0
except ZeroDivisionError as e:
print "Error Code:",e
Output
Error Code: integer division or modulo by zero
Task
You are given two values $\boldsymbol{\alpha}$ and $\boldsymbol{b}$.
Perform integer division and print $a/b$.
Input Format
The first line contains $\mathbf{T}$, the number of test cases.
The next $\mathbf{T}$ lines each contain the space separated values of $\boldsymbol{\alpha}$ and $\boldsymbol{b}$.
Constraints
$0<T<10$
Output Format
Print the value of $a/b$.
In the case of ZeroDivisionError or ValueError, print the error code.
Sample Input
3
1 0
2 $
3 1
Sample Output
Error Code: integer division or modulo by zero
Error Code: invalid literal for int() with base 10: '$'
3
Note:
For integer division in Python 3 use //.
|
{"inputs": ["3\n1 0\n2 $\n3 1\n"], "outputs": ["Error Code: integer division or modulo by zero\nError Code: invalid literal for int() with base 10: '$'\n3\n"]}
| 402
| 52
|
coding
|
Solve the programming task below in a Python markdown code block.
Based on the information of the time when study started and the time when study ended, check whether the total time studied in one day is t or more, and if not, create a program to find the shortage time. Time is one unit per hour, and minutes and seconds are not considered. The time is expressed in 24-hour notation in 1-hour units.
Enter the target time of study per day and the information of the time actually studied (number of studies n, start time s and end time f of each study), and check whether the total study time has reached the target. If it has reached, write "OK", and if it has not reached, write a program that outputs the insufficient time. However, the study time spent on each will not overlap.
Example: Target time | Study time | Judgment
--- | --- | ---
10 hours | 6:00 to 11:00 = 5 hours
12:00 to 15:00 = 3 hours
18:00 to 22:00 = 4 hours | OK
14 hours | 6:00 to 11:00 = 5 hours
13:00 to 20:00 = 7 hours
| 2 hours shortage
Input example
Ten
3
6 11
12 15
18 22
14
2
6 11
13 20
0
Output example
OK
2
input
Given a sequence of multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
t
n
s1 f1
s2 f2
::
sn fn
The first line gives the target time of the day t (0 ≤ t ≤ 22), and the second line gives the number of studies n (1 ≤ n ≤ 10). The following n lines are given the start time si and end time f (6 ≤ si, fi ≤ 22) of the i-th study.
The number of datasets does not exceed 100.
output
Outputs OK or missing time on one line for each dataset.
Example
Input
10
3
6 11
12 15
18 22
14
2
6 11
13 20
0
Output
OK
2
|
{"inputs": ["10\n3\n8 11\n12 19\n18 9\n8\n2\n6 6\n5 6\n0", "10\n3\n8 11\n12 19\n18 9\n8\n2\n4 6\n5 6\n0", "10\n3\n6 11\n12 19\n18 9\n8\n2\n4 6\n5 6\n0", "10\n3\n6 11\n12 19\n18 9\n8\n2\n3 6\n5 6\n0", "10\n3\n6 11\n12 19\n18 9\n8\n2\n3 6\n5 9\n0", "10\n3\n8 11\n12 19\n18 38\n8\n2\n6 6\n5 6\n0", "10\n3\n8 8\n12 19\n18 8\n11\n2\n1 14\n5 6\n0", "10\n3\n6 11\n12 19\n18 11\n8\n2\n3 6\n5 9\n0"], "outputs": ["9\n7\n", "9\n5\n", "7\n5\n", "7\n4\n", "7\n1\n", "OK\n7\n", "13\nOK\n", "5\n1\n"]}
| 530
| 346
|
coding
|
Solve the programming task below in a Python markdown code block.
An integer N is a multiple of 9 if and only if the sum of the digits in the decimal representation of N is a multiple of 9.
Determine whether N is a multiple of 9.
-----Constraints-----
- 0 \leq N < 10^{200000}
- N is an integer.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
If N is a multiple of 9, print Yes; otherwise, print No.
-----Sample Input-----
123456789
-----Sample Output-----
Yes
The sum of these digits is 1+2+3+4+5+6+7+8+9=45, which is a multiple of 9, so 123456789 is a multiple of 9.
|
{"inputs": ["1", "2", "3", "9", "6", "8", "4", "0"], "outputs": ["No\n", "No\n", "No\n", "Yes\n", "No\n", "No\n", "No\n", "Yes"]}
| 193
| 61
|
coding
|
Solve the programming task below in a Python markdown code block.
The letters shop showcase is a string s, consisting of n lowercase Latin letters. As the name tells, letters are sold in the shop.
Letters are sold one by one from the leftmost to the rightmost. Any customer can only buy some prefix of letters from the string s.
There are m friends, the i-th of them is named t_i. Each of them is planning to estimate the following value: how many letters (the length of the shortest prefix) would s/he need to buy if s/he wanted to construct her/his name of bought letters. The name can be constructed if each letter is presented in the equal or greater amount.
* For example, for s="arrayhead" and t_i="arya" 5 letters have to be bought ("arrayhead").
* For example, for s="arrayhead" and t_i="harry" 6 letters have to be bought ("arrayhead").
* For example, for s="arrayhead" and t_i="ray" 5 letters have to be bought ("arrayhead").
* For example, for s="arrayhead" and t_i="r" 2 letters have to be bought ("arrayhead").
* For example, for s="arrayhead" and t_i="areahydra" all 9 letters have to be bought ("arrayhead").
It is guaranteed that every friend can construct her/his name using the letters from the string s.
Note that the values for friends are independent, friends are only estimating them but not actually buying the letters.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of showcase string s.
The second line contains string s, consisting of exactly n lowercase Latin letters.
The third line contains one integer m (1 ≤ m ≤ 5 ⋅ 10^4) — the number of friends.
The i-th of the next m lines contains t_i (1 ≤ |t_i| ≤ 2 ⋅ 10^5) — the name of the i-th friend.
It is guaranteed that ∑ _{i=1}^m |t_i| ≤ 2 ⋅ 10^5.
Output
For each friend print the length of the shortest prefix of letters from s s/he would need to buy to be able to construct her/his name of them. The name can be constructed if each letter is presented in the equal or greater amount.
It is guaranteed that every friend can construct her/his name using the letters from the string s.
Example
Input
9
arrayhead
5
arya
harry
ray
r
areahydra
Output
5
6
5
2
9
|
{"inputs": ["4\nabcd\n3\na\nd\nac\n", "4\nacbd\n3\na\nd\nac\n", "4\ndbca\n3\na\nd\nca\n", "4\nacbd\n3\na\nc\nac\n", "4\nacbd\n3\nb\nd\nca\n", "4\ndcba\n3\na\nc\nac\n", "4\nacbd\n3\nb\nd\ncb\n", "4\ndbca\n3\na\nc\nac\n"], "outputs": ["1\n4\n3\n", "1\n4\n2\n", "4\n1\n4\n", "1\n2\n2\n", "3\n4\n2\n", "4\n2\n4\n", "3\n4\n3\n", "4\n3\n4\n"]}
| 588
| 192
|
coding
|
Solve the programming task below in a Python markdown code block.
Let's write all the positive integer numbers one after another from $1$ without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536...
Your task is to print the $k$-th digit of this sequence.
-----Input-----
The first and only line contains integer $k$ ($1 \le k \le 10000$) — the position to process ($1$-based index).
-----Output-----
Print the $k$-th digit of the resulting infinite sequence.
-----Examples-----
Input
7
Output
7
Input
21
Output
5
|
{"inputs": ["7\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "8\n"], "outputs": ["7\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "8\n"]}
| 206
| 70
|
coding
|
Solve the programming task below in a Python markdown code block.
Monica decides that she would like to get to know the neighbours in the apartment better. She makes a batch of wonderful chocolates and hangs them on the door in a basket hoping that her neighbors will take some and they can meet. The neighbours (including Joey) eventually go crazy over the candy and demand more. Eventually, she keeps a bowl full of chocolates at the door for the last time.
There are N neighbours. The i^{th} neigbhour has initial energy equal to A_{i}. There is one bowl filled with chocolates. The neighbours are made to stand in a row and the bowl is passed around by obeying the following rules:
Any person can hold the bowl initially.
If the person holding the bowl has positive energy, he/she passes the bowl to the person on the immediate right of him/her. The rightmost person in the row passes the bowl to the leftmost person in the row.
The act of passing the bowl takes 1 second.
If the person holding the bowl has non-positive energy, he/she drops the bowl.
After each pass, the energy of the person reduces by 1.
Among all possible ways in which the N neighbours start the game, find the maximum time until the bowl is dropped.
------ Input Format ------
- First line will contain T, number of testcases. Then the testcases follow.
- First line of each testcase contains one integer N.
- Second line of each testcase contains of N integers, denoting the elements of array A.
------ Output Format ------
For each testcase, output in a single line the maximum time until the bowl is dropped.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 10^{5}$
$0 ≤ A[i] ≤ 10^{6}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$
----- Sample Input 1 ------
3
3
2 1 1
3
0 5 0
4
3 0 2 1
----- Sample Output 1 ------
4
1
3
----- explanation 1 ------
Test case 1: One of the optimal orders in which all the neighbours can stand in the row is:
$1$ $\rightarrow$ $2$ $\rightarrow$ $3$ $\rightarrow$ $1$. The bowl is initially with person $1$.
- Person $1$, in one second, passes the bowl to person $2$ and his/her own energy becomes $1$.
- Person $2$, in one second, passes the bowl to person $3$ and his/her own energy becomes $0$.
- Person $3$, in one second, passes the bowl to person $1$ and his/her own energy becomes $0$.
- Person $1$, in one second, passes the bowl to person $2$ and his/her own energy becomes $0$.
- Person $2$ has $0$ energy, so he/she drops the bowl.
Thus, the bowl is dropped after $4$ seconds.
Test case 2: One of the optimal orders in which all the neighbours can stand in the row is:
$2$ $\rightarrow$ $1$ $\rightarrow$ $3$ $\rightarrow$ $2$. The bowl is initially with person $2$. Thus, it would travel as $2$ $\rightarrow$ $1$. The bowl can not be passed further due to $0$ energy of person $1$.
|
{"inputs": ["3\n3\n2 1 1\n3\n0 5 0\n4\n3 0 2 1\n"], "outputs": ["4\n1\n3\n"]}
| 741
| 44
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an undirected graph. You are given an integer n which is the number of nodes in the graph and an array edges, where each edges[i] = [ui, vi] indicates that there is an undirected edge between ui and vi.
A connected trio is a set of three nodes where there is an edge between every pair of them.
The degree of a connected trio is the number of edges where one endpoint is in the trio, and the other is not.
Return the minimum degree of a connected trio in the graph, or -1 if the graph has no connected trios.
Please complete the following python code precisely:
```python
class Solution:
def minTrioDegree(self, n: int, edges: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]) == 3\n assert candidate(n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]]) == 0\n\n\ncheck(Solution().minTrioDegree)"}
| 176
| 107
|
coding
|
Solve the programming task below in a Python markdown code block.
A direced graph is strongly connected if every two nodes are reachable from each other. In a strongly connected component of a directed graph, every two nodes of the component are mutually reachable.
Constraints
* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 30,000
* 1 ≤ Q ≤ 100,000
Input
A directed graph G(V, E) and a sequence of queries where each query contains a pair of nodes u and v.
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
Q
u0 v0
u1 v1
:
uQ-1 vQ-1
|V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (directed).
ui and vi represent a pair of nodes given as the i-th query.
Output
For each query, pinrt "1" if the given nodes belong to the same strongly connected component, "0" otherwise.
Example
Input
5 6
0 1
1 0
1 2
2 4
4 3
3 2
4
0 1
0 3
2 3
3 4
Output
1
0
1
1
|
{"inputs": ["5 6\n0 1\n1 0\n0 2\n2 4\n4 3\n3 2\n4\n0 1\n0 3\n2 3\n3 4", "5 6\n0 1\n1 0\n1 2\n2 4\n4 3\n3 2\n4\n0 1\n0 3\n2 0\n3 4", "5 6\n0 1\n1 0\n1 2\n2 4\n4 0\n3 2\n4\n0 1\n0 3\n2 0\n3 4", "5 6\n0 1\n1 0\n1 2\n2 4\n1 1\n3 2\n4\n0 1\n0 3\n2 0\n3 4", "7 6\n0 1\n1 0\n1 2\n2 4\n4 3\n3 2\n4\n1 2\n0 3\n2 3\n3 4", "5 6\n0 0\n1 0\n1 2\n2 4\n4 0\n3 2\n4\n0 1\n0 3\n2 0\n3 4", "5 6\n0 1\n1 0\n1 2\n2 4\n4 1\n3 2\n4\n0 1\n0 0\n2 0\n3 4", "5 6\n0 1\n1 0\n1 2\n2 4\n1 1\n3 2\n3\n0 1\n0 3\n2 0\n3 4"], "outputs": ["1\n0\n1\n1\n", "1\n0\n0\n1\n", "1\n0\n1\n0\n", "1\n0\n0\n0\n", "0\n0\n1\n1\n", "0\n0\n0\n0\n", "1\n1\n1\n0\n", "1\n0\n0\n"]}
| 336
| 460
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a string s and a character c that occurs in s, return an array of integers answer where answer.length == s.length and answer[i] is the distance from index i to the closest occurrence of character c in s.
The distance between two indices i and j is abs(i - j), where abs is the absolute value function.
Please complete the following python code precisely:
```python
class Solution:
def shortestToChar(self, s: str, c: str) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"loveleetcode\", c = \"e\") == [3,2,1,0,1,0,0,1,2,2,1,0]\n assert candidate(s = \"aaab\", c = \"b\") == [3,2,1,0]\n\n\ncheck(Solution().shortestToChar)"}
| 120
| 87
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an integer n and a 0-indexed 2D array queries where queries[i] = [typei, indexi, vali].
Initially, there is a 0-indexed n x n matrix filled with 0's. For each query, you must apply one of the following changes:
if typei == 0, set the values in the row with indexi to vali, overwriting any previous values.
if typei == 1, set the values in the column with indexi to vali, overwriting any previous values.
Return the sum of integers in the matrix after all queries are applied.
Please complete the following python code precisely:
```python
class Solution:
def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 3, queries = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]]) == 23\n assert candidate(n = 3, queries = [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]]) == 17\n\n\ncheck(Solution().matrixSumQueries)"}
| 184
| 106
|
coding
|
Solve the programming task below in a Python markdown code block.
The penultimate task involved the calculation of the typical Fibonacci sequence up to the nth term.
Input – single integer n
Output – Fibonacci sequence up to the nth term, all terms on a new line
SAMPLE INPUT
5
SAMPLE OUTPUT
1
1
2
3
5
|
{"inputs": ["5"], "outputs": ["1\n1\n2\n3\n5"]}
| 71
| 20
|
coding
|
Solve the programming task below in a Python markdown code block.
You need to handle two extraordinarily large integers. The good news is that you don't need to perform any arithmetic operation on them. You just need to compare them and see whether they are equal or one is greater than the other.
Given two strings x and y, print either "x< y"(ignore space, editor issues), "x>y" or "x=y" depending on the values represented by x and y. x and y each consist of a decimal integer followed zero or more '!' characters. Each '!' represents the factorial operation. For example, "3!!" represents 3!! = 6! = 720.
Input - First line of input contains no. of testcases and each test consist of 2 lines x and y.
Ouptut - Print the required output.
SAMPLE INPUT
3
0!
1
9!!
999999999
456!!!
123!!!!!!
SAMPLE OUTPUT
x=y
x>y
x<y
|
{"inputs": ["27\n0!!!\n0\n1\n1!!!\n2!!!\n2\n3!!!\n999999999\n0!\n0\n11!\n40000000\n3!!\n721\n3\n2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n7!!!\n3!!!!\n0!\n2\n1\n2\n0\n0\n3!!!!\n4!!!!\n15!\n999999999\n999999999\n4!!\n7!!!\n5000!!\n40321\n8!\n3!\n5\n5!!\n3!!!\n7!\n3!!\n3!\n7\n6!\n719\n719!!\n6!!!\n6!\n721\n721!!!!\n6!!!!!\n720\n3!!\n0!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n1!!!!!!!", "25\n0!\n1\n9!\n999999999\n9!!\n999999999\n456!!!\n123!!!!!!\n5!\n120\n9!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n999999999!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n0!\n1\n1!\n1\n2!\n2\n6\n3!\n4!\n24\n5!\n120\n720\n6!\n5040!\n7!!\n8!!!\n40320!!\n362880!!!\n9!!!!\n10!!!!!\n3628800!!!!\n39916800!!!!!\n11!!!!!!\n12!!!!!!!\n479001600!!!!!!\n13!\n999999999\n0!!\n1\n1\n1!!\n2!!\n2\n720\n3!!\n4!!\n999999999"], "outputs": ["x>y\nx=y\nx=y\nx>y\nx>y\nx<y\nx<y\nx>y\nx>y\nx<y\nx<y\nx=y\nx<y\nx>y\nx<y\nx>y\nx>y\nx>y\nx<y\nx>y\nx<y\nx>y\nx<y\nx<y\nx>y\nx=y\nx=y", "x=y\nx<y\nx>y\nx<y\nx=y\nx>y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx=y\nx>y\nx=y\nx=y\nx=y\nx=y\nx>y"]}
| 218
| 655
|
coding
|
Solve the programming task below in a Python markdown code block.
Given a string of integers, return the number of odd-numbered substrings that can be formed.
For example, in the case of `"1341"`, they are `1, 1, 3, 13, 41, 341, 1341`, a total of `7` numbers.
`solve("1341") = 7`. See test cases for more examples.
Good luck!
If you like substring Katas, please try
[Longest vowel chain](https://www.codewars.com/kata/59c5f4e9d751df43cf000035)
[Alphabet symmetry](https://www.codewars.com/kata/59d9ff9f7905dfeed50000b0)
Also feel free to reuse/extend the following starter code:
```python
def solve(s):
```
|
{"functional": "_inputs = [['1341'], ['1357'], ['13471'], ['134721'], ['1347231'], ['13472315']]\n_outputs = [[7], [10], [12], [13], [20], [28]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
| 215
| 217
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef is the new king of the country Chefland. As first and most important responsibility he wants to reconstruct the road system of Chefland. There are N (1 to N) cities in the country and each city i has a population Pi. Chef wants to build some bi-directional roads connecting different cities such that each city is connected to every other city (by a direct road or through some other intermediate city) and starting from any city one can visit every other city in the country through these roads. Cost of building a road between two cities u and v is Pu x Pv. Cost to build the road system is the sum of cost of every individual road that would be built.
Help king Chef to find the minimum cost to build the new road system in Chefland such that every city is connected to each other.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
First line contains an integer N denoting the number of cities in the country. Second line contains N space separated integers Pi, the population of i-th city.
-----Output-----
For each test case, print a single integer, the minimum cost to build the new road system on separate line.
-----Constraints-----
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 105
- 1 ≤ Pi ≤ 106
-----Example-----
Input:
2
2
5 10
4
15 10 7 13
Output:
50
266
|
{"inputs": ["2\n2\n9 3\n4\n29 0 4 2", "2\n2\n8 9\n4\n5 18 2 5", "2\n2\n8 9\n4\n5 36 2 5", "2\n2\n9 2\n4\n29 0 4 2", "2\n2\n8 9\n4\n4 18 2 5", "2\n2\n8 9\n4\n5 18 2 13", "2\n2\n3 5\n4\n8 10 7 13", "2\n2\n3 5\n4\n9 10 7 13"], "outputs": ["27\n0\n", "72\n56\n", "72\n92\n", "18\n0\n", "72\n54\n", "72\n72\n", "15\n217\n", "15\n224\n"]}
| 339
| 233
|
coding
|
Solve the programming task below in a Python markdown code block.
Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system.
Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova.
Input
The first line contains integer n (1 ≤ n ≤ 109).
Output
In the first line print one integer k — number of different values of x satisfying the condition.
In next k lines print these values in ascending order.
Examples
Input
21
Output
1
15
Input
20
Output
0
Note
In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21.
In the second test case there are no such x.
|
{"inputs": ["2\n", "9\n", "3\n", "1\n", "4\n", "5\n", "8\n", "6\n"], "outputs": ["1\n1 ", "0\n", "0\n", "0\n", "1\n2\n", "0\n\n", "1\n4\n", "1\n3\n"]}
| 294
| 78
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 2·10^5) — the number of throws of the first team. Then follow n integer numbers — the distances of throws a_{i} (1 ≤ a_{i} ≤ 2·10^9).
Then follows number m (1 ≤ m ≤ 2·10^5) — the number of the throws of the second team. Then follow m integer numbers — the distances of throws of b_{i} (1 ≤ b_{i} ≤ 2·10^9).
-----Output-----
Print two numbers in the format a:b — the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
{"inputs": ["1\n3\n1\n3\n", "1\n3\n1\n3\n", "1\n3\n1\n1\n", "1\n1\n2\n1 2\n", "1\n1\n2\n1 1\n", "1\n1\n2\n1 2\n", "1\n1\n2\n1 1\n", "2\n2 2\n2\n2 2\n"], "outputs": ["3:3\n", "3:3", "3:2\n", "2:4\n", "2:4\n", "2:4", "2:4", "6:6\n"]}
| 378
| 143
|
coding
|
Solve the programming task below in a Python markdown code block.
The male gametes or sperm cells in humans and other mammals are heterogametic and contain one of two types of sex chromosomes. They are either X or Y. The female gametes or eggs however, contain only the X sex chromosome and are homogametic.
The sperm cell determines the sex of an individual in this case. If a sperm cell containing an X chromosome fertilizes an egg, the resulting zygote will be XX or female. If the sperm cell contains a Y chromosome, then the resulting zygote will be XY or male.
Determine if the sex of the offspring will be male or female based on the X or Y chromosome present in the male's sperm.
If the sperm contains the X chromosome, return "Congratulations! You're going to have a daughter.";
If the sperm contains the Y chromosome, return "Congratulations! You're going to have a son.";
Also feel free to reuse/extend the following starter code:
```python
def chromosome_check(sperm):
```
|
{"functional": "_inputs = [['XY'], ['XX']]\n_outputs = [[\"Congratulations! You're going to have a son.\"], [\"Congratulations! You're going to have a daughter.\"]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(chromosome_check(*i), o[0])"}
| 215
| 181
|
coding
|
Solve the programming task below in a Python markdown code block.
Let S(n) denote the sum of the digits in the decimal notation of n.
For example, S(123) = 1 + 2 + 3 = 6.
We will call an integer n a Snuke number when, for all positive integers m such that m > n, \frac{n}{S(n)} \leq \frac{m}{S(m)} holds.
Given an integer K, list the K smallest Snuke numbers.
-----Constraints-----
- 1 \leq K
- The K-th smallest Snuke number is not greater than 10^{15}.
-----Input-----
Input is given from Standard Input in the following format:
K
-----Output-----
Print K lines. The i-th line should contain the i-th smallest Snuke number.
-----Sample Input-----
10
-----Sample Output-----
1
2
3
4
5
6
7
8
9
19
|
{"inputs": ["7", "5", "8", "2", "6", "4", "1", "3"], "outputs": ["1\n2\n3\n4\n5\n6\n7\n", "1\n2\n3\n4\n5\n", "1\n2\n3\n4\n5\n6\n7\n8\n", "1\n2\n", "1\n2\n3\n4\n5\n6\n", "1\n2\n3\n4\n", "1\n", "1\n2\n3\n"]}
| 211
| 118
|
coding
|
Solve the programming task below in a Python markdown code block.
Rakesh has built a model rocket and wants to test how stable it is. He usually uses a magic box which runs some tests on the rocket and tells if it is stable or not, but his friend broke it by trying to find out how stable he is (very delicate magic indeed). The box only gives a polynomial equation now which can help Rakesh find the stability (a failsafe by the manufacturers).
Rakesh reads the manual for the magic box and understands that in order to determine stability, he needs to take every other term and put them in two rows. Eg. If the polynomial is:
10 x^4 + 12 x^3 + 4 x^2 + 5 x + 3, the first two rows will be:
Row 1: 10 4 3
Row 2: 12 5 0
For all other rows, the nth element of the rth row is found recursively by multiplying the 1st element of the (r-1)th row and (n+1)th element of the (r-2)th row and subtracting it with 1st element of the (r-2)th row multiplied by (n+1)th element of the (r-1)th row.
So Row 3 will be (12 * 4 - 10 * 5) (12 * 3 - 10 * 0)
Row 3: -2 36 0
Row 4: -442 0 0
Row 5: -15912 0 0
There will not be any row six (number of rows = maximum power of x + 1)
The rocket is stable if there are no sign changes in the first column.
If all the elements of the rth row are 0, replace the nth element of the rth row by the nth element of the (r-1)th row multiplied by (maximum power of x + 4 - r - 2n).
If the first element of any row is 0 and some of the other elements are non zero, the rocket is unstable.
Can you help Rakesh check if his rocket is stable?
Input Format:
1. First row with number of test cases (T).
2. Next T rows with the coefficients of the polynomials for each case (10 12 4 5 3 for the case above).
Output Format:
1. "1" if stable (without "")
2. "0" if unstable (without "")
Sample Input:
1
10 12 4 5 3
Sample Output:
0
|
{"inputs": ["1\n10 12 4 5 3"], "outputs": ["0"]}
| 585
| 24
|
coding
|
Solve the programming task below in a Python markdown code block.
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
$n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once.
For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively).
For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of friends.
The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) — the coordinates of the houses of the friends.
-----Output-----
Print two integers — the minimum and the maximum possible number of occupied houses after all moves are performed.
-----Examples-----
Input
4
1 2 4 4
Output
2 4
Input
9
1 1 8 8 8 4 4 4 4
Output
3 8
Input
7
4 3 7 1 4 3 3
Output
3 6
-----Note-----
In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses.
For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example.
|
{"inputs": ["1\n1\n", "1\n1\n", "2\n1 1\n", "2\n1 2\n", "2\n1 2\n", "2\n1 1\n", "2\n2 2\n", "2\n2 1\n"], "outputs": ["1 1\n", "1 1\n", "1 2\n", "1 2\n", "1 2\n", "1 2\n", "1 2\n", "1 2\n"]}
| 663
| 114
|
coding
|
Solve the programming task below in a Python markdown code block.
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively.
Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k.
Determine if there exists a pair of antennas that cannot communicate directly.
Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p.
-----Constraints-----
- a, b, c, d, e and k are integers between 0 and 123 (inclusive).
- a < b < c < d < e
-----Input-----
Input is given from Standard Input in the following format:
a
b
c
d
e
k
-----Output-----
Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.
-----Sample Input-----
1
2
4
8
9
15
-----Sample Output-----
Yay!
In this case, there is no pair of antennas that cannot communicate directly, because:
- the distance between A and B is 2 - 1 = 1
- the distance between A and C is 4 - 1 = 3
- the distance between A and D is 8 - 1 = 7
- the distance between A and E is 9 - 1 = 8
- the distance between B and C is 4 - 2 = 2
- the distance between B and D is 8 - 2 = 6
- the distance between B and E is 9 - 2 = 7
- the distance between C and D is 8 - 4 = 4
- the distance between C and E is 9 - 4 = 5
- the distance between D and E is 9 - 8 = 1
and none of them is greater than 15. Thus, the correct output is Yay!.
|
{"inputs": ["1\n0\n7\n1\n5\n3", "1\n0\n7\n0\n5\n3", "1\n0\n9\n0\n5\n3", "1\n0\n5\n8\n4\n1", "1\n0\n7\n2\n5\n3", "1\n1\n9\n0\n5\n3", "1\n0\n5\n8\n4\n0", "1\n0\n2\n8\n4\n1"], "outputs": [":(\n", ":(\n", ":(\n", ":(\n", ":(\n", ":(\n", ":(\n", ":(\n"]}
| 451
| 150
|
coding
|
Solve the programming task below in a Python markdown code block.
Given an array $a$ of $n$ elements, print any value that appears at least three times or print -1 if there is no such value.
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 2\cdot10^5$) — the length of the array.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$) — the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$.
-----Output-----
For each test case, print any value that appears at least three times or print -1 if there is no such value.
-----Examples-----
Input
7
1
1
3
2 2 2
7
2 2 3 3 4 2 2
8
1 4 3 4 3 2 4 1
9
1 1 1 2 2 2 3 3 3
5
1 5 2 4 3
4
4 4 4 4
Output
-1
2
2
4
3
-1
4
-----Note-----
In the first test case there is just a single element, so it can't occur at least three times and the answer is -1.
In the second test case, all three elements of the array are equal to $2$, so $2$ occurs three times, and so the answer is $2$.
For the third test case, $2$ occurs four times, so the answer is $2$.
For the fourth test case, $4$ occurs three times, so the answer is $4$.
For the fifth test case, $1$, $2$ and $3$ all occur at least three times, so they are all valid outputs.
For the sixth test case, all elements are distinct, so none of them occurs at least three times and the answer is -1.
|
{"inputs": ["7\n1\n1\n3\n2 2 2\n7\n2 2 3 3 4 2 2\n8\n1 4 3 4 3 2 4 1\n9\n1 1 1 2 2 2 3 3 3\n5\n1 5 2 4 3\n4\n4 4 4 4\n"], "outputs": ["-1\n2\n2\n4\n3\n-1\n4\n"]}
| 499
| 116
|
coding
|
Solve the programming task below in a Python markdown code block.
Given a mathematical equation that has `*,+,-,/`, reverse it as follows:
```Haskell
solve("100*b/y") = "y/b*100"
solve("a+b-c/d*30") = "30*d/c-b+a"
```
More examples in test cases.
Good luck!
Please also try:
[Simple time difference](https://www.codewars.com/kata/5b76a34ff71e5de9db0000f2)
[Simple remove duplicates](https://www.codewars.com/kata/5ba38ba180824a86850000f7)
Also feel free to reuse/extend the following starter code:
```python
def solve(eq):
```
|
{"functional": "_inputs = [['100*b/y'], ['a+b-c/d*30'], ['a*b/c+50']]\n_outputs = [['y/b*100'], ['30*d/c-b+a'], ['50+c/b*a']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(solve(*i), o[0])"}
| 181
| 195
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
Output
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
Examples
Input
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
Output
1 4 3 5
Input
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
Output
1 4 3 5
|
{"inputs": ["2 1\n1 2 1\n", "3 1\n1 2 1\n", "2 1\n1 2 0\n", "2 1\n1 1 1\n", "3 1\n2 2 1\n", "3 3\n1 2 1\n1 3 2\n2 3 1\n", "3 3\n1 2 1\n1 3 2\n2 3 0\n", "3 3\n1 2 1\n1 3 2\n1 3 1\n"], "outputs": ["1 2\n", "-1\n", "1 2\n", "-1\n", "-1\n", "1 3\n", "1 2 3\n", "1 3\n"]}
| 299
| 182
|
coding
|
Solve the programming task below in a Python markdown code block.
We have N+1 integers: 10^{100}, 10^{100}+1, ..., 10^{100}+N.
We will choose K or more of these integers. Find the number of possible values of the sum of the chosen numbers, modulo (10^9+7).
-----Constraints-----
- 1 \leq N \leq 2\times 10^5
- 1 \leq K \leq N+1
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
N K
-----Output-----
Print the number of possible values of the sum, modulo (10^9+7).
-----Sample Input-----
3 2
-----Sample Output-----
10
The sum can take 10 values, as follows:
- (10^{100})+(10^{100}+1)=2\times 10^{100}+1
- (10^{100})+(10^{100}+2)=2\times 10^{100}+2
- (10^{100})+(10^{100}+3)=(10^{100}+1)+(10^{100}+2)=2\times 10^{100}+3
- (10^{100}+1)+(10^{100}+3)=2\times 10^{100}+4
- (10^{100}+2)+(10^{100}+3)=2\times 10^{100}+5
- (10^{100})+(10^{100}+1)+(10^{100}+2)=3\times 10^{100}+3
- (10^{100})+(10^{100}+1)+(10^{100}+3)=3\times 10^{100}+4
- (10^{100})+(10^{100}+2)+(10^{100}+3)=3\times 10^{100}+5
- (10^{100}+1)+(10^{100}+2)+(10^{100}+3)=3\times 10^{100}+6
- (10^{100})+(10^{100}+1)+(10^{100}+2)+(10^{100}+3)=4\times 10^{100}+6
|
{"inputs": ["3 1", "1 1", "3 4", "6 1", "2 1", "6 2", "4 3", "4 1"], "outputs": ["14\n", "3\n", "1\n", "63\n", "7\n", "56\n", "13\n", "25\n"]}
| 620
| 83
|
coding
|
Solve the programming task below in a Python markdown code block.
Shortest Common Non-Subsequence
A subsequence of a sequence $P$ is a sequence that can be derived from the original sequence $P$ by picking up some or no elements of $P$ preserving the order. For example, "ICPC" is a subsequence of "MICROPROCESSOR".
A common subsequence of two sequences is a subsequence of both sequences. The famous longest common subsequence problem is finding the longest of common subsequences of two given sequences.
In this problem, conversely, we consider the shortest common non-subsequence problem: Given two sequences consisting of 0 and 1, your task is to find the shortest sequence also consisting of 0 and 1 that is a subsequence of neither of the two sequences.
Input
The input consists of a single test case with two lines. Both lines are sequences consisting only of 0 and 1. Their lengths are between 1 and 4000, inclusive.
Output
Output in one line the shortest common non-subsequence of two given sequences. If there are two or more such sequences, you should output the lexicographically smallest one. Here, a sequence $P$ is lexicographically smaller than another sequence $Q$ of the same length if there exists $k$ such that $P_1 = Q_1, ... , P_{k-1} = Q_{k-1}$, and $P_k < Q_k$, where $S_i$ is the $i$-th character of a sequence $S$.
Sample Input 1
0101
1100001
Sample Output 1
0010
Sample Input 2
101010101
010101010
Sample Output 2
000000
Sample Input 3
11111111
00000000
Sample Output 3
01
Example
Input
0101
1100001
Output
0010
|
{"inputs": ["0101\n1101001", "0101\n1100000", "0101\n1101000", "0101\n0001000", "0101\n0011001", "0101\n0110001", "0101\n1111001", "0101\n1001001"], "outputs": ["0000\n", "111\n", "0001\n", "110\n", "1000\n", "0010\n", "000\n", "0110\n"]}
| 448
| 171
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a regular polygon with $n$ vertices labeled from $1$ to $n$ in counter-clockwise order. The triangulation of a given polygon is a set of triangles such that each vertex of each triangle is a vertex of the initial polygon, there is no pair of triangles such that their intersection has non-zero area, and the total area of all triangles is equal to the area of the given polygon. The weight of a triangulation is the sum of weigths of triangles it consists of, where the weight of a triagle is denoted as the product of labels of its vertices.
Calculate the minimum weight among all triangulations of the polygon.
-----Input-----
The first line contains single integer $n$ ($3 \le n \le 500$) — the number of vertices in the regular polygon.
-----Output-----
Print one integer — the minimum weight among all triangulations of the given polygon.
-----Examples-----
Input
3
Output
6
Input
4
Output
18
-----Note-----
According to Wiki: polygon triangulation is the decomposition of a polygonal area (simple polygon) $P$ into a set of triangles, i. e., finding a set of triangles with pairwise non-intersecting interiors whose union is $P$.
In the first example the polygon is a triangle, so we don't need to cut it further, so the answer is $1 \cdot 2 \cdot 3 = 6$.
In the second example the polygon is a rectangle, so it should be divided into two triangles. It's optimal to cut it using diagonal $1-3$ so answer is $1 \cdot 2 \cdot 3 + 1 \cdot 3 \cdot 4 = 6 + 12 = 18$.
|
{"inputs": ["3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "5\n"], "outputs": ["6\n", "18\n", "38\n", "68\n", "110\n", "166\n", "238\n", "38\n"]}
| 383
| 80
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an integer n denoting the total number of servers and a 2D 0-indexed integer array logs, where logs[i] = [server_id, time] denotes that the server with id server_id received a request at time time.
You are also given an integer x and a 0-indexed integer array queries.
Return a 0-indexed integer array arr of length queries.length where arr[i] represents the number of servers that did not receive any requests during the time interval [queries[i] - x, queries[i]].
Note that the time intervals are inclusive.
Please complete the following python code precisely:
```python
class Solution:
def countServers(self, n: int, logs: List[List[int]], x: int, queries: List[int]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11]) == [1,2]\n assert candidate(n = 3, logs = [[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4]) == [0,1]\n\n\ncheck(Solution().countServers)"}
| 183
| 109
|
coding
|
Solve the programming task below in a Python markdown code block.
Shapur was an extremely gifted student. He was great at everything including Combinatorics, Algebra, Number Theory, Geometry, Calculus, etc. He was not only smart but extraordinarily fast! He could manage to sum 1018 numbers in a single second.
One day in 230 AD Shapur was trying to find out if any one can possibly do calculations faster than him. As a result he made a very great contest and asked every one to come and take part.
In his contest he gave the contestants many different pairs of numbers. Each number is made from digits 0 or 1. The contestants should write a new number corresponding to the given pair of numbers. The rule is simple: The i-th digit of the answer is 1 if and only if the i-th digit of the two given numbers differ. In the other case the i-th digit of the answer is 0.
Shapur made many numbers and first tried his own speed. He saw that he can perform these operations on numbers of length ∞ (length of a number is number of digits in it) in a glance! He always gives correct answers so he expects the contestants to give correct answers, too. He is a good fellow so he won't give anyone very big numbers and he always gives one person numbers of same length.
Now you are going to take part in Shapur's contest. See if you are faster and more accurate.
Input
There are two lines in each input. Each of them contains a single number. It is guaranteed that the numbers are made from 0 and 1 only and that their length is same. The numbers may start with 0. The length of each number doesn't exceed 100.
Output
Write one line — the corresponding answer. Do not omit the leading 0s.
Examples
Input
1010100
0100101
Output
1110001
Input
000
111
Output
111
Input
1110
1010
Output
0100
Input
01110
01100
Output
00010
|
{"inputs": ["0\n0\n", "0\n1\n", "10\n01\n", "000\n111\n", "1110\n1010\n", "01110\n01100\n", "011101\n000001\n", "011101\n000000\n"], "outputs": ["0\n", "1\n", "11\n", "111\n", "0100\n", "00010\n", "011100\n", "011101\n"]}
| 471
| 146
|
coding
|
Solve the programming task below in a Python markdown code block.
Gregor is learning about RSA cryptography, and although he doesn't understand how RSA works, he is now fascinated with prime numbers and factoring them.
Gregor's favorite prime number is $P$. Gregor wants to find two bases of $P$. Formally, Gregor is looking for two integers $a$ and $b$ which satisfy both of the following properties.
$P mod a = P mod b$, where $x mod y$ denotes the remainder when $x$ is divided by $y$, and
$2 \le a < b \le P$.
Help Gregor find two bases of his favorite prime number!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each subsequent line contains the integer $P$ ($5 \le P \le {10}^9$), with $P$ guaranteed to be prime.
-----Output-----
Your output should consist of $t$ lines. Each line should consist of two integers $a$ and $b$ ($2 \le a < b \le P$). If there are multiple possible solutions, print any.
-----Examples-----
Input
2
17
5
Output
3 5
2 4
-----Note-----
The first query is $P=17$. $a=3$ and $b=5$ are valid bases in this case, because $17 mod 3 = 17 mod 5 = 2$. There are other pairs which work as well.
In the second query, with $P=5$, the only solution is $a=2$ and $b=4$.
|
{"inputs": ["2\n7\n5\n", "1\n271\n", "1\n833\n", "1\n497\n", "1\n643\n", "1\n997\n", "1\n611\n", "1\n887\n"], "outputs": ["2 6\n2 4\n", "2 270\n", "2 832\n", "2 496\n", "2 642\n", "2 996\n", "2 610\n", "2 886\n"]}
| 367
| 136
|
coding
|
Solve the programming task below in a Python markdown code block.
Chan has decided to make a list of all possible combinations of letters of a given string S. If there are two strings with the same set of characters, print the lexicographically smallest arrangement of the two strings.
abc acb cab bac bca
all the above strings' lexicographically smallest string is abc.
Each character in the string S is unique. Your task is to print the entire list of Chan's in lexicographic order.
for string abc, the list in lexicographic order is given below
a ab abc ac b bc c
Input Format
The first line contains the number of test cases T. T testcases follow.
Each testcase has 2 lines. The first line is an integer N ( the length of the string).
The second line contains the string S.
Output Format
For each testcase, print the entire list of combinations of string S, with each combination of letters in a newline.
Constraints
0< T< 50
1< N< 16
string S contains only small alphabets(a-z)
Sample Input
2
2
ab
3
xyz
Sample Output
a
ab
b
x
xy
xyz
xz
y
yz
z
Explanation
In the first case we have ab, the possibilities are a, ab and b. Similarly, all combination of characters of xyz.
|
{"inputs": ["2\n2\nab\n3\nxyz\n"], "outputs": ["a\nab\nb\nx\nxy\nxyz\nxz\ny\nyz\nz\n"]}
| 300
| 40
|
coding
|
Solve the programming task below in a Python markdown code block.
Stellar history 2005.11.5. You are about to engage an enemy spacecraft as the captain of the UAZ Advance spacecraft. Fortunately, the enemy spaceship is still unnoticed. In addition, the space coordinates of the enemy are already known, and the "feather cannon" that emits a powerful straight beam is ready to launch. After that, I just issue a launch command.
However, there is an energy barrier installed by the enemy in outer space. The barrier is triangular and bounces off the "feather cannon" beam. Also, if the beam hits the barrier, the enemy will notice and escape. If you cannot determine that you will hit in advance, you will not be able to issue a launch command.
Therefore, enter the cosmic coordinates (three-dimensional coordinates x, y, z) of the UAZ Advance, enemy, and barrier positions, and if the beam avoids the barrier and hits the enemy, "HIT", if it hits the barrier " Create a program that outputs "MISS".
However, the barrier is only for those that look like a triangle from the Advance issue, and nothing that looks like a line segment is crushed. Barriers are also valid at boundaries containing triangular vertices and shall bounce the beam. Also, if the enemy is in the barrier, output "MISS".
Constraints
* -100 ≤ x, y, z ≤ 100
* The UAZ Advance and the enemy are never in the same position.
Input
The format of the input data is as follows:
The first line is the coordinates of UAZ Advance (x, y, z) (integer, half-width space delimited)
The second line is the coordinates of the enemy (x, y, z) (integer, half-width space delimiter)
The third line is the coordinates of vertex 1 of the barrier (x, y, z) (integer, half-width space delimiter)
The fourth line is the coordinates (x, y, z) of the vertex 2 of the barrier (integer, half-width space delimiter)
The fifth line is the coordinates (x, y, z) of the vertex 3 of the barrier (integer, half-width space delimiter)
Output
Output on one line with HIT or MISS.
Examples
Input
-10 0 0
10 0 0
0 10 0
0 10 10
0 0 10
Output
HIT
Input
-10 6 6
10 6 6
0 10 0
0 10 10
0 0 10
Output
MISS
|
{"inputs": ["-19 0 0\n14 0 0\n0 2 1\n0 8 9\n0 0 8", "-19 0 0\n14 0 0\n1 2 1\n0 8 9\n0 0 8", "-2 0 0\n14 0 0\n1 1 1\n0 8 9\n-1 0 4", "-2 0 0\n14 0 0\n1 1 1\n0 8 9\n-2 0 4", "-2 0 1\n14 0 0\n1 1 1\n0 8 9\n-2 0 4", "-2 0 1\n14 0 0\n1 1 1\n0 8 9\n-1 0 4", "-4 0 1\n14 0 0\n1 1 2\n0 5 9\n-1 0 4", "-4 0 2\n14 0 0\n1 1 2\n0 5 9\n-1 0 4"], "outputs": ["HIT\n", "HIT\n", "HIT\n", "HIT\n", "HIT\n", "HIT\n", "HIT\n", "HIT\n"]}
| 561
| 311
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian.
Mike is fond of collecting stamps. He has a lot of rare and unusual ones in his collection. Unfortunately, a few days ago he was fired from his well-paid job.
But he doesn't complain about that, he acts! That's why Mike picked up N stamps from his collection and is going to sell them. He has already placed an announcement on the Internet and got M offers. Each offer can be described as a set of stamps, that the buyer wants to buy. Now Mike needs your help to choose a set of offers, that he should accept.
He can't accept offers partially. Also, as far as Mike has the only copy of each stamp, he can sell one stamp to at most one buyer.
Of course, Mike wants to maximize the number of accepted offers. Help him!
------ Input ------
The first line contains two integer N and M, denoting the number of the stamps and the number of the offers.
The next M lines contain the descriptions of the offers. The (i+1)'th line of the input contains the description of the i'th offer and corresponds to the following pattern: K_{i} A_{i, 1} A_{i, 2}, ..., Ai, K_{i}. K_{i} - is the number of the stamps, which the i'th buyer wants to buy, A_{i} - is the list of the stamps sorted in the ascending order.
------ Output ------
Output should contain the only integer, denoting the maximal number of the offers, that Mike can accept.
------ Constraints ------
1 ≤ N ≤ 20,000
1 ≤ M ≤ 20
1 ≤ K_{i}
----- Sample Input 1 ------
4 3
2 1 2
2 2 3
2 3 4
----- Sample Output 1 ------
2
----- explanation 1 ------
In the example test Mike should accept the first and the third offer.
|
{"inputs": ["4 3\n2 1 2\n2 2 3\n2 3 4\n"], "outputs": ["2"]}
| 434
| 33
|
coding
|
Solve the programming task below in a Python markdown code block.
Victor tries to write his own text editor, with word correction included. However, the rules of word correction are really strange.
Victor thinks that if a word contains two consecutive vowels, then it's kinda weird and it needs to be replaced. So the word corrector works in such a way: as long as there are two consecutive vowels in the word, it deletes the first vowel in a word such that there is another vowel right before it. If there are no two consecutive vowels in the word, it is considered to be correct.
You are given a word s. Can you predict what will it become after correction?
In this problem letters a, e, i, o, u and y are considered to be vowels.
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 100) — the number of letters in word s before the correction.
The second line contains a string s consisting of exactly n lowercase Latin letters — the word before the correction.
-----Output-----
Output the word s after the correction.
-----Examples-----
Input
5
weird
Output
werd
Input
4
word
Output
word
Input
5
aaeaa
Output
a
-----Note-----
Explanations of the examples: There is only one replace: weird $\rightarrow$ werd; No replace needed since there are no two consecutive vowels; aaeaa $\rightarrow$ aeaa $\rightarrow$ aaa $\rightarrow$ aa $\rightarrow$ a.
|
{"inputs": ["1\na\n", "1\nb\n", "1\ne\n", "1\ne\n", "1\na\n", "1\nb\n", "1\nf\n", "1\n`\n"], "outputs": ["a\n", "b\n", "e\n", "e\n", "a\n", "b\n", "f\n", "`\n"]}
| 316
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
You are given a tree of $N$ nodes rooted at node $1$. Each node $u$ initially has a positive value $a_{u}$ associated with it.
You randomly choose one node in the tree and change its value to zero. If any node $u$ has a value equal to $0$, a random node $v$ will be chosen in the subtree of $u$ ($u \neq v$), and the values $a_{u}$ and $a_{v}$ will be swapped, hence the value of the node $v$ will become zero and the process will continue until the zero is on a leaf node. At this point, we will call the tree a final tree.
Note: there is exactly one node with a value equal to zero in a final tree and that node is a leaf node.
Your task is to tell how many different final trees are possible. Two final trees are different if there exists at least one node $u$, such that the value of the node $u$ in one final tree differs from the value of the node $u$ in the other final tree. Since the answer can be large, print it modulo $10^{9} +7$.
------ Input ------
The first line contains an integer $T$, the number of test cases. Then the test cases follow.
The first line of each test case contains an integer $N$, the number of nodes.
Each of the next $N-1$ lines contains two integers $u$ and $v$, denoting that there is an undirected edge between the node $u$ and the node $v$.
The next line contains $N$ integers $a_{1},\ldots, a_{N}$.
------ Output ------
For each test case, output on a new line, number of different final trees possible, modulo $10^{9}+7$.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ u, v ≤ N$
$1≤ a_{i}≤ 10^{9}$
The edges form a tree structure.
The sum of $N$ over all test cases does not exceed $10^{6}$.
------ Sample Input ------
3
1
1
4
1 2
2 3
2 4
1 1 1 2
7
1 2
1 3
2 4
2 5
3 6
3 7
1 2 3 4 1 2 1
------ Sample Output ------
1
4
14
------ Explanation ------
For the first test case, the only possible final tree is when node $1$ is chosen and its value becomes 0.
For the second test case, the four possible final trees are:
- [ 1, 1, 1, 0 ]
- [ 1, 1, 0, 2 ]
- [ 1, 2, 1, 0 ]
- [ 2, 1, 1, 0 ]
( The $i$-th number in the array denotes the value of node $i$ in the final tree).
|
{"inputs": ["3\n1\n1\n4\n1 2\n2 3\n2 4\n1 1 1 2\n7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n1 2 3 4 1 2 1\n"], "outputs": ["1\n4\n14\n"]}
| 716
| 85
|
coding
|
Solve the programming task below in a Python markdown code block.
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,S is smaller. Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.
Constraints
* 4 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq 10^9
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.
Examples
Input
5
3 2 4 1 2
Output
2
Input
10
10 71 84 33 6 47 23 25 52 64
Output
36
Input
7
1 2 3 1000000000 4 5 6
Output
999999994
|
{"inputs": ["5\n3 2 4 2 2", "5\n5 2 4 2 1", "5\n4 3 4 2 1", "5\n2 1 6 0 2", "5\n2 3 6 2 1", "5\n2 1 7 0 2", "5\n2 1 7 0 0", "5\n8 0 2 2 0"], "outputs": ["2\n", "3\n", "1\n", "5\n", "4\n", "6\n", "7\n", "8\n"]}
| 311
| 142
|
coding
|
Solve the programming task below in a Python markdown code block.
We need a function (for commercial purposes) that may perform integer partitions with some constraints.
The function should select how many elements each partition should have.
The function should discard some "forbidden" values in each partition.
So, create ```part_const()```, that receives three arguments.
```part_const((1), (2), (3))```
```
(1) - The integer to be partitioned
(2) - The number of elements that each partition should have
(3) - The "forbidden" element that cannot appear in any partition
```
```part_const()``` should output the amount of different integer partitions with the constraints required.
Let's see some cases:
```python
part_const(10, 3, 2) ------> 4
/// we may have a total of 8 partitions of three elements (of course, the sum of the elements of each partition should be equal 10) :
[1, 1, 8], [1, 2, 7], [1, 3, 6], [1, 4, 5], [2, 2, 6], [2, 3, 5], [2, 4, 4], [3, 3, 4]
but 2 is the forbidden element, so we have to discard [1, 2, 7], [2, 2, 6], [2, 3, 5] and [2, 4, 4]
So the obtained partitions of three elements without having 2 in them are:
[1, 1, 8], [1, 3, 6], [1, 4, 5] and [3, 3, 4] (4 partitions)///
```
```part_const()``` should have a particular feature:
if we introduce ```0``` as the forbidden element, we will obtain the total amount of partitions with the constrained number of elements.
In fact,
```python
part_const(10, 3, 0) ------> 8 # The same eight partitions that we saw above.
```
Enjoy it and happy coding!!
Also feel free to reuse/extend the following starter code:
```python
def part_const(n, k, num):
```
|
{"functional": "_inputs = [[10, 3, 2], [10, 3, 0], [10, 4, 1], [10, 5, 3], [15, 5, 3], [15, 5, 4], [15, 3, 3]]\n_outputs = [[4], [8], [2], [4], [15], [19], [13]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(part_const(*i), o[0])"}
| 491
| 242
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given 3 numbers a, b and x. You need to output the multiple of x which is closest to a^{b}. If more than one answer exists , display the smallest one.
Input Format
The first line contains T, the number of testcases.
T lines follow, each line contains 3 space separated integers (a, b and x respectively)
Constraints
1 ≤ T ≤ 10^{5}
1 ≤ x ≤ 10^{9}
0 < a^{b} ≤ 10^{9}
1 ≤ a ≤ 10^{9}
-10^{9} ≤ b ≤ 10^{9}
Output Format
For each test case , output the multiple of x which is closest to a^{b}
Sample Input 0
3
349 1 4
395 1 7
4 -2 2
Sample Output 0
348
392
0
Explanation 0
The closest multiple of 4 to 349 is 348.
The closest multiple of 7 to 395 is 392.
The closest multiple of 2 to 1/16 is 0.
|
{"inputs": ["3\n349 1 4\n395 1 7\n4 -2 2\n"], "outputs": ["348\n392\n0\n"]}
| 277
| 44
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given two strings s and t, return the number of distinct subsequences of s which equals t.
The test cases are generated so that the answer fits on a 32-bit signed integer.
Please complete the following python code precisely:
```python
class Solution:
def numDistinct(self, s: str, t: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"rabbbit\", t = \"rabbit\") == 3\n assert candidate(s = \"babgbag\", t = \"bag\") == 5\n\n\ncheck(Solution().numDistinct)"}
| 90
| 57
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.