task_id
stringlengths
11
12
prompt
stringlengths
145
1.35k
entry_point
stringlengths
3
25
canonical_solution
stringlengths
17
1.88k
test
stringlengths
253
113k
HumanEval/0
from typing import List def has_close_elements(numbers: List[float], threshold: float) -> bool: """ Check if in a given list of numbers, there are any two numbers closer to each other than the given threshold. Examples: >>> has_close_elements([1.0, 2.0, 3.0], 0.5) False >>> ha...
has_close_elements
if len(numbers) < 2: return False for idx, elem in enumerate(numbers): for idx2, elem2 in enumerate(numbers): if idx != idx2: distance = abs(elem - elem2) if distance < threshold: return True return False
def check(candidate): assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.3) == True assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.05) == False assert candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.95) == True assert candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.8) == False assert candidate([1.0, 2.0, 3.0, 4.0...
HumanEval/1
from typing import List def separate_paren_groups(paren_string: str) -> List[str]: """ Input to this function is a string possibly containing multiple groups of nested parentheses. Your goal is to separate those groups into separate strings and return the list of those. Ignore anything other than "(" ...
separate_paren_groups
input = "".join([x for x in paren_string if x in '()']) breakpoints = input.split('()') groups = [] for i in range(len(breakpoints) - 1): group = '()' while breakpoints[i] != "" and breakpoints[i].endswith('(') and breakpoints[i + 1] != "" and breakpoints[i + 1].startswith(')'): ...
def check(candidate): assert candidate('()') == ['()'] assert candidate('()()') == ['()', '()'] assert candidate('(())') == ['(())'] assert candidate('') == [] assert candidate('True') == [] assert candidate('{}') == [] assert candidate('((') == [] assert candidate('))') == [] assert...
HumanEval/2
def truncate_number(number: float) -> float: """ Given a positive floating point number, it can be decomposed into and integer part (largest integer smaller than given number) and decimals (leftover part always smaller than 1). Return the decimal part of the number. Example: >>> truncat...
truncate_number
assert number > 0 return number % 1.0
def check(candidate): assert candidate(3.5) == 0.5 assert candidate(1.33) < 0.330001 assert candidate(1.33) > 0.329999 assert candidate(123.456) < 0.456001 assert candidate(123.456) > 0.455999 assert candidate(9.99999) < 0.99999001 assert candidate(9.99999) > 0.99998999 assert candidate(...
HumanEval/3
from typing import List def below_zero(operations: List[int]) -> bool: """ You're given a list of deposit and withdrawal operations on a bank account that starts with zero balance. Your task is to detect if at any point the balance of account falls below zero, and at that point function should return ...
below_zero
balance = 0 for op in operations: balance += op if balance < 0: return True return False
def check(candidate): assert candidate([]) == False assert candidate([-1]) == True assert candidate([1]) == False assert candidate([10000000, -10000000]) == False assert candidate([1, 2, -3, 1, 2, -3]) == False assert candidate([1, 2, -4, 5, 6]) == True assert candidate([2, 2, 2, 2, 2, -10, ...
HumanEval/4
from typing import List def mean_absolute_deviation(numbers: List[float]) -> float: """ For a given list of input numbers, calculate Mean Absolute Deviation around the mean of this dataset. Mean Absolute Deviation is the average absolute difference between each element and a centerpoint (mean in t...
mean_absolute_deviation
assert len(numbers) > 0 mean = sum(numbers) / len(numbers) return sum(abs(x - mean) for x in numbers) / len(numbers)
def check(candidate): assert abs(candidate([1.0, 2.0, 3.0]) - 2.0 / 3.0) < 1e-6 assert abs(candidate([1.0, 2.0, 3.0, 4.0]) - 1.0) < 1e-6 assert abs(candidate([1.0, 2.0, 3.0, 4.0, 5.0]) - 6.0 / 5.0) < 1e-6 assert abs(candidate([0.0, 0.0, 0.0]) - 0.0) < 1e-6 assert abs(candidate([-1.0, 1.0]) - 1.0) < ...
HumanEval/5
from typing import List def intersperse(numbers: List[int], delimeter: int) -> List[int]: """ Insert a number 'delimeter' between every two consecutive elements of input list `numbers' Example: >>> intersperse([1, 2, 3], 4) [1, 4, 2, 4, 3] """
intersperse
if not numbers: return [] result = [] for n in numbers[:-1]: result.append(n) result.append(delimeter) result.append(numbers[-1]) return result
def check(candidate): assert candidate([], 7) == [] assert candidate([1], 1) == [1] assert candidate([1, 3], 2) == [1, 2, 3] assert candidate([5, 6, 3, 2], 8) == [5, 8, 6, 8, 3, 8, 2] assert candidate([2, 2, 2], 2) == [2, 2, 2, 2, 2] assert candidate([0, 0, 0, 0, 0], 0) == [0, 0, 0, 0, 0, 0, 0, ...
HumanEval/6
from typing import List def parse_nested_parens(paren_string: str) -> List[int]: """ Input to this function is a string containing "(" and ")"s (not exclusively), possibly including multiple groups for nested parentheses. For each of these groups, output the deepest level of nesting of parentheses. ...
parse_nested_parens
input = "".join([x for x in paren_string if x in '()']) breakpoints = input.split('()') groups = [] for i in range(len(breakpoints) - 1): group = '()' while breakpoints[i] != "" and breakpoints[i].endswith('(') and breakpoints[i + 1] != "" and breakpoints[i + 1].startswith(')'): ...
def check(candidate): assert candidate('') == [] assert candidate('(())') == [2] assert candidate(')(') == [] assert candidate('((') == [] assert candidate('))') == [] assert candidate('((())') == [2] assert candidate('(()))') == [2] assert candidate('()') == [1] assert candidate('((...
HumanEval/7
from typing import List def filter_by_substring(strings: List[str], substring: str) -> List[str]: """ Filter an input list of strings only for ones that contain given substring. Example: >>> filter_by_substring(['abc', 'bacd', 'cde', 'array'], 'a') ['abc', 'bacd', 'array'] """
filter_by_substring
return [x for x in strings if substring in x]
def check(candidate): assert candidate([], 'john') == [] assert candidate([], '') == [] assert candidate(['', ' ', ' '], ' ') == [' ', ' '] assert candidate(['jo'], 'john') == [] assert candidate(['john'], 'john') == ['john'] assert candidate(['JOHN'], 'john') == [] assert candidate(['jo...
HumanEval/8
from typing import List, Tuple def sum_product(numbers: List[int]) -> Tuple[int, int]: """ For a given list of integers, return a tuple consisting of a sum and a product of all the integers in a list. Empty sum should be equal to 0 and empty product should be equal to 1. Example: >>> sum_prod...
sum_product
sum_value = 0 prod_value = 1 for n in numbers: sum_value += n prod_value *= n return sum_value, prod_value
def check(candidate): assert candidate([]) == (0, 1) assert candidate([1, 1, 1]) == (3, 1) assert candidate([100, 0]) == (100, 0) assert candidate([3, 5, 7]) == (15, 105) assert candidate([10]) == (10, 10) assert candidate([-1, -2, -3]) == (-6, -6) assert candidate([0, 0, 0]) == (0, 0) a...
HumanEval/9
from typing import List def rolling_max(numbers: List[int]) -> List[int]: """ From a given list of integers, generate a list of rolling maximum element found until given moment in the sequence. Example: >>> rolling_max([1, 2, 3, 2, 3, 4, 2]) [1, 2, 3, 3, 3, 4, 4] """
rolling_max
running_max = None result = [] for n in numbers: if running_max is None: running_max = n else: running_max = max(running_max, n) result.append(running_max) return result
def check(candidate): assert candidate([]) == [] assert candidate([-1]) == [-1] assert candidate([0, 1, 2, 3, 4]) == [0, 1, 2, 3, 4] assert candidate([4, 3, 2, 1]) == [4, 4, 4, 4] assert candidate([3, 2, 3, 100, 3]) == [3, 3, 3, 100, 100] assert candidate([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5] ...
HumanEval/10
def make_palindrome(string: str) -> str: """ Find the shortest palindrome that begins with a supplied string. Algorithm idea is simple: - Find the longest postfix of supplied string that is a palindrome. - Append to the end of the string reverse of a string prefix that comes before the palindromic s...
make_palindrome
if not string: return "" def is_palindrome(string: str) -> bool: """ Test if given string is a palindrome """ return string == string[::-1] beginning_of_suffix = 0 while not is_palindrome(string[beginning_of_suffix:]): beginning_of_suffix += 1 return string + s...
def check(candidate): assert candidate("") == "" assert candidate("x") == "x" assert candidate("xyz") == "xyzyx" assert candidate("xyx") == "xyx" assert candidate("noon") == "noon" assert candidate("race") == "racecar" assert candidate("jerry") == "jerryrrej" assert candidate("1234543") ...
HumanEval/11
from typing import List def string_xor(a: str, b: str) -> str: """ Input are two strings a and b consisting only of 1s and 0s. Perform binary XOR on these inputs and return result also as a string. Example: >>> string_xor('10', '110') '100' """
string_xor
if len(a) < len(b): a = '0' * (len(b) - len(a)) + a elif len(b) < len(a): b = '0' * (len(a) - len(b)) + b def xor(i, j): if i == j: return '0' else: return '1' return ''.join(xor(x, y) for x, y in zip(a, b))
def check(candidate): assert candidate('111000', '101010') == '010010' assert candidate('1', '1') == '0' assert candidate('0', '0') == '0' assert candidate('1', '0') == '1' assert candidate('0', '1') == '1' assert candidate('0101', '0000') == '0101' assert candidate('', '101') == '101' a...
HumanEval/12
from typing import List def longest(strings: List[str]) -> str: """ Out of a list of strings, return the longest one. Return the first one in case of multiple strings of the same length. Return an empty string for edge cases. Examples: >>> longest(['a', 'b', 'c']) 'a' >>> ...
longest
if not strings: return '' maxlen = max(len(x) for x in strings) for s in strings: if len(s) == maxlen: return s
def check(candidate): assert candidate([]) == '' assert candidate(['x', 'y', 'z']) == 'x' assert candidate(['x', 'yyy', 'zzzz', 'www', 'kkkk', 'abc']) == 'zzzz' assert candidate(['bb', 'aa']) == 'bb' assert candidate(['\n\n\n', 'n']) == '\n\n\n' assert candidate(['', ' n ', 'nnn']) == ' n ' ...
HumanEval/13
def greatest_common_divisor(a: int, b: int) -> int: """ Return the greatest common divisor of two integers a and b Examples: >>> greatest_common_divisor(3, 5) 1 >>> greatest_common_divisor(25, 15) 5 """
greatest_common_divisor
while b: a, b = b, a % b return abs(a)
def check(candidate): assert candidate(3, 7) == 1 assert candidate(10, 15) == 5 assert candidate(49, 14) == 7 assert candidate(144, 60) == 12 assert candidate(8, 0) == 8 assert candidate(0, -4) == 4 assert candidate(-5, -10) == 5 assert candidate(-2, 9) == 1 def test_check(): check...
HumanEval/14
from typing import List def all_prefixes(string: str) -> List[str]: """ Return list of all prefixes from shortest to longest of the input string. Example: >>> all_prefixes('abc') ['a', 'ab', 'abc'] """
all_prefixes
result = [] for i in range(len(string)): result.append(string[:i+1]) return result
def check(candidate): assert candidate('') == [] assert candidate(' a ') == [' ', ' a', ' a '] assert candidate('abc\nde\tf') == ['a', 'ab', 'abc', 'abc\n', 'abc\nd', 'abc\nde', 'abc\nde\t', 'abc\nde\tf'] assert candidate('asdfgh') == ['a', 'as', 'asd', 'asdf', 'asdfg', 'asdfgh'] assert candidate('W...
HumanEval/15
def string_sequence(n: int) -> str: """ Return a string containing space-delimited numbers starting from 0 upto n inclusive. If n is negative, return an empty string. Examples: >>> string_sequence(0) '0' >>> string_sequence(5) '0 1 2 3 4 5' """
string_sequence
if n < 0: return '' return ' '.join([str(x) for x in range(n + 1)])
def check(candidate): assert candidate(0) == '0' assert candidate(1) == '0 1' assert candidate(3) == '0 1 2 3' assert candidate(10) == '0 1 2 3 4 5 6 7 8 9 10' assert candidate(100) == '0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 4...
HumanEval/16
def count_distinct_characters(string: str) -> int: """ Find out how many distinct characters (regardless of case) are in the string. Examples: >>> count_distinct_characters("xyzXYZ") 3 >>> count_distinct_characters("Jerry") 4 """
count_distinct_characters
return len(set(string.lower()))
def check(candidate): assert candidate("") == 0 assert candidate("abcde") == 5 assert candidate("abcdecadeCADE") == 5 assert candidate("aaaaAAAAaaaa") == 1 assert candidate("Jerry jERRY JeRRRY") == 5 assert candidate(' ...
HumanEval/17
from typing import List def parse_music(music_string: str) -> List[int]: """ Input to this function is a string representing musical notes in a special ASCII format. Your task is to parse this string and return list of integers corresponding to how many beats does each note last. Here is a legend...
parse_music
note_map = {'o': 4, 'o|': 2, '.|': 1} return [note_map[x] for x in music_string.split(' ') if x]
def check(candidate): assert candidate('') == [] assert candidate('o o o o') == [4, 4, 4, 4] assert candidate('.| .| .| .|') == [1, 1, 1, 1] assert candidate('o| o| .| .| o o o o') == [2, 2, 1, 1, 4, 4, 4, 4] assert candidate('o| .| o| .| o o| o o|') == [2, 1, 2, 1, 4, 2, 4, 2] assert candidate(...
HumanEval/18
def how_many_times(string: str, substring: str) -> int: """ Find how many times a given substring can be found in the original string. Count overlapping cases. Example: >>> how_many_times('aaaa', 'aa') 3 """
how_many_times
times = 0 if not substring: return len(string) + 1 for i in range(len(string) - len(substring) + 1): if string[i:i + len(substring)] == substring: times += 1 return times
def check(candidate): assert candidate('', 'x') == 0 assert candidate('xyxyxyx', 'x') == 4 assert candidate('cacacacac', 'cac') == 4 assert candidate('john doe', 'john') == 1 assert candidate('1 2 324 5', ' 2') == 1 assert candidate('xy', 'xyz') == 0 assert candidate('abc', '') == 4 ass...
HumanEval/19
def sort_numbers(numbers: str) -> str: """ Input is a space-delimited string of numerals from 'zero' to 'nine'. Valid choices are 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight' and 'nine'. Return the string with numbers sorted from smallest to largest Example: >>> so...
sort_numbers
value_map = { 'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9 } return ' '.join(sorted([x for x in numbers.split(' ') if x and x in value_map], key=lambda x: value_map...
def check(candidate): assert candidate('') == '' assert candidate('three') == 'three' assert candidate('two two') == 'two two' assert candidate('three five nine') == 'three five nine' assert candidate(' two one three ') == 'one two three' assert candidate('five zero four seven nine eight'...
HumanEval/20
from typing import List, Tuple def find_closest_elements(numbers: List[float]) -> Tuple[float, float]: """ From a supplied list of numbers (of length at least two) select and return the last two elements that are the closest to each other and return them in order (smaller number, larger number). ...
find_closest_elements
assert len(numbers) >= 2 closest_pair = None distance = None for idx, elem in enumerate(numbers): for idx2, elem2 in enumerate(numbers): if idx != idx2: if distance is None: distance = abs(elem - elem2) closest_pair = tuple(so...
def check(candidate): assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2]) == (3.9, 4.0) assert candidate([1.0, 2.0, 5.9, 4.0, 5.0]) == (5.0, 5.9) assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.2]) == (2.0, 2.2) assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0]) == (2.0, 2.0) assert candidate([1.1, 2.2, 3.1...
HumanEval/21
from typing import List def rescale_to_unit(numbers: List[float]) -> List[float]: """ Given a list of numbers (of at least two elements), apply a linear transform to that list, such that the smallest number will become 0 and the largest will become 1. Return an empty list for any undefined edge cases....
rescale_to_unit
if len(set(numbers)) < 2: return [] min_number = min(numbers) max_number = max(numbers) return [(x - min_number) / (max_number - min_number) for x in numbers]
def check(candidate): assert candidate([]) == [] assert candidate([1.0]) == [] assert candidate([1.0, 2.0]) == [0.0, 1.0] assert candidate([-4.0, -4.0, -4.0]) == [] assert candidate([0.0, 0.0]) == [] assert candidate([2.0, 49.9]) == [0.0, 1.0] assert candidate([100.0, 49.9]) == [1.0, 0.0] ...
HumanEval/22
from typing import List def filter_integers(values: List[str]) -> List[int]: """ Filter given list of string values only for integers. Example: >>> filter_integers(['10', ' 123 ', '5.0', three', 'abc', '{}']) [10] """
filter_integers
return [int(x) for x in values if x.isdigit()]
def check(candidate): assert candidate([]) == [] assert candidate(['4', '{}', '[1]', '23.2', '9', 'adasd']) == [4, 9] assert candidate(['3', 'c', '3', '3', 'a', 'b']) == [3, 3, 3] assert candidate(['0', '1', 'O', 'o', 'False', '1e5']) == [0, 1] assert candidate(['10-8', '10 - 8', '4*2', '4 / 2', '4+...
HumanEval/23
def strlen(string: str) -> int: """ Return the length of the given string. Example: >>> strlen('abc') 3 """
strlen
return len(string)
def check(candidate): assert candidate('') == 0 assert candidate(' ') == 1 assert candidate('x') == 1 assert candidate('asdasnakj') == 9 assert candidate(' s p a c e s ') == 23 assert candidate('a\nb\tc') == 5 assert candidate('False') == 5 assert candidate('10') == 2 asser...
HumanEval/24
def largest_divisor(n: int) -> int: """ Find the largest number that divides n evenly, smaller than n. Return -1 if such a number does not exist. Example: >>> largest_divisor(15) 5 """
largest_divisor
if n <= 0: return -1 for i in reversed(range(n)): if n % i == 0: return i
def check(candidate): assert candidate(3) == 1 assert candidate(7) == 1 assert candidate(10) == 5 assert candidate(100) == 50 assert candidate(49) == 7 assert candidate(-4) == -1 assert candidate(0) == -1 def test_check(): check(largest_divisor)
HumanEval/25
from typing import List def factorize(n: int) -> List[int]: """ Return a list of prime factors of the given integer in the order of smallest to largest. Each of the factors should be listed as often as it appears in the factorization. The input number should be equal to the product of all factors. ...
factorize
assert n > 1 import math fact = [] i = 2 while i <= int(math.sqrt(n) + 1): if n % i == 0: fact.append(i) n //= i else: i += 1 if n > 1: fact.append(n) return fact
def check(candidate): assert candidate(2) == [2] assert candidate(4) == [2, 2] assert candidate(8) == [2, 2, 2] assert candidate(57) == [3, 19] assert candidate(3249) == [3, 3, 19, 19] assert candidate(185193) == [3, 3, 3, 19, 19, 19] assert candidate(20577) == [3, 19, 19, 19] assert can...
HumanEval/26
from typing import List def remove_duplicates(numbers: List[int]) -> List[int]: """ From a list of integers, remove all the elements that occur more than once. Keep the order of the elements the same as in the input. Example: >>> remove_duplicates([1, 2, 3, 2, 4]) [1, 3, 4] """
remove_duplicates
import collections c = collections.Counter(numbers) return [n for n in numbers if c[n] <= 1]
def check(candidate): assert candidate([]) == [] assert candidate([1, 1, 1]) == [] assert candidate([1, 2, 3, 4]) == [1, 2, 3, 4] assert candidate([1, 2, 3, 2, 4, 3, 5]) == [1, 4, 5] assert candidate([-1, -2, -3, -2, -4, -3, -5]) == [-1, -4, -5] assert candidate([1, -1]) == [1, -1] def test_ch...
HumanEval/27
def flip_case(string: str) -> str: """ For a given string, flip lowercase characters to uppercase and uppercase to lowercase. Example: >>> flip_case('Hello') 'hELLO' """
flip_case
return string.swapcase()
def check(candidate): assert candidate('') == '' assert candidate('Hello!') == 'hELLO!' assert candidate('These violent delights have violent ends') == 'tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS' assert candidate('n\nN') == 'N\nn' assert candidate(' spa\tces ') == ' SPA\tCES ' assert ...
HumanEval/28
from typing import List def concatenate(strings: List[str]) -> str: """ Concatenate list of strings into a single string. Example: >>> concatenate(['a', 'b', 'c']) 'abc' """
concatenate
return ''.join(strings)
def check(candidate): assert candidate([]) == '' assert candidate(['', '', '']) == '' assert candidate(['x', 'yy', 'zzz']) == 'xyyzzz' assert candidate(['x', 'y', 'z,', 'w', 'k!']) == 'xyz,wk!' assert candidate(['\n', '\t', ' ']) == '\n\t ' assert candidate(['\"', 'string', '\"']) == '\"string\"...
HumanEval/29
from typing import List def filter_by_prefix(strings: List[str], prefix: str) -> List[str]: """ Filter an input list of strings only for ones that start with a given prefix. Examples: >>> filter_by_prefix([], 'a') [] >>> filter_by_prefix(['abc', 'bcd', 'cde', 'array'], 'a') ...
filter_by_prefix
return [x for x in strings if x.startswith(prefix)]
def check(candidate): assert candidate([], 'john') == [] assert candidate(['john', 'jo', 'johnny'], 'john') == ['john', 'johnny'] assert candidate(['a', '', ' ', 'bbb', '!C@'], '') == ['a', '', ' ', 'bbb', '!C@'] assert candidate(['caps', 'CAPS', 'CaPs', 'cApS'], 'CAPS') == ['CAPS'] assert candidate...
HumanEval/30
from typing import List def get_positive(l: List[int]) -> List[int]: """ Return only the positive numbers in the list. Example: >>> get_positive([-1, 2, -4, 5, 6]) [2, 5, 6] """
get_positive
return [e for e in l if e > 0]
def check(candidate): assert candidate([-1, -2, 4, 5, 6]) == [4, 5, 6] assert candidate([5, 3, -5, 2, 3, 3, 9, 0, 123, 1, -10]) == [5, 3, 2, 3, 3, 9, 123, 1] assert candidate([-1, -2]) == [] assert candidate([0]) == [] assert candidate([]) == [] assert candidate([2, 2, 2]) == [2, 2, 2] def tes...
HumanEval/31
def is_prime(n: int) -> bool: """ Check if given number is considered to be prime. Examples: >>> is_prime(6) False >>> is_prime(100) False >>> is_prime(11) True """
is_prime
if n < 2: return False for k in range(2, n - 1): if n % k == 0: return False return True
def check(candidate): assert candidate(-5) == False assert candidate(-1) == False assert candidate(0) == False assert candidate(1) == False assert candidate(2) == True assert candidate(3) == True assert candidate(4) == False assert candidate(5) == True assert candidate(6) == False ...
HumanEval/32
import math from typing import List def poly(xs: list, x: float) -> float: """ Evaluates polynomial with coefficients xs at point x. return xs[0] + xs[1] * x + xs[1] * x^2 + .... xs[n] * x^n """ return sum([coeff * math.pow(x, i) for i, coeff in enumerate(xs)]) def find_zero(xs: List[int]) -> fl...
find_zero
begin, end = -1., 1. while poly(xs, begin) * poly(xs, end) > 0: begin *= 2.0 end *= 2.0 while end - begin > 1e-10: center = (begin + end) / 2.0 if poly(xs, center) * poly(xs, begin) > 0: begin = center else: end = center return begin
def check(candidate): assert abs(candidate([-10, -2]) - (-5.000000000058208)) < 1e-6 assert abs(candidate([-3, -6, -7, 7]) - (1.6679422343731858)) < 1e-6 assert abs(candidate([8, 3]) - (-2.666666666686069)) < 1e-6 assert abs(candidate([-10, -8]) - (-1.2500000000582077)) < 1e-6 assert abs(candidate([...
HumanEval/33
from typing import List def sort_third(l: List[int]) -> List[int]: """ This function takes a list l and returns a list l' such that l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal to the values of the correspo...
sort_third
l = list(l) l[::3] = sorted(l[::3]) return l
def check(candidate): assert candidate([]) == [] assert candidate([1, 2, 3]) == [1, 2, 3] assert candidate([4, 2, 3, 1]) == [1, 2, 3, 4] assert candidate([5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10]) == [1, 3, -5, 2, -3, 3, 5, 0, 123, 9, -10] assert candidate([5, 8, -12, 4, 23, 2, 3, 11, 12, -10]) == [-1...
HumanEval/34
from typing import List def unique(l: List[int]) -> List[int]: """ Return sorted unique elements in a list. Example: >>> unique([5, 3, 5, 2, 3, 3, 9, 0, 123]) [0, 2, 3, 5, 9, 123] """
unique
return sorted(list(set(l)))
def check(candidate): assert candidate([]) == [] assert candidate([2, 1, 2, 1, 2]) == [1, 2] assert candidate([-3, -2, -4, -2, -2, -3, -4]) == [-4, -3, -2] assert candidate([1, 2, 3]) == [1, 2, 3] assert candidate([0, 0, -1, 0, 1, 0, 1]) == [-1, 0, 1] assert candidate([5, 3, 5, 2, 3, 3, 9, 0, 12...
HumanEval/35
from typing import List def max_element(l: List[int]) -> int: """ Return the maximum element in the list. Assume that the list is not empty. Example: >>> max_element([1, 2, 3]) 3 """
max_element
assert len(l) > 0 m = l[0] for e in l: if e > m: m = e return m
def check(candidate): assert candidate([0]) == 0 assert candidate([-3, -8, -21]) == -3 assert candidate([1, 2, 3, 4, 0, 1, 2, 3]) == 4 assert candidate([5, 3, -5, 2, -3, 3, 9, 0, 124, 1, -10]) == 124 assert candidate([4, 4, 4, 3, 3, 3, 5, 0, 2, 2]) == 5 def test_check(): check(max_element)
HumanEval/36
def fizz_buzz(n: int) -> int: """ Return the number of times the digit 7 appears in integers less than n which are divisible by 11 or 13. Examples: >>> fizz_buzz(50) 0 >>> fizz_buzz(78) 2 >>> fizz_buzz(79) 3 """
fizz_buzz
assert n >= 0 ns = [] for i in range(n): if i % 11 == 0 or i % 13 == 0: ns.append(i) s = ''.join(list(map(str, ns))) ans = 0 for c in s: ans += (c == '7') return ans
def check(candidate): assert candidate(0) == 0 assert candidate(1) == 0 assert candidate(50) == 0 assert candidate(78) == 2 assert candidate(79) == 3 assert candidate(100) == 3 assert candidate(200) == 6 assert candidate(4000) == 192 assert candidate(10000) == 639 assert candidat...
HumanEval/37
from typing import List def sort_even(l: List[int]) -> List[int]: """ This function takes a list l and returns a list l' such that l' is identical to l in the odd indicies, while its values at the even indicies are equal to the values of the even indicies of l, but sorted. Examples: >>> s...
sort_even
evens = l[::2] odds = l[1::2] evens.sort() ans = [] for e, o in zip(evens, odds): ans.extend([e, o]) if len(evens) > len(odds): ans.append(evens[-1]) return ans
def check(candidate): assert candidate([]) == [] assert candidate([1, 2, 3]) == [1, 2, 3] assert candidate([5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10]) == [-10, 3, -5, 2, -3, 3, 5, 0, 9, 1, 123] assert candidate([5, 8, -12, 4, 23, 2, 3, 11, 12, -10]) == [-12, 8, 3, 4, 5, 2, 12, 11, 23, -10] def test_check...
HumanEval/38
def encode_cyclic(s: str) -> str: """ Returns encoded string by cycling groups of three characters. """ # split string to groups. Each of length 3. groups = [s[(3 * i):min((3 * i + 3), len(s))] for i in range((len(s) + 2) // 3)] # cycle elements in each group. Unless group has fewe...
decode_cyclic
return encode_cyclic(encode_cyclic(s))
def check(candidate): assert candidate("[Z*uqK1`_ZGX<: C*[8vFn)-SpO") == "*[ZKuq_1`XZG <:[C*F8v-n)OSp" assert candidate("bo(") == "(bo" assert candidate("fH:*B=]1a@A*W2C~7i95~!(.1") == ":fH=*Ba]1*@ACW2i~7~95.!(1" assert candidate("uWiFq6U+hJ|f") == "iuW6FqhU+fJ|" assert candidate("NT)OBVt; yxb") == ...
HumanEval/39
def prime_fib(n: int) -> int: """ prime_fib returns n-th number that is a Fibonacci number and also prime. Examples: >>> prime_fib(1) 2 >>> prime_fib(2) 3 >>> prime_fib(3) 5 >>> prime_fib(4) 13 >>> prime_fib(5) 89 """
prime_fib
import math assert n > 0 def is_prime(p: int) -> bool: if p < 2: return False for k in range(2, min(int(math.sqrt(p)) + 1, p - 1)): if p % k == 0: return False return True f = [0, 1] while True: f.append(f[-1] + f[-2]) ...
def check(candidate): assert candidate(1) == 2 assert candidate(2) == 3 assert candidate(3) == 5 assert candidate(4) == 13 assert candidate(5) == 89 assert candidate(6) == 233 assert candidate(7) == 1597 assert candidate(8) == 28657 assert candidate(9) == 514229 assert candidate(...
HumanEval/40
from typing import List def triples_sum_to_zero(l: List[int]) -> bool: """ Check if there are three distinct elements in the list that sum to zero. Examples: >>> triples_sum_to_zero([1, 3, 5, 0]) False >>> triples_sum_to_zero([1, 3, -2, 1]) True """
triples_sum_to_zero
for i in range(len(l)): for j in range(i + 1, len(l)): for k in range(j + 1, len(l)): if l[i] + l[j] + l[k] == 0: return True return False
def check(candidate): assert candidate([1, 3, 5, 0]) == False assert candidate([1, 3, 5, -1]) == False assert candidate([1, 3, -2, 1]) == True assert candidate([1, 2, 3, 7]) == False assert candidate([1, 2, 5, 7]) == False assert candidate([2, 4, -5, 3, 9, 7]) == True assert candidate([-5, 0...
HumanEval/41
def car_race_collision(n: int) -> int: """ Imagine a road that's a perfectly straight infinitely long line. n cars are driving left to right; simultaneously, a different set of n cars are driving right to left. n cars -> ..... <- n other cars The two sets of cars start out being very far f...
car_race_collision
assert n >= 0 return n**2
def check(candidate): assert candidate(0) == 0 assert candidate(1) == 1 assert candidate(2) == 4 assert candidate(3) == 9 assert candidate(4) == 16 assert candidate(8) == 64 assert candidate(10) == 100 def test_check(): check(car_race_collision)
HumanEval/42
from typing import List def incr_list(l: List[int]) -> List[int]: """ Increment all elements of the list by 1. Example: >>> incr_list([1, 2, 3]) [2, 3, 4] """
incr_list
return [(e + 1) for e in l]
def check(candidate): assert candidate([]) == [] assert candidate([3, 2, 1]) == [4, 3, 2] assert candidate([5, 2, 5, 2, 3, 3, 9, 0, 123]) == [6, 3, 6, 3, 4, 4, 10, 1, 124] assert candidate([1, 0, -1, -2, -3]) == [2, 1, 0, -1, -2] def test_check(): check(incr_list)
HumanEval/43
from typing import List def pairs_sum_to_zero(l: List[int]) -> bool: """ Check if there are two distinct elements in the list that sum to zero. Examples: >>> pairs_sum_to_zero([1, 3, 5, 0]) False >>> pairs_sum_to_zero([2, 4, -5, 3, 5, 7]) True """
pairs_sum_to_zero
for i, l1 in enumerate(l): for j in range(i + 1, len(l)): if l1 + l[j] == 0: return True return False
def check(candidate): assert candidate([1, 3, 5, 0]) == False assert candidate([1, 3, -2, 1]) == False assert candidate([1, 2, 3, 7]) == False assert candidate([2, 4, -5, 3, 5, 7]) == True assert candidate([1]) == False assert candidate([]) == False assert candidate([0, 0]) == True asse...
HumanEval/44
def change_base(x: int, base: int) -> str: """ Change numerical base of input number x to base. return string representation after the conversion. base numbers are less than 10. Examples: >>> change_base(8, 3) '22' >>> change_base(8, 2) '1000' >>> change_base...
change_base
ret = "" while x > 0: ret = str(x % base) + ret x //= base return ret
def check(candidate): assert candidate(8, 3) == "22" assert candidate(9, 3) == "100" assert candidate(234, 2) == "11101010" assert candidate(16, 2) == "10000" assert candidate(8, 2) == "1000" assert candidate(7, 2) == "111" assert candidate(2, 3) == "2" assert candidate(3, 4) == "3" ...
HumanEval/45
def triangle_area(a: float, h: float) -> float: """ Given length of a side and height, return area for a triangle. Examples: >>> triangle_area(5, 3) 7.5 """
triangle_area
return abs(a) * abs(h) / 2.0
def check(candidate): assert candidate(3, 5) == 7.5 assert candidate(2, 2) == 2.0 assert candidate(10, 8) == 40.0 assert candidate(0, 0) == 0.0 assert candidate(2, -8) == 8.0 assert candidate(-8, 2) == 8.0 def test_check(): check(triangle_area)
HumanEval/46
def fib4(n: int) -> int: """ The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fib4(0) -> 0 fib4(1) -> 0 fib4(2) -> 2 fib4(3) -> 0 fib4(n) -> fib4(n - 1) + fib4(n - 2) + fib4(n - 3) + fib4(n - 4) Please write a functi...
fib4
assert n >= 0 results = [0, 0, 2, 0] if n < 4: assert n >= 0 return results[n] for _ in range(4, n + 1): results.append(results[-1] + results[-2] + results[-3] + results[-4]) results.pop(0) return results[-1]
def check(candidate): assert candidate(0) == 0 assert candidate(5) == 4 assert candidate(8) == 28 assert candidate(10) == 104 assert candidate(12) == 386 assert candidate(49) == 13546793363542 def test_check(): check(fib4)
HumanEval/47
from typing import List def median(l: List[float]) -> float: """ Return median of elements in the list l. Examples: >>> median([3, 1, 2, 4, 5]) 3 >>> median([-10, 4, 6, 1000, 10, 20]) 8.0 """
median
assert len(l) > 0 l = sorted(l) if len(l) % 2 == 1: return l[len(l) // 2] else: return (l[len(l) // 2 - 1] + l[len(l) // 2]) / 2.0
def check(candidate): assert candidate([3, 1, 2, 4, 5]) == 3 assert candidate([-10, 4, 6, 1000, 10, 20]) == 8.0 assert candidate([5]) == 5 assert candidate([6, 5]) == 5.5 assert candidate([8, 1, 3, 9, 9, 2, 7]) == 7 def test_check(): check(median)
HumanEval/48
def is_palindrome(text: str) -> bool: """ Checks if given string is a palindrome. Examples: >>> is_palindrome('racecar') True >>> is_palindrome('car') False """
is_palindrome
for i in range(len(text)): if text[i] != text[len(text) - 1 - i]: return False return True
def check(candidate): assert candidate('') == True assert candidate('aba') == True assert candidate('aaaaa') == True assert candidate('zbcd') == False assert candidate('xywyx') == True assert candidate('xywyz') == False assert candidate('xywzx') == False assert candidate('palindrome') ==...
HumanEval/49
def modp(n: int, p: int) -> int: """ Solve 2^n modulo p (be aware of numerics). Return the least non-negative residue. Examples: >>> modp(3, 5) 3 >>> modp(100, 101) 1 """
modp
assert n >= 0 ret = 1 for _ in range(n): ret = (2 * ret) % p while ret < 0: ret += abs(p) return ret
def check(candidate): assert candidate(3, 5) == 3 assert candidate(1101, 101) == 2 assert candidate(0, 101) == 1 assert candidate(3, 11) == 8 assert candidate(3, -11) == 8 assert candidate(100, 101) == 1 assert candidate(30, 5) == 4 assert candidate(31, 5) == 3 assert candidate(31, -...
HumanEval/50
def decode_shift(s: str) -> str: """ Takes as input a lowercase string encoded as follows: - Shift every letter by 5 in the alphabet. This function should return the decoded string. Examples: >>> decode_shift("cryim") "xmtdh" >>> decode_shift("zlsebmzxbqrvxkrwlqvrvg...
decode_shift
return "".join([chr(((ord(ch) - 5 - ord("a")) % 26) + ord("a")) for ch in s])
def check(candidate): assert candidate("cryim") == "xmtdh" assert candidate("zlsebmzxbqrvxkrwlqvrvgpmbrgerh") == "ugnzwhuswlmqsfmrglqmqbkhwmbzmc" assert candidate("cfxchfesawcaybmbq") == "xasxcaznvrxvtwhwl" assert candidate("qenusc") == "lzipnx" assert candidate("awxsembugkohodgmfgukoe") == "vrsnzhw...
HumanEval/51
def remove_vowels(text: str) -> str: """ Return the text without vowels (a, e, i, o, u). Examples: >>> remove_vowels('abcdef') 'bcdf' >>> remove_vowels('aaaaa') '' >>> remove_vowels('aaBAA') 'B' >>> remove_vowels('zbcd') 'zbcd' """
remove_vowels
return "".join([s for s in text if s.lower() not in ["a", "e", "i", "o", "u"]])
def check(candidate): assert candidate('') == '' assert candidate("abcdef\nghijklm") == 'bcdf\nghjklm' assert candidate('abc\tdef') == 'bc\tdf' assert candidate('fedcba') == 'fdcb' assert candidate('eeeee') == '' assert candidate('acBAA') == 'cB' assert candidate('EcBOO') == 'cB' assert ...
HumanEval/52
from typing import List def below_threshold(l: List[int], t: int) -> bool: """ Check if all numbers in the list l are below threshold t. Examples: >>> below_threshold([1, 2, 4, 10], 100) True >>> below_threshold([1, 20, 4, 10], 5) False """
below_threshold
for e in l: if e >= t: return False return True
def check(candidate): assert candidate([1, 2, 4, 10], 100) == True assert candidate([1, 20, 4, 10], 5) == False assert candidate([1, 20, 4, 10], 21) == True assert candidate([1, 20, 4, 10], 22) == True assert candidate([1, 8, 4, 10], 11) == True assert candidate([1, 8, 4, 10], 10) == False a...
HumanEval/53
def add(x: int, y: int) -> int: """ Add the two numbers x and y. Examples: >>> add(2, 3) 5 >>> add(5, 7) 12 """
add
return x + y
def check(candidate): assert candidate(0, 1) == 1 assert candidate(1, 0) == 1 assert candidate(2, 3) == 5 assert candidate(5, 7) == 12 assert candidate(7, 5) == 12 assert candidate(-1, 1) == 0 assert candidate(1, -1) == 0 assert candidate(-1, -1) == -2 assert candidate(0, 0) == 0 ...
HumanEval/54
def same_chars(s0: str, s1: str) -> bool: """ Check if two words have the same characters. Examples: >>> same_chars('abcd', 'dddddddabc') True >>> same_chars('dddddddabc', 'abcd') True >>> same_chars('eabcd', 'dddddddabc') False >>> same_chars('abcd',...
same_chars
return set(s0) == set(s1)
def check(candidate): assert candidate('eabcdzzzz', 'dddzzzzzzzddeddabc') == True assert candidate('abcd', 'dddddddabc') == True assert candidate('dddddddabc', 'abcd') == True assert candidate('eabcd', 'dddddddabc') == False assert candidate('abcd', 'dddddddabcf') == False assert candidate('eabc...
HumanEval/55
def fib(n: int) -> int: """ Return the n-th Fibonacci number. Examples: >>> fib(10) 55 >>> fib(1) 1 >>> fib(8) 21 """
fib
assert n >= 0 if n == 0: return 0 if n == 1: return 1 return fib(n - 1) + fib(n - 2)
def check(candidate): assert candidate(0) == 0 assert candidate(10) == 55 assert candidate(1) == 1 assert candidate(2) == 1 assert candidate(3) == 2 assert candidate(8) == 21 assert candidate(11) == 89 assert candidate(12) == 144 assert candidate(39) == 63245986 def test_check(): ...
HumanEval/56
def correct_bracketing(brackets: str) -> bool: """ brackets is a string of "<" and ">". Check if every opening bracket has a corresponding closing bracket. Examples: >>> correct_bracketing("<") False >>> correct_bracketing("<>") True >>> correct_bracketing("<<><>...
correct_bracketing
depth = 0 for b in brackets: if b == "<": depth += 1 else: depth -= 1 if depth < 0: return False return depth == 0
def check(candidate): assert candidate("") == True assert candidate("<>") == True assert candidate("<<><>>") == True assert candidate("<><><<><>><>") == True assert candidate("<><><<<><><>><>><<><><<>>>") == True assert candidate("<<<><>>>>") == False assert candidate("><<>") == False as...
HumanEval/57
from typing import List def monotonic(l: List[int]) -> bool: """ Check if list elements are monotonically increasing or decreasing. Examples: >>> monotonic([1, 2, 4, 20]) True >>> monotonic([1, 20, 4, 10]) False >>> monotonic([4, 1, 0, -10]) True """
monotonic
if l == sorted(l) or l == sorted(l, reverse=True): return len(set(l)) == len(l) return False
def check(candidate): assert candidate([]) == True assert candidate([1, 2, 4, 10]) == True assert candidate([1, 2, 4, 20]) == True assert candidate([1, 20, 4, 10]) == False assert candidate([4, 1, 0, -10]) == True assert candidate([4, 1, 1, 0]) == False assert candidate([1, 2, 3, 2, 5, 60]) ...
HumanEval/58
from typing import List def common(l1: List[int], l2: List[int]) -> List[int]: """ Return sorted unique common elements for two integer lists. Examples: >>> common([1, 4, 3, 34, 653, 2, 5], [5, 7, 1, 5, 9, 653, 121]) [1, 5, 653] >>> common([5, 3, 2, 8], [3, 2]) [2, 3] ...
common
ret = set() for e1 in l1: for e2 in l2: if e1 == e2: ret.add(e1) return sorted(list(ret))
def check(candidate): assert candidate([1, 4, 3, 34, 653, 2, 5], [5, 7, 1, 5, 9, 653, 121]) == [1, 5, 653] assert candidate([5, 3, 2, 8], [3, 2]) == [2, 3] assert candidate([4, 3, 2, 8], [3, 2, 4]) == [2, 3, 4] assert candidate([4, 3, 2, 8], []) == [] assert candidate([], [3, 2, 4]) == [] assert...
HumanEval/59
def largest_prime_factor(n: int) -> int: """ Return the largest prime factor of n. Assume n > 1 and is not a prime. Examples: >>> largest_prime_factor(13195) 29 >>> largest_prime_factor(2048) 2 """
largest_prime_factor
assert n > 1 def is_prime(k: int) -> bool: if k < 2: return False for i in range(2, k - 1): if k % i == 0: return False return True largest = 1 for j in range(2, n + 1): if n % j == 0 and is_prime(j): largest = max(larg...
def check(candidate): assert candidate(4) == 2 assert candidate(15) == 5 assert candidate(27) == 3 assert candidate(63) == 7 assert candidate(330) == 11 assert candidate(13195) == 29 def test_check(): check(largest_prime_factor)
HumanEval/60
def sum_to_n(n: int) -> int: """ sum_to_n is a function that sums numbers from 1 to n. Examples: >>> sum_to_n(5) 15 >>> sum_to_n(10) 55 >>> sum_to_n(1) 1 >>> sum_to_n(0) 1 """
sum_to_n
sign = 1 if n > 0 else -1 return sum(range(1, n + sign, sign))
def check(candidate): assert candidate(1) == 1 assert candidate(6) == 21 assert candidate(11) == 66 assert candidate(30) == 465 assert candidate(100) == 5050 assert candidate(0) == 1 assert candidate(-5) == -14 assert candidate(-44) == -989 def test_check(): check(sum_to_n)
HumanEval/61
def correct_bracketing(brackets: str) -> bool: """ brackets is a string of "(" and ")". Check if every opening bracket has a corresponding closing bracket. Examples: >>> correct_bracketing("(") False >>> correct_bracketing("()") True >>> correct_bracketing("(()()...
correct_bracketing
depth = 0 for b in brackets: if b == "(": depth += 1 elif b == ")": depth -= 1 if depth < 0: return False return depth == 0
def check(candidate): assert candidate("") == True assert candidate("()") == True assert candidate("(()())") == True assert candidate("()()(()())()") == True assert candidate("()()((()()())())(()()(()))") == True assert candidate("((()())))") == False assert candidate(")(()") == False as...
HumanEval/62
from typing import List def derivative(xs: List[int]) -> List[int]: """ xs represents the coefficients of a polynomial: xs[0] + xs[1] * x + xs[2] * x^2 + .... Return the derivative of this polynomial in the same form. Examples: >>> derivative([3, 1, 2, 4, 5]) [1, 4, 12, 20] ...
derivative
return [(i * x) for i, x in enumerate(xs)][1:]
def check(candidate): assert candidate([]) == [] assert candidate([3, 1, 2, 4, 5]) == [1, 4, 12, 20] assert candidate([1, 2, 3]) == [2, 6] assert candidate([3, 2, 1]) == [2, 2] assert candidate([3, 2, 1, 0, 4]) == [2, 2, 0, 16] assert candidate([1]) == [] assert candidate([4, -2, -1, -3, -5]...
HumanEval/63
def fibfib(n: int) -> int: """ The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fibfib(0) == 0 fibfib(1) == 0 fibfib(2) == 1 fibfib(n) == fibfib(n - 1) + fibfib(n - 2) + fibfib(n - 3) Please write a function to efficiently...
fibfib
assert n >= 0 if n == 0: return 0 if n == 1: return 0 if n == 2: return 1 return fibfib(n - 1) + fibfib(n - 2) + fibfib(n - 3)
def check(candidate): assert candidate(0) == 0 assert candidate(2) == 1 assert candidate(1) == 0 assert candidate(5) == 4 assert candidate(8) == 24 assert candidate(10) == 81 assert candidate(12) == 274 assert candidate(14) == 927 def test_check(): check(fibfib)
HumanEval/64
def vowels_count(s: str) -> int: """ Write a function vowels_count which takes a string representing a single word as input and returns the number of vowels in the string. Vowels in this case are 'a', 'e', 'i', 'o', 'u'. Here, 'y' is also a vowel, but only when it is at the end of the given word. No...
vowels_count
assert " " not in s.strip() # input is a single word import re if len(re.sub(r'[^aeiouyAEIOUY]', '', s)) == 0: return 0 vowels = "aeiouAEIOU" n_vowels = sum(c in vowels for c in s) last_vowel = re.sub(r'[^a-zA-Z0-9]', '', s) if last_vowel[-1] == 'y' or last_vowel[-1] == 'Y': ...
def check(candidate): assert candidate("") == 0 assert candidate("abcde") == 2 assert candidate("AlOne") == 3 assert candidate("key") == 2 assert candidate("bye") == 1 assert candidate("keY") == 2 assert candidate("bYe") == 1 assert candidate("yy") == 1 assert candidate("ACEDY") == 3...
HumanEval/65
def circular_shift(x: int, shift: int) -> str: """ Circular shift the digits of the integer x, shift the digits right by shift and return the result as a string. If shift > number of digits, return digits reversed. Examples: >>> circular_shift(12, 1) "21" >>> circular_shift(...
circular_shift
s = str(x) if shift > len(s): return s[::-1] shift = shift % len(s) return s[len(s) - shift:] + s[:len(s) - shift]
def check(candidate): assert candidate(100, 2) == "001" assert candidate(12, 2) == "12" assert candidate(97, 8) == "79" assert candidate(12, 1) == "21" assert candidate(10, 101) == "01" assert candidate(123, 0) == "123" assert candidate(123, 2) == "231" assert candidate(123, 3) == "123" ...
HumanEval/66
def digitSum(s: str) -> int: """ Take a string as input and return the sum of the the ASCII codes from upper characters only. Examples: digitSum("") => 0 digitSum("abAB") => 131 digitSum("abcCd") => 67 digitSum("helloE") => 69 digitSum("woArBld") => 131 digit...
digitSum
if s == "": return 0 return sum(ord(char) if char.isupper() else 0 for char in s)
def check(candidate): assert candidate("") == 0 assert candidate("abAB\nabAB") == 262 assert candidate("ab_AB") == 131 assert candidate("abcCd") == 67 assert candidate("hel!loE") == 69 assert candidate("woArBld") == 131 assert candidate("aAaaaXa") == 153 assert candidate(" How are yOu?")...
HumanEval/67
def fruit_distribution(s: str, n: int) -> int: """ In this task, you will be given a string that represents a number of apples and oranges that are distributed in a basket of fruit this basket contains apples, oranges, and mango fruits. Given the string, that represents the total number of the or...
fruit_distribution
lis = list() for i in s.split(' '): if i.isdigit(): lis.append(int(i)) return n - sum(lis)
def check(candidate): assert candidate("5 apples and 6 oranges", 19) == 8 assert candidate("5 apples and 6 oranges", 21) == 10 assert candidate("0 apples and 1 orange", 3) == 2 assert candidate("1 apple and 0 oranges", 3) == 2 assert candidate("1 apple and 1 orange", 2) == 0 assert candidate("2 ...
HumanEval/68
from typing import List def pluck(arr: List[int]) -> List[int]: """ Given an array representing a branch of a tree that has integer nodes, your task is to pluck one of the nodes and return it. The plucked node should be the node with the smallest even value. If multiple nodes with the same smalles...
pluck
if (len(arr) == 0): return [] evens = list(filter(lambda x: x % 2 == 0, arr)) if (evens == []): return [] return [min(evens), arr.index(min(evens))]
def check(candidate): assert candidate([4, 2, 3]) == [2, 1] assert candidate([1, 2, 3]) == [2, 1] assert candidate([]) == [] assert candidate([5, 0, 3, 0, 4, 2]) == [0, 1] assert candidate([1, 2, 3, 0, 5, 3]) == [0, 3] assert candidate([5, 4, 8, 4, 8]) == [4, 1] assert candidate([1, 6, 7, 1,...
HumanEval/69
from typing import List def search(lst: List[int]) -> int: """ You are given a non-empty list of positive integers. Return the greatest integer that is greater than zero, and has a frequency greater than or equal to the value of the integer itself. The frequency of an integer is the number of times ...
search
assert len(lst) > 0 frq = [0] * (max(lst) + 1) for i in lst: frq[i] += 1; ans = -1 for i in range(1, len(frq)): if frq[i] >= i: ans = i return ans
def check(candidate): assert candidate([5, 5, 5, 5, 1]) == 1 assert candidate([4, 1, 4, 1, 4, 4]) == 4 assert candidate([3, 3]) == -1 assert candidate([8, 8, 8, 8, 8, 8, 8, 8]) == 8 assert candidate([2, 3, 3, 2, 2]) == 2 assert candidate([1000000000]) == -1 assert candidate([2, 7, 8, 8, 4, 8...
HumanEval/70
from typing import List def strange_sort_list(lst: List[int]) -> List[int]: """ Given a list of integers, return the list in a 'strange' order. Strange sorting, is when you start with the minimum value, then the maximum value from the remaining integers, then the minimum and so on. Examples: ...
strange_sort_list
res, switch = [], True while lst: res.append(min(lst) if switch else max(lst)) lst.remove(res[-1]) switch = not switch return res
def check(candidate): assert candidate([1, 2, 3, 4]) == [1, 4, 2, 3] assert candidate([5, 6, 7, 8, 9]) == [5, 9, 6, 8, 7] assert candidate([1, 2, 3, 4, 5]) == [1, 5, 2, 4, 3] assert candidate([5, 6, 7, 8, 9, 1]) == [1, 9, 5, 8, 6, 7] assert candidate([5, 5, 5, 5]) == [5, 5, 5, 5] assert candidat...
HumanEval/71
def triangle_area(a: int, b: int, c: int) -> float: """ Given the lengths of the three sides of a triangle. Return the area of the triangle rounded to 2 decimal points if the three sides form a valid triangle. Otherwise return -1 Three sides make a valid triangle when the sum of any two sides is gr...
triangle_area
if a < 0 or b < 0 or c < 0: return -1 if a + b <= c or a + c <= b or b + c <= a: return -1 s = (a + b + c)/2 area = (s * (s - a) * (s - b) * (s - c)) ** 0.5 area = round(area, 2) return area
def check(candidate): assert candidate(3, 4, 5) == 6.00 assert candidate(1, 2, 10) == -1 assert candidate(4, 8, 5) == 8.18 assert candidate(2, 2, 2) == 1.73 assert candidate(1, 2, 3) == -1 assert candidate(10, 5, 7) == 16.25 assert candidate(2, 6, 3) == -1 assert candidate(1, 1, 1) == 0....
HumanEval/72
from typing import List def will_it_fly(q: List[int], w: int) -> bool: """ Write a function that returns True if the object q will fly, and False otherwise. The object q will fly if it's balanced (it is a palindromic list) and the sum of its elements is less than or equal the maximum possible weight w. ...
will_it_fly
if w < 0: # Invalid weight return False if sum(q) > w: # Too heavy return False if len(q) == 0 or sum(q) <= 0: # Nothing to fly return False if any([x < 0 for x in q]): # Negative elements return False # Remove leading a...
def check(candidate): assert candidate([1, 1, 1], 3) == True assert candidate([1], 0) == False assert candidate([3, 2, 3], 9) == True assert candidate([1, 2], 5) == False assert candidate([3], 5) == True assert candidate([3, 2, 3], 1) == False assert candidate([1, 2, 3], 6) == False asse...
HumanEval/73
from typing import List def smallest_change(arr: List[int]) -> int: """ Given an array arr of integers, find the minimum number of elements that need to be changed to make the array palindromic. A palindromic array is an array that is read the same backwards and forwards. In one change, you can change...
smallest_change
ans = 0 for i in range(len(arr) // 2): if arr[i] != arr[len(arr) - i - 1]: ans += 1 return ans
def check(candidate): assert candidate([1, 2, 3, 5, 4, 7, 9, 6]) == 4 assert candidate([1, 11, 111, 1111, 1, 11]) == 3 assert candidate([1, 2, 3, 4, 3, 2, 2]) == 1 assert candidate([1, 4, 2]) == 1 assert candidate([1, 4, 4, 2]) == 1 assert candidate([1, 2, 3, 2, 1]) == 0 assert candidate([3,...
HumanEval/74
from typing import List def total_match(lst1: List[str], lst2: List[str]) -> List[str]: """ Write a function that accepts two lists of strings and returns the list that has less total number of chars in the all strings of the list compared to the other list. If the two lists have the same number of ...
total_match
l1 = 0 for st in lst1: l1 += len(st) l2 = 0 for st in lst2: l2 += len(st) if l1 <= l2: return lst1 return lst2
def check(candidate): assert candidate([], []) == [] assert candidate(['hi', 'admin'], ['hi', 'hi']) == ['hi', 'hi'] assert candidate(['hi', 'admin'], ['hi', 'hi', 'admin', 'project']) == ['hi', 'admin'] assert candidate(['4'], ['1', '2', '3', '4', '5']) == ['4'] assert candidate(['hi', 'admin'], ['...
HumanEval/75
def is_multiply_prime(a: int) -> bool: """ Check if the given number is the multiplication of 3 prime numbers. Constraints: The three prime numbers must be <= 100. Example: is_multiply_prime(30) == True 30 = 2 * 3 * 5 """
is_multiply_prime
if a < 2: return False def is_prime(n): for j in range(2, n): if n % j == 0: return False return True for i in range(2, 101): if not is_prime(i): continue for j in range(2, 101): if not is_prime(j): ...
def check(candidate): assert candidate(-30) == False assert candidate(-5) == False assert candidate(0) == False assert candidate(1) == False assert candidate(5) == False assert candidate(30) == True assert candidate(8) == True assert candidate(10) == False assert candidate(125) == Tr...
HumanEval/76
def is_simple_power(x: int, n: int) -> bool: """ Check if number x is a simple power of n. x is a simple power of n if n to the power of some integer equals x. For example: is_simple_power(1, 4) => true (4^0 = 1) is_simple_power(2, 2) => true (2^1 = 2) is_simple_power(8, 2) => tr...
is_simple_power
if (n == 1): return (x == 1) power = 1 while (abs(power) < abs(x)): power = power * n return (power == x)
def check(candidate): assert candidate(16, 2) == True assert candidate(143214, 16) == False assert candidate(4, 2) == True assert candidate(9, 3) == True assert candidate(16, 4) == True assert candidate(24, 2) == False assert candidate(128, 4) == False assert candidate(12, 6) == False ...
HumanEval/77
def iscube(a: int) -> bool: """ Check if number a is a cube of some integer number. Examples: iscube(2) == False iscube(64) == True iscube(-180) == False """
iscube
a = abs(a) return int(round(a ** (1. / 3))) ** 3 == a
def check(candidate): # Check some simple cases assert candidate(0) == True assert candidate(1) == True assert candidate(2) == False assert candidate(-1) == True assert candidate(64) == True assert candidate(180) == False assert candidate(1000) == True assert candidate(29791) == Tru...
HumanEval/78
def hex_key(num: str) -> int: """ Based on a hexadecimal number, received as a string, counts the number of hexadecimal digits that are primes. You may assume the input is always correct or empty string, and symbols A,B,C,D,E,F are always uppercase. Examples: For num = "AB" the output...
hex_key
primes = ('2', '3', '5', '7', 'B', 'D') total = 0 for i in range(0, len(num)): if num[i] in primes: total += 1 return total
def check(candidate): assert candidate("0") == 0 assert candidate("AB") == 1 assert candidate("1077E") == 2 assert candidate("ABED1A33") == 4 assert candidate("2020") == 2 assert candidate("123456789ABCDEF0") == 6 assert candidate("112233445566778899AABBCCDDEEFF00") == 12 asser...
HumanEval/79
def decimal_to_binary(decimal: int) -> str: """ You will be given a number in decimal form (decimal system). Convert it to binary format and return it as a string. You may assume the input is positive. There will be an extra couple of characters 'db' at the beginning and at the end of the string. ...
decimal_to_binary
assert decimal >= 0 return "db" + bin(decimal)[2:] + "db"
def check(candidate): assert candidate(0) == "db0db" assert candidate(32) == "db100000db" assert candidate(103) == "db1100111db" assert candidate(15) == "db1111db" assert candidate(61239) == "db1110111100110111db" def test_check(): check(decimal_to_binary)
HumanEval/80
def is_happy(s: str) -> bool: """ Check if the string is happy or not: A string is happy if its length is at least 3 and every 3 consecutive letters are distinct. Examples: is_happy(a) => False is_happy(aa) => False is_happy(abcd) => True is_happy(aabb) => False """
is_happy
if len(s) < 3: return False for i in range(len(s) - 2): if s[i] == s[i+1] or s[i+1] == s[i+2] or s[i] == s[i+2]: return False return True
def check(candidate): assert candidate("g") == False assert candidate("qq") == False assert candidate("pqrst") == True assert candidate("hello") == False assert candidate("adb") == True assert candidate("xyy") == False assert candidate("iopaxpoi") == True assert candidate("iopaxioi") == ...
HumanEval/81
from typing import List def numerical_letter_grade(grades: List[float]) -> List[str]: """ It is the last week of the semester and the teacher has to give the grades to students. The teacher has been making her own algorithm for grading. The only problem is, she has lost the code she used for grading. ...
numerical_letter_grade
tol = 1e-6 import decimal def round_grade_with_tolerance(grade: decimal.Decimal, tol: float = tol) -> decimal.Decimal: rounded = grade.quantize(decimal.Decimal('1.0'), rounding=decimal.ROUND_HALF_UP) return rounded - decimal.Decimal(tol) letter_grade = [] for gpa in [decimal.De...
def check(candidate): assert candidate([]) == [] assert candidate([-1.5, 8.0, 10.0, 5.5]) == ['', '', '', ''] assert candidate([4.0, 3, 1.72, 2, 3.5, 4.5]) == ['A+', 'B', 'C-', 'C', 'A-', ''] assert candidate([1, 0.3, 1.5, 2.8, 3.3]) == ['D', 'D-', 'C-', 'B', 'B+'] assert candidate([4.01, 4.001]) ==...