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.
Problem statement
Jota knows only the characters from `a` to` z` and the addition symbol `+`. Jota has an older sister, Tachiko. In addition to that, Tatsuko knows multiplication `*`, parentheses `(`, `)`, and integers (numbers) between `1` and` 9`. However, I don't know how to use multiple parentheses and how to multiply inside parentheses.
For example, if you have the following formula:
1. `a + a + a`
2. `a + 4 * (b + c)`
3. `a + 3 * (b + 2 * (c + d))`
4. `a-b`
5. `a / b`
6. `11 * a`
Of these, Jota-kun can write only 1., and Tachiko-san can write 1. and 2. Neither of 3 to 6 can write.
One day, Jota wrote a polynomial $ S $ with a length of $ n $ as a string. Tachiko, a short coder, wants to rewrite $ S $ as a string into a shorter identity polynomial $ T $, using multiplication, parentheses, and integers greater than or equal to `1` and less than or equal to` 9`. .. But it doesn't seem that easy.
Now, instead of Mr. Tachiko, write a program that creates the shortest $ T $ as a character string and outputs that length.
Constraint
$ 1 \ leq N \ leq 499 $
$ N $ is odd
$ S $ consists only of lowercase letters from `a` to` z` and `+`
The first letter of $ S $ is the alphabet, followed by the alternating `+` and $ 1 $ letters of the alphabet.
$ S $ contains less than $ 9 $ of the same alphabet
sample
Sample input 1
Five
a + a + a
Sample output 1
3
The $ 3 $ character of `a * 3` is the shortest.
Sample input 2
9
a + a + b + a + b
Sample output 2
7
The $ 7 $ character of `a * 3 + 2 * b` or` a * 3 + b + b` is the shortest.
Sample input 3
11
a + a + b + a + b + b
Sample output 3
7
$ 7 $ character in `(a + b) * 3`
Sample input 4
3
a + a
Sample output 4
3
$ 3 $ character with `a + a` or` a * 2`
input
$ N $
$ S $
output
Print the answer on the $ 1 $ line.
Example
Input
5
a+a+a
Output
3
|
{"inputs": ["5\nb+a+a", "5\nb+b+b", "5\na+a+b", "5\na+b+b", "5\nb+b+a", "5\nc+a+a", "5\nb+a+b", "5\nc+a+b"], "outputs": ["5\n", "3\n", "5\n", "5\n", "5\n", "5\n", "5\n", "5\n"]}
| 617
| 94
|
coding
|
Solve the programming task below in a Python markdown code block.
Call a number stepping if adjacent digits, as well as the first and last digits, differ by one. How many n-digit base 11 stepping numbers are there? Give your answer modulo 4294967143.
For example, 8789A9 is a 6-digit base 11 stepping number. 9A0A and 234 are not stepping.
Input
The first line contains an integer T, the number of test cases (about 20000). Each of the next T lines contains an integer n (2≤ n < 264).
Output
Output the answer to each test case on a separate line.
SAMPLE INPUT
4
2
4
6
10
SAMPLE OUTPUT
19
54
171
1958
|
{"inputs": ["4\n2\n4\n6\n10"], "outputs": ["19\n54\n171\n1958"]}
| 188
| 34
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two integer arrays nums1 and nums2 sorted in non-decreasing order and an integer k.
Define a pair (u, v) which consists of one element from the first array and one element from the second array.
Return the k pairs (u1, v1), (u2, v2), ..., (uk, vk) with the smallest sums.
Please complete the following python code precisely:
```python
class Solution:
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
```
|
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,7,11], nums2 = [2,4,6], k = 3) == [[1,2],[1,4],[1,6]]\n assert candidate(nums1 = [1,1,2], nums2 = [1,2,3], k = 2) == [[1,1],[1,1]]\n\n\ncheck(Solution().kSmallestPairs)"}
| 140
| 106
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an array pairs, where pairs[i] = [xi, yi], and:
There are no duplicates.
xi < yi
Let ways be the number of rooted trees that satisfy the following conditions:
The tree consists of nodes whose values appeared in pairs.
A pair [xi, yi] exists in pairs if and only if xi is an ancestor of yi or yi is an ancestor of xi.
Note: the tree does not have to be a binary tree.
Two ways are considered to be different if there is at least one node that has different parents in both ways.
Return:
0 if ways == 0
1 if ways == 1
2 if ways > 1
A rooted tree is a tree that has a single root node, and all edges are oriented to be outgoing from the root.
An ancestor of a node is any node on the path from the root to that node (excluding the node itself). The root has no ancestors.
Please complete the following python code precisely:
```python
class Solution:
def checkWays(self, pairs: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(pairs = [[1,2],[2,3]]) == 1\n assert candidate(pairs = [[1,2],[2,3],[1,3]]) == 2\n assert candidate(pairs = [[1,2],[2,3],[2,4],[1,5]]) == 0\n\n\ncheck(Solution().checkWays)"}
| 243
| 89
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a string s and an integer k, partition s into k substrings such that the letter changes needed to make each substring a semi-palindrome are minimized.
Return the minimum number of letter changes required.
A semi-palindrome is a special type of string that can be divided into palindromes based on a repeating pattern. To check if a string is a semi-palindrome:
Choose a positive divisor d of the string's length. d can range from 1 up to, but not including, the string's length. For a string of length 1, it does not have a valid divisor as per this definition, since the only divisor is its length, which is not allowed.
For a given divisor d, divide the string into groups where each group contains characters from the string that follow a repeating pattern of length d. Specifically, the first group consists of characters at positions 1, 1 + d, 1 + 2d, and so on; the second group includes characters at positions 2, 2 + d, 2 + 2d, etc.
The string is considered a semi-palindrome if each of these groups forms a palindrome.
Consider the string "abcabc":
The length of "abcabc" is 6. Valid divisors are 1, 2, and 3.
For d = 1: The entire string "abcabc" forms one group. Not a palindrome.
For d = 2:
Group 1 (positions 1, 3, 5): "acb"
Group 2 (positions 2, 4, 6): "bac"
Neither group forms a palindrome.
For d = 3:
Group 1 (positions 1, 4): "aa"
Group 2 (positions 2, 5): "bb"
Group 3 (positions 3, 6): "cc"
All groups form palindromes. Therefore, "abcabc" is a semi-palindrome.
Please complete the following python code precisely:
```python
class Solution:
def minimumChanges(self, s: str, k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"abcac\", k = 2) == 1\n assert candidate(s = \"abcdef\", k = 2) == 2\n assert candidate(s = \"aabbaa\", k = 3) == 0\n\n\ncheck(Solution().minimumChanges)"}
| 464
| 73
|
coding
|
Solve the programming task below in a Python markdown code block.
There are two groups, one of $N$ boys and the other of $N$ girls numbered from $1$ to $N$.
You are given two arrays $A$ and $B$ containing $N$ numbers each, denoting the height of boys and girls in the group.
You have to form $N$ couples, where each couple will consist of $1$ boy and $1$ girl.
Each couple has a LIKENESS VALUE, where
LIKENESS VALUE = height of girl in the couple + height of boy in that couple.
You have to form $N$ couples in such a way that the maximum of LIKENESS VALUE of all the couples is minimum.
Note:- No boy or girl can form more than one couple.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer $N$:- number of boys and number of girls in the group.
The second line of each test case contains $N$ space-separated integers, denoting the height of $N$ boys.
The third line of each test case contains $N$ space-separated integers, denoting the height of $N$ girls.
------ Output ------
For each test case, print the maximum LIKENESS VALUE in a new line.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 2*10^{4}$
$1 ≤ A_{i}, B_{i} ≤ 10^{9}$ , for all $1 ≤ i ≤ N $
----- Sample Input 1 ------
1
3
4 5 1
2 2 2
----- Sample Output 1 ------
7
----- explanation 1 ------
You can form couples of boy and girl with index pair $(1,1), (2,2), (3,3)$. So, the Likeness value of the three couples will be $(4+2), (5+2), (1+2) = 6,7,3$. The maximum value will be $7$ and there are no other ways that can minimize the maximum value$(7)$.
|
{"inputs": ["1\n3\n4 5 1\n2 2 2"], "outputs": ["7"]}
| 482
| 26
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
We are playing the Guessing Game. The game will work as follows:
I pick a number between 1 and n.
You guess a number.
If you guess the right number, you win the game.
If you guess the wrong number, then I will tell you whether the number I picked is higher or lower, and you will continue guessing.
Every time you guess a wrong number x, you will pay x dollars. If you run out of money, you lose the game.
Given a particular n, return the minimum amount of money you need to guarantee a win regardless of what number I pick.
Please complete the following python code precisely:
```python
class Solution:
def getMoneyAmount(self, n: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 10) == 16\n assert candidate(n = 1) == 0\n assert candidate(n = 2) == 1\n\n\ncheck(Solution().getMoneyAmount)"}
| 180
| 58
|
coding
|
Solve the programming task below in a Python markdown code block.
You have a tree of $n$ vertices. You are going to convert this tree into $n$ rubber bands on infinitely large plane. Conversion rule follows:
For every pair of vertices $a$ and $b$, rubber bands $a$ and $b$ should intersect if and only if there is an edge exists between $a$ and $b$ in the tree.
Shape of rubber bands must be a simple loop. In other words, rubber band is a loop which doesn't self-intersect.
Now let's define following things:
Rubber band $a$ includes rubber band $b$, if and only if rubber band $b$ is in rubber band $a$'s area, and they don't intersect each other.
Sequence of rubber bands $a_{1}, a_{2}, \ldots, a_{k}$ ($k \ge 2$) are nested, if and only if for all $i$ ($2 \le i \le k$), $a_{i-1}$ includes $a_{i}$.
This is an example of conversion. Note that rubber bands $5$ and $6$ are nested.
It can be proved that is it possible to make a conversion and sequence of nested rubber bands under given constraints.
What is the maximum length of sequence of nested rubber bands can be obtained from given tree? Find and print it.
-----Input-----
The first line contains integer $n$ ($3 \le n \le 10^{5}$) — the number of vertices in tree.
The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$) — it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices.
-----Output-----
Print the answer.
-----Examples-----
Input
6
1 3
2 3
3 4
4 5
4 6
Output
4
Input
4
1 2
2 3
3 4
Output
2
-----Note-----
In the first sample, you can obtain a nested sequence of $4$ rubber bands($1$, $2$, $5$, and $6$) by the conversion shown below. Of course, there are other conversions exist to make a nested sequence of $4$ rubber bands. However, you cannot make sequence of $5$ or more nested rubber bands with given tree.
You can see one of the possible conversions for the second sample below.
|
{"inputs": ["3\n1 3\n2 3\n", "3\n1 3\n2 3\n", "3\n1 2\n2 3\n", "4\n1 2\n2 3\n3 4\n", "4\n1 2\n2 4\n3 4\n", "4\n1 3\n2 4\n3 4\n", "4\n1 2\n1 4\n3 4\n", "4\n1 4\n2 4\n3 4\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "3\n"]}
| 559
| 154
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef wants to make a feast. In order to do that, he needs a lot of different ingredients. Each ingredient has a certain tastiness; the tastiness of each ingredient may be any positive integer. Initially, for each tastiness between $K$ and $K+N-1$ (inclusive), Chef has an infinite supply of ingredients with this tastiness.
The ingredients have a special property: any two of them can be mixed to create a new ingredient. If the original ingredients had tastiness $x$ and $y$ (possibly $x = y$), the new ingredient has tastiness $x+y$. The ingredients created this way may be used to mix other ingredients as well. Chef is free to mix ingredients in any way he chooses any number of times.
Let's call a tastiness $v$ ($v > 0$) unreachable if there is no way to obtain an ingredient with tastiness $v$; otherwise, tastiness $v$ is reachable. Chef wants to make ingredients with all reachable values of tastiness and he would like to know the number of unreachable values. Help him solve this problem. Since the answer may be large, compute it modulo $1,000,000,007$ ($10^9+7$).
Note that there is an infinite number of reachable values of tastiness, but it can be proven that the number of unreachable values is always finite for $N \ge 2$.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $N$ and $K$.
-----Output-----
For each test case, print a single line containing one integer — the number of unreachable values of tastiness, modulo $1,000,000,007$.
-----Constraints-----
- $1 \le T \le 10^5$
- $2 \le N \le 10^{18}$
- $1 \le K \le 10^{18}$
-----Subtasks-----
Subtask #1 (20 points): $N = 2$
Subtask #2 (80 points): original constraints
-----Example Input-----
2
2 1
3 3
-----Example Output-----
0
2
-----Explanation-----
Example case 1: It is possible to obtain ingredients with all values of tastiness.
Example case 2: Ingredients with tastiness $1$ and $2$ cannot be made.
|
{"inputs": ["2\n2 1\n3 3"], "outputs": ["0\n2"]}
| 556
| 22
|
coding
|
Solve the programming task below in a Python markdown code block.
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya was delivered a string s, containing only digits. He needs to find a string that
* represents a lucky number without leading zeroes,
* is not empty,
* is contained in s as a substring the maximum number of times.
Among all the strings for which the three conditions given above are fulfilled, Petya only needs the lexicographically minimum one. Find this string for Petya.
Input
The single line contains a non-empty string s whose length can range from 1 to 50, inclusive. The string only contains digits. The string can contain leading zeroes.
Output
In the only line print the answer to Petya's problem. If the sought string does not exist, print "-1" (without quotes).
Examples
Input
047
Output
4
Input
16
Output
-1
Input
472747
Output
7
Note
The lexicographical comparison of strings is performed by the < operator in the modern programming languages. String x is lexicographically less than string y either if x is a prefix of y, or exists such i (1 ≤ i ≤ min(|x|, |y|)), that xi < yi and for any j (1 ≤ j < i) xj = yj. Here |a| denotes the length of string a.
In the first sample three conditions are fulfilled for strings "4", "7" and "47". The lexicographically minimum one is "4".
In the second sample s has no substrings which are lucky numbers.
In the third sample the three conditions are only fulfilled for string "7".
|
{"inputs": ["7\n", "4\n", "5\n", "9\n", "8\n", "2\n", "3\n", "0\n"], "outputs": ["7\n", "4\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
| 417
| 70
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given two strings a and b, return the length of the longest uncommon subsequence between a and b. If no such uncommon subsequence exists, return -1.
An uncommon subsequence between two strings is a string that is a subsequence of exactly one of them.
Please complete the following python code precisely:
```python
class Solution:
def findLUSlength(self, a: str, b: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(a = \"aaa\", b = \"bbb\") == 3\n assert candidate(a = \"aaa\", b = \"aaa\") == -1\n\n\ncheck(Solution().findLUSlength)"}
| 107
| 55
|
coding
|
Solve the programming task below in a Python markdown code block.
Faizal and Sultan are having a one-on-one faceoff. Before stepping into the fight, each of them smoked a special kind of weed from Jhalwa. Smoking k grams of this weed grants the smoker the ability to resurrect k times after dying. Sultan smoked M grams of this weed, and Faizal smoked N grams.
However, Faizal was addicted to this kind of weed since his childhood, and so developed a side effect: if Faizal dies and is resurrected, he temporarily loses his ability to resurrect; and only gains it back after killing Sultan at least once (given that he hasn't resurrected N times already). Sultan has no such restriction.
The fight ends whenever one of the two is killed without being able to resurrect. In other words, either Sultan is killed M+1 times, or Faizal is killed N+1 times, or Faizal is killed twice in a row.
Any possible fight can be described by the sequence of deaths - for example, with N = 2 and M = 2, the string FSSFS encodes the fight where:
1. First, Sultan kills Faizal. Faizal resurrects, and temporarily loses his ability to resurrect.
2. Then, Faizal kills Sultan. Sultan resurrects, and Faizal's resurrection is now active.
3. Faizal kills Sultan again. Sultan resurrects.
4. Sultan kills Faizal, who resurrects. Now Sultan has no more chances to revive.
5. Faizal kills Sultan, who stays dead; and the fight ends.
Faizal's wife Mohsina is getting anxious about the fight and asks for your help in finding the number of different ways the fight between Faizal and Sultan can end up happening. Because this value can be quite large, compute it modulo 10^{9} + 7.
Two fights are considered different if and only if the sequence of deaths corresponding to those fights are different. For example, the sequence SF is different from the sequence FS and hence these represent different fights.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- Each test case consists of a single line of input, containing two space-separated integers N and M.
------ Output Format ------
For each test case, output a single line containing the answer - the possible number of fights between Faizal and Sultan, modulo 10^{9} + 7.
------ Constraints ------
$1 ≤ T ≤ 2000$
$0 ≤ N ≤ 10^{5}$
$0 ≤ M ≤ 10^{5}$
- Sum of $N$ over all test cases will not exceed $10^{6}$
- Sum of $M$ over all test cases will not exceed $10^{6}$
----- Sample Input 1 ------
3
2 1
12 6
6969 8563
----- Sample Output 1 ------
7
255
988402422
----- explanation 1 ------
Test Case 1: The death sequences corresponding to the $7$ possible fights are:
- SS
- SFS
- FSS
- FSFS
- FF
- SFF
- FSFF
|
{"inputs": ["3\n2 1\n12 6\n6969 8563"], "outputs": ["7\n255\n988402422\n"]}
| 711
| 46
|
coding
|
Solve the programming task below in a Python markdown code block.
```if:python,php
In this kata you will have to write a function that takes `litres` and `price_per_litre` as arguments. Purchases of 2 or more litres get a discount of 5 cents per litre, purchases of 4 or more litres get a discount of 10 cents per litre, and so on every two litres, up to a maximum discount of 25 cents per litre. But total discount per litre cannot be more than 25 cents. Return the toal cost rounded to 2 decimal places. Also you can guess that there will not be negative or non-numeric inputs.
Good Luck!
```
```if:csharp,java,javascript
In this kata you will have to write a function that takes `litres` and `pricePerLitre` as arguments. Purchases of 2 or more litres get a discount of 5 cents per litre, purchases of 4 or more litres get a discount of 10 cents per litre, and so on every two litres, up to a maximum discount of 25 cents per litre. But total discount per litre cannot be more than 25 cents. Return the toal cost rounded to 2 decimal places. Also you can guess that there will not be negative or non-numeric inputs.
Good Luck!
```
Also feel free to reuse/extend the following starter code:
```python
def fuel_price(litres, price_per_liter):
```
|
{"functional": "_inputs = [[10, 21.5], [40, 10], [15, 5.83]]\n_outputs = [[212.5], [390], [83.7]]\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(fuel_price(*i), o[0])"}
| 315
| 195
|
coding
|
Solve the programming task below in a Python markdown code block.
[Haikus](https://en.wikipedia.org/wiki/Haiku_in_English) are short poems in a three-line format, with 17 syllables arranged in a 5–7–5 pattern. Your task is to check if the supplied text is a haiku or not.
### About syllables
[Syllables](https://en.wikipedia.org/wiki/Syllable) are the phonological building blocks of words. *In this kata*, a syllable is a part of a word including a vowel ("a-e-i-o-u-y") or a group of vowels (e.g. "ou", "ee", "ay"). A few examples: "tea", "can", "to·day", "week·end", "el·e·phant".
**However**, silent "E"s **do not** create syllables. *In this kata*, an "E" is considered silent if it's alone at the end of the word, preceded by one (or more) consonant(s) and there is at least one other syllable in the word. Examples: "age", "ar·range", "con·crete"; but not in "she", "blue", "de·gree".
Some more examples:
* one syllable words: "cat", "cool", "sprout", "like", "eye", "squeeze"
* two syllables words: "ac·count", "hon·est", "beau·ty", "a·live", "be·cause", "re·store"
## Examples
```
An old silent pond...
A frog jumps into the pond,
splash! Silence again.
```
...should return `True`, as this is a valid 5–7–5 haiku:
```
An old si·lent pond... # 5 syllables
A frog jumps in·to the pond, # 7
splash! Si·lence a·gain. # 5
```
Another example:
```
Autumn moonlight -
a worm digs silently
into the chestnut.
```
...should return `False`, because the number of syllables per line is not correct:
```
Au·tumn moon·light - # 4 syllables
a worm digs si·lent·ly # 6
in·to the chest·nut. # 5
```
---
## My other katas
If you enjoyed this kata then please try [my other katas](https://www.codewars.com/collections/katas-created-by-anter69)! :-)
Also feel free to reuse/extend the following starter code:
```python
def is_haiku(text):
```
|
{"functional": "_inputs = [['An old silent pond...\\nA frog jumps into the pond,\\nsplash! Silence again.'], ['An old silent pond...\\nA frog jumps into the pond, splash!\\nSilence again.'], ['An old silent pond...\\nA frog jumps into the pond,\\nsplash!\\nSilence again.'], ['An old silent pond... A frog jumps into the pond, splash! Silence again.'], ['Autumn moonlight -\\na worm digs silently\\ninto the chestnut.'], [''], ['\\n\\n'], ['My code is cool, right?\\nJava # Pyhton ; Ruby // Go:\\nI know them all, yay! ;-)'], ['Edge case the urge come;\\nFurthermore eye the garage.\\nLike literature!'], ['a e i o u\\noo ee ay ie ey oa ie\\ny a e i o']]\n_outputs = [[True], [False], [False], [False], [False], [False], [False], [True], [True], [True]]\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_haiku(*i), o[0])"}
| 571
| 370
|
coding
|
Solve the programming task below in a Python markdown code block.
Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins.
Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.
Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 1000) — the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to 1000.
-----Output-----
On a single line, print two integers. The first number is the number of Sereja's points at the end of the game, the second number is the number of Dima's points at the end of the game.
-----Examples-----
Input
4
4 1 2 10
Output
12 5
Input
7
1 2 3 4 5 6 7
Output
16 12
-----Note-----
In the first sample Sereja will take cards with numbers 10 and 2, so Sereja's sum is 12. Dima will take cards with numbers 4 and 1, so Dima's sum is 5.
|
{"inputs": ["1\n3\n", "1\n1\n", "1\n1\n", "1\n3\n", "1\n2\n", "1\n4\n", "1\n0\n", "4\n4 1 2 10\n"], "outputs": ["3 0\n", "1 0\n", "1 0\n", "3 0\n", "2 0\n", "4 0\n", "0 0\n", "12 5\n"]}
| 403
| 110
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:
pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i].
Note that ^ denotes the bitwise-xor operation.
It can be proven that the answer is unique.
Please complete the following python code precisely:
```python
class Solution:
def findArray(self, pref: List[int]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(pref = [5,2,0,3,1]) == [5,7,2,3,2]\n assert candidate(pref = [13]) == [13]\n\n\ncheck(Solution().findArray)"}
| 111
| 63
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian as well.
You are given a function f which is defined as :
Your task is to find the value of
where M is given in input.
------ Input Format ------
First line contains T, the number of test cases.
First line of each test case contain 3 space separated integers N, M and Q.
Next Q line follows, each line contain r.
------ Output Format ------
For each test case, output Q lines, each line containing the required answer.
------ Constraints ------
2 ≤ N ≤ 10^{6}
1 ≤ M ≤ 10^{9}
2 ≤ Sum of N over all test cases ≤ 10^{6}
1 ≤ Sum of Q over all test cases ≤ 2*10^{5}
1 ≤ T ≤ 10^{5}
1 < r < N
Sample Input
2
5 114 1
2
50 3874 3
31
17
21
Sample Output
72
3718
624
1144
Explanation for first test case
f[1] = 1
f[2] = 2
f[3] = 1*2^{2} * 3 = 12
f[4] =1*2^{3}*3^{2}*4 = 8*9*4 = 288
f[5] = 1*2^{4}*3^{3}*4^{2}*5 =34560
value of f[5] / (f[2]*f[3]) = 1440 and 1440 %114 is 72
|
{"inputs": ["2\n5 114 1\n2\n50 3874 3\n31\n17\n21"], "outputs": ["72\n3718\n624\n1144"]}
| 381
| 56
|
coding
|
Solve the programming task below in a Python markdown code block.
Given an array A having N elements, a subarray S of A is called good if XOR(S) ≥ K, where XOR(S) denotes the [bitwise XOR] of all the elements of the subarray S.
Find the length of the smallest subarray S which is good. If there is no good subarray, print -1 instead.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of the T test cases follows.
- The first line of each test case contains two space-separated integers N and K - as described in the problem statement.
- The second line of each test case contains N space-separated integers A_{1}, A_{2},..., A_{N}, A_{i} representing the elements of the array A.
------ Output Format ------
For each test case, output in a single line, the length of the shortest subarray which is good. If there is no good subarray, print -1.
------ Constraints ------
$1 ≤ T ≤ 2 \cdot 10^{4}$
$1 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
$0 ≤ K < 2^{31}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
5 15
1 4 2 8 1
5 7
1 4 2 8 1
5 20
1 4 2 8 1
----- Sample Output 1 ------
4
1
-1
----- explanation 1 ------
Test case $1$:
For $S = [A_{1}, A_{2}, A_{3}, A_{4}]$, $XOR(S) = 1\oplus 4\oplus 2\oplus 8 = 15$.
Similarly, for $S = [A_{2}, A_{3}, A_{4}, A_{5}]$, $XOR(S) = 4\oplus 2\oplus 8\oplus 1 = 15$.
These are the only two subarrays which are good. The length of these subarrays is $4$. It can be proven that no subarray of length less than $4$ is good.
Test case $2$: There are several good subarrays. Some examples are $S = [A_{4}]$, $S = [A_{1}, A_{2}, A_{3}]$,
and $S = [A_{3}, A_{4}]$. The length of the smallest subarray is $1$, and that subarray is $[A_{4}]$. It can be proven that no subarray of length less than $1$ is good.
Test case $3$: There is no subarray $S$ such that $XOR(S) ≥ 20$. Hence, there is no good subarray.
|
{"inputs": ["3\n5 15\n1 4 2 8 1\n5 7\n1 4 2 8 1\n5 20\n1 4 2 8 1"], "outputs": ["4\n1\n-1"]}
| 657
| 61
|
coding
|
Solve the programming task below in a Python markdown code block.
Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had c pages.
At first day Mister B read v_0 pages, but after that he started to speed up. Every day, starting from the second, he read a pages more than on the previous day (at first day he read v_0 pages, at second — v_0 + a pages, at third — v_0 + 2a pages, and so on). But Mister B is just a human, so he physically wasn't able to read more than v_1 pages per day.
Also, to refresh his memory, every day, starting from the second, Mister B had to reread last l pages he read on the previous day. Mister B finished the book when he read the last page for the first time.
Help Mister B to calculate how many days he needed to finish the book.
-----Input-----
First and only line contains five space-separated integers: c, v_0, v_1, a and l (1 ≤ c ≤ 1000, 0 ≤ l < v_0 ≤ v_1 ≤ 1000, 0 ≤ a ≤ 1000) — the length of the book in pages, the initial reading speed, the maximum reading speed, the acceleration in reading speed and the number of pages for rereading.
-----Output-----
Print one integer — the number of days Mister B needed to finish the book.
-----Examples-----
Input
5 5 10 5 4
Output
1
Input
12 4 12 4 1
Output
3
Input
15 1 100 0 0
Output
15
-----Note-----
In the first sample test the book contains 5 pages, so Mister B read it right at the first day.
In the second sample test at first day Mister B read pages number 1 - 4, at second day — 4 - 11, at third day — 11 - 12 and finished the book.
In third sample test every day Mister B read 1 page of the book, so he finished in 15 days.
|
{"inputs": ["1 1 1 0 0\n", "4 1 2 2 0\n", "1 5 5 1 1\n", "1 2 3 0 0\n", "1 1 1 1 0\n", "9 1 4 2 0\n", "4 2 2 0 1\n", "8 3 4 2 0\n"], "outputs": ["1\n", "3\n", "1\n", "1\n", "1\n", "4\n", "3\n", "3\n"]}
| 482
| 134
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given n item's value and label as two integer arrays values and labels. You are also given two integers numWanted and useLimit.
Your task is to find a subset of items with the maximum sum of their values such that:
The number of items is at most numWanted.
The number of items with the same label is at most useLimit.
Return the maximum sum.
Please complete the following python code precisely:
```python
class Solution:
def largestValsFromLabels(self, values: List[int], labels: List[int], numWanted: int, useLimit: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1) == 9\n assert candidate(values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2) == 12\n assert candidate(values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1) == 16\n\n\ncheck(Solution().largestValsFromLabels)"}
| 145
| 162
|
coding
|
Solve the programming task below in a Python markdown code block.
Round any given number to the closest 0.5 step
I.E.
```
solution(4.2) = 4
solution(4.3) = 4.5
solution(4.6) = 4.5
solution(4.8) = 5
```
Round **up** if number is as close to previous and next 0.5 steps.
```
solution(4.75) == 5
```
Also feel free to reuse/extend the following starter code:
```python
def solution(n):
```
|
{"functional": "_inputs = [[4.2], [4.25], [4.4], [4.6], [4.75], [4.8], [4.5], [4.55], [4.74], [4.74999999999], [4.74999999991]]\n_outputs = [[4], [4.5], [4.5], [4.5], [5], [5], [4.5], [4.5], [4.5], [4.5], [4.5]]\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(solution(*i), o[0])"}
| 131
| 275
|
coding
|
Solve the programming task below in a Python markdown code block.
International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Among all such valid abbreviations they choose the shortest one and announce it to be the abbreviation of this year's competition.
For example, the first three Olympiads (years 1989, 1990 and 1991, respectively) received the abbreviations IAO'9, IAO'0 and IAO'1, while the competition in 2015 received an abbreviation IAO'15, as IAO'5 has been already used in 1995.
You are given a list of abbreviations. For each of them determine the year it stands for.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of abbreviations to process.
Then n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits.
Output
For each abbreviation given in the input, find the year of the corresponding Olympiad.
Examples
Input
5
IAO'15
IAO'2015
IAO'1
IAO'9
IAO'0
Output
2015
12015
1991
1989
1990
Input
4
IAO'9
IAO'99
IAO'999
IAO'9999
Output
1989
1999
2999
9999
|
{"inputs": ["1\nIAO'001\n", "1\nIAO'089\n", "1\nIAO'000\n", "1\nIAO'2000\n", "1\nIAO'2015\n", "1\nIAO'1015\n", "1\nIAO'3015\n", "1\nIAO'1014\n"], "outputs": ["3001\n", "3089\n", "3000\n", "12000\n", "12015\n", "11015\n", "13015\n", "11014\n"]}
| 395
| 160
|
coding
|
Solve the programming task below in a Python markdown code block.
It's almost summertime, so Big Cat and Little Cat are getting in shape. They decide the core of their fitness plan is to start jogging every day.
Their city consists of $N$ intersections connected by $\mbox{M}$ bidirectional roads. The cats decide that their jogging route should be cyclic (i.e., starting and ending at the same intersection) and consist of $4$ different roads.
The cats also love exploring new places, so each day they want to choose a new route to jog on that is not equal to any of their previous routes. Two routes are considered to be equal if their sets of component roads are equal.
Given a map of the city, can you help our heroic cats determine the maximum number of days they can go jogging so that every route traveled is different?
Input Format
The first line contains a pair of space-separated integers, $N$ (the number of intersections) and $\mbox{M}$ (the number of roads), respectively.
Each line $\boldsymbol{i}$ of the $\mbox{M}$ subsequent lines contains a pair of space-separated integers, $X_i$ and $Y_i$, defining a bidirectional road connecting intersections $X_i$ and $Y_i$.
Constraints
$1\leq N\leq5\cdot10^4$
$1\leq M\leq10^5$
$1\leq X_i,Y_i\leq N$
Each bidirectional road connects $2$ distinct intersections (i.e., no road connects an intersection to itself).
Each pair of intersections is directly connected by no more than $1$ road.
Output Format
Print the maximum number of days for which the cats can go jogging without repeating a route.
Sample Input
4 6
1 2
2 3
3 4
4 1
1 3
2 4
Sample Output
3
Explanation
There are $3$ different routes:
$1\to2\to3\to4\to1$
$1\to3\to2\to4\to1$
$1\to2\to4\to3\to1$
Recall that each route is a set of intersections forming a cycle, so each unique route is the same regardless of which city on the route the cats start out at. Thus, we print $3$ (the number of routes) as our answer.
|
{"inputs": ["4 6\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n"], "outputs": ["3\n"]}
| 513
| 40
|
coding
|
Solve the programming task below in a Python markdown code block.
For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).
Constraints
* 1 ≤ |V| ≤ 100000
* 0 ≤ di ≤ 10000
* 0 ≤ |E| ≤ 500000
* There are no parallel edges
* There are no self-loops
Input
An edge-weighted graph G (V, E) and the source r.
|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1
|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.
si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.
Output
Print the costs of SSSP in the following format.
c0
c1
:
c|V|-1
The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.
Examples
Input
4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5
Output
0
1
3
4
Input
4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5
Output
3
0
2
INF
|
{"inputs": ["4 5 0\n0 1 1\n0 2 4\n1 2 2\n2 3 1\n1 3 2", "4 5 0\n0 1 1\n0 2 4\n1 2 0\n2 3 1\n1 3 5", "4 5 0\n0 1 1\n1 2 4\n1 1 0\n2 3 1\n1 3 2", "4 5 0\n0 1 1\n1 2 4\n1 1 0\n2 3 1\n0 3 2", "4 5 0\n0 1 1\n1 2 5\n1 1 0\n2 3 1\n0 3 2", "4 5 0\n0 2 1\n0 3 4\n1 2 2\n2 2 2\n1 3 2", "4 5 0\n0 2 2\n0 3 4\n1 3 2\n3 2 2\n1 3 2", "4 5 0\n0 2 2\n0 3 4\n0 3 2\n3 2 2\n1 3 2"], "outputs": ["0\n1\n3\n3\n", "0\n1\n1\n2\n", "0\n1\n5\n3\n", "0\n1\n5\n2\n", "0\n1\n6\n2\n", "0\nINF\n1\n4\n", "0\nINF\n2\n4\n", "0\nINF\n2\n2\n"]}
| 426
| 382
|
coding
|
Solve the programming task below in a Python markdown code block.
The Robot is in a rectangular maze of size n × m. Each cell of the maze is either empty or occupied by an obstacle. The Robot can move between neighboring cells on the side left (the symbol "L"), right (the symbol "R"), up (the symbol "U") or down (the symbol "D"). The Robot can move to the cell only if it is empty. Initially, the Robot is in the empty cell.
Your task is to find lexicographically minimal Robot's cycle with length exactly k, which begins and ends in the cell where the Robot was initially. It is allowed to the Robot to visit any cell many times (including starting).
Consider that Robot's way is given as a line which consists of symbols "L", "R", "U" and "D". For example, if firstly the Robot goes down, then left, then right and up, it means that his way is written as "DLRU".
In this task you don't need to minimize the length of the way. Find the minimum lexicographical (in alphabet order as in the dictionary) line which satisfies requirements above.
-----Input-----
The first line contains three integers n, m and k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10^6) — the size of the maze and the length of the cycle.
Each of the following n lines contains m symbols — the description of the maze. If the symbol equals to "." the current cell is empty. If the symbol equals to "*" the current cell is occupied by an obstacle. If the symbol equals to "X" then initially the Robot is in this cell and it is empty. It is guaranteed that the symbol "X" is found in the maze exactly once.
-----Output-----
Print the lexicographically minimum Robot's way with the length exactly k, which starts and ends in the cell where initially Robot is. If there is no such way, print "IMPOSSIBLE"(without quotes).
-----Examples-----
Input
2 3 2
.**
X..
Output
RL
Input
5 6 14
..***.
*...X.
..*...
..*.**
....*.
Output
DLDDLLLRRRUURU
Input
3 3 4
***
*X*
***
Output
IMPOSSIBLE
-----Note-----
In the first sample two cyclic ways for the Robot with the length 2 exist — "UD" and "RL". The second cycle is lexicographically less.
In the second sample the Robot should move in the following way: down, left, down, down, left, left, left, right, right, right, up, up, right, up.
In the third sample the Robot can't move to the neighboring cells, because they are occupied by obstacles.
|
{"inputs": ["1 1 1\nX\n", "1 1 1\nX\n", "1 1 2\nX\n", "1 1 1\nX\n", "1 1 2\nX\n", "1 2 2\nX.\n", "1 2 2\n.X\n", "1 2 2\nX*\n"], "outputs": ["IMPOSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "RL\n", "LR\n", "IMPOSSIBLE\n"]}
| 595
| 138
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left.
Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it.
Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it.
Q queries are given. We ask you to process them in order.
There are two kinds of queries. Their input format and description are as follows:
- 1 x: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone.
- 2 x: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone.
How many black stones are there on the grid after processing all Q queries?
-----Constraints-----
- 3 \leq N \leq 2\times 10^5
- 0 \leq Q \leq \min(2N-4,2\times 10^5)
- 2 \leq x \leq N-1
- Queries are pairwise distinct.
-----Input-----
Input is given from Standard Input in the following format:
N Q
Query_1
\vdots
Query_Q
-----Output-----
Print how many black stones there are on the grid after processing all Q queries.
-----Sample Input-----
5 5
1 3
2 3
1 4
2 2
1 2
-----Sample Output-----
1
After each query, the grid changes in the following way:
|
{"inputs": ["39 0", "630 0", "279 0", "328 0", "272 0", "173 0", "218 0", "674 0"], "outputs": ["1369", "394384", "76729", "106276", "72900", "29241", "46656", "451584"]}
| 419
| 119
|
coding
|
Solve the programming task below in a Python markdown code block.
In this kata you're expected to sort an array of 32-bit integers in ascending order of the number of **on** bits they have.
E.g Given the array **[7, 6, 15, 8]**
- 7 has **3 on** bits (000...0**111**)
- 6 has **2 on** bits (000...00**11**)
- 15 has **4 on** bits (000...**1111**)
- 8 has **1 on** bit (000...**1**000)
So the array in sorted order would be **[8, 6, 7, 15]**.
In cases where two numbers have the same number of bits, compare their real values instead.
E.g between 10 **(...1010)** and 12 **(...1100)**, they both have the same number of **on** bits '**2**' but the integer 10 is less than 12 so it comes first in sorted order.
Your task is to write the function `sortBybit()` that takes an array of integers and sort them as described above.
```if-not:haskell
Note: Your function should modify the input rather than creating a new array.
```
Also feel free to reuse/extend the following starter code:
```python
def sort_by_bit(arr):
```
|
{"functional": "_inputs = [[[3, 8, 3, 6, 5, 7, 9, 1]], [[9, 4, 5, 3, 5, 7, 2, 56, 8, 2, 6, 8, 0]]]\n_outputs = [[[1, 8, 3, 3, 5, 6, 9, 7]], [[0, 2, 2, 4, 8, 8, 3, 5, 5, 6, 9, 7, 56]]]\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(sort_by_bit(*i), o[0])"}
| 321
| 277
|
coding
|
Solve the programming task below in a Python markdown code block.
Vladik was bored on his way home and decided to play the following game. He took n cards and put them in a row in front of himself. Every card has a positive integer number not exceeding 8 written on it. He decided to find the longest subsequence of cards which satisfies the following conditions:
the number of occurrences of each number from 1 to 8 in the subsequence doesn't differ by more then 1 from the number of occurrences of any other number. Formally, if there are c_{k} cards with number k on them in the subsequence, than for all pairs of integers $i \in [ 1,8 ], j \in [ 1,8 ]$ the condition |c_{i} - c_{j}| ≤ 1 must hold. if there is at least one card with number x on it in the subsequence, then all cards with number x in this subsequence must form a continuous segment in it (but not necessarily a continuous segment in the original sequence). For example, the subsequence [1, 1, 2, 2] satisfies this condition while the subsequence [1, 2, 2, 1] doesn't. Note that [1, 1, 2, 2] doesn't satisfy the first condition.
Please help Vladik to find the length of the longest subsequence that satisfies both conditions.
-----Input-----
The first line contains single integer n (1 ≤ n ≤ 1000) — the number of cards in Vladik's sequence.
The second line contains the sequence of n positive integers not exceeding 8 — the description of Vladik's sequence.
-----Output-----
Print single integer — the length of the longest subsequence of Vladik's sequence that satisfies both conditions.
-----Examples-----
Input
3
1 1 1
Output
1
Input
8
8 7 6 5 4 3 2 1
Output
8
Input
24
1 8 1 2 8 2 3 8 3 4 8 4 5 8 5 6 8 6 7 8 7 8 8 8
Output
17
-----Note-----
In the first sample all the numbers written on the cards are equal, so you can't take more than one card, otherwise you'll violate the first condition.
|
{"inputs": ["1\n8\n", "1\n8\n", "1\n7\n", "1\n6\n", "2\n5 4\n", "2\n5 4\n", "2\n5 1\n", "2\n5 2\n"], "outputs": ["1", "1", "1\n", "1\n", "2", "2", "2\n", "2\n"]}
| 520
| 90
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string s consisting of n lowercase Latin letters. You have to type this string using your keyboard.
Initially, you have an empty string. Until you type the whole string, you may perform the following operation: add a character to the end of the string.
Besides, at most once you may perform one additional operation: copy the string and append it to itself.
For example, if you have to type string abcabca, you can type it in 7 operations if you type all the characters one by one. However, you can type it in 5 operations if you type the string abc first and then copy it and type the last character.
If you have to type string aaaaaaaaa, the best option is to type 4 characters one by one, then copy the string, and then type the remaining character.
Print the minimum number of operations you need to type the given string.
-----Input-----
The first line of the input containing only one integer number n (1 ≤ n ≤ 100) — the length of the string you have to type. The second line containing the string s consisting of n lowercase Latin letters.
-----Output-----
Print one integer number — the minimum number of operations you need to type the given string.
-----Examples-----
Input
7
abcabca
Output
5
Input
8
abcdefgh
Output
8
-----Note-----
The first test described in the problem statement.
In the second test you can only type all the characters one by one.
|
{"inputs": ["1\na\n", "1\ns\n", "1\ns\n", "1\na\n", "1\nt\n", "1\n`\n", "2\naa\n", "2\naa\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "2\n", "2\n"]}
| 322
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
For an array of non-negative integers $a$ of size $n$, we construct another array $d$ as follows: $d_1 = a_1$, $d_i = |a_i - a_{i - 1}|$ for $2 \le i \le n$.
Your task is to restore the array $a$ from a given array $d$, or to report that there are multiple possible arrays.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 100$) — the size of the arrays $a$ and $d$.
The second line contains $n$ integers $d_1, d_2, \dots, d_n$ ($0 \le d_i \le 100$) — the elements of the array $d$.
It can be shown that there always exists at least one suitable array $a$ under these constraints.
-----Output-----
For each test case, print the elements of the array $a$, if there is only one possible array $a$. Otherwise, print $-1$.
-----Examples-----
Input
3
4
1 0 2 5
3
2 6 3
5
0 0 0 0 0
Output
1 1 3 8
-1
0 0 0 0 0
-----Note-----
In the second example, there are two suitable arrays: $[2, 8, 5]$ and $[2, 8, 11]$.
|
{"inputs": ["1\n2\n5 5\n", "1\n2\n1 1\n", "1\n2\n3 3\n", "1\n2\n2 2\n", "1\n2\n4 4\n", "1\n2\n7 7\n", "1\n2\n2 3\n", "1\n2\n6 6\n"], "outputs": ["-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "2 5\n", "-1\n"]}
| 373
| 121
|
coding
|
Solve the programming task below in a Python markdown code block.
------ Problem Statement ------
Write a program that accepts sets of three numbers, and prints the second-maximum number among the three.
------ Input ------
First line contains the number of triples, N.
The next N lines which follow each have three space separated integers.
------ Output ------
For each of the N triples, output one new line which contains the second-maximum integer among the three.
------ Constraints ------
$1 ≤ N ≤ 6$
$1 ≤ every integer ≤ 10000$
$The three integers in a single triplet are all distinct. That is, no two of them are equal.
----- Sample Input 1 ------
3
1 2 3
10 15 5
100 999 500
----- Sample Output 1 ------
2
10
500
|
{"inputs": ["3\n1 2 3\n10 15 5\n100 999 500"], "outputs": ["2\n10\n500"]}
| 189
| 45
|
coding
|
Solve the programming task below in a Python markdown code block.
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white.
There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white.
A white line is a row or a column without any black cells.
Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question.
-----Input-----
The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser.
The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell.
-----Output-----
Print one integer: the maximum number of white lines after using the eraser exactly once.
-----Examples-----
Input
4 2
BWWW
WBBW
WBBW
WWWB
Output
4
Input
3 1
BWB
WWB
BWB
Output
2
Input
5 3
BWBBB
BWBBB
BBBBB
BBBBB
WBBBW
Output
2
Input
2 2
BW
WB
Output
4
Input
2 1
WW
WW
Output
4
-----Note-----
In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW
WWWW
WWWW
WWWB
Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column.
In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line.
In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
|
{"inputs": ["1 1\nB\n", "1 1\nW\n", "1 1\nW\n", "1 1\nB\n", "2 2\nBW\nWB\n", "2 1\nWW\nWW\n", "2 1\nBB\nWW\n", "2 1\nBB\nBB\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "4\n", "4\n", "2\n", "0\n"]}
| 721
| 110
|
coding
|
Solve the programming task below in a Python markdown code block.
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 200$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $200$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer — the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
{"inputs": ["6\n8\n1 2 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n2 1\n3\n2 1 1\n", "6\n8\n1 1 1 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n3 1\n3\n3 1 2\n", "6\n8\n1 1 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 1\n1\n26\n2\n2 1\n3\n2 1 1\n", "6\n8\n1 1 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n2 1\n3\n2 1 1\n", "6\n8\n1 1 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n2 1\n3\n3 1 1\n", "6\n8\n1 1 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n3 1\n3\n3 1 1\n", "6\n8\n1 1 2 2 3 2 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n3 2\n3\n3 1 1\n", "6\n8\n1 1 2 2 3 1 1 1\n3\n1 3 1\n4\n1 2 3 2\n1\n26\n2\n3 1\n3\n3 1 1\n"], "outputs": ["6\n3\n3\n1\n1\n2\n", "6\n3\n3\n1\n1\n1\n", "7\n3\n3\n1\n1\n2\n", "7\n3\n3\n1\n1\n2\n", "7\n3\n3\n1\n1\n2\n", "7\n3\n3\n1\n1\n2\n", "7\n3\n3\n1\n1\n2\n", "6\n3\n3\n1\n1\n2\n"]}
| 757
| 590
|
coding
|
Solve the programming task below in a Python markdown code block.
We have a 3 \times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.
According to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.
Determine if he is correct.
-----Constraints-----
- c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3) is an integer between 0 and 100 (inclusive).
-----Input-----
Input is given from Standard Input in the following format:
c_{1,1} c_{1,2} c_{1,3}
c_{2,1} c_{2,2} c_{2,3}
c_{3,1} c_{3,2} c_{3,3}
-----Output-----
If Takahashi's statement is correct, print Yes; otherwise, print No.
-----Sample Input-----
1 0 1
2 1 2
1 0 1
-----Sample Output-----
Yes
Takahashi is correct, since there are possible sets of integers such as: a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1.
|
{"inputs": ["1 0 1\n4 1 2\n1 0 1", "1 8 6\n2 9 7\n1 7 7", "0 8 8\n0 8 8\n0 0 8", "2 2 2\n2 1 0\n2 2 2", "1 0 1\n4 1 2\n0 0 1", "1 8 6\n4 9 7\n1 7 7", "0 8 2\n0 8 8\n0 0 8", "1 0 1\n4 1 4\n0 0 1"], "outputs": ["No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
| 347
| 190
|
coding
|
Solve the programming task below in a Python markdown code block.
At the store, the salespeople want to make all prices round.
In this problem, a number that is a power of $10$ is called a round number. For example, the numbers $10^0 = 1$, $10^1 = 10$, $10^2 = 100$ are round numbers, but $20$, $110$ and $256$ are not round numbers.
So, if an item is worth $m$ bourles (the value of the item is not greater than $10^9$), the sellers want to change its value to the nearest round number that is not greater than $m$. They ask you: by how many bourles should you decrease the value of the item to make it worth exactly $10^k$ bourles, where the value of $k$ — is the maximum possible ($k$ — any non-negative integer).
For example, let the item have a value of $178$-bourles. Then the new price of the item will be $100$, and the answer will be $178-100=78$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases .
Each test case is a string containing a single integer $m$ ($1 \le m \le 10^9$) — the price of the item.
-----Output-----
For each test case, output on a separate line a single integer $d$ ($0 \le d < m$) such that if you reduce the cost of the item by $d$ bourles, the cost of the item will be the maximal possible round number. More formally: $m - d = 10^k$, where $k$ — the maximum possible non-negative integer.
-----Examples-----
Input
7
1
2
178
20
999999999
9000
987654321
Output
0
1
78
10
899999999
8000
887654321
-----Note-----
In the example:
$1 - 0 = 10^0$,
$2 - 1 = 10^0$,
$178 - 78 = 10^2$,
$20 - 10 = 10^1$,
$999999999 - 899999999 = 10^8$,
$9000 - 8000 = 10^3$,
$987654321 - 887654321 = 10^8$.
Note that in each test case, we get the maximum possible round number.
|
{"inputs": ["4\n1\n1\n1\n1\n", "1\n100000000\n", "14\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n", "7\n1\n2\n178\n20\n999999999\n9000\n987654321\n", "7\n100\n40000000\n89657\n326894\n2325566\n74444\n200055\n"], "outputs": ["0\n0\n0\n0\n", "0\n", "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n", "0\n1\n78\n10\n899999999\n8000\n887654321\n", "0\n30000000\n79657\n226894\n1325566\n64444\n100055\n"]}
| 651
| 284
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed m x n matrix grid consisting of positive integers.
You can start at any cell in the first column of the matrix, and traverse the grid in the following way:
From a cell (row, col), you can move to any of the cells: (row - 1, col + 1), (row, col + 1) and (row + 1, col + 1) such that the value of the cell you move to, should be strictly bigger than the value of the current cell.
Return the maximum number of moves that you can perform.
Please complete the following python code precisely:
```python
class Solution:
def maxMoves(self, grid: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]) == 3\n assert candidate(grid = [[3,2,4],[2,1,9],[1,1,7]]) == 0\n\n\ncheck(Solution().maxMoves)"}
| 172
| 93
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
For example, "Hello World", "HELLO", "hello world hello world" are all sentences.
Words consist of only uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.
A sentence is circular if:
The last character of a word is equal to the first character of the next word.
The last character of the last word is equal to the first character of the first word.
For example, "leetcode exercises sound delightful", "eetcode", "leetcode eats soul" are all circular sentences. However, "Leetcode is cool", "happy Leetcode", "Leetcode" and "I like Leetcode" are not circular sentences.
Given a string sentence, return true if it is circular. Otherwise, return false.
Please complete the following python code precisely:
```python
class Solution:
def isCircularSentence(self, sentence: str) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(sentence = \"leetcode exercises sound delightful\") == True\n assert candidate(sentence = \"eetcode\") == True\n assert candidate(sentence = \"Leetcode is cool\") == False\n\n\ncheck(Solution().isCircularSentence)"}
| 222
| 60
|
coding
|
Solve the programming task below in a Python markdown code block.
# Story&Task
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all houses in the city were built in one row.
Let's enumerate all the houses from left to right, starting with 0. A house is considered to be luxurious if the number of floors in it is strictly greater than in each house with larger number. In other words, a house is luxurious if the number of floors in it is strictly greater than in all houses, located to the right from it.
The new architect is interested in n questions, the ith of them is the following: "how many floors should be added to the ith house to make it luxurious?" (For each i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
# Input/Output
- `[input]` integer array `houses`
Array of positive integers, representing the number of floors in each house.
The ith element is the number of houses in the ith house.
`1 ≤ houses.length ≤ 1000`
- `[output]` an integer array
An array has the same length as input array, the ith element represents the number of floors that should be added to the ith house to make it luxurious.
# Example
For `houses = [1,2,3,1,2]`, the output should be `[3,2,0,2,0]`.
```
For houses[0], 3 floors should be added,
then its floors is strictly greater than all houses of right side.
For houses[1], 2 floors should be added.
For houses[2], no need to add floor
because it's already the luxurious.
For houses[3], 2 floors need to added
For houses[4], no house on the right,
so no need to add any floor.
```
Also feel free to reuse/extend the following starter code:
```python
def luxhouse(houses):
```
|
{"functional": "_inputs = [[[1, 2, 3, 1, 2]], [[3, 2, 1, 4]], [[1, 2, 3]], [[3, 2, 1]], [[1, 1, 1]]]\n_outputs = [[[3, 2, 0, 2, 0]], [[2, 3, 4, 0]], [[3, 2, 0]], [[0, 0, 0]], [[1, 1, 0]]]\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(luxhouse(*i), o[0])"}
| 466
| 257
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef got his dream seat in F1 and secured a 3^{rd} place in his debut race. He now wants to know the time gap between him and the winner of the race.
You are his chief engineer and you only know the time gap between Chef and the runner up of the race, given as A seconds, and the time gap between the runner up and the winner of the race, given as B seconds.
Please calculate and inform Chef about the time gap between him and the winner of the race.
------ 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 of input containing two space-separated integers A and B denoting the time gap between Chef and the runner up and the time gap between the runner up and the winner respectively.
------ Output Format ------
For each test case, output the time gap between Chef and the winner of the race.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ A, B ≤ 10$
----- Sample Input 1 ------
4
1 1
2 5
3 2
5 6
----- Sample Output 1 ------
2
7
5
11
----- explanation 1 ------
Test case $1$: The time gap between Chef and runner up is $1$ second. The time gap between runner up and the winner is $1$ second. Thus, the time gap between Chef and the winner is $1+1=2$ seconds.
Test case $2$: The time gap between Chef and runner up is $2$ seconds. The time gap between runner up and the winner is $5$ second. Thus, the time gap between Chef and the winner is $2+5=7$ seconds.
Test case $3$: The time gap between Chef and runner up is $3$ seconds. The time gap between runner up and the winner is $2$ second. Thus, the time gap between Chef and the winner is $3+2=5$ seconds.
Test case $4$: The time gap between Chef and runner up is $5$ seconds. The time gap between runner up and the winner is $6$ second. Thus, the time gap between Chef and the winner is $5+6=11$ seconds.
|
{"inputs": ["4\n1 1\n2 5\n3 2\n5 6\n"], "outputs": ["2\n7\n5\n11\n"]}
| 500
| 37
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given two 0-indexed integer arrays nums1 and nums2, each of length n, and a 1-indexed 2D array queries where queries[i] = [xi, yi].
For the ith query, find the maximum value of nums1[j] + nums2[j] among all indices j (0 <= j < n), where nums1[j] >= xi and nums2[j] >= yi, or -1 if there is no j satisfying the constraints.
Return an array answer where answer[i] is the answer to the ith query.
Please complete the following python code precisely:
```python
class Solution:
def maximumSumQueries(self, nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(nums1 = [4,3,1,2], nums2 = [2,4,9,5], queries = [[4,1],[1,3],[2,5]]) == [6,10,7]\n assert candidate(nums1 = [3,2,5], nums2 = [2,3,4], queries = [[4,4],[3,2],[1,1]]) == [9,9,9]\n assert candidate(nums1 = [2,1], nums2 = [2,3], queries = [[3,3]]) == [-1]\n\n\ncheck(Solution().maximumSumQueries)"}
| 178
| 150
|
coding
|
Solve the programming task below in a Python markdown code block.
In the online judge system, a judge file may include multiple datasets to check whether the submitted program outputs a correct answer for each test case. This task is to practice solving a problem with multiple datasets.
Write a program which reads an integer x and print it as is. Note that multiple datasets are given for this problem.
Constraints
* 1 ≤ x ≤ 10000
* The number of datasets ≤ 10000
Input
The input consists of multiple datasets. Each dataset consists of an integer x in a line.
The input ends with an integer 0. You program should not process (print) for this terminal symbol.
Output
For each dataset, print x in the following format:
Case i: x
where i is the case number which starts with 1. Put a single space between "Case" and i. Also, put a single space between ':' and x.
Example
Input
3
5
11
7
8
19
0
Output
Case 1: 3
Case 2: 5
Case 3: 11
Case 4: 7
Case 5: 8
Case 6: 19
|
{"inputs": ["3\n5\n9\n6\n2\n2\n0", "3\n5\n9\n6\n3\n2\n0", "3\n5\n9\n5\n3\n2\n0", "3\n5\n9\n1\n3\n2\n0", "3\n5\n9\n1\n4\n2\n0", "3\n5\n9\n2\n4\n2\n0", "3\n5\n9\n4\n2\n19\n0", "3\n5\n5\n3\n8\n19\n0"], "outputs": ["Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 6\nCase 5: 2\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 6\nCase 5: 3\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 5\nCase 5: 3\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 1\nCase 5: 3\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 1\nCase 5: 4\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 2\nCase 5: 4\nCase 6: 2\n", "Case 1: 3\nCase 2: 5\nCase 3: 9\nCase 4: 4\nCase 5: 2\nCase 6: 19\n", "Case 1: 3\nCase 2: 5\nCase 3: 5\nCase 4: 3\nCase 5: 8\nCase 6: 19\n"]}
| 264
| 482
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian.
Chef is judging a game called "Broken telephone". There are total N players taking part in the game. They are all sitting in a line. In the start of the game, first player is given a secret message written on a sheet of paper. Then they keep sending the message by whispering it to the player sitting immediate right to one and so on until it reaches the last person.
Finally, the message received by the last player is compared with the message said by first player. If these messages aren't equal, there is someone who has misheard the message or whispered it wrongly to the next player. If messages is equal, then the players win and receive a tasty chocolate.
Note that first player receives the message on a sheet of paper, thus he cannot mishear it.
As Chef wants to be sure that every player has fulfilled his/ her role in the game, so he asks everyone to state their received messages after the end of the game. You are given an array A of N integers denoting messages received by each person.
Please help Chef to find the number of players that could mishear the message or whisper it wrongly.
------ Input ------
The first line of the input contains an integer T denoting the number of test cases.
The first line of each test case contains a single integer N denoting the number of players
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N} denoting the messages of players.
------ Output ------
For each test case, output a single line containing an integer corresponding to the number of players that could mishear the message or whisper it wrongly.
------
------ Constraints -----
$1 ≤ T ≤ 5$
$1 ≤ A_{i} ≤ 10^{9}$
Subtask 1: 40 points
$2 ≤ N ≤ 10^{3}$
Subtask 2: 60 points
$2 ≤ N ≤ 10^{5}$
----- Sample Input 1 ------
3
7
1 1 1 3 3 3 2
5
1 3 1 1 1
4
5 5 5 5
----- Sample Output 1 ------
4
3
0
----- explanation 1 ------
Example 1: The 3-rd, 4-th, 6-th and 7-th player could mishear the message or whisper it wrongly.
Example 2: First 3 players could mishear the message or whisper it wrongly.
|
{"inputs": ["3\n7\n1 1 1 3 3 3 2\n5\n1 3 1 1 1\n4\n5 5 5 5", "3\n7\n1 1 1 3 3 3 2\n5\n1 3 1 1 2\n4\n5 5 5 5", "3\n7\n1 1 1 3 3 3 3\n5\n1 3 1 1 2\n4\n5 5 5 5", "3\n7\n1 1 1 3 3 3 2\n5\n1 0 1 1 1\n4\n5 5 5 5", "3\n7\n1 1 1 3 3 3 2\n5\n1 3 1 1 2\n4\n9 5 5 5", "3\n7\n1 1 1 3 3 3 3\n5\n1 3 1 1 2\n4\n5 5 9 5", "3\n7\n1 2 1 3 3 3 2\n5\n1 0 1 1 1\n4\n5 5 5 5", "3\n7\n1 1 1 3 3 3 3\n5\n1 3 1 1 2\n4\n9 5 5 5"], "outputs": ["4\n3\n0", "4\n5\n0\n", "2\n5\n0\n", "4\n3\n0\n", "4\n5\n2\n", "2\n5\n3\n", "6\n3\n0\n", "2\n5\n2\n"]}
| 547
| 397
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef calls a sequence *good* if it does not contain any two adjacent identical elements.
Initially, Chef has a sequence $a_{1}, a_{2}, \ldots, a_{N}$. He likes to change the sequence every now and then. You should process $Q$ queries. In each query, Chef chooses an index $x$ and changes the $x$-th element of the sequence to $y$. After each query, can you find the length of the longest good subsequence of the current sequence?
Note that the queries are not independent.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $Q$.
The second line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$.
$Q$ lines follow. Each of these lines contains two space-separated integers $x$ and $y$ describing a query.
------ Output ------
After each query, print a single line containing one integer ― the length of the longest good subsequence.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N, Q ≤ 10^{5}$
$1 ≤ a_{i} ≤ 10^{9}$ for each valid $i$
$1 ≤ x ≤ N$
$1 ≤ y ≤ 10^{9}$
------ Subtasks ------
Subtask #1 (35 points): $Q = 1$
Subtask #2 (65 points): original constraints
----- Sample Input 1 ------
1
5 2
1 1 2 5 2
1 3
4 2
----- Sample Output 1 ------
5
3
|
{"inputs": ["1\n5 2\n1 1 2 5 2\n1 3\n4 2"], "outputs": ["5\n3"]}
| 435
| 36
|
coding
|
Solve the programming task below in a Python markdown code block.
The eval() expression is a very powerful built-in function of Python. It helps in evaluating an expression. The expression can be a Python statement, or a code object.
For example:
>>> eval("9 + 5")
14
>>> x = 2
>>> eval("x + 3")
5
Here, eval() can also be used to work with Python keywords or defined functions and variables. These would normally be stored as strings.
For example:
>>> type(eval("len"))
<type 'builtin_function_or_method'>
Without eval()
>>> type("len")
<type 'str'>
Task
You are given an expression in a line. Read that line as a string variable, such as var, and print the result using eval(var).
NOTE: Python2 users, please import from __future__ import print_function.
Constraint
Input string is less than 100 characters.
Sample Input
print(2 + 3)
Sample Output
5
|
{"inputs": ["print(2 + 3)\n"], "outputs": ["5\n"]}
| 214
| 20
|
coding
|
Solve the programming task below in a Python markdown code block.
You have a binary string S of length N. In one operation you can select a substring of S and reverse it. For example, on reversing the substring S[2,4] for S=11000, we change 1 \textcolor{red}{100} 0 \rightarrow 1 \textcolor{red}{001} 0.
Find the minimum number of operations required to sort this binary string.
It can be proven that the string can always be sorted using the above operation finite number of times.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of 2 lines of input.
- The first line of each test case contains a single integer N — the length of the binary string.
- The second line of each test case contains a binary string S of length N.
------ Output Format ------
For each test case, output on a new line — the minimum number of operations required to sort the binary string.
------ Constraints ------
$1 ≤ T ≤ 2\cdot 10^{5}$
$1 ≤ N ≤ 2\cdot 10^{5}$
- Sum of $N$ over all test cases does not exceed $10^{6}$.
- String $S$ consists of only '$0$'s and '$1$'s.
----- Sample Input 1 ------
4
3
000
4
1001
4
1010
6
010101
----- Sample Output 1 ------
0
1
2
2
----- explanation 1 ------
Test case $1$: The string is already sorted, hence, zero operations are required to sort it.
Test case $2$: We can sort the string in the following way: $\textcolor{red}{100} 1$ $\rightarrow$ $0011$.
Test case $3$: We can sort the string in the following way:
$1 \textcolor{red}{01} 0$ $\rightarrow$ $\textcolor{red}{1100}$ $\rightarrow$ $0011$.
It can be proven that this string cannot be sorted in less than $2$ operations.
Test case $4$: We can sort the string in the following way:
$0 \textcolor{red}{1010}1$ $\rightarrow$ $00 \textcolor{red}{10}11$ $\rightarrow$ $000111$.
It can be proven that this string cannot be sorted in less than $2$ operations.
|
{"inputs": ["4\n3\n000\n4\n1001\n4\n1010\n6\n010101"], "outputs": ["0\n1\n2\n2"]}
| 577
| 47
|
coding
|
Solve the programming task below in a Python markdown code block.
The capital of Berland looks like a rectangle of size n × m of the square blocks of same size.
Fire!
It is known that k + 1 blocks got caught on fire (k + 1 ≤ n·m). Those blocks are centers of ignition. Moreover positions of k of these centers are known and one of these stays unknown. All k + 1 positions are distinct.
The fire goes the following way: during the zero minute of fire only these k + 1 centers of ignition are burning. Every next minute the fire goes to all neighbouring blocks to the one which is burning. You can consider blocks to burn for so long that this time exceeds the time taken in the problem. The neighbouring blocks are those that touch the current block by a side or by a corner.
Berland Fire Deparment wants to estimate the minimal time it takes the fire to lighten up the whole city. Remember that the positions of k blocks (centers of ignition) are known and (k + 1)-th can be positioned in any other block.
Help Berland Fire Department to estimate the minimal time it takes the fire to lighten up the whole city.
-----Input-----
The first line contains three integers n, m and k (1 ≤ n, m ≤ 10^9, 1 ≤ k ≤ 500).
Each of the next k lines contain two integers x_{i} and y_{i} (1 ≤ x_{i} ≤ n, 1 ≤ y_{i} ≤ m) — coordinates of the i-th center of ignition. It is guaranteed that the locations of all centers of ignition are distinct.
-----Output-----
Print the minimal time it takes the fire to lighten up the whole city (in minutes).
-----Examples-----
Input
7 7 3
1 2
2 1
5 5
Output
3
Input
10 5 1
3 3
Output
2
-----Note-----
In the first example the last block can have coordinates (4, 4).
In the second example the last block can have coordinates (8, 3).
|
{"inputs": ["8 5 1\n3 3\n", "10 5 1\n3 3\n", "10 5 1\n3 5\n", "10 5 1\n3 3\n", "5 1 2\n4 1\n5 1\n", "5 1 2\n4 1\n5 1\n", "5 1 2\n4 1\n1 1\n", "5 1 2\n4 1\n2 1\n"], "outputs": ["2\n", "2\n", "4\n", "2\n", "1\n", "1\n", "1\n", "1\n"]}
| 445
| 153
|
coding
|
Solve the programming task below in a Python markdown code block.
This is a problem that involves adding numbers to items in a list.
In a list you will have to add the item's remainder when divided by a given divisor to each item.
For example if the item is 40 and the divisor is 3 you would have to add 1 since 40 minus the closest multiple of 3 which is 39 is 1. So the 40 in the list will become 41. You would have to return the modified list in this problem.
For this problem you will receive a divisor called `div` as well as simple list of whole numbers called `nums`. Good luck and happy coding.
# Examples
```python
nums = [2, 7, 5, 9, 100, 34, 32, 0], div = 3
==> [4, 8, 7, 9, 101, 35, 34, 0]
nums = [1000, 999, 998, 997], div = 5
==> [1000, 1003, 1001, 999]
nums = [], div = 2
==> []
```
**Note:** random tests check lists containing up to 10000 elements.
Also feel free to reuse/extend the following starter code:
```python
def solve(nums,div):
```
|
{"functional": "_inputs = [[[2, 7, 5, 9, 100, 34, 32, 0], 3], [[], 2], [[1000, 999, 998, 997], 5], [[0, 0, 0, 0], 5], [[4, 3, 2, 1], 5], [[33, 23, 45, 78, 65], 10]]\n_outputs = [[[4, 8, 7, 9, 101, 35, 34, 0]], [[]], [[1000, 1003, 1001, 999]], [[0, 0, 0, 0]], [[8, 6, 4, 2]], [[36, 26, 50, 86, 70]]]\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])"}
| 326
| 359
|
coding
|
Solve the programming task below in a Python markdown code block.
We have a string s consisting of lowercase English letters. Snuke is partitioning s into some number of non-empty substrings. Let the subtrings obtained be s_1, s_2, ..., s_N from left to right. (Here, s = s_1 + s_2 + ... + s_N holds.) Snuke wants to satisfy the following condition:
* For each i (1 \leq i \leq N), it is possible to permute the characters in s_i and obtain a palindrome.
Find the minimum possible value of N when the partition satisfies the condition.
Constraints
* 1 \leq |s| \leq 2 \times 10^5
* s consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
s
Output
Print the minimum possible value of N when the partition satisfies the condition.
Examples
Input
aabxyyzz
Output
2
Input
byebye
Output
1
Input
abcdefghijklmnopqrstuvwxyz
Output
26
Input
abcabcxabcx
Output
3
|
{"inputs": ["byeybe", "eybcxb", "ebyeyb", "eybeyb", "eybdyb", "eybcyb", "byecxb", "cyebxb"], "outputs": ["1\n", "6\n", "1\n", "1\n", "2\n", "2\n", "6\n", "4\n"]}
| 242
| 80
|
coding
|
Solve the programming task below in a Python markdown code block.
The only difference between easy and hard versions is the size of the input.
You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'.
You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...".
A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not.
You have to answer $q$ independent queries.
-----Input-----
The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow.
The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring.
The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'.
It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...".
-----Example-----
Input
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR
Output
1
0
3
-----Note-----
In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB".
In the second example, the substring is "BRG".
|
{"inputs": ["3\n5 2\nBGGGG\n5 3\nRBRGR\n5 5\nBBBRR\n", "3\n5 2\nBGGGG\n5 3\nRBRGR\n5 5\nBBRBR\n", "3\n5 3\nBGGGG\n5 3\nRBRGR\n5 5\nBBBRR\n", "3\n5 2\nBGGGG\n5 4\nRBRGR\n5 5\nBBRBR\n", "3\n5 3\nBGGGG\n5 3\nRBRGR\n5 2\nBBBRR\n", "3\n5 2\nBGGGG\n5 4\nRBRGR\n5 5\nBRBRB\n", "3\n5 1\nBGGGG\n5 2\nRBRGR\n5 5\nBBRBR\n", "3\n5 1\nBGGGG\n5 4\nRBRGR\n5 5\nBBRBR\n"], "outputs": ["1\n0\n3\n", "1\n0\n2\n", "1\n0\n3\n", "1\n1\n2\n", "1\n0\n0\n", "1\n1\n3\n", "0\n0\n2\n", "0\n1\n2\n"]}
| 591
| 294
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given three piles of casino chips: white, green and black chips:
* the first pile contains only white chips
* the second pile contains only green chips
* the third pile contains only black chips
Each day you take exactly two chips of different colors and head to the casino. You can choose any color, but you are not allowed to take two chips of the same color in a day.
You will be given an array representing the number of chips of each color and your task is to return the maximum number of days you can pick the chips. Each day you need to take exactly two chips.
```python
solve([1,1,1]) = 1, because after you pick on day one, there will be only one chip left
solve([1,2,1] = 2, you can pick twice; you pick two chips on day one then on day two
solve([4,1,1]) = 2
```
```javascript
solve([1,1,1]) = 1, because after you pick on day one, there will be only one chip left
solve([1,2,1]) = 2, you can pick twice; you pick two chips on day one then on day two
solve([4,1,1]) = 2
```
```go
solve([1,1,1]) = 1, because after you pick on day one, there will be only one chip left
solve([1,2,1]) = 2, you can pick twice; you pick two chips on day one then on day two
solve([4,1,1]) = 2
```
```ruby
solve([1,1,1]) = 1, because after you pick on day one, there will be only one chip left
solve([1,2,1]) = 2, you can pick twice; you pick two chips on day, two chips on day two
solve([4,1,1]) = 2
```
More examples in the test cases. Good luck!
Brute force is not the way to go here. Look for a simplifying mathematical approach.
Also feel free to reuse/extend the following starter code:
```python
def solve(arr):
```
|
{"functional": "_inputs = [[[1, 1, 1]], [[1, 2, 1]], [[4, 1, 1]], [[8, 2, 8]], [[8, 1, 4]], [[7, 4, 10]], [[12, 12, 12]], [[1, 23, 2]]]\n_outputs = [[1], [2], [2], [9], [5], [10], [18], [3]]\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])"}
| 482
| 251
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
There exists an undirected and initially unrooted tree with n nodes indexed from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
Each node has an associated price. You are given an integer array price, where price[i] is the price of the ith node.
The price sum of a given path is the sum of the prices of all nodes lying on that path.
The tree can be rooted at any node root of your choice. The incurred cost after choosing root is the difference between the maximum and minimum price sum amongst all paths starting at root.
Return the maximum possible cost amongst all possible root choices.
Please complete the following python code precisely:
```python
class Solution:
def maxOutput(self, n: int, edges: List[List[int]], price: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 6, edges = [[0,1],[1,2],[1,3],[3,4],[3,5]], price = [9,8,7,6,10,5]) == 24\n assert candidate(n = 3, edges = [[0,1],[1,2]], price = [1,1,1]) == 2\n\n\ncheck(Solution().maxOutput)"}
| 223
| 103
|
coding
|
Solve the programming task below in a Python markdown code block.
Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one.
GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping substrings equal either to b or c as possible. Substring of string x is a string formed by consecutive segment of characters from x. Two substrings of string x overlap if there is position i in string x occupied by both of them.
GukiZ was disappointed because none of his students managed to solve the problem. Can you help them and find one of possible strings k?
Input
The first line contains string a, the second line contains string b, and the third line contains string c (1 ≤ |a|, |b|, |c| ≤ 105, where |s| denotes the length of string s).
All three strings consist only of lowercase English letters.
It is possible that b and c coincide.
Output
Find one of possible strings k, as described in the problem statement. If there are multiple possible answers, print any of them.
Examples
Input
aaa
a
b
Output
aaa
Input
pozdravstaklenidodiri
niste
dobri
Output
nisteaadddiiklooprrvz
Input
abbbaaccca
ab
aca
Output
ababacabcc
Note
In the third sample, this optimal solutions has three non-overlaping substrings equal to either b or c on positions 1 – 2 (ab), 3 – 4 (ab), 5 – 7 (aca). In this sample, there exist many other optimal solutions, one of them would be acaababbcc.
|
{"inputs": ["aaa\na\nb\n", "cumurcumur\num\ncur\n", "cumurcumur\nul\ncur\n", "abbbaaccca\nba\naca\n", "cunurcumur\nul\ncur\n", "abbbaaccca\nab\naca\n", "xxxbbbcccoca\nca\ncb\n", "xxxbbbcccoca\nca\nca\n"], "outputs": ["aaa\n", "umumcurcur\n", "curcurmmuu", "babaacabcc", "curcurmnuu", "ababacabcc\n", "cacbcbcboxxx\n", "cabbbcccoxxx"]}
| 390
| 150
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a programing contest named SnakeUp, 2n people want to compete for it. In order to attend this contest, people need to form teams of exactly two people. You are given the strength of each possible combination of two people. All the values of the strengths are distinct.
Every contestant hopes that he can find a teammate so that their team’s strength is as high as possible. That is, a contestant will form a team with highest strength possible by choosing a teammate from ones who are willing to be a teammate with him/her. More formally, two people A and B may form a team if each of them is the best possible teammate (among the contestants that remain unpaired) for the other one.
Can you determine who will be each person’s teammate?
-----Input-----
There are 2n lines in the input.
The first line contains an integer n (1 ≤ n ≤ 400) — the number of teams to be formed.
The i-th line (i > 1) contains i - 1 numbers a_{i}1, a_{i}2, ... , a_{i}(i - 1). Here a_{ij} (1 ≤ a_{ij} ≤ 10^6, all a_{ij} are distinct) denotes the strength of a team consisting of person i and person j (people are numbered starting from 1.)
-----Output-----
Output a line containing 2n numbers. The i-th number should represent the number of teammate of i-th person.
-----Examples-----
Input
2
6
1 2
3 4 5
Output
2 1 4 3
Input
3
487060
3831 161856
845957 794650 976977
83847 50566 691206 498447
698377 156232 59015 382455 626960
Output
6 5 4 3 2 1
-----Note-----
In the first sample, contestant 1 and 2 will be teammates and so do contestant 3 and 4, so the teammate of contestant 1, 2, 3, 4 will be 2, 1, 4, 3 respectively.
|
{"inputs": ["1\n1000000\n", "1\n1000000\n", "1\n1000001\n", "1\n1000100\n", "1\n0000001\n", "1\n1000110\n", "1\n0001001\n", "1\n1001110\n"], "outputs": ["2 1\n", "2 1\n", "2 1 ", "2 1 ", "2 1 ", "2 1 ", "2 1 ", "2 1 "]}
| 531
| 145
|
coding
|
Solve the programming task below in a Python markdown code block.
There are N integers a_1, a_2, ..., a_N not less than 1. The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P.
Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.
Constraints
* 1 \leq N \leq 10^{12}
* 1 \leq P \leq 10^{12}
Input
Input is given from Standard Input in the following format:
N P
Output
Print the answer.
Examples
Input
3 24
Output
2
Input
5 1
Output
1
Input
1 111
Output
111
Input
4 972439611840
Output
206
|
{"inputs": ["5 2", "1 2", "1 3", "1 5", "1 4", "1 8", "1 6", "1 9"], "outputs": ["1\n", "2\n", "3\n", "5\n", "4\n", "8\n", "6\n", "9\n"]}
| 212
| 78
|
coding
|
Solve the programming task below in a Python markdown code block.
Let's arrange a deck of cards. Your task is to sort totally n cards. A card consists of a part of a suit (S, H, C or D) and an number. Write a program which sorts such cards based on the following pseudocode:
Partition(A, p, r)
1 x = A[r]
2 i = p-1
3 for j = p to r-1
4 do if A[j] <= x
5 then i = i+1
6 exchange A[i] and A[j]
7 exchange A[i+1] and A[r]
8 return i+1
Quicksort(A, p, r)
1 if p < r
2 then q = Partition(A, p, r)
3 run Quicksort(A, p, q-1)
4 run Quicksort(A, q+1, r)
Here, A is an array which represents a deck of cards and comparison operations are performed based on the numbers.
Your program should also report the stability of the output for the given input (instance). Here, 'stability of the output' means that: cards with the same value appear in the output in the same order as they do in the input (instance).
Constraints
* 1 ≤ n ≤ 100,000
* 1 ≤ the number of a card ≤ 109
* There are no identical card in the input
Input
The first line contains an integer n, the number of cards.
n cards are given in the following lines. Each card is given in a line and represented by a pair of a character and an integer separated by a single space.
Output
In the first line, print the stability ("Stable" or "Not stable") of this output.
In the following lines, print the arranged cards in the same manner of that of the input.
Examples
Input
6
D 3
H 2
D 1
S 3
D 2
C 1
Output
Not stable
D 1
C 1
D 2
H 2
D 3
S 3
Input
2
S 1
H 1
Output
Stable
S 1
H 1
|
{"inputs": ["2\nS 1\nH 2", "2\nS 0\nH 1", "2\nS 2\nH 2", "2\nS 1\nH 1", "6\nD 3\nH 2\nD 1\nS 3\nC 2\nC 1", "6\nD 3\nH 2\nD 1\nS 3\nC 2\nC 2", "6\nD 3\nH 2\nC 1\nS 3\nC 2\nC 2", "6\nD 5\nH 2\nC 1\nS 3\nC 2\nC 2"], "outputs": ["Stable\nS 1\nH 2\n", "Stable\nS 0\nH 1\n", "Stable\nS 2\nH 2\n", "Stable\nS 1\nH 1", "Not stable\nD 1\nC 1\nC 2\nH 2\nD 3\nS 3\n", "Stable\nD 1\nH 2\nC 2\nC 2\nD 3\nS 3\n", "Stable\nC 1\nH 2\nC 2\nC 2\nD 3\nS 3\n", "Stable\nC 1\nH 2\nC 2\nC 2\nS 3\nD 5\n"]}
| 485
| 325
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
A decimal number can be converted to its Hexspeak representation by first converting it to an uppercase hexadecimal string, then replacing all occurrences of the digit '0' with the letter 'O', and the digit '1' with the letter 'I'. Such a representation is valid if and only if it consists only of the letters in the set {'A', 'B', 'C', 'D', 'E', 'F', 'I', 'O'}.
Given a string num representing a decimal integer n, return the Hexspeak representation of n if it is valid, otherwise return "ERROR".
Please complete the following python code precisely:
```python
class Solution:
def toHexspeak(self, num: str) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(num = \"257\") == \"IOI\"\n assert candidate(num = \"3\") == \"ERROR\"\n\n\ncheck(Solution().toHexspeak)"}
| 170
| 50
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a string s consisting of n characters which are either 'X' or 'O'.
A move is defined as selecting three consecutive characters of s and converting them to 'O'. Note that if a move is applied to the character 'O', it will stay the same.
Return the minimum number of moves required so that all the characters of s are converted to 'O'.
Please complete the following python code precisely:
```python
class Solution:
def minimumMoves(self, s: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"XXX\") == 1\n assert candidate(s = \"XXOX\") == 2\n assert candidate(s = \"OOOO\") == 0\n\n\ncheck(Solution().minimumMoves)"}
| 124
| 57
|
coding
|
Solve the programming task below in a Python markdown code block.
# Write Number in Expanded Form
You will be given a number and you will need to return it as a string in [Expanded Form](https://www.mathplacementreview.com/arithmetic/whole-numbers.php#expanded-form). For example:
```python
expanded_form(12) # Should return '10 + 2'
expanded_form(42) # Should return '40 + 2'
expanded_form(70304) # Should return '70000 + 300 + 4'
```
NOTE: All numbers will be whole numbers greater than 0.
If you liked this kata, check out [part 2](https://www.codewars.com/kata/write-number-in-expanded-form-part-2)!!
Also feel free to reuse/extend the following starter code:
```python
def expanded_form(num):
```
|
{"functional": "_inputs = [[2], [12], [42], [70304], [4982342]]\n_outputs = [['2'], ['10 + 2'], ['40 + 2'], ['70000 + 300 + 4'], ['4000000 + 900000 + 80000 + 2000 + 300 + 40 + 2']]\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(expanded_form(*i), o[0])"}
| 192
| 250
|
coding
|
Solve the programming task below in a Python markdown code block.
We have an integer sequence A of length N, where A_1 = X, A_{i+1} = A_i + D (1 \leq i < N ) holds.
Takahashi will take some (possibly all or none) of the elements in this sequence, and Aoki will take all of the others.
Let S and T be the sum of the numbers taken by Takahashi and Aoki, respectively. How many possible values of S - T are there?
-----Constraints-----
- -10^8 \leq X, D \leq 10^8
- 1 \leq N \leq 2 \times 10^5
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
N X D
-----Output-----
Print the number of possible values of S - T.
-----Sample Input-----
3 4 2
-----Sample Output-----
8
A is (4, 6, 8).
There are eight ways for (Takahashi, Aoki) to take the elements: ((), (4, 6, 8)), ((4), (6, 8)), ((6), (4, 8)), ((8), (4, 6))), ((4, 6), (8))), ((4, 8), (6))), ((6, 8), (4))), and ((4, 6, 8), ()).
The values of S - T in these ways are -18, -10, -6, -2, 2, 6, 10, and 18, respectively, so there are eight possible values of S - T.
|
{"inputs": ["3 3 2", "3 3 3", "0 3 2", "2 4 3", "3 4 2", "3 4 2\n", "7 5 5\n", "8 6 0\n"], "outputs": ["8\n", "7\n", "1\n", "4\n", "8", "8\n", "29\n", "9\n"]}
| 372
| 97
|
coding
|
Solve the programming task below in a Python markdown code block.
Much cooler than your run-of-the-mill Fibonacci numbers, the Triple Shiftian are so defined: `T[n] = 4 * T[n-1] - 5 * T[n-2] + 3 * T[n-3]`.
You are asked to create a function which accept a base with the first 3 numbers and then returns the nth element.
```python
triple_shiftian([1,1,1],25) == 1219856746
triple_shiftian([1,2,3],25) == 2052198929
triple_shiftian([6,7,2],25) == -2575238999
triple_shiftian([3,2,1],35) == 23471258855679
triple_shiftian([1,9,2],2) == 2
```
*Note: this is meant to be an interview quiz, so the description is scarce in detail on purpose*
Special thanks to the [first person I met in person here in London just because of CW](http://www.codewars.com/users/webtechalex) and that assisted me during the creation of this kata ;)
Also feel free to reuse/extend the following starter code:
```python
def triple_shiftian(base,n):
```
|
{"functional": "_inputs = [[[1, 1, 1], 25], [[1, 2, 3], 25], [[3, 2, 1], 25], [[6, 7, 2], 25], [[1, 1, 1], 35], [[1, 2, 3], 35], [[3, 2, 1], 35], [[6, 7, 2], 35], [[3, 2, 1], 0], [[6, 7, 2], 2]]\n_outputs = [[1219856746], [2052198929], [2827228055], [-2575238999], [10127083068293], [17037073417493], [23471258855679], [-21379280887590], [3], [2]]\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(triple_shiftian(*i), o[0])"}
| 312
| 396
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.