problem_id
int64
80
5k
question
stringlengths
170
10.1k
solutions
stringlengths
0
675k
input_output
stringlengths
0
33.5k
difficulty
stringclasses
3 values
url
stringlengths
38
68
starter_code
stringlengths
0
315
4,937
You're in the casino, playing Roulette, going for the "1-18" bets only and desperate to beat the house and so you want to test how effective the [Martingale strategy](https://en.wikipedia.org/wiki/Martingale_(betting_system)) is. You will be given a starting cash balance and an array of binary digits to represent a w...
["def martingale(bank, outcomes):\n stake = 100\n for i in outcomes:\n if i == 0:\n bank -= stake\n stake *= 2\n else:\n bank += stake\n stake = 100\n return bank", "def martingale(bank, outcomes):\n bet=100\n for results in outcomes:\n if ...
{"fn_name": "martingale", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/5eb34624fec7d10016de426e
def martingale(bank, outcomes):
4,939
# Background I drink too much coffee. Eventually it will probably kill me. *Or will it..?* Anyway, there's no way to know. *Or is there...?* # The Discovery of the Formula I proudly announce my discovery of a formula for measuring the life-span of coffee drinkers! For * ```h``` is a health number assigned to ...
["def coffee_limits(year, month, day):\n h = int(f'{year:04}{month:02}{day:02}')\n return [limit(h, 0xcafe), limit(h, 0xdecaf)]\n \ndef limit(h, c):\n for i in range(1, 5000):\n h += c\n if 'DEAD' in f'{h:X}':\n return i\n return 0\n\n", "def coffee_limits(year, month, day):\n ...
{"fn_name": "coffee_limits", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/57db78d3b43dfab59c001abe
def coffee_limits(year, month, day):
4,941
Suzuki needs help lining up his students! Today Suzuki will be interviewing his students to ensure they are progressing in their training. He decided to schedule the interviews based on the length of the students name in descending order. The students will line up and wait for their turn. You will be given a string o...
["lineup_students = lambda (s): sorted(s.split(), key=lambda x: (len(x), x), reverse=True)", "def lineup_students(s):\n return sorted(s.split(), key=lambda i:(len(i),i), reverse=True)\n", "def lineup_students(names):\n return sorted(names.split(), key=lambda s: (len(s), s), reverse=True)", "def lineup_students(na...
{"fn_name": "lineup_students", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/5701800886306a876a001031
def lineup_students(s):
4,949
Robinson Crusoe decides to explore his isle. On a sheet of paper he plans the following process. His hut has coordinates `origin = [0, 0]`. From that origin he walks a given distance `d` on a line that has a given angle `ang` with the x-axis. He gets to a point A. (Angles are measured with respect to the x-axis) Fro...
["from math import cos, sin, radians\n\ndef crusoe(n, d, ang, dist_mult, ang_mult):\n x, y, a = 0, 0, radians(ang)\n for i in range(n):\n x += d * cos(a)\n y += d * sin(a)\n d *= dist_mult\n a *= ang_mult\n return x, y", "from math import sin,cos,pi\ndef crusoe(n, d, ang, dis_tmult,...
{"fn_name": "crusoe", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/5d95b7644a336600271f52ba
def crusoe(n, d, ang, dist_mult, ang_mult):
4,953
Lets play some Pong! ![pong](http://gifimage.net/wp-content/uploads/2017/08/pong-gif-3.gif) For those who don't know what Pong is, it is a simple arcade game where two players can move their paddles to hit a ball towards the opponent's side of the screen, gaining a point for each opponent's miss. You can read more a...
["from itertools import cycle\n\nclass Pong:\n def __init__(self, max_score):\n self.max_score = max_score;\n self.scores = {1: 0, 2: 0}\n self.players = cycle((1, 2))\n\n def game_over(self):\n return any(score >= self.max_score for score in list(self.scores.values()))\n\n def play...
{"fn_name": "__init__", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/5b432bdf82417e3f39000195
def __init__(self, max_score):
4,959
There are a **n** balls numbered from 0 to **n-1** (0,1,2,3,etc). Most of them have the same weight, but one is heavier. Your task is to find it. Your function will receive two arguments - a `scales` object, and a ball count. The `scales` object has only one method: ```python get_weight(left, right) ``` where `l...
["def find_ball(scales, n):\n select = list(range(n))\n while len(select) > 1:\n left, right, unused = select[::3], select[1::3], select[2::3]\n if len(select) % 3 == 1: unused.append(left.pop())\n select = [left, unused, right][scales.get_weight(left, right) + 1]\n return select.pop()", "...
{"fn_name": "find_ball", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/545c4f7682e55d3c6e0011a1
def find_ball(scales, ball_count):
4,960
[Harshad numbers](http://en.wikipedia.org/wiki/Harshad_number) (also called Niven numbers) are positive numbers that can be divided (without remainder) by the sum of their digits. For example, the following numbers are Harshad numbers: * 10, because 1 + 0 = 1 and 10 is divisible by 1 * 27, because 2 + 7 = 9 and 27 is...
["from itertools import count, islice\n\n\nclass Harshad:\n @staticmethod\n def is_valid(number):\n return number % sum(map(int, str(number))) == 0\n \n @classmethod\n def get_next(cls, number):\n return next(i for i in count(number+1) if cls.is_valid(i))\n \n @classmethod\n def ge...
{"fn_name": "is_valid", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/54a0689443ab7271a90000c6
def is_valid(number):
4,969
Build Tower Advanced --- Build Tower by the following given arguments: __number of floors__ (integer and always greater than 0) __block size__ (width, height) (integer pair and always greater than (0, 0)) Tower block unit is represented as `*` * Python: return a `list`; * JavaScript: returns an `Array`; Have fun! *...
["def tower_builder(n, (w, h)):\n return [str.center(\"*\" * (i*2-1)*w, (n*2-1)*w) for i in range(1, n+1) for _ in range(h)]", "def tower_builder(n_floors, block_size):\n w, h = block_size\n filled_block = '*' * w\n empty_block = ' ' * w\n tower = []\n for n in range(1,n_floors+1):\n for _ in r...
{"fn_name": "tower_builder", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/57675f3dedc6f728ee000256
def tower_builder(n_floors, block_size):
4,970
---- Vampire Numbers ---- Our loose definition of [Vampire Numbers](http://en.wikipedia.org/wiki/Vampire_number) can be described as follows: ```python 6 * 21 = 126 # 6 and 21 would be valid 'fangs' for a vampire number as the # digits 6, 1, and 2 are present in both the product and multiplicands 10 * 11 = 110 # 11...
["def vampire_test(x, y):\n return sorted(str(x * y)) == sorted(str(x) + str(y))", "def vampire_test(x, y):\n return sorted(list(str(x) + str(y))) == sorted(list(str(x*y)))", "from collections import Counter\n\ndef vampire_test(x, y):\n return Counter(str(x) + str(y)) == Counter(str(x * y))", "def vampire_test...
{"fn_name": "vampire_test", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/54d418bd099d650fa000032d
def vampire_test(x, y):
4,975
Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer. Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is...
["def solution(n):\n roman_numerals = {1000:'M',\n 900: 'CM',\n 500: 'D',\n 400: 'CD',\n 100: 'C',\n 90: 'XC',\n 50: 'L',\n 40: 'XL',\n 10: 'X',\n ...
{"fn_name": "solution", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/51b62bf6a9c58071c600001b
def solution(n):
4,977
# Messi goals function [Messi](https://en.wikipedia.org/wiki/Lionel_Messi) is a soccer player with goals in three leagues: - LaLiga - Copa del Rey - Champions Complete the function to return his total number of goals in all three leagues. Note: the input will always be valid. For example: ``` 5, 10, 2 --> 17 `...
["def goals(*a):\n return sum(a)", "def goals(*args):\n return sum(args)", "def goals(laLiga, copaDelRey, championsLeague):\n return laLiga+copaDelRey+championsLeague", "def goals(laLiga, copaDelRey, championsLeague):\n \n resultado = laLiga + copaDelRey + championsLeague\n \n return resultado\n ...
{"fn_name": "goals", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/55f73be6e12baaa5900000d4
def goals(laLiga, copaDelRey, championsLeague):
4,978
In this kata, your task is to implement what I call **Iterative Rotation Cipher (IRC)**. To complete the task, you will create an object with two methods, `encode` and `decode`. (For non-JavaScript versions, you only need to write the two functions without the enclosing dict) Input The encode method will receive two a...
["def shift(string, step):\n i = (step % len(string)) if string else 0\n return f\"{string[-i:]}{string[:-i]}\"\n\ndef encode(n, string):\n for _ in range(n):\n shifted = shift(string.replace(\" \", \"\"), n)\n l = [len(word) for word in string.split(\" \")]\n string = \" \".join(shift(shi...
{"fn_name": "encode", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/5a3357ae8058425bde002674
def encode(n,strng):
4,981
Many websites use weighted averages of various polls to make projections for elections. They’re weighted based on a variety of factors, such as historical accuracy of the polling firm, sample size, as well as date(s). The weights, in this kata, are already calculated for you. All you need to do is convert a set of poll...
["def predict(candidates, polls):\n x = zip(*[list(map(lambda i: i * weight, poll)) for poll, weight in polls])\n x = list(map(round1, (map(lambda i: sum(i) / sum([i[1] for i in polls]), x))))\n return dict(zip(candidates,x))", "def predict(candidates, polls):\n weight = sum(w for _, w in polls)\n scores...
{"fn_name": "predict", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/5827d31b86f3b0d9390001d4
def predict(candidates, polls):
4,986
# Write this function ![](http://i.imgur.com/mlbRlEm.png) `for i from 1 to n`, do `i % m` and return the `sum` f(n=10, m=5) // returns 20 (1+2+3+4+0 + 1+2+3+4+0) *You'll need to get a little clever with performance, since n can be a very large number*
["def f(n, m):\n re, c = divmod(n,m) \n return m*(m-1)/2*re + (c+1)*c/2", "def f(n, m):\n # Function to return the sum of the remnainders when n is divided by m for all n from 1 to n\n # The remainders cycle from 0, 1, 2, 3,... m-1. This happens n // m times, with a final sequnce \n # up to n % m. The ...
{"fn_name": "f", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/54c2fc0552791928c9000517
def f(n, m):
4,992
Given a random bingo card and an array of called numbers, determine if you have a bingo! *Parameters*: `card` and `numbers` arrays. *Example input*: ``` card = [ ['B', 'I', 'N', 'G', 'O'], [1, 16, 31, 46, 61], [3, 18, 33, 48, 63], [5, 20, 'FREE SPACE', 50, 65], [7, 22, 37, 52, 67], [9, 24, 39, 54, 69] ] ...
["def bingo(card, numbers):\n \n rc, cc, dc = [0]*5, [0]*5, [1]*2 # rows count, columns count, diag counts\n rc[2] = cc[2] = 1 # preaffect 'FREE SPACE'\n s = set(numbers)\n \n for x,line in enumerate(card[1:]):\n for y,(c,n) in enumerate(zip(card[0], line)):...
{"fn_name": "bingo", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/5af304892c5061951e0000ce
def bingo(card, numbers):
4,993
The description is rather long but you are given all needed formulas (almost:-) John has bought a bike but before going moutain biking he wants us to do a few simulations. He gathered information: - His trip will consist of an ascent of `dTot` kilometers with an average slope of `slope` *percent* - We suppose that:...
["from math import sin, atan\n\ndef temps(v0, slope, d_tot):\n GRAVITY_ACC = 9.81 * 3.6 * 60.0 # gravity acceleration\n DRAG = 60.0 * 0.3 / 3.6 # force applied by air on the cyclist\n DELTA_T = 1.0/60.0 # in minutes\n D_WATTS = 0.5 ...
{"fn_name": "temps", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/5879f95892074d769f000272
def temps(v0, slope, d_tot):
4,997
The function sigma 1, σ1 in mathematics, is known as the one that gives the sum of the divisors of an integer number. For example for the number 10, ```python σ1(10) = 18 # because the divisors of 10 are: 1, 2, 5, 10 σ1(10) = 1 + 2 + 5 + 10 = 18 ``` You can see the graph of this important function up to 250: The n...
["cache = {}\ndef sum_div(x):\n if x not in cache:\n cache[x] = sum(i for i in range(1, x+1) if x % i == 0)\n return cache[x]\n\ndef is_required(x):\n reversed = int(str(x)[::-1])\n return x != reversed and sum_div(x) == sum_div(reversed)\n\nrequired = [x for x in range(528, 10**4) if is_required(x)]...
{"fn_name": "sigma1", "inputs": [], "outputs": []}
introductory
https://www.codewars.com/kata/5619dbc22e69620e5a000010
def sigma1(n):
102
Today Tavas got his test result as an integer score and he wants to share it with his girlfriend, Nafas. His phone operating system is Tavdroid, and its keyboard doesn't have any digits! He wants to share his score with Nafas via text, so he has no choice but to send this number using words. [Image] He ate coffee mi...
["n = int(input())\nif n == 0:\n\tprint('zero')\nelif n == 1:\n\tprint('one')\nelif n == 2:\n\tprint('two')\nelif n == 3:\n\tprint('three')\nelif n == 4:\n\tprint('four')\nelif n == 5:\n\tprint('five')\nelif n == 6:\n\tprint('six')\nelif n == 7:\n\tprint('seven')\nelif n == 8:\n\tprint('eight')\nelif n == 9:\n\tprint('...
{ "inputs": [ "6\n", "99\n", "20\n", "10\n", "15\n", "27\n", "40\n", "63\n", "0\n", "1\n", "2\n", "8\n", "9\n", "11\n", "12\n", "13\n", "14\n", "16\n", "17\n", "18\n", "19\n", "21\n", "29\n", "30\n", "32\n", "38\n"...
interview
https://codeforces.com/problemset/problem/535/A
107
Top-model Izabella participates in the competition. She wants to impress judges and show her mathematical skills. Her problem is following: for given string, consisting of only 0 and 1, tell if it's possible to remove some digits in such a way, that remaining number is a representation of some positive integer, divisi...
["s = input()\ni = 0\nwhile i < len(s) and s[i] == '0':\n i += 1\ncnt = 0\nwhile i < len(s):\n if s[i] == '0':\n cnt += 1\n i += 1\n\nif cnt >= 6:\n print('yes')\nelse:\n print('no')\n", "s=input()\none=False\nzero=0\nfor x in s:\n if not one:\n if x=='1':\n one=True\n else...
{ "inputs": [ "100010001\n", "100\n", "0000001000000\n", "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\n", "1111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111\n", "011111110111111111111111...
interview
https://codeforces.com/problemset/problem/887/A
318
You are given the current time in 24-hour format hh:mm. Find and print the time after a minutes. Note that you should find only the time after a minutes, see the examples to clarify the problem statement. You can read more about 24-hour format here https://en.wikipedia.org/wiki/24-hour_clock. -----Input----- The f...
["def main():\n a, b = map(int, input().split(\":\"))\n c = int(input())\n\n a += c // 60\n b += c % 60\n if b > 59:\n b %= 60\n a += 1\n\n aa = str(a % 24)\n if len(aa) < 2:\n aa = \"0\" + aa\n\n bb = str(b % 60)\n if len(bb) < 2:\n bb = \"0\" + bb\n\n print(aa...
{ "inputs": [ "23:59\n10\n", "20:20\n121\n", "10:10\n0\n", "12:34\n10000\n", "00:00\n10000\n", "00:00\n1440\n", "23:59\n8640\n", "10:01\n0\n", "04:05\n0\n", "02:59\n1\n", "05:15\n10\n", "03:10\n20\n", "09:11\n0\n", "19:00\n0\n", "23:59\n1\n", "11:59\n1\n...
interview
https://codeforces.com/problemset/problem/622/B
384
Recently Adaltik discovered japanese crosswords. Japanese crossword is a picture, represented as a table sized a × b squares, and each square is colored white or black. There are integers to the left of the rows and to the top of the columns, encrypting the corresponding row or column. The number of integers represents...
["# You lost the game.\n\nn = int(input())\ns = str(input())\n\nR = []\nb = 0\nfor i in range(n):\n if s[i] == \"B\":\n b += 1\n elif b > 0:\n R += [b]\n b = 0\nif b > 0:\n R += [b]\nprint(len(R))\nfor i in range(len(R)):\n print(R[i],end=\" \")\n", "#!/usr/bin/env python3\n\nimport re\...
{ "inputs": [ "3\nBBW\n", "5\nBWBWB\n", "4\nWWWW\n", "4\nBBBB\n", "13\nWBBBBWWBWBBBW\n", "1\nB\n", "2\nBB\n", "100\nWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB\n", "1\nW\n", "2\nWW\n", "2\nWB\n", "2\nBW\n", "3...
interview
https://codeforces.com/problemset/problem/721/A
459
During the breaks between competitions, top-model Izabella tries to develop herself and not to be bored. For example, now she tries to solve Rubik's cube 2x2x2. It's too hard to learn to solve Rubik's cube instantly, so she learns to understand if it's possible to solve the cube in some state using 90-degrees rotation...
["l=list(map(int,input().split()))\nl.insert(0,0)\nc1=[1,6,3,8,5,10,7,12,9,23,11,21,13,14,15,16,17,18,19,20,4,22,2,24]\nc2=[1,23,3,21,5,2,7,4,9,6,11,8,13,14,15,16,17,18,19,20,12,22,10,24]\nc3=[1,2,3,4,5,6,15,16,9,10,11,12,13,14,23,24,17,18,7,8,21,22,19,20]\nc4=[1,2,3,4,5,6,19,20,9,10,11,12,13,14,7,8,17,18,23,24,21,22,1...
{ "inputs": [ "2 5 4 6 1 3 6 2 5 5 1 2 3 5 3 1 1 2 4 6 6 4 3 4\n", "5 3 5 3 2 5 2 5 6 2 6 2 4 4 4 4 1 1 1 1 6 3 6 3\n", "2 6 3 3 5 5 2 6 1 1 6 4 4 4 2 4 6 5 3 1 2 5 3 1\n", "3 4 2 3 5 5 6 6 4 5 4 6 5 1 1 1 6 2 1 3 3 2 4 2\n", "5 5 2 5 3 3 2 6 6 4 2 4 6 1 4 3 1 6 2 1 3 4 5 1\n", "6 6 1 2 6 1 1 ...
interview
https://codeforces.com/problemset/problem/887/C
595
The girl Taylor has a beautiful calendar for the year y. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. The calendar is so beautiful that she wants to know what is the next year after y when the calendar will be exactly the same. Help Tayl...
["def iswis(a):\n\treturn a % 400 == 0 or (a%100!= 0 and a %4==0)\n\nn = int(input())\nwis = iswis(n)\nfr = 0;\nn += 1\nif (wis):\n\tfr += 1\nfr += 1\nwhile (iswis(n) != wis or fr != 0):\n\tif (iswis(n)):\n\t\tfr += 1\n\tfr += 1\n\tfr %= 7\n\tn += 1\nprint(n)", "def is_leap(n):\n return n % 400 == 0 or (n % 4 == 0 a...
{ "inputs": [ "2016\n", "2000\n", "50501\n", "1000\n", "1900\n", "1899\n", "99999\n", "50000\n", "99900\n", "12345\n", "1004\n", "2100\n", "1313\n", "1872\n", "2098\n", "2072\n", "2002\n", "1179\n", "2096\n", "1096\n", "1796\n", "...
interview
https://codeforces.com/problemset/problem/678/B
620
Long time ago Alex created an interesting problem about parallelogram. The input data for this problem contained four integer points on the Cartesian plane, that defined the set of vertices of some non-degenerate (positive area) parallelogram. Points not necessary were given in the order of clockwise or counterclockwis...
["x1, y1 = list(map(int, input().split()))\nx2, y2 = list(map(int, input().split()))\nx3, y3 = list(map(int, input().split()))\na = set()\na.add(((x1 + x2) - x3, y1 + y2 - y3))\na.add(((x1 + x3) - x2, y1 + y3 - y2))\na.add(((x2 + x3) - x1, y2 + y3 - y1))\nprint(len(a))\nfor i in a:\n print(*i)", "points = []\n\nfor ...
{ "inputs": [ "0 0\n1 0\n0 1\n", "0 -1\n-1 0\n1 1\n", "-1 -1\n0 1\n1 1\n", "1000 1000\n-1000 -1000\n-1000 1000\n", "-1000 1000\n1000 -1000\n-1000 -1000\n", "-4 -5\n7 10\n3 -10\n", "-86 25\n-55 18\n58 24\n", "-301 -397\n192 317\n-98 729\n", "1000 1000\n999 1000\n-1000 -1000\n", ...
interview
https://codeforces.com/problemset/problem/749/B
641
Today is Wednesday, the third day of the week. What's more interesting is that tomorrow is the last day of the year 2015. Limak is a little polar bear. He enjoyed this year a lot. Now, he is so eager to the coming year 2016. Limak wants to prove how responsible a bear he is. He is going to regularly save candies for ...
["x = input().split(' ')\nif x[-1] == \"month\":\n if x[0] == '31':\n print(7)\n elif x[0] == '30':\n print(11)\n else:\n print(12)\nelse:\n if x[0] in ['5', '6']:\n print(53)\n else:\n print(52)", "p=input().split()\nif p[2]=='week':\n x=int(p[0])\n if x==5 or x=...
{ "inputs": [ "4 of week\n", "30 of month\n", "17 of month\n", "31 of month\n", "6 of week\n", "1 of week\n", "2 of week\n", "3 of week\n", "5 of week\n", "7 of week\n", "1 of month\n", "2 of month\n", "3 of month\n", "4 of month\n", "5 of month\n", "6 o...
interview
https://codeforces.com/problemset/problem/611/A
691
Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a new set to replenish his beautiful collection of sets. Dr. Evil has his favorite evil integer x. He asks Mahmoud and Ehab to f...
["n, x = input().split()\nn, x = int(n), int(x)\n\nm1 = 2 ** 17\nm2 = 2 ** 18\n\nif n == 2 and x == 0:\n print(\"NO\")\nelif n == 1:\n a = [x]\nelif n == 2 and x > 0:\n a = [0, x]\nelse:\n a = []\n ans = 0\n for i in range(1, n-2):\n ans ^= i\n a.append(i)\n if ans == x:\n a.ap...
{"inputs": ["5 5\n", "3 6\n", "3 0\n", "1 0\n", "3 3\n", "32 32\n", "32 31\n", "1 1\n", "2 0\n", "3 1\n", "3 2\n", "3 5\n", "3 4\n", "3 10203\n", "3 10100\n", "5 0\n", "5 1\n", "5 2\n", "5 3\n", "5 4\n", "5 6\n", "5 7\n", "5 8\n", "5 9\n", "4 2\n", "10 2\n", "1 2\n", "1 3\n", "2 1\n", "2 2\n", "2 3\n", "4 0\n", "4 1\n"...
interview
https://codeforces.com/problemset/problem/862/C
816
There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≤ i < j ≤ n) such that $a_{i} \oplus a_{j} = x$, where $\oplus$ is bitwise xor operation (see notes for explanation). [Image] Im...
["n, x = list(map(int, input().split()))\nar = list(map(int, input().split()))\n\ncnt = {}\nans = 0\nfor val in ar:\n\tcur = val ^ x\n\t\n\tif cur in cnt:\n\t\tans += cnt[cur]\n\t\n\tif val in cnt:\n\t\tcnt[val] += 1\n\telse:\n\t\tcnt[val] = 1\n\nprint(ans)\n\n", "N = 10 ** 5 + 1\nn, x = list(map(int, input().split()))...
{ "inputs": [ "2 3\n1 2\n", "6 1\n5 1 2 3 4 1\n", "38 101\n395 5 339 366 409 150 400 180 348 200 409 20 182 409 208 74 176 401 459 158 282 207 241 406 33 484 65 245 363 337 204 197 445 445 72 435 126 423\n", "47 117\n77 57 535 240 250 321 51 29 42 582 390 525 149 195 119 465 198 494 456 313 497 205 11...
interview
https://codeforces.com/problemset/problem/742/B
1,053
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus...
["a = [0]\ncur = 1\n\nwhile(cur < 10 ** 13):\n x = a[-1]\n a.append(x * 2 + cur)\n cur *= 2\n\nn = int(input())\nn -= 1\n\nans = 0\ni = 1\ncur = 1\nwhile(n > 0):\n x = n % 2\n n = n // 2\n if(x > 0):\n ans += cur + a[i - 1]\n i += 1\n cur *= 2\n \nprint(ans)", "n = int(input())\nres = ...
{ "inputs": [ "4\n", "10\n", "2\n", "1000000000000\n", "999999999999\n", "23131234\n", "100000\n", "1024\n", "536870912\n", "536870911\n", "536870913\n", "123456789\n", "200\n", "3\n", "5\n", "6\n", "7\n", "1000\n", "12000\n", "65536\n", ...
interview
https://codeforces.com/problemset/problem/959/E
1,096
The only king stands on the standard chess board. You are given his position in format "cd", where c is the column from 'a' to 'h' and d is the row from '1' to '8'. Find the number of moves permitted for the king. Check the king's moves here https://en.wikipedia.org/wiki/King_(chess). [Image] King moves from the posit...
["#!/usr/bin/env python3\n\ntry:\n while True:\n s = input()\n if s[0] in \"ah\" and s[1] in \"18\":\n print(3)\n elif s[0] in \"ah\" or s[1] in \"18\":\n print(5)\n else:\n print(8)\n\nexcept EOFError:\n pass\n", "cd = input()\nc = cd[0]\nd = cd[1]\n\n...
{ "inputs": [ "e4\n", "a1\n", "h8\n", "a4\n", "g7\n", "e1\n", "b2\n", "c7\n", "h6\n", "c8\n", "h2\n", "h5\n", "a8\n", "f8\n", "h1\n", "f2\n", "e8\n", "h3\n", "b8\n", "g8\n", "d8\n", "h4\n", "b1\n", "a2\n" ], "outputs":...
interview
https://codeforces.com/problemset/problem/710/A
1,541
You have a description of a lever as string s. We'll represent the string length as record |s|, then the lever looks as a horizontal bar with weights of length |s| - 1 with exactly one pivot. We will assume that the bar is a segment on the Ox axis between points 0 and |s| - 1. The decoding of the lever description is ...
["s = input()\nsuc = 0\np = 0\nkde = 0\nfor i in range(len(s)):\n\tc = s[i]\n\tif c == '^':\n\t\tkde = i\nfor i in range(len(s)):\n\tc = s[i]\n\tif '1' <= c <= '9':\n\t\tsuc+=((kde-i)*int(c))\n\t\tp+=1\nif suc < 0:\n\tprint(\"right\")\nelif suc > 0:\n\tprint(\"left\")\nelse:\n\tprint(\"balance\")\n", "string = list(inp...
{ "inputs": [ "=^==\n", "9===^==1\n", "2==^7==\n", "41^52==\n", "=^2=4=1===1=\n", "9=6===5==3=9=1=1^7=1==\n", "85=61=36=^93===4==44==35==94===39===15===\n", "==88=^95==83=45===8====73===7==7====1=29====29=8=85=\n", "==41^52==\n", "2===================^2\n", "9^=============...
interview
https://codeforces.com/problemset/problem/376/A
1,728
You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by c_{v}. Initially c_{v} = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you...
["\n\nn = int(input())\n\nT = [int(x) for x in input().split()]\n\nC = [int(x) for x in input().split()]\n\nT_inv = [[] for _ in range(n)]\n\nfor i in range(n - 1):\n T_inv[T[i] - 1] += [i + 1]\n\nresult = 1\n\n#for i in range(n):\n# print(T_inv[i])\n\nfor i in range(n):\n for child in T_inv[i]:\n if C[...
{ "inputs": [ "6\n1 2 2 1 5\n2 1 1 1 1 1\n", "7\n1 1 2 3 1 4\n3 3 1 1 1 2 3\n", "2\n1\n2 2\n", "3\n1 1\n2 2 2\n", "4\n1 2 1\n1 2 3 4\n", "4\n1 2 3\n4 1 2 4\n", "5\n1 2 1 4\n1 1 1 2 2\n", "3\n1 2\n2 1 1\n", "4\n1 1 1\n3 1 3 1\n", "4\n1 1 2\n4 1 4 1\n", "4\n1 2 2\n3 1 2 3\n",...
interview
https://codeforces.com/problemset/problem/902/B
1,807
Once Max found an electronic calculator from his grandfather Dovlet's chest. He noticed that the numbers were written with seven-segment indicators (https://en.wikipedia.org/wiki/Seven-segment_display). [Image] Max starts to type all the values from a to b. After typing each number Max resets the calculator. Find the...
["def main():\n v = {\n \"1\" : 2,\n \"2\" : 5,\n \"3\" : 5,\n \"4\" : 4,\n \"5\" : 5,\n \"6\" : 6,\n \"7\" : 3,\n \"8\" : 7,\n \"9\" : 6,\n \"0\" : 6\n }\n a, b = map(int, input().split())\n answer = 0\n\n for i in range(a, b + 1):\n ...
{ "inputs": [ "1 3\n", "10 15\n", "1 100\n", "100 10000\n", "213 221442\n", "1 1000000\n", "1000000 1000000\n", "222145 353252\n", "2 1000000\n", "1 999999\n", "192 200\n" ], "outputs": [ "12\n", "39\n", "928\n", "188446\n", "5645356\n", "2873337...
interview
https://codeforces.com/problemset/problem/620/B
2,014
Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can...
["def main():\n n, k = tuple(map(int, input().split()))\n\n a = [set(range(n)) for _ in range(n)]\n\n for i in range(k):\n p = set()\n for j in map(int, input().split()):\n a[j-1] -= p\n p.add(j-1)\n\n sa = sorted(list(range(n)), key=lambda i: len(a[i]))\n maxx = [0] *...
{ "inputs": [ "4 3\n1 4 2 3\n4 1 2 3\n1 2 4 3\n", "6 3\n2 5 1 4 6 3\n5 1 4 3 2 6\n5 4 2 6 3 1\n", "41 4\n24 15 17 35 13 41 4 14 23 5 8 16 21 18 30 36 6 22 11 29 26 1 40 31 7 3 32 10 28 38 12 20 39 37 34 19 33 27 2 25 9\n22 13 25 24 38 35 29 12 15 8 11 37 3 19 4 23 18 32 30 40 36 21 16 34 27 9 5 41 39 2 14...
interview
https://codeforces.com/problemset/problem/463/D
2,113
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees. A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and ...
["n = int(input())\nedges = [[] for i in range(n)]\nfor i in range(n-1):\n u, v = map(int, input().split())\n edges[u-1].append(v-1)\n edges[v-1].append(u-1)\n\ncolors = [-1 for i in range(n)]\ndfs = [(0,0)]\nwhile len(dfs) > 0:\n node, color = dfs.pop()\n colors[node] = color\n for neighbor in edges[...
{ "inputs": [ "3\n1 2\n1 3\n", "5\n1 2\n2 3\n3 4\n4 5\n", "10\n3 8\n6 2\n9 7\n10 1\n3 5\n1 3\n6 7\n5 4\n3 6\n", "10\n7 6\n2 7\n4 1\n8 5\n9 4\n5 3\n8 7\n10 8\n10 4\n", "10\n2 6\n3 7\n8 4\n4 10\n6 9\n9 7\n3 10\n1 2\n5 8\n", "10\n6 9\n9 7\n9 4\n10 9\n9 1\n9 8\n9 2\n9 5\n3 9\n", "2\n1 2\n" ]...
interview
https://codeforces.com/problemset/problem/862/B
2,225
Xenia the beginner programmer has a sequence a, consisting of 2^{n} non-negative integers: a_1, a_2, ..., a_2^{n}. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a. Namely, it takes several iterations to calculate value v. At the first iterat...
["#!/usr/bin/pypy3\n\nfrom sys import stdin,stderr\nimport random\nimport cProfile\n\ndef readInts(): return map(int,stdin.readline().strip().split())\ndef print_err(*args,**kwargs): print(*args,file=stderr,**kwargs)\n \ndef solve(vs):\n return None\n\ndef generate_tree(n,ns):\n out = [0 for _ in range(2**(n+1...
{ "inputs": [ "2 4\n1 6 3 5\n1 4\n3 4\n1 2\n1 2\n", "1 1\n1 1\n1 1\n", "1 10\n6 26\n1 11\n1 9\n1 31\n1 10\n2 12\n1 8\n2 10\n2 4\n2 18\n1 31\n", "1 10\n22 17\n2 15\n2 12\n1 6\n1 16\n2 24\n1 21\n2 19\n2 25\n2 19\n2 1\n", "2 10\n15 27 17 18\n2 12\n4 19\n2 3\n4 1\n3 15\n3 11\n2 5\n1 26\n1 17\n3 18\n",...
interview
https://codeforces.com/problemset/problem/339/D
2,246
There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them....
["from sys import stdin\nfrom decimal import Decimal as D\ninput = stdin.readline\nn = int(input())\nadj = [[] for i in range(n+1)]\ntree = [[] for i in range(n+1)]\nvisit = [-1]*(n+1)\nlength = [-1]*(n+1)\nfor i in range(n-1):\n a, b = map(int,input().split())\n adj[a].append(b)\n adj[b].append(a)\nbfsord = [...
{ "inputs": [ "4\n1 2\n1 3\n2 4\n", "5\n1 2\n1 3\n3 4\n2 5\n", "70\n1 25\n57 1\n18 1\n65 1\n38 1\n1 41\n1 5\n1 69\n1 3\n31 1\n1 8\n1 9\n53 1\n70 1\n45 1\n1 24\n1 42\n1 30\n1 12\n1 37\n64 1\n1 28\n1 58\n1 22\n11 1\n1 4\n1 27\n1 16\n1 21\n54 1\n1 51\n1 43\n29 1\n56 1\n1 39\n32 1\n1 15\n1 17\n1 19\n1 40\n36 ...
interview
https://codeforces.com/problemset/problem/839/C
2,839
VisuAlgo (http://visualgo.net) is a website developed by a team of staff and students of School of Computing, National University of Singapore, the host of the 2015 ACM-ICPC Asia Singapore Regional. VisuAlgo visualizes a number of popular data structures and algorithms in the Computer Science curriculum. Currently, it ...
{ "inputs": [ "6 10\n0 1 26\n1 3 29\n1 5 9\n2 3 25\n2 4 43\n4 2 3\n5 0 13\n5 2 33\n5 3 18\n5 4 58\n5 1\n", "7 9\n0 1 1\n0 2 2\n1 2 1\n2 3 1\n2 4 3\n3 4 1\n4 5 1\n4 6 2\n5 6 1\n0 6\n", "5 6\n0 1 1\n1 2 2\n2 4 3\n0 3 3\n3 4 4\n0 4 6\n0 4\n" ], "outputs": [ "1\n", "4\n",...
interview
https://open.kattis.com/problems/visualgo
2,888
-----INOI 2017, Problem 2, Training----- Ash and his Pokemon Pikachu are going on a journey. Ash has planned his route for the journey so that it passes through N cities, numbered 1, 2, …, N, and in this order. When they set out, Pikachu has an initial strength of Sin as well as an experience value (XV) of 0. As they...
["#include<bits/stdc++.h>\r\nusing namespace std;\r\n#define mod 1000000007\r\n#define inf 100000000000000\r\ntypedef long long ll;\r\n#define mp make_pair\r\n#define f first \r\n#define s second\r\nll xp[5005];\r\nll train[5005];\r\nll dp[5005][5005];\r\nint n;\r\nll power(ll x,ll y){\r\n ll res=1;\r\n while (y)...
{"inputs": [["2 12", "5 10", "", ""], ["4 1", "100 1 6 2", "", ""]], "outputs": [["390"], ["120"]]}
interview
https://www.codechef.com/INOIPRAC/problems/TINOI17B
3,762
Fox Ciel studies number theory. She thinks a non-empty set S contains non-negative integers is perfect if and only if for any $a, b \in S$ (a can be equal to b), $(a \text{xor} b) \in S$. Where operation xor means exclusive or operation (http://en.wikipedia.org/wiki/Exclusive_or). Please calculate the number of perfe...
["from math import factorial\nMOD = 10**9+7\n\nk = int(input())\nbink = list(map(int, bin(k)[2:]))\nN = len(bink)\n\n# dp[i][j][k] = first i bits, j bases,\n# k = 1 if maxor matches k, 0 else\ndp = [[[0,0] for j in range(i+2)] for i in range(N+1)]\ndp[0][0][1] = 1\nfor i in range(1, N+1):\n for j in range(i+1):\n ...
{ "inputs": [ "1\n", "2\n", "3\n", "4\n", "8\n", "5\n", "6\n", "10\n", "20\n", "1023\n", "536870911\n", "641009859\n", "524125987\n", "702209411\n", "585325539\n", "58376259\n", "941492387\n", "824608515\n", "2691939\n", "802030518\n", ...
competition
https://codeforces.com/problemset/problem/388/D
3,829
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game. The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, ...
["\"\"\"\nCodeforces Contest 259 Div 1 Problem A\n\nAuthor : chaotic_iak\nLanguage: Python 3.3.4\n\"\"\"\n\ndef main():\n m,n = read()\n print(m - sum((i/m)**n for i in range(1,m)))\n\n################################### NON-SOLUTION STUFF BELOW\n\ndef read(mode=2):\n # 0: String\n # 1: List of strings\n ...
{ "inputs": [ "6 1\n", "6 3\n", "2 2\n", "5 4\n", "5 8\n", "3 10\n", "3 6\n", "1 8\n", "24438 9\n", "94444 9\n", "8 66716\n", "4 25132\n", "51520 73331\n", "54230 31747\n", "24236 90163\n", "26946 99523\n", "50323 7\n", "53033 3\n", "55743 5\...
competition
https://codeforces.com/problemset/problem/453/A
4,137
In this problem you will write a simple generator of Brainfuck (https://en.wikipedia.org/wiki/Brainfuck) calculators. You are given an arithmetic expression consisting of integers from 0 to 255 and addition/subtraction signs between them. Output a Brainfuck program which, when executed, will print the result of evalua...
["import re\n\ns = input()\nops = re.split('([+-])', s)\nassert len(ops) % 2 == 1\nops = ['+'] + ops\n\ntotal = 0\nfor i in range(0, len(ops), 2):\n if ops[i] == '+':\n total += int(ops[i+1])\n elif ops[i] == '-':\n total -= int(ops[i+1])\n else:\n assert False\n\nfor b in bytes(str(total)...
{ "inputs": [ "2+3\n", "9-7\n", "1+1+1\n", "1+11+111\n", "111-11-1\n", "1+1-1+1-1+1-1+1-1+1\n", "9+1\n", "10-1\n", "31+49+49+71-51-61+59-111+51\n", "255+255+255+255+255-255-255-255-255-255\n", "100+100+10+10+10+10+10+5\n", "255-255+255-255+255-255+255-255+255\n", "0...
introductory
https://codeforces.com/problemset/problem/784/G
4,866
HiQ recently got an assignment from a client to create a clone of the immensely popular website https://IsItHalloween.com. The website is a very simple one. People will visit the site occasionally to see if it is Halloween. Whenever it is, the website should print out yup, otherwise it should print out nope on the scre...
{ "inputs": [ "OCT 31\n", "JUN 24\n" ], "outputs": [ "yup\n", "nope\n" ] }
introductory
https://open.kattis.com/problems/isithalloween