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_100_make_a_pile.json-L19
py
from typing import List def make_a_pile(n: int) -> List[int]: """ Given a positive integer n, you have to make a pile of n levels of stones. The first level has n stones. The number of stones in the next level is: - the next odd number if n is odd. - the next even number if n is even. Return the number of stones in each level in a list, where element at index i represents the number of stones in the level (i+1). Examples: >>> make_a_pile(3) [3, 5, 7] """ pile = [n] for i in range(1, n):
return pile
pile.append(pile[i-1] + 2)
def check(candidate): assert candidate(3) == [3, 5, 7] assert candidate(4) == [4, 6, 8, 10] assert candidate(5) == [5, 7, 9, 11, 13] assert candidate(6) == [6, 8, 10, 12, 14, 16] assert candidate(8) == [8, 10, 12, 14, 16, 18, 20, 22] def test_check(): check(make_a_pile) test_check()
humaneval-HumanEval_81_numerical_letter_grade.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 { // It is the last week of the semester and the teacher has to give the grades // to students. The teacher has been making her own algorithm for grading. // The only problem is, she has lost the code she used for grading. // She has given you an array array list of GPAs for some students and you have to write // a function that can output an array array 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: // >>> gradeEquation((new ArrayList<Float>(Arrays.asList((float)4.0f, (float)3l, (float)1.7f, (float)2l, (float)3.5f)))) // (new ArrayList<String>(Arrays.asList((String)"A+", (String)"B", (String)"C-", (String)"C", (String)"A-"))) public static ArrayList<String> numericalLetterGrade(ArrayList<Float> grades) { ArrayList<String> letterGrades = new ArrayList<String>(); for (int i = 0; i < grades.size(); i++) { float currentGrade = grades.get(i); if (currentGrade == 4.0f) { letterGrades.add("A+"); } else if (currentGrade > 3.7f) { letterGrades.add("A"); } else if (currentGrade > 3.3f) { letterGrades.add("A-");
letterGrades.add("B+"); } else if (currentGrade > 2.7f) { letterGrades.add("B"); } else if (currentGrade > 2.3f) { letterGrades.add("B-"); } else if (currentGrade > 2.0f) { letterGrades.add("C+"); } else if (currentGrade > 1.7f) { letterGrades.add("C"); } else if (currentGrade > 1.3f) { letterGrades.add("C-"); } else if (currentGrade > 1.0f) { letterGrades.add("D+"); } else if (currentGrade > 0.7f) { letterGrades.add("D"); } else if (currentGrade > 0.0f) { letterGrades.add("D-"); } else { letterGrades.add("E"); } } return letterGrades; } }
} else if (currentGrade > 3.0f) {
} public static void main(String[] args) { assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)4.0f, (float)3l, (float)1.7f, (float)2l, (float)3.5f)))).equals((new ArrayList<String>(Arrays.asList((String)"A+", (String)"B", (String)"C-", (String)"C", (String)"A-"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)1.2f)))).equals((new ArrayList<String>(Arrays.asList((String)"D+"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.5f)))).equals((new ArrayList<String>(Arrays.asList((String)"D-"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.0f)))).equals((new ArrayList<String>(Arrays.asList((String)"E"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)0.3f, (float)1.5f, (float)2.8f, (float)3.3f)))).equals((new ArrayList<String>(Arrays.asList((String)"D", (String)"D-", (String)"C-", (String)"B", (String)"B+"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.0f, (float)0.7f)))).equals((new ArrayList<String>(Arrays.asList((String)"E", (String)"D-"))))); } }
humaneval-HumanEval_138_is_equal_to_sum_even.json-L11
py
def is_equal_to_sum_even(n: int) -> bool: """Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers Example >>> is_equal_to_sum_even(4) False >>> is_equal_to_sum_even(6) False >>> is_equal_to_sum_even(8) True """
return False return n % 2 == 0
if n < 8:
def check(candidate): assert candidate(4) == False assert candidate(6) == False assert candidate(8) == True assert candidate(10) == True assert candidate(11) == False assert candidate(12) == True assert candidate(13) == False assert candidate(16) == True def test_check(): check(is_equal_to_sum_even) test_check()
humaneval-HumanEval_88_sort_array.json-L37
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given an array array list of non-negative integers, return a cojava of the given array array list after sorting, // you will sort the given array array list in ascending order if the sum( first index value, last index value) is odd, // or sort it in descending order if the sum( first index value, last index value) is even. // Note: // * don't change the given array array list. // Examples: // >>> sortArray((new ArrayList<Long>(Arrays.asList()))) // (new ArrayList<Long>(Arrays.asList())) // >>> sortArray((new ArrayList<Long>(Arrays.asList((long)5l)))) // (new ArrayList<Long>(Arrays.asList((long)5l))) // >>> sortArray((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)3l, (long)0l, (long)1l, (long)5l)))) // (new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)2l, (long)3l, (long)4l, (long)5l))) // >>> sortArray((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)3l, (long)0l, (long)1l, (long)5l, (long)6l)))) // (new ArrayList<Long>(Arrays.asList((long)6l, (long)5l, (long)4l, (long)3l, (long)2l, (long)1l, (long)0l))) public static ArrayList<Long> sortArray(ArrayList<Long> array) { if(array.size()==0) { return array; } if(array.size()==1) { return array; } if(array.size()>1) { if((array.get(0)+array.get(array.size()-1))%2==0) { Collections.sort(array, Collections.reverseOrder()); } else { Collections.sort(array); }
return array; } }
}
} public static void main(String[] args) { assert(sortArray((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<Long>(Arrays.asList())))); assert(sortArray((new ArrayList<Long>(Arrays.asList((long)5l)))).equals((new ArrayList<Long>(Arrays.asList((long)5l))))); assert(sortArray((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)3l, (long)0l, (long)1l, (long)5l)))).equals((new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)2l, (long)3l, (long)4l, (long)5l))))); assert(sortArray((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)3l, (long)0l, (long)1l, (long)5l, (long)6l)))).equals((new ArrayList<Long>(Arrays.asList((long)6l, (long)5l, (long)4l, (long)3l, (long)2l, (long)1l, (long)0l))))); assert(sortArray((new ArrayList<Long>(Arrays.asList((long)2l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l))))); assert(sortArray((new ArrayList<Long>(Arrays.asList((long)15l, (long)42l, (long)87l, (long)32l, (long)11l, (long)0l)))).equals((new ArrayList<Long>(Arrays.asList((long)0l, (long)11l, (long)15l, (long)32l, (long)42l, (long)87l))))); assert(sortArray((new ArrayList<Long>(Arrays.asList((long)21l, (long)14l, (long)23l, (long)11l)))).equals((new ArrayList<Long>(Arrays.asList((long)23l, (long)21l, (long)14l, (long)11l))))); } }
humaneval-HumanEval_82_prime_length.json-L15
py
def prime_length(string: str) -> bool: """Write a function that takes a string and returns True if the string length is a prime number or False otherwise Examples >>> prime_length('Hello') True >>> prime_length('abcdcba') True >>> prime_length('kittens') True >>> prime_length('orange') False """ num = len(string)
for i in range(2, num): if (num % i) == 0: return False else: return True else: return False pass
if num > 1:
def check(candidate): assert candidate('Hello') == True assert candidate('abcdcba') == True assert candidate('kittens') == True assert candidate('orange') == False assert candidate('wow') == True assert candidate('world') == True assert candidate('MadaM') == True assert candidate('Wow') == True assert candidate('') == False assert candidate('HI') == True assert candidate('go') == True assert candidate('gogo') == False assert candidate('aaaaaaaaaaaaaaa') == False assert candidate('Madam') == True assert candidate('M') == False assert candidate('0') == False def test_check(): check(prime_length) test_check()
humaneval-HumanEval_107_even_odd_palindrome.json-L22
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; }
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]; }
for(var i = 1; i <= n; i++){
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_81_numerical_letter_grade.json-L52
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 { // It is the last week of the semester and the teacher has to give the grades // to students. The teacher has been making her own algorithm for grading. // The only problem is, she has lost the code she used for grading. // She has given you an array array list of GPAs for some students and you have to write // a function that can output an array array 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: // >>> gradeEquation((new ArrayList<Float>(Arrays.asList((float)4.0f, (float)3l, (float)1.7f, (float)2l, (float)3.5f)))) // (new ArrayList<String>(Arrays.asList((String)"A+", (String)"B", (String)"C-", (String)"C", (String)"A-"))) public static ArrayList<String> numericalLetterGrade(ArrayList<Float> grades) { ArrayList<String> letterGrades = new ArrayList<String>(); for (int i = 0; i < grades.size(); i++) { float currentGrade = grades.get(i); if (currentGrade == 4.0f) { letterGrades.add("A+"); } else if (currentGrade > 3.7f) { letterGrades.add("A"); } else if (currentGrade > 3.3f) { letterGrades.add("A-"); } else if (currentGrade > 3.0f) { letterGrades.add("B+"); } else if (currentGrade > 2.7f) { letterGrades.add("B"); } else if (currentGrade > 2.3f) { letterGrades.add("B-"); } else if (currentGrade > 2.0f) { letterGrades.add("C+"); } else if (currentGrade > 1.7f) { letterGrades.add("C"); } else if (currentGrade > 1.3f) {
} else if (currentGrade > 1.0f) { letterGrades.add("D+"); } else if (currentGrade > 0.7f) { letterGrades.add("D"); } else if (currentGrade > 0.0f) { letterGrades.add("D-"); } else { letterGrades.add("E"); } } return letterGrades; } }
letterGrades.add("C-");
} public static void main(String[] args) { assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)4.0f, (float)3l, (float)1.7f, (float)2l, (float)3.5f)))).equals((new ArrayList<String>(Arrays.asList((String)"A+", (String)"B", (String)"C-", (String)"C", (String)"A-"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)1.2f)))).equals((new ArrayList<String>(Arrays.asList((String)"D+"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.5f)))).equals((new ArrayList<String>(Arrays.asList((String)"D-"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.0f)))).equals((new ArrayList<String>(Arrays.asList((String)"E"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)0.3f, (float)1.5f, (float)2.8f, (float)3.3f)))).equals((new ArrayList<String>(Arrays.asList((String)"D", (String)"D-", (String)"C-", (String)"B", (String)"B+"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.0f, (float)0.7f)))).equals((new ArrayList<String>(Arrays.asList((String)"E", (String)"D-"))))); } }
humaneval-HumanEval_156_int_to_mini_roman.json-L36
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_131_digits.json-L30
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_113_odd_count.json-L13
js
//Given an array of strings, where each string consists of only digits, return an array. // Each element i of the output should be "the number of odd elements in the // string i of the input." where all the i's should be replaced by the number // of odd digits in the i'th string of the input. // >>> odd_count(["1234567"]) // ["the number of odd elements 4n the str4ng 4 of the 4nput."] // >>> odd_count(["3", "11111111"]) // ["the number of odd elements 1n the str1ng 1 of the 1nput.", "the number of odd elements 8n the str8ng 8 of the 8nput."] function odd_count(lst){ var new_lst = []; for (var i = 0; i < lst.length; i++){ var word = lst[i];
for (var j = 0; j < word.length; j++){ if (word[j] % 2 == 1){ odd_sum += 1; } } var odd_word = "the number of odd elements " + odd_sum + "n the str" + odd_sum + "ng " + odd_sum + " of the " + odd_sum + "nput."; new_lst.push(odd_word); } return new_lst; }
var odd_sum = 0;
const assert = require('node:assert'); function test() { let candidate = odd_count; assert.deepEqual(candidate(["1234567"]),["the number of odd elements 4n the str4ng 4 of the 4nput."]); assert.deepEqual(candidate(["3", "11111111"]),["the number of odd elements 1n the str1ng 1 of the 1nput.", "the number of odd elements 8n the str8ng 8 of the 8nput."]); assert.deepEqual(candidate(["271", "137", "314"]),["the number of odd elements 2n the str2ng 2 of the 2nput.", "the number of odd elements 3n the str3ng 3 of the 3nput.", "the number of odd elements 2n the str2ng 2 of the 2nput."]); } test();
humaneval-HumanEval_81_numerical_letter_grade.json-L46
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:
elif x > 1.3: final.append('C-') 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_108_count_nums.json-L31
js
//Write a function count_nums which takes an array of integers and returns // the number of elements which has a sum of digits > 0. // If a number is negative, then its first signed digit will be negative: // e.g. -123 has signed digits -1, 2, and 3. // >>> count_nums([]) // 0 // >>> count_nums([-1, 11, -11]) // 1 // >>> count_nums([1, 1, 2]) // 3 function count_nums(arr){ if(!arr.length) { return 0; } var count = 0; for(var i = 0; i < arr.length; i++) { var sum = 0; var str = arr[i].toString(); if(str[0] === '-') { sum += Number(str[0] + str[1]); for(var j = 2; j < str.length; j++) { sum += Number(str[j]); } } else { for(var k = 0; k < str.length; k++) { sum += Number(str[k]); } } if(sum > 0) { count++;
} return count; }
}
const assert = require('node:assert'); function test() { let candidate = count_nums; assert.deepEqual(candidate([]),0); assert.deepEqual(candidate([-1, -2, 0]),0); assert.deepEqual(candidate([1, 1, 2, -2, 3, 4, 5]),6); assert.deepEqual(candidate([1, 6, 9, -6, 0, 1, 5]),5); assert.deepEqual(candidate([1, 100, 98, -7, 1, -1]),4); assert.deepEqual(candidate([12, 23, 34, -45, -56, 0]),5); assert.deepEqual(candidate([0, 1]),1); assert.deepEqual(candidate([1]),1); } test();
humaneval-HumanEval_71_triangle_area.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 the lengths of the three sides of a triangle. Return the area of // the triangle rounded to 2 decimal points if the three sides form a valid triangle. // Otherwise return -1 // Three sides make a valid triangle when the sum of any two sides is greater // than the third side. // Example: // >>> triangleArea((3l), (4l), (5l)) // (6.0f) // >>> triangleArea((1l), (2l), (10l)) // (float)-1l public static float triangleArea(long a, long b, long c) { if(a + b > c && a + c > b && b + c > a) { float s = (a + b + c) / 2.0f;
return (float)Math.round(area * 100) / 100; } return -1; } }
float area = (float)Math.sqrt(s * (s - a) * (s - b) * (s - c));
} public static void main(String[] args) { assert(triangleArea((3l), (4l), (5l)) == (6.0f)); assert(triangleArea((1l), (2l), (10l)) == (float)-1l); assert(triangleArea((4l), (8l), (5l)) == (8.18f)); assert(triangleArea((2l), (2l), (2l)) == (1.73f)); assert(triangleArea((1l), (2l), (3l)) == (float)-1l); assert(triangleArea((10l), (5l), (7l)) == (16.25f)); assert(triangleArea((2l), (6l), (3l)) == (float)-1l); assert(triangleArea((1l), (1l), (1l)) == (0.43f)); assert(triangleArea((2l), (2l), (10l)) == (float)-1l); } }
humaneval-HumanEval_69_search.json-L25
js
//You are given a non-empty array 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. // If no such a value exist, return -1. // Examples: // >>> search([4, 1, 2, 2, 3, 1]) // 2 // >>> search([1, 2, 2, 3, 3, 3, 4, 4, 4]) // 3 // >>> search([5, 5, 4, 4, 4]) // -1 function search(lst){ var h = {}; for (var i=0; i<lst.length; i++){ var x = lst[i]; if (x in h){ h[x] += 1; } else{ h[x] = 1; } } var keys = Object.keys(h); var max = 0;
if (keys[i] > max && h[keys[i]] >= keys[i]){ max = keys[i]; } } if (max == 0){ return -1; } return max; }
for (var i=0; i<keys.length; i++){
const assert = require('node:assert'); function test() { let candidate = search; assert.deepEqual(candidate([5, 5, 5, 5, 1]),1); assert.deepEqual(candidate([4, 1, 4, 1, 4, 4]),4); assert.deepEqual(candidate([3, 3]),-1); assert.deepEqual(candidate([8, 8, 8, 8, 8, 8, 8, 8]),8); assert.deepEqual(candidate([2, 3, 3, 2, 2]),2); assert.deepEqual(candidate([2, 7, 8, 8, 4, 8, 7, 3, 9, 6, 5, 10, 4, 3, 6, 7, 1, 7, 4, 10, 8, 1]),1); assert.deepEqual(candidate([3, 2, 8, 2]),2); assert.deepEqual(candidate([6, 7, 1, 8, 8, 10, 5, 8, 5, 3, 10]),1); assert.deepEqual(candidate([8, 8, 3, 6, 5, 6, 4]),-1); assert.deepEqual(candidate([6, 9, 6, 7, 1, 4, 7, 1, 8, 8, 9, 8, 10, 10, 8, 4, 10, 4, 10, 1, 2, 9, 5, 7, 9]),1); assert.deepEqual(candidate([1, 9, 10, 1, 3]),1); assert.deepEqual(candidate([6, 9, 7, 5, 8, 7, 5, 3, 7, 5, 10, 10, 3, 6, 10, 2, 8, 6, 5, 4, 9, 5, 3, 10]),5); assert.deepEqual(candidate([1]),1); assert.deepEqual(candidate([8, 8, 10, 6, 4, 3, 5, 8, 2, 4, 2, 8, 4, 6, 10, 4, 2, 1, 10, 2, 1, 1, 5]),4); assert.deepEqual(candidate([2, 10, 4, 8, 2, 10, 5, 1, 2, 9, 5, 5, 6, 3, 8, 6, 4, 10]),2); assert.deepEqual(candidate([1, 6, 10, 1, 6, 9, 10, 8, 6, 8, 7, 3]),1); assert.deepEqual(candidate([9, 2, 4, 1, 5, 1, 5, 2, 5, 7, 7, 7, 3, 10, 1, 5, 4, 2, 8, 4, 1, 9, 10, 7, 10, 2, 8, 10, 9, 4]),4); assert.deepEqual(candidate([2, 6, 4, 2, 8, 7, 5, 6, 4, 10, 4, 6, 3, 7, 8, 8, 3, 1, 4, 2, 2, 10, 7]),4); assert.deepEqual(candidate([9, 8, 6, 10, 2, 6, 10, 2, 7, 8, 10, 3, 8, 2, 6, 2, 3, 1]),2); assert.deepEqual(candidate([5, 5, 3, 9, 5, 6, 3, 2, 8, 5, 6, 10, 10, 6, 8, 4, 10, 7, 7, 10, 8]),-1); assert.deepEqual(candidate([10]),-1); assert.deepEqual(candidate([9, 7, 7, 2, 4, 7, 2, 10, 9, 7, 5, 7, 2]),2); assert.deepEqual(candidate([5, 4, 10, 2, 1, 1, 10, 3, 6, 1, 8]),1); assert.deepEqual(candidate([7, 9, 9, 9, 3, 4, 1, 5, 9, 1, 2, 1, 1, 10, 7, 5, 6, 7, 6, 7, 7, 6]),1); assert.deepEqual(candidate([3, 10, 10, 9, 2]),-1); } test();
humaneval-HumanEval_56_correct_bracketing.json-L21
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // 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) { int open = 0;
char c = brackets.charAt(i); if (c == '<') { open++; } else { open--; } if (open < 0) { return false; } } return open == 0; } }
for (int i = 0; i < brackets.length(); i++) {
} 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_0_has_close_elements.json-L10
js
//Check if in given array of numbers, are any two numbers closer to each other than // given threshold. // >>> has_close_elements([1.0, 2.0, 3.0], 0.5) // false // >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3) // true function has_close_elements(numbers, threshold){ for(var i = 0; i < numbers.length; i++){ for(var j = i + 1; j < numbers.length; j++){
return true; } } } return false; }
if(Math.abs(numbers[i] - numbers[j]) < threshold){
const assert = require('node:assert'); function test() { let candidate = has_close_elements; assert.deepEqual(candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.3),true); assert.deepEqual(candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.05),false); assert.deepEqual(candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.95),true); assert.deepEqual(candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.8),false); assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0], 0.1),true); assert.deepEqual(candidate([1.1, 2.2, 3.1, 4.1, 5.1], 1.0),true); assert.deepEqual(candidate([1.1, 2.2, 3.1, 4.1, 5.1], 0.5),false); } test();
humaneval-HumanEval_141_file_name_check.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 { // Create a function which takes a string representing a file's name, and returns // 'Yes' if the the file's name is valid, and returns 'No' otherwise. // A file's name is considered to be valid if and only if all the following conditions // are met: // - There should not be more than three digits ('0'-'9') in the file's name. // - The file's name contains exactly one dot '.' // - The substring before the dot should not be empty, and it starts with a letter from // the latin alphapet ('a'-'z' and 'A'-'Z'). // - The substring after the dot should be one of these: ['txt', 'exe', 'dll'] // Examples: // >>> fileNameCheck(("example.txt")) // ("Yes") // >>> fileNameCheck(("1example.dll")) // ("No") public static String fileNameCheck(String file_name) { String result = "No"; String[] tokens = file_name.split("\\."); if (tokens.length != 2) {
} String extension = tokens[1]; if (!(extension.equals("txt") || extension.equals("exe") || extension.equals("dll"))) { return result; } String name = tokens[0]; int count = 0; for (int i = 0; i < name.length(); i++) { if (Character.isDigit(name.charAt(i))) { count++; } } if (count > 3) { return result; } if (name.length() == 0) { return result; } if (!Character.isLetter(name.charAt(0))) { return result; } return "Yes"; } }
return result;
} public static void main(String[] args) { assert(fileNameCheck(("example.txt")).equals(("Yes"))); assert(fileNameCheck(("1example.dll")).equals(("No"))); assert(fileNameCheck(("s1sdf3.asd")).equals(("No"))); assert(fileNameCheck(("K.dll")).equals(("Yes"))); assert(fileNameCheck(("MY16FILE3.exe")).equals(("Yes"))); assert(fileNameCheck(("His12FILE94.exe")).equals(("No"))); assert(fileNameCheck(("_Y.txt")).equals(("No"))); assert(fileNameCheck(("?aREYA.exe")).equals(("No"))); assert(fileNameCheck(("/this_is_valid.dll")).equals(("No"))); assert(fileNameCheck(("this_is_valid.wow")).equals(("No"))); assert(fileNameCheck(("this_is_valid.txt")).equals(("Yes"))); assert(fileNameCheck(("this_is_valid.txtexe")).equals(("No"))); assert(fileNameCheck(("#this2_i4s_5valid.ten")).equals(("No"))); assert(fileNameCheck(("@this1_is6_valid.exe")).equals(("No"))); assert(fileNameCheck(("this_is_12valid.6exe4.txt")).equals(("No"))); assert(fileNameCheck(("all.exe.txt")).equals(("No"))); assert(fileNameCheck(("I563_No.exe")).equals(("Yes"))); assert(fileNameCheck(("Is3youfault.txt")).equals(("Yes"))); assert(fileNameCheck(("no_one#knows.dll")).equals(("Yes"))); assert(fileNameCheck(("1I563_Yes3.exe")).equals(("No"))); assert(fileNameCheck(("I563_Yes3.txtt")).equals(("No"))); assert(fileNameCheck(("final..txt")).equals(("No"))); assert(fileNameCheck(("final132")).equals(("No"))); assert(fileNameCheck(("_f4indsartal132.")).equals(("No"))); assert(fileNameCheck((".txt")).equals(("No"))); assert(fileNameCheck(("s.")).equals(("No"))); } }
humaneval-HumanEval_93_encode.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 { // Write a function that takes a message, and encodes in such a // way that it swaps case of all letters, replaces all vowels in // the message with the letter that appears 2 places ahead of that // vowel in the english alphabet. // Assume only letters. // Examples: // >>> encode(("test")) // ("TGST") // >>> encode(("This is a message")) // ("tHKS KS C MGSSCGG") public static String encode(String message) { char[] arr = message.toCharArray(); char[] vowels = "AEIOUaeiou".toCharArray(); for (int i = 0; i < arr.length; i++) { if (Arrays.binarySearch(vowels, arr[i]) > -1) { arr[i] = (char) (arr[i] + 2); } if (Character.isUpperCase(arr[i])) { arr[i] = Character.toLowerCase(arr[i]); } else { arr[i] = Character.toUpperCase(arr[i]); }
return new String(arr); } }
}
} public static void main(String[] args) { assert(encode(("TEST")).equals(("tgst"))); assert(encode(("Mudasir")).equals(("mWDCSKR"))); assert(encode(("YES")).equals(("ygs"))); assert(encode(("This is a message")).equals(("tHKS KS C MGSSCGG"))); assert(encode(("I DoNt KnOw WhAt tO WrItE")).equals(("k dQnT kNqW wHcT Tq wRkTg"))); } }
humaneval-HumanEval_156_int_to_mini_roman.json-L73
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) { 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) {
num -= 1; } return res; } }
res += "i";
} 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_94_skjkasdkd.json-L28
js
//You are given an array of integers. // You need to find the largest prime value and return the sum of its digits. // Examples: // >>> skjkasdkd([0, 3, 2, 1, 3, 5, 7, 4, 5, 5, 5, 2, 181, 32, 4, 32, 3, 2, 32, 324, 4, 3]) // 10 // >>> skjkasdkd([1, 0, 1, 8, 2, 4597, 2, 1, 3, 40, 1, 2, 1, 2, 4, 2, 5, 1]) // 25 // >>> skjkasdkd([1, 3, 1, 32, 5107, 34, 83278, 109, 163, 23, 2323, 32, 30, 1, 9, 3]) // 13 // >>> skjkasdkd([0, 724, 32, 71, 99, 32, 6, 0, 5, 91, 83, 0, 5, 6]) // 11 // >>> skjkasdkd([0, 81, 12, 3, 1, 21]) // 3 // >>> skjkasdkd([0, 8, 1, 2, 1, 7]) // 7 function skjkasdkd(lst){ function prime(n){ if (n<2) return false; for (let i=2; i<=Math.sqrt(n); i++){ if (n%i==0) return false; } return true; } let max = Math.max(...lst.filter(x=>prime(x))); let digits = String(max).split(''); let sum = 0; for (let i=0; i<digits.length; i++){
} return sum; }
sum += Number(digits[i]);
const assert = require('node:assert'); function test() { let candidate = skjkasdkd; assert.deepEqual(candidate([0, 3, 2, 1, 3, 5, 7, 4, 5, 5, 5, 2, 181, 32, 4, 32, 3, 2, 32, 324, 4, 3]),10); assert.deepEqual(candidate([1, 0, 1, 8, 2, 4597, 2, 1, 3, 40, 1, 2, 1, 2, 4, 2, 5, 1]),25); assert.deepEqual(candidate([1, 3, 1, 32, 5107, 34, 83278, 109, 163, 23, 2323, 32, 30, 1, 9, 3]),13); assert.deepEqual(candidate([0, 724, 32, 71, 99, 32, 6, 0, 5, 91, 83, 0, 5, 6]),11); assert.deepEqual(candidate([0, 81, 12, 3, 1, 21]),3); assert.deepEqual(candidate([0, 8, 1, 2, 1, 7]),7); assert.deepEqual(candidate([8191]),19); assert.deepEqual(candidate([8191, 123456, 127, 7]),19); assert.deepEqual(candidate([127, 97, 8192]),10); } test();
humaneval-HumanEval_31_is_prime.json-L25
js
//Return true if a given number is prime, and false otherwise. // >>> is_prime(6) // false // >>> is_prime(101) // true // >>> is_prime(11) // true // >>> is_prime(13441) // true // >>> is_prime(61) // true // >>> is_prime(4) // false // >>> is_prime(1) // false function is_prime(n){ if (n==1) { return false; } if (n==2) { return true; }
return false; } var limit = Math.sqrt(n); for (var i = 3; i <= limit; i += 2) { if (n%i==0) { return false; } } return true; }
if (n%2==0) {
const assert = require('node:assert'); function test() { let candidate = is_prime; assert.deepEqual(candidate(6),false); assert.deepEqual(candidate(101),true); assert.deepEqual(candidate(11),true); assert.deepEqual(candidate(13441),true); assert.deepEqual(candidate(61),true); assert.deepEqual(candidate(4),false); assert.deepEqual(candidate(1),false); assert.deepEqual(candidate(5),true); assert.deepEqual(candidate(11),true); assert.deepEqual(candidate(17),true); assert.deepEqual(candidate(85),false); assert.deepEqual(candidate(77),false); assert.deepEqual(candidate(255379),false); } test();
humaneval-HumanEval_110_exchange.json-L36
js
//In this problem, you will implement a function that takes two arrays of numbers, // and determines whether it is possible to perform an exchange of elements // between them to make lst1 an array of only even numbers. // There is no limit on the number of exchanged elements between lst1 and lst2. // If it is possible to exchange elements between the lst1 and lst2 to make // all the elements of lst1 to be even, return "YES". // Otherwise, return "NO". // For example: // >>> exchange([1, 2, 3, 4], [1, 2, 3, 4]) // "YES" // >>> exchange([1, 2, 3, 4], [1, 5, 3, 4]) // "NO" // It is assumed that the input arrays will be non-empty. function exchange(lst1, lst2){ var num1 = 0; var num2 = 0; var exchange = true; while(num1 < lst1.length && exchange){ if(lst1[num1] % 2 !== 0){ exchange = false; if(num2 < lst2.length){ while(num2 < lst2.length && exchange == false){ if(lst2[num2] % 2 == 0){ lst1[num1] = lst2[num2]; exchange = true; } num2++; } } } num1++; } if(exchange){ return "YES"; }else{
} }
return "NO";
const assert = require('node:assert'); function test() { let candidate = exchange; assert.deepEqual(candidate([1, 2, 3, 4], [1, 2, 3, 4]),"YES"); assert.deepEqual(candidate([1, 2, 3, 4], [1, 5, 3, 4]),"NO"); assert.deepEqual(candidate([1, 2, 3, 4], [2, 1, 4, 3]),"YES"); assert.deepEqual(candidate([5, 7, 3], [2, 6, 4]),"YES"); assert.deepEqual(candidate([5, 7, 3], [2, 6, 3]),"NO"); assert.deepEqual(candidate([3, 2, 6, 1, 8, 9], [3, 5, 5, 1, 1, 1]),"NO"); assert.deepEqual(candidate([100, 200], [200, 200]),"YES"); } test();
humaneval-HumanEval_137_compare_one.json-L42
py
from typing import Union def compare_one(a: Union[int, float, str], b: Union[int, float, str]) -> Union[int, float, str, None]: """ Create a function that takes integers, floats, or strings representing real numbers, and returns the larger variable in its given variable type. Return None if the values are equal. Note: If a real number is represented as a string, the floating point might be . or , >>> compare_one(1, 2.5) 2.5 >>> compare_one(1, '2,3') '2,3' >>> compare_one('5,1', '6') '6' >>> compare_one('1', 1) None """ if isinstance(a, int) and isinstance(b, int): if a > b: return a elif a < b: return b else: return None elif isinstance(a, float) and isinstance(b, float): if a > b: return a elif a < b: return b else: return None elif isinstance(a, str) and isinstance(b, str): a_tmp = a.replace(',', '.') b_tmp = b.replace(',', '.') if float(a_tmp) > float(b_tmp): return a elif float(a_tmp) < float(b_tmp): return b else: return None
if a > b: return a elif a < b: return b else: return None elif isinstance(a, float) and isinstance(b, int): if a > b: return a elif a < b: return b else: return None elif isinstance(a, str) and isinstance(b, int): a_tmp = a.replace(',', '.') if float(a_tmp) > b: return a elif float(a_tmp) < b: return b else: return None elif isinstance(a, int) and isinstance(b, str): b_tmp = b.replace(',', '.') if a > float(b_tmp): return a elif a < float(b_tmp): return b else: return None elif isinstance(a, str) and isinstance(b, float): a_tmp = a.replace(',', '.') if float(a_tmp) > b: return a elif float(a_tmp) < b: return b
elif isinstance(a, int) and isinstance(b, float):
def check(candidate): assert candidate(1, 2) == 2 assert candidate(1, 2.5) == 2.5 assert candidate(2, 3) == 3 assert candidate(5, 6) == 6 assert candidate(1, '2,3') == '2,3' assert candidate('5,1', '6') == '6' assert candidate('1', '2') == '2' assert candidate('1', 1) == None def test_check(): check(compare_one) test_check()
humaneval-HumanEval_94_skjkasdkd.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 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) {
} 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; } } }
counter++;
} 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-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 { // Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers // Example // >>> isEqualToSumEven((4l)) // (false) // >>> isEqualToSumEven((6l)) // (false) // >>> isEqualToSumEven((8l)) // (true) public static boolean isEqualToSumEven(long n) { if (n < 4) { return false; } long d = 2; if (n % 2 == 1) { d = 1; } long minX = d; long maxX = n / 4; while (minX <= maxX) { long x = (minX + maxX) / 2; long y = n - 2 * x; if (y % 2 == 1) { y--;
if (2 * x + y < n) { minX = x + 1; } else if (2 * x + y > n) { maxX = x - 1; } else { return true; } } return false; } }
}
} public static void main(String[] args) { assert(isEqualToSumEven((4l)) == (false)); assert(isEqualToSumEven((6l)) == (false)); assert(isEqualToSumEven((8l)) == (true)); assert(isEqualToSumEven((10l)) == (true)); assert(isEqualToSumEven((11l)) == (false)); assert(isEqualToSumEven((12l)) == (true)); assert(isEqualToSumEven((13l)) == (false)); assert(isEqualToSumEven((16l)) == (true)); } }
humaneval-HumanEval_19_sort_numbers.json-L20
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Input 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 // >>> sortNumbers(("three one five")) // ("one three five") public static String sortNumbers(String numbers) { String [] nums = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; HashMap<String, Integer> indexMap = new HashMap<>(); for (int i = 0; i < nums.length; i++) { indexMap.put(nums[i], i); }
Arrays.sort(split, new Comparator<String>() { @Override public int compare(String s1, String s2) { return indexMap.get(s1) - indexMap.get(s2); } }); return String.join(" ", split); } }
String [] split = numbers.split(" ");
} public static void main(String[] args) { assert(sortNumbers(("")).equals((""))); assert(sortNumbers(("three")).equals(("three"))); assert(sortNumbers(("three five nine")).equals(("three five nine"))); assert(sortNumbers(("five zero four seven nine eight")).equals(("zero four five seven eight nine"))); assert(sortNumbers(("six five four three two one zero")).equals(("zero one two three four five six"))); } }
humaneval-HumanEval_39_prime_fib.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 { // prime_fib returns n-th number that is a Fibonacci number and it's also prime. // >>> primeFib((1l)) // (2l) // >>> primeFib((2l)) // (3l) // >>> primeFib((3l)) // (5l) // >>> primeFib((4l)) // (13l) // >>> primeFib((5l)) // (89l) public static long primeFib(long n) { long fib = 1; long prev = 0; long count = 0; while (count != n) { long tmp = fib; fib += prev; prev = tmp; if (BigInteger.valueOf(fib).isProbablePrime(1)) { count++; } }
} }
return fib;
} public static void main(String[] args) { assert(primeFib((1l)) == (2l)); assert(primeFib((2l)) == (3l)); assert(primeFib((3l)) == (5l)); assert(primeFib((4l)) == (13l)); assert(primeFib((5l)) == (89l)); assert(primeFib((6l)) == (233l)); assert(primeFib((7l)) == (1597l)); assert(primeFib((8l)) == (28657l)); assert(primeFib((9l)) == (514229l)); assert(primeFib((10l)) == (433494437l)); } }
humaneval-HumanEval_85_add.json-L9
js
//Given a non-empty array of integers lst. add the even elements that are at odd indices.. // Examples: // >>> add([4, 2, 6, 7]) // 2 function add(lst){ var add = 0; for(var i = 0; i < lst.length; i++) { if(lst[i] % 2 == 0 && i % 2 != 0) {
} } return add; }
add += lst[i];
const assert = require('node:assert'); function test() { let candidate = add; assert.deepEqual(candidate([4, 88]),88); assert.deepEqual(candidate([4, 5, 6, 7, 2, 122]),122); assert.deepEqual(candidate([4, 0, 6, 7]),0); assert.deepEqual(candidate([4, 4, 6, 8]),12); } test();
humaneval-HumanEval_96_count_up_to.json-L30
py
from typing import List def count_up_to(n: int) -> List[int]: """Implement a function that takes an non-negative integer and returns an array of the first n integers that are prime numbers and less than n. for example: >>> count_up_to(5) [2, 3] >>> count_up_to(11) [2, 3, 5, 7] >>> count_up_to(0) [] >>> count_up_to(20) [2, 3, 5, 7, 11, 13, 17, 19] >>> count_up_to(1) [] >>> count_up_to(18) [2, 3, 5, 7, 11, 13, 17] """ if n <= 1: return [] result = [] for number in range(2, n): is_prime = True for divisor in range(2, number): if number % divisor == 0: is_prime = False break if is_prime:
return result
result.append(number)
def check(candidate): assert candidate(5) == [2, 3] assert candidate(6) == [2, 3, 5] assert candidate(7) == [2, 3, 5] assert candidate(10) == [2, 3, 5, 7] assert candidate(0) == [] assert candidate(22) == [2, 3, 5, 7, 11, 13, 17, 19] assert candidate(1) == [] assert candidate(18) == [2, 3, 5, 7, 11, 13, 17] assert candidate(47) == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43] assert candidate(101) == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] def test_check(): check(count_up_to) test_check()
humaneval-HumanEval_121_solution.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 non-empty array list of integers, return the sum of all of the odd elements that are in even positions. // Examples // >>> solution((new ArrayList<Long>(Arrays.asList((long)5l, (long)8l, (long)7l, (long)1l)))) // (12l) // >>> solution((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l, (long)3l, (long)3l, (long)3l)))) // (9l) // >>> solution((new ArrayList<Long>(Arrays.asList((long)30l, (long)13l, (long)24l, (long)321l)))) // (0l) public static long solution(ArrayList<Long> lst) { long sum = 0; for (int i = 0; i < lst.size(); i++) {
sum += lst.get(i); } } return sum; } }
if (i % 2 == 0 && lst.get(i) % 2 == 1) {
} public static void main(String[] args) { assert(solution((new ArrayList<Long>(Arrays.asList((long)5l, (long)8l, (long)7l, (long)1l)))) == (12l)); assert(solution((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l, (long)3l, (long)3l, (long)3l)))) == (9l)); assert(solution((new ArrayList<Long>(Arrays.asList((long)30l, (long)13l, (long)24l, (long)321l)))) == (0l)); assert(solution((new ArrayList<Long>(Arrays.asList((long)5l, (long)9l)))) == (5l)); assert(solution((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)8l)))) == (0l)); assert(solution((new ArrayList<Long>(Arrays.asList((long)30l, (long)13l, (long)23l, (long)32l)))) == (23l)); assert(solution((new ArrayList<Long>(Arrays.asList((long)3l, (long)13l, (long)2l, (long)9l)))) == (3l)); } }
humaneval-HumanEval_81_numerical_letter_grade.json-L37
js
//It is the last week of the semester and the teacher has to give the grades // to students. The teacher has been making her own algorithm for grading. // The only problem is, she has lost the code she used for grading. // She has given you an array of GPAs for some students and you have to write // a function that can output an array of letter grades using the following table: // GPA | Letter grade // 4.0 A+ // > 3.7 A // > 3.3 A- // > 3.0 B+ // > 2.7 B // > 2.3 B- // > 2.0 C+ // > 1.7 C // > 1.3 C- // > 1.0 D+ // > 0.7 D // > 0.0 D- // 0.0 E // Example: // >>> grade_equation([4.0, 3, 1.7, 2, 3.5]) // ["A+", "B", "C-", "C", "A-"] function numerical_letter_grade(grades){ var grades_array = []; for (var i = 0; i < grades.length; i++){ if (grades[i] >= 4.0){ grades_array.push("A+"); } else if (grades[i] > 3.7){ grades_array.push("A"); } else if (grades[i] > 3.3){ grades_array.push("A-"); } else if (grades[i] > 3.0){ grades_array.push("B+");
else if (grades[i] > 2.7){ grades_array.push("B"); } else if (grades[i] > 2.3){ grades_array.push("B-"); } else if (grades[i] > 2.0){ grades_array.push("C+"); } else if (grades[i] > 1.7){ grades_array.push("C"); } else if (grades[i] > 1.3){ grades_array.push("C-"); } else if (grades[i] > 1.0){ grades_array.push("D+"); } else if (grades[i] > 0.7){ grades_array.push("D"); } else if (grades[i] > 0.0){ grades_array.push("D-"); } else if (grades[i] == 0.0){ grades_array.push("E"); } } return grades_array; }
}
const assert = require('node:assert'); function test() { let candidate = numerical_letter_grade; assert.deepEqual(candidate([4.0, 3, 1.7, 2, 3.5]),["A+", "B", "C-", "C", "A-"]); assert.deepEqual(candidate([1.2]),["D+"]); assert.deepEqual(candidate([0.5]),["D-"]); assert.deepEqual(candidate([0.0]),["E"]); assert.deepEqual(candidate([1.0, 0.3, 1.5, 2.8, 3.3]),["D", "D-", "C-", "B", "B+"]); assert.deepEqual(candidate([0.0, 0.7]),["E", "D-"]); } test();
humaneval-HumanEval_126_is_sorted.json-L26
js
//Given an array of numbers, return whether or not they are sorted // in ascending order. If array has more than 1 duplicate of the same // number, return false. Assume no negative numbers and only integers. // Examples // >>> is_sorted([5]) // true // >>> is_sorted([1, 2, 3, 4, 5]) // true // >>> is_sorted([1, 3, 2, 4, 5]) // false // >>> is_sorted([1, 2, 3, 4, 5, 6]) // true // >>> is_sorted([1, 2, 3, 4, 5, 6, 7]) // true // >>> is_sorted([1, 3, 2, 4, 5, 6, 7]) // false // >>> is_sorted([1, 2, 2, 3, 3, 4]) // true // >>> is_sorted([1, 2, 2, 2, 3, 4]) // false function is_sorted(lst){ var last = null; var dupe_found = false; for (var x of lst){ if (last == null){
} else if (x < last){ return false; } else if (x == last){ if (dupe_found){ return false; } else { dupe_found = true; } } else { last = x; dupe_found = false; } } return true; }
last = x;
const assert = require('node:assert'); function test() { let candidate = is_sorted; assert.deepEqual(candidate([5]),true); assert.deepEqual(candidate([1, 2, 3, 4, 5]),true); assert.deepEqual(candidate([1, 3, 2, 4, 5]),false); assert.deepEqual(candidate([1, 2, 3, 4, 5, 6]),true); assert.deepEqual(candidate([1, 2, 3, 4, 5, 6, 7]),true); assert.deepEqual(candidate([1, 3, 2, 4, 5, 6, 7]),false); assert.deepEqual(candidate([]),true); assert.deepEqual(candidate([1]),true); assert.deepEqual(candidate([3, 2, 1]),false); assert.deepEqual(candidate([1, 2, 2, 2, 3, 4]),false); assert.deepEqual(candidate([1, 2, 3, 3, 3, 4]),false); assert.deepEqual(candidate([1, 2, 2, 3, 3, 4]),true); assert.deepEqual(candidate([1, 2, 3, 4]),true); } test();
humaneval-HumanEval_147_get_max_triples.json-L18
py
def get_max_triples(n: int) -> int: """ You are given a positive integer n. You have to create an integer array 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 : >>> get_max_triples(5) 1 Explanation: a = [1, 3, 7, 13, 21] The only valid triple is (1, 7, 13). """ a = [i * i - i + 1 for i in range(1, n + 1)] i = 0 j = 1
count = 0 while (i < n - 2): while (j < n - 1): while (k < n): if (a[i] + a[j] + a[k]) % 3 == 0: count += 1 k += 1 j += 1 k = j + 1 i += 1 j = i + 1 k = j + 1 return count
k = 2
def check(candidate): assert candidate(5) == 1 assert candidate(6) == 4 assert candidate(10) == 36 assert candidate(100) == 53361 def test_check(): check(get_max_triples) test_check()
humaneval-HumanEval_93_encode.json-L29
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 a message, and encodes in such a // way that it swaps case of all letters, replaces all vowels in // the message with the letter that appears 2 places ahead of that // vowel in the english alphabet. // Assume only letters. // Examples: // >>> encode(("test")) // ("TGST") // >>> encode(("This is a message")) // ("tHKS KS C MGSSCGG") public static String encode(String message) { char[] arr = message.toCharArray(); char[] vowels = "AEIOUaeiou".toCharArray(); for (int i = 0; i < arr.length; i++) { if (Arrays.binarySearch(vowels, arr[i]) > -1) { arr[i] = (char) (arr[i] + 2); } if (Character.isUpperCase(arr[i])) { arr[i] = Character.toLowerCase(arr[i]); } else {
} } return new String(arr); } }
arr[i] = Character.toUpperCase(arr[i]);
} public static void main(String[] args) { assert(encode(("TEST")).equals(("tgst"))); assert(encode(("Mudasir")).equals(("mWDCSKR"))); assert(encode(("YES")).equals(("ygs"))); assert(encode(("This is a message")).equals(("tHKS KS C MGSSCGG"))); assert(encode(("I DoNt KnOw WhAt tO WrItE")).equals(("k dQnT kNqW wHcT Tq wRkTg"))); } }
humaneval-HumanEval_94_skjkasdkd.json-L27
js
//You are given an array of integers. // You need to find the largest prime value and return the sum of its digits. // Examples: // >>> skjkasdkd([0, 3, 2, 1, 3, 5, 7, 4, 5, 5, 5, 2, 181, 32, 4, 32, 3, 2, 32, 324, 4, 3]) // 10 // >>> skjkasdkd([1, 0, 1, 8, 2, 4597, 2, 1, 3, 40, 1, 2, 1, 2, 4, 2, 5, 1]) // 25 // >>> skjkasdkd([1, 3, 1, 32, 5107, 34, 83278, 109, 163, 23, 2323, 32, 30, 1, 9, 3]) // 13 // >>> skjkasdkd([0, 724, 32, 71, 99, 32, 6, 0, 5, 91, 83, 0, 5, 6]) // 11 // >>> skjkasdkd([0, 81, 12, 3, 1, 21]) // 3 // >>> skjkasdkd([0, 8, 1, 2, 1, 7]) // 7 function skjkasdkd(lst){ function prime(n){ if (n<2) return false; for (let i=2; i<=Math.sqrt(n); i++){ if (n%i==0) return false; } return true; } let max = Math.max(...lst.filter(x=>prime(x))); let digits = String(max).split(''); let sum = 0;
sum += Number(digits[i]); } return sum; }
for (let i=0; i<digits.length; i++){
const assert = require('node:assert'); function test() { let candidate = skjkasdkd; assert.deepEqual(candidate([0, 3, 2, 1, 3, 5, 7, 4, 5, 5, 5, 2, 181, 32, 4, 32, 3, 2, 32, 324, 4, 3]),10); assert.deepEqual(candidate([1, 0, 1, 8, 2, 4597, 2, 1, 3, 40, 1, 2, 1, 2, 4, 2, 5, 1]),25); assert.deepEqual(candidate([1, 3, 1, 32, 5107, 34, 83278, 109, 163, 23, 2323, 32, 30, 1, 9, 3]),13); assert.deepEqual(candidate([0, 724, 32, 71, 99, 32, 6, 0, 5, 91, 83, 0, 5, 6]),11); assert.deepEqual(candidate([0, 81, 12, 3, 1, 21]),3); assert.deepEqual(candidate([0, 8, 1, 2, 1, 7]),7); assert.deepEqual(candidate([8191]),19); assert.deepEqual(candidate([8191, 123456, 127, 7]),19); assert.deepEqual(candidate([127, 97, 8192]),10); } test();
humaneval-HumanEval_160_do_algebra.json-L39
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;
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; }
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_49_modp.json-L17
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:
return 1 if n == 0 else 2**(n % (p-1)) % p
raise ValueError("modp: invalid n")
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_52_below_threshold.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 { // Return true if all numbers in the array list l are below threshold t. // >>> belowThreshold((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)4l, (long)10l))), (100l)) // (true) // >>> belowThreshold((new ArrayList<Long>(Arrays.asList((long)1l, (long)20l, (long)4l, (long)10l))), (5l)) // (false) public static boolean belowThreshold(ArrayList<Long> l, long t) { Long[] a = l.toArray(new Long[l.size()]); for (int i = 0; i < a.length; i++) { if (a[i] >= t) { return false; } } return true; }
}
} public static void main(String[] args) { assert(belowThreshold((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)4l, (long)10l))), (100l)) == (true)); assert(belowThreshold((new ArrayList<Long>(Arrays.asList((long)1l, (long)20l, (long)4l, (long)10l))), (5l)) == (false)); assert(belowThreshold((new ArrayList<Long>(Arrays.asList((long)1l, (long)20l, (long)4l, (long)10l))), (21l)) == (true)); assert(belowThreshold((new ArrayList<Long>(Arrays.asList((long)1l, (long)20l, (long)4l, (long)10l))), (22l)) == (true)); assert(belowThreshold((new ArrayList<Long>(Arrays.asList((long)1l, (long)8l, (long)4l, (long)10l))), (11l)) == (true)); assert(belowThreshold((new ArrayList<Long>(Arrays.asList((long)1l, (long)8l, (long)4l, (long)10l))), (10l)) == (false)); } }
humaneval-HumanEval_140_fix_spaces.json-L30
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given a string text, replace all spaces in it with underscores, // and if a string has more than 2 consecutive spaces, // then replace all consecutive spaces with - // >>> fixSpaces((" Example")) // ("Example") // >>> fixSpaces((" Example 1")) // ("Example_1") // >>> fixSpaces((" Example 2")) // ("_Example_2") // >>> fixSpaces((" Example 3")) // ("_Example-3") public static String fixSpaces(String text) { String result = text.replace(" ", "_"); int i = 0; while (i < result.length() - 2) { if (result.charAt(i) == '_' && result.charAt(i + 1) == '_' && result.charAt(i + 2) == '_') { result = result.substring(0, i) + "-" + result.substring(i + 3); } i++; } return result;
}
}
} public static void main(String[] args) { assert(fixSpaces(("Example")).equals(("Example"))); assert(fixSpaces(("Mudasir Hanif ")).equals(("Mudasir_Hanif_"))); assert(fixSpaces(("Yellow Yellow Dirty Fellow")).equals(("Yellow_Yellow__Dirty__Fellow"))); assert(fixSpaces(("Exa mple")).equals(("Exa-mple"))); assert(fixSpaces((" Exa 1 2 2 mple")).equals(("-Exa_1_2_2_mple"))); } }
humaneval-HumanEval_131_digits.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 n, return the product of the odd digits. // Return 0 if all digits are even. // For example: // >>> digits((1l)) // (1l) // >>> digits((4l)) // (0l) // >>> digits((235l)) // (15l) public static long digits(long n) { if (n < 10) {
} return digits(n / 10) * (n % 2 == 1 ? n % 10 : 1); } }
return n % 2 == 1 ? n : 0;
} public static void main(String[] args) { assert(digits((5l)) == (5l)); assert(digits((54l)) == (5l)); assert(digits((120l)) == (1l)); assert(digits((5014l)) == (5l)); assert(digits((98765l)) == (315l)); assert(digits((5576543l)) == (2625l)); assert(digits((2468l)) == (0l)); } }
humaneval-HumanEval_160_do_algebra.json-L55
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){
case '+': answer += operand[0]; break; case '-': answer -= operand[0]; break; } operand.splice(0, 1); operator.splice(0, 1); } return answer; }
switch(operator[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_129_minPath.json-L43
py
from typing import List def minPath(grid: List[List[int]], k: int) -> List[int]: """ 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 lists 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 list 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] """ min_val = float('inf') for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] < min_val: min_val = grid[i][j] row = i col = j path = [min_val] while len(path) < k: min_val = float('inf') for i, j in (row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1):
min_val = grid[i][j] row = i col = j path.append(min_val) return path
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] < min_val:
def check(candidate): assert candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3) == [1, 2, 1] assert candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1) == [1] assert candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4) == [1, 2, 1, 2] assert 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 candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5) == [1, 7, 1, 7, 1] assert 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 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 candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8) == [1, 3, 1, 3, 1, 3, 1, 3] assert candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8) == [1, 5, 1, 5, 1, 5, 1, 5] assert candidate([[1, 2], [3, 4]], 10) == [1, 2, 1, 2, 1, 2, 1, 2, 1, 2] assert candidate([[1, 3], [3, 2]], 10) == [1, 3, 1, 3, 1, 3, 1, 3, 1, 3] def test_check(): check(minPath) test_check()
humaneval-HumanEval_155_even_odd_count.json-L15
js
//Given an integer. return an array that has the number of even and odd digits respectively. // Example: // >>> even_odd_count(-12) // [1, 1] // >>> even_odd_count(123) // [1, 2] function even_odd_count(num){ num = Math.abs(num); var evens = 0; var odds = 0; for(var i = 0; i < num.toString().length; i++){ if(num.toString()[i] % 2 == 0){ evens ++; }
odds ++; } } return [evens, odds]; }
else{
const assert = require('node:assert'); function test() { let candidate = even_odd_count; assert.deepEqual(candidate(7),[0, 1]); assert.deepEqual(candidate(-78),[1, 1]); assert.deepEqual(candidate(3452),[2, 2]); assert.deepEqual(candidate(346211),[3, 3]); assert.deepEqual(candidate(-345821),[3, 3]); assert.deepEqual(candidate(-2),[1, 0]); assert.deepEqual(candidate(-45347),[2, 3]); assert.deepEqual(candidate(0),[1, 0]); } test();
humaneval-HumanEval_152_compare.json-L21
js
//I think we all remember that feeling when the result of some long-awaited // event is finally known. The feelings and thoughts you have at that moment are // definitely worth noting down and comparing. // Your task is to determine if a person correctly guessed the results of a number of matches. // You are given two arrays of scores and guesses of equal length, where each index shows a match. // Return an array of the same length denoting how far off each guess was. If they have guessed correctly, // the value is 0, and if not, the value is the absolute difference between the guess and the score. // example: // >>> compare([1, 2, 3, 4, 5, 1], [1, 2, 3, 4, 2, -2]) // [0, 0, 0, 0, 3, 3] // >>> compare([0, 5, 0, 0, 0, 4], [4, 1, 1, 0, 0, -2]) // [4, 4, 1, 0, 0, 6] function compare(game, guess){ let result = []; for(let i = 0; i < game.length; i++){ if(game[i] === guess[i]){ result.push(0); } else { result.push(Math.abs(game[i] - guess[i])); }
return result; }
}
const assert = require('node:assert'); function test() { let candidate = compare; assert.deepEqual(candidate([1, 2, 3, 4, 5, 1], [1, 2, 3, 4, 2, -2]),[0, 0, 0, 0, 3, 3]); assert.deepEqual(candidate([0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]),[0, 0, 0, 0, 0, 0]); assert.deepEqual(candidate([1, 2, 3], [-1, -2, -3]),[2, 4, 6]); assert.deepEqual(candidate([1, 2, 3, 5], [-1, 2, 3, 4]),[2, 0, 0, 1]); } test();
humaneval-HumanEval_94_skjkasdkd.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 { // 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++; }
} } 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; } } }
l++;
} 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_10_make_palindrome.json-L23
js
//Find the shortest palindrome that begins with a supplied string. // Algorithm idea is simple: // - Find the longest postfix of supplied string that is a palindrome. // - Append to the end of the string reverse of a string prefix that comes before the palindromic suffix. // >>> make_palindrome("") // "" // >>> make_palindrome("cat") // "catac" // >>> make_palindrome("cata") // "catac" function make_palindrome(string){ var string_length = string.length; var is_palindrome = function(s){ return s == s.split("").reverse().join(""); }; var i = string_length; while(i > 0){ if(is_palindrome(string.substring(0, i))){ return string + string.substring(0, string_length - i).split("").reverse().join(""); } i--; }
}
return string;
const assert = require('node:assert'); function test() { let candidate = make_palindrome; assert.deepEqual(candidate(""),""); assert.deepEqual(candidate("x"),"x"); assert.deepEqual(candidate("xyz"),"xyzyx"); assert.deepEqual(candidate("xyx"),"xyx"); assert.deepEqual(candidate("jerry"),"jerryrrej"); } test();
humaneval-HumanEval_67_fruit_distribution.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 { // In this task, you will be given a string that represents a number of apples and oranges // that are distributed in a basket of fruit this basket contains // apples, oranges, and mango fruits. Given the string that represents the total number of // the oranges and apples and an integer that represent the total number of the fruits // in the basket return the number of the mango fruits in the basket. // for examble: // >>> fruitDistribution(("5 apples and 6 oranges"), (19l)) // (8l) // >>> fruitDistribution(("0 apples and 1 oranges"), (3l)) // (2l) // >>> fruitDistribution(("2 apples and 3 oranges"), (100l)) // (95l) // >>> fruitDistribution(("100 apples and 1 oranges"), (120l)) // (19l) public static long fruitDistribution(String s, long n) {
long apples = Long.parseLong(splitted[0]); long oranges = Long.parseLong(splitted[3]); return n - apples - oranges; } }
String[] splitted = s.split(" ");
} public static void main(String[] args) { assert(fruitDistribution(("5 apples and 6 oranges"), (19l)) == (8l)); assert(fruitDistribution(("5 apples and 6 oranges"), (21l)) == (10l)); assert(fruitDistribution(("0 apples and 1 oranges"), (3l)) == (2l)); assert(fruitDistribution(("1 apples and 0 oranges"), (3l)) == (2l)); assert(fruitDistribution(("2 apples and 3 oranges"), (100l)) == (95l)); assert(fruitDistribution(("2 apples and 3 oranges"), (5l)) == (0l)); assert(fruitDistribution(("1 apples and 100 oranges"), (120l)) == (19l)); } }
humaneval-HumanEval_141_file_name_check.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 { // Create a function which takes a string representing a file's name, and returns // 'Yes' if the the file's name is valid, and returns 'No' otherwise. // A file's name is considered to be valid if and only if all the following conditions // are met: // - There should not be more than three digits ('0'-'9') in the file's name. // - The file's name contains exactly one dot '.' // - The substring before the dot should not be empty, and it starts with a letter from // the latin alphapet ('a'-'z' and 'A'-'Z'). // - The substring after the dot should be one of these: ['txt', 'exe', 'dll'] // Examples: // >>> fileNameCheck(("example.txt")) // ("Yes") // >>> fileNameCheck(("1example.dll")) // ("No") public static String fileNameCheck(String file_name) { String result = "No"; String[] tokens = file_name.split("\\."); if (tokens.length != 2) { return result; } String extension = tokens[1]; if (!(extension.equals("txt") || extension.equals("exe") || extension.equals("dll"))) { return result; } String name = tokens[0]; int count = 0; for (int i = 0; i < name.length(); i++) { if (Character.isDigit(name.charAt(i))) { count++; } } if (count > 3) { return result; } if (name.length() == 0) { return result; } if (!Character.isLetter(name.charAt(0))) {
} return "Yes"; } }
return result;
} public static void main(String[] args) { assert(fileNameCheck(("example.txt")).equals(("Yes"))); assert(fileNameCheck(("1example.dll")).equals(("No"))); assert(fileNameCheck(("s1sdf3.asd")).equals(("No"))); assert(fileNameCheck(("K.dll")).equals(("Yes"))); assert(fileNameCheck(("MY16FILE3.exe")).equals(("Yes"))); assert(fileNameCheck(("His12FILE94.exe")).equals(("No"))); assert(fileNameCheck(("_Y.txt")).equals(("No"))); assert(fileNameCheck(("?aREYA.exe")).equals(("No"))); assert(fileNameCheck(("/this_is_valid.dll")).equals(("No"))); assert(fileNameCheck(("this_is_valid.wow")).equals(("No"))); assert(fileNameCheck(("this_is_valid.txt")).equals(("Yes"))); assert(fileNameCheck(("this_is_valid.txtexe")).equals(("No"))); assert(fileNameCheck(("#this2_i4s_5valid.ten")).equals(("No"))); assert(fileNameCheck(("@this1_is6_valid.exe")).equals(("No"))); assert(fileNameCheck(("this_is_12valid.6exe4.txt")).equals(("No"))); assert(fileNameCheck(("all.exe.txt")).equals(("No"))); assert(fileNameCheck(("I563_No.exe")).equals(("Yes"))); assert(fileNameCheck(("Is3youfault.txt")).equals(("Yes"))); assert(fileNameCheck(("no_one#knows.dll")).equals(("Yes"))); assert(fileNameCheck(("1I563_Yes3.exe")).equals(("No"))); assert(fileNameCheck(("I563_Yes3.txtt")).equals(("No"))); assert(fileNameCheck(("final..txt")).equals(("No"))); assert(fileNameCheck(("final132")).equals(("No"))); assert(fileNameCheck(("_f4indsartal132.")).equals(("No"))); assert(fileNameCheck((".txt")).equals(("No"))); assert(fileNameCheck(("s.")).equals(("No"))); } }
humaneval-HumanEval_103_rounded_avg.json-L19
py
from typing import Union def rounded_avg(n: int, m: int) -> Union[str, int]: """You are given two positive integers n and m, and your task is to compute the average of the integers from n through m (including n and m). Round the answer to the nearest integer and convert that to binary. If n is greater than m, return -1. Example: >>> rounded_avg(1, 5) '0b11' >>> rounded_avg(7, 5) -1 >>> rounded_avg(10, 20) '0b1111' >>> rounded_avg(20, 33) '0b11010' """ if n <= m:
return(bin(rounded_avg)) else: return(-1)
rounded_avg = round(((n + m) / 2))
def check(candidate): assert candidate(1, 5) == '0b11' assert candidate(7, 13) == '0b1010' assert candidate(964, 977) == '0b1111001010' assert candidate(996, 997) == '0b1111100100' assert candidate(560, 851) == '0b1011000010' assert candidate(185, 546) == '0b101101110' assert candidate(362, 496) == '0b110101101' assert candidate(350, 902) == '0b1001110010' assert candidate(197, 233) == '0b11010111' assert candidate(7, 5) == -1 assert candidate(5, 1) == -1 assert candidate(5, 5) == '0b101' def test_check(): check(rounded_avg) test_check()
humaneval-HumanEval_26_remove_duplicates.json-L11
js
//From an array of integers, remove all elements that occur more than once. // Keep order of elements left the same as in the input. // >>> remove_duplicates([1, 2, 3, 2, 4]) // [1, 3, 4] function remove_duplicates(numbers){ let result = []; numbers.forEach(function(value){ if(numbers.indexOf(value) === numbers.lastIndexOf(value)){ result.push(value); }
return result; }
});
const assert = require('node:assert'); function test() { let candidate = remove_duplicates; assert.deepEqual(candidate([]),[]); assert.deepEqual(candidate([1, 2, 3, 4]),[1, 2, 3, 4]); assert.deepEqual(candidate([1, 2, 3, 2, 4, 3, 5]),[1, 4, 5]); } test();
humaneval-HumanEval_156_int_to_mini_roman.json-L78
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) { 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; }
}
} 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_44_change_base.json-L17
py
def change_base(x: int, base: int) -> str: """Change numerical base of input number x to base. return string representation after the conversion. base numbers are less than 10. >>> change_base(8, 3) '22' >>> change_base(8, 2) '1000' >>> change_base(7, 2) '111' """ if x == 0: return '0' ans = [] while x != 0: ans.append(str(x % base))
return ''.join(ans[::-1])
x //= base
def check(candidate): assert candidate(8, 3) == '22' assert candidate(9, 3) == '100' assert candidate(234, 2) == '11101010' assert candidate(16, 2) == '10000' assert candidate(8, 2) == '1000' assert candidate(7, 2) == '111' assert candidate(2, 3) == '2' assert candidate(3, 4) == '3' assert candidate(4, 5) == '4' assert candidate(5, 6) == '5' assert candidate(6, 7) == '6' assert candidate(7, 8) == '7' def test_check(): check(change_base) test_check()
humaneval-HumanEval_141_file_name_check.json-L22
py
def file_name_check(file_name: str) -> str: """Create a function which takes a string representing a file's name, and returns 'Yes' if the the file's name is valid, and returns 'No' otherwise. A file's name is considered to be valid if and only if all the following conditions are met: - There should not be more than three digits ('0'-'9') in the file's name. - The file's name contains exactly one dot '.' - The substring before the dot should not be empty, and it starts with a letter from the latin alphapet ('a'-'z' and 'A'-'Z'). - The substring after the dot should be one of these: ['txt', 'exe', 'dll'] Examples: >>> file_name_check('example.txt') 'Yes' >>> file_name_check('1example.dll') 'No' """ if not file_name: return 'No' s_count = 0 e_count = 0 for i in file_name:
s_count += 1 if i == '.': e_count += 1 if s_count > 3 or e_count != 1: return 'No' s_name = file_name.split('.')[0] e_name = file_name.split('.')[1] if not s_name or not e_name or not s_name[0].isalpha(): return 'No' if e_name not in ['txt', 'exe', 'dll']: return 'No' return 'Yes'
if i.isdigit():
def check(candidate): assert candidate('example.txt') == 'Yes' assert candidate('1example.dll') == 'No' assert candidate('s1sdf3.asd') == 'No' assert candidate('K.dll') == 'Yes' assert candidate('MY16FILE3.exe') == 'Yes' assert candidate('His12FILE94.exe') == 'No' assert candidate('_Y.txt') == 'No' assert candidate('?aREYA.exe') == 'No' assert candidate('/this_is_valid.dll') == 'No' assert candidate('this_is_valid.wow') == 'No' assert candidate('this_is_valid.txt') == 'Yes' assert candidate('this_is_valid.txtexe') == 'No' assert candidate('#this2_i4s_5valid.ten') == 'No' assert candidate('@this1_is6_valid.exe') == 'No' assert candidate('this_is_12valid.6exe4.txt') == 'No' assert candidate('all.exe.txt') == 'No' assert candidate('I563_No.exe') == 'Yes' assert candidate('Is3youfault.txt') == 'Yes' assert candidate('no_one#knows.dll') == 'Yes' assert candidate('1I563_Yes3.exe') == 'No' assert candidate('I563_Yes3.txtt') == 'No' assert candidate('final..txt') == 'No' assert candidate('final132') == 'No' assert candidate('_f4indsartal132.') == 'No' assert candidate('.txt') == 'No' assert candidate('s.') == 'No' def test_check(): check(file_name_check) test_check()
humaneval-HumanEval_90_next_smallest.json-L26
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]) min2 = max(lst[0], lst[1]) for i in range(2, len(lst)): if lst[i] < min1: min2 = min1 min1 = lst[i] elif lst[i] < min2:
return min2 if min1 != min2 else None
min2 = lst[i]
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_131_digits.json-L25
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_6_parse_nested_parens.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 { // Input to this function is a string represented multiple groups for nested parentheses separated by spaces. // For each of the group, output the deepest level of nesting of parentheses. // E.g. (()()) has maximum two levels of nesting while ((())) has three. // >>> parseNestedParens(("(()()) ((())) () ((())()())")) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)1l, (long)3l))) public static ArrayList<Long> parseNestedParens(String paren_string) { ArrayList<Long> result = new ArrayList<Long>(); String[] paren_strings = paren_string.split(" "); for (String paren : paren_strings) { long max_depth = 0; long current_depth = 0; for (char c : paren.toCharArray()) { if (c == '(') { current_depth += 1; max_depth = Math.max(max_depth, current_depth); } else if (c == ')') { current_depth -= 1; } } result.add(max_depth); } return result; }
}
} public static void main(String[] args) { assert(parseNestedParens(("(()()) ((())) () ((())()())")).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)1l, (long)3l))))); assert(parseNestedParens(("() (()) ((())) (((())))")).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l))))); assert(parseNestedParens(("(()(())((())))")).equals((new ArrayList<Long>(Arrays.asList((long)4l))))); } }
humaneval-HumanEval_105_by_length.json-L18
js
//Given an array of integers, sort the integers that are between 1 and 9 inclusive, // reverse the resulting array, and then replace each digit by its corresponding name from // "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine". // For example: // >>> by_length([2, 1, 1, 4, 5, 8, 2, 3]) // ["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"] // If the array is empty, return an empty array: // >>> by_length([]) // [] // If the array has any strange number ignore it: // >>> by_length([1, -1, 55]) // ["One"] function by_length(arr){ var arr2 = []; for(var i = 0; i < arr.length; i++){ var num = arr[i]; if(num > 0 && num < 10){
} } arr2.sort(); arr2.reverse(); var arr3 = []; for(var i = 0; i < arr2.length; i++){ var num = arr2[i]; if(num == 1){ arr3.push("One"); } else if(num == 2){ arr3.push("Two"); } else if(num == 3){ arr3.push("Three"); } else if(num == 4){ arr3.push("Four"); } else if(num == 5){ arr3.push("Five"); } else if(num == 6){ arr3.push("Six"); } else if(num == 7){ arr3.push("Seven"); } else if(num == 8){ arr3.push("Eight"); } else if(num == 9){ arr3.push("Nine"); } } return arr3; }
arr2.push(num);
const assert = require('node:assert'); function test() { let candidate = by_length; assert.deepEqual(candidate([2, 1, 1, 4, 5, 8, 2, 3]),["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"]); assert.deepEqual(candidate([]),[]); assert.deepEqual(candidate([1, -1, 55]),["One"]); assert.deepEqual(candidate([1, -1, 3, 2]),["Three", "Two", "One"]); assert.deepEqual(candidate([9, 4, 8]),["Nine", "Eight", "Four"]); } test();
humaneval-HumanEval_33_sort_third.json-L35
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(j, l_e.get(i)); 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; } }
}
} 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_81_numerical_letter_grade.json-L57
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: final.append('C-') 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
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_105_by_length.json-L35
js
//Given an array of integers, sort the integers that are between 1 and 9 inclusive, // reverse the resulting array, and then replace each digit by its corresponding name from // "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine". // For example: // >>> by_length([2, 1, 1, 4, 5, 8, 2, 3]) // ["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"] // If the array is empty, return an empty array: // >>> by_length([]) // [] // If the array has any strange number ignore it: // >>> by_length([1, -1, 55]) // ["One"] function by_length(arr){ var arr2 = []; for(var i = 0; i < arr.length; i++){ var num = arr[i]; if(num > 0 && num < 10){ arr2.push(num); } } arr2.sort(); arr2.reverse(); var arr3 = []; for(var i = 0; i < arr2.length; i++){ var num = arr2[i]; if(num == 1){ arr3.push("One"); } else if(num == 2){ arr3.push("Two"); } else if(num == 3){ arr3.push("Three"); }
arr3.push("Four"); } else if(num == 5){ arr3.push("Five"); } else if(num == 6){ arr3.push("Six"); } else if(num == 7){ arr3.push("Seven"); } else if(num == 8){ arr3.push("Eight"); } else if(num == 9){ arr3.push("Nine"); } } return arr3; }
else if(num == 4){
const assert = require('node:assert'); function test() { let candidate = by_length; assert.deepEqual(candidate([2, 1, 1, 4, 5, 8, 2, 3]),["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"]); assert.deepEqual(candidate([]),[]); assert.deepEqual(candidate([1, -1, 55]),["One"]); assert.deepEqual(candidate([1, -1, 3, 2]),["Three", "Two", "One"]); assert.deepEqual(candidate([9, 4, 8]),["Nine", "Eight", "Four"]); } test();
humaneval-HumanEval_89_encrypt.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 { // Create a function encrypt that takes a string as an argument and // returns a string encrypted with the alphabet being rotated. // The alphabet should be rotated in a manner such that the letters // shift down by two multiplied to two places. // For example: // >>> encrypt(("hi")) // ("lm") // >>> encrypt(("asdfghjkl")) // ("ewhjklnop") // >>> encrypt(("gf")) // ("kj") // >>> encrypt(("et")) // ("ix") public static String encrypt(String s) {
for (char c : s.toCharArray()) { b.append((char)((c+4) > 'z' ? (((c+4)%'z')+'a'-1) : c+4)); } return b.toString(); } }
StringBuffer b = new StringBuffer();
} public static void main(String[] args) { assert(encrypt(("hi")).equals(("lm"))); assert(encrypt(("asdfghjkl")).equals(("ewhjklnop"))); assert(encrypt(("gf")).equals(("kj"))); assert(encrypt(("et")).equals(("ix"))); assert(encrypt(("faewfawefaewg")).equals(("jeiajeaijeiak"))); assert(encrypt(("hellomyfriend")).equals(("lippsqcjvmirh"))); assert(encrypt(("dxzdlmnilfuhmilufhlihufnmlimnufhlimnufhfucufh")).equals(("hbdhpqrmpjylqmpyjlpmlyjrqpmqryjlpmqryjljygyjl"))); assert(encrypt(("a")).equals(("e"))); } }
humaneval-HumanEval_154_cycpattern_check.json-L28
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 2 words. You need to return true if the second word or any of its rotations is a substring in the first word // >>> cycpatternCheck(("abcd"), ("abd")) // (false) // >>> cycpatternCheck(("hello"), ("ell")) // (true) // >>> cycpatternCheck(("whassup"), ("psus")) // (false) // >>> cycpatternCheck(("abab"), ("baa")) // (true) // >>> cycpatternCheck(("efef"), ("eeff")) // (false) // >>> cycpatternCheck(("himenss"), ("simen")) // (true) public static boolean cycpatternCheck(String a, String b) { int len = b.length(); boolean found = false; for (int i = 0; i < len; i++) { b = b.substring(1) + b.charAt(0); found = a.contains(b);
} return found; } }
if (found) break;
} public static void main(String[] args) { assert(cycpatternCheck(("xyzw"), ("xyw")) == (false)); assert(cycpatternCheck(("yello"), ("ell")) == (true)); assert(cycpatternCheck(("whattup"), ("ptut")) == (false)); assert(cycpatternCheck(("efef"), ("fee")) == (true)); assert(cycpatternCheck(("abab"), ("aabb")) == (false)); assert(cycpatternCheck(("winemtt"), ("tinem")) == (true)); } }
humaneval-HumanEval_17_parse_music.json-L28
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Input to this function is a string representing musical notes in a special ASCII format. // Your task is to parse this string and return array list of integers corresponding to how many beats does each // not last. // Here is a legend: // 'o' - whole note, lasts four beats // 'o|' - half note, lasts two beats // '.|' - quater note, lasts one beat // >>> parseMusic(("o o| .| o| o| .| .| .| .| o o")) // (new ArrayList<Long>(Arrays.asList((long)4l, (long)2l, (long)1l, (long)2l, (long)2l, (long)1l, (long)1l, (long)1l, (long)1l, (long)4l, (long)4l))) public static ArrayList<Long> parseMusic(String music_string) { ArrayList<Long> res = new ArrayList<Long>(); char[] chars = music_string.toCharArray(); for (int i = 0; i < chars.length; i++) { if (chars[i] == 'o') { if (i + 1 < chars.length && chars[i + 1] == '|') { res.add((long)2l); i += 1; } else { res.add((long)4l);
} else if (chars[i] == '.') { if (i + 1 < chars.length && chars[i + 1] == '|') { res.add((long)1l); i += 1; } } } return res; } }
}
} public static void main(String[] args) { assert(parseMusic(("")).equals((new ArrayList<Long>(Arrays.asList())))); assert(parseMusic(("o o o o")).equals((new ArrayList<Long>(Arrays.asList((long)4l, (long)4l, (long)4l, (long)4l))))); assert(parseMusic((".| .| .| .|")).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))); assert(parseMusic(("o| o| .| .| o o o o")).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l, (long)1l, (long)1l, (long)4l, (long)4l, (long)4l, (long)4l))))); assert(parseMusic(("o| .| o| .| o o| o o|")).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)1l, (long)2l, (long)1l, (long)4l, (long)2l, (long)4l, (long)2l))))); } }
humaneval-HumanEval_115_max_fill.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 a rectangular grid of wells. Each row represents a single well, // and each 1 in a row represents a single unit of water. // Each well has a corresponding bucket that can be used to extract water from it, // and all buckets have the same capacity. // Your task is to use the buckets to empty the wells. // Output the number of times you need to lower the buckets. // Example 1: // >>> maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (1l)) // (6l) // Example 2: // >>> maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)1l, (long)1l))))), (2l)) // (5l) // Example 3: // >>> maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l))))), (5l)) // (0l) // Constraints: // * all wells have the same length // * 1 <= grid.length <= 10^2 // * 1 <= grid[:,1].length <= 10^2 // * grid[i][j] -> 0 | 1 // * 1 <= capacity <= 10 public static long maxFill(ArrayList<ArrayList<Long>> grid, long capacity) { long count = 0l; for (ArrayList<Long> well : grid) { long level = 0l; for (Long unit : well) { if (unit == 1l) { level += 1l;
} long units = level / capacity; if (level % capacity != 0l) { units += 1l; } count += units; } return count; } }
}
} public static void main(String[] args) { assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (1l)) == (6l)); assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)1l, (long)1l))))), (2l)) == (5l)); assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l))))), (5l)) == (0l)); assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (2l)) == (4l)); assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (9l)) == (2l)); } }
humaneval-HumanEval_81_numerical_letter_grade.json-L44
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 { // It is the last week of the semester and the teacher has to give the grades // to students. The teacher has been making her own algorithm for grading. // The only problem is, she has lost the code she used for grading. // She has given you an array array list of GPAs for some students and you have to write // a function that can output an array array 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: // >>> gradeEquation((new ArrayList<Float>(Arrays.asList((float)4.0f, (float)3l, (float)1.7f, (float)2l, (float)3.5f)))) // (new ArrayList<String>(Arrays.asList((String)"A+", (String)"B", (String)"C-", (String)"C", (String)"A-"))) public static ArrayList<String> numericalLetterGrade(ArrayList<Float> grades) { ArrayList<String> letterGrades = new ArrayList<String>(); for (int i = 0; i < grades.size(); i++) { float currentGrade = grades.get(i); if (currentGrade == 4.0f) { letterGrades.add("A+"); } else if (currentGrade > 3.7f) { letterGrades.add("A"); } else if (currentGrade > 3.3f) { letterGrades.add("A-"); } else if (currentGrade > 3.0f) { letterGrades.add("B+"); } else if (currentGrade > 2.7f) {
} else if (currentGrade > 2.3f) { letterGrades.add("B-"); } else if (currentGrade > 2.0f) { letterGrades.add("C+"); } else if (currentGrade > 1.7f) { letterGrades.add("C"); } else if (currentGrade > 1.3f) { letterGrades.add("C-"); } else if (currentGrade > 1.0f) { letterGrades.add("D+"); } else if (currentGrade > 0.7f) { letterGrades.add("D"); } else if (currentGrade > 0.0f) { letterGrades.add("D-"); } else { letterGrades.add("E"); } } return letterGrades; } }
letterGrades.add("B");
} public static void main(String[] args) { assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)4.0f, (float)3l, (float)1.7f, (float)2l, (float)3.5f)))).equals((new ArrayList<String>(Arrays.asList((String)"A+", (String)"B", (String)"C-", (String)"C", (String)"A-"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)1.2f)))).equals((new ArrayList<String>(Arrays.asList((String)"D+"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.5f)))).equals((new ArrayList<String>(Arrays.asList((String)"D-"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.0f)))).equals((new ArrayList<String>(Arrays.asList((String)"E"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)0.3f, (float)1.5f, (float)2.8f, (float)3.3f)))).equals((new ArrayList<String>(Arrays.asList((String)"D", (String)"D-", (String)"C-", (String)"B", (String)"B+"))))); assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.0f, (float)0.7f)))).equals((new ArrayList<String>(Arrays.asList((String)"E", (String)"D-"))))); } }
humaneval-HumanEval_153_Strongest_Extension.json-L23
js
//You will be given the name of a class (a string) and an array of extensions. // The extensions are to be used to load additional classes to the class. The // strength of the extension is as follows: Let CAP be the number of the uppercase // letters in the extension's name, and let SM be the number of lowercase letters // in the extension's name, the strength is given by the fraction CAP - SM. // You should find the strongest extension and return a string in this // format: ClassName.StrongestExtensionName. // If there are two or more extensions with the same strength, you should // choose the one that comes first in the array. // For example, if you are given "Slices" as the class and an array of the // extensions: ['SErviNGSliCes', 'Cheese', 'StuFfed'] then you should // return 'Slices.SErviNGSliCes' since 'SErviNGSliCes' is the strongest extension // (its strength is -1). // Example: // >>> Strongest_Extension("my_class", ["AA", "Be", "CC"]) // "my_class.AA" function Strongest_Extension(class_name, extensions){ var output = class_name + "."; var min_strength = 1; for (var i = 0; i < extensions.length; i++) { var strength = -1*(extensions[i].replace(/[^A-Z]/g, "").length - extensions[i].replace(/[^a-z]/g, "").length); if (strength < min_strength) {
output = class_name + "." + extensions[i]; } } return output; }
min_strength = strength;
const assert = require('node:assert'); function test() { let candidate = Strongest_Extension; assert.deepEqual(candidate("Watashi", ["tEN", "niNE", "eIGHt8OKe"]),"Watashi.eIGHt8OKe"); assert.deepEqual(candidate("Boku123", ["nani", "NazeDa", "YEs.WeCaNe", "32145tggg"]),"Boku123.YEs.WeCaNe"); assert.deepEqual(candidate("__YESIMHERE", ["t", "eMptY", "nothing", "zeR00", "NuLl__", "123NoooneB321"]),"__YESIMHERE.NuLl__"); assert.deepEqual(candidate("K", ["Ta", "TAR", "t234An", "cosSo"]),"K.TAR"); assert.deepEqual(candidate("__HAHA", ["Tab", "123", "781345", "-_-"]),"__HAHA.123"); assert.deepEqual(candidate("YameRore", ["HhAas", "okIWILL123", "WorkOut", "Fails", "-_-"]),"YameRore.okIWILL123"); assert.deepEqual(candidate("finNNalLLly", ["Die", "NowW", "Wow", "WoW"]),"finNNalLLly.WoW"); assert.deepEqual(candidate("_", ["Bb", "91245"]),"_.Bb"); assert.deepEqual(candidate("Sp", ["671235", "Bb"]),"Sp.671235"); } test();
humaneval-HumanEval_87_get_row.json-L20
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];
if (lst[i][j] === x){ coordinates.push([i, j]); } } } return coordinates.sort(function(a, b){ return a[0] - b[0] || b[1] - a[1]; }); }
for (let j = 0; j < array.length; j++){
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_4_mean_absolute_deviation.json-L16
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] return sum(absolute_deviations) / len(absolute_deviations)
print(mean_absolute_deviation([1.0, 2.0, 3.0, 4.0]))
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_85_add.json-L15
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given a non-empty array list of integers lst. add the even elements that are at odd indices.. // Examples: // >>> add((new ArrayList<Long>(Arrays.asList((long)4l, (long)2l, (long)6l, (long)7l)))) // (2l) public static long add(ArrayList<Long> lst) { long sum = 0;
if (i % 2 == 1 && lst.get(i) % 2 == 0) { sum += lst.get(i); } } return sum; } }
for (int i = 0; i < lst.size(); i++) {
} public static void main(String[] args) { assert(add((new ArrayList<Long>(Arrays.asList((long)4l, (long)88l)))) == (88l)); assert(add((new ArrayList<Long>(Arrays.asList((long)4l, (long)5l, (long)6l, (long)7l, (long)2l, (long)122l)))) == (122l)); assert(add((new ArrayList<Long>(Arrays.asList((long)4l, (long)0l, (long)6l, (long)7l)))) == (0l)); assert(add((new ArrayList<Long>(Arrays.asList((long)4l, (long)4l, (long)6l, (long)8l)))) == (12l)); } }
humaneval-HumanEval_3_below_zero.json-L11
js
//You're given an array of deposit and withdrawal operations on a bank account that starts with // zero balance. Your task is to detect if at any point the balance of account fallls below zero, and // at that point function should return true. Otherwise it should return false. // >>> below_zero([1, 2, 3]) // false // >>> below_zero([1, 2, -4, 5]) // true function below_zero(operations){ var sum = 0; for (var i = 0; i < operations.length; i++) {
if (sum < 0) { return true; } } return false; }
sum += operations[i];
const assert = require('node:assert'); function test() { let candidate = below_zero; assert.deepEqual(candidate([]),false); assert.deepEqual(candidate([1, 2, -3, 1, 2, -3]),false); assert.deepEqual(candidate([1, 2, -4, 5, 6]),true); assert.deepEqual(candidate([1, -1, 2, -2, 5, -5, 4, -4]),false); assert.deepEqual(candidate([1, -1, 2, -2, 5, -5, 4, -5]),true); assert.deepEqual(candidate([1, -2, 2, -2, 5, -5, 4, -4]),true); } test();
humaneval-HumanEval_144_simplify.json-L16
js
//Your task is to implement a function that will simplify the expression // x * n. The function returns true if x * n evaluates to a whole number and false // otherwise. Both x and n, are string representation of a fraction, and have the following format, // <numerator>/<denominator> where both numerator and denominator are positive whole numbers. // You can assume that x, and n are valid fractions, and do not have zero as denominator. // >>> simplify("1/5", "5/1") // true // >>> simplify("1/6", "2/1") // false // >>> simplify("7/10", "10/2") // false function simplify(x, n){ let xNumerator = parseInt(x.split('/')[0]); let xDenominator = parseInt(x.split('/')[1]); let nNumerator = parseInt(n.split('/')[0]);
let xTimesN = xNumerator * nNumerator / (xDenominator * nDenominator); return Number.isInteger(xTimesN); }
let nDenominator = parseInt(n.split('/')[1]);
const assert = require('node:assert'); function test() { let candidate = simplify; assert.deepEqual(candidate("1/5", "5/1"),true); assert.deepEqual(candidate("1/6", "2/1"),false); assert.deepEqual(candidate("5/1", "3/1"),true); assert.deepEqual(candidate("7/10", "10/2"),false); assert.deepEqual(candidate("2/10", "50/10"),true); assert.deepEqual(candidate("7/2", "4/2"),true); assert.deepEqual(candidate("11/6", "6/1"),true); assert.deepEqual(candidate("2/3", "5/2"),false); assert.deepEqual(candidate("5/2", "3/5"),false); assert.deepEqual(candidate("2/4", "8/4"),true); assert.deepEqual(candidate("2/4", "4/2"),true); assert.deepEqual(candidate("1/5", "5/1"),true); assert.deepEqual(candidate("1/5", "1/5"),false); } test();
humaneval-HumanEval_104_unique_digits.json-L24
py
from typing import List def unique_digits(x: List[int]) -> List[int]: """Given a list of positive integers x. return a sorted list of all elements that hasn't any even digit. Note: Returned list should be sorted in increasing order. For example: >>> unique_digits([15, 33, 1422, 1]) [1, 15, 33] >>> unique_digits([152, 323, 1422, 10]) [] """ s=[] for i in x: temp=i while temp: if temp%2==0: s.append(i) break temp=temp//10 for i in s:
return sorted(x)
x.remove(i)
def check(candidate): assert candidate([15, 33, 1422, 1]) == [1, 15, 33] assert candidate([152, 323, 1422, 10]) == [] assert candidate([12345, 2033, 111, 151]) == [111, 151] assert candidate([135, 103, 31]) == [31, 135] def test_check(): check(unique_digits) test_check()
humaneval-HumanEval_74_total_match.json-L18
js
//Write a function that accepts two arrays of strings and returns the array that has // total number of chars in the all strings of the array less than the other array. // if the two arrays have the same number of chars, return the first array. // Examples // >>> total_match([], []) // [] // >>> total_match(["hi", "admin"], ["hI", "Hi"]) // ["hI", "Hi"] // >>> total_match(["hi", "admin"], ["hi", "hi", "admin", "project"]) // ["hi", "admin"] // >>> total_match(["hi", "admin"], ["hI", "hi", "hi"]) // ["hI", "hi", "hi"] // >>> total_match(["4"], ["1", "2", "3", "4", "5"]) // ["4"] function total_match(lst1, lst2){ let sum_lst1 = 0; let sum_lst2 = 0;
sum_lst1 += item.length; } for(let item of lst2){ sum_lst2 += item.length; } if(sum_lst1 > sum_lst2){ return lst2; } else{ return lst1; } }
for(let item of lst1){
const assert = require('node:assert'); function test() { let candidate = total_match; assert.deepEqual(candidate([], []),[]); assert.deepEqual(candidate(["hi", "admin"], ["hi", "hi"]),["hi", "hi"]); assert.deepEqual(candidate(["hi", "admin"], ["hi", "hi", "admin", "project"]),["hi", "admin"]); assert.deepEqual(candidate(["4"], ["1", "2", "3", "4", "5"]),["4"]); assert.deepEqual(candidate(["hi", "admin"], ["hI", "Hi"]),["hI", "Hi"]); assert.deepEqual(candidate(["hi", "admin"], ["hI", "hi", "hi"]),["hI", "hi", "hi"]); assert.deepEqual(candidate(["hi", "admin"], ["hI", "hi", "hii"]),["hi", "admin"]); assert.deepEqual(candidate([], ["this"]),[]); assert.deepEqual(candidate(["this"], []),[]); } test();
humaneval-HumanEval_118_get_closest_vowel.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 { // You are given a word. Your task is to find the closest vowel that stands between // two consonants from the right side of the word (case sensitive). // Vowels in the beginning and ending doesn't count. Return empty string if you didn't // find any vowel met the above condition. // You may assume that the given string contains English letter only. // Example: // >>> getClosestVowel(("yogurt")) // ("u") // >>> getClosestVowel(("FULL")) // ("U") // >>> getClosestVowel(("quick")) // ("") // >>> getClosestVowel(("ab")) // ("") public static String getClosestVowel(String word) { final String vowels = "aeiou"; final String consonants = "bcdfghjklmnpqrstvwxyz"; List<Integer> idx = new ArrayList<>(); word = word.toLowerCase(); for (int i = 0; i < word.length(); i++) { if (vowels.indexOf(word.charAt(i)) != -1) {
} } for (int i = idx.size() - 1; i >= 0; i--) { int j = idx.get(i); if (j == 0 || j == word.length() - 1) continue; char left = word.charAt(j - 1); char right = word.charAt(j + 1); if (consonants.indexOf(left) != -1 && consonants.indexOf(right) != -1) { return String.valueOf(word.charAt(j)); } } return ""; } }
idx.add(i);
} public static void main(String[] args) { assert(getClosestVowel(("yogurt")).equals(("u"))); assert(getClosestVowel(("full")).equals(("u"))); assert(getClosestVowel(("easy")).equals((""))); assert(getClosestVowel(("eAsy")).equals((""))); assert(getClosestVowel(("ali")).equals((""))); assert(getClosestVowel(("bad")).equals(("a"))); assert(getClosestVowel(("most")).equals(("o"))); assert(getClosestVowel(("ab")).equals((""))); assert(getClosestVowel(("ba")).equals((""))); assert(getClosestVowel(("quick")).equals((""))); assert(getClosestVowel(("anime")).equals(("i"))); assert(getClosestVowel(("Asia")).equals((""))); assert(getClosestVowel(("Above")).equals(("o"))); } }
humaneval-HumanEval_35_max_element.json-L7
js
//Return maximum element in the array. // >>> max_element([1, 2, 3]) // 3 // >>> max_element([5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10]) // 123 function max_element(l){
}
return Math.max.apply(null, l);
const assert = require('node:assert'); function test() { let candidate = max_element; assert.deepEqual(candidate([1, 2, 3]),3); assert.deepEqual(candidate([5, 3, -5, 2, -3, 3, 9, 0, 124, 1, -10]),124); } test();
humaneval-HumanEval_106_f.json-L21
py
from typing import List def f(n: int) -> List[int]: """ Implement the function f that takes n as a parameter, and returns a list of size n, such that the value of the element at index i is the factorial of i if i is even or the sum of numbers from 1 to i otherwise. i starts from 1. the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i). Example: >>> f(5) [1, 2, 6, 24, 15] """ import math a=[] for i in range(n): i=i+1 if i%2==0: a.append(math.factorial(i)) else: a.append(sum(range(1,i+1)))
pass
return a
def check(candidate): assert candidate(5) == [1, 2, 6, 24, 15] assert candidate(7) == [1, 2, 6, 24, 15, 720, 28] assert candidate(1) == [1] assert candidate(3) == [1, 2, 6] def test_check(): check(f) test_check()
humaneval-HumanEval_144_simplify.json-L28
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Your task is to implement a function that will simplify the expression // x * n. The function returns true if x * n evaluates to a whole number and false // otherwise. Both x and n, are string representation of a fraction, and have the following format, // <numerator>/<denominator> where both numerator and denominator are positive whole numbers. // You can assume that x, and n are valid fractions, and do not have zero as denominator. // >>> simplify(("1/5"), ("5/1")) // (true) // >>> simplify(("1/6"), ("2/1")) // (false) // >>> simplify(("7/10"), ("10/2")) // (false) public static boolean simplify(String x, String n) { String[] nums = x.split("/"); String[] n_nums = n.split("/"); int x_num = Integer.parseInt(nums[0]); int n_num = Integer.parseInt(n_nums[0]); int x_denom = Integer.parseInt(nums[1]); int n_denom = Integer.parseInt(n_nums[1]); int res_num = x_num * n_num;
return (res_num % res_denom == 0); } }
int res_denom = x_denom * n_denom;
} public static void main(String[] args) { assert(simplify(("1/5"), ("5/1")) == (true)); assert(simplify(("1/6"), ("2/1")) == (false)); assert(simplify(("5/1"), ("3/1")) == (true)); assert(simplify(("7/10"), ("10/2")) == (false)); assert(simplify(("2/10"), ("50/10")) == (true)); assert(simplify(("7/2"), ("4/2")) == (true)); assert(simplify(("11/6"), ("6/1")) == (true)); assert(simplify(("2/3"), ("5/2")) == (false)); assert(simplify(("5/2"), ("3/5")) == (false)); assert(simplify(("2/4"), ("8/4")) == (true)); assert(simplify(("2/4"), ("4/2")) == (true)); assert(simplify(("1/5"), ("5/1")) == (true)); assert(simplify(("1/5"), ("1/5")) == (false)); } }
humaneval-HumanEval_124_valid_date.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 have to write a function which validates a given date string and // returns true if the date is valid otherwise false. // The date is valid if all of the following rules are satisfied: // 1. The date string is not empty. // 2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the number of days is not less than 1 or higher than 30 days for months 4,6,9,11. And, the number of days is not less than 1 or higher than 29 for the month 2. // 3. The months should not be less than 1 or higher than 12. // 4. The date should be in the format: mm-dd-yyyy // >>> validDate(("03-11-2000")) // (true) // >>> validDate(("15-01-2012")) // (false) // >>> validDate(("04-0-2040")) // (false) // >>> validDate(("06-04-2020")) // (true) // >>> validDate(("06/04/2020")) // (false) public static boolean validDate(String date) { boolean valid_date = false; if (date.equals("")) return valid_date; String[] parts = date.split("-"); if (parts.length != 3) return valid_date; try {
int day = Integer.parseInt(parts[1]); int year = Integer.parseInt(parts[2]); if (month < 1 || month > 12) return valid_date; if (month == 2) { if (day < 1 || day > 29) return valid_date; } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if (day < 1 || day > 31) return valid_date; } else { if (day < 1 || day > 30) return valid_date; } valid_date = true; } catch (Exception e) { valid_date = false; } return valid_date; } }
int month = Integer.parseInt(parts[0]);
} public static void main(String[] args) { assert(validDate(("03-11-2000")) == (true)); assert(validDate(("15-01-2012")) == (false)); assert(validDate(("04-0-2040")) == (false)); assert(validDate(("06-04-2020")) == (true)); assert(validDate(("01-01-2007")) == (true)); assert(validDate(("03-32-2011")) == (false)); assert(validDate(("")) == (false)); assert(validDate(("04-31-3000")) == (false)); assert(validDate(("06-06-2005")) == (true)); assert(validDate(("21-31-2000")) == (false)); assert(validDate(("04-12-2003")) == (true)); assert(validDate(("04122003")) == (false)); assert(validDate(("20030412")) == (false)); assert(validDate(("2003-04")) == (false)); assert(validDate(("2003-04-12")) == (false)); assert(validDate(("04-2003")) == (false)); } }
humaneval-HumanEval_68_pluck.json-L40
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // "Given an array array list 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 array list, [ smalest_value, its index ], // If there are no even values or the given array array list is empty, return []. // Example 1: // >>> pluck((new ArrayList<Long>(Arrays.asList((long)4l, (long)2l, (long)3l)))) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)1l))) // Explanation: 2 has the smallest even value, and 2 has the smallest index. // Example 2: // >>> pluck((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)1l))) // Explanation: 2 has the smallest even value, and 2 has the smallest index. // Example 3: // >>> pluck((new ArrayList<Long>(Arrays.asList()))) // (new ArrayList<Long>(Arrays.asList())) // Example 4: // >>> pluck((new ArrayList<Long>(Arrays.asList((long)5l, (long)0l, (long)3l, (long)0l, (long)4l, (long)2l)))) // (new ArrayList<Long>(Arrays.asList((long)0l, (long)1l))) // 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 public static ArrayList<Long> pluck(ArrayList<Long> arr) { int minIndex = -1; long minValue = -1; for (int i = 0; i < arr.size(); i++) { if (arr.get(i) % 2 == 0) { if (minIndex == -1 || arr.get(i) < minValue) {
minValue = arr.get(i); } } } ArrayList<Long> newArr = new ArrayList<Long>(); if (minIndex == -1) { return newArr; } newArr.add(minValue); newArr.add((long)minIndex); return newArr; } }
minIndex = i;
} public static void main(String[] args) { assert(pluck((new ArrayList<Long>(Arrays.asList((long)4l, (long)2l, (long)3l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)1l))))); assert(pluck((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)1l))))); assert(pluck((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<Long>(Arrays.asList())))); assert(pluck((new ArrayList<Long>(Arrays.asList((long)5l, (long)0l, (long)3l, (long)0l, (long)4l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)0l, (long)1l))))); assert(pluck((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)0l, (long)5l, (long)3l)))).equals((new ArrayList<Long>(Arrays.asList((long)0l, (long)3l))))); assert(pluck((new ArrayList<Long>(Arrays.asList((long)5l, (long)4l, (long)8l, (long)4l, (long)8l)))).equals((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l))))); assert(pluck((new ArrayList<Long>(Arrays.asList((long)7l, (long)6l, (long)7l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)6l, (long)1l))))); assert(pluck((new ArrayList<Long>(Arrays.asList((long)7l, (long)9l, (long)7l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList())))); } }
humaneval-HumanEval_131_digits.json-L21
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)){
if(temp % 2 != 0){ prod *= temp; odd = false; } } if(odd){ return 0; } return prod; }
temp = n % 10;
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_152_compare.json-L17
js
//I think we all remember that feeling when the result of some long-awaited // event is finally known. The feelings and thoughts you have at that moment are // definitely worth noting down and comparing. // Your task is to determine if a person correctly guessed the results of a number of matches. // You are given two arrays of scores and guesses of equal length, where each index shows a match. // Return an array of the same length denoting how far off each guess was. If they have guessed correctly, // the value is 0, and if not, the value is the absolute difference between the guess and the score. // example: // >>> compare([1, 2, 3, 4, 5, 1], [1, 2, 3, 4, 2, -2]) // [0, 0, 0, 0, 3, 3] // >>> compare([0, 5, 0, 0, 0, 4], [4, 1, 1, 0, 0, -2]) // [4, 4, 1, 0, 0, 6] function compare(game, guess){ let result = []; for(let i = 0; i < game.length; i++){ if(game[i] === guess[i]){
} else { result.push(Math.abs(game[i] - guess[i])); } } return result; }
result.push(0);
const assert = require('node:assert'); function test() { let candidate = compare; assert.deepEqual(candidate([1, 2, 3, 4, 5, 1], [1, 2, 3, 4, 2, -2]),[0, 0, 0, 0, 3, 3]); assert.deepEqual(candidate([0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]),[0, 0, 0, 0, 0, 0]); assert.deepEqual(candidate([1, 2, 3], [-1, -2, -3]),[2, 4, 6]); assert.deepEqual(candidate([1, 2, 3, 5], [-1, 2, 3, 4]),[2, 0, 0, 1]); } test();
humaneval-HumanEval_40_triples_sum_to_zero.json-L25
js
//triples_sum_to_zero takes an array of integers as an input. // it returns true if there are three distinct elements in the array that // sum to zero, and false otherwise. // >>> triples_sum_to_zero([1, 3, 5, 0]) // false // >>> triples_sum_to_zero([1, 3, -2, 1]) // true // >>> triples_sum_to_zero([1, 2, 3, 7]) // false // >>> triples_sum_to_zero([2, 4, -5, 3, 9, 7]) // true // >>> triples_sum_to_zero([1]) // false function triples_sum_to_zero(l){ for (var i = 0; i < l.length; i++) { for (var j = 0; j < l.length; j++) { for (var k = 0; k < l.length; k++) { if (i !== j && i !== k && j !== k) { if (l[i] + l[j] + l[k] === 0) { return true; } } } }
return false; }
}
const assert = require('node:assert'); function test() { let candidate = triples_sum_to_zero; assert.deepEqual(candidate([1, 3, 5, 0]),false); assert.deepEqual(candidate([1, 3, 5, -1]),false); assert.deepEqual(candidate([1, 3, -2, 1]),true); assert.deepEqual(candidate([1, 2, 3, 7]),false); assert.deepEqual(candidate([1, 2, 5, 7]),false); assert.deepEqual(candidate([2, 4, -5, 3, 9, 7]),true); assert.deepEqual(candidate([1]),false); assert.deepEqual(candidate([1, 3, 5, -100]),false); assert.deepEqual(candidate([100, 3, 5, -100]),false); } test();
humaneval-HumanEval_158_find_max.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 { // Write a function that accepts an array array list of strings. // The array list contains different words. Return the word with maximum number // of unique characters. If multiple strings have maximum number of unique // characters, return the one which comes first in lexicographical order. // >>> findMax((new ArrayList<String>(Arrays.asList((String)"name", (String)"of", (String)"string")))) // ("string") // >>> findMax((new ArrayList<String>(Arrays.asList((String)"name", (String)"enam", (String)"game")))) // ("enam") // >>> findMax((new ArrayList<String>(Arrays.asList((String)"aaaaaaa", (String)"bb", (String)"cc")))) // ("aaaaaaa") public static String findMax(ArrayList<String> words) { Map<String, Integer> wordsToUniqueCharacters = new HashMap<String, Integer>(); for (String word : words) { Set<Character> uniqueCharacters = new HashSet<Character>(); for (char c : word.toCharArray()) {
} 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); } }
uniqueCharacters.add(c);
} 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_60_sum_to_n.json-L21
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // sum_to_n is a function that sums numbers from 1 to n. // >>> sumToN((30l)) // (465l) // >>> sumToN((100l)) // (5050l) // >>> sumToN((5l)) // (15l) // >>> sumToN((10l)) // (55l) // >>> sumToN((1l)) // (1l) public static long sumToN(long n) {
} }
return (n * (n + 1)) / 2;
} public static void main(String[] args) { assert(sumToN((1l)) == (1l)); assert(sumToN((6l)) == (21l)); assert(sumToN((11l)) == (66l)); assert(sumToN((30l)) == (465l)); assert(sumToN((100l)) == (5050l)); } }
humaneval-HumanEval_84_solve.json-L18
js
//Given a positive integer N, return the total sum of its digits in binary. // Example // >>> solve(1000) // "1" // >>> solve(150) // "110" // >>> solve(147) // "1100" // Variables: // @N integer // Constraints: 0 ≤ N ≤ 10000. // Output: // a string of binary number function solve(N){ let newN = N.toString(); let total = 0; for(let i = 0; i < newN.length; i++){
total += parseInt(digit); } return total.toString(2); }
let digit = newN[i];
const assert = require('node:assert'); function test() { let candidate = solve; assert.deepEqual(candidate(1000),"1"); assert.deepEqual(candidate(150),"110"); assert.deepEqual(candidate(147),"1100"); assert.deepEqual(candidate(333),"1001"); assert.deepEqual(candidate(963),"10010"); } test();
humaneval-HumanEval_61_correct_bracketing.json-L25
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_63_fibfib.json-L14
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){
return 0; } if(n == 1){ return 0; } if(n == 2){ return 1; } return fibfib(n-1) + fibfib(n-2) + fibfib(n-3); }
if(n <= 0){
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_19_sort_numbers.json-L13
js
//Input is a space-delimited string of numberals from 'zero' to 'nine'. // Valid choices are 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight' and 'nine'. // Return the string with numbers sorted from smallest to largest // >>> sort_numbers("three one five") // "one three five" function sort_numbers(numbers){ var numString = numbers.split(" "); var numMap = { "zero": 0, "one": 1, "two": 2, "three": 3,
"five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9 }; numString.sort(function(a, b){ return numMap[a] - numMap[b]; }); numString = numString.join(" "); return numString; }
"four": 4,
const assert = require('node:assert'); function test() { let candidate = sort_numbers; assert.deepEqual(candidate(""),""); assert.deepEqual(candidate("three"),"three"); assert.deepEqual(candidate("three five nine"),"three five nine"); assert.deepEqual(candidate("five zero four seven nine eight"),"zero four five seven eight nine"); assert.deepEqual(candidate("six five four three two one zero"),"zero one two three four five six"); } test();
humaneval-HumanEval_143_words_in_sentence.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 { // You are given a string representing a sentence, // the sentence contains some words separated by a space, // and you have to return a string that contains the words from the original sentence, // whose lengths are prime numbers, // the order of the words in the new string should be the same as the original one. // Example 1: // >>> wordsInSentence(("This is a test")) // ("is") // Example 2: // >>> wordsInSentence(("lets go for swimming")) // ("go for") // Constraints: // * 1 <= len(sentence) <= 100 // * sentence contains only letters public static String wordsInSentence(String sentence) { StringBuilder ans = new StringBuilder(); String[] words = sentence.split(" "); int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; boolean isPrime; for (String word : words) { isPrime = false; for (int prime : primes) { if (word.length() == prime) { isPrime = true; break; } } if (isPrime) { ans.append(word + " ");
} if (ans.length() > 0) { ans.deleteCharAt(ans.length() - 1); } return ans.toString(); } }
}
} public static void main(String[] args) { assert(wordsInSentence(("This is a test")).equals(("is"))); assert(wordsInSentence(("lets go for swimming")).equals(("go for"))); assert(wordsInSentence(("there is no place available here")).equals(("there is no place"))); assert(wordsInSentence(("Hi I am Hussein")).equals(("Hi am Hussein"))); assert(wordsInSentence(("go for it")).equals(("go for it"))); assert(wordsInSentence(("here")).equals((""))); assert(wordsInSentence(("here is")).equals(("is"))); } }
humaneval-HumanEval_138_is_equal_to_sum_even.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 { // Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers // Example // >>> isEqualToSumEven((4l)) // (false) // >>> isEqualToSumEven((6l)) // (false) // >>> isEqualToSumEven((8l)) // (true) public static boolean isEqualToSumEven(long n) { if (n < 4) { return false; } long d = 2; if (n % 2 == 1) {
} 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; } }
d = 1;
} 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_1_separate_paren_groups.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 { // Input to this function is a string containing multiple groups of nested parentheses. Your goal is to // separate those group into separate strings and return the array list of those. // Separate groups are balanced (each open brace is properly closed) and not nested within each other // Ignore any spaces in the input string. // >>> separateParenGroups(("( ) (( )) (( )( ))")) // (new ArrayList<String>(Arrays.asList((String)"()", (String)"(())", (String)"(()())"))) public static ArrayList<String> separateParenGroups(String paren_string) { ArrayList<String> output = new ArrayList<String>(); StringBuffer temp = new StringBuffer();
for (int i = 0; i < paren_string.length(); i++) { char c = paren_string.charAt(i); if (c == '(') { if (open_paren == 0) { temp = new StringBuffer(); } open_paren++; temp.append(c); } else if (c == ')') { open_paren--; temp.append(c); if (open_paren == 0) { output.add(temp.toString()); } } } return output; } }
int open_paren = 0;
} public static void main(String[] args) { assert(separateParenGroups(("(()()) ((())) () ((())()())")).equals((new ArrayList<String>(Arrays.asList((String)"(()())", (String)"((()))", (String)"()", (String)"((())()())"))))); assert(separateParenGroups(("() (()) ((())) (((())))")).equals((new ArrayList<String>(Arrays.asList((String)"()", (String)"(())", (String)"((()))", (String)"(((())))"))))); assert(separateParenGroups(("(()(())((())))")).equals((new ArrayList<String>(Arrays.asList((String)"(()(())((())))"))))); assert(separateParenGroups(("( ) (( )) (( )( ))")).equals((new ArrayList<String>(Arrays.asList((String)"()", (String)"(())", (String)"(()())"))))); } }
humaneval-HumanEval_18_how_many_times.json-L28
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 { // Find how many times a given substring can be found in the original string. Count overlaping cases. // >>> howManyTimes((""), ("a")) // (0l) // >>> howManyTimes(("aaa"), ("a")) // (3l) // >>> howManyTimes(("aaaa"), ("aa")) // (3l) public static long howManyTimes(String string, String substring) { long count = 0; int index = 0; while (index != -1) { index = string.indexOf(substring, index); if (index != -1) { index++; count++; } } return count; }
}
} public static void main(String[] args) { assert(howManyTimes((""), ("x")) == (0l)); assert(howManyTimes(("xyxyxyx"), ("x")) == (4l)); assert(howManyTimes(("cacacacac"), ("cac")) == (4l)); assert(howManyTimes(("john doe"), ("john")) == (1l)); } }
humaneval-HumanEval_138_is_equal_to_sum_even.json-L29
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers // Example // >>> isEqualToSumEven((4l)) // (false) // >>> isEqualToSumEven((6l)) // (false) // >>> isEqualToSumEven((8l)) // (true) public static boolean isEqualToSumEven(long n) { if (n < 4) { return false; } long d = 2; if (n % 2 == 1) { d = 1; } long minX = d; long maxX = n / 4; while (minX <= maxX) { long x = (minX + maxX) / 2;
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; } }
long y = n - 2 * x;
} 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_64_vowels_count.json-L20
py
def vowels_count(s: str) -> int: """Write a function vowels_count which takes a string representing a word as input and returns the number of vowels in the string. Vowels in this case are 'a', 'e', 'i', 'o', 'u'. Here, 'y' is also a vowel, but only when it is at the end of the given word. Example: >>> vowels_count('abcde') 2 >>> vowels_count('ACEDY') 3 """ s = s.lower() i = 0 count = 0 while i < len(s): if (s[i] == 'a' or s[i] == 'e' or s[i] == 'i' or s[i] == 'o' or s[i] == 'u'): count += 1 if (s[i] == 'y' and i == len(s) - 1):
i += 1 return count
count += 1
def check(candidate): assert candidate('abcde') == 2 assert candidate('Alone') == 3 assert candidate('key') == 2 assert candidate('bye') == 1 assert candidate('keY') == 2 assert candidate('bYe') == 1 assert candidate('ACEDY') == 3 def test_check(): check(vowels_count) test_check()
humaneval-HumanEval_109_move_one_ball.json-L40
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]: flag = False
return True
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_156_int_to_mini_roman.json-L30
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', 9: 'ix', 5: 'v', 4: 'iv', 1: 'i' }
for key in sorted(roman_numerals.keys(), reverse=True): roman_numeral += roman_numerals[key] * (number // key) number = number % key return roman_numeral
roman_numeral = ''
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_126_is_sorted.json-L31
js
//Given an array of numbers, return whether or not they are sorted // in ascending order. If array has more than 1 duplicate of the same // number, return false. Assume no negative numbers and only integers. // Examples // >>> is_sorted([5]) // true // >>> is_sorted([1, 2, 3, 4, 5]) // true // >>> is_sorted([1, 3, 2, 4, 5]) // false // >>> is_sorted([1, 2, 3, 4, 5, 6]) // true // >>> is_sorted([1, 2, 3, 4, 5, 6, 7]) // true // >>> is_sorted([1, 3, 2, 4, 5, 6, 7]) // false // >>> is_sorted([1, 2, 2, 3, 3, 4]) // true // >>> is_sorted([1, 2, 2, 2, 3, 4]) // false function is_sorted(lst){ var last = null; var dupe_found = false; for (var x of lst){ if (last == null){ last = x; } else if (x < last){ return false; } else if (x == last){ if (dupe_found){
} else { dupe_found = true; } } else { last = x; dupe_found = false; } } return true; }
return false;
const assert = require('node:assert'); function test() { let candidate = is_sorted; assert.deepEqual(candidate([5]),true); assert.deepEqual(candidate([1, 2, 3, 4, 5]),true); assert.deepEqual(candidate([1, 3, 2, 4, 5]),false); assert.deepEqual(candidate([1, 2, 3, 4, 5, 6]),true); assert.deepEqual(candidate([1, 2, 3, 4, 5, 6, 7]),true); assert.deepEqual(candidate([1, 3, 2, 4, 5, 6, 7]),false); assert.deepEqual(candidate([]),true); assert.deepEqual(candidate([1]),true); assert.deepEqual(candidate([3, 2, 1]),false); assert.deepEqual(candidate([1, 2, 2, 2, 3, 4]),false); assert.deepEqual(candidate([1, 2, 3, 3, 3, 4]),false); assert.deepEqual(candidate([1, 2, 2, 3, 3, 4]),true); assert.deepEqual(candidate([1, 2, 3, 4]),true); } test();
humaneval-HumanEval_74_total_match.json-L29
js
//Write a function that accepts two arrays of strings and returns the array that has // total number of chars in the all strings of the array less than the other array. // if the two arrays have the same number of chars, return the first array. // Examples // >>> total_match([], []) // [] // >>> total_match(["hi", "admin"], ["hI", "Hi"]) // ["hI", "Hi"] // >>> total_match(["hi", "admin"], ["hi", "hi", "admin", "project"]) // ["hi", "admin"] // >>> total_match(["hi", "admin"], ["hI", "hi", "hi"]) // ["hI", "hi", "hi"] // >>> total_match(["4"], ["1", "2", "3", "4", "5"]) // ["4"] function total_match(lst1, lst2){ let sum_lst1 = 0; let sum_lst2 = 0; for(let item of lst1){ sum_lst1 += item.length; } for(let item of lst2){ sum_lst2 += item.length; } if(sum_lst1 > sum_lst2){ return lst2; } else{ return lst1;
}
}
const assert = require('node:assert'); function test() { let candidate = total_match; assert.deepEqual(candidate([], []),[]); assert.deepEqual(candidate(["hi", "admin"], ["hi", "hi"]),["hi", "hi"]); assert.deepEqual(candidate(["hi", "admin"], ["hi", "hi", "admin", "project"]),["hi", "admin"]); assert.deepEqual(candidate(["4"], ["1", "2", "3", "4", "5"]),["4"]); assert.deepEqual(candidate(["hi", "admin"], ["hI", "Hi"]),["hI", "Hi"]); assert.deepEqual(candidate(["hi", "admin"], ["hI", "hi", "hi"]),["hI", "hi", "hi"]); assert.deepEqual(candidate(["hi", "admin"], ["hI", "hi", "hii"]),["hi", "admin"]); assert.deepEqual(candidate([], ["this"]),[]); assert.deepEqual(candidate(["this"], []),[]); } test();
humaneval-HumanEval_104_unique_digits.json-L18
py
from typing import List def unique_digits(x: List[int]) -> List[int]: """Given a list of positive integers x. return a sorted list of all elements that hasn't any even digit. Note: Returned list should be sorted in increasing order. For example: >>> unique_digits([15, 33, 1422, 1]) [1, 15, 33] >>> unique_digits([152, 323, 1422, 10]) [] """ s=[] for i in x: temp=i
if temp%2==0: s.append(i) break temp=temp//10 for i in s: x.remove(i) return sorted(x)
while temp:
def check(candidate): assert candidate([15, 33, 1422, 1]) == [1, 15, 33] assert candidate([152, 323, 1422, 10]) == [] assert candidate([12345, 2033, 111, 151]) == [111, 151] assert candidate([135, 103, 31]) == [31, 135] def test_check(): check(unique_digits) test_check()
humaneval-HumanEval_129_minPath.json-L60
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]); } if(y > 0){ neighbours.push(grid[x][y - 1]); } if(y < n - 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; }
neighbours.push(grid[x][y + 1]);
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();