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_69_search.json-L32
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>(); Long max = (long)-1; 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; } }
}
} 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_120_maximum.json-L34
py
from typing import List def maximum(arr: List[int], k: int) -> List[int]: """ Given an array arr of integers and a positive integer k, return a sorted list of length k with the maximum k numbers in arr. Example 1: >>> maximum([-3, -4, 5], 3) [-4, -3, 5] Example 2: >>> maximum([4, -4, 4], 2) [4, 4] Example 3: >>> maximum([-3, 2, 1, 2, -1, -2, 1], 1) [2] Note: 1. The length of the array will be in the range of [1, 1000]. 2. The elements in the array will be in the range of [-1000, 1000]. 3. 0 <= k <= len(arr) """ def get_max(arr: List[int], k: int) -> List[int]: """ Returns the maximum k numbers in the given array """ if k == 0: return []
return [max(arr)] else: max_num = max(arr) arr.remove(max_num) return [max_num] + get_max(arr, k - 1) return sorted(get_max(arr, k))
elif k == 1:
def check(candidate): assert candidate([-3, -4, 5], 3) == [-4, -3, 5] assert candidate([4, -4, 4], 2) == [4, 4] assert candidate([-3, 2, 1, 2, -1, -2, 1], 1) == [2] assert candidate([123, -123, 20, 0, 1, 2, -3], 3) == [2, 20, 123] assert candidate([-123, 20, 0, 1, 2, -3], 4) == [0, 1, 2, 20] assert candidate([5, 15, 0, 3, -13, -8, 0], 7) == [-13, -8, 0, 0, 3, 5, 15] assert candidate([-1, 0, 2, 5, 3, -10], 2) == [3, 5] assert candidate([1, 0, 5, -7], 1) == [5] assert candidate([4, -4], 2) == [-4, 4] assert candidate([-10, 10], 2) == [-10, 10] assert candidate([1, 2, 3, -23, 243, -400, 0], 0) == [] def test_check(): check(maximum) test_check()
humaneval-HumanEval_135_can_arrange.json-L17
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){ res = i; }else{
} } return res; }
max = arr[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_111_histogram.json-L26
py
from typing import Dict def histogram(test: str) -> Dict[str, int]: """Given a string representing a space separated lowercase letters, return a dictionary 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('') { } """ letters = {} most_count = 0 result = {} for letter in test.split(): letters[letter] = letters.get(letter, 0) + 1
most_count = letters[letter] for letter in letters: if letters[letter] == most_count: result[letter] = most_count return result
if letters[letter] >= most_count:
def check(candidate): assert candidate('a b b a') == { 'a': 2, 'b': 2 } assert candidate('a b c a b') == { 'a': 2, 'b': 2 } assert candidate('a b c d g') == { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'g': 1 } assert candidate('r t g') == { 'r': 1, 't': 1, 'g': 1 } assert candidate('b b b b a') == { 'b': 4 } assert candidate('r t g') == { 'r': 1, 't': 1, 'g': 1 } assert candidate('') == { } assert candidate('a') == { 'a': 1 } def test_check(): check(histogram) test_check()
humaneval-HumanEval_114_minSubArraySum.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 { // Given an array array list of integers nums, find the minimum sum of any non-empty sub-array array list // of nums. // Example // >>> minSubArraySum((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)1l, (long)2l, (long)4l)))) // (1l) // >>> minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l)))) // (-6l) public static long minSubArraySum(ArrayList<Long> nums) { long minSum = Long.MAX_VALUE; long prevMinSum = Long.MAX_VALUE; for (int i = 0; i < nums.size(); i++) { long currSum = nums.get(i); if (currSum < prevMinSum) { prevMinSum = currSum; } if (currSum < minSum) { minSum = currSum; } for (int j = i + 1; j < nums.size(); j++) { currSum += nums.get(j); if (currSum < prevMinSum) { prevMinSum = currSum; } if (currSum < minSum) { minSum = currSum; } } } return minSum;
}
}
} public static void main(String[] args) { assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)1l, (long)2l, (long)4l)))) == (1l)); assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l)))) == (-6l)); assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l, (long)2l, (long)-10l)))) == (-14l)); assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-9999999999999999l)))) == (-9999999999999999l)); assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)0l, (long)10l, (long)20l, (long)1000000l)))) == (0l)); assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l, (long)10l, (long)-5l)))) == (-6l)); assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)100l, (long)-1l, (long)-2l, (long)-3l, (long)10l, (long)-5l)))) == (-6l)); assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)10l, (long)11l, (long)13l, (long)8l, (long)3l, (long)4l)))) == (3l)); assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)100l, (long)-33l, (long)32l, (long)-1l, (long)0l, (long)-2l)))) == (-33l)); assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-10l)))) == (-10l)); assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)7l)))) == (7l)); assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)1l, (long)-1l)))) == (-1l)); } }
humaneval-HumanEval_149_sorted_list_sum.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 that accepts an array array list of strings as a parameter, // deletes the strings that have odd lengths from it, // and returns the resulted array list with a sorted order, // The array list is always an array array list of strings and never an array array list of numbers, // and it may contain duplicates. // The order of the array list should be ascending by length of each word, and you // should return the array list sorted by that rule. // If two words have the same length, sort the array list alphabetically. // The function should return an array array list of strings in sorted order. // You may assume that all words will have the same length. // For example: // >>> listSort((new ArrayList<String>(Arrays.asList((String)"aa", (String)"a", (String)"aaa")))) // (new ArrayList<String>(Arrays.asList((String)"aa"))) // >>> listSort((new ArrayList<String>(Arrays.asList((String)"ab", (String)"a", (String)"aaa", (String)"cd")))) // (new ArrayList<String>(Arrays.asList((String)"ab", (String)"cd"))) public static ArrayList<String> sortedListSum(ArrayList<String> lst) { ArrayList<String> result = new ArrayList<String>(); for (String s : lst) {
result.add(s); } } Collections.sort(result, new Comparator<String>() { @Override public int compare(String s1, String s2) { if (s1.length() == s2.length()) { return s1.compareTo(s2); } return s1.length() - s2.length(); } }); return result; } }
if (s.length() % 2 == 0) {
} public static void main(String[] args) { assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"aa", (String)"a", (String)"aaa")))).equals((new ArrayList<String>(Arrays.asList((String)"aa"))))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"school", (String)"AI", (String)"asdf", (String)"b")))).equals((new ArrayList<String>(Arrays.asList((String)"AI", (String)"asdf", (String)"school"))))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"d", (String)"b", (String)"c", (String)"a")))).equals((new ArrayList<String>(Arrays.asList())))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"d", (String)"dcba", (String)"abcd", (String)"a")))).equals((new ArrayList<String>(Arrays.asList((String)"abcd", (String)"dcba"))))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"AI", (String)"ai", (String)"au")))).equals((new ArrayList<String>(Arrays.asList((String)"AI", (String)"ai", (String)"au"))))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"a", (String)"b", (String)"b", (String)"c", (String)"c", (String)"a")))).equals((new ArrayList<String>(Arrays.asList())))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"aaaa", (String)"bbbb", (String)"dd", (String)"cc")))).equals((new ArrayList<String>(Arrays.asList((String)"cc", (String)"dd", (String)"aaaa", (String)"bbbb"))))); } }
humaneval-HumanEval_128_prod_signs.json-L34
py
from typing import List, Optional def prod_signs(arr: List[int]) -> Optional[int]: """ You are given an array arr of integers and you need to return sum of magnitudes of integers multiplied by product of all signs of each number in the array, represented by 1, -1 or 0. Note: return None for empty arr. Example: >>> prod_signs([1, 2, 2, -4]) 9 >>> prod_signs([0, 1]) 0 >>> prod_signs([]) None """ def product(lst: List[int]) -> int: out = 1 for item in lst: out *= item return out if len(arr) == 0: return None else: sign_arr = [] for num in arr: if num > 0: sign_arr.append(1) elif num < 0: sign_arr.append(-1) else:
return sum(map(abs, arr)) * product(sign_arr)
sign_arr.append(0)
def check(candidate): assert candidate([1, 2, 2, -4]) == -9 assert candidate([0, 1]) == 0 assert candidate([1, 1, 1, 2, 3, -1, 1]) == -10 assert candidate([]) == None assert candidate([2, 4, 1, 2, -1, -1, 9]) == 20 assert candidate([-1, 1, -1, 1]) == 4 assert candidate([-1, 1, 1, 1]) == -4 assert candidate([-1, 1, 1, 0]) == 0 def test_check(): check(prod_signs) test_check()
humaneval-HumanEval_160_do_algebra.json-L42
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]);
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; }
operand.splice(count + 1, 1);
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_154_cycpattern_check.json-L20
py
def cycpattern_check(a: str, b: str) -> bool: """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 """ for i in range(len(b)): if a.find(b) != -1: return True
return False
b = b[1:] + b[0]
def check(candidate): assert candidate('xyzw', 'xyw') == False assert candidate('yello', 'ell') == True assert candidate('whattup', 'ptut') == False assert candidate('efef', 'fee') == True assert candidate('abab', 'aabb') == False assert candidate('winemtt', 'tinem') == True def test_check(): check(cycpattern_check) test_check()
humaneval-HumanEval_75_is_multiply_prime.json-L11
js
//Write a function that returns true if the given number is the multiplication of 3 prime numbers // and false otherwise. // Knowing that (a) is less then 100. // Example: // >>> is_multiply_prime(30) // true // 30 = 2 * 3 * 5 function is_multiply_prime(a){ a = Math.abs(a); const prime = [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];
let check = []; while(a != 1){ if(a % prime[i] == 0){ a = a / prime[i]; check.push(prime[i]); }else{ i++; } } if(check.length == 3){ return true; }else{ return false; } }
let i = 0;
const assert = require('node:assert'); function test() { let candidate = is_multiply_prime; assert.deepEqual(candidate(5),false); assert.deepEqual(candidate(30),true); assert.deepEqual(candidate(8),true); assert.deepEqual(candidate(10),false); assert.deepEqual(candidate(125),true); assert.deepEqual(candidate(105),true); assert.deepEqual(candidate(126),false); assert.deepEqual(candidate(729),false); assert.deepEqual(candidate(891),false); assert.deepEqual(candidate(1001),true); } test();
humaneval-HumanEval_27_flip_case.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 string, flip lowercase characters to uppercase and uppercase to lowercase. // >>> flipCase(("Hello")) // ("hELLO") public static String flipCase(String string) { String answer = ""; for (int i = 0; i < string.length(); i++) { if (Character.isLowerCase(string.charAt(i))) { answer += Character.toUpperCase(string.charAt(i)); } else { answer += Character.toLowerCase(string.charAt(i));
} return answer; } }
}
} public static void main(String[] args) { assert(flipCase(("")).equals((""))); assert(flipCase(("Hello!")).equals(("hELLO!"))); assert(flipCase(("These violent delights have violent ends")).equals(("tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS"))); } }
humaneval-HumanEval_137_compare_one.json-L40
js
//Create a function that takes integers, floats, or strings representing // real numbers, and returns the larger variable in its given variable type. // Return undefined 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) // undefined function compare_one(a, b){ if (typeof a === typeof b){ if (a > b) { return a; } else if (a < b) { return b; } else { return undefined; } } else if (typeof a === "string" && typeof b === "number"){ if (a[0] > b){ return a; } else if (a[0] < b){ return b; } else { return undefined; } } else if (typeof a === "number" && typeof b === "string"){ if (a > b[0]){ return a; } else if (a < b[0]){ return b; } else { return undefined; }
}
}
const assert = require('node:assert'); function test() { let candidate = compare_one; assert.deepEqual(candidate(1, 2),2); assert.deepEqual(candidate(1, 2.5),2.5); assert.deepEqual(candidate(2, 3),3); assert.deepEqual(candidate(5, 6),6); assert.deepEqual(candidate(1, "2,3"),"2,3"); assert.deepEqual(candidate("5,1", "6"),"6"); assert.deepEqual(candidate("1", "2"),"2"); assert.deepEqual(candidate("1", 1),undefined); } test();
humaneval-HumanEval_156_int_to_mini_roman.json-L37
js
//Given a positive integer, obtain its roman numeral equivalent as a string, // and return it in lowercase. // Restrictions: 1 <= num <= 1000 // Examples: // >>> int_to_mini_roman(19) // "xix" // >>> int_to_mini_roman(152) // "clii" // >>> int_to_mini_roman(426) // "cdxxvi" function int_to_mini_roman(number){ var roman_conversion = { 1000: "m", 900: "cm", 500: "d", 400: "cd", 100: "c", 90: "xc", 50: "l", 40: "xl", 10: "x", 9: "ix", 5: "v", 4: "iv", 1: "i" }; var output = ""; while (number > 0){ var key_list = Object.keys(roman_conversion).map(Number); key_list.sort(function(a, b){return b-a}); for (var i = 0; i < key_list.length; i++){ if (key_list[i] <= number){ output += roman_conversion[key_list[i]]; number -= key_list[i]; break; }
} return output; }
}
const assert = require('node:assert'); function test() { let candidate = int_to_mini_roman; assert.deepEqual(candidate(19),"xix"); assert.deepEqual(candidate(152),"clii"); assert.deepEqual(candidate(251),"ccli"); assert.deepEqual(candidate(426),"cdxxvi"); assert.deepEqual(candidate(500),"d"); assert.deepEqual(candidate(1),"i"); assert.deepEqual(candidate(4),"iv"); assert.deepEqual(candidate(43),"xliii"); assert.deepEqual(candidate(90),"xc"); assert.deepEqual(candidate(94),"xciv"); assert.deepEqual(candidate(532),"dxxxii"); assert.deepEqual(candidate(900),"cm"); assert.deepEqual(candidate(994),"cmxciv"); assert.deepEqual(candidate(1000),"m"); } test();
humaneval-HumanEval_94_skjkasdkd.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 { // You are given an array array list of integers. // You need to find the largest prime value and return the sum of its digits. // Examples: // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)3l, (long)2l, (long)1l, (long)3l, (long)5l, (long)7l, (long)4l, (long)5l, (long)5l, (long)5l, (long)2l, (long)181l, (long)32l, (long)4l, (long)32l, (long)3l, (long)2l, (long)32l, (long)324l, (long)4l, (long)3l)))) // (10l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)1l, (long)8l, (long)2l, (long)4597l, (long)2l, (long)1l, (long)3l, (long)40l, (long)1l, (long)2l, (long)1l, (long)2l, (long)4l, (long)2l, (long)5l, (long)1l)))) // (25l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)1l, (long)32l, (long)5107l, (long)34l, (long)83278l, (long)109l, (long)163l, (long)23l, (long)2323l, (long)32l, (long)30l, (long)1l, (long)9l, (long)3l)))) // (13l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)724l, (long)32l, (long)71l, (long)99l, (long)32l, (long)6l, (long)0l, (long)5l, (long)91l, (long)83l, (long)0l, (long)5l, (long)6l)))) // (11l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)81l, (long)12l, (long)3l, (long)1l, (long)21l)))) // (3l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)8l, (long)1l, (long)2l, (long)1l, (long)7l)))) // (7l) public static long skjkasdkd(ArrayList<Long> lst) { long largestPrime = 0l; for (Long item : lst) { long number = item; int counter = 0; if (item == 1) { continue;
if (item == 2) { counter = 1; } else { int l = 1; while (l <= (int) number) { if (number % l == 0) { counter++; } l++; } } if (counter == 2) { if (number > largestPrime) { largestPrime = number; } } } if (largestPrime == 0) { return 0l; } else { long output = 0l; while (largestPrime > 0) { output += largestPrime % 10; largestPrime /= 10; } return output; } } }
}
} public static void main(String[] args) { assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)3l, (long)2l, (long)1l, (long)3l, (long)5l, (long)7l, (long)4l, (long)5l, (long)5l, (long)5l, (long)2l, (long)181l, (long)32l, (long)4l, (long)32l, (long)3l, (long)2l, (long)32l, (long)324l, (long)4l, (long)3l)))) == (10l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)1l, (long)8l, (long)2l, (long)4597l, (long)2l, (long)1l, (long)3l, (long)40l, (long)1l, (long)2l, (long)1l, (long)2l, (long)4l, (long)2l, (long)5l, (long)1l)))) == (25l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)1l, (long)32l, (long)5107l, (long)34l, (long)83278l, (long)109l, (long)163l, (long)23l, (long)2323l, (long)32l, (long)30l, (long)1l, (long)9l, (long)3l)))) == (13l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)724l, (long)32l, (long)71l, (long)99l, (long)32l, (long)6l, (long)0l, (long)5l, (long)91l, (long)83l, (long)0l, (long)5l, (long)6l)))) == (11l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)81l, (long)12l, (long)3l, (long)1l, (long)21l)))) == (3l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)8l, (long)1l, (long)2l, (long)1l, (long)7l)))) == (7l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)8191l)))) == (19l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)8191l, (long)123456l, (long)127l, (long)7l)))) == (19l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)127l, (long)97l, (long)8192l)))) == (10l)); } }
humaneval-HumanEval_138_is_equal_to_sum_even.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 { // 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) {
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; } }
if (n < 4) {
} 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_27_flip_case.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 string, flip lowercase characters to uppercase and uppercase to lowercase. // >>> flipCase(("Hello")) // ("hELLO") public static String flipCase(String string) { String answer = ""; for (int i = 0; i < string.length(); i++) { if (Character.isLowerCase(string.charAt(i))) { answer += Character.toUpperCase(string.charAt(i)); } else {
} } return answer; } }
answer += Character.toLowerCase(string.charAt(i));
} public static void main(String[] args) { assert(flipCase(("")).equals((""))); assert(flipCase(("Hello!")).equals(("hELLO!"))); assert(flipCase(("These violent delights have violent ends")).equals(("tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS"))); } }
humaneval-HumanEval_43_pairs_sum_to_zero.json-L16
js
//pairs_sum_to_zero takes an array of integers as an input. // it returns true if there are two distinct elements in the array that // sum to zero, and false otherwise. // >>> pairs_sum_to_zero([1, 3, 5, 0]) // false // >>> pairs_sum_to_zero([1, 3, -2, 1]) // false // >>> pairs_sum_to_zero([1, 2, 3, 7]) // false // >>> pairs_sum_to_zero([2, 4, -5, 3, 5, 7]) // true // >>> pairs_sum_to_zero([1]) // false function pairs_sum_to_zero(l){ var i = 0
var j = i+1 while (j < l.length){ if ((l[i] + l[j]) === 0){ return true } j = j + 1 } i = i + 1 } return false }
while (i < l.length){
const assert = require('node:assert'); function test() { let candidate = pairs_sum_to_zero; assert.deepEqual(candidate([1, 3, 5, 0]),false); assert.deepEqual(candidate([1, 3, -2, 1]),false); assert.deepEqual(candidate([1, 2, 3, 7]),false); assert.deepEqual(candidate([2, 4, -5, 3, 5, 7]),true); assert.deepEqual(candidate([1]),false); assert.deepEqual(candidate([-3, 9, -1, 3, 2, 30]),true); assert.deepEqual(candidate([-3, 9, -1, 3, 2, 31]),true); assert.deepEqual(candidate([-3, 9, -1, 4, 2, 30]),false); assert.deepEqual(candidate([-3, 9, -1, 4, 2, 31]),false); } test();
humaneval-HumanEval_56_correct_bracketing.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 { // brackets is a string of "<" and ">". // return true if every opening bracket has a corresponding closing bracket. // >>> correctBracketing(("<")) // (false) // >>> correctBracketing(("<>")) // (true) // >>> correctBracketing(("<<><>>")) // (true) // >>> correctBracketing(("><<>")) // (false) public static boolean correctBracketing(String brackets) {
for (int i = 0; i < brackets.length(); i++) { char c = brackets.charAt(i); if (c == '<') { open++; } else { open--; } if (open < 0) { return false; } } return open == 0; } }
int open = 0;
} public static void main(String[] args) { assert(correctBracketing(("<>")) == (true)); assert(correctBracketing(("<<><>>")) == (true)); assert(correctBracketing(("<><><<><>><>")) == (true)); assert(correctBracketing(("<><><<<><><>><>><<><><<>>>")) == (true)); assert(correctBracketing(("<<<><>>>>")) == (false)); assert(correctBracketing(("><<>")) == (false)); assert(correctBracketing(("<")) == (false)); assert(correctBracketing(("<<<<")) == (false)); assert(correctBracketing((">")) == (false)); assert(correctBracketing(("<<>")) == (false)); assert(correctBracketing(("<><><<><>><>><<>")) == (false)); assert(correctBracketing(("<><><<><>><>>><>")) == (false)); } }
humaneval-HumanEval_146_specialFilter.json-L43
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 takes an array array list of numbers as input and returns // the number of elements in the array array list that are greater than 10 and both // first and last digits of a number are odd (1, 3, 5, 7, 9). // For example: // >>> specialFilter((new ArrayList<Long>(Arrays.asList((long)15l, (long)-73l, (long)14l, (long)-15l)))) // (1l) // >>> specialFilter((new ArrayList<Long>(Arrays.asList((long)33l, (long)-2l, (long)-3l, (long)45l, (long)21l, (long)109l)))) // (2l) public static long specialFilter(ArrayList<Long> nums) { class helper { public long getFirstDigit(long n) { long[] digits = getDigits(n); return digits[0]; } public long getLastDigit(long n) { long[] digits = getDigits(n); return digits[digits.length - 1]; } public long[] getDigits(long n) { ArrayList<Long> digits = new ArrayList<Long>(); while (n != 0) { digits.add(n % 10); n /= 10; } Collections.reverse(digits); return digits.stream().mapToLong(i -> i).toArray(); } } helper h = new helper(); return nums.stream().filter(x -> x > 10).filter(x -> { long firstDigit = h.getFirstDigit(x); long lastDigit = h.getLastDigit(x); return firstDigit % 2 != 0 && lastDigit % 2 != 0; }).count();
}
}
} public static void main(String[] args) { assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)5l, (long)-2l, (long)1l, (long)-5l)))) == (0l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)15l, (long)-73l, (long)14l, (long)-15l)))) == (1l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)33l, (long)-2l, (long)-3l, (long)45l, (long)21l, (long)109l)))) == (2l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)43l, (long)-12l, (long)93l, (long)125l, (long)121l, (long)109l)))) == (4l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)71l, (long)-2l, (long)-33l, (long)75l, (long)21l, (long)19l)))) == (3l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)1l)))) == (0l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList()))) == (0l)); } }
humaneval-HumanEval_117_select_words.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 { // Given a string s and a natural number n, you have been tasked to implement // a function that returns an array array list of all words from string s that contain exactly // n consonants, in order these words appear in the string s. // If the string s is empty then the function should return an empty array list. // Note: you may assume the input string contains only letters and spaces. // Examples: // >>> selectWords(("Mary had a little lamb"), (4l)) // (new ArrayList<String>(Arrays.asList((String)"little"))) // >>> selectWords(("Mary had a little lamb"), (3l)) // (new ArrayList<String>(Arrays.asList((String)"Mary", (String)"lamb"))) // >>> selectWords(("simple white space"), (2l)) // (new ArrayList<String>(Arrays.asList())) // >>> selectWords(("Hello world"), (4l)) // (new ArrayList<String>(Arrays.asList((String)"world"))) // >>> selectWords(("Uncle sam"), (3l)) // (new ArrayList<String>(Arrays.asList((String)"Uncle"))) public static ArrayList<String> selectWords(String s, long n) { ArrayList<String> ret = new ArrayList<String>(); String[] words = s.split(" "); for (String word : words) { String wordInLowerCase = word.toLowerCase(); long consonants = 0l; for (char c : wordInLowerCase.toCharArray()) { switch (c) { case 'a':
case 'i': case 'o': case 'u': break; default: consonants++; break; } } if (consonants == n) { ret.add(word); } } return ret; } }
case 'e':
} public static void main(String[] args) { assert(selectWords(("Mary had a little lamb"), (4l)).equals((new ArrayList<String>(Arrays.asList((String)"little"))))); assert(selectWords(("Mary had a little lamb"), (3l)).equals((new ArrayList<String>(Arrays.asList((String)"Mary", (String)"lamb"))))); assert(selectWords(("simple white space"), (2l)).equals((new ArrayList<String>(Arrays.asList())))); assert(selectWords(("Hello world"), (4l)).equals((new ArrayList<String>(Arrays.asList((String)"world"))))); assert(selectWords(("Uncle sam"), (3l)).equals((new ArrayList<String>(Arrays.asList((String)"Uncle"))))); assert(selectWords((""), (4l)).equals((new ArrayList<String>(Arrays.asList())))); assert(selectWords(("a b c d e f"), (1l)).equals((new ArrayList<String>(Arrays.asList((String)"b", (String)"c", (String)"d", (String)"f"))))); } }
humaneval-HumanEval_39_prime_fib.json-L49
js
//prime_fib returns n-th number that is a Fibonacci number and it's also prime. // >>> prime_fib(1) // 2 // >>> prime_fib(2) // 3 // >>> prime_fib(3) // 5 // >>> prime_fib(4) // 13 // >>> prime_fib(5) // 89 function prime_fib(n){ function isPrime(number){ if(number <= 3){ return number > 1; } else if(number % 2 == 0 || number % 3 == 0){ return false; } else{ for(var i = 5; i * i <= number; i += 6){ if(number % i == 0 || number % (i + 2) == 0){ return false; } } return true; } } function fib(num){ var a = 1, b = 0, temp; while (num >= 0){ temp = a; a = a + b; b = temp; num--; } return b; } var res = 0; var fibNum = 0; while(res < n){ if(isPrime(fib(fibNum))){ res++; } fibNum++; }
}
return fib(fibNum - 1);
const assert = require('node:assert'); function test() { let candidate = prime_fib; assert.deepEqual(candidate(1),2); assert.deepEqual(candidate(2),3); assert.deepEqual(candidate(3),5); assert.deepEqual(candidate(4),13); assert.deepEqual(candidate(5),89); assert.deepEqual(candidate(6),233); assert.deepEqual(candidate(7),1597); assert.deepEqual(candidate(8),28657); assert.deepEqual(candidate(9),514229); assert.deepEqual(candidate(10),433494437); } test();
humaneval-HumanEval_64_vowels_count.json-L12
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 count = 0; for(var i=0; i<s.length; i++){ if(vowels.indexOf(s[i])!=-1){ count++; } } if(s[s.length-1]=='y'){ count++; } return count; }
var vowels = ['a','e','i','o','u'];
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_33_sort_third.json-L32
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 an array array list l and returns an array array list l' such that // l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal // to the values of the corresponding indicies of l, but sorted. // >>> sortThird((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))) // >>> sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)2l)))) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)5l))) public static ArrayList<Long> sortThird(ArrayList<Long> l) { ArrayList<Long> l_r = new ArrayList<Long>(); ArrayList<Long> l_e = new ArrayList<Long>(); ArrayList<Long> l_s = new ArrayList<Long>(); for (int i = 0; i < l.size(); i++) { if (i % 3 == 0) { l_e.add(l.get(i)); } else { l_r.add(l.get(i)); } } for (int i = 0; i < l_e.size(); i++) { long min = l_e.get(i); for (int j = i; j < l_e.size(); j++) { if (l_e.get(j) < min) { min = l_e.get(j);
l_e.set(i, min); } } } int j = 0; for (int i = 0; i < l.size(); i++) { if (i % 3 == 0) { l_s.add(l_e.get(j)); j++; } else { l_s.add(l_r.get(i - j)); } } return l_s; } }
l_e.set(j, l_e.get(i));
} public static void main(String[] args) { assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)5l))))); assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)8l, (long)3l, (long)4l, (long)6l, (long)9l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)8l, (long)3l, (long)4l, (long)6l, (long)9l, (long)5l))))); assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)9l, (long)4l, (long)8l, (long)3l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)9l, (long)4l, (long)8l, (long)3l, (long)5l))))); assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)2l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)5l, (long)1l))))); } }
humaneval-HumanEval_73_smallest_change.json-L12
js
//Given an array arr of integers, find the minimum number of elements that // need to be changed to make the array palindromic. A palindromic array is an array that // is read the same backwards and forwards. In one change, you can change one element to any other element. // For example: // >>> smallest_change([1, 2, 3, 5, 4, 7, 9, 6]) // 4 // >>> smallest_change([1, 2, 3, 4, 3, 2, 2]) // 1 // >>> smallest_change([1, 2, 3, 2, 1]) // 0 function smallest_change(arr){
for(let i = 0; i < arr.length/2; i++){ if(arr[i] != arr[arr.length - 1 - i]){ changes++; } } return changes; }
let changes = 0;
const assert = require('node:assert'); function test() { let candidate = smallest_change; assert.deepEqual(candidate([1, 2, 3, 5, 4, 7, 9, 6]),4); assert.deepEqual(candidate([1, 2, 3, 4, 3, 2, 2]),1); assert.deepEqual(candidate([1, 4, 2]),1); assert.deepEqual(candidate([1, 4, 4, 2]),1); assert.deepEqual(candidate([1, 2, 3, 2, 1]),0); assert.deepEqual(candidate([3, 1, 1, 3]),0); assert.deepEqual(candidate([1]),0); assert.deepEqual(candidate([0, 1]),1); } test();
humaneval-HumanEval_14_all_prefixes.json-L11
py
from typing import List def all_prefixes(string: str) -> List[str]: """ Return list of all prefixes from shortest to longest of the input string >>> all_prefixes('abc') ['a', 'ab', 'abc'] """ prefixes = [] for i in range(len(string)): prefixes.append(string[:i+1])
return prefixes
def check(candidate): assert candidate('') == [] assert candidate('asdfgh') == ['a', 'as', 'asd', 'asdf', 'asdfg', 'asdfgh'] assert candidate('WWW') == ['W', 'WW', 'WWW'] def test_check(): check(all_prefixes) test_check()
humaneval-HumanEval_8_sum_product.json-L11
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){ if(numbers.length == 0){ return [0, 1]; }
let product = 1; for(let i = 0; i < numbers.length; i++){ sum += numbers[i]; product *= numbers[i]; } return [sum, product]; }
let sum = 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_76_is_simple_power.json-L20
js
//Your task is to write a function that returns true if a number x is a simple // power of n and false in other cases. // x is a simple power of n if n**int=x // For example: // >>> is_simple_power(1, 4) // true // >>> is_simple_power(2, 2) // true // >>> is_simple_power(8, 2) // true // >>> is_simple_power(3, 2) // false // >>> is_simple_power(3, 1) // false // >>> is_simple_power(5, 3) // false function is_simple_power(x, n){ if(x===1){ return true;
if(x < n){ return false; } if(x % n !== 0){ return false; } return is_simple_power(x/n, n); }
}
const assert = require('node:assert'); function test() { let candidate = is_simple_power; assert.deepEqual(candidate(16, 2),true); assert.deepEqual(candidate(143214, 16),false); assert.deepEqual(candidate(4, 2),true); assert.deepEqual(candidate(9, 3),true); assert.deepEqual(candidate(16, 4),true); assert.deepEqual(candidate(24, 2),false); assert.deepEqual(candidate(128, 4),false); assert.deepEqual(candidate(12, 6),false); assert.deepEqual(candidate(1, 1),true); assert.deepEqual(candidate(1, 12),true); } test();
humaneval-HumanEval_119_match_parens.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 { // You are given an array array list 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: // >>> matchParens((new ArrayList<String>(Arrays.asList((String)"()(", (String)")")))) // ("Yes") // >>> matchParens((new ArrayList<String>(Arrays.asList((String)")", (String)")")))) // ("No") public static String matchParens(ArrayList<String> lst) { String s1 = lst.get(0); String s2 = lst.get(1); String s3 = s1+s2; String s4 = s2+s1; int c1 = 0; int c2 = 0; for (int i = 0; i < s3.length(); i++) { if (s3.charAt(i) == '(') c1 += 1; else c1 -= 1; if (c1 < 0) break; } for (int i = 0; i < s4.length(); i++) { if (s4.charAt(i) == '(') c2 += 1; else c2 -= 1;
} if (c1 == 0 || c2 == 0) return "Yes"; else return "No"; } }
if (c2 < 0) break;
} public static void main(String[] args) { assert(matchParens((new ArrayList<String>(Arrays.asList((String)"()(", (String)")")))).equals(("Yes"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)")", (String)")")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(()(())", (String)"())())")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)")())", (String)"(()()(")))).equals(("Yes"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(())))", (String)"(()())((")))).equals(("Yes"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"()", (String)"())")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(()(", (String)"()))()")))).equals(("Yes"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"((((", (String)"((())")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)")(()", (String)"(()(")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)")(", (String)")(")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(", (String)")")))).equals(("Yes"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)")", (String)"(")))).equals(("Yes"))); } }
humaneval-HumanEval_49_modp.json-L18
py
def modp(n: int, p: int) -> int: """Return 2^n modulo p (be aware of numerics). >>> modp(3, 5) 3 >>> modp(1101, 101) 2 >>> modp(0, 101) 1 >>> modp(3, 11) 8 >>> modp(100, 101) 1 """ if p <= 0: raise ValueError("modp: invalid p") if n < 0: raise ValueError("modp: invalid n")
return 1 if n == 0 else 2**(n % (p-1)) % p
def check(candidate): assert candidate(3, 5) == 3 assert candidate(1101, 101) == 2 assert candidate(0, 101) == 1 assert candidate(3, 11) == 8 assert candidate(100, 101) == 1 assert candidate(30, 5) == 4 assert candidate(31, 5) == 3 def test_check(): check(modp) test_check()
humaneval-HumanEval_33_sort_third.json-L20
js
//This function takes an array l and returns an array l' such that // l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal // to the values of the corresponding indicies of l, but sorted. // >>> sort_third([1, 2, 3]) // [1, 2, 3] // >>> sort_third([5, 6, 3, 4, 8, 9, 2]) // [2, 6, 3, 4, 8, 9, 5] function sort_third(l){ let x=[]; let y=[]; let k=0; for(let i=0;i<l.length;i++){ if(i%3==0){ y[k]=l[i]; k=k+1; } else{ x[i]=l[i]; }
y.sort(); k=0; for(let i=0;i<l.length;i++){ if(i%3==0){ x[i]=y[k]; k=k+1; } } return x; }
}
const assert = require('node:assert'); function test() { let candidate = sort_third; assert.deepEqual(candidate([5, 6, 3, 4, 8, 9, 2]),[2, 6, 3, 4, 8, 9, 5]); assert.deepEqual(candidate([5, 8, 3, 4, 6, 9, 2]),[2, 8, 3, 4, 6, 9, 5]); assert.deepEqual(candidate([5, 6, 9, 4, 8, 3, 2]),[2, 6, 9, 4, 8, 3, 5]); assert.deepEqual(candidate([5, 6, 3, 4, 8, 9, 2, 1]),[2, 6, 3, 4, 8, 9, 5, 1]); } test();
humaneval-HumanEval_160_do_algebra.json-L65
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_107_even_odd_palindrome.json-L35
js
//Given a positive integer n, return an array that has the number of even and odd // integer palindromes that fall within the range(1, n), inclusive. // Example 1: // >>> even_odd_palindrome(3) // [1, 2] // Explanation: // Integer palindrome are 1, 2, 3. one of them is even, and two of them are odd. // Example 2: // >>> even_odd_palindrome(12) // [4, 6] // Explanation: // Integer palindrome are 1, 2, 3, 4, 5, 6, 7, 8, 9, 11. four of them are even, and 6 of them are odd. // Note: // 1. 1 <= n <= 10^3 // 2. returned array has the number of even and odd integer palindromes respectively. function even_odd_palindrome(n){ var pal = []; var isPalindrome = function(x){ var y = x.toString().split('').reverse().join(''); return y == x; } for(var i = 1; i <= n; i++){ if(isPalindrome(i)){ pal.push(i); } } var odd = 0; var even = 0; for(var i = 0; i < pal.length; i++){ if(pal[i] % 2 == 0){ even++; } else{ odd++;
} return [even, odd]; }
}
const assert = require('node:assert'); function test() { let candidate = even_odd_palindrome; assert.deepEqual(candidate(123),[8, 13]); assert.deepEqual(candidate(12),[4, 6]); assert.deepEqual(candidate(3),[1, 2]); assert.deepEqual(candidate(63),[6, 8]); assert.deepEqual(candidate(25),[5, 6]); assert.deepEqual(candidate(19),[4, 6]); assert.deepEqual(candidate(9),[4, 5]); assert.deepEqual(candidate(1),[0, 1]); } test();
humaneval-HumanEval_161_solve.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 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) {
StringBuilder sb = new StringBuilder(); if (s.length() == 0) return s; for (int i = 0; i < s.length(); i++) { if (Character.isLetter(s.charAt(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(); } }
boolean letterNotFound = true;
} 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_156_int_to_mini_roman.json-L57
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, obtain its roman numeral equivalent as a string, // and return it in lowercase. // Restrictions: 1 <= num <= 1000 // Examples: // >>> intToMiniRoman((19l)) // ("xix") // >>> intToMiniRoman((152l)) // ("clii") // >>> intToMiniRoman((426l)) // ("cdxxvi") public static String intToMiniRoman(long number) { int num = (int) number; String res = ""; if (num == 0) return ""; while (num >= 1000) { res += "m"; num -= 1000; } if (num >= 900) { res += "cm"; num -= 900; } if (num >= 500) { res += "d"; num -= 500; } if (num >= 400) { res += "cd"; num -= 400; } while (num >= 100) { res += "c"; num -= 100; } if (num >= 90) { res += "xc"; num -= 90; } if (num >= 50) { res += "l"; num -= 50; } if (num >= 40) { res += "xl"; num -= 40; } while (num >= 10) {
num -= 10; } if (num >= 9) { res += "ix"; num -= 9; } if (num >= 5) { res += "v"; num -= 5; } if (num >= 4) { res += "iv"; num -= 4; } while (num >= 1) { res += "i"; num -= 1; } return res; } }
res += "x";
} public static void main(String[] args) { assert(intToMiniRoman((19l)).equals(("xix"))); assert(intToMiniRoman((152l)).equals(("clii"))); assert(intToMiniRoman((251l)).equals(("ccli"))); assert(intToMiniRoman((426l)).equals(("cdxxvi"))); assert(intToMiniRoman((500l)).equals(("d"))); assert(intToMiniRoman((1l)).equals(("i"))); assert(intToMiniRoman((4l)).equals(("iv"))); assert(intToMiniRoman((43l)).equals(("xliii"))); assert(intToMiniRoman((90l)).equals(("xc"))); assert(intToMiniRoman((94l)).equals(("xciv"))); assert(intToMiniRoman((532l)).equals(("dxxxii"))); assert(intToMiniRoman((900l)).equals(("cm"))); assert(intToMiniRoman((994l)).equals(("cmxciv"))); assert(intToMiniRoman((1000l)).equals(("m"))); } }
humaneval-HumanEval_146_specialFilter.json-L23
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 takes an array array list of numbers as input and returns // the number of elements in the array array list that are greater than 10 and both // first and last digits of a number are odd (1, 3, 5, 7, 9). // For example: // >>> specialFilter((new ArrayList<Long>(Arrays.asList((long)15l, (long)-73l, (long)14l, (long)-15l)))) // (1l) // >>> specialFilter((new ArrayList<Long>(Arrays.asList((long)33l, (long)-2l, (long)-3l, (long)45l, (long)21l, (long)109l)))) // (2l) public static long specialFilter(ArrayList<Long> nums) { class helper { public long getFirstDigit(long n) { long[] digits = getDigits(n); return digits[0]; }
long[] digits = getDigits(n); return digits[digits.length - 1]; } public long[] getDigits(long n) { ArrayList<Long> digits = new ArrayList<Long>(); while (n != 0) { digits.add(n % 10); n /= 10; } Collections.reverse(digits); return digits.stream().mapToLong(i -> i).toArray(); } } helper h = new helper(); return nums.stream().filter(x -> x > 10).filter(x -> { long firstDigit = h.getFirstDigit(x); long lastDigit = h.getLastDigit(x); return firstDigit % 2 != 0 && lastDigit % 2 != 0; }).count(); } }
public long getLastDigit(long n) {
} public static void main(String[] args) { assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)5l, (long)-2l, (long)1l, (long)-5l)))) == (0l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)15l, (long)-73l, (long)14l, (long)-15l)))) == (1l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)33l, (long)-2l, (long)-3l, (long)45l, (long)21l, (long)109l)))) == (2l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)43l, (long)-12l, (long)93l, (long)125l, (long)121l, (long)109l)))) == (4l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)71l, (long)-2l, (long)-33l, (long)75l, (long)21l, (long)19l)))) == (3l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)1l)))) == (0l)); assert(specialFilter((new ArrayList<Long>(Arrays.asList()))) == (0l)); } }
humaneval-HumanEval_150_x_or_y.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 { // 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: // >>> xOrY((7l), (34l), (12l)) // (34l) // >>> xOrY((15l), (8l), (5l)) // (5l) public static long xOrY(long n, long x, long y) { BigInteger nBig = BigInteger.valueOf(n);
return x; } else { return y; } } }
if (nBig.isProbablePrime(100)) {
} public static void main(String[] args) { assert(xOrY((7l), (34l), (12l)) == (34l)); assert(xOrY((15l), (8l), (5l)) == (5l)); assert(xOrY((3l), (33l), (5212l)) == (33l)); assert(xOrY((1259l), (3l), (52l)) == (3l)); assert(xOrY((7919l), (-1l), (12l)) == (-1l)); assert(xOrY((3609l), (1245l), (583l)) == (583l)); assert(xOrY((91l), (56l), (129l)) == (129l)); assert(xOrY((6l), (34l), (1234l)) == (1234l)); assert(xOrY((1l), (2l), (0l)) == (0l)); assert(xOrY((2l), (2l), (0l)) == (2l)); } }
humaneval-HumanEval_58_common.json-L17
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 sorted unique common elements for two array lists. // >>> common((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)3l, (long)34l, (long)653l, (long)2l, (long)5l))), (new ArrayList<Long>(Arrays.asList((long)5l, (long)7l, (long)1l, (long)5l, (long)9l, (long)653l, (long)121l)))) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)5l, (long)653l))) // >>> common((new ArrayList<Long>(Arrays.asList((long)5l, (long)3l, (long)2l, (long)8l))), (new ArrayList<Long>(Arrays.asList((long)3l, (long)2l)))) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l))) public static ArrayList<Long> common(ArrayList<Long> l1, ArrayList<Long> l2) { return new ArrayList<Long>( l1.stream()
.collect(Collectors.toSet()) ); } }
.filter(l2::contains)
} public static void main(String[] args) { assert(common((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)3l, (long)34l, (long)653l, (long)2l, (long)5l))), (new ArrayList<Long>(Arrays.asList((long)5l, (long)7l, (long)1l, (long)5l, (long)9l, (long)653l, (long)121l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)5l, (long)653l))))); assert(common((new ArrayList<Long>(Arrays.asList((long)5l, (long)3l, (long)2l, (long)8l))), (new ArrayList<Long>(Arrays.asList((long)3l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l))))); assert(common((new ArrayList<Long>(Arrays.asList((long)4l, (long)3l, (long)2l, (long)8l))), (new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l))))); assert(common((new ArrayList<Long>(Arrays.asList((long)4l, (long)3l, (long)2l, (long)8l))), (new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<Long>(Arrays.asList())))); } }
humaneval-HumanEval_20_find_closest_elements.json-L22
py
from typing import List, Tuple def find_closest_elements(numbers: List[float]) -> Tuple[float, float]: """ From a supplied list of numbers (of length at least two) select and return 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) """ temp, temp2 = abs(numbers[0] - numbers[1]), abs(numbers[0] - numbers[1]) temp_index, temp2_index = 0, 1 if numbers[0] > numbers[1]: temp, temp2 = temp2, temp temp_index, temp2_index = temp2_index, temp_index for i in range(len(numbers)): for j in range(i+1, len(numbers)): if abs(numbers[i] - numbers[j]) < temp: temp = abs(numbers[i] - numbers[j]) temp_index = i temp2_index = j
if numbers[i] > numbers[j]: temp = abs(numbers[i] - numbers[j]) temp_index = i temp2_index = j if numbers[temp_index] > numbers[temp2_index]: temp, temp2 = numbers[temp2_index], numbers[temp_index] else: temp, temp2 = numbers[temp_index], numbers[temp2_index] return temp, temp2 print(find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.2]))
elif abs(numbers[i] - numbers[j]) == temp:
def check(candidate): assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2]) == (3.9, 4.0) assert candidate([1.0, 2.0, 5.9, 4.0, 5.0]) == (5.0, 5.9) assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.2]) == (2.0, 2.2) assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0]) == (2.0, 2.0) assert candidate([1.1, 2.2, 3.1, 4.1, 5.1]) == (2.2, 3.1) def test_check(): check(find_closest_elements) test_check()
humaneval-HumanEval_160_do_algebra.json-L21
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 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; }
let count = 0;
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_86_anti_shuffle.json-L21
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(" "); strings.forEach(function(str){ var temp = str.split(""); temp.sort(); result.push(temp.join("")); });
}
return result.join(" ");
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_61_correct_bracketing.json-L16
js
//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 function correct_bracketing(brackets){ var opens = []; for (var i = 0; i < brackets.length; i++){ if (brackets[i] == "("){ opens.push(brackets[i]);
else if (brackets[i] == ")"){ if (opens.length > 0){ opens.pop(); } else { return false; } } } if (opens.length == 0){ return true; } else { return false; } }
}
const assert = require('node:assert'); function test() { let candidate = correct_bracketing; assert.deepEqual(candidate("()"),true); assert.deepEqual(candidate("(()())"),true); assert.deepEqual(candidate("()()(()())()"),true); assert.deepEqual(candidate("()()((()()())())(()()(()))"),true); assert.deepEqual(candidate("((()())))"),false); assert.deepEqual(candidate(")(()"),false); assert.deepEqual(candidate("("),false); assert.deepEqual(candidate("(((("),false); assert.deepEqual(candidate(")"),false); assert.deepEqual(candidate("(()"),false); assert.deepEqual(candidate("()()(()())())(()"),false); assert.deepEqual(candidate("()()(()())()))()"),false); } test();
humaneval-HumanEval_39_prime_fib.json-L19
js
//prime_fib returns n-th number that is a Fibonacci number and it's also prime. // >>> prime_fib(1) // 2 // >>> prime_fib(2) // 3 // >>> prime_fib(3) // 5 // >>> prime_fib(4) // 13 // >>> prime_fib(5) // 89 function prime_fib(n){ function isPrime(number){ if(number <= 3){ return number > 1; } else if(number % 2 == 0 || number % 3 == 0){ return false;
else{ for(var i = 5; i * i <= number; i += 6){ if(number % i == 0 || number % (i + 2) == 0){ return false; } } return true; } } function fib(num){ var a = 1, b = 0, temp; while (num >= 0){ temp = a; a = a + b; b = temp; num--; } return b; } var res = 0; var fibNum = 0; while(res < n){ if(isPrime(fib(fibNum))){ res++; } fibNum++; } return fib(fibNum - 1); }
}
const assert = require('node:assert'); function test() { let candidate = prime_fib; assert.deepEqual(candidate(1),2); assert.deepEqual(candidate(2),3); assert.deepEqual(candidate(3),5); assert.deepEqual(candidate(4),13); assert.deepEqual(candidate(5),89); assert.deepEqual(candidate(6),233); assert.deepEqual(candidate(7),1597); assert.deepEqual(candidate(8),28657); assert.deepEqual(candidate(9),514229); assert.deepEqual(candidate(10),433494437); } test();
humaneval-HumanEval_68_pluck.json-L41
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 [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_125_split_words.json-L23
js
//Given a string of words, return an array of words split on whitespace, if no whitespaces exists in the text you // should split on commas ',' if no commas exists you should return the number of lower-case letters with odd order in the // alphabet, ord('a') = 0, ord('b') = 1, ... ord('z') = 25 // Examples // >>> split_words("Hello world!") // ["Hello", "world!"] // >>> split_words("Hello,world!") // ["Hello", "world!"] // >>> split_words("abcdef") // 3 function split_words(txt){ if(txt.includes(' ')){ return txt.split(' '); } else if(txt.includes(',')){ return txt.split(','); } else{ let count = 0; for(let i = 0; i < txt.length; i++){ if(i % 2 != 0){ if(txt[i] == txt[i].toLowerCase()){
} } } return count; } }
count++;
const assert = require('node:assert'); function test() { let candidate = split_words; assert.deepEqual(candidate("Hello world!"),["Hello", "world!"]); assert.deepEqual(candidate("Hello,world!"),["Hello", "world!"]); assert.deepEqual(candidate("Hello world,!"),["Hello", "world,!"]); assert.deepEqual(candidate("Hello,Hello,world !"),["Hello,Hello,world", "!"]); assert.deepEqual(candidate("abcdef"),3); assert.deepEqual(candidate("aaabb"),2); assert.deepEqual(candidate("aaaBb"),1); assert.deepEqual(candidate(""),0); } test();
humaneval-HumanEval_149_sorted_list_sum.json-L41
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 as a parameter, // deletes the strings that have odd lengths from it, // and returns the resulted array list with a sorted order, // The array list is always an array array list of strings and never an array array list of numbers, // and it may contain duplicates. // The order of the array list should be ascending by length of each word, and you // should return the array list sorted by that rule. // If two words have the same length, sort the array list alphabetically. // The function should return an array array list of strings in sorted order. // You may assume that all words will have the same length. // For example: // >>> listSort((new ArrayList<String>(Arrays.asList((String)"aa", (String)"a", (String)"aaa")))) // (new ArrayList<String>(Arrays.asList((String)"aa"))) // >>> listSort((new ArrayList<String>(Arrays.asList((String)"ab", (String)"a", (String)"aaa", (String)"cd")))) // (new ArrayList<String>(Arrays.asList((String)"ab", (String)"cd"))) public static ArrayList<String> sortedListSum(ArrayList<String> lst) { ArrayList<String> result = new ArrayList<String>(); for (String s : lst) { if (s.length() % 2 == 0) { result.add(s); } } Collections.sort(result, new Comparator<String>() { @Override public int compare(String s1, String s2) { if (s1.length() == s2.length()) { return s1.compareTo(s2); } return s1.length() - s2.length(); } }); return result;
}
}
} public static void main(String[] args) { assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"aa", (String)"a", (String)"aaa")))).equals((new ArrayList<String>(Arrays.asList((String)"aa"))))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"school", (String)"AI", (String)"asdf", (String)"b")))).equals((new ArrayList<String>(Arrays.asList((String)"AI", (String)"asdf", (String)"school"))))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"d", (String)"b", (String)"c", (String)"a")))).equals((new ArrayList<String>(Arrays.asList())))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"d", (String)"dcba", (String)"abcd", (String)"a")))).equals((new ArrayList<String>(Arrays.asList((String)"abcd", (String)"dcba"))))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"AI", (String)"ai", (String)"au")))).equals((new ArrayList<String>(Arrays.asList((String)"AI", (String)"ai", (String)"au"))))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"a", (String)"b", (String)"b", (String)"c", (String)"c", (String)"a")))).equals((new ArrayList<String>(Arrays.asList())))); assert(sortedListSum((new ArrayList<String>(Arrays.asList((String)"aaaa", (String)"bbbb", (String)"dd", (String)"cc")))).equals((new ArrayList<String>(Arrays.asList((String)"cc", (String)"dd", (String)"aaaa", (String)"bbbb"))))); } }
humaneval-HumanEval_160_do_algebra.json-L26
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]){
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; }
case '*':
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_48_is_palindrome.json-L12
js
//Checks if given string is a palindrome // >>> is_palindrome("") // true // >>> is_palindrome("aba") // true // >>> is_palindrome("aaaaa") // true // >>> is_palindrome("zbcd") // false function is_palindrome(text){ return text == text.split("").reverse().join("");
}
const assert = require('node:assert'); function test() { let candidate = is_palindrome; assert.deepEqual(candidate(""),true); assert.deepEqual(candidate("aba"),true); assert.deepEqual(candidate("aaaaa"),true); assert.deepEqual(candidate("zbcd"),false); assert.deepEqual(candidate("xywyx"),true); assert.deepEqual(candidate("xywyz"),false); assert.deepEqual(candidate("xywzx"),false); } test();
humaneval-HumanEval_73_smallest_change.json-L23
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 arr of integers, find the minimum number of elements that // need to be changed to make the array array list palindromic. A palindromic array array list is an array array list that // is read the same backwards and forwards. In one change, you can change one element to any other element. // For example: // >>> smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)5l, (long)4l, (long)7l, (long)9l, (long)6l)))) // (4l) // >>> smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)3l, (long)2l, (long)2l)))) // (1l) // >>> smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)2l, (long)1l)))) // (0l) public static long smallestChange(ArrayList<Long> arr) { int first = 0; int last = arr.size() - 1; int count = 0;
if (arr.get(first) != arr.get(last)) { count++; } first++; last--; } return count; } }
while (first < last) {
} public static void main(String[] args) { assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)5l, (long)4l, (long)7l, (long)9l, (long)6l)))) == (4l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)3l, (long)2l, (long)2l)))) == (1l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)2l)))) == (1l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)4l, (long)2l)))) == (1l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)2l, (long)1l)))) == (0l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)3l, (long)1l, (long)1l, (long)3l)))) == (0l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l)))) == (0l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)0l, (long)1l)))) == (1l)); } }
humaneval-HumanEval_24_largest_divisor.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 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_160_do_algebra.json-L56
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]){
answer += operand[0]; break; case '-': answer -= operand[0]; break; } operand.splice(0, 1); operator.splice(0, 1); } return answer; }
case '+':
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_64_vowels_count.json-L23
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++) {
sum++; } if (sUpper.charAt(i) == 'Y' && i == lastIndex) { sum++; } } return sum; } }
if (sUpper.charAt(i) == 'A' || sUpper.charAt(i) == 'E' || sUpper.charAt(i) == 'I' || sUpper.charAt(i) == 'O' || sUpper.charAt(i) == 'U') {
} 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_90_next_smallest.json-L19
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){
second_smallest=lst[i]; } } if(second_smallest==Number.MAX_VALUE){ return undefined; } return second_smallest; }
if(lst[i]>smallest&&lst[i]<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_131_digits.json-L31
js
//Given a positive integer n, return the product of the odd digits. // Return 0 if all digits are even. // For example: // >>> digits(1) // 1 // >>> digits(4) // 0 // >>> digits(235) // 15 function digits(n){ if(!n){ return "error"; } if(n < 0){ return "error"; } var temp = 0; var prod = 1; var odd = true; for(n; n > 0; n = Math.floor(n/10)){ temp = n % 10; if(temp % 2 != 0){ prod *= temp; odd = false; } } if(odd){ return 0; } return prod;
}
const assert = require('node:assert'); function test() { let candidate = digits; assert.deepEqual(candidate(5),5); assert.deepEqual(candidate(54),5); assert.deepEqual(candidate(120),1); assert.deepEqual(candidate(5014),5); assert.deepEqual(candidate(98765),315); assert.deepEqual(candidate(5576543),2625); assert.deepEqual(candidate(2468),0); } test();
humaneval-HumanEval_111_histogram.json-L27
py
from typing import Dict def histogram(test: str) -> Dict[str, int]: """Given a string representing a space separated lowercase letters, return a dictionary 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('') { } """ letters = {} most_count = 0 result = {} for letter in test.split(): letters[letter] = letters.get(letter, 0) + 1 if letters[letter] >= most_count:
for letter in letters: if letters[letter] == most_count: result[letter] = most_count return result
most_count = letters[letter]
def check(candidate): assert candidate('a b b a') == { 'a': 2, 'b': 2 } assert candidate('a b c a b') == { 'a': 2, 'b': 2 } assert candidate('a b c d g') == { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'g': 1 } assert candidate('r t g') == { 'r': 1, 't': 1, 'g': 1 } assert candidate('b b b b a') == { 'b': 4 } assert candidate('r t g') == { 'r': 1, 't': 1, 'g': 1 } assert candidate('') == { } assert candidate('a') == { 'a': 1 } def test_check(): check(histogram) test_check()
humaneval-HumanEval_161_solve.json-L39
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++) { if (Character.isLetter(s.charAt(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(); } }
}
} 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_26_remove_duplicates.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 { // From an array array list of integers, remove all elements that occur more than once. // Keep order of elements left the same as in the input. // >>> removeDuplicates((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)2l, (long)4l)))) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)4l))) public static ArrayList<Long> removeDuplicates(ArrayList<Long> numbers) { HashMap<Long, Long> hashmap = new HashMap<Long, Long>(); for (int i = 0; i < numbers.size(); i++) { Long key = numbers.get(i); if (hashmap.containsKey(key)) { hashmap.put(key, hashmap.get(key) + 1); } else { hashmap.put(key, 1L); } } ArrayList<Long> out = new ArrayList<Long>();
Long key = numbers.get(i); if (hashmap.get(key) == 1) { out.add(key); } } return out; } }
for (int i = 0; i < numbers.size(); i++) {
} public static void main(String[] args) { assert(removeDuplicates((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<Long>(Arrays.asList())))); assert(removeDuplicates((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l))))); assert(removeDuplicates((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)2l, (long)4l, (long)3l, (long)5l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)5l))))); } }
humaneval-HumanEval_158_find_max.json-L36
js
//Write a function that accepts an array of strings. // The array 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. // >>> find_max(["name", "of", "string"]) // "string" // >>> find_max(["name", "enam", "game"]) // "enam" // >>> find_max(["aaaaaaa", "bb", "cc"]) // "aaaaaaa" function find_max(words){ var max_unique = {}; var max_unique_word = ""; for(var i = 0; i < words.length; i++){ var unique_word = {}; var word = words[i]; for(var j = 0; j < word.length; j++){ var char = word[j]; if(unique_word[char] === undefined){ unique_word[char] = 1; } } if(max_unique_word === ""){ max_unique_word = word; max_unique = unique_word; } else if(Object.keys(unique_word).length > Object.keys(max_unique).length){ max_unique_word = word; max_unique = unique_word; } else if(Object.keys(unique_word).length === Object.keys(max_unique).length){ if(word < max_unique_word){ max_unique_word = word; max_unique = unique_word; }
} return max_unique_word; }
}
const assert = require('node:assert'); function test() { let candidate = find_max; assert.deepEqual(candidate(["name", "of", "string"]),"string"); assert.deepEqual(candidate(["name", "enam", "game"]),"enam"); assert.deepEqual(candidate(["aaaaaaa", "bb", "cc"]),"aaaaaaa"); assert.deepEqual(candidate(["abc", "cba"]),"abc"); assert.deepEqual(candidate(["play", "this", "game", "of", "footbott"]),"footbott"); assert.deepEqual(candidate(["we", "are", "gonna", "rock"]),"gonna"); assert.deepEqual(candidate(["we", "are", "a", "mad", "nation"]),"nation"); assert.deepEqual(candidate(["this", "is", "a", "prrk"]),"this"); assert.deepEqual(candidate(["b"]),"b"); assert.deepEqual(candidate(["play", "play", "play"]),"play"); } test();
humaneval-HumanEval_11_string_xor.json-L26
js
//Input are two strings a and b consisting only of 1s and 0s. // Perform binary XOR on these inputs and return result also as a string. // >>> string_xor("010", "110") // "100" function string_xor(a, b){ var arrayA = []; var arrayB = []; var arrayXOR = []; var result = ''; for (var i = 0; i < a.length; i++) { arrayA.push(parseInt(a[i])); } for (var j = 0; j < b.length; j++) { arrayB.push(parseInt(b[j])); } for (var k = 0; k < arrayA.length; k++) { if (arrayA[k] !== arrayB[k]) { arrayXOR.push(1); } else { arrayXOR.push(0); } } result = arrayXOR.join(''); return result;
}
const assert = require('node:assert'); function test() { let candidate = string_xor; assert.deepEqual(candidate("111000", "101010"),"010010"); assert.deepEqual(candidate("1", "1"),"0"); assert.deepEqual(candidate("0101", "0000"),"0101"); } test();
humaneval-HumanEval_4_mean_absolute_deviation.json-L14
py
from typing import List def mean_absolute_deviation(numbers: List[float]) -> float: """ For a given list of input numbers, calculate Mean Absolute Deviation around the mean of this dataset. Mean Absolute Deviation is the average absolute difference between each element and a centerpoint (mean in this case): MAD = average | x - x_mean | >>> mean_absolute_deviation([1.0, 2.0, 3.0, 4.0]) 1.0 """ mean = sum(numbers) / len(numbers) absolute_deviations = [abs(x - mean) for x in numbers]
print(mean_absolute_deviation([1.0, 2.0, 3.0, 4.0]))
return sum(absolute_deviations) / len(absolute_deviations)
def check(candidate): assert candidate([1.0, 2.0]) == 0.5 assert candidate([1.0, 2.0, 3.0, 4.0]) == 1.0 assert candidate([1.0, 2.0, 3.0, 4.0, 5.0]) == 1.2 def test_check(): check(mean_absolute_deviation) test_check()
humaneval-HumanEval_158_find_max.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 { // 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) {
for (String word : words) { 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); } }
Map<String, Integer> wordsToUniqueCharacters = new HashMap<String, Integer>();
} 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_29_filter_by_prefix.json-L10
js
//Filter an input array of strings only for ones that start with a given prefix. // >>> filter_by_prefix([], "a") // [] // >>> filter_by_prefix(["abc", "bcd", "cde", "array"], "a") // ["abc", "array"] function filter_by_prefix(strings, prefix){ return strings.filter( function(str){ return str.startsWith(prefix); });
}
const assert = require('node:assert'); function test() { let candidate = filter_by_prefix; assert.deepEqual(candidate([], "john"),[]); assert.deepEqual(candidate(["xxx", "asd", "xxy", "john doe", "xxxAAA", "xxx"], "xxx"),["xxx", "xxxAAA", "xxx"]); } test();
humaneval-HumanEval_148_bf.json-L47
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 { // 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 a pair 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 pair if planet1 or planet2 // are not correct planet names. // Examples // >>> bf(("Jupiter"), ("Neptune")) // (new ArrayList<String>(Arrays.asList((String)"Saturn", (String)"Uranus"))) // >>> bf(("Earth"), ("Mercury")) // (ArrayList<String>("Venus")) // >>> bf(("Mercury"), ("Uranus")) // (new ArrayList<String>(Arrays.asList((String)"Venus", (String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn"))) public static ArrayList<String> bf(String planet1, String planet2) { ArrayList<String> planets = new ArrayList<String>(Arrays.asList((String)"Mercury", (String)"Venus", (String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn", (String)"Uranus", (String)"Neptune")); int indexPlanet1 = planets.indexOf(planet1); int indexPlanet2 = planets.indexOf(planet2); int indexMin = 0; int indexMax = 0; if (indexPlanet1 >= 0 && indexPlanet2 >= 0) { if (indexPlanet1 < indexPlanet2) { indexMin = indexPlanet1; indexMax = indexPlanet2; } else { indexMin = indexPlanet2; indexMax = indexPlanet1; } ArrayList<String> planetsInBetween = new ArrayList<String>(); for (int i = indexMin + 1; i < indexMax; i++) { planetsInBetween.add(planets.get(i)); } return planetsInBetween; } else { return new ArrayList<String>(); }
}
}
} public static void main(String[] args) { assert(bf(("Jupiter"), ("Neptune")).equals((new ArrayList<String>(Arrays.asList((String)"Saturn", (String)"Uranus"))))); assert(bf(("Earth"), ("Mercury")).equals((new ArrayList<String>(Arrays.asList((String)"Venus"))))); assert(bf(("Mercury"), ("Uranus")).equals((new ArrayList<String>(Arrays.asList((String)"Venus", (String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn"))))); assert(bf(("Neptune"), ("Venus")).equals((new ArrayList<String>(Arrays.asList((String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn", (String)"Uranus"))))); assert(bf(("Earth"), ("Earth")).equals((new ArrayList<String>(Arrays.asList())))); assert(bf(("Mars"), ("Earth")).equals((new ArrayList<String>(Arrays.asList())))); assert(bf(("Jupiter"), ("Makemake")).equals((new ArrayList<String>(Arrays.asList())))); } }
humaneval-HumanEval_90_next_smallest.json-L20
py
from typing import List, Optional def next_smallest(lst: List[int]) -> Optional[int]: """ You are given a list of integers. Write a function next_smallest() that returns the 2nd smallest element of the list. Return None if there is no such element. >>> next_smallest([1, 2, 3, 4, 5]) 2 >>> next_smallest([5, 1, 4, 3, 2]) 2 >>> next_smallest([]) None >>> next_smallest([1, 1]) None """ if len(lst) < 2: return None min1 = min(lst[0], lst[1])
for i in range(2, len(lst)): if lst[i] < min1: min2 = min1 min1 = lst[i] elif lst[i] < min2: min2 = lst[i] return min2 if min1 != min2 else None
min2 = max(lst[0], lst[1])
def check(candidate): assert candidate([1, 2, 3, 4, 5]) == 2 assert candidate([5, 1, 4, 3, 2]) == 2 assert candidate([]) == None assert candidate([1, 1]) == None assert candidate([1, 1, 1, 1, 0]) == 1 assert candidate([1, 1]) == None assert candidate([-35, 34, 12, -45]) == -35 def test_check(): check(next_smallest) test_check()
humaneval-HumanEval_156_int_to_mini_roman.json-L25
py
def int_to_mini_roman(number: int) -> str: """ Given a positive integer, obtain its roman numeral equivalent as a string, and return it in lowercase. Restrictions: 1 <= num <= 1000 Examples: >>> int_to_mini_roman(19) 'xix' >>> int_to_mini_roman(152) 'clii' >>> int_to_mini_roman(426) 'cdxxvi' """ roman_numerals = { 1000: 'm', 900: 'cm', 500: 'd', 400: 'cd', 100: 'c', 90: 'xc', 50: 'l', 40: 'xl', 10: 'x',
5: 'v', 4: 'iv', 1: 'i' } roman_numeral = '' for key in sorted(roman_numerals.keys(), reverse=True): roman_numeral += roman_numerals[key] * (number // key) number = number % key return roman_numeral
9: 'ix',
def check(candidate): assert candidate(19) == 'xix' assert candidate(152) == 'clii' assert candidate(251) == 'ccli' assert candidate(426) == 'cdxxvi' assert candidate(500) == 'd' assert candidate(1) == 'i' assert candidate(4) == 'iv' assert candidate(43) == 'xliii' assert candidate(90) == 'xc' assert candidate(94) == 'xciv' assert candidate(532) == 'dxxxii' assert candidate(900) == 'cm' assert candidate(994) == 'cmxciv' assert candidate(1000) == 'm' def test_check(): check(int_to_mini_roman) test_check()
humaneval-HumanEval_158_find_max.json-L19
py
from typing import List def find_max(words: List[str]) -> str: """Write a function that accepts a list of strings. The 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. >>> find_max(['name', 'of', 'string']) 'string' >>> find_max(['name', 'enam', 'game']) 'enam' >>> find_max(['aaaaaaa', 'bb', 'cc']) 'aaaaaaa' """ max_num = 0 max_word = "" for word in words:
if unique_num > max_num: max_num = unique_num max_word = word elif unique_num == max_num: if word < max_word: max_word = word return max_word
unique_num = len(set(word))
def check(candidate): assert candidate(['name', 'of', 'string']) == 'string' assert candidate(['name', 'enam', 'game']) == 'enam' assert candidate(['aaaaaaa', 'bb', 'cc']) == 'aaaaaaa' assert candidate(['abc', 'cba']) == 'abc' assert candidate(['play', 'this', 'game', 'of', 'footbott']) == 'footbott' assert candidate(['we', 'are', 'gonna', 'rock']) == 'gonna' assert candidate(['we', 'are', 'a', 'mad', 'nation']) == 'nation' assert candidate(['this', 'is', 'a', 'prrk']) == 'this' assert candidate(['b']) == 'b' assert candidate(['play', 'play', 'play']) == 'play' def test_check(): check(find_max) test_check()
humaneval-HumanEval_83_starts_one_ends.json-L6
py
def starts_one_ends(n: int) -> int: """ Given a positive integer n, return the count of the numbers of n-digit positive integers that start or end with 1. """
for i in range(10**(n-1), 10**n): if i // 10**(n-1) == 1 or i % 10 == 1: count += 1 return count
count = 0
def check(candidate): assert candidate(1) == 1 assert candidate(2) == 18 assert candidate(3) == 180 assert candidate(4) == 1800 assert candidate(5) == 18000 def test_check(): check(starts_one_ends) test_check()
humaneval-HumanEval_48_is_palindrome.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 { // Checks if given string is a palindrome // >>> isPalindrome(("")) // (true) // >>> isPalindrome(("aba")) // (true) // >>> isPalindrome(("aaaaa")) // (true) // >>> isPalindrome(("zbcd")) // (false) public static boolean isPalindrome(String text) { boolean isPalindrome = true; int left = 0; int right = text.length() - 1; while (left < right) { if (text.charAt(left) != text.charAt(right)) { isPalindrome = false; break; } left++; right--; }
} }
return isPalindrome;
} public static void main(String[] args) { assert(isPalindrome(("")) == (true)); assert(isPalindrome(("aba")) == (true)); assert(isPalindrome(("aaaaa")) == (true)); assert(isPalindrome(("zbcd")) == (false)); assert(isPalindrome(("xywyx")) == (true)); assert(isPalindrome(("xywyz")) == (false)); assert(isPalindrome(("xywzx")) == (false)); } }
humaneval-HumanEval_81_numerical_letter_grade.json-L48
py
from typing import List def numerical_letter_grade(grades: List[float]) -> List[str]: """It is the last week of the semester and the teacher has to give the grades to students. The teacher has been making her own algorithm for grading. The only problem is, she has lost the code she used for grading. She has given you a list of GPAs for some students and you have to write a function that can output a list 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-'] """ final = [] for x in grades: if x == 4.0: final.append('A+') elif x > 3.7: final.append('A') elif x > 3.3: final.append('A-') elif x > 3.0: final.append('B+') elif x > 2.7: final.append('B') elif x > 2.3: final.append('B-') elif x > 2.0: final.append('C+') elif x > 1.7: final.append('C') elif x > 1.3:
elif x > 1.0: final.append('D+') elif x > 0.7: final.append('D') elif x > 0.0: final.append('D-') else: final.append('E') return final
final.append('C-')
def check(candidate): assert candidate([4.0, 3, 1.7, 2, 3.5]) == ['A+', 'B', 'C-', 'C', 'A-'] assert candidate([1.2]) == ['D+'] assert candidate([0.5]) == ['D-'] assert candidate([0.0]) == ['E'] assert candidate([1.0, 0.3, 1.5, 2.8, 3.3]) == ['D', 'D-', 'C-', 'B', 'B+'] assert candidate([0.0, 0.7]) == ['E', 'D-'] def test_check(): check(numerical_letter_grade) test_check()
humaneval-HumanEval_156_int_to_mini_roman.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 { // Given a positive integer, obtain its roman numeral equivalent as a string, // and return it in lowercase. // Restrictions: 1 <= num <= 1000 // Examples: // >>> intToMiniRoman((19l)) // ("xix") // >>> intToMiniRoman((152l)) // ("clii") // >>> intToMiniRoman((426l)) // ("cdxxvi") public static String intToMiniRoman(long number) {
String res = ""; if (num == 0) return ""; while (num >= 1000) { res += "m"; num -= 1000; } if (num >= 900) { res += "cm"; num -= 900; } if (num >= 500) { res += "d"; num -= 500; } if (num >= 400) { res += "cd"; num -= 400; } while (num >= 100) { res += "c"; num -= 100; } if (num >= 90) { res += "xc"; num -= 90; } if (num >= 50) { res += "l"; num -= 50; } if (num >= 40) { res += "xl"; num -= 40; } while (num >= 10) { res += "x"; num -= 10; } if (num >= 9) { res += "ix"; num -= 9; } if (num >= 5) { res += "v"; num -= 5; } if (num >= 4) { res += "iv"; num -= 4; } while (num >= 1) { res += "i"; num -= 1; } return res; } }
int num = (int) number;
} public static void main(String[] args) { assert(intToMiniRoman((19l)).equals(("xix"))); assert(intToMiniRoman((152l)).equals(("clii"))); assert(intToMiniRoman((251l)).equals(("ccli"))); assert(intToMiniRoman((426l)).equals(("cdxxvi"))); assert(intToMiniRoman((500l)).equals(("d"))); assert(intToMiniRoman((1l)).equals(("i"))); assert(intToMiniRoman((4l)).equals(("iv"))); assert(intToMiniRoman((43l)).equals(("xliii"))); assert(intToMiniRoman((90l)).equals(("xc"))); assert(intToMiniRoman((94l)).equals(("xciv"))); assert(intToMiniRoman((532l)).equals(("dxxxii"))); assert(intToMiniRoman((900l)).equals(("cm"))); assert(intToMiniRoman((994l)).equals(("cmxciv"))); assert(intToMiniRoman((1000l)).equals(("m"))); } }
humaneval-HumanEval_95_check_dict_case.json-L25
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 hash map, 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 hash map is empty. // Examples: // >>> checkDictCase((new HashMap<String,String>(Map.of("a", "apple", "b", "banana")))) // (true) // >>> checkDictCase((new HashMap<String,String>(Map.of("a", "apple", "A", "banana", "B", "banana")))) // (false) // >>> checkDictCase((new HashMap<String,String>(Map.of("a", "apple", 8l, "banana", "a", "apple")))) // (false) // >>> checkDictCase((new HashMap<String,String>(Map.of("Name", "John", "Age", "36", "City", "Houston")))) // (false) // >>> checkDictCase((new HashMap<String,String>(Map.of("STATE", "NC", "ZIP", "12345")))) // (true) public static boolean checkDictCase(HashMap<String,String> dict) { if (dict.isEmpty()) {
} boolean allLowerCase = dict.keySet().stream().allMatch(k -> k.matches("^[a-z]+$")); boolean allUpperCase = dict.keySet().stream().allMatch(k -> k.matches("^[A-Z]+$")); return allLowerCase || allUpperCase; } }
return false;
} public static void main(String[] args) { assert(checkDictCase((new HashMap<String,String>(Map.of("p", "pineapple", "b", "banana")))) == (true)); assert(checkDictCase((new HashMap<String,String>(Map.of("p", "pineapple", "A", "banana", "B", "banana")))) == (false)); assert(checkDictCase((new HashMap<String,String>(Map.of("p", "pineapple", "5", "banana", "a", "apple")))) == (false)); assert(checkDictCase((new HashMap<String,String>(Map.of("Name", "John", "Age", "36", "City", "Houston")))) == (false)); assert(checkDictCase((new HashMap<String,String>(Map.of("STATE", "NC", "ZIP", "12345")))) == (true)); assert(checkDictCase((new HashMap<String,String>(Map.of("fruit", "Orange", "taste", "Sweet")))) == (true)); assert(checkDictCase((new HashMap<String,String>())) == (false)); } }
humaneval-HumanEval_18_how_many_times.json-L13
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++;
} return count; }
i = string.indexOf(substring, i) + 1;
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_19_sort_numbers.json-L10
py
def sort_numbers(numbers: str) -> str: """ 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' """ words = numbers.split() res = ''
for word in sorted(words, key=lambda x: mapper[x]): res += ' ' + word return res.strip()
mapper = {'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9}
def check(candidate): assert candidate('') == '' assert candidate('three') == 'three' assert candidate('three five nine') == 'three five nine' assert candidate('five zero four seven nine eight') == 'zero four five seven eight nine' assert candidate('six five four three two one zero') == 'zero one two three four five six' def test_check(): check(sort_numbers) test_check()
humaneval-HumanEval_37_sort_even.json-L32
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 an array array list l and returns an array array 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. // >>> sortEven((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))) // >>> sortEven((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)3l, (long)4l)))) // (new ArrayList<Long>(Arrays.asList((long)3l, (long)6l, (long)5l, (long)4l))) public static ArrayList<Long> sortEven(ArrayList<Long> l) { ArrayList<Long> res = new ArrayList<Long>(); ArrayList<Long> evenIndexed = new ArrayList<Long>(); int index = 0; for (Long num : l) { if (index % 2 == 0) { evenIndexed.add(num); } index++; } Collections.sort(evenIndexed); index = 0; for (Long num : l) { if (index % 2 == 0) { res.add(evenIndexed.get(0)); evenIndexed.remove(0);
res.add(num); } index++; } return res; } }
} else {
} public static void main(String[] args) { assert(sortEven((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))))); assert(sortEven((new ArrayList<Long>(Arrays.asList((long)5l, (long)3l, (long)-5l, (long)2l, (long)-3l, (long)3l, (long)9l, (long)0l, (long)123l, (long)1l, (long)-10l)))).equals((new ArrayList<Long>(Arrays.asList((long)-10l, (long)3l, (long)-5l, (long)2l, (long)-3l, (long)3l, (long)5l, (long)0l, (long)9l, (long)1l, (long)123l))))); assert(sortEven((new ArrayList<Long>(Arrays.asList((long)5l, (long)8l, (long)-12l, (long)4l, (long)23l, (long)2l, (long)3l, (long)11l, (long)12l, (long)-10l)))).equals((new ArrayList<Long>(Arrays.asList((long)-12l, (long)8l, (long)3l, (long)4l, (long)5l, (long)2l, (long)12l, (long)11l, (long)23l, (long)-10l))))); } }
humaneval-HumanEval_13_greatest_common_divisor.json-L12
js
//Return a greatest common divisor of two integers a and b // >>> greatest_common_divisor(3, 5) // 1 // >>> greatest_common_divisor(25, 15) // 5 function greatest_common_divisor(a, b){ let max = Math.max(a, b) let min = Math.min(a, b) if (max % min == 0) { return min } else {
} }
return greatest_common_divisor(min, max % min)
const assert = require('node:assert'); function test() { let candidate = greatest_common_divisor; assert.deepEqual(candidate(3, 7),1); assert.deepEqual(candidate(10, 15),5); assert.deepEqual(candidate(49, 14),7); assert.deepEqual(candidate(144, 60),12); } test();
humaneval-HumanEval_25_factorize.json-L25
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 array list of prime factors of given integer in the order from smallest to largest. // Each of the factors should be array listed number of times corresponding to how many times it appeares in factorization. // Input number should be equal to the product of all factors // >>> factorize((8l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)2l, (long)2l))) // >>> factorize((25l)) // (new ArrayList<Long>(Arrays.asList((long)5l, (long)5l))) // >>> factorize((70l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)5l, (long)7l))) public static ArrayList<Long> factorize(long n) { ArrayList<Long> factors = new ArrayList<Long>(); for (long d = 2; d*d <= n; d++) { while (n % d == 0) { factors.add(d); n /= d; }
if (n > 1) factors.add(n); return factors; } }
}
} public static void main(String[] args) { assert(factorize((2l)).equals((new ArrayList<Long>(Arrays.asList((long)2l))))); assert(factorize((4l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l))))); assert(factorize((8l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l, (long)2l))))); assert(factorize((57l)).equals((new ArrayList<Long>(Arrays.asList((long)3l, (long)19l))))); assert(factorize((3249l)).equals((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l, (long)19l, (long)19l))))); assert(factorize((185193l)).equals((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l, (long)3l, (long)19l, (long)19l, (long)19l))))); assert(factorize((20577l)).equals((new ArrayList<Long>(Arrays.asList((long)3l, (long)19l, (long)19l, (long)19l))))); assert(factorize((18l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)3l))))); } }
humaneval-HumanEval_15_string_sequence.json-L8
js
//Return a string containing space-delimited numbers starting from 0 upto n inclusive. // >>> string_sequence(0) // "0" // >>> string_sequence(5) // "0 1 2 3 4 5" function string_sequence(n){ let arr = [];
arr.push(i); } return arr.join(" "); }
for(let i = 0; i <= n; i++){
const assert = require('node:assert'); function test() { let candidate = string_sequence; assert.deepEqual(candidate(0),"0"); assert.deepEqual(candidate(3),"0 1 2 3"); assert.deepEqual(candidate(10),"0 1 2 3 4 5 6 7 8 9 10"); } test();
humaneval-HumanEval_131_digits.json-L15
py
def digits(n: int) -> int: """Given a positive integer n, return the product of the odd digits. Return 0 if all digits are even. For example: >>> digits(1) 1 >>> digits(4) 0 >>> digits(235) 15 """ if n < 10: return n if n % 2 == 1 else 0 d, m = divmod(n, 10)
return digits(d) * (m if m % 2 == 1 else 1)
def check(candidate): assert candidate(5) == 5 assert candidate(54) == 5 assert candidate(120) == 1 assert candidate(5014) == 5 assert candidate(98765) == 315 assert candidate(5576543) == 2625 assert candidate(2468) == 0 def test_check(): check(digits) test_check()
humaneval-HumanEval_75_is_multiply_prime.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 { // Write a function that returns true if the given number is the multiplication of 3 prime numbers // and false otherwise. // Knowing that (a) is less then 100. // Example: // >>> isMultiplyPrime((30l)) // (true) // 30 = 2 * 3 * 5 public static boolean isMultiplyPrime(long a) { if (a == 1) { return false; } if (a == 2) { return true; } long copyOfInput = a; ArrayList<Long> arr = new ArrayList<Long>(); while (a % 2 == 0) {
a /= 2; } for (long i = 3; i <= Math.sqrt(a); i += 2) { while (a % i == 0) { arr.add(i); a /= i; } } if (a > 2) { arr.add(a); } return arr.size() == 3; } }
arr.add(2l);
} public static void main(String[] args) { assert(isMultiplyPrime((5l)) == (false)); assert(isMultiplyPrime((30l)) == (true)); assert(isMultiplyPrime((8l)) == (true)); assert(isMultiplyPrime((10l)) == (false)); assert(isMultiplyPrime((125l)) == (true)); assert(isMultiplyPrime((105l)) == (true)); assert(isMultiplyPrime((126l)) == (false)); assert(isMultiplyPrime((729l)) == (false)); assert(isMultiplyPrime((891l)) == (false)); assert(isMultiplyPrime((1001l)) == (true)); } }
humaneval-HumanEval_129_minPath.json-L56
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){ x = i; y = j; } } } if(x > 0){ neighbours.push(grid[x - 1][y]); } if(x < n - 1){ neighbours.push(grid[x + 1][y]); }
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(y > 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-L21
js
//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(1) // 0 // >>> fibfib(5) // 4 // >>> fibfib(8) // 24 function fibfib(n){ if(n <= 0){ return 0; } if(n == 1){ return 0; } if(n == 2){
} return fibfib(n-1) + fibfib(n-2) + fibfib(n-3); }
return 1;
const assert = require('node:assert'); function test() { let candidate = fibfib; assert.deepEqual(candidate(2),1); assert.deepEqual(candidate(1),0); assert.deepEqual(candidate(5),4); assert.deepEqual(candidate(8),24); assert.deepEqual(candidate(10),81); assert.deepEqual(candidate(12),274); assert.deepEqual(candidate(14),927); } test();
humaneval-HumanEval_91_is_bored.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 { // You'll be given a string of words, and your task is to count the number // of boredoms. A boredom is a sentence that starts with the word "I". // Sentences are delimited by '.', '?' or '!'. // For example: // >>> isBored(("Hello world")) // (0l) // >>> isBored(("The sky is blue. The sun is shining. I love this weather")) // (1l) public static long isBored(String S) { long boredoms = 0; String[] sentences = S.split("\\.\\s|\\?\\s|\\!\\s"); for (String sentence : sentences) { String[] words = sentence.split("\\s"); if (words.length > 0 && words[0].equals("I")) { boredoms++;
} return boredoms; } }
}
} public static void main(String[] args) { assert(isBored(("Hello world")) == (0l)); assert(isBored(("Is the sky blue?")) == (0l)); assert(isBored(("I love It !")) == (1l)); assert(isBored(("bIt")) == (0l)); assert(isBored(("I feel good today. I will be productive. will kill It")) == (2l)); assert(isBored(("You and I are going for a walk")) == (0l)); } }
humaneval-HumanEval_74_total_match.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 { // Write a function that accepts two array lists of strings and returns the array list that has // total number of chars in the all strings of the array list less than the other array list. // if the two array lists have the same number of chars, return the first array list. // Examples // >>> totalMatch((new ArrayList<String>(Arrays.asList())), (new ArrayList<String>(Arrays.asList()))) // (new ArrayList<String>(Arrays.asList())) // >>> totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hI", (String)"Hi")))) // (new ArrayList<String>(Arrays.asList((String)"hI", (String)"Hi"))) // >>> totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hi", (String)"hi", (String)"admin", (String)"project")))) // (new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))) // >>> totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hI", (String)"hi", (String)"hi")))) // (new ArrayList<String>(Arrays.asList((String)"hI", (String)"hi", (String)"hi"))) // >>> totalMatch((new ArrayList<String>(Arrays.asList((String)"4"))), (new ArrayList<String>(Arrays.asList((String)"1", (String)"2", (String)"3", (String)"4", (String)"5")))) // (new ArrayList<String>(Arrays.asList((String)"4"))) public static ArrayList<String> totalMatch(ArrayList<String> lst1, ArrayList<String> lst2) { int sum1 = 0; int sum2 = 0; for (String s : lst1) { sum1 += s.length(); } for (String s : lst2) { sum2 += s.length(); } if (sum1 <= sum2) { return lst1;
return lst2; } } }
} else {
} public static void main(String[] args) { assert(totalMatch((new ArrayList<String>(Arrays.asList())), (new ArrayList<String>(Arrays.asList()))).equals((new ArrayList<String>(Arrays.asList())))); assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hi", (String)"hi")))).equals((new ArrayList<String>(Arrays.asList((String)"hi", (String)"hi"))))); assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hi", (String)"hi", (String)"admin", (String)"project")))).equals((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))))); assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"4"))), (new ArrayList<String>(Arrays.asList((String)"1", (String)"2", (String)"3", (String)"4", (String)"5")))).equals((new ArrayList<String>(Arrays.asList((String)"4"))))); assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hI", (String)"Hi")))).equals((new ArrayList<String>(Arrays.asList((String)"hI", (String)"Hi"))))); assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hI", (String)"hi", (String)"hi")))).equals((new ArrayList<String>(Arrays.asList((String)"hI", (String)"hi", (String)"hi"))))); assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hI", (String)"hi", (String)"hii")))).equals((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))))); assert(totalMatch((new ArrayList<String>(Arrays.asList())), (new ArrayList<String>(Arrays.asList((String)"this")))).equals((new ArrayList<String>(Arrays.asList())))); assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"this"))), (new ArrayList<String>(Arrays.asList()))).equals((new ArrayList<String>(Arrays.asList())))); } }
humaneval-HumanEval_8_sum_product.json-L12
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){ if(numbers.length == 0){ return [0, 1]; } let sum = 0;
for(let i = 0; i < numbers.length; i++){ sum += numbers[i]; product *= numbers[i]; } return [sum, product]; }
let product = 1;
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_91_is_bored.json-L23
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'll be given a string of words, and your task is to count the number // of boredoms. A boredom is a sentence that starts with the word "I". // Sentences are delimited by '.', '?' or '!'. // For example: // >>> isBored(("Hello world")) // (0l) // >>> isBored(("The sky is blue. The sun is shining. I love this weather")) // (1l) public static long isBored(String S) { long boredoms = 0; String[] sentences = S.split("\\.\\s|\\?\\s|\\!\\s"); for (String sentence : sentences) { String[] words = sentence.split("\\s"); if (words.length > 0 && words[0].equals("I")) {
} } return boredoms; } }
boredoms++;
} public static void main(String[] args) { assert(isBored(("Hello world")) == (0l)); assert(isBored(("Is the sky blue?")) == (0l)); assert(isBored(("I love It !")) == (1l)); assert(isBored(("bIt")) == (0l)); assert(isBored(("I feel good today. I will be productive. will kill It")) == (2l)); assert(isBored(("You and I are going for a walk")) == (0l)); } }
humaneval-HumanEval_59_largest_prime_factor.json-L11
js
//Return the largest prime factor of n. Assume n > 1 and is not a prime. // >>> largest_prime_factor(13195) // 29 // >>> largest_prime_factor(2048) // 2 function largest_prime_factor(n){ var result = 2; while(n > result){ if(n % result == 0){ n = n / result;
else{ result = result + 1; } } return result; }
}
const assert = require('node:assert'); function test() { let candidate = largest_prime_factor; assert.deepEqual(candidate(15),5); assert.deepEqual(candidate(27),3); assert.deepEqual(candidate(63),7); assert.deepEqual(candidate(330),11); assert.deepEqual(candidate(13195),29); } test();
humaneval-HumanEval_87_get_row.json-L29
js
//You are given a 2 dimensional data, as a nested arrays, // which is similar to matrix, however, unlike matrices, // each row may contain a different number of columns. // Given lst, and integer x, find integers x in the array, // and return array of arrays, [(x1, y1), (x2, y2) ...] such that // each array is a coordinate - (row, columns), starting with 0. // Sort coordinates initially by rows in ascending order. // Also, sort coordinates of the row by columns in descending order. // Examples: // >>> get_row([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 1, 6], [1, 2, 3, 4, 5, 1]], 1) // [[0, 0], [1, 4], [1, 0], [2, 5], [2, 0]] // >>> get_row([], 1) // [] // >>> get_row([[], [1], [1, 2, 3]], 3) // [[2, 2]] function get_row(lst, x){ let coordinates = []; for (let i = 0; i < lst.length; i++){ let array = lst[i]; for (let j = 0; j < array.length; j++){ if (lst[i][j] === x){ coordinates.push([i, j]); } } } return coordinates.sort(function(a, b){ return a[0] - b[0] || b[1] - a[1]; });
}
const assert = require('node:assert'); function test() { let candidate = get_row; assert.deepEqual(candidate([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 1, 6], [1, 2, 3, 4, 5, 1]], 1),[[0, 0], [1, 4], [1, 0], [2, 5], [2, 0]]); assert.deepEqual(candidate([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]], 2),[[0, 1], [1, 1], [2, 1], [3, 1], [4, 1], [5, 1]]); assert.deepEqual(candidate([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 1, 3, 4, 5, 6], [1, 2, 1, 4, 5, 6], [1, 2, 3, 1, 5, 6], [1, 2, 3, 4, 1, 6], [1, 2, 3, 4, 5, 1]], 1),[[0, 0], [1, 0], [2, 1], [2, 0], [3, 2], [3, 0], [4, 3], [4, 0], [5, 4], [5, 0], [6, 5], [6, 0]]); assert.deepEqual(candidate([], 1),[]); assert.deepEqual(candidate([[1]], 2),[]); assert.deepEqual(candidate([[], [1], [1, 2, 3]], 3),[[2, 2]]); } test();
humaneval-HumanEval_94_skjkasdkd.json-L58
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 an array array list of integers. // You need to find the largest prime value and return the sum of its digits. // Examples: // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)3l, (long)2l, (long)1l, (long)3l, (long)5l, (long)7l, (long)4l, (long)5l, (long)5l, (long)5l, (long)2l, (long)181l, (long)32l, (long)4l, (long)32l, (long)3l, (long)2l, (long)32l, (long)324l, (long)4l, (long)3l)))) // (10l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)1l, (long)8l, (long)2l, (long)4597l, (long)2l, (long)1l, (long)3l, (long)40l, (long)1l, (long)2l, (long)1l, (long)2l, (long)4l, (long)2l, (long)5l, (long)1l)))) // (25l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)1l, (long)32l, (long)5107l, (long)34l, (long)83278l, (long)109l, (long)163l, (long)23l, (long)2323l, (long)32l, (long)30l, (long)1l, (long)9l, (long)3l)))) // (13l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)724l, (long)32l, (long)71l, (long)99l, (long)32l, (long)6l, (long)0l, (long)5l, (long)91l, (long)83l, (long)0l, (long)5l, (long)6l)))) // (11l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)81l, (long)12l, (long)3l, (long)1l, (long)21l)))) // (3l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)8l, (long)1l, (long)2l, (long)1l, (long)7l)))) // (7l) public static long skjkasdkd(ArrayList<Long> lst) { long largestPrime = 0l; for (Long item : lst) { long number = item; int counter = 0; if (item == 1) { continue; } if (item == 2) { counter = 1; } else { int l = 1; while (l <= (int) number) { if (number % l == 0) { counter++; } l++; } } if (counter == 2) { if (number > largestPrime) { largestPrime = number; } } } if (largestPrime == 0) { return 0l; } else { long output = 0l; while (largestPrime > 0) { output += largestPrime % 10; largestPrime /= 10;
return output; } } }
}
} public static void main(String[] args) { assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)3l, (long)2l, (long)1l, (long)3l, (long)5l, (long)7l, (long)4l, (long)5l, (long)5l, (long)5l, (long)2l, (long)181l, (long)32l, (long)4l, (long)32l, (long)3l, (long)2l, (long)32l, (long)324l, (long)4l, (long)3l)))) == (10l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)1l, (long)8l, (long)2l, (long)4597l, (long)2l, (long)1l, (long)3l, (long)40l, (long)1l, (long)2l, (long)1l, (long)2l, (long)4l, (long)2l, (long)5l, (long)1l)))) == (25l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)1l, (long)32l, (long)5107l, (long)34l, (long)83278l, (long)109l, (long)163l, (long)23l, (long)2323l, (long)32l, (long)30l, (long)1l, (long)9l, (long)3l)))) == (13l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)724l, (long)32l, (long)71l, (long)99l, (long)32l, (long)6l, (long)0l, (long)5l, (long)91l, (long)83l, (long)0l, (long)5l, (long)6l)))) == (11l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)81l, (long)12l, (long)3l, (long)1l, (long)21l)))) == (3l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)8l, (long)1l, (long)2l, (long)1l, (long)7l)))) == (7l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)8191l)))) == (19l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)8191l, (long)123456l, (long)127l, (long)7l)))) == (19l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)127l, (long)97l, (long)8192l)))) == (10l)); } }
humaneval-HumanEval_109_move_one_ball.json-L41
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 { // We have an array array list 'arr' of N integers arr[1], arr[2], ..., arr[N].The // numbers in the array array list will be randomly ordered. Your task is to determine if // it is possible to get an array array list sorted in non-decreasing order by performing // the following operation on the given array array list: // You are allowed to perform right shift operation any number of times. // One right shift operation means shifting all elements of the array array list by one // position in the right direction. The last element of the array array list will be moved to // the starting position in the array array list i.e. 0th index. // If it is possible to obtain the sorted array array list by performing the above operation // then return true else return false. // If the given array array list is empty then return true. // Note: The given array list is guaranteed to have unique elements. // For Example: // >>> moveOneBall((new ArrayList<Long>(Arrays.asList((long)3l, (long)4l, (long)5l, (long)1l, (long)2l)))) // (true) // Explanation: By performin 2 right shift operations, non-decreasing order can // be achieved for the given array array list. // >>> moveOneBall((new ArrayList<Long>(Arrays.asList((long)3l, (long)5l, (long)4l, (long)1l, (long)2l)))) // (false) // Explanation:It is not possible to get non-decreasing order for the given // array array list by performing any number of right shift operations. public static boolean moveOneBall(ArrayList<Long> arr) { if (arr.size() == 0) return true; ArrayList<Long> sorted = new ArrayList<Long>(arr); Collections.sort(sorted); for (int i = 0; i < arr.size(); i++) { ArrayList<Long> copy = new ArrayList<Long>(arr); long last = copy.remove(copy.size() - 1); copy.add(0, last); if (copy.equals(sorted)) return true; arr = copy; }
} }
return false;
} public static void main(String[] args) { assert(moveOneBall((new ArrayList<Long>(Arrays.asList((long)3l, (long)4l, (long)5l, (long)1l, (long)2l)))) == (true)); assert(moveOneBall((new ArrayList<Long>(Arrays.asList((long)3l, (long)5l, (long)10l, (long)1l, (long)2l)))) == (true)); assert(moveOneBall((new ArrayList<Long>(Arrays.asList((long)4l, (long)3l, (long)1l, (long)2l)))) == (false)); assert(moveOneBall((new ArrayList<Long>(Arrays.asList((long)3l, (long)5l, (long)4l, (long)1l, (long)2l)))) == (false)); assert(moveOneBall((new ArrayList<Long>(Arrays.asList()))) == (true)); } }
humaneval-HumanEval_156_int_to_mini_roman.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 { // Given a positive integer, obtain its roman numeral equivalent as a string, // and return it in lowercase. // Restrictions: 1 <= num <= 1000 // Examples: // >>> intToMiniRoman((19l)) // ("xix") // >>> intToMiniRoman((152l)) // ("clii") // >>> intToMiniRoman((426l)) // ("cdxxvi") public static String intToMiniRoman(long number) { int num = (int) number; String res = "";
return ""; while (num >= 1000) { res += "m"; num -= 1000; } if (num >= 900) { res += "cm"; num -= 900; } if (num >= 500) { res += "d"; num -= 500; } if (num >= 400) { res += "cd"; num -= 400; } while (num >= 100) { res += "c"; num -= 100; } if (num >= 90) { res += "xc"; num -= 90; } if (num >= 50) { res += "l"; num -= 50; } if (num >= 40) { res += "xl"; num -= 40; } while (num >= 10) { res += "x"; num -= 10; } if (num >= 9) { res += "ix"; num -= 9; } if (num >= 5) { res += "v"; num -= 5; } if (num >= 4) { res += "iv"; num -= 4; } while (num >= 1) { res += "i"; num -= 1; } return res; } }
if (num == 0)
} public static void main(String[] args) { assert(intToMiniRoman((19l)).equals(("xix"))); assert(intToMiniRoman((152l)).equals(("clii"))); assert(intToMiniRoman((251l)).equals(("ccli"))); assert(intToMiniRoman((426l)).equals(("cdxxvi"))); assert(intToMiniRoman((500l)).equals(("d"))); assert(intToMiniRoman((1l)).equals(("i"))); assert(intToMiniRoman((4l)).equals(("iv"))); assert(intToMiniRoman((43l)).equals(("xliii"))); assert(intToMiniRoman((90l)).equals(("xc"))); assert(intToMiniRoman((94l)).equals(("xciv"))); assert(intToMiniRoman((532l)).equals(("dxxxii"))); assert(intToMiniRoman((900l)).equals(("cm"))); assert(intToMiniRoman((994l)).equals(("cmxciv"))); assert(intToMiniRoman((1000l)).equals(("m"))); } }
humaneval-HumanEval_20_find_closest_elements.json-L14
py
from typing import List, Tuple def find_closest_elements(numbers: List[float]) -> Tuple[float, float]: """ From a supplied list of numbers (of length at least two) select and return 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) """ temp, temp2 = abs(numbers[0] - numbers[1]), abs(numbers[0] - numbers[1]) temp_index, temp2_index = 0, 1 if numbers[0] > numbers[1]:
temp_index, temp2_index = temp2_index, temp_index for i in range(len(numbers)): for j in range(i+1, len(numbers)): if abs(numbers[i] - numbers[j]) < temp: temp = abs(numbers[i] - numbers[j]) temp_index = i temp2_index = j elif abs(numbers[i] - numbers[j]) == temp: if numbers[i] > numbers[j]: temp = abs(numbers[i] - numbers[j]) temp_index = i temp2_index = j if numbers[temp_index] > numbers[temp2_index]: temp, temp2 = numbers[temp2_index], numbers[temp_index] else: temp, temp2 = numbers[temp_index], numbers[temp2_index] return temp, temp2 print(find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.2]))
temp, temp2 = temp2, temp
def check(candidate): assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2]) == (3.9, 4.0) assert candidate([1.0, 2.0, 5.9, 4.0, 5.0]) == (5.0, 5.9) assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.2]) == (2.0, 2.2) assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0]) == (2.0, 2.0) assert candidate([1.1, 2.2, 3.1, 4.1, 5.1]) == (2.2, 3.1) def test_check(): check(find_closest_elements) test_check()
humaneval-HumanEval_160_do_algebra.json-L58
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];
case '-': answer -= operand[0]; break; } operand.splice(0, 1); operator.splice(0, 1); } return answer; }
break;
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_155_even_odd_count.json-L18
py
from typing import Tuple def even_odd_count(num: int) -> Tuple[int, int]: """Given an integer. return a tuple that has the number of even and odd digits respectively. Example: >>> even_odd_count(-12) (1, 1) >>> even_odd_count(123) (1, 2) """ num_str = str(abs(num)) even_count = 0 odd_count = 0 for ch in num_str: digit = int(ch) if digit % 2 == 0:
else: odd_count += 1 return even_count, odd_count
even_count += 1
def check(candidate): assert candidate(7) == (0, 1) assert candidate(-78) == (1, 1) assert candidate(3452) == (2, 2) assert candidate(346211) == (3, 3) assert candidate(-345821) == (3, 3) assert candidate(-2) == (1, 0) assert candidate(-45347) == (2, 3) assert candidate(0) == (1, 0) def test_check(): check(even_odd_count) test_check()
humaneval-HumanEval_55_fib.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 { // Return n-th Fibonacci number. // >>> fib((10l)) // (55l) // >>> fib((1l)) // (1l) // >>> fib((8l)) // (21l) public static long fib(long n) { if (n == 0) { return 0l; } else if (n == 1) { return 1l; } else {
} } }
return Problem.fib((n - 1)) + Problem.fib((n - 2));
} public static void main(String[] args) { assert(fib((10l)) == (55l)); assert(fib((1l)) == (1l)); assert(fib((8l)) == (21l)); assert(fib((11l)) == (89l)); assert(fib((12l)) == (144l)); } }
humaneval-HumanEval_111_histogram.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 a string representing a space separated lowercase letters, return a hash map // 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")) // (new HashMap<String,Long>(Map.of("a", 1l, "b", 1l, "c", 1l))) // >>> histogram(("a b b a")) // (new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))) // >>> histogram(("a b c a b")) // (new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))) // >>> histogram(("b b b b a")) // (new HashMap<String,Long>(Map.of("b", 4l))) // >>> histogram(("")) // (new HashMap<String,Long>()) public static HashMap<String,Long> histogram(String test) { HashMap<String,Long> s = new HashMap<String,Long>(); if (test.length() > 0) { HashMap<String,Long> hm = new HashMap<String,Long>(); String[] ss = test.split(" "); for (String sss : ss) { if (hm.containsKey(sss)) { hm.put(sss, hm.get(sss) + 1); } else { hm.put(sss, 1l); } } long max = 0; for (String key : hm.keySet()) { if (hm.get(key) > max) { max = hm.get(key); }
for (String key : hm.keySet()) { if (hm.get(key) == max) { s.put(key, hm.get(key)); } } } return s; } }
}
} public static void main(String[] args) { assert(histogram(("a b b a")).equals((new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))))); assert(histogram(("a b c a b")).equals((new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))))); assert(histogram(("a b c d g")).equals((new HashMap<String,Long>(Map.of("a", 1l, "b", 1l, "c", 1l, "d", 1l, "g", 1l))))); assert(histogram(("r t g")).equals((new HashMap<String,Long>(Map.of("r", 1l, "t", 1l, "g", 1l))))); assert(histogram(("b b b b a")).equals((new HashMap<String,Long>(Map.of("b", 4l))))); assert(histogram(("r t g")).equals((new HashMap<String,Long>(Map.of("r", 1l, "t", 1l, "g", 1l))))); assert(histogram(("")).equals((new HashMap<String,Long>()))); assert(histogram(("a")).equals((new HashMap<String,Long>(Map.of("a", 1l))))); } }
humaneval-HumanEval_68_pluck.json-L32
js
//"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 an array, [ 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 function pluck(arr){ let result = [], index = -1; for(let i = 0; i < arr.length; i++){ if(arr[i] % 2 === 0){ if(result.length === 0 || result[0] > arr[i]){
} } } return result; }
result = [arr[i], i];
const assert = require('node:assert'); function test() { let candidate = pluck; assert.deepEqual(candidate([4, 2, 3]),[2, 1]); assert.deepEqual(candidate([1, 2, 3]),[2, 1]); assert.deepEqual(candidate([]),[]); assert.deepEqual(candidate([5, 0, 3, 0, 4, 2]),[0, 1]); assert.deepEqual(candidate([1, 2, 3, 0, 5, 3]),[0, 3]); assert.deepEqual(candidate([5, 4, 8, 4, 8]),[4, 1]); assert.deepEqual(candidate([7, 6, 7, 1]),[6, 1]); assert.deepEqual(candidate([7, 9, 7, 1]),[]); } test();
humaneval-HumanEval_73_smallest_change.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 { // Given an array array list arr of integers, find the minimum number of elements that // need to be changed to make the array array list palindromic. A palindromic array array list is an array array list that // is read the same backwards and forwards. In one change, you can change one element to any other element. // For example: // >>> smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)5l, (long)4l, (long)7l, (long)9l, (long)6l)))) // (4l) // >>> smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)3l, (long)2l, (long)2l)))) // (1l) // >>> smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)2l, (long)1l)))) // (0l) public static long smallestChange(ArrayList<Long> arr) {
int last = arr.size() - 1; int count = 0; while (first < last) { if (arr.get(first) != arr.get(last)) { count++; } first++; last--; } return count; } }
int first = 0;
} public static void main(String[] args) { assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)5l, (long)4l, (long)7l, (long)9l, (long)6l)))) == (4l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)3l, (long)2l, (long)2l)))) == (1l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)2l)))) == (1l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)4l, (long)2l)))) == (1l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)2l, (long)1l)))) == (0l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)3l, (long)1l, (long)1l, (long)3l)))) == (0l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)1l)))) == (0l)); assert(smallestChange((new ArrayList<Long>(Arrays.asList((long)0l, (long)1l)))) == (1l)); } }
humaneval-HumanEval_109_move_one_ball.json-L39
py
from typing import List def move_one_ball(arr: List[int]) -> bool: """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 list 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. """ flag = False for i in range(1, len(arr)): if arr[i-1] > arr[i]: if flag == True: return False flag = True elif arr[i-1] < arr[i]:
return True
flag = False
def check(candidate): assert candidate([3, 4, 5, 1, 2]) == True assert candidate([3, 5, 10, 1, 2]) == True assert candidate([4, 3, 1, 2]) == False assert candidate([3, 5, 4, 1, 2]) == False assert candidate([]) == True def test_check(): check(move_one_ball) test_check()
humaneval-HumanEval_136_largest_smallest_integers.json-L20
py
from typing import List, Tuple, Optional def largest_smallest_integers(lst: List[int]) -> Tuple[Optional[int], Optional[int]]: """ Create a function that returns a tuple (a, b), where 'a' is the largest of negative integers, and 'b' is the smallest of positive integers in a list. If there is no negative or positive integers, return them as None. Examples: >>> largest_smallest_integers([2, 4, 1, 3, 5, 7]) (None, 1) >>> largest_smallest_integers([]) (None, None) >>> largest_smallest_integers([0]) (None, None) """ smallest_positive = None largest_negative = None
if num > 0: if smallest_positive is None or num < smallest_positive: smallest_positive = num elif num < 0: if largest_negative is None or num > largest_negative: largest_negative = num return (largest_negative, smallest_positive)
for num in lst:
def check(candidate): assert candidate([2, 4, 1, 3, 5, 7]) == (None, 1) assert candidate([2, 4, 1, 3, 5, 7, 0]) == (None, 1) assert candidate([1, 3, 2, 4, 5, 6, -2]) == (-2, 1) assert candidate([4, 5, 3, 6, 2, 7, -7]) == (-7, 2) assert candidate([7, 3, 8, 4, 9, 2, 5, -9]) == (-9, 2) assert candidate([]) == (None, None) assert candidate([0]) == (None, None) assert candidate([-1, -3, -5, -6]) == (-1, None) assert candidate([-1, -3, -5, -6, 0]) == (-1, None) assert candidate([-6, -4, -4, -3, 1]) == (-3, 1) assert candidate([-6, -4, -4, -3, -100, 1]) == (-3, 1) def test_check(): check(largest_smallest_integers) test_check()
humaneval-HumanEval_147_get_max_triples.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 { // You are given a positive integer n. You have to create an integer array array list a of length n. // For each i (1 ≤ i ≤ n), the value of a[i] = i * i - i + 1. // Return the number of triples (a[i], a[j], a[k]) of a where i < j < k, // and a[i] + a[j] + a[k] is a multiple of 3. // Example : // >>> getMaxTriples((5l)) // (1l) // Explanation: // a = [1, 3, 7, 13, 21] // The only valid triple is (1, 7, 13). public static long getMaxTriples(long n) { int count = 0; ArrayList<Integer> arrayList = new ArrayList<Integer>(); for (int i = 1; i <= n; i++) { arrayList.add(i * i - i + 1); } for (int i = 0; i < arrayList.size(); i++) { for (int j = i + 1; j < arrayList.size(); j++) { for (int k = j + 1; k < arrayList.size(); k++) { if ((arrayList.get(i) + arrayList.get(j) + arrayList.get(k)) % 3 == 0) { count++; } } } }
} }
return count;
} public static void main(String[] args) { assert(getMaxTriples((5l)) == (1l)); assert(getMaxTriples((6l)) == (4l)); assert(getMaxTriples((10l)) == (36l)); assert(getMaxTriples((100l)) == (53361l)); } }
humaneval-HumanEval_64_vowels_count.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 { // 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) { sum++; } }
} }
return 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)); } }