name
stringlengths
34
63
language
stringclasses
3 values
prompt
stringlengths
91
2.81k
suffix
stringlengths
0
1.57k
canonical_solution
stringlengths
1
219
tests
stringlengths
149
4.98k
humaneval-HumanEval_137_compare_one.json-L54
py
from typing import Union def compare_one(a: Union[int, float, str], b: Union[int, float, str]) -> Union[int, float, str, None]: """ Create a function that takes integers, floats, or strings representing real numbers, and returns the larger variable in its given variable type. Return None if the values are equal. Note: If a real number is represented as a string, the floating point might be . or , >>> compare_one(1, 2.5) 2.5 >>> compare_one(1, '2,3') '2,3' >>> compare_one('5,1', '6') '6' >>> compare_one('1', 1) None """ if isinstance(a, int) and isinstance(b, int): if a > b: return a elif a < b: return b else: return None elif isinstance(a, float) and isinstance(b, float): if a > b: return a elif a < b: return b else: return None elif isinstance(a, str) and isinstance(b, str): a_tmp = a.replace(',', '.') b_tmp = b.replace(',', '.') if float(a_tmp) > float(b_tmp): return a elif float(a_tmp) < float(b_tmp): return b else: return None elif isinstance(a, int) and isinstance(b, float): if a > b: return a elif a < b: return b else: return None elif isinstance(a, float) and isinstance(b, int): if a > b: return a elif a < b: return b
return None elif isinstance(a, str) and isinstance(b, int): a_tmp = a.replace(',', '.') if float(a_tmp) > b: return a elif float(a_tmp) < b: return b else: return None elif isinstance(a, int) and isinstance(b, str): b_tmp = b.replace(',', '.') if a > float(b_tmp): return a elif a < float(b_tmp): return b else: return None elif isinstance(a, str) and isinstance(b, float): a_tmp = a.replace(',', '.') if float(a_tmp) > b: return a elif float(a_tmp) < b: return b
else:
def check(candidate): assert candidate(1, 2) == 2 assert candidate(1, 2.5) == 2.5 assert candidate(2, 3) == 3 assert candidate(5, 6) == 6 assert candidate(1, '2,3') == '2,3' assert candidate('5,1', '6') == '6' assert candidate('1', '2') == '2' assert candidate('1', 1) == None def test_check(): check(compare_one) test_check()
humaneval-HumanEval_42_incr_list.json-L12
js
//Return array with elements incremented by 1. // >>> incr_list([1, 2, 3]) // [2, 3, 4] // >>> incr_list([5, 3, 5, 2, 3, 3, 9, 0, 123]) // [6, 4, 6, 3, 4, 4, 10, 1, 124] function incr_list(l){ var new_list = []; for(var i = 0; i < l.length; i++){ new_list.push(l[i] + 1); } return new_list;
}
const assert = require('node:assert'); function test() { let candidate = incr_list; assert.deepEqual(candidate([]),[]); assert.deepEqual(candidate([3, 2, 1]),[4, 3, 2]); assert.deepEqual(candidate([5, 2, 5, 2, 3, 3, 9, 0, 123]),[6, 3, 6, 3, 4, 4, 10, 1, 124]); } test();
humaneval-HumanEval_95_check_dict_case.json-L41
js
//Given an object, return true if all keys are strings in lower // case or all keys are strings in upper case, else return false. // The function should return false is the given object is empty. // Examples: // >>> check_dict_case({"a": "apple", "b": "banana"}) // true // >>> check_dict_case({"a": "apple", "A": "banana", "B": "banana"}) // false // >>> check_dict_case({"a": "apple", 8: "banana", "a": "apple"}) // false // >>> check_dict_case({"Name": "John", "Age": "36", "City": "Houston"}) // false // >>> check_dict_case({"STATE": "NC", "ZIP": "12345"}) // true function check_dict_case(dict){ if (Object.keys(dict).length == 0){ return false; } var case_type = -1; for (let key in dict){ if (typeof key == "string"){ if (case_type == -1){ if (key == key.toUpperCase()){ case_type = 1; } else if (key == key.toLowerCase()){ case_type = 0; } else{ return false; } } else if (case_type == 1){ if (key != key.toUpperCase()){ return false; } } else if (case_type == 0){ if (key != key.toLowerCase()){ return false;
} } else{ return false; } } return true; }
}
const assert = require('node:assert'); function test() { let candidate = check_dict_case; assert.deepEqual(candidate({"p": "pineapple", "b": "banana"}),true); assert.deepEqual(candidate({"p": "pineapple", "A": "banana", "B": "banana"}),false); assert.deepEqual(candidate({"p": "pineapple", "5": "banana", "a": "apple"}),false); assert.deepEqual(candidate({"Name": "John", "Age": "36", "City": "Houston"}),false); assert.deepEqual(candidate({"STATE": "NC", "ZIP": "12345"}),true); assert.deepEqual(candidate({"fruit": "Orange", "taste": "Sweet"}),true); assert.deepEqual(candidate({}),false); } test();
humaneval-HumanEval_64_vowels_count.json-L19
js
//Write a function vowels_count which takes a string representing // a 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. // Example: // >>> vowels_count("abcde") // 2 // >>> vowels_count("ACEDY") // 3 function vowels_count(s){ s=s.toLowerCase(); var vowels = ['a','e','i','o','u']; var count = 0; for(var i=0; i<s.length; i++){ if(vowels.indexOf(s[i])!=-1){ count++; } }
count++; } return count; }
if(s[s.length-1]=='y'){
const assert = require('node:assert'); function test() { let candidate = vowels_count; assert.deepEqual(candidate("abcde"),2); assert.deepEqual(candidate("Alone"),3); assert.deepEqual(candidate("key"),2); assert.deepEqual(candidate("bye"),1); assert.deepEqual(candidate("keY"),2); assert.deepEqual(candidate("bYe"),1); assert.deepEqual(candidate("ACEDY"),3); } test();
humaneval-HumanEval_142_sum_squares.json-L24
js
//" // This function will take an array of integers. For all entries in the array, the function shall square the integer entry if its index is a // multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not // change the entries in the array whose indexes are not a multiple of 3 or 4. The function shall then return the sum of all entries. // Examples: // >>> lst // [1, 2, 3] // >>> lst // [] // >>> lst // [-1, -5, 2, -1, -5] function sum_squares(lst){ var sum = 0; var len = lst.length; for (var i = 0; i < len; i++){ if (i % 3 == 0){ sum += lst[i]*lst[i]; } else if (i % 4 == 0){ sum += lst[i]*lst[i]*lst[i]; } else { sum += lst[i]; } }
}
return sum;
const assert = require('node:assert'); function test() { let candidate = sum_squares; assert.deepEqual(candidate([1, 2, 3]),6); assert.deepEqual(candidate([1, 4, 9]),14); assert.deepEqual(candidate([]),0); assert.deepEqual(candidate([1, 1, 1, 1, 1, 1, 1, 1, 1]),9); assert.deepEqual(candidate([-1, -1, -1, -1, -1, -1, -1, -1, -1]),-3); assert.deepEqual(candidate([0]),0); assert.deepEqual(candidate([-1, -5, 2, -1, -5]),-126); assert.deepEqual(candidate([-56, -99, 1, 0, -2]),3030); assert.deepEqual(candidate([-1, 0, 0, 0, 0, 0, 0, 0, -1]),0); assert.deepEqual(candidate([-16, -9, -2, 36, 36, 26, -20, 25, -40, 20, -4, 12, -26, 35, 37]),-14196); assert.deepEqual(candidate([-1, -3, 17, -1, -15, 13, -1, 14, -14, -12, -5, 14, -14, 6, 13, 11, 16, 16, 4, 10]),-1448); } test();
humaneval-HumanEval_68_pluck.json-L42
py
from typing import List def pluck(arr: List[int]) -> List[int]: """ "Given an array representing a branch of a tree that has non-negative 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 smallest even value are found return the node that has smallest index. The plucked node should be returned in a list, [ smalest_value, its index ], If there are no even values or the given array is empty, return []. Example 1: >>> pluck([4, 2, 3]) [2, 1] Explanation: 2 has the smallest even value, and 2 has the smallest index. Example 2: >>> pluck([1, 2, 3]) [2, 1] Explanation: 2 has the smallest even value, and 2 has the smallest index. Example 3: >>> pluck([]) [] Example 4: >>> pluck([5, 0, 3, 0, 4, 2]) [0, 1] Explanation: 0 is the smallest value, but there are two zeros, so we will choose the first zero, which has the smallest index. Constraints: * 1 <= nodes.length <= 10000 * 0 <= node.value """ if len(arr) == 0: return [] evens = [e for e in arr if e % 2 == 0] if len(evens) == 0: 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([7, 6, 7, 1]) == [6, 1] assert candidate([7, 9, 7, 1]) == [] def test_check(): check(pluck) test_check()
humaneval-HumanEval_111_histogram.json-L21
js
//Given a string representing a space separated lowercase letters, return an object // of the letter with the most repetition and containing the corresponding count. // If several letters have the same occurrence, return all of them. // Example: // >>> histogram("a b c") // {"a": 1, "b": 1, "c": 1} // >>> histogram("a b b a") // {"a": 2, "b": 2} // >>> histogram("a b c a b") // {"a": 2, "b": 2} // >>> histogram("b b b b a") // {"b": 4} // >>> histogram("") // {} function histogram(test){ if (test.length === 0){ return {}; } var lst = test.split(" "); var count = {};
if (count[lst[i]]){ count[lst[i]] += 1; } else{ count[lst[i]] = 1; } } console.log(count); var max = 0; var result = {}; for (var key in count){ if (count[key] > max){ max = count[key]; result = {}; result[key] = max; } else if (count[key] === max){ result[key] = max; } } return result; }
for (var i = 0; i < lst.length; i++){
const assert = require('node:assert'); function test() { let candidate = histogram; assert.deepEqual(candidate("a b b a"),{"a": 2, "b": 2}); assert.deepEqual(candidate("a b c a b"),{"a": 2, "b": 2}); assert.deepEqual(candidate("a b c d g"),{"a": 1, "b": 1, "c": 1, "d": 1, "g": 1}); assert.deepEqual(candidate("r t g"),{"r": 1, "t": 1, "g": 1}); assert.deepEqual(candidate("b b b b a"),{"b": 4}); assert.deepEqual(candidate("r t g"),{"r": 1, "t": 1, "g": 1}); assert.deepEqual(candidate(""),{}); assert.deepEqual(candidate("a"),{"a": 1}); } test();
humaneval-HumanEval_62_derivative.json-L13
js
//xs represent coefficients of a polynomial. // xs[0] + xs[1] * x + xs[2] * x^2 + .... // Return derivative of this polynomial in the same form. // >>> derivative([3, 1, 2, 4, 5]) // [1, 4, 12, 20] // >>> derivative([1, 2, 3]) // [2, 6] function derivative(xs){ let coeff = []; for(let i = 1; i < xs.length; i++){ coeff.push(xs[i] * i); }
}
return coeff;
const assert = require('node:assert'); function test() { let candidate = derivative; assert.deepEqual(candidate([3, 1, 2, 4, 5]),[1, 4, 12, 20]); assert.deepEqual(candidate([1, 2, 3]),[2, 6]); assert.deepEqual(candidate([3, 2, 1]),[2, 2]); assert.deepEqual(candidate([3, 2, 1, 0, 4]),[2, 2, 0, 16]); assert.deepEqual(candidate([1]),[]); } test();
humaneval-HumanEval_70_strange_sort_list.json-L21
py
from typing import List def strange_sort_list(lst: List[int]) -> List[int]: """ Given list of integers, return list in strange order. Strange sorting, is when you start with the minimum value, then maximum of the remaining integers, then minimum and so on. Examples: >>> strange_sort_list([1, 2, 3, 4]) [1, 4, 2, 3] >>> strange_sort_list([5, 5, 5, 5]) [5, 5, 5, 5] >>> strange_sort_list([]) [] """ lst = sorted(lst) out = [] while lst: out.append(lst.pop(0))
break out.append(lst.pop()) return out
if not lst:
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 candidate([]) == [] assert candidate([1, 2, 3, 4, 5, 6, 7, 8]) == [1, 8, 2, 7, 3, 6, 4, 5] assert candidate([0, 2, 2, 2, 5, 5, -5, -5]) == [-5, 5, -5, 5, 0, 2, 2, 2] assert candidate([111111]) == [111111] def test_check(): check(strange_sort_list) test_check()
humaneval-HumanEval_134_check_if_last_char_is_a_letter.json-L24
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Create a function that returns true if the last character // of a given string is an alphabetical character and is not // a part of a word, and false otherwise. // Note: "word" is a group of characters separated by space. // Examples: // >>> checkIfLastCharIsALetter(("apple pie")) // (false) // >>> checkIfLastCharIsALetter(("apple pi e")) // (true) // >>> checkIfLastCharIsALetter(("apple pi e ")) // (false) // >>> checkIfLastCharIsALetter(("")) // (false) public static boolean checkIfLastCharIsALetter(String txt) { if (txt.length() == 0) {
} int len = txt.length(); if (txt.substring(len - 1, len).matches("[A-Z]") || txt.substring(len - 1, len).matches("[a-z]")) { if (len == 1) { return true; } if (txt.substring(len - 2, len - 1).matches(" ")) { return true; } } return false; } }
return false;
} public static void main(String[] args) { assert(checkIfLastCharIsALetter(("apple")) == (false)); assert(checkIfLastCharIsALetter(("apple pi e")) == (true)); assert(checkIfLastCharIsALetter(("eeeee")) == (false)); assert(checkIfLastCharIsALetter(("A")) == (true)); assert(checkIfLastCharIsALetter(("Pumpkin pie ")) == (false)); assert(checkIfLastCharIsALetter(("Pumpkin pie 1")) == (false)); assert(checkIfLastCharIsALetter(("")) == (false)); assert(checkIfLastCharIsALetter(("eeeee e ")) == (false)); assert(checkIfLastCharIsALetter(("apple pie")) == (false)); assert(checkIfLastCharIsALetter(("apple pi e ")) == (false)); } }
humaneval-HumanEval_41_car_race_collision.json-L20
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // 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. The two sets of cars start out being very far from // each other. All cars move in the same speed. Two cars are said to collide // when a car that's moving left to right hits a car that's moving right to left. // However, the cars are infinitely sturdy and strong; as a result, they continue moving // in their trajectory as if they did not collide. // This function outputs the number of such collisions. public static long carRaceCollision(long n) { BigInteger nBigInteger = BigInteger.valueOf(n); BigInteger nSquared = nBigInteger.multiply(nBigInteger);
} }
return nSquared.longValue();
} public static void main(String[] args) { assert(carRaceCollision((2l)) == (4l)); assert(carRaceCollision((3l)) == (9l)); assert(carRaceCollision((4l)) == (16l)); assert(carRaceCollision((8l)) == (64l)); assert(carRaceCollision((10l)) == (100l)); } }
humaneval-HumanEval_129_minPath.json-L31
js
//Given a grid with N rows and N columns (N >= 2) and a positive integer k, // each cell of the grid contains a value. Every integer in the range [1, N * N] // inclusive appears exactly once on the cells of the grid. // You have to find the minimum path of length k in the grid. You can start // from any cell, and in each step you can move to any of the neighbor cells, // in other words, you can go to cells which share an edge with you current // cell. // Please note that a path of length k means visiting exactly k cells (not // necessarily distinct). // You CANNOT go off the grid. // A path A (of length k) is considered less than a path B (of length k) if // after making the ordered arrays of the values on the cells that A and B go // through (let's call them lst_A and lst_B), lst_A is lexicographically less // than lst_B, in other words, there exist an integer index i (1 <= i <= k) // such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have // lst_A[j] = lst_B[j]. // It is guaranteed that the answer is unique. // Return an ordered array of the values on the cells that the minimum path go through. // Examples: // >>> minPath([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3) // [1, 2, 1] // >>> minPath([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1) // [1] function minPath(grid, k){ let n = grid.length; let path = []; let startCell = grid[0][0]; for(let i = 0; i < n; i++){ for(let j = 0; j < n; j++){ let curr = grid[i][j];
startCell = curr; } } } let currCell = startCell; for(let l = 0; l < k; l++){ path.push(currCell); let neighbours = []; let x = null; let y = null; for(let i = 0; i < n; i++){ for(let j = 0; j < n; j++){ if(grid[i][j] === currCell){ x = i; y = j; } } } if(x > 0){ neighbours.push(grid[x - 1][y]); } if(x < n - 1){ neighbours.push(grid[x + 1][y]); } if(y > 0){ neighbours.push(grid[x][y - 1]); } if(y < n - 1){ neighbours.push(grid[x][y + 1]); } let nextCell = neighbours[0]; for(let i = 0; i < neighbours.length; i++){ let curr = neighbours[i]; if(curr < nextCell){ nextCell = curr; } } currCell = nextCell; } return path; }
if(curr < startCell){
const assert = require('node:assert'); function test() { let candidate = minPath; assert.deepEqual(candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3),[1, 2, 1]); assert.deepEqual(candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1),[1]); assert.deepEqual(candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4),[1, 2, 1, 2]); assert.deepEqual(candidate([[6, 4, 13, 10], [5, 7, 12, 1], [3, 16, 11, 15], [8, 14, 9, 2]], 7),[1, 10, 1, 10, 1, 10, 1]); assert.deepEqual(candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5),[1, 7, 1, 7, 1]); assert.deepEqual(candidate([[11, 8, 7, 2], [5, 16, 14, 4], [9, 3, 15, 6], [12, 13, 10, 1]], 9),[1, 6, 1, 6, 1, 6, 1, 6, 1]); assert.deepEqual(candidate([[12, 13, 10, 1], [9, 3, 15, 6], [5, 16, 14, 4], [11, 8, 7, 2]], 12),[1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6]); assert.deepEqual(candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8),[1, 3, 1, 3, 1, 3, 1, 3]); assert.deepEqual(candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8),[1, 5, 1, 5, 1, 5, 1, 5]); assert.deepEqual(candidate([[1, 2], [3, 4]], 10),[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]); assert.deepEqual(candidate([[1, 3], [3, 2]], 10),[1, 3, 1, 3, 1, 3, 1, 3, 1, 3]); } test();
humaneval-HumanEval_24_largest_divisor.json-L19
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // For a given number n, find the largest number that divides n evenly, smaller than n // >>> largestDivisor((15l)) // (5l) public static long largestDivisor(long n) { for (long i = 2; i <= Math.floor(Math.sqrt(n)); i++) { if (n % i == 0) { return n / i; } } return 1;
}
}
} public static void main(String[] args) { assert(largestDivisor((3l)) == (1l)); assert(largestDivisor((7l)) == (1l)); assert(largestDivisor((10l)) == (5l)); assert(largestDivisor((100l)) == (50l)); assert(largestDivisor((49l)) == (7l)); } }
humaneval-HumanEval_10_make_palindrome.json-L22
py
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 suffix. >>> make_palindrome('') '' >>> make_palindrome('cat') 'catac' >>> make_palindrome('cata') 'catac' """ if not string: return string prefix = string[::-1] for i in range(len(string)): if string[i:] == prefix[:len(string) - i]: return string + prefix[len(string) - i:]
return string
def check(candidate): assert candidate('') == '' assert candidate('x') == 'x' assert candidate('xyz') == 'xyzyx' assert candidate('xyx') == 'xyx' assert candidate('jerry') == 'jerryrrej' def test_check(): check(make_palindrome) test_check()
humaneval-HumanEval_68_pluck.json-L38
py
from typing import List def pluck(arr: List[int]) -> List[int]: """ "Given an array representing a branch of a tree that has non-negative 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 smallest even value are found return the node that has smallest index. The plucked node should be returned in a list, [ smalest_value, its index ], If there are no even values or the given array is empty, return []. Example 1: >>> pluck([4, 2, 3]) [2, 1] Explanation: 2 has the smallest even value, and 2 has the smallest index. Example 2: >>> pluck([1, 2, 3]) [2, 1] Explanation: 2 has the smallest even value, and 2 has the smallest index. Example 3: >>> pluck([]) [] Example 4: >>> pluck([5, 0, 3, 0, 4, 2]) [0, 1] Explanation: 0 is the smallest value, but there are two zeros, so we will choose the first zero, which has the smallest index. Constraints: * 1 <= nodes.length <= 10000 * 0 <= node.value """ if len(arr) == 0:
evens = [e for e in arr if e % 2 == 0] if len(evens) == 0: return [] return [min(evens), arr.index(min(evens))]
return []
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([7, 6, 7, 1]) == [6, 1] assert candidate([7, 9, 7, 1]) == [] def test_check(): check(pluck) test_check()
humaneval-HumanEval_49_modp.json-L26
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Return 2^n modulo p (be aware of numerics). // >>> modp((3l), (5l)) // (3l) // >>> modp((1101l), (101l)) // (2l) // >>> modp((0l), (101l)) // (1l) // >>> modp((3l), (11l)) // (8l) // >>> modp((100l), (101l)) // (1l) public static long modp(long n, long p) { if (n == 0) return 1; long sqrt = modp(n / 2, p); sqrt = (sqrt * sqrt) % p; if (n % 2 == 0) { return sqrt;
return (sqrt * 2) % p; } }
}
} public static void main(String[] args) { assert(modp((3l), (5l)) == (3l)); assert(modp((1101l), (101l)) == (2l)); assert(modp((0l), (101l)) == (1l)); assert(modp((3l), (11l)) == (8l)); assert(modp((100l), (101l)) == (1l)); assert(modp((30l), (5l)) == (4l)); assert(modp((31l), (5l)) == (3l)); } }
humaneval-HumanEval_144_simplify.json-L24
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Your task is to implement a function that will simplify the expression // x * n. The function returns true if x * n evaluates to a whole number and false // otherwise. Both x and n, are string representation of a fraction, and have the following format, // <numerator>/<denominator> where both numerator and denominator are positive whole numbers. // You can assume that x, and n are valid fractions, and do not have zero as denominator. // >>> simplify(("1/5"), ("5/1")) // (true) // >>> simplify(("1/6"), ("2/1")) // (false) // >>> simplify(("7/10"), ("10/2")) // (false) public static boolean simplify(String x, String n) { String[] nums = x.split("/"); String[] n_nums = n.split("/"); int x_num = Integer.parseInt(nums[0]);
int x_denom = Integer.parseInt(nums[1]); int n_denom = Integer.parseInt(n_nums[1]); int res_num = x_num * n_num; int res_denom = x_denom * n_denom; return (res_num % res_denom == 0); } }
int n_num = Integer.parseInt(n_nums[0]);
} public static void main(String[] args) { assert(simplify(("1/5"), ("5/1")) == (true)); assert(simplify(("1/6"), ("2/1")) == (false)); assert(simplify(("5/1"), ("3/1")) == (true)); assert(simplify(("7/10"), ("10/2")) == (false)); assert(simplify(("2/10"), ("50/10")) == (true)); assert(simplify(("7/2"), ("4/2")) == (true)); assert(simplify(("11/6"), ("6/1")) == (true)); assert(simplify(("2/3"), ("5/2")) == (false)); assert(simplify(("5/2"), ("3/5")) == (false)); assert(simplify(("2/4"), ("8/4")) == (true)); assert(simplify(("2/4"), ("4/2")) == (true)); assert(simplify(("1/5"), ("5/1")) == (true)); assert(simplify(("1/5"), ("1/5")) == (false)); } }
humaneval-HumanEval_142_sum_squares.json-L27
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // " // This function will take an array array list of integers. For all entries in the array list, the function shall square the integer entry if its index is a // multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not // change the entries in the array list whose indexes are not a multiple of 3 or 4. The function shall then return the sum of all entries. // Examples: // >>> lst // (long)new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)) // >>> lst // (long)new ArrayList<Long>(Arrays.asList()) // >>> lst // (long)new ArrayList<Long>(Arrays.asList((long)-1l, (long)-5l, (long)2l, (long)-1l, (long)-5l)) public static long sumSquares(ArrayList<Long> lst) { int sz = lst.size(); for (int i = 0; i < sz; i++) { if (i % 3 == 0) { lst.set(i, lst.get(i) * lst.get(i)); } else if (i % 4 == 0) { lst.set(i, lst.get(i) * lst.get(i) * lst.get(i));
} long ans = 0; for (int i = 0; i < sz; i++) { ans += lst.get(i); } return ans; } }
}
} public static void main(String[] args) { assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))) == (6l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)9l)))) == (14l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList()))) == (0l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l)))) == (9l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l)))) == (-3l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)0l)))) == (0l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-5l, (long)2l, (long)-1l, (long)-5l)))) == (-126l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-56l, (long)-99l, (long)1l, (long)0l, (long)-2l)))) == (3030l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)-1l)))) == (0l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-16l, (long)-9l, (long)-2l, (long)36l, (long)36l, (long)26l, (long)-20l, (long)25l, (long)-40l, (long)20l, (long)-4l, (long)12l, (long)-26l, (long)35l, (long)37l)))) == (-14196l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-3l, (long)17l, (long)-1l, (long)-15l, (long)13l, (long)-1l, (long)14l, (long)-14l, (long)-12l, (long)-5l, (long)14l, (long)-14l, (long)6l, (long)13l, (long)11l, (long)16l, (long)16l, (long)4l, (long)10l)))) == (-1448l)); } }
humaneval-HumanEval_150_x_or_y.json-L12
py
def x_or_y(n: int, x: int, y: int) -> int: """A simple program which should return the value of x if n is a prime number and should return the value of y otherwise. Examples: >>> x_or_y(7, 34, 12) 34 >>> x_or_y(15, 8, 5) 5 """
return y for i in range(2, n): if n % i == 0: return y return x
if n < 2:
def check(candidate): assert candidate(7, 34, 12) == 34 assert candidate(15, 8, 5) == 5 assert candidate(3, 33, 5212) == 33 assert candidate(1259, 3, 52) == 3 assert candidate(7919, -1, 12) == -1 assert candidate(3609, 1245, 583) == 583 assert candidate(91, 56, 129) == 129 assert candidate(6, 34, 1234) == 1234 assert candidate(1, 2, 0) == 0 assert candidate(2, 2, 0) == 2 def test_check(): check(x_or_y) test_check()
humaneval-HumanEval_129_minPath.json-L45
js
//Given a grid with N rows and N columns (N >= 2) and a positive integer k, // each cell of the grid contains a value. Every integer in the range [1, N * N] // inclusive appears exactly once on the cells of the grid. // You have to find the minimum path of length k in the grid. You can start // from any cell, and in each step you can move to any of the neighbor cells, // in other words, you can go to cells which share an edge with you current // cell. // Please note that a path of length k means visiting exactly k cells (not // necessarily distinct). // You CANNOT go off the grid. // A path A (of length k) is considered less than a path B (of length k) if // after making the ordered arrays of the values on the cells that A and B go // through (let's call them lst_A and lst_B), lst_A is lexicographically less // than lst_B, in other words, there exist an integer index i (1 <= i <= k) // such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have // lst_A[j] = lst_B[j]. // It is guaranteed that the answer is unique. // Return an ordered array of the values on the cells that the minimum path go through. // Examples: // >>> minPath([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3) // [1, 2, 1] // >>> minPath([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1) // [1] function minPath(grid, k){ let n = grid.length; let path = []; let startCell = grid[0][0]; for(let i = 0; i < n; i++){ for(let j = 0; j < n; j++){ let curr = grid[i][j]; if(curr < startCell){ startCell = curr; } } } let currCell = startCell; for(let l = 0; l < k; l++){ path.push(currCell); let neighbours = []; let x = null; let y = null; for(let i = 0; i < n; i++){ for(let j = 0; j < n; j++){ if(grid[i][j] === currCell){
y = j; } } } if(x > 0){ neighbours.push(grid[x - 1][y]); } if(x < n - 1){ neighbours.push(grid[x + 1][y]); } if(y > 0){ neighbours.push(grid[x][y - 1]); } if(y < n - 1){ neighbours.push(grid[x][y + 1]); } let nextCell = neighbours[0]; for(let i = 0; i < neighbours.length; i++){ let curr = neighbours[i]; if(curr < nextCell){ nextCell = curr; } } currCell = nextCell; } return path; }
x = i;
const assert = require('node:assert'); function test() { let candidate = minPath; assert.deepEqual(candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3),[1, 2, 1]); assert.deepEqual(candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1),[1]); assert.deepEqual(candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4),[1, 2, 1, 2]); assert.deepEqual(candidate([[6, 4, 13, 10], [5, 7, 12, 1], [3, 16, 11, 15], [8, 14, 9, 2]], 7),[1, 10, 1, 10, 1, 10, 1]); assert.deepEqual(candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5),[1, 7, 1, 7, 1]); assert.deepEqual(candidate([[11, 8, 7, 2], [5, 16, 14, 4], [9, 3, 15, 6], [12, 13, 10, 1]], 9),[1, 6, 1, 6, 1, 6, 1, 6, 1]); assert.deepEqual(candidate([[12, 13, 10, 1], [9, 3, 15, 6], [5, 16, 14, 4], [11, 8, 7, 2]], 12),[1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6]); assert.deepEqual(candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8),[1, 3, 1, 3, 1, 3, 1, 3]); assert.deepEqual(candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8),[1, 5, 1, 5, 1, 5, 1, 5]); assert.deepEqual(candidate([[1, 2], [3, 4]], 10),[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]); assert.deepEqual(candidate([[1, 3], [3, 2]], 10),[1, 3, 1, 3, 1, 3, 1, 3, 1, 3]); } test();
humaneval-HumanEval_13_greatest_common_divisor.json-L19
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Return a greatest common divisor of two integers a and b // >>> greatestCommonDivisor((3l), (5l)) // (1l) // >>> greatestCommonDivisor((25l), (15l)) // (5l) public static long greatestCommonDivisor(long a, long b) { if (a == 0) { return b; } if (b == 0) {
} return greatestCommonDivisor(b, a % b); } }
return a;
} public static void main(String[] args) { assert(greatestCommonDivisor((3l), (7l)) == (1l)); assert(greatestCommonDivisor((10l), (15l)) == (5l)); assert(greatestCommonDivisor((49l), (14l)) == (7l)); assert(greatestCommonDivisor((144l), (60l)) == (12l)); } }
humaneval-HumanEval_82_prime_length.json-L16
py
def prime_length(string: str) -> bool: """Write a function that takes a string and returns True if the string length is a prime number or False otherwise Examples >>> prime_length('Hello') True >>> prime_length('abcdcba') True >>> prime_length('kittens') True >>> prime_length('orange') False """ num = len(string) if num > 1:
if (num % i) == 0: return False else: return True else: return False pass
for i in range(2, num):
def check(candidate): assert candidate('Hello') == True assert candidate('abcdcba') == True assert candidate('kittens') == True assert candidate('orange') == False assert candidate('wow') == True assert candidate('world') == True assert candidate('MadaM') == True assert candidate('Wow') == True assert candidate('') == False assert candidate('HI') == True assert candidate('go') == True assert candidate('gogo') == False assert candidate('aaaaaaaaaaaaaaa') == False assert candidate('Madam') == True assert candidate('M') == False assert candidate('0') == False def test_check(): check(prime_length) test_check()
humaneval-HumanEval_6_parse_nested_parens.json-L21
js
//Input to this function is a string represented multiple groups for nested parentheses separated by spaces. // For each of the group, output the deepest level of nesting of parentheses. // E.g. (()()) has maximum two levels of nesting while ((())) has three. // >>> parse_nested_parens("(()()) ((())) () ((())()())") // [2, 3, 1, 3] function parse_nested_parens(paren_string){ var level = 0; var maxlevel = 0; var result = []; for (var i = 0; i < paren_string.length; i++){ if (paren_string[i] == "("){ level += 1; if (level > maxlevel){ maxlevel = level; } } else if (paren_string[i] == ")"){ level -= 1; } else if (paren_string[i] == " "){
maxlevel = 0; level = 0; } } result.push(maxlevel); return result; }
result.push(maxlevel);
const assert = require('node:assert'); function test() { let candidate = parse_nested_parens; assert.deepEqual(candidate("(()()) ((())) () ((())()())"),[2, 3, 1, 3]); assert.deepEqual(candidate("() (()) ((())) (((())))"),[1, 2, 3, 4]); assert.deepEqual(candidate("(()(())((())))"),[4]); } test();
humaneval-HumanEval_17_parse_music.json-L20
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Input to this function is a string representing musical notes in a special ASCII format. // Your task is to parse this string and return array list of integers corresponding to how many beats does each // not last. // Here is a legend: // 'o' - whole note, lasts four beats // 'o|' - half note, lasts two beats // '.|' - quater note, lasts one beat // >>> parseMusic(("o o| .| o| o| .| .| .| .| o o")) // (new ArrayList<Long>(Arrays.asList((long)4l, (long)2l, (long)1l, (long)2l, (long)2l, (long)1l, (long)1l, (long)1l, (long)1l, (long)4l, (long)4l))) public static ArrayList<Long> parseMusic(String music_string) { ArrayList<Long> res = new ArrayList<Long>();
for (int i = 0; i < chars.length; i++) { if (chars[i] == 'o') { if (i + 1 < chars.length && chars[i + 1] == '|') { res.add((long)2l); i += 1; } else { res.add((long)4l); } } else if (chars[i] == '.') { if (i + 1 < chars.length && chars[i + 1] == '|') { res.add((long)1l); i += 1; } } } return res; } }
char[] chars = music_string.toCharArray();
} public static void main(String[] args) { assert(parseMusic(("")).equals((new ArrayList<Long>(Arrays.asList())))); assert(parseMusic(("o o o o")).equals((new ArrayList<Long>(Arrays.asList((long)4l, (long)4l, (long)4l, (long)4l))))); assert(parseMusic((".| .| .| .|")).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))); assert(parseMusic(("o| o| .| .| o o o o")).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l, (long)1l, (long)1l, (long)4l, (long)4l, (long)4l, (long)4l))))); assert(parseMusic(("o| .| o| .| o o| o o|")).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)1l, (long)2l, (long)1l, (long)4l, (long)2l, (long)4l, (long)2l))))); } }
humaneval-HumanEval_142_sum_squares.json-L26
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // " // This function will take an array array list of integers. For all entries in the array list, the function shall square the integer entry if its index is a // multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not // change the entries in the array list whose indexes are not a multiple of 3 or 4. The function shall then return the sum of all entries. // Examples: // >>> lst // (long)new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)) // >>> lst // (long)new ArrayList<Long>(Arrays.asList()) // >>> lst // (long)new ArrayList<Long>(Arrays.asList((long)-1l, (long)-5l, (long)2l, (long)-1l, (long)-5l)) public static long sumSquares(ArrayList<Long> lst) { int sz = lst.size(); for (int i = 0; i < sz; i++) { if (i % 3 == 0) { lst.set(i, lst.get(i) * lst.get(i)); } else if (i % 4 == 0) {
} } long ans = 0; for (int i = 0; i < sz; i++) { ans += lst.get(i); } return ans; } }
lst.set(i, lst.get(i) * lst.get(i) * lst.get(i));
} public static void main(String[] args) { assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))) == (6l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)9l)))) == (14l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList()))) == (0l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l)))) == (9l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l)))) == (-3l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)0l)))) == (0l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-5l, (long)2l, (long)-1l, (long)-5l)))) == (-126l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-56l, (long)-99l, (long)1l, (long)0l, (long)-2l)))) == (3030l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)-1l)))) == (0l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-16l, (long)-9l, (long)-2l, (long)36l, (long)36l, (long)26l, (long)-20l, (long)25l, (long)-40l, (long)20l, (long)-4l, (long)12l, (long)-26l, (long)35l, (long)37l)))) == (-14196l)); assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-3l, (long)17l, (long)-1l, (long)-15l, (long)13l, (long)-1l, (long)14l, (long)-14l, (long)-12l, (long)-5l, (long)14l, (long)-14l, (long)6l, (long)13l, (long)11l, (long)16l, (long)16l, (long)4l, (long)10l)))) == (-1448l)); } }
humaneval-HumanEval_61_correct_bracketing.json-L16
py
def correct_bracketing(brackets: str) -> bool: """ brackets is a string of "(" and ")". return True if every opening bracket has a corresponding closing bracket. >>> correct_bracketing('(') False >>> correct_bracketing('()') True >>> correct_bracketing('(()())') True >>> correct_bracketing(')(()') False """ count = 0 for char in brackets:
count += 1 elif char == ')': count -= 1 if count < 0: return False return count == 0
if char == '(':
def check(candidate): assert candidate('()') == True assert candidate('(()())') == True assert candidate('()()(()())()') == True assert candidate('()()((()()())())(()()(()))') == True assert candidate('((()())))') == False assert candidate(')(()') == False assert candidate('(') == False assert candidate('((((') == False assert candidate(')') == False assert candidate('(()') == False assert candidate('()()(()())())(()') == False assert candidate('()()(()())()))()') == False def test_check(): check(correct_bracketing) test_check()
humaneval-HumanEval_22_filter_integers.json-L13
js
//Filter given array of any jsthon values only for integers // >>> filter_integers(["a", 3.14, 5]) // [5] // >>> filter_integers([1, 2, 3, "abc", {}, []]) // [1, 2, 3] function filter_integers(values){ var result = []; values.forEach(function(value){ if(typeof value === "number" && value % 1 === 0){ result.push(value); } });
}
return result;
const assert = require('node:assert'); function test() { let candidate = filter_integers; assert.deepEqual(candidate([]),[]); assert.deepEqual(candidate([4, {}, [], 23.2, 9, "adasd"]),[4, 9]); assert.deepEqual(candidate([3, "c", 3, 3, "a", "b"]),[3, 3, 3]); } test();
humaneval-HumanEval_69_search.json-L22
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // You are given a non-empty array 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 it appears in the array list. // If no such a value exist, return -1. // Examples: // >>> search((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)2l, (long)2l, (long)3l, (long)1l)))) // (2l) // >>> search((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)2l, (long)3l, (long)3l, (long)3l, (long)4l, (long)4l, (long)4l)))) // (3l) // >>> search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)4l, (long)4l, (long)4l)))) // (-1l) public static long search(ArrayList<Long> lst) { HashMap<Long, Long> map = new HashMap<Long, Long>();
for (int i = 0; i < lst.size(); i++) { long val = lst.get(i); if (map.containsKey(val)) { map.put(val, map.get(val) + 1); } else { map.put(val, (long)1); } if (map.get(val) >= val && val > max) { max = val; } } return max; } }
Long max = (long)-1;
} public static void main(String[] args) { assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)5l, (long)5l, (long)1l)))) == (1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)4l, (long)1l, (long)4l, (long)4l)))) == (4l)); assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l)))) == (-1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l)))) == (8l)); assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)3l, (long)2l, (long)2l)))) == (2l)); assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)7l, (long)8l, (long)8l, (long)4l, (long)8l, (long)7l, (long)3l, (long)9l, (long)6l, (long)5l, (long)10l, (long)4l, (long)3l, (long)6l, (long)7l, (long)1l, (long)7l, (long)4l, (long)10l, (long)8l, (long)1l)))) == (1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)8l, (long)2l)))) == (2l)); assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)7l, (long)1l, (long)8l, (long)8l, (long)10l, (long)5l, (long)8l, (long)5l, (long)3l, (long)10l)))) == (1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)3l, (long)6l, (long)5l, (long)6l, (long)4l)))) == (-1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)9l, (long)6l, (long)7l, (long)1l, (long)4l, (long)7l, (long)1l, (long)8l, (long)8l, (long)9l, (long)8l, (long)10l, (long)10l, (long)8l, (long)4l, (long)10l, (long)4l, (long)10l, (long)1l, (long)2l, (long)9l, (long)5l, (long)7l, (long)9l)))) == (1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)1l, (long)9l, (long)10l, (long)1l, (long)3l)))) == (1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)9l, (long)7l, (long)5l, (long)8l, (long)7l, (long)5l, (long)3l, (long)7l, (long)5l, (long)10l, (long)10l, (long)3l, (long)6l, (long)10l, (long)2l, (long)8l, (long)6l, (long)5l, (long)4l, (long)9l, (long)5l, (long)3l, (long)10l)))) == (5l)); assert(search((new ArrayList<Long>(Arrays.asList((long)1l)))) == (1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)10l, (long)6l, (long)4l, (long)3l, (long)5l, (long)8l, (long)2l, (long)4l, (long)2l, (long)8l, (long)4l, (long)6l, (long)10l, (long)4l, (long)2l, (long)1l, (long)10l, (long)2l, (long)1l, (long)1l, (long)5l)))) == (4l)); assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)10l, (long)4l, (long)8l, (long)2l, (long)10l, (long)5l, (long)1l, (long)2l, (long)9l, (long)5l, (long)5l, (long)6l, (long)3l, (long)8l, (long)6l, (long)4l, (long)10l)))) == (2l)); assert(search((new ArrayList<Long>(Arrays.asList((long)1l, (long)6l, (long)10l, (long)1l, (long)6l, (long)9l, (long)10l, (long)8l, (long)6l, (long)8l, (long)7l, (long)3l)))) == (1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)2l, (long)4l, (long)1l, (long)5l, (long)1l, (long)5l, (long)2l, (long)5l, (long)7l, (long)7l, (long)7l, (long)3l, (long)10l, (long)1l, (long)5l, (long)4l, (long)2l, (long)8l, (long)4l, (long)1l, (long)9l, (long)10l, (long)7l, (long)10l, (long)2l, (long)8l, (long)10l, (long)9l, (long)4l)))) == (4l)); assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)4l, (long)2l, (long)8l, (long)7l, (long)5l, (long)6l, (long)4l, (long)10l, (long)4l, (long)6l, (long)3l, (long)7l, (long)8l, (long)8l, (long)3l, (long)1l, (long)4l, (long)2l, (long)2l, (long)10l, (long)7l)))) == (4l)); assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)8l, (long)6l, (long)10l, (long)2l, (long)6l, (long)10l, (long)2l, (long)7l, (long)8l, (long)10l, (long)3l, (long)8l, (long)2l, (long)6l, (long)2l, (long)3l, (long)1l)))) == (2l)); assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)3l, (long)9l, (long)5l, (long)6l, (long)3l, (long)2l, (long)8l, (long)5l, (long)6l, (long)10l, (long)10l, (long)6l, (long)8l, (long)4l, (long)10l, (long)7l, (long)7l, (long)10l, (long)8l)))) == (-1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)10l)))) == (-1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)7l, (long)7l, (long)2l, (long)4l, (long)7l, (long)2l, (long)10l, (long)9l, (long)7l, (long)5l, (long)7l, (long)2l)))) == (2l)); assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)4l, (long)10l, (long)2l, (long)1l, (long)1l, (long)10l, (long)3l, (long)6l, (long)1l, (long)8l)))) == (1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)7l, (long)9l, (long)9l, (long)9l, (long)3l, (long)4l, (long)1l, (long)5l, (long)9l, (long)1l, (long)2l, (long)1l, (long)1l, (long)10l, (long)7l, (long)5l, (long)6l, (long)7l, (long)6l, (long)7l, (long)7l, (long)6l)))) == (1l)); assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)10l, (long)10l, (long)9l, (long)2l)))) == (-1l)); } }
humaneval-HumanEval_151_double_the_difference.json-L17
js
//Given an array of numbers, return the sum of squares of the numbers // in the array that are odd. Ignore numbers that are negative or not integers. // >>> double_the_difference([1, 3, 2, 0]) // 10 // >>> double_the_difference([-1, -2, 0]) // 0 // >>> double_the_difference([9, -2]) // 81 // >>> double_the_difference([0]) // 0 // If the input array is empty, return 0. function double_the_difference(lst){ sum = 0; for (var i = 0; i < lst.length; i++){ if(lst[i] % 2 == 1 && lst[i] > 0){ sum += lst[i] ** 2;
} return sum; }
}
const assert = require('node:assert'); function test() { let candidate = double_the_difference; assert.deepEqual(candidate([]),0); assert.deepEqual(candidate([5.0, 4.0]),25); assert.deepEqual(candidate([0.1, 0.2, 0.3]),0); assert.deepEqual(candidate([-10.0, -20.0, -30.0]),0); assert.deepEqual(candidate([-1.0, -2.0, 8.0]),0); assert.deepEqual(candidate([0.2, 3.0, 5.0]),34); assert.deepEqual(candidate([-9.0, -7.0, -5.0, -3.0, -1.0, 1.0, 3.0, 5.0, 7.0, 9.0]),165); } test();
humaneval-HumanEval_138_is_equal_to_sum_even.json-L24
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers // Example // >>> isEqualToSumEven((4l)) // (false) // >>> isEqualToSumEven((6l)) // (false) // >>> isEqualToSumEven((8l)) // (true) public static boolean isEqualToSumEven(long n) { if (n < 4) { return false; } long d = 2; if (n % 2 == 1) { d = 1;
long minX = d; long maxX = n / 4; while (minX <= maxX) { long x = (minX + maxX) / 2; long y = n - 2 * x; if (y % 2 == 1) { y--; } if (2 * x + y < n) { minX = x + 1; } else if (2 * x + y > n) { maxX = x - 1; } else { return true; } } return false; } }
}
} public static void main(String[] args) { assert(isEqualToSumEven((4l)) == (false)); assert(isEqualToSumEven((6l)) == (false)); assert(isEqualToSumEven((8l)) == (true)); assert(isEqualToSumEven((10l)) == (true)); assert(isEqualToSumEven((11l)) == (false)); assert(isEqualToSumEven((12l)) == (true)); assert(isEqualToSumEven((13l)) == (false)); assert(isEqualToSumEven((16l)) == (true)); } }
humaneval-HumanEval_102_choose_num.json-L21
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // This function takes two positive numbers x and y and returns the // biggest even integer number that is in the range [x, y] inclusive. If // there's no such number, then the function should return -1. // For example: // >>> chooseNum((12l), (15l)) // (14l) // >>> chooseNum((13l), (12l)) // (-1l) public static long chooseNum(long x, long y) { long chosen = -1; for (long i = x; i <= y; i++) { if (i % 2 == 0) {
} } return chosen; } }
chosen = i;
} public static void main(String[] args) { assert(chooseNum((12l), (15l)) == (14l)); assert(chooseNum((13l), (12l)) == (-1l)); assert(chooseNum((33l), (12354l)) == (12354l)); assert(chooseNum((5234l), (5233l)) == (-1l)); assert(chooseNum((6l), (29l)) == (28l)); assert(chooseNum((27l), (10l)) == (-1l)); assert(chooseNum((7l), (7l)) == (-1l)); assert(chooseNum((546l), (546l)) == (546l)); } }
humaneval-HumanEval_1_separate_paren_groups.json-L11
js
//Input to this function is a string containing multiple groups of nested parentheses. Your goal is to // separate those group into separate strings and return the array of those. // Separate groups are balanced (each open brace is properly closed) and not nested within each other // Ignore any spaces in the input string. // >>> separate_paren_groups("( ) (( )) (( )( ))") // ["()", "(())", "(()())"] function separate_paren_groups(paren_string){ var count = 0; var group = ''; var groups = [];
if (paren_string[i] === '('){ count++; group += paren_string[i]; } else if (paren_string[i] === ')'){ count--; group += paren_string[i]; } if (count === 0 && group){ groups.push(group); group = ''; } } return groups; }
for (var i = 0; i < paren_string.length; i++){
const assert = require('node:assert'); function test() { let candidate = separate_paren_groups; assert.deepEqual(candidate("(()()) ((())) () ((())()())"),["(()())", "((()))", "()", "((())()())"]); assert.deepEqual(candidate("() (()) ((())) (((())))"),["()", "(())", "((()))", "(((())))"]); assert.deepEqual(candidate("(()(())((())))"),["(()(())((())))"]); assert.deepEqual(candidate("( ) (( )) (( )( ))"),["()", "(())", "(()())"]); } test();
humaneval-HumanEval_96_count_up_to.json-L26
js
//Implement a function that takes an non-negative integer and returns an array of the first n // integers that are prime numbers and less than n. // for example: // >>> count_up_to(5) // [2, 3] // >>> count_up_to(11) // [2, 3, 5, 7] // >>> count_up_to(0) // [] // >>> count_up_to(20) // [2, 3, 5, 7, 11, 13, 17, 19] // >>> count_up_to(1) // [] // >>> count_up_to(18) // [2, 3, 5, 7, 11, 13, 17] function count_up_to(n){ if (n < 2) return []; var output = [2]; for (var i = 3; i < n; i += 2){ var prime = true; for (var j = 3; j < i; j += 2){ if (i % j === 0){ prime = false; break; }
if (prime) output.push(i); } return output; }
}
const assert = require('node:assert'); function test() { let candidate = count_up_to; assert.deepEqual(candidate(5),[2, 3]); assert.deepEqual(candidate(6),[2, 3, 5]); assert.deepEqual(candidate(7),[2, 3, 5]); assert.deepEqual(candidate(10),[2, 3, 5, 7]); assert.deepEqual(candidate(0),[]); assert.deepEqual(candidate(22),[2, 3, 5, 7, 11, 13, 17, 19]); assert.deepEqual(candidate(1),[]); assert.deepEqual(candidate(18),[2, 3, 5, 7, 11, 13, 17]); assert.deepEqual(candidate(47),[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]); assert.deepEqual(candidate(101),[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]); } test();
humaneval-HumanEval_30_get_positive.json-L11
py
from typing import List def get_positive(l: List[int]) -> List[int]: """Return only positive numbers in the list. >>> get_positive([-1, 2, -4, 5, 6]) [2, 5, 6] >>> get_positive([5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10]) [5, 3, 2, 3, 9, 123, 1] """ p = []
if i > 0: p.append(i) return p
for i in l:
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([]) == [] def test_check(): check(get_positive) test_check()
humaneval-HumanEval_8_sum_product.json-L8
js
//For a given array of integers, return an array consisting of a sum and a product of all the integers in an array. // Empty sum should be equal to 0 and empty product should be equal to 1. // >>> sum_product([]) // [0, 1] // >>> sum_product([1, 2, 3, 4]) // [10, 24] function sum_product(numbers){
return [0, 1]; } let sum = 0; let product = 1; for(let i = 0; i < numbers.length; i++){ sum += numbers[i]; product *= numbers[i]; } return [sum, product]; }
if(numbers.length == 0){
const assert = require('node:assert'); function test() { let candidate = sum_product; assert.deepEqual(candidate([]),[0, 1]); assert.deepEqual(candidate([1, 1, 1]),[3, 1]); assert.deepEqual(candidate([100, 0]),[100, 0]); assert.deepEqual(candidate([3, 5, 7]),[15, 105]); assert.deepEqual(candidate([10]),[10, 10]); } test();
humaneval-HumanEval_148_bf.json-L26
js
//There are eight planets in our solar system: the closerst to the Sun // is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, // Uranus, Neptune. // Write a function that takes two planet names as strings planet1 and planet2. // The function should return an array containing all planets whose orbits are // located between the orbit of planet1 and the orbit of planet2, sorted by // the proximity to the sun. // The function should return an empty array if planet1 or planet2 // are not correct planet names. // Examples // >>> bf("Jupiter", "Neptune") // ["Saturn", "Uranus"] // >>> bf("Earth", "Mercury") // "Venus" // >>> bf("Mercury", "Uranus") // ["Venus", "Earth", "Mars", "Jupiter", "Saturn"] function bf(planet1, planet2){ var planets = { 'Mercury':1, 'Venus':2, 'Earth':3, 'Mars':4, 'Jupiter':5, 'Saturn':6, 'Uranus':7,
} if(!planets[planet1] || !planets[planet2]){ return []; } var keys = Object.keys(planets); var position1 = keys.indexOf(planet1); var position2 = keys.indexOf(planet2); if(position1 > position2){ var temp = position1; position1 = position2; position2 = temp; } var res = []; for(var i = position1 + 1; i < position2; i++){ res.push(keys[i]); } return res; }
'Neptune':8
const assert = require('node:assert'); function test() { let candidate = bf; assert.deepEqual(candidate("Jupiter", "Neptune"),["Saturn", "Uranus"]); assert.deepEqual(candidate("Earth", "Mercury"),["Venus"]); assert.deepEqual(candidate("Mercury", "Uranus"),["Venus", "Earth", "Mars", "Jupiter", "Saturn"]); assert.deepEqual(candidate("Neptune", "Venus"),["Earth", "Mars", "Jupiter", "Saturn", "Uranus"]); assert.deepEqual(candidate("Earth", "Earth"),[]); assert.deepEqual(candidate("Mars", "Earth"),[]); assert.deepEqual(candidate("Jupiter", "Makemake"),[]); } test();
humaneval-HumanEval_119_match_parens.json-L31
js
//You are given an array of two strings, both strings consist of open // parentheses '(' or close parentheses ')' only. // Your job is to check if it is possible to concatenate the two strings in // some order, that the resulting string will be good. // A string S is considered to be good if and only if all parentheses in S // are balanced. For example: the string '(())()' is good, while the string // '())' is not. // Return 'Yes' if there's a way to make a good string, and return 'No' otherwise. // Examples: // >>> match_parens(["()(", ")"]) // "Yes" // >>> match_parens([")", ")"]) // "No" function match_parens(lst){ const open = '('; const close = ')'; const is_good = (s) => { let stack = []; for (let p of s){ if (p === open){ stack.push(p); } else if (p === close){ if (stack.length === 0){ return false; } stack.pop(); } } return stack.length === 0; };
const str2 = lst[1]; if (is_good(str1 + str2)){ return 'Yes'; } if (is_good(str2 + str1)){ return 'Yes'; } return 'No'; }
const str1 = lst[0];
const assert = require('node:assert'); function test() { let candidate = match_parens; assert.deepEqual(candidate(["()(", ")"]),"Yes"); assert.deepEqual(candidate([")", ")"]),"No"); assert.deepEqual(candidate(["(()(())", "())())"]),"No"); assert.deepEqual(candidate([")())", "(()()("]),"Yes"); assert.deepEqual(candidate(["(())))", "(()())(("]),"Yes"); assert.deepEqual(candidate(["()", "())"]),"No"); assert.deepEqual(candidate(["(()(", "()))()"]),"Yes"); assert.deepEqual(candidate(["((((", "((())"]),"No"); assert.deepEqual(candidate([")(()", "(()("]),"No"); assert.deepEqual(candidate([")(", ")("]),"No"); assert.deepEqual(candidate(["(", ")"]),"Yes"); assert.deepEqual(candidate([")", "("]),"Yes"); } test();
humaneval-HumanEval_18_how_many_times.json-L16
js
//Find how many times a given substring can be found in the original string. Count overlaping cases. // >>> how_many_times("", "a") // 0 // >>> how_many_times("aaa", "a") // 3 // >>> how_many_times("aaaa", "aa") // 3 function how_many_times(string, substring){ var i = 0; var count = 0; while(string.indexOf(substring, i) !== -1){ count++; i = string.indexOf(substring, i) + 1; } return count;
}
const assert = require('node:assert'); function test() { let candidate = how_many_times; assert.deepEqual(candidate("", "x"),0); assert.deepEqual(candidate("xyxyxyx", "x"),4); assert.deepEqual(candidate("cacacacac", "cac"),4); assert.deepEqual(candidate("john doe", "john"),1); } test();
humaneval-HumanEval_66_digitSum.json-L27
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Task // Write a function that takes a string as input and returns the sum of the upper characters only' // ASCII codes. // Examples: // >>> digitSum(("")) // (0l) // >>> digitSum(("abAB")) // (131l) // >>> digitSum(("abcCd")) // (67l) // >>> digitSum(("helloE")) // (69l) // >>> digitSum(("woArBld")) // (131l) // >>> digitSum(("aAaaaXa")) // (153l) public static long digitSum(String s) { return s.chars().filter(i -> i >= 65 && i <= 90).sum();
}
}
} public static void main(String[] args) { assert(digitSum(("")) == (0l)); assert(digitSum(("abAB")) == (131l)); assert(digitSum(("abcCd")) == (67l)); assert(digitSum(("helloE")) == (69l)); assert(digitSum(("woArBld")) == (131l)); assert(digitSum(("aAaaaXa")) == (153l)); assert(digitSum((" How are yOu?")) == (151l)); assert(digitSum(("You arE Very Smart")) == (327l)); } }
humaneval-HumanEval_20_find_closest_elements.json-L10
js
//From a supplied array of numbers (of length at least two) select and return two that are the closest to each // other and return them in order (smaller number, larger number). // >>> find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.2]) // [2.0, 2.2] // >>> find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.0]) // [2.0, 2.0] function find_closest_elements(numbers){ let closest = Math.abs(numbers[0] - numbers[1]); let a = numbers[0];
for (let i = 0; i < numbers.length; i++) { for (let j = i + 1; j < numbers.length; j++) { if (Math.abs(numbers[i] - numbers[j]) < closest) { closest = Math.abs(numbers[i] - numbers[j]); a = numbers[i]; b = numbers[j]; } } } return a > b ? [b, a] : [a, b]; }
let b = numbers[1];
const assert = require('node:assert'); function test() { let candidate = find_closest_elements; assert.deepEqual(candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2]),[3.9, 4.0]); assert.deepEqual(candidate([1.0, 2.0, 5.9, 4.0, 5.0]),[5.0, 5.9]); assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.2]),[2.0, 2.2]); assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0]),[2.0, 2.0]); assert.deepEqual(candidate([1.1, 2.2, 3.1, 4.1, 5.1]),[2.2, 3.1]); } test();
humaneval-HumanEval_129_minPath.json-L27
js
//Given a grid with N rows and N columns (N >= 2) and a positive integer k, // each cell of the grid contains a value. Every integer in the range [1, N * N] // inclusive appears exactly once on the cells of the grid. // You have to find the minimum path of length k in the grid. You can start // from any cell, and in each step you can move to any of the neighbor cells, // in other words, you can go to cells which share an edge with you current // cell. // Please note that a path of length k means visiting exactly k cells (not // necessarily distinct). // You CANNOT go off the grid. // A path A (of length k) is considered less than a path B (of length k) if // after making the ordered arrays of the values on the cells that A and B go // through (let's call them lst_A and lst_B), lst_A is lexicographically less // than lst_B, in other words, there exist an integer index i (1 <= i <= k) // such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have // lst_A[j] = lst_B[j]. // It is guaranteed that the answer is unique. // Return an ordered array of the values on the cells that the minimum path go through. // Examples: // >>> minPath([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3) // [1, 2, 1] // >>> minPath([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1) // [1] function minPath(grid, k){ let n = grid.length; let path = [];
for(let i = 0; i < n; i++){ for(let j = 0; j < n; j++){ let curr = grid[i][j]; if(curr < startCell){ startCell = curr; } } } let currCell = startCell; for(let l = 0; l < k; l++){ path.push(currCell); let neighbours = []; let x = null; let y = null; for(let i = 0; i < n; i++){ for(let j = 0; j < n; j++){ if(grid[i][j] === currCell){ x = i; y = j; } } } if(x > 0){ neighbours.push(grid[x - 1][y]); } if(x < n - 1){ neighbours.push(grid[x + 1][y]); } if(y > 0){ neighbours.push(grid[x][y - 1]); } if(y < n - 1){ neighbours.push(grid[x][y + 1]); } let nextCell = neighbours[0]; for(let i = 0; i < neighbours.length; i++){ let curr = neighbours[i]; if(curr < nextCell){ nextCell = curr; } } currCell = nextCell; } return path; }
let startCell = grid[0][0];
const assert = require('node:assert'); function test() { let candidate = minPath; assert.deepEqual(candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3),[1, 2, 1]); assert.deepEqual(candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1),[1]); assert.deepEqual(candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4),[1, 2, 1, 2]); assert.deepEqual(candidate([[6, 4, 13, 10], [5, 7, 12, 1], [3, 16, 11, 15], [8, 14, 9, 2]], 7),[1, 10, 1, 10, 1, 10, 1]); assert.deepEqual(candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5),[1, 7, 1, 7, 1]); assert.deepEqual(candidate([[11, 8, 7, 2], [5, 16, 14, 4], [9, 3, 15, 6], [12, 13, 10, 1]], 9),[1, 6, 1, 6, 1, 6, 1, 6, 1]); assert.deepEqual(candidate([[12, 13, 10, 1], [9, 3, 15, 6], [5, 16, 14, 4], [11, 8, 7, 2]], 12),[1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6]); assert.deepEqual(candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8),[1, 3, 1, 3, 1, 3, 1, 3]); assert.deepEqual(candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8),[1, 5, 1, 5, 1, 5, 1, 5]); assert.deepEqual(candidate([[1, 2], [3, 4]], 10),[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]); assert.deepEqual(candidate([[1, 3], [3, 2]], 10),[1, 3, 1, 3, 1, 3, 1, 3, 1, 3]); } test();
humaneval-HumanEval_63_fibfib.json-L36
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // 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 compute the n-th element of the fibfib number sequence. // >>> fibfib((1l)) // (0l) // >>> fibfib((5l)) // (4l) // >>> fibfib((8l)) // (24l) public static long fibfib(long n) { if (n == 0) { return 0; } if (n == 1) { return 0; } if (n == 2) { return 1; } long[] memo = new long[(int) n + 1]; memo[0] = 0; memo[1] = 0; memo[2] = 1; for (int i = 3; i <= n; i++) {
} return memo[(int) n]; } }
memo[i] = memo[i - 1] + memo[i - 2] + memo[i - 3];
} public static void main(String[] args) { assert(fibfib((2l)) == (1l)); assert(fibfib((1l)) == (0l)); assert(fibfib((5l)) == (4l)); assert(fibfib((8l)) == (24l)); assert(fibfib((10l)) == (81l)); assert(fibfib((12l)) == (274l)); assert(fibfib((14l)) == (927l)); } }
humaneval-HumanEval_143_words_in_sentence.json-L28
js
//You are given a string representing a sentence, // the sentence contains some words separated by a space, // and you have to return a string that contains the words from the original sentence, // whose lengths are prime numbers, // the order of the words in the new string should be the same as the original one. // Example 1: // >>> words_in_sentence("This is a test") // "is" // Example 2: // >>> words_in_sentence("lets go for swimming") // "go for" // Constraints: // * 1 <= len(sentence) <= 100 // * sentence contains only letters function words_in_sentence(sentence){ return sentence.split(' ').filter(word => { var number = word.length; var isPrime = true; if(number === 1 || number === 0){ return false; } for(var i = 2; i < number; i++){ if(number % i === 0){ isPrime = false; break; } }
}).join(' '); }
return isPrime;
const assert = require('node:assert'); function test() { let candidate = words_in_sentence; assert.deepEqual(candidate("This is a test"),"is"); assert.deepEqual(candidate("lets go for swimming"),"go for"); assert.deepEqual(candidate("there is no place available here"),"there is no place"); assert.deepEqual(candidate("Hi I am Hussein"),"Hi am Hussein"); assert.deepEqual(candidate("go for it"),"go for it"); assert.deepEqual(candidate("here"),""); assert.deepEqual(candidate("here is"),"is"); } test();
humaneval-HumanEval_6_parse_nested_parens.json-L31
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Input to this function is a string represented multiple groups for nested parentheses separated by spaces. // For each of the group, output the deepest level of nesting of parentheses. // E.g. (()()) has maximum two levels of nesting while ((())) has three. // >>> parseNestedParens(("(()()) ((())) () ((())()())")) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)1l, (long)3l))) public static ArrayList<Long> parseNestedParens(String paren_string) { ArrayList<Long> result = new ArrayList<Long>(); String[] paren_strings = paren_string.split(" "); for (String paren : paren_strings) { long max_depth = 0; long current_depth = 0; for (char c : paren.toCharArray()) { if (c == '(') { current_depth += 1; max_depth = Math.max(max_depth, current_depth); } else if (c == ')') { current_depth -= 1; } } result.add(max_depth); } return result;
}
}
} public static void main(String[] args) { assert(parseNestedParens(("(()()) ((())) () ((())()())")).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)1l, (long)3l))))); assert(parseNestedParens(("() (()) ((())) (((())))")).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l))))); assert(parseNestedParens(("(()(())((())))")).equals((new ArrayList<Long>(Arrays.asList((long)4l))))); } }
humaneval-HumanEval_64_vowels_count.json-L27
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Write a function vowels_count which takes a string representing // a 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. // Example: // >>> vowelsCount(("abcde")) // (2l) // >>> vowelsCount(("ACEDY")) // (3l) public static long vowelsCount(String s) { long sum = 0; String sUpper = s.toUpperCase(); int lastIndex = sUpper.length() - 1; for (int i = 0; i < sUpper.length(); i++) { if (sUpper.charAt(i) == 'A' || sUpper.charAt(i) == 'E' || sUpper.charAt(i) == 'I' || sUpper.charAt(i) == 'O' || sUpper.charAt(i) == 'U') { sum++; } if (sUpper.charAt(i) == 'Y' && i == lastIndex) {
} } return sum; } }
sum++;
} public static void main(String[] args) { assert(vowelsCount(("abcde")) == (2l)); assert(vowelsCount(("Alone")) == (3l)); assert(vowelsCount(("key")) == (2l)); assert(vowelsCount(("bye")) == (1l)); assert(vowelsCount(("keY")) == (2l)); assert(vowelsCount(("bYe")) == (1l)); assert(vowelsCount(("ACEDY")) == (3l)); } }
humaneval-HumanEval_22_filter_integers.json-L10
py
from typing import List, Any def filter_integers(values: List[Any]) -> List[int]: """ Filter given list of any python values only for integers >>> filter_integers(['a', 3.14, 5]) [5] >>> filter_integers([1, 2, 3, 'abc', { }, []]) [1, 2, 3] """
return [n for n in values if isinstance(n, int)]
def check(candidate): assert candidate([]) == [] assert candidate([4, { }, [], 23.2, 9, 'adasd']) == [4, 9] assert candidate([3, 'c', 3, 3, 'a', 'b']) == [3, 3, 3] def test_check(): check(filter_integers) test_check()
humaneval-HumanEval_37_sort_even.json-L15
py
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. >>> sort_even([1, 2, 3]) [1, 2, 3] >>> sort_even([5, 6, 3, 4]) [3, 6, 5, 4] """ if l: temp: List[int] = [] for i in range(0, len(l), 2):
temp.sort() for i in range(0, len(l), 2): l[i] = temp[i // 2] return l
temp.append(l[i])
def check(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(): check(sort_even) test_check()
humaneval-HumanEval_111_histogram.json-L36
js
//Given a string representing a space separated lowercase letters, return an object // of the letter with the most repetition and containing the corresponding count. // If several letters have the same occurrence, return all of them. // Example: // >>> histogram("a b c") // {"a": 1, "b": 1, "c": 1} // >>> histogram("a b b a") // {"a": 2, "b": 2} // >>> histogram("a b c a b") // {"a": 2, "b": 2} // >>> histogram("b b b b a") // {"b": 4} // >>> histogram("") // {} function histogram(test){ if (test.length === 0){ return {}; } var lst = test.split(" "); var count = {}; for (var i = 0; i < lst.length; i++){ if (count[lst[i]]){ count[lst[i]] += 1; } else{ count[lst[i]] = 1; } } console.log(count); var max = 0; var result = {}; for (var key in count){ if (count[key] > max){ max = count[key]; result = {};
} else if (count[key] === max){ result[key] = max; } } return result; }
result[key] = max;
const assert = require('node:assert'); function test() { let candidate = histogram; assert.deepEqual(candidate("a b b a"),{"a": 2, "b": 2}); assert.deepEqual(candidate("a b c a b"),{"a": 2, "b": 2}); assert.deepEqual(candidate("a b c d g"),{"a": 1, "b": 1, "c": 1, "d": 1, "g": 1}); assert.deepEqual(candidate("r t g"),{"r": 1, "t": 1, "g": 1}); assert.deepEqual(candidate("b b b b a"),{"b": 4}); assert.deepEqual(candidate("r t g"),{"r": 1, "t": 1, "g": 1}); assert.deepEqual(candidate(""),{}); assert.deepEqual(candidate("a"),{"a": 1}); } test();
humaneval-HumanEval_83_starts_one_ends.json-L15
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given a positive integer n, return the count of the numbers of n-digit // positive integers that start or end with 1. public static long startsOneEnds(long n) { long lower = (long) Math.pow(10, n - 1); long upper = (long) Math.pow(10, n) - 1; long count = 0;
String s = "" + i; if (s.startsWith("1") || s.endsWith("1")) count++; } return count; } }
for (long i = lower; i <= upper; i++) {
} public static void main(String[] args) { assert(startsOneEnds((1l)) == (1l)); assert(startsOneEnds((2l)) == (18l)); assert(startsOneEnds((3l)) == (180l)); assert(startsOneEnds((4l)) == (1800l)); assert(startsOneEnds((5l)) == (18000l)); } }
humaneval-HumanEval_135_can_arrange.json-L15
js
//Create a function which returns the largest index of an element which // is not greater than or equal to the element immediately preceding it. If // no such element exists then return -1. The given array will not contain // duplicate values. // Examples: // >>> can_arrange([1, 2, 4, 3, 5]) // 3 // >>> can_arrange([1, 2, 3]) // -1 function can_arrange(arr){ var max = arr[0]; var res = -1; for(var i = 1; i < arr.length; i++){ if(arr[i] < max){
}else{ max = arr[i]; } } return res; }
res = i;
const assert = require('node:assert'); function test() { let candidate = can_arrange; assert.deepEqual(candidate([1, 2, 4, 3, 5]),3); assert.deepEqual(candidate([1, 2, 4, 5]),-1); assert.deepEqual(candidate([1, 4, 2, 5, 6, 7, 8, 9, 10]),2); assert.deepEqual(candidate([4, 8, 5, 7, 3]),4); assert.deepEqual(candidate([]),-1); } test();
humaneval-HumanEval_88_sort_array.json-L23
py
from typing import List def sort_array(array: List[int]) -> List[int]: """ Given an array of non-negative integers, return a copy of the given array after sorting, you will sort the given array in ascending order if the sum( first index value, last index value) is odd, or sort it in descending order if the sum( first index value, last index value) is even. Note: * don't change the given array. Examples: >>> sort_array([]) [] >>> sort_array([5]) [5] >>> sort_array([2, 4, 3, 0, 1, 5]) [0, 1, 2, 3, 4, 5] >>> sort_array([2, 4, 3, 0, 1, 5, 6]) [6, 5, 4, 3, 2, 1, 0] """ if array:
return array
return sorted(array, reverse=True) if (array[0] + array[-1]) % 2 == 0 else sorted(array)
def check(candidate): assert candidate([]) == [] assert candidate([5]) == [5] assert candidate([2, 4, 3, 0, 1, 5]) == [0, 1, 2, 3, 4, 5] assert candidate([2, 4, 3, 0, 1, 5, 6]) == [6, 5, 4, 3, 2, 1, 0] assert candidate([2, 1]) == [1, 2] assert candidate([15, 42, 87, 32, 11, 0]) == [0, 11, 15, 32, 42, 87] assert candidate([21, 14, 23, 11]) == [23, 21, 14, 11] def test_check(): check(sort_array) test_check()
humaneval-HumanEval_4_mean_absolute_deviation.json-L18
js
//For a given array 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 this case): // MAD = average | x - x_mean | // >>> mean_absolute_deviation([1.0, 2.0, 3.0, 4.0]) // 1.0 function mean_absolute_deviation(numbers){ var sum = 0; for(var i = 0; i < numbers.length; i++){ sum += numbers[i]; } var mean = sum / numbers.length; var sum_abs_diff = 0; for(var i = 0; i < numbers.length; i++){ sum_abs_diff += Math.abs(numbers[i] - mean); }
}
return sum_abs_diff / numbers.length;
const assert = require('node:assert'); function test() { let candidate = mean_absolute_deviation; assert.deepEqual(candidate([1.0, 2.0]),0.5); assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0]),1.0); assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0, 5.0]),1.2); } test();
humaneval-HumanEval_114_minSubArraySum.json-L14
py
from typing import List def minSubArraySum(nums: List[int]) -> int: """ Given an array of integers nums, find the minimum sum of any non-empty sub-array of nums. Example >>> minSubArraySum([2, 3, 4, 1, 2, 4]) 1 >>> minSubArraySum([-1, -2, -3]) -6 """ for i in range(1, len(nums)):
return min(nums)
nums[i] = min(nums[i], nums[i] + nums[i - 1])
def check(candidate): assert candidate([2, 3, 4, 1, 2, 4]) == 1 assert candidate([-1, -2, -3]) == -6 assert candidate([-1, -2, -3, 2, -10]) == -14 assert candidate([-9999999999999999]) == -9999999999999999 assert candidate([0, 10, 20, 1000000]) == 0 assert candidate([-1, -2, -3, 10, -5]) == -6 assert candidate([100, -1, -2, -3, 10, -5]) == -6 assert candidate([10, 11, 13, 8, 3, 4]) == 3 assert candidate([100, -33, 32, -1, 0, -2]) == -33 assert candidate([-10]) == -10 assert candidate([7]) == 7 assert candidate([1, -1]) == -1 def test_check(): check(minSubArraySum) test_check()
humaneval-HumanEval_105_by_length.json-L48
js
//Given an array of integers, sort the integers that are between 1 and 9 inclusive, // reverse the resulting array, and then replace each digit by its corresponding name from // "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine". // For example: // >>> by_length([2, 1, 1, 4, 5, 8, 2, 3]) // ["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"] // If the array is empty, return an empty array: // >>> by_length([]) // [] // If the array has any strange number ignore it: // >>> by_length([1, -1, 55]) // ["One"] function by_length(arr){ var arr2 = []; for(var i = 0; i < arr.length; i++){ var num = arr[i]; if(num > 0 && num < 10){ arr2.push(num); } } arr2.sort(); arr2.reverse(); var arr3 = []; for(var i = 0; i < arr2.length; i++){ var num = arr2[i]; if(num == 1){ arr3.push("One"); } else if(num == 2){ arr3.push("Two"); } else if(num == 3){ arr3.push("Three"); } else if(num == 4){ arr3.push("Four"); } else if(num == 5){ arr3.push("Five"); } else if(num == 6){ arr3.push("Six"); } else if(num == 7){ arr3.push("Seven"); } else if(num == 8){
} else if(num == 9){ arr3.push("Nine"); } } return arr3; }
arr3.push("Eight");
const assert = require('node:assert'); function test() { let candidate = by_length; assert.deepEqual(candidate([2, 1, 1, 4, 5, 8, 2, 3]),["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"]); assert.deepEqual(candidate([]),[]); assert.deepEqual(candidate([1, -1, 55]),["One"]); assert.deepEqual(candidate([1, -1, 3, 2]),["Three", "Two", "One"]); assert.deepEqual(candidate([9, 4, 8]),["Nine", "Eight", "Four"]); } test();
humaneval-HumanEval_108_count_nums.json-L32
js
//Write a function count_nums which takes an array of integers and returns // the number of elements which has a sum of digits > 0. // If a number is negative, then its first signed digit will be negative: // e.g. -123 has signed digits -1, 2, and 3. // >>> count_nums([]) // 0 // >>> count_nums([-1, 11, -11]) // 1 // >>> count_nums([1, 1, 2]) // 3 function count_nums(arr){ if(!arr.length) { return 0; } var count = 0; for(var i = 0; i < arr.length; i++) { var sum = 0; var str = arr[i].toString(); if(str[0] === '-') { sum += Number(str[0] + str[1]); for(var j = 2; j < str.length; j++) { sum += Number(str[j]); } } else { for(var k = 0; k < str.length; k++) { sum += Number(str[k]); } } if(sum > 0) { count++; }
return count; }
}
const assert = require('node:assert'); function test() { let candidate = count_nums; assert.deepEqual(candidate([]),0); assert.deepEqual(candidate([-1, -2, 0]),0); assert.deepEqual(candidate([1, 1, 2, -2, 3, 4, 5]),6); assert.deepEqual(candidate([1, 6, 9, -6, 0, 1, 5]),5); assert.deepEqual(candidate([1, 100, 98, -7, 1, -1]),4); assert.deepEqual(candidate([12, 23, 34, -45, -56, 0]),5); assert.deepEqual(candidate([0, 1]),1); assert.deepEqual(candidate([1]),1); } test();
humaneval-HumanEval_81_numerical_letter_grade.json-L43
js
//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. // She has given you an array of GPAs for some students and you have to write // a function that can output an array of letter grades using the following table: // GPA | Letter grade // 4.0 A+ // > 3.7 A // > 3.3 A- // > 3.0 B+ // > 2.7 B // > 2.3 B- // > 2.0 C+ // > 1.7 C // > 1.3 C- // > 1.0 D+ // > 0.7 D // > 0.0 D- // 0.0 E // Example: // >>> grade_equation([4.0, 3, 1.7, 2, 3.5]) // ["A+", "B", "C-", "C", "A-"] function numerical_letter_grade(grades){ var grades_array = []; for (var i = 0; i < grades.length; i++){ if (grades[i] >= 4.0){ grades_array.push("A+"); } else if (grades[i] > 3.7){ grades_array.push("A"); } else if (grades[i] > 3.3){ grades_array.push("A-"); } else if (grades[i] > 3.0){ grades_array.push("B+"); } else if (grades[i] > 2.7){ grades_array.push("B"); } else if (grades[i] > 2.3){ grades_array.push("B-");
else if (grades[i] > 2.0){ grades_array.push("C+"); } else if (grades[i] > 1.7){ grades_array.push("C"); } else if (grades[i] > 1.3){ grades_array.push("C-"); } else if (grades[i] > 1.0){ grades_array.push("D+"); } else if (grades[i] > 0.7){ grades_array.push("D"); } else if (grades[i] > 0.0){ grades_array.push("D-"); } else if (grades[i] == 0.0){ grades_array.push("E"); } } return grades_array; }
}
const assert = require('node:assert'); function test() { let candidate = numerical_letter_grade; assert.deepEqual(candidate([4.0, 3, 1.7, 2, 3.5]),["A+", "B", "C-", "C", "A-"]); assert.deepEqual(candidate([1.2]),["D+"]); assert.deepEqual(candidate([0.5]),["D-"]); assert.deepEqual(candidate([0.0]),["E"]); assert.deepEqual(candidate([1.0, 0.3, 1.5, 2.8, 3.3]),["D", "D-", "C-", "B", "B+"]); assert.deepEqual(candidate([0.0, 0.7]),["E", "D-"]); } test();
humaneval-HumanEval_54_same_chars.json-L24
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Check if two words have the same characters. // >>> sameChars(("eabcdzzzz"), ("dddzzzzzzzddeddabc")) // (true) // >>> sameChars(("abcd"), ("dddddddabc")) // (true) // >>> sameChars(("dddddddabc"), ("abcd")) // (true) // >>> sameChars(("eabcd"), ("dddddddabc")) // (false) // >>> sameChars(("abcd"), ("dddddddabce")) // (false) // >>> sameChars(("eabcdzzzz"), ("dddzzzzzzzddddabc")) // (false) public static boolean sameChars(String s0, String s1) { return s0.chars().distinct().sorted().boxed().collect(Collectors.toList()).equals(s1.chars().distinct().sorted().boxed().collect(Collectors.toList()));
}
}
} public static void main(String[] args) { assert(sameChars(("eabcdzzzz"), ("dddzzzzzzzddeddabc")) == (true)); assert(sameChars(("abcd"), ("dddddddabc")) == (true)); assert(sameChars(("dddddddabc"), ("abcd")) == (true)); assert(sameChars(("eabcd"), ("dddddddabc")) == (false)); assert(sameChars(("abcd"), ("dddddddabcf")) == (false)); assert(sameChars(("eabcdzzzz"), ("dddzzzzzzzddddabc")) == (false)); assert(sameChars(("aabb"), ("aaccc")) == (false)); } }
humaneval-HumanEval_31_is_prime.json-L23
js
//Return true if a given number is prime, and false otherwise. // >>> is_prime(6) // false // >>> is_prime(101) // true // >>> is_prime(11) // true // >>> is_prime(13441) // true // >>> is_prime(61) // true // >>> is_prime(4) // false // >>> is_prime(1) // false function is_prime(n){ if (n==1) { return false; } if (n==2) { return true;
if (n%2==0) { return false; } var limit = Math.sqrt(n); for (var i = 3; i <= limit; i += 2) { if (n%i==0) { return false; } } return true; }
}
const assert = require('node:assert'); function test() { let candidate = is_prime; assert.deepEqual(candidate(6),false); assert.deepEqual(candidate(101),true); assert.deepEqual(candidate(11),true); assert.deepEqual(candidate(13441),true); assert.deepEqual(candidate(61),true); assert.deepEqual(candidate(4),false); assert.deepEqual(candidate(1),false); assert.deepEqual(candidate(5),true); assert.deepEqual(candidate(11),true); assert.deepEqual(candidate(17),true); assert.deepEqual(candidate(85),false); assert.deepEqual(candidate(77),false); assert.deepEqual(candidate(255379),false); } test();
humaneval-HumanEval_158_find_max.json-L21
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Write a function that accepts an array array list of strings. // The array list contains different words. Return the word with maximum number // of unique characters. If multiple strings have maximum number of unique // characters, return the one which comes first in lexicographical order. // >>> findMax((new ArrayList<String>(Arrays.asList((String)"name", (String)"of", (String)"string")))) // ("string") // >>> findMax((new ArrayList<String>(Arrays.asList((String)"name", (String)"enam", (String)"game")))) // ("enam") // >>> findMax((new ArrayList<String>(Arrays.asList((String)"aaaaaaa", (String)"bb", (String)"cc")))) // ("aaaaaaa") public static String findMax(ArrayList<String> words) { Map<String, Integer> wordsToUniqueCharacters = new HashMap<String, Integer>();
Set<Character> uniqueCharacters = new HashSet<Character>(); for (char c : word.toCharArray()) { uniqueCharacters.add(c); } wordsToUniqueCharacters.put(word, uniqueCharacters.size()); } words.sort(new Comparator<String>() { @Override public int compare(String o1, String o2) { int compareResult = wordsToUniqueCharacters.get(o2).compareTo(wordsToUniqueCharacters.get(o1)); if (compareResult == 0) { return o1.compareTo(o2); } return compareResult; } }); return words.get(0); } }
for (String word : words) {
} public static void main(String[] args) { assert(findMax((new ArrayList<String>(Arrays.asList((String)"name", (String)"of", (String)"string")))).equals(("string"))); assert(findMax((new ArrayList<String>(Arrays.asList((String)"name", (String)"enam", (String)"game")))).equals(("enam"))); assert(findMax((new ArrayList<String>(Arrays.asList((String)"aaaaaaa", (String)"bb", (String)"cc")))).equals(("aaaaaaa"))); assert(findMax((new ArrayList<String>(Arrays.asList((String)"abc", (String)"cba")))).equals(("abc"))); assert(findMax((new ArrayList<String>(Arrays.asList((String)"play", (String)"this", (String)"game", (String)"of", (String)"footbott")))).equals(("footbott"))); assert(findMax((new ArrayList<String>(Arrays.asList((String)"we", (String)"are", (String)"gonna", (String)"rock")))).equals(("gonna"))); assert(findMax((new ArrayList<String>(Arrays.asList((String)"we", (String)"are", (String)"a", (String)"mad", (String)"nation")))).equals(("nation"))); assert(findMax((new ArrayList<String>(Arrays.asList((String)"this", (String)"is", (String)"a", (String)"prrk")))).equals(("this"))); assert(findMax((new ArrayList<String>(Arrays.asList((String)"b")))).equals(("b"))); assert(findMax((new ArrayList<String>(Arrays.asList((String)"play", (String)"play", (String)"play")))).equals(("play"))); } }
humaneval-HumanEval_7_filter_by_substring.json-L9
js
//Filter an input array of strings only for ones that contain given substring // >>> filter_by_substring([], "a") // [] // >>> filter_by_substring(["abc", "bacd", "cde", "array"], "a") // ["abc", "bacd", "array"] function filter_by_substring(strings, substring){ var filtered = []; for(var i = 0; i < strings.length; i++){
filtered.push(strings[i]); } } return filtered; }
if(strings[i].includes(substring)){
const assert = require('node:assert'); function test() { let candidate = filter_by_substring; assert.deepEqual(candidate([], "john"),[]); assert.deepEqual(candidate(["xxx", "asd", "xxy", "john doe", "xxxAAA", "xxx"], "xxx"),["xxx", "xxxAAA", "xxx"]); assert.deepEqual(candidate(["xxx", "asd", "aaaxxy", "john doe", "xxxAAA", "xxx"], "xx"),["xxx", "aaaxxy", "xxxAAA", "xxx"]); assert.deepEqual(candidate(["grunt", "trumpet", "prune", "gruesome"], "run"),["grunt", "prune"]); } test();
humaneval-HumanEval_157_right_angle_triangle.json-L13
js
//Given the lengths of the three sides of a triangle. Return true if the three // sides form a right-angled triangle, false otherwise. // A right-angled triangle is a triangle in which one angle is right angle or // 90 degree. // Example: // >>> right_angle_triangle(3, 4, 5) // true // >>> right_angle_triangle(1, 2, 3) // false function right_angle_triangle(a, b, c){ if(a**2 + b**2 == c**2 || b**2 + c**2 == a**2 || a**2 + c**2 == b**2) { return true;
return false; } }
} else {
const assert = require('node:assert'); function test() { let candidate = right_angle_triangle; assert.deepEqual(candidate(3, 4, 5),true); assert.deepEqual(candidate(1, 2, 3),false); assert.deepEqual(candidate(10, 6, 8),true); assert.deepEqual(candidate(2, 2, 2),false); assert.deepEqual(candidate(7, 24, 25),true); assert.deepEqual(candidate(10, 5, 7),false); assert.deepEqual(candidate(5, 12, 13),true); assert.deepEqual(candidate(15, 8, 17),true); assert.deepEqual(candidate(48, 55, 73),true); assert.deepEqual(candidate(1, 1, 1),false); assert.deepEqual(candidate(2, 2, 10),false); } test();
humaneval-HumanEval_105_by_length.json-L40
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given an array array list of integers, sort the integers that are between 1 and 9 inclusive, // reverse the resulting array array list, and then replace each digit by its corresponding name from // "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine". // For example: // >>> byLength((new ArrayList<Long>(Arrays.asList((long)2l, (long)1l, (long)1l, (long)4l, (long)5l, (long)8l, (long)2l, (long)3l)))) // (new ArrayList<String>(Arrays.asList((String)"Eight", (String)"Five", (String)"Four", (String)"Three", (String)"Two", (String)"Two", (String)"One", (String)"One"))) // If the array array list is empty, return an empty array array list: // >>> byLength((new ArrayList<Long>(Arrays.asList()))) // (new ArrayList<String>(Arrays.asList())) // If the array array list has any strange number ignore it: // >>> byLength((new ArrayList<Long>(Arrays.asList((long)1l, (long)-1l, (long)55l)))) // (new ArrayList<String>(Arrays.asList((String)"One"))) public static ArrayList<String> byLength(ArrayList<Long> arr) { String[] a = new String[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; ArrayList<String> res = new ArrayList<String>(); ArrayList<Long> good = new ArrayList<Long>(); for (Long i : arr) { if (i >= 1 && i <= 9) { good.add(i); } } good.sort(new Comparator<Long>() { public int compare(Long i1, Long i2) { return i1.compareTo(i2); } }); Collections.reverse(good); for (Long i : good) { res.add(a[i.intValue() - 1]); } return res;
}
}
} public static void main(String[] args) { assert(byLength((new ArrayList<Long>(Arrays.asList((long)2l, (long)1l, (long)1l, (long)4l, (long)5l, (long)8l, (long)2l, (long)3l)))).equals((new ArrayList<String>(Arrays.asList((String)"Eight", (String)"Five", (String)"Four", (String)"Three", (String)"Two", (String)"Two", (String)"One", (String)"One"))))); assert(byLength((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<String>(Arrays.asList())))); assert(byLength((new ArrayList<Long>(Arrays.asList((long)1l, (long)-1l, (long)55l)))).equals((new ArrayList<String>(Arrays.asList((String)"One"))))); assert(byLength((new ArrayList<Long>(Arrays.asList((long)1l, (long)-1l, (long)3l, (long)2l)))).equals((new ArrayList<String>(Arrays.asList((String)"Three", (String)"Two", (String)"One"))))); assert(byLength((new ArrayList<Long>(Arrays.asList((long)9l, (long)4l, (long)8l)))).equals((new ArrayList<String>(Arrays.asList((String)"Nine", (String)"Eight", (String)"Four"))))); } }
humaneval-HumanEval_88_sort_array.json-L18
js
//Given an array of non-negative integers, return a cojs of the given array after sorting, // you will sort the given array in ascending order if the sum( first index value, last index value) is odd, // or sort it in descending order if the sum( first index value, last index value) is even. // Note: // * don't change the given array. // Examples: // >>> sort_array([]) // [] // >>> sort_array([5]) // [5] // >>> sort_array([2, 4, 3, 0, 1, 5]) // [0, 1, 2, 3, 4, 5] // >>> sort_array([2, 4, 3, 0, 1, 5, 6]) // [6, 5, 4, 3, 2, 1, 0] function sort_array(array){ let sum = array[0] + array[array.length - 1]; let newArr = [];
newArr = array.sort(function(a, b){return b - a;}); }else{ newArr = array.sort(function(a, b){return a - b;}); } return newArr; }
if(sum % 2 === 0){
const assert = require('node:assert'); function test() { let candidate = sort_array; assert.deepEqual(candidate([]),[]); assert.deepEqual(candidate([5]),[5]); assert.deepEqual(candidate([2, 4, 3, 0, 1, 5]),[0, 1, 2, 3, 4, 5]); assert.deepEqual(candidate([2, 4, 3, 0, 1, 5, 6]),[6, 5, 4, 3, 2, 1, 0]); assert.deepEqual(candidate([2, 1]),[1, 2]); assert.deepEqual(candidate([15, 42, 87, 32, 11, 0]),[0, 11, 15, 32, 42, 87]); assert.deepEqual(candidate([21, 14, 23, 11]),[23, 21, 14, 11]); } test();
humaneval-HumanEval_123_get_odd_collatz.json-L19
js
//Given a positive integer n, return a sorted array that has the odd numbers in collatz sequence. // The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined // as follows: start with any positive integer n. Then each term is obtained from the // previous term as follows: if the previous term is even, the next term is one half of // the previous term. If the previous term is odd, the next term is 3 times the previous // term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1. // Note: // 1. Collatz(1) is [1]. // 2. returned array sorted in increasing order. // For example: // get_odd_collatz(5) returns [1, 5] # The collatz sequence for 5 is [5, 16, 8, 4, 2, 1], so the odd numbers are only 1, and 5. // >>> get_odd_collatz(5) // [1, 5] function get_odd_collatz(n){ var collatz_sequence = [] var odd_numbers = [] var odd_count = 0 collatz_sequence.push(n)
if(collatz_sequence[i]%2 == 0){ collatz_sequence.push(collatz_sequence[i]/2) } else if(collatz_sequence[i]%2 == 1){ collatz_sequence.push(collatz_sequence[i]*3+1) } if(collatz_sequence[i]%2 == 1){ odd_numbers.push(collatz_sequence[i]) odd_count += 1 } if(collatz_sequence[i] == 1){ break } } if(odd_count != 0){ odd_numbers.sort(function(a, b){return a-b}) return odd_numbers } else{ odd_numbers.push(1) return odd_numbers } }
for(var i = 0; i < collatz_sequence.length; i++){
const assert = require('node:assert'); function test() { let candidate = get_odd_collatz; assert.deepEqual(candidate(14),[1, 5, 7, 11, 13, 17]); assert.deepEqual(candidate(5),[1, 5]); assert.deepEqual(candidate(12),[1, 3, 5]); assert.deepEqual(candidate(1),[1]); } test();
humaneval-HumanEval_1_separate_paren_groups.json-L26
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Input to this function is a string containing multiple groups of nested parentheses. Your goal is to // separate those group into separate strings and return the array list of those. // Separate groups are balanced (each open brace is properly closed) and not nested within each other // Ignore any spaces in the input string. // >>> separateParenGroups(("( ) (( )) (( )( ))")) // (new ArrayList<String>(Arrays.asList((String)"()", (String)"(())", (String)"(()())"))) public static ArrayList<String> separateParenGroups(String paren_string) { ArrayList<String> output = new ArrayList<String>(); StringBuffer temp = new StringBuffer(); int open_paren = 0; for (int i = 0; i < paren_string.length(); i++) { char c = paren_string.charAt(i); if (c == '(') { if (open_paren == 0) { temp = new StringBuffer(); } open_paren++;
} else if (c == ')') { open_paren--; temp.append(c); if (open_paren == 0) { output.add(temp.toString()); } } } return output; } }
temp.append(c);
} public static void main(String[] args) { assert(separateParenGroups(("(()()) ((())) () ((())()())")).equals((new ArrayList<String>(Arrays.asList((String)"(()())", (String)"((()))", (String)"()", (String)"((())()())"))))); assert(separateParenGroups(("() (()) ((())) (((())))")).equals((new ArrayList<String>(Arrays.asList((String)"()", (String)"(())", (String)"((()))", (String)"(((())))"))))); assert(separateParenGroups(("(()(())((())))")).equals((new ArrayList<String>(Arrays.asList((String)"(()(())((())))"))))); assert(separateParenGroups(("( ) (( )) (( )( ))")).equals((new ArrayList<String>(Arrays.asList((String)"()", (String)"(())", (String)"(()())"))))); } }
humaneval-HumanEval_19_sort_numbers.json-L17
js
//Input is a space-delimited string of numberals 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 // >>> sort_numbers("three one five") // "one three five" function sort_numbers(numbers){ var numString = numbers.split(" "); var numMap = { "zero": 0, "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7,
"nine": 9 }; numString.sort(function(a, b){ return numMap[a] - numMap[b]; }); numString = numString.join(" "); return numString; }
"eight": 8,
const assert = require('node:assert'); function test() { let candidate = sort_numbers; assert.deepEqual(candidate(""),""); assert.deepEqual(candidate("three"),"three"); assert.deepEqual(candidate("three five nine"),"three five nine"); assert.deepEqual(candidate("five zero four seven nine eight"),"zero four five seven eight nine"); assert.deepEqual(candidate("six five four three two one zero"),"zero one two three four five six"); } test();
humaneval-HumanEval_5_intersperse.json-L20
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Insert a number 'delimeter' between every two consecutive elements of input array list `numbers' // >>> intersperse((new ArrayList<Long>(Arrays.asList())), (4l)) // (new ArrayList<Long>(Arrays.asList())) // >>> intersperse((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))), (4l)) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)2l, (long)4l, (long)3l))) public static ArrayList<Long> intersperse(ArrayList<Long> numbers, long delimeter) { ArrayList<Long> result = new ArrayList<Long>(); for (int i = 0; i < (numbers.size()); i++) { result.add(numbers.get(i)); if ((i < (numbers.size() - 1))) { result.add(delimeter);
} return result; } }
}
} public static void main(String[] args) { assert(intersperse((new ArrayList<Long>(Arrays.asList())), (7l)).equals((new ArrayList<Long>(Arrays.asList())))); assert(intersperse((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)3l, (long)2l))), (8l)).equals((new ArrayList<Long>(Arrays.asList((long)5l, (long)8l, (long)6l, (long)8l, (long)3l, (long)8l, (long)2l))))); assert(intersperse((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l, (long)2l))), (2l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l, (long)2l, (long)2l, (long)2l))))); } }
humaneval-HumanEval_123_get_odd_collatz.json-L23
js
//Given a positive integer n, return a sorted array that has the odd numbers in collatz sequence. // The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined // as follows: start with any positive integer n. Then each term is obtained from the // previous term as follows: if the previous term is even, the next term is one half of // the previous term. If the previous term is odd, the next term is 3 times the previous // term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1. // Note: // 1. Collatz(1) is [1]. // 2. returned array sorted in increasing order. // For example: // get_odd_collatz(5) returns [1, 5] # The collatz sequence for 5 is [5, 16, 8, 4, 2, 1], so the odd numbers are only 1, and 5. // >>> get_odd_collatz(5) // [1, 5] function get_odd_collatz(n){ var collatz_sequence = [] var odd_numbers = [] var odd_count = 0 collatz_sequence.push(n) for(var i = 0; i < collatz_sequence.length; i++){ if(collatz_sequence[i]%2 == 0){ collatz_sequence.push(collatz_sequence[i]/2) }
collatz_sequence.push(collatz_sequence[i]*3+1) } if(collatz_sequence[i]%2 == 1){ odd_numbers.push(collatz_sequence[i]) odd_count += 1 } if(collatz_sequence[i] == 1){ break } } if(odd_count != 0){ odd_numbers.sort(function(a, b){return a-b}) return odd_numbers } else{ odd_numbers.push(1) return odd_numbers } }
else if(collatz_sequence[i]%2 == 1){
const assert = require('node:assert'); function test() { let candidate = get_odd_collatz; assert.deepEqual(candidate(14),[1, 5, 7, 11, 13, 17]); assert.deepEqual(candidate(5),[1, 5]); assert.deepEqual(candidate(12),[1, 3, 5]); assert.deepEqual(candidate(1),[1]); } test();
humaneval-HumanEval_90_next_smallest.json-L15
js
//You are given an array of integers. // Write a function next_smallest() that returns the 2nd smallest element of the array. // Return undefined if there is no such element. // >>> next_smallest([1, 2, 3, 4, 5]) // 2 // >>> next_smallest([5, 1, 4, 3, 2]) // 2 // >>> next_smallest([]) // undefined // >>> next_smallest([1, 1]) // undefined function next_smallest(lst){ if(lst.length<=1){ return undefined;
var smallest=Math.min(...lst); var second_smallest=Number.MAX_VALUE; for(var i in lst){ if(lst[i]>smallest&&lst[i]<second_smallest){ second_smallest=lst[i]; } } if(second_smallest==Number.MAX_VALUE){ return undefined; } return second_smallest; }
}
const assert = require('node:assert'); function test() { let candidate = next_smallest; assert.deepEqual(candidate([1, 2, 3, 4, 5]),2); assert.deepEqual(candidate([5, 1, 4, 3, 2]),2); assert.deepEqual(candidate([]),undefined); assert.deepEqual(candidate([1, 1]),undefined); assert.deepEqual(candidate([1, 1, 1, 1, 0]),1); assert.deepEqual(candidate([1, 1]),undefined); assert.deepEqual(candidate([-35, 34, 12, -45]),-35); } test();
humaneval-HumanEval_160_do_algebra.json-L66
js
//Given two arrays operator, and operand. The first array has basic algebra operations, and // the second array is an array of integers. Use the two given arrays to build the algebric // expression and return the evaluation of this expression. // The basic algebra operations: // Addition ( + ) // Subtraction ( - ) // Multiplication ( * ) // Floor division ( // ) // Exponentiation ( ** ) // Example: // operator['+', '*', '-'] // array = [2, 3, 4, 5] // result = 2 + 3 * 4 - 5 // => result = 9 // Note: // The length of operator array is equal to the length of operand array minus one. // Operand is an array of of non-negative integers. // Operator array has at least one operator, and operand array has at least two operands. function do_algebra(operator, operand){ let do_again; let count = 0; let temp; let answer; while(count < operator.length){ switch(operator[count]){ case '*': operand[count] = operand[count] * operand[count + 1]; operand.splice(count + 1, 1); operator.splice(count, 1); count = 0; do_again = true; break; case '//': operand[count] = Math.floor(operand[count] / operand[count + 1]); operand.splice(count + 1, 1); operator.splice(count, 1); count = 0; do_again = true; break; case '**': operand[count] = Math.pow(operand[count], operand[count + 1]); operand.splice(count + 1, 1); operator.splice(count, 1); count = 0; do_again = true; break; default: count++; do_again = false; } } answer = operand[0]; operand.splice(0, 1); while(operand.length > 0){ switch(operator[0]){ case '+': answer += operand[0]; break; case '-': answer -= operand[0]; break; } operand.splice(0, 1); operator.splice(0, 1); }
}
return answer;
const assert = require('node:assert'); function test() { let candidate = do_algebra; assert.deepEqual(candidate(["**", "*", "+"], [2, 3, 4, 5]),37); assert.deepEqual(candidate(["+", "*", "-"], [2, 3, 4, 5]),9); assert.deepEqual(candidate(["//", "*"], [7, 3, 4]),8); } test();
humaneval-HumanEval_106_f.json-L26
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Implement the function f that takes n as a parameter, // and returns an array array list of size n, such that the value of the element at index i is the factorial of i if i is even // or the sum of numbers from 1 to i otherwise. // i starts from 1. // the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i). // Example: // >>> f((5l)) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)6l, (long)24l, (long)15l))) public static ArrayList<Long> f(long n) { ArrayList<Long> ret = new ArrayList<Long>(); for (long i = 1l; i <= n; i++) { long v = 0l; if (i % 2 == 0) { v = 1l; for (long j = 2l; j <= i; j++) { v *= j; }
v = (i * (i + 1l)) / 2l; } ret.add(v); } return ret; } }
} else {
} public static void main(String[] args) { assert(f((5l)).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)6l, (long)24l, (long)15l))))); assert(f((7l)).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)6l, (long)24l, (long)15l, (long)720l, (long)28l))))); assert(f((1l)).equals((new ArrayList<Long>(Arrays.asList((long)1l))))); assert(f((3l)).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)6l))))); } }
humaneval-HumanEval_104_unique_digits.json-L37
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given an array array list of positive integers x. return a sorted array list of all // elements that hasn't any even digit. // Note: Returned array list should be sorted in increasing order. // For example: // >>> uniqueDigits((new ArrayList<Long>(Arrays.asList((long)15l, (long)33l, (long)1422l, (long)1l)))) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)15l, (long)33l))) // >>> uniqueDigits((new ArrayList<Long>(Arrays.asList((long)152l, (long)323l, (long)1422l, (long)10l)))) // (new ArrayList<Long>(Arrays.asList())) public static ArrayList<Long> uniqueDigits(ArrayList<Long> x) { ArrayList<Long> b = new ArrayList<Long>(); for (int i = 0; i < x.size(); i++) { if (x.get(i) < 0) { x.set(i, x.get(i)*(long)-1); } boolean unique = true; long z = x.get(i); while (z > 0) { long y = z % 10; if (y % 2 == 0) { unique = false; break; } z = z / 10; } if (unique) { b.add(x.get(i)); } }
return b; } }
Collections.sort(b);
} public static void main(String[] args) { assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)15l, (long)33l, (long)1422l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)15l, (long)33l))))); assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)152l, (long)323l, (long)1422l, (long)10l)))).equals((new ArrayList<Long>(Arrays.asList())))); assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)12345l, (long)2033l, (long)111l, (long)151l)))).equals((new ArrayList<Long>(Arrays.asList((long)111l, (long)151l))))); assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)135l, (long)103l, (long)31l)))).equals((new ArrayList<Long>(Arrays.asList((long)31l, (long)135l))))); } }
humaneval-HumanEval_124_valid_date.json-L34
js
//You have to write a function which validates a given date string and // returns true if the date is valid otherwise false. // The date is valid if all of the following rules are satisfied: // 1. The date string is not empty. // 2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the number of days is not less than 1 or higher than 30 days for months 4,6,9,11. And, the number of days is not less than 1 or higher than 29 for the month 2. // 3. The months should not be less than 1 or higher than 12. // 4. The date should be in the format: mm-dd-yyyy // >>> valid_date("03-11-2000") // true // >>> valid_date("15-01-2012") // false // >>> valid_date("04-0-2040") // false // >>> valid_date("06-04-2020") // true // >>> valid_date("06/04/2020") // false function valid_date(date){ var date_arr = date.split("-"); if(date_arr.length == 3){ var month = parseInt(date_arr[0]); var day = parseInt(date_arr[1]); var year = parseInt(date_arr[2]); if(month < 1 || month > 12){ return false; }else if(month == 2){ if(day < 1 || day > 29){ return false; } }else if((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day < 1 || day > 31)){ return false; }else if((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)){ return false;
return true; } return false; }
}
const assert = require('node:assert'); function test() { let candidate = valid_date; assert.deepEqual(candidate("03-11-2000"),true); assert.deepEqual(candidate("15-01-2012"),false); assert.deepEqual(candidate("04-0-2040"),false); assert.deepEqual(candidate("06-04-2020"),true); assert.deepEqual(candidate("01-01-2007"),true); assert.deepEqual(candidate("03-32-2011"),false); assert.deepEqual(candidate(""),false); assert.deepEqual(candidate("04-31-3000"),false); assert.deepEqual(candidate("06-06-2005"),true); assert.deepEqual(candidate("21-31-2000"),false); assert.deepEqual(candidate("04-12-2003"),true); assert.deepEqual(candidate("04122003"),false); assert.deepEqual(candidate("20030412"),false); assert.deepEqual(candidate("2003-04"),false); assert.deepEqual(candidate("2003-04-12"),false); assert.deepEqual(candidate("04-2003"),false); } test();
humaneval-HumanEval_77_iscube.json-L22
js
//Write a function that takes an integer a and returns true // if this ingeger is a cube of some integer number. // Note: you may assume the input is always valid. // Examples: // >>> iscube(1) // true // >>> iscube(2) // false // >>> iscube(-1) // true // >>> iscube(64) // true // >>> iscube(0) // true // >>> iscube(180) // false function iscube(a){ if(Math.cbrt(a)%1==0){ return true; }else{ return false;
}
}
const assert = require('node:assert'); function test() { let candidate = iscube; assert.deepEqual(candidate(1),true); assert.deepEqual(candidate(2),false); assert.deepEqual(candidate(-1),true); assert.deepEqual(candidate(64),true); assert.deepEqual(candidate(180),false); assert.deepEqual(candidate(1000),true); assert.deepEqual(candidate(0),true); assert.deepEqual(candidate(1729),false); } test();
humaneval-HumanEval_154_cycpattern_check.json-L17
js
//You are given 2 words. You need to return true if the second word or any of its rotations is a substring in the first word // >>> cycpattern_check("abcd", "abd") // false // >>> cycpattern_check("hello", "ell") // true // >>> cycpattern_check("whassup", "psus") // false // >>> cycpattern_check("abab", "baa") // true // >>> cycpattern_check("efef", "eeff") // false // >>> cycpattern_check("himenss", "simen") // true function cycpattern_check(a, b){ var result = false; for(var i = 0; i < b.length; i++){
b = b.slice(1) + b[0]; } return result; }
result = result || a.includes(b);
const assert = require('node:assert'); function test() { let candidate = cycpattern_check; assert.deepEqual(candidate("xyzw", "xyw"),false); assert.deepEqual(candidate("yello", "ell"),true); assert.deepEqual(candidate("whattup", "ptut"),false); assert.deepEqual(candidate("efef", "fee"),true); assert.deepEqual(candidate("abab", "aabb"),false); assert.deepEqual(candidate("winemtt", "tinem"),true); } test();
humaneval-HumanEval_161_solve.json-L26
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // You are given a string s. // if s[i] is a letter, reverse its case from lower to upper or vise versa, // otherwise keep it as it is. // If the string contains no letters, reverse the string. // The function should return the resulted string. // Examples // >>> solve(("1234")) // ("4321") // >>> solve(("ab")) // ("AB") // >>> solve(("#a@C")) // ("#A@c") public static String solve(String s) { boolean letterNotFound = true; StringBuilder sb = new StringBuilder(); if (s.length() == 0) return s; for (int i = 0; i < s.length(); i++) {
letterNotFound = false; if (Character.isUpperCase(s.charAt(i))) { sb.append(Character.toLowerCase(s.charAt(i))); } else { sb.append(Character.toUpperCase(s.charAt(i))); } } else { sb.append(s.charAt(i)); } } if (letterNotFound) { return sb.reverse().toString(); } return sb.toString(); } }
if (Character.isLetter(s.charAt(i))) {
} public static void main(String[] args) { assert(solve(("AsDf")).equals(("aSdF"))); assert(solve(("1234")).equals(("4321"))); assert(solve(("ab")).equals(("AB"))); assert(solve(("#a@C")).equals(("#A@c"))); assert(solve(("#AsdfW^45")).equals(("#aSDFw^45"))); assert(solve(("#6@2")).equals(("2@6#"))); assert(solve(("#$a^D")).equals(("#$A^d"))); assert(solve(("#ccc")).equals(("#CCC"))); } }
humaneval-HumanEval_40_triples_sum_to_zero.json-L20
py
from typing import List def triples_sum_to_zero(l: List[int]) -> bool: """ triples_sum_to_zero takes a list of integers as an input. it returns True if there are three distinct elements in the list that sum to zero, and False otherwise. >>> triples_sum_to_zero([1, 3, 5, 0]) False >>> triples_sum_to_zero([1, 3, -2, 1]) True >>> triples_sum_to_zero([1, 2, 3, 7]) False >>> triples_sum_to_zero([2, 4, -5, 3, 9, 7]) True >>> triples_sum_to_zero([1]) False """
l.sort() 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
assert len(l) > 0, "The input list cannot be empty"
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([1]) == False assert candidate([1, 3, 5, -100]) == False assert candidate([100, 3, 5, -100]) == False def test_check(): check(triples_sum_to_zero) test_check()
humaneval-HumanEval_14_all_prefixes.json-L9
js
//Return array of all prefixes from shortest to longest of the input string // >>> all_prefixes("abc") // ["a", "ab", "abc"] function all_prefixes(string){ var result = []; for (var i = 1; i <= string.length; i++){ result.push(string.slice(0, i)); }
}
return result;
const assert = require('node:assert'); function test() { let candidate = all_prefixes; assert.deepEqual(candidate(""),[]); assert.deepEqual(candidate("asdfgh"),["a", "as", "asd", "asdf", "asdfg", "asdfgh"]); assert.deepEqual(candidate("WWW"),["W", "WW", "WWW"]); } test();
humaneval-HumanEval_106_f.json-L23
js
//Implement the function f that takes n as a parameter, // and returns an array of size n, such that the value of the element at index i is the factorial of i if i is even // or the sum of numbers from 1 to i otherwise. // i starts from 1. // the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i). // Example: // >>> f(5) // [1, 2, 6, 24, 15] function f(n){ var result = []; var sum = 0; var factorial = 1; for(var i = 1; i <= n; i++){ if(i % 2 == 0){ for(var j = 1; j <= i; j++){ factorial *= j; } result.push(factorial); factorial = 1; }else{ for(var k = 1; k <= i; k++){ sum += k;
result.push(sum); sum = 0; } } return result; }
}
const assert = require('node:assert'); function test() { let candidate = f; assert.deepEqual(candidate(5),[1, 2, 6, 24, 15]); assert.deepEqual(candidate(7),[1, 2, 6, 24, 15, 720, 28]); assert.deepEqual(candidate(1),[1]); assert.deepEqual(candidate(3),[1, 2, 6]); } test();
humaneval-HumanEval_1_separate_paren_groups.json-L13
py
from typing import List def separate_paren_groups(paren_string: str) -> List[str]: """ Input to this function is a string containing multiple groups of nested parentheses. Your goal is to separate those group into separate strings and return the list of those. Separate groups are balanced (each open brace is properly closed) and not nested within each other Ignore any spaces in the input string. >>> separate_paren_groups('( ) (( )) (( )( ))') ['()', '(())', '(()())'] """ paren_string = paren_string.replace(' ', '') groups = []
open_count = 0 for c in paren_string: if c == '(': open_count += 1 elif c == ')': open_count -= 1 group += c if open_count == 0: groups.append(group) group = '' return groups
group = ''
def check(candidate): assert candidate('(()()) ((())) () ((())()())') == ['(()())', '((()))', '()', '((())()())'] assert candidate('() (()) ((())) (((())))') == ['()', '(())', '((()))', '(((())))'] assert candidate('(()(())((())))') == ['(()(())((())))'] assert candidate('( ) (( )) (( )( ))') == ['()', '(())', '(()())'] def test_check(): check(separate_paren_groups) test_check()
humaneval-HumanEval_1_separate_paren_groups.json-L30
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Input to this function is a string containing multiple groups of nested parentheses. Your goal is to // separate those group into separate strings and return the array list of those. // Separate groups are balanced (each open brace is properly closed) and not nested within each other // Ignore any spaces in the input string. // >>> separateParenGroups(("( ) (( )) (( )( ))")) // (new ArrayList<String>(Arrays.asList((String)"()", (String)"(())", (String)"(()())"))) public static ArrayList<String> separateParenGroups(String paren_string) { ArrayList<String> output = new ArrayList<String>(); StringBuffer temp = new StringBuffer(); int open_paren = 0; for (int i = 0; i < paren_string.length(); i++) { char c = paren_string.charAt(i); if (c == '(') { if (open_paren == 0) { temp = new StringBuffer(); } open_paren++; temp.append(c); } else if (c == ')') { open_paren--; temp.append(c);
output.add(temp.toString()); } } } return output; } }
if (open_paren == 0) {
} public static void main(String[] args) { assert(separateParenGroups(("(()()) ((())) () ((())()())")).equals((new ArrayList<String>(Arrays.asList((String)"(()())", (String)"((()))", (String)"()", (String)"((())()())"))))); assert(separateParenGroups(("() (()) ((())) (((())))")).equals((new ArrayList<String>(Arrays.asList((String)"()", (String)"(())", (String)"((()))", (String)"(((())))"))))); assert(separateParenGroups(("(()(())((())))")).equals((new ArrayList<String>(Arrays.asList((String)"(()(())((())))"))))); assert(separateParenGroups(("( ) (( )) (( )( ))")).equals((new ArrayList<String>(Arrays.asList((String)"()", (String)"(())", (String)"(()())"))))); } }
humaneval-HumanEval_109_move_one_ball.json-L33
js
//We have an array 'arr' of N integers arr[1], arr[2], ..., arr[N].The // numbers in the array will be randomly ordered. Your task is to determine if // it is possible to get an array sorted in non-decreasing order by performing // the following operation on the given array: // You are allowed to perform right shift operation any number of times. // One right shift operation means shifting all elements of the array by one // position in the right direction. The last element of the array will be moved to // the starting position in the array i.e. 0th index. // If it is possible to obtain the sorted array by performing the above operation // then return true else return false. // If the given array is empty then return true. // Note: The given array is guaranteed to have unique elements. // For Example: // >>> move_one_ball([3, 4, 5, 1, 2]) // true // Explanation: By performin 2 right shift operations, non-decreasing order can // be achieved for the given array. // >>> move_one_ball([3, 5, 4, 1, 2]) // false // Explanation:It is not possible to get non-decreasing order for the given // array by performing any number of right shift operations. function move_one_ball(arr){ var diff_count = 0; var i = 0; var temp = 0; for(var i = 0; i < arr.length - 1; i++){ if(arr[i] > arr[i + 1]){ diff_count += 1; } } if(diff_count > 1){ return false;
else{ return true; } }
}
const assert = require('node:assert'); function test() { let candidate = move_one_ball; assert.deepEqual(candidate([3, 4, 5, 1, 2]),true); assert.deepEqual(candidate([3, 5, 10, 1, 2]),true); assert.deepEqual(candidate([4, 3, 1, 2]),false); assert.deepEqual(candidate([3, 5, 4, 1, 2]),false); assert.deepEqual(candidate([]),true); } test();
humaneval-HumanEval_113_odd_count.json-L11
js
//Given an array of strings, where each string consists of only digits, return an array. // Each element i of the output should be "the number of odd elements in the // string i of the input." where all the i's should be replaced by the number // of odd digits in the i'th string of the input. // >>> odd_count(["1234567"]) // ["the number of odd elements 4n the str4ng 4 of the 4nput."] // >>> odd_count(["3", "11111111"]) // ["the number of odd elements 1n the str1ng 1 of the 1nput.", "the number of odd elements 8n the str8ng 8 of the 8nput."] function odd_count(lst){ var new_lst = [];
var word = lst[i]; var odd_sum = 0; for (var j = 0; j < word.length; j++){ if (word[j] % 2 == 1){ odd_sum += 1; } } var odd_word = "the number of odd elements " + odd_sum + "n the str" + odd_sum + "ng " + odd_sum + " of the " + odd_sum + "nput."; new_lst.push(odd_word); } return new_lst; }
for (var i = 0; i < lst.length; i++){
const assert = require('node:assert'); function test() { let candidate = odd_count; assert.deepEqual(candidate(["1234567"]),["the number of odd elements 4n the str4ng 4 of the 4nput."]); assert.deepEqual(candidate(["3", "11111111"]),["the number of odd elements 1n the str1ng 1 of the 1nput.", "the number of odd elements 8n the str8ng 8 of the 8nput."]); assert.deepEqual(candidate(["271", "137", "314"]),["the number of odd elements 2n the str2ng 2 of the 2nput.", "the number of odd elements 3n the str3ng 3 of the 3nput.", "the number of odd elements 2n the str2ng 2 of the 2nput."]); } test();
humaneval-HumanEval_96_count_up_to.json-L34
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Implement a function that takes an non-negative integer and returns an array array list of the first n // integers that are prime numbers and less than n. // for example: // >>> countUpTo((5l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l))) // >>> countUpTo((11l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l))) // >>> countUpTo((0l)) // (new ArrayList<Long>(Arrays.asList())) // >>> countUpTo((20l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l))) // >>> countUpTo((1l)) // (new ArrayList<Long>(Arrays.asList())) // >>> countUpTo((18l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l))) public static ArrayList<Long> countUpTo(long n) { ArrayList<Long> arrayList = new ArrayList<Long>(); for (long i = 2l; i < n; i++) { boolean isPrime = true; for (long j = 2l; j < i; j++) { if (i % j == 0) { isPrime = false; break; } }
arrayList.add(i); } } return arrayList; } }
if (isPrime) {
} public static void main(String[] args) { assert(countUpTo((5l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l))))); assert(countUpTo((6l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l))))); assert(countUpTo((7l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l))))); assert(countUpTo((10l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l))))); assert(countUpTo((0l)).equals((new ArrayList<Long>(Arrays.asList())))); assert(countUpTo((22l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l))))); assert(countUpTo((1l)).equals((new ArrayList<Long>(Arrays.asList())))); assert(countUpTo((18l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l))))); assert(countUpTo((47l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l, (long)23l, (long)29l, (long)31l, (long)37l, (long)41l, (long)43l))))); assert(countUpTo((101l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l, (long)23l, (long)29l, (long)31l, (long)37l, (long)41l, (long)43l, (long)47l, (long)53l, (long)59l, (long)61l, (long)67l, (long)71l, (long)73l, (long)79l, (long)83l, (long)89l, (long)97l))))); } }
humaneval-HumanEval_105_by_length.json-L14
js
//Given an array of integers, sort the integers that are between 1 and 9 inclusive, // reverse the resulting array, and then replace each digit by its corresponding name from // "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine". // For example: // >>> by_length([2, 1, 1, 4, 5, 8, 2, 3]) // ["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"] // If the array is empty, return an empty array: // >>> by_length([]) // [] // If the array has any strange number ignore it: // >>> by_length([1, -1, 55]) // ["One"] function by_length(arr){
for(var i = 0; i < arr.length; i++){ var num = arr[i]; if(num > 0 && num < 10){ arr2.push(num); } } arr2.sort(); arr2.reverse(); var arr3 = []; for(var i = 0; i < arr2.length; i++){ var num = arr2[i]; if(num == 1){ arr3.push("One"); } else if(num == 2){ arr3.push("Two"); } else if(num == 3){ arr3.push("Three"); } else if(num == 4){ arr3.push("Four"); } else if(num == 5){ arr3.push("Five"); } else if(num == 6){ arr3.push("Six"); } else if(num == 7){ arr3.push("Seven"); } else if(num == 8){ arr3.push("Eight"); } else if(num == 9){ arr3.push("Nine"); } } return arr3; }
var arr2 = [];
const assert = require('node:assert'); function test() { let candidate = by_length; assert.deepEqual(candidate([2, 1, 1, 4, 5, 8, 2, 3]),["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"]); assert.deepEqual(candidate([]),[]); assert.deepEqual(candidate([1, -1, 55]),["One"]); assert.deepEqual(candidate([1, -1, 3, 2]),["Three", "Two", "One"]); assert.deepEqual(candidate([9, 4, 8]),["Nine", "Eight", "Four"]); } test();
humaneval-HumanEval_81_numerical_letter_grade.json-L46
js
//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. // She has given you an array of GPAs for some students and you have to write // a function that can output an array of letter grades using the following table: // GPA | Letter grade // 4.0 A+ // > 3.7 A // > 3.3 A- // > 3.0 B+ // > 2.7 B // > 2.3 B- // > 2.0 C+ // > 1.7 C // > 1.3 C- // > 1.0 D+ // > 0.7 D // > 0.0 D- // 0.0 E // Example: // >>> grade_equation([4.0, 3, 1.7, 2, 3.5]) // ["A+", "B", "C-", "C", "A-"] function numerical_letter_grade(grades){ var grades_array = []; for (var i = 0; i < grades.length; i++){ if (grades[i] >= 4.0){ grades_array.push("A+"); } else if (grades[i] > 3.7){ grades_array.push("A"); } else if (grades[i] > 3.3){ grades_array.push("A-"); } else if (grades[i] > 3.0){ grades_array.push("B+"); } else if (grades[i] > 2.7){ grades_array.push("B"); } else if (grades[i] > 2.3){ grades_array.push("B-"); } else if (grades[i] > 2.0){ grades_array.push("C+");
else if (grades[i] > 1.7){ grades_array.push("C"); } else if (grades[i] > 1.3){ grades_array.push("C-"); } else if (grades[i] > 1.0){ grades_array.push("D+"); } else if (grades[i] > 0.7){ grades_array.push("D"); } else if (grades[i] > 0.0){ grades_array.push("D-"); } else if (grades[i] == 0.0){ grades_array.push("E"); } } return grades_array; }
}
const assert = require('node:assert'); function test() { let candidate = numerical_letter_grade; assert.deepEqual(candidate([4.0, 3, 1.7, 2, 3.5]),["A+", "B", "C-", "C", "A-"]); assert.deepEqual(candidate([1.2]),["D+"]); assert.deepEqual(candidate([0.5]),["D-"]); assert.deepEqual(candidate([0.0]),["E"]); assert.deepEqual(candidate([1.0, 0.3, 1.5, 2.8, 3.3]),["D", "D-", "C-", "B", "B+"]); assert.deepEqual(candidate([0.0, 0.7]),["E", "D-"]); } test();
humaneval-HumanEval_122_add_elements.json-L20
py
from typing import List def add_elements(arr: List[int], k: int) -> int: """ Given a non-empty array of integers arr and an integer k, return the sum of the elements with at most two digits from the first k elements of arr. Example: >>> add_elements([111, 21, 3, 4000, 5, 6, 7, 8, 9], 4) 24 Constraints: 1. 1 <= len(arr) <= 100 2. 1 <= k <= len(arr) """ total = 0 for num in arr[:k]: if num < 100:
return total
total += num
def check(candidate): assert candidate([1, -2, -3, 41, 57, 76, 87, 88, 99], 3) == -4 assert candidate([111, 121, 3, 4000, 5, 6], 2) == 0 assert candidate([11, 21, 3, 90, 5, 6, 7, 8, 9], 4) == 125 assert candidate([111, 21, 3, 4000, 5, 6, 7, 8, 9], 4) == 24 assert candidate([1], 1) == 1 def test_check(): check(add_elements) test_check()
humaneval-HumanEval_151_double_the_difference.json-L18
js
//Given an array of numbers, return the sum of squares of the numbers // in the array that are odd. Ignore numbers that are negative or not integers. // >>> double_the_difference([1, 3, 2, 0]) // 10 // >>> double_the_difference([-1, -2, 0]) // 0 // >>> double_the_difference([9, -2]) // 81 // >>> double_the_difference([0]) // 0 // If the input array is empty, return 0. function double_the_difference(lst){ sum = 0; for (var i = 0; i < lst.length; i++){ if(lst[i] % 2 == 1 && lst[i] > 0){ sum += lst[i] ** 2; }
return sum; }
}
const assert = require('node:assert'); function test() { let candidate = double_the_difference; assert.deepEqual(candidate([]),0); assert.deepEqual(candidate([5.0, 4.0]),25); assert.deepEqual(candidate([0.1, 0.2, 0.3]),0); assert.deepEqual(candidate([-10.0, -20.0, -30.0]),0); assert.deepEqual(candidate([-1.0, -2.0, 8.0]),0); assert.deepEqual(candidate([0.2, 3.0, 5.0]),34); assert.deepEqual(candidate([-9.0, -7.0, -5.0, -3.0, -1.0, 1.0, 3.0, 5.0, 7.0, 9.0]),165); } test();
humaneval-HumanEval_86_anti_shuffle.json-L16
js
//Write a function that takes a string and returns an ordered version of it. // Ordered version of string, is a string where all words (separated by space) // are replaced by a new word where all the characters arranged in // ascending order based on ascii value. // Note: You should keep the order of words and blank spaces in the sentence. // For example: // >>> anti_shuffle("Hi") // "Hi" // >>> anti_shuffle("hello") // "ehllo" // >>> anti_shuffle("Hello World!!!") // "Hello !!!Wdlor" function anti_shuffle(s){ var result = []; var strings = s.split(" ");
var temp = str.split(""); temp.sort(); result.push(temp.join("")); }); return result.join(" "); }
strings.forEach(function(str){
const assert = require('node:assert'); function test() { let candidate = anti_shuffle; assert.deepEqual(candidate("Hi"),"Hi"); assert.deepEqual(candidate("hello"),"ehllo"); assert.deepEqual(candidate("number"),"bemnru"); assert.deepEqual(candidate("abcd"),"abcd"); assert.deepEqual(candidate("Hello World!!!"),"Hello !!!Wdlor"); assert.deepEqual(candidate(""),""); assert.deepEqual(candidate("Hi. My name is Mister Robot. How are you?"),".Hi My aemn is Meirst .Rboot How aer ?ouy"); } test();
humaneval-HumanEval_70_strange_sort_list.json-L30
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given array list of integers, return array list in strange order. // Strange sorting, is when you start with the minimum value, // then maximum of the remaining integers, then minimum and so on. // Examples: // >>> strangeSortList((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l)))) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)2l, (long)3l))) // >>> strangeSortList((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)5l, (long)5l)))) // (new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)5l, (long)5l))) // >>> strangeSortList((new ArrayList<Long>(Arrays.asList()))) // (new ArrayList<Long>(Arrays.asList())) public static ArrayList<Long> strangeSortList(ArrayList<Long> lst) { if (lst.size() <= 1) { return lst; } ArrayList<Long> result = new ArrayList<Long>(); ArrayList<Long> sorted = new ArrayList<Long>(lst); Collections.sort(sorted); int min = 0; int max = sorted.size() - 1; while (min <= max) { result.add(sorted.get(min));
if (min <= max) { result.add(sorted.get(max)); max--; } } return result; } }
min++;
} public static void main(String[] args) { assert(strangeSortList((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)2l, (long)3l))))); assert(strangeSortList((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)7l, (long)8l, (long)9l)))).equals((new ArrayList<Long>(Arrays.asList((long)5l, (long)9l, (long)6l, (long)8l, (long)7l))))); assert(strangeSortList((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)5l, (long)2l, (long)4l, (long)3l))))); assert(strangeSortList((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)7l, (long)8l, (long)9l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)9l, (long)5l, (long)8l, (long)6l, (long)7l))))); assert(strangeSortList((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)5l, (long)5l)))).equals((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)5l, (long)5l))))); assert(strangeSortList((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<Long>(Arrays.asList())))); assert(strangeSortList((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l, (long)6l, (long)7l, (long)8l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)8l, (long)2l, (long)7l, (long)3l, (long)6l, (long)4l, (long)5l))))); assert(strangeSortList((new ArrayList<Long>(Arrays.asList((long)0l, (long)2l, (long)2l, (long)2l, (long)5l, (long)5l, (long)-5l, (long)-5l)))).equals((new ArrayList<Long>(Arrays.asList((long)-5l, (long)5l, (long)-5l, (long)5l, (long)0l, (long)2l, (long)2l, (long)2l))))); assert(strangeSortList((new ArrayList<Long>(Arrays.asList((long)111111l)))).equals((new ArrayList<Long>(Arrays.asList((long)111111l))))); } }
humaneval-HumanEval_21_rescale_to_unit.json-L12
py
from typing import List def rescale_to_unit(numbers: List[float]) -> List[float]: """ Given 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 >>> rescale_to_unit([1.0, 2.0, 3.0, 4.0, 5.0]) [0.0, 0.25, 0.5, 0.75, 1.0] """ _min, _max = min(numbers), max(numbers) if _min == _max: return [0.5] * len(numbers)
return [(val - _min) / (_max - _min) for val in numbers]
def check(candidate): assert candidate([2.0, 49.9]) == [0.0, 1.0] assert candidate([100.0, 49.9]) == [1.0, 0.0] assert candidate([1.0, 2.0, 3.0, 4.0, 5.0]) == [0.0, 0.25, 0.5, 0.75, 1.0] assert candidate([2.0, 1.0, 5.0, 3.0, 4.0]) == [0.25, 0.0, 1.0, 0.5, 0.75] assert candidate([12.0, 11.0, 15.0, 13.0, 14.0]) == [0.25, 0.0, 1.0, 0.5, 0.75] def test_check(): check(rescale_to_unit) test_check()
humaneval-HumanEval_5_intersperse.json-L15
js
//Insert a number 'delimeter' between every two consecutive elements of input array `numbers' // >>> intersperse([], 4) // [] // >>> intersperse([1, 2, 3], 4) // [1, 4, 2, 4, 3] function intersperse(numbers, delimeter){ var result = []; for(var i = 0; i < numbers.length; i++){ result.push(numbers[i]); if(i !== numbers.length - 1){ result.push(delimeter); } } return result;
}
const assert = require('node:assert'); function test() { let candidate = intersperse; assert.deepEqual(candidate([], 7),[]); assert.deepEqual(candidate([5, 6, 3, 2], 8),[5, 8, 6, 8, 3, 8, 2]); assert.deepEqual(candidate([2, 2, 2], 2),[2, 2, 2, 2, 2]); } test();
humaneval-HumanEval_96_count_up_to.json-L38
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Implement a function that takes an non-negative integer and returns an array array list of the first n // integers that are prime numbers and less than n. // for example: // >>> countUpTo((5l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l))) // >>> countUpTo((11l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l))) // >>> countUpTo((0l)) // (new ArrayList<Long>(Arrays.asList())) // >>> countUpTo((20l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l))) // >>> countUpTo((1l)) // (new ArrayList<Long>(Arrays.asList())) // >>> countUpTo((18l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l))) public static ArrayList<Long> countUpTo(long n) { ArrayList<Long> arrayList = new ArrayList<Long>(); for (long i = 2l; i < n; i++) { boolean isPrime = true; for (long j = 2l; j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) { arrayList.add(i); } }
} }
return arrayList;
} public static void main(String[] args) { assert(countUpTo((5l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l))))); assert(countUpTo((6l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l))))); assert(countUpTo((7l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l))))); assert(countUpTo((10l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l))))); assert(countUpTo((0l)).equals((new ArrayList<Long>(Arrays.asList())))); assert(countUpTo((22l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l))))); assert(countUpTo((1l)).equals((new ArrayList<Long>(Arrays.asList())))); assert(countUpTo((18l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l))))); assert(countUpTo((47l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l, (long)23l, (long)29l, (long)31l, (long)37l, (long)41l, (long)43l))))); assert(countUpTo((101l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l, (long)23l, (long)29l, (long)31l, (long)37l, (long)41l, (long)43l, (long)47l, (long)53l, (long)59l, (long)61l, (long)67l, (long)71l, (long)73l, (long)79l, (long)83l, (long)89l, (long)97l))))); } }
humaneval-HumanEval_51_remove_vowels.json-L24
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // remove_vowels is a function that takes string and returns string without vowels. // >>> removeVowels(("")) // ("") // >>> removeVowels(("abcdef")) // ("bcdf") // >>> removeVowels(("aaaaa")) // ("") // >>> removeVowels(("aaBAA")) // ("B") // >>> removeVowels(("zbcd")) // ("zbcd") public static String removeVowels(String text) { String vowels = "aeiouAEIOU"; String newText = ""; for (int i = 0; i < text.length(); i++) {
newText += text.charAt(i); } } return newText; } }
if (vowels.indexOf(text.charAt(i)) == -1) {
} public static void main(String[] args) { assert(removeVowels(("")).equals((""))); assert(removeVowels(("abcdef\nghijklm")).equals(("bcdf\nghjklm"))); assert(removeVowels(("fedcba")).equals(("fdcb"))); assert(removeVowels(("eeeee")).equals((""))); assert(removeVowels(("acBAA")).equals(("cB"))); assert(removeVowels(("EcBOO")).equals(("cB"))); assert(removeVowels(("ybcd")).equals(("ybcd"))); } }
humaneval-HumanEval_102_choose_num.json-L16
js
//This function takes two positive numbers x and y and returns the // biggest even integer number that is in the range [x, y] inclusive. If // there's no such number, then the function should return -1. // For example: // >>> choose_num(12, 15) // 14 // >>> choose_num(13, 12) // -1 function choose_num(x, y){ var num = -1; for(var i = x; i <= y; i++){ if(i % 2 == 0){ num = i; } }
}
return num;
const assert = require('node:assert'); function test() { let candidate = choose_num; assert.deepEqual(candidate(12, 15),14); assert.deepEqual(candidate(13, 12),-1); assert.deepEqual(candidate(33, 12354),12354); assert.deepEqual(candidate(5234, 5233),-1); assert.deepEqual(candidate(6, 29),28); assert.deepEqual(candidate(27, 10),-1); assert.deepEqual(candidate(7, 7),-1); assert.deepEqual(candidate(546, 546),546); } test();
humaneval-HumanEval_64_vowels_count.json-L22
py
def vowels_count(s: str) -> int: """Write a function vowels_count which takes a string representing a 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. Example: >>> vowels_count('abcde') 2 >>> vowels_count('ACEDY') 3 """ s = s.lower() i = 0 count = 0 while i < len(s): if (s[i] == 'a' or s[i] == 'e' or s[i] == 'i' or s[i] == 'o' or s[i] == 'u'): count += 1 if (s[i] == 'y' and i == len(s) - 1): count += 1 i += 1
return count
def check(candidate): 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('ACEDY') == 3 def test_check(): check(vowels_count) test_check()
humaneval-HumanEval_64_vowels_count.json-L15
js
//Write a function vowels_count which takes a string representing // a 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. // Example: // >>> vowels_count("abcde") // 2 // >>> vowels_count("ACEDY") // 3 function vowels_count(s){ s=s.toLowerCase(); var vowels = ['a','e','i','o','u']; var count = 0; for(var i=0; i<s.length; i++){
count++; } } if(s[s.length-1]=='y'){ count++; } return count; }
if(vowels.indexOf(s[i])!=-1){
const assert = require('node:assert'); function test() { let candidate = vowels_count; assert.deepEqual(candidate("abcde"),2); assert.deepEqual(candidate("Alone"),3); assert.deepEqual(candidate("key"),2); assert.deepEqual(candidate("bye"),1); assert.deepEqual(candidate("keY"),2); assert.deepEqual(candidate("bYe"),1); assert.deepEqual(candidate("ACEDY"),3); } test();
humaneval-HumanEval_39_prime_fib.json-L26
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // prime_fib returns n-th number that is a Fibonacci number and it's also prime. // >>> primeFib((1l)) // (2l) // >>> primeFib((2l)) // (3l) // >>> primeFib((3l)) // (5l) // >>> primeFib((4l)) // (13l) // >>> primeFib((5l)) // (89l) public static long primeFib(long n) { long fib = 1; long prev = 0; long count = 0; while (count != n) { long tmp = fib;
prev = tmp; if (BigInteger.valueOf(fib).isProbablePrime(1)) { count++; } } return fib; } }
fib += prev;
} public static void main(String[] args) { assert(primeFib((1l)) == (2l)); assert(primeFib((2l)) == (3l)); assert(primeFib((3l)) == (5l)); assert(primeFib((4l)) == (13l)); assert(primeFib((5l)) == (89l)); assert(primeFib((6l)) == (233l)); assert(primeFib((7l)) == (1597l)); assert(primeFib((8l)) == (28657l)); assert(primeFib((9l)) == (514229l)); assert(primeFib((10l)) == (433494437l)); } }
humaneval-HumanEval_127_intersection.json-L27
js
//You are given two intervals, // where each interval is a pair of integers. For example, interval = (start, end) = (1, 2). // The given intervals are closed which means that the interval (start, end) // includes both start and end. // For each given interval, it is assumed that its start is less or equal its end. // Your task is to determine whether the length of intersection of these two // intervals is a prime number. // Example, the intersection of the intervals (1, 3), (2, 4) is (2, 3) // which its length is 1, which not a prime number. // If the length of the intersection is a prime number, return "YES", // otherwise, return "NO". // If the two intervals don't intersect, return "NO". // [input/output] samples: // >>> intersection([1, 2], [2, 3]) // "NO" // >>> intersection([-1, 1], [0, 4]) // "NO" // >>> intersection([-3, -1], [-5, 5]) // "YES" function intersection(interval1, interval2){ let start = Math.max(interval1[0], interval2[0]); let end = Math.min(interval1[1], interval2[1]); if (start >= end) { return "NO"; } let length = end - start;
return primes.includes(length) ? "YES" : "NO"; }
let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
const assert = require('node:assert'); function test() { let candidate = intersection; assert.deepEqual(candidate([1, 2], [2, 3]),"NO"); assert.deepEqual(candidate([-1, 1], [0, 4]),"NO"); assert.deepEqual(candidate([-3, -1], [-5, 5]),"YES"); assert.deepEqual(candidate([-2, 2], [-4, 0]),"YES"); assert.deepEqual(candidate([-11, 2], [-1, -1]),"NO"); assert.deepEqual(candidate([1, 2], [3, 5]),"NO"); assert.deepEqual(candidate([1, 2], [1, 2]),"NO"); assert.deepEqual(candidate([-2, -2], [-3, -2]),"NO"); } test();
humaneval-HumanEval_4_mean_absolute_deviation.json-L18
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // For a given array 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 this case): // MAD = average | x - x_mean | // >>> meanAbsoluteDeviation((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)2.0f, (float)3.0f, (float)4.0f)))) // (1.0f) public static float meanAbsoluteDeviation(ArrayList<Float> numbers) { float mean = (float)numbers.stream().mapToDouble(Float::floatValue).average().getAsDouble();
} }
return (float)numbers.stream().mapToDouble(n -> Math.abs(n - mean)).average().getAsDouble();
} public static void main(String[] args) { assert(meanAbsoluteDeviation((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)2.0f)))) == (0.5f)); assert(meanAbsoluteDeviation((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)2.0f, (float)3.0f, (float)4.0f)))) == (1.0f)); assert(meanAbsoluteDeviation((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)2.0f, (float)3.0f, (float)4.0f, (float)5.0f)))) == (1.2f)); } }