Datasets:

Modalities:
Text
Formats:
parquet
ArXiv:
Tags:
code
Libraries:
Datasets
pandas
License:
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_59_largest_prime_factor.json-L26
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Return the largest prime factor of n. Assume n > 1 and is not a prime. // >>> largestPrimeFactor((13195l)) // (29l) // >>> largestPrimeFactor((2048l)) // (2l) public static long largestPrimeFactor(long n) { List<Long> primes = new ArrayList<>(); long x = n; for (long i = 2; i * i <= x; i++) { if (x % i == 0) { primes.add(i); x /= i; i--; } } if (x > 1) { primes.add(x);
return primes.get(primes.size() - 1); } }
}
} public static void main(String[] args) { assert(largestPrimeFactor((15l)) == (5l)); assert(largestPrimeFactor((27l)) == (3l)); assert(largestPrimeFactor((63l)) == (7l)); assert(largestPrimeFactor((330l)) == (11l)); assert(largestPrimeFactor((13195l)) == (29l)); } }
humaneval-HumanEval_93_encode.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 { // 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_91_is_bored.json-L25
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // You'll be given a string of words, and your task is to count the number // of boredoms. A boredom is a sentence that starts with the word "I". // Sentences are delimited by '.', '?' or '!'. // For example: // >>> isBored(("Hello world")) // (0l) // >>> isBored(("The sky is blue. The sun is shining. I love this weather")) // (1l) public static long isBored(String S) { long boredoms = 0; String[] sentences = S.split("\\.\\s|\\?\\s|\\!\\s"); for (String sentence : sentences) { String[] words = sentence.split("\\s"); if (words.length > 0 && words[0].equals("I")) { boredoms++; }
return boredoms; } }
}
} public static void main(String[] args) { assert(isBored(("Hello world")) == (0l)); assert(isBored(("Is the sky blue?")) == (0l)); assert(isBored(("I love It !")) == (1l)); assert(isBored(("bIt")) == (0l)); assert(isBored(("I feel good today. I will be productive. will kill It")) == (2l)); assert(isBored(("You and I are going for a walk")) == (0l)); } }
humaneval-HumanEval_75_is_multiply_prime.json-L24
py
def is_multiply_prime(a: int) -> bool: """Write a function that returns true if the given number is the multiplication of 3 prime numbers and false otherwise. Knowing that (a) is less then 100. Example: >>> is_multiply_prime(30) True 30 = 2 * 3 * 5 """ n = a d = 2 prime_factors = [] while d * d <= n: if n % d == 0: prime_factors.append(d) n //= d else: d += 1 if n > 1: prime_factors.append(n) if len(prime_factors) == 3: return True else:
return False
def check(candidate): assert candidate(5) == False assert candidate(30) == True assert candidate(8) == True assert candidate(10) == False assert candidate(125) == True assert candidate(105) == True assert candidate(126) == False assert candidate(729) == False assert candidate(891) == False assert candidate(1001) == True def test_check(): check(is_multiply_prime) test_check()
humaneval-HumanEval_108_count_nums.json-L23
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Write a function count_nums which takes an array array list 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. // >>> countNums((new ArrayList<Long>(Arrays.asList()))) // (0l) // >>> countNums((new ArrayList<Long>(Arrays.asList((long)-1l, (long)11l, (long)-11l)))) // (1l) // >>> countNums((new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)2l)))) // (3l) public static long countNums(ArrayList<Long> arr) { long result = 0l; for (long i : arr) { String str = String.valueOf(i);
if (str.startsWith("-")) { sum = sum - (int) Character.getNumericValue(str.charAt(1)); for (int j = 2; j < str.length(); j++) { sum = sum + (int) Character.getNumericValue(str.charAt(j)); } } else { for (int j = 0; j < str.length(); j++) { sum = sum + (int) Character.getNumericValue(str.charAt(j)); } } if (sum > 0) { result++; } } return result; } }
int sum = 0;
} public static void main(String[] args) { assert(countNums((new ArrayList<Long>(Arrays.asList()))) == (0l)); assert(countNums((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)0l)))) == (0l)); assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)2l, (long)-2l, (long)3l, (long)4l, (long)5l)))) == (6l)); assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l, (long)6l, (long)9l, (long)-6l, (long)0l, (long)1l, (long)5l)))) == (5l)); assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l, (long)100l, (long)98l, (long)-7l, (long)1l, (long)-1l)))) == (4l)); assert(countNums((new ArrayList<Long>(Arrays.asList((long)12l, (long)23l, (long)34l, (long)-45l, (long)-56l, (long)0l)))) == (5l)); assert(countNums((new ArrayList<Long>(Arrays.asList((long)0l, (long)1l)))) == (1l)); assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l)))) == (1l)); } }
humaneval-HumanEval_109_move_one_ball.json-L36
js
//We have an array 'arr' of N integers arr[1], arr[2], ..., arr[N].The // numbers in the array will be randomly ordered. Your task is to determine if // it is possible to get an array sorted in non-decreasing order by performing // the following operation on the given array: // You are allowed to perform right shift operation any number of times. // One right shift operation means shifting all elements of the array by one // position in the right direction. The last element of the array will be moved to // the starting position in the array i.e. 0th index. // If it is possible to obtain the sorted array by performing the above operation // then return true else return false. // If the given array is empty then return true. // Note: The given array is guaranteed to have unique elements. // For Example: // >>> move_one_ball([3, 4, 5, 1, 2]) // true // Explanation: By performin 2 right shift operations, non-decreasing order can // be achieved for the given array. // >>> move_one_ball([3, 5, 4, 1, 2]) // false // Explanation:It is not possible to get non-decreasing order for the given // array by performing any number of right shift operations. function move_one_ball(arr){ var diff_count = 0; var i = 0; var temp = 0; for(var i = 0; i < arr.length - 1; i++){ if(arr[i] > arr[i + 1]){ diff_count += 1; } } if(diff_count > 1){ return false; } else{ return true;
}
}
const assert = require('node:assert'); function test() { let candidate = move_one_ball; assert.deepEqual(candidate([3, 4, 5, 1, 2]),true); assert.deepEqual(candidate([3, 5, 10, 1, 2]),true); assert.deepEqual(candidate([4, 3, 1, 2]),false); assert.deepEqual(candidate([3, 5, 4, 1, 2]),false); assert.deepEqual(candidate([]),true); } test();
humaneval-HumanEval_81_numerical_letter_grade.json-L47
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+"); }
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; }
else if (grades[i] > 1.7){
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_148_bf.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 { // There are eight planets in our solar system: the closerst to the Sun // is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, // Uranus, Neptune. // Write a function that takes two planet names as strings planet1 and planet2. // The function should return a pair containing all planets whose orbits are // located between the orbit of planet1 and the orbit of planet2, sorted by // the proximity to the sun. // The function should return an empty pair if planet1 or planet2 // are not correct planet names. // Examples // >>> bf(("Jupiter"), ("Neptune")) // (new ArrayList<String>(Arrays.asList((String)"Saturn", (String)"Uranus"))) // >>> bf(("Earth"), ("Mercury")) // (ArrayList<String>("Venus")) // >>> bf(("Mercury"), ("Uranus")) // (new ArrayList<String>(Arrays.asList((String)"Venus", (String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn"))) public static ArrayList<String> bf(String planet1, String planet2) { ArrayList<String> planets = new ArrayList<String>(Arrays.asList((String)"Mercury", (String)"Venus", (String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn", (String)"Uranus", (String)"Neptune")); int indexPlanet1 = planets.indexOf(planet1); int indexPlanet2 = planets.indexOf(planet2); int indexMin = 0; int indexMax = 0; if (indexPlanet1 >= 0 && indexPlanet2 >= 0) { if (indexPlanet1 < indexPlanet2) { indexMin = indexPlanet1; indexMax = indexPlanet2; } else { indexMin = indexPlanet2; indexMax = indexPlanet1; } ArrayList<String> planetsInBetween = new ArrayList<String>(); for (int i = indexMin + 1; i < indexMax; i++) { planetsInBetween.add(planets.get(i)); } return planetsInBetween;
return new ArrayList<String>(); } } }
} else {
} public static void main(String[] args) { assert(bf(("Jupiter"), ("Neptune")).equals((new ArrayList<String>(Arrays.asList((String)"Saturn", (String)"Uranus"))))); assert(bf(("Earth"), ("Mercury")).equals((new ArrayList<String>(Arrays.asList((String)"Venus"))))); assert(bf(("Mercury"), ("Uranus")).equals((new ArrayList<String>(Arrays.asList((String)"Venus", (String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn"))))); assert(bf(("Neptune"), ("Venus")).equals((new ArrayList<String>(Arrays.asList((String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn", (String)"Uranus"))))); assert(bf(("Earth"), ("Earth")).equals((new ArrayList<String>(Arrays.asList())))); assert(bf(("Mars"), ("Earth")).equals((new ArrayList<String>(Arrays.asList())))); assert(bf(("Jupiter"), ("Makemake")).equals((new ArrayList<String>(Arrays.asList())))); } }
humaneval-HumanEval_86_anti_shuffle.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 takes a string and returns an ordered version of it. // Ordered version of string, is a string where all words (separated by space) // are replaced by a new word where all the characters arranged in // ascending order based on ascii value. // Note: You should keep the order of words and blank spaces in the sentence. // For example: // >>> antiShuffle(("Hi")) // ("Hi") // >>> antiShuffle(("hello")) // ("ehllo") // >>> antiShuffle(("Hello World!!!")) // ("Hello !!!Wdlor") public static String antiShuffle(String s) { StringBuilder sb = new StringBuilder(); String[] words = s.split(" ");
char[] chars = word.toCharArray(); Arrays.sort(chars); sb.append(new String(chars)); sb.append(" "); } sb.deleteCharAt(sb.length() - 1); return sb.toString(); } }
for (String word : words) {
} public static void main(String[] args) { assert(antiShuffle(("Hi")).equals(("Hi"))); assert(antiShuffle(("hello")).equals(("ehllo"))); assert(antiShuffle(("number")).equals(("bemnru"))); assert(antiShuffle(("abcd")).equals(("abcd"))); assert(antiShuffle(("Hello World!!!")).equals(("Hello !!!Wdlor"))); assert(antiShuffle(("")).equals((""))); assert(antiShuffle(("Hi. My name is Mister Robot. How are you?")).equals((".Hi My aemn is Meirst .Rboot How aer ?ouy"))); } }
humaneval-HumanEval_37_sort_even.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 { // This function takes an array array list l and returns an array array list l' such that // l' is identical to l in the odd indicies, while its values at the even indicies are equal // to the values of the even indicies of l, but sorted. // >>> sortEven((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))) // >>> sortEven((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)3l, (long)4l)))) // (new ArrayList<Long>(Arrays.asList((long)3l, (long)6l, (long)5l, (long)4l))) public static ArrayList<Long> sortEven(ArrayList<Long> l) { ArrayList<Long> res = new ArrayList<Long>(); ArrayList<Long> evenIndexed = new ArrayList<Long>(); int index = 0; for (Long num : l) { if (index % 2 == 0) { evenIndexed.add(num); } index++; } Collections.sort(evenIndexed); index = 0; for (Long num : l) { if (index % 2 == 0) { res.add(evenIndexed.get(0)); evenIndexed.remove(0); } else { res.add(num); } index++; } return res; }
}
} public static void main(String[] args) { assert(sortEven((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))))); assert(sortEven((new ArrayList<Long>(Arrays.asList((long)5l, (long)3l, (long)-5l, (long)2l, (long)-3l, (long)3l, (long)9l, (long)0l, (long)123l, (long)1l, (long)-10l)))).equals((new ArrayList<Long>(Arrays.asList((long)-10l, (long)3l, (long)-5l, (long)2l, (long)-3l, (long)3l, (long)5l, (long)0l, (long)9l, (long)1l, (long)123l))))); assert(sortEven((new ArrayList<Long>(Arrays.asList((long)5l, (long)8l, (long)-12l, (long)4l, (long)23l, (long)2l, (long)3l, (long)11l, (long)12l, (long)-10l)))).equals((new ArrayList<Long>(Arrays.asList((long)-12l, (long)8l, (long)3l, (long)4l, (long)5l, (long)2l, (long)12l, (long)11l, (long)23l, (long)-10l))))); } }
humaneval-HumanEval_137_compare_one.json-L35
js
//Create a function that takes integers, floats, or strings representing // real numbers, and returns the larger variable in its given variable type. // Return undefined if the values are equal. // Note: If a real number is represented as a string, the floating point might be . or , // >>> compare_one(1, 2.5) // 2.5 // >>> compare_one(1, "2,3") // "2,3" // >>> compare_one("5,1", "6") // "6" // >>> compare_one("1", 1) // undefined function compare_one(a, b){ if (typeof a === typeof b){ if (a > b) { return a; } else if (a < b) { return b; } else { return undefined; } } else if (typeof a === "string" && typeof b === "number"){ if (a[0] > b){ return a; } else if (a[0] < b){ return b; } else { return undefined; } } else if (typeof a === "number" && typeof b === "string"){ if (a > b[0]){ return a;
return b; } else { return undefined; } } }
} else if (a < b[0]){
const assert = require('node:assert'); function test() { let candidate = compare_one; assert.deepEqual(candidate(1, 2),2); assert.deepEqual(candidate(1, 2.5),2.5); assert.deepEqual(candidate(2, 3),3); assert.deepEqual(candidate(5, 6),6); assert.deepEqual(candidate(1, "2,3"),"2,3"); assert.deepEqual(candidate("5,1", "6"),"6"); assert.deepEqual(candidate("1", "2"),"2"); assert.deepEqual(candidate("1", 1),undefined); } test();
humaneval-HumanEval_160_do_algebra.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 { // Given two array lists operator, and operand. The first array list has basic algebra operations, and // the second array list is an array array list of integers. Use the two given array lists 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 array list = [2, 3, 4, 5] // result = 2 + 3 * 4 - 5 // => result = 9 // Note: // The length of operator array list is equal to the length of operand array list minus one. // Operand is an array array list of of non-negative integers. // Operator array list has at least one operator, and operand array list has at least two operands. public static long doAlgebra(ArrayList<String> op, ArrayList<Long> operand) { ArrayList<Long> list = new ArrayList<>();
for (int i = 0; i < op.size(); i++) { if (op.get(i).equals("+")) list.add(operand.get(i + 1)); else if (op.get(i).equals("-")) list.add(-operand.get(i + 1)); else if (op.get(i).equals("*")) list.set(list.size() - 1, list.get(list.size() - 1) * operand.get(i + 1)); else if (op.get(i).equals("//")) list.set(list.size() - 1, list.get(list.size() - 1) / operand.get(i + 1)); else if (op.get(i).equals("**")) list.set(list.size() - 1, (long) Math.pow(list.get(list.size() - 1), operand.get(i + 1))); } return list.stream().mapToLong(x -> x).sum(); } }
list.add(operand.get(0));
} public static void main(String[] args) { assert(doAlgebra((new ArrayList<String>(Arrays.asList((String)"**", (String)"*", (String)"+"))), (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)5l)))) == (37l)); assert(doAlgebra((new ArrayList<String>(Arrays.asList((String)"+", (String)"*", (String)"-"))), (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)5l)))) == (9l)); assert(doAlgebra((new ArrayList<String>(Arrays.asList((String)"//", (String)"*"))), (new ArrayList<Long>(Arrays.asList((long)7l, (long)3l, (long)4l)))) == (8l)); } }
humaneval-HumanEval_81_numerical_letter_grade.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 { // 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+");
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) { 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.7f) {
} 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_63_fibfib.json-L17
py
def fibfib(n: int) -> int: """The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fibfib(0) == 0 fibfib(1) == 0 fibfib(2) == 1 fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3). Please write a function to efficiently compute the n-th element of the fibfib number sequence. >>> fibfib(1) 0 >>> fibfib(5) 4 >>> fibfib(8) 24 """ a, b, c = 0, 0, 1 while n > 0:
n -= 1 return a
a, b, c = b, c, a + b + c
def check(candidate): assert candidate(2) == 1 assert candidate(1) == 0 assert candidate(5) == 4 assert candidate(8) == 24 assert candidate(10) == 81 assert candidate(12) == 274 assert candidate(14) == 927 def test_check(): check(fibfib) test_check()
humaneval-HumanEval_119_match_parens.json-L28
py
from typing import List def match_parens(lst: List[str]) -> str: """ You are given a list of two strings, both strings consist of open parentheses '(' or close parentheses ')' only. Your job is to check if it is possible to concatenate the two strings in some order, that the resulting string will be good. A string S is considered to be good if and only if all parentheses in S are balanced. For example: the string '(())()' is good, while the string '())' is not. Return 'Yes' if there's a way to make a good string, and return 'No' otherwise. Examples: >>> match_parens(['()(', ')']) 'Yes' >>> match_parens([')', ')']) 'No' """ s1, s2 = lst strings = [s1 + s2, s2 + s1] for s in strings: open_parens = 0 close_parens = 0 for c in s: if c == '(': open_parens += 1
if open_parens: open_parens -= 1 else: close_parens += 1 if open_parens == 0 and close_parens == 0: return 'Yes' return 'No'
elif c == ')':
def check(candidate): assert candidate(['()(', ')']) == 'Yes' assert candidate([')', ')']) == 'No' assert candidate(['(()(())', '())())']) == 'No' assert candidate([')())', '(()()(']) == 'Yes' assert candidate(['(())))', '(()())((']) == 'Yes' assert candidate(['()', '())']) == 'No' assert candidate(['(()(', '()))()']) == 'Yes' assert candidate(['((((', '((())']) == 'No' assert candidate([')(()', '(()(']) == 'No' assert candidate([')(', ')(']) == 'No' assert candidate(['(', ')']) == 'Yes' assert candidate([')', '(']) == 'Yes' def test_check(): check(match_parens) test_check()
humaneval-HumanEval_151_double_the_difference.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 an array array list of numbers, return the sum of squares of the numbers // in the array list that are odd. Ignore numbers that are negative or not integers. // >>> doubleTheDifference((new ArrayList<Float>(Arrays.asList((long)1l, (long)3l, (long)2l, (long)0l)))) // (10l) // >>> doubleTheDifference((new ArrayList<Float>(Arrays.asList((long)-1l, (long)-2l, (long)0l)))) // (0l) // >>> doubleTheDifference((new ArrayList<Float>(Arrays.asList((long)9l, (long)-2l)))) // (81l) // >>> doubleTheDifference((new ArrayList<Float>(Arrays.asList((long)0l)))) // (0l) // If the input array list is empty, return 0. public static long doubleTheDifference(ArrayList<Float> lst) { return lst.stream().filter(x -> x%2 == 1).mapToLong(Math::round).map(x -> x*x).sum();
}
}
} public static void main(String[] args) { assert(doubleTheDifference((new ArrayList<Float>(Arrays.asList()))) == (0l)); assert(doubleTheDifference((new ArrayList<Float>(Arrays.asList((float)5.0f, (float)4.0f)))) == (25l)); assert(doubleTheDifference((new ArrayList<Float>(Arrays.asList((float)0.1f, (float)0.2f, (float)0.3f)))) == (0l)); assert(doubleTheDifference((new ArrayList<Float>(Arrays.asList((float)-10.0f, (float)-20.0f, (float)-30.0f)))) == (0l)); assert(doubleTheDifference((new ArrayList<Float>(Arrays.asList((float)-1.0f, (float)-2.0f, (float)8.0f)))) == (0l)); assert(doubleTheDifference((new ArrayList<Float>(Arrays.asList((float)0.2f, (float)3.0f, (float)5.0f)))) == (34l)); assert(doubleTheDifference((new ArrayList<Float>(Arrays.asList((float)-9.0f, (float)-7.0f, (float)-5.0f, (float)-3.0f, (float)-1.0f, (float)1.0f, (float)3.0f, (float)5.0f, (float)7.0f, (float)9.0f)))) == (165l)); } }
humaneval-HumanEval_42_incr_list.json-L19
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Return array list with elements incremented by 1. // >>> incrList((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l))) // >>> incrList((new ArrayList<Long>(Arrays.asList((long)5l, (long)3l, (long)5l, (long)2l, (long)3l, (long)3l, (long)9l, (long)0l, (long)123l)))) // (new ArrayList<Long>(Arrays.asList((long)6l, (long)4l, (long)6l, (long)3l, (long)4l, (long)4l, (long)10l, (long)1l, (long)124l))) public static ArrayList<Long> incrList(ArrayList<Long> l) { for (int i = 0; i < l.size(); i++) { l.set(i, l.get(i) + 1l); } return l;
}
}
} public static void main(String[] args) { assert(incrList((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<Long>(Arrays.asList())))); assert(incrList((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)4l, (long)3l, (long)2l))))); assert(incrList((new ArrayList<Long>(Arrays.asList((long)5l, (long)2l, (long)5l, (long)2l, (long)3l, (long)3l, (long)9l, (long)0l, (long)123l)))).equals((new ArrayList<Long>(Arrays.asList((long)6l, (long)3l, (long)6l, (long)3l, (long)4l, (long)4l, (long)10l, (long)1l, (long)124l))))); } }
humaneval-HumanEval_59_largest_prime_factor.json-L9
js
//Return the largest prime factor of n. Assume n > 1 and is not a prime. // >>> largest_prime_factor(13195) // 29 // >>> largest_prime_factor(2048) // 2 function largest_prime_factor(n){ var result = 2; while(n > result){
n = n / result; } else{ result = result + 1; } } return result; }
if(n % result == 0){
const assert = require('node:assert'); function test() { let candidate = largest_prime_factor; assert.deepEqual(candidate(15),5); assert.deepEqual(candidate(27),3); assert.deepEqual(candidate(63),7); assert.deepEqual(candidate(330),11); assert.deepEqual(candidate(13195),29); } test();
humaneval-HumanEval_36_fizz_buzz.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 { // Return the number of times the digit 7 appears in integers less than n which are divisible by 11 or 13. // >>> fizzBuzz((50l)) // (0l) // >>> fizzBuzz((78l)) // (2l) // >>> fizzBuzz((79l)) // (3l) public static long fizzBuzz(long n) { long res = 0;
if (i % 11 == 0 || i % 13 == 0) { String s = String.valueOf(i); for (int j = 0; j < s.length(); j++) { if (s.charAt(j) == '7') { res++; } } } } return res; } }
for (long i = 0; i < n; i++) {
} public static void main(String[] args) { assert(fizzBuzz((50l)) == (0l)); assert(fizzBuzz((78l)) == (2l)); assert(fizzBuzz((79l)) == (3l)); assert(fizzBuzz((100l)) == (3l)); assert(fizzBuzz((200l)) == (6l)); assert(fizzBuzz((4000l)) == (192l)); assert(fizzBuzz((10000l)) == (639l)); assert(fizzBuzz((100000l)) == (8026l)); } }
humaneval-HumanEval_156_int_to_mini_roman.json-L33
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' } roman_numeral = '' for key in sorted(roman_numerals.keys(), reverse=True): roman_numeral += roman_numerals[key] * (number // key)
return roman_numeral
number = number % key
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_104_unique_digits.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 an array array list of positive integers x. return a sorted array list of all // elements that hasn't any even digit. // Note: Returned array list should be sorted in increasing order. // For example: // >>> uniqueDigits((new ArrayList<Long>(Arrays.asList((long)15l, (long)33l, (long)1422l, (long)1l)))) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)15l, (long)33l))) // >>> uniqueDigits((new ArrayList<Long>(Arrays.asList((long)152l, (long)323l, (long)1422l, (long)10l)))) // (new ArrayList<Long>(Arrays.asList())) public static ArrayList<Long> uniqueDigits(ArrayList<Long> x) { ArrayList<Long> b = new ArrayList<Long>(); for (int i = 0; i < x.size(); i++) { if (x.get(i) < 0) { x.set(i, x.get(i)*(long)-1); } boolean unique = true; long z = x.get(i); while (z > 0) { long y = z % 10; if (y % 2 == 0) { unique = false; break;
z = z / 10; } if (unique) { b.add(x.get(i)); } } Collections.sort(b); return b; } }
}
} public static void main(String[] args) { assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)15l, (long)33l, (long)1422l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)15l, (long)33l))))); assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)152l, (long)323l, (long)1422l, (long)10l)))).equals((new ArrayList<Long>(Arrays.asList())))); assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)12345l, (long)2033l, (long)111l, (long)151l)))).equals((new ArrayList<Long>(Arrays.asList((long)111l, (long)151l))))); assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)135l, (long)103l, (long)31l)))).equals((new ArrayList<Long>(Arrays.asList((long)31l, (long)135l))))); } }
humaneval-HumanEval_128_prod_signs.json-L18
py
from typing import List, Optional def prod_signs(arr: List[int]) -> Optional[int]: """ You are given an array arr of integers and you need to return sum of magnitudes of integers multiplied by product of all signs of each number in the array, represented by 1, -1 or 0. Note: return None for empty arr. Example: >>> prod_signs([1, 2, 2, -4]) 9 >>> prod_signs([0, 1]) 0 >>> prod_signs([]) None """
out = 1 for item in lst: out *= item return out if len(arr) == 0: return None else: sign_arr = [] for num in arr: if num > 0: sign_arr.append(1) elif num < 0: sign_arr.append(-1) else: sign_arr.append(0) return sum(map(abs, arr)) * product(sign_arr)
def product(lst: List[int]) -> int:
def check(candidate): assert candidate([1, 2, 2, -4]) == -9 assert candidate([0, 1]) == 0 assert candidate([1, 1, 1, 2, 3, -1, 1]) == -10 assert candidate([]) == None assert candidate([2, 4, 1, 2, -1, -1, 9]) == 20 assert candidate([-1, 1, -1, 1]) == 4 assert candidate([-1, 1, 1, 1]) == -4 assert candidate([-1, 1, 1, 0]) == 0 def test_check(): check(prod_signs) test_check()
humaneval-HumanEval_117_select_words.json-L43
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given a string s and a natural number n, you have been tasked to implement // a function that returns an array array list of all words from string s that contain exactly // n consonants, in order these words appear in the string s. // If the string s is empty then the function should return an empty array list. // Note: you may assume the input string contains only letters and spaces. // Examples: // >>> selectWords(("Mary had a little lamb"), (4l)) // (new ArrayList<String>(Arrays.asList((String)"little"))) // >>> selectWords(("Mary had a little lamb"), (3l)) // (new ArrayList<String>(Arrays.asList((String)"Mary", (String)"lamb"))) // >>> selectWords(("simple white space"), (2l)) // (new ArrayList<String>(Arrays.asList())) // >>> selectWords(("Hello world"), (4l)) // (new ArrayList<String>(Arrays.asList((String)"world"))) // >>> selectWords(("Uncle sam"), (3l)) // (new ArrayList<String>(Arrays.asList((String)"Uncle"))) public static ArrayList<String> selectWords(String s, long n) { ArrayList<String> ret = new ArrayList<String>(); String[] words = s.split(" "); for (String word : words) { String wordInLowerCase = word.toLowerCase(); long consonants = 0l; for (char c : wordInLowerCase.toCharArray()) { switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': break; default: consonants++; break; }
if (consonants == n) { ret.add(word); } } return ret; } }
}
} public static void main(String[] args) { assert(selectWords(("Mary had a little lamb"), (4l)).equals((new ArrayList<String>(Arrays.asList((String)"little"))))); assert(selectWords(("Mary had a little lamb"), (3l)).equals((new ArrayList<String>(Arrays.asList((String)"Mary", (String)"lamb"))))); assert(selectWords(("simple white space"), (2l)).equals((new ArrayList<String>(Arrays.asList())))); assert(selectWords(("Hello world"), (4l)).equals((new ArrayList<String>(Arrays.asList((String)"world"))))); assert(selectWords(("Uncle sam"), (3l)).equals((new ArrayList<String>(Arrays.asList((String)"Uncle"))))); assert(selectWords((""), (4l)).equals((new ArrayList<String>(Arrays.asList())))); assert(selectWords(("a b c d e f"), (1l)).equals((new ArrayList<String>(Arrays.asList((String)"b", (String)"c", (String)"d", (String)"f"))))); } }
humaneval-HumanEval_64_vowels_count.json-L14
js
//Write a function vowels_count which takes a string representing // a word as input and returns the number of vowels in the string. // Vowels in this case are 'a', 'e', 'i', 'o', 'u'. Here, 'y' is also a // vowel, but only when it is at the end of the given word. // Example: // >>> vowels_count("abcde") // 2 // >>> vowels_count("ACEDY") // 3 function vowels_count(s){ s=s.toLowerCase(); var vowels = ['a','e','i','o','u']; var count = 0;
if(vowels.indexOf(s[i])!=-1){ count++; } } if(s[s.length-1]=='y'){ count++; } return count; }
for(var i=0; i<s.length; i++){
const assert = require('node:assert'); function test() { let candidate = vowels_count; assert.deepEqual(candidate("abcde"),2); assert.deepEqual(candidate("Alone"),3); assert.deepEqual(candidate("key"),2); assert.deepEqual(candidate("bye"),1); assert.deepEqual(candidate("keY"),2); assert.deepEqual(candidate("bYe"),1); assert.deepEqual(candidate("ACEDY"),3); } test();
humaneval-HumanEval_56_correct_bracketing.json-L25
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // 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; for (int i = 0; i < brackets.length(); i++) { char c = brackets.charAt(i); if (c == '<') { open++;
open--; } if (open < 0) { return false; } } return open == 0; } }
} else {
} 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_96_count_up_to.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 { // Implement a function that takes an non-negative integer and returns an array array list of the first n // integers that are prime numbers and less than n. // for example: // >>> countUpTo((5l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l))) // >>> countUpTo((11l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l))) // >>> countUpTo((0l)) // (new ArrayList<Long>(Arrays.asList())) // >>> countUpTo((20l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l))) // >>> countUpTo((1l)) // (new ArrayList<Long>(Arrays.asList())) // >>> countUpTo((18l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l))) public static ArrayList<Long> countUpTo(long n) { ArrayList<Long> arrayList = new ArrayList<Long>(); for (long i = 2l; i < n; i++) { boolean isPrime = true; for (long j = 2l; j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) { arrayList.add(i); } } return arrayList;
}
}
} public static void main(String[] args) { assert(countUpTo((5l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l))))); assert(countUpTo((6l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l))))); assert(countUpTo((7l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l))))); assert(countUpTo((10l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l))))); assert(countUpTo((0l)).equals((new ArrayList<Long>(Arrays.asList())))); assert(countUpTo((22l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l))))); assert(countUpTo((1l)).equals((new ArrayList<Long>(Arrays.asList())))); assert(countUpTo((18l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l))))); assert(countUpTo((47l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l, (long)23l, (long)29l, (long)31l, (long)37l, (long)41l, (long)43l))))); assert(countUpTo((101l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l, (long)23l, (long)29l, (long)31l, (long)37l, (long)41l, (long)43l, (long)47l, (long)53l, (long)59l, (long)61l, (long)67l, (long)71l, (long)73l, (long)79l, (long)83l, (long)89l, (long)97l))))); } }
humaneval-HumanEval_119_match_parens.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 { // You are given an array array list of two strings, both strings consist of open // parentheses '(' or close parentheses ')' only. // Your job is to check if it is possible to concatenate the two strings in // some order, that the resulting string will be good. // A string S is considered to be good if and only if all parentheses in S // are balanced. For example: the string '(())()' is good, while the string // '())' is not. // Return 'Yes' if there's a way to make a good string, and return 'No' otherwise. // Examples: // >>> matchParens((new ArrayList<String>(Arrays.asList((String)"()(", (String)")")))) // ("Yes") // >>> matchParens((new ArrayList<String>(Arrays.asList((String)")", (String)")")))) // ("No") public static String matchParens(ArrayList<String> lst) { String s1 = lst.get(0); String s2 = lst.get(1); String s3 = s1+s2; String s4 = s2+s1; int c1 = 0; int c2 = 0;
if (s3.charAt(i) == '(') c1 += 1; else c1 -= 1; if (c1 < 0) break; } for (int i = 0; i < s4.length(); i++) { if (s4.charAt(i) == '(') c2 += 1; else c2 -= 1; if (c2 < 0) break; } if (c1 == 0 || c2 == 0) return "Yes"; else return "No"; } }
for (int i = 0; i < s3.length(); i++) {
} public static void main(String[] args) { assert(matchParens((new ArrayList<String>(Arrays.asList((String)"()(", (String)")")))).equals(("Yes"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)")", (String)")")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(()(())", (String)"())())")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)")())", (String)"(()()(")))).equals(("Yes"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(())))", (String)"(()())((")))).equals(("Yes"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"()", (String)"())")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(()(", (String)"()))()")))).equals(("Yes"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"((((", (String)"((())")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)")(()", (String)"(()(")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)")(", (String)")(")))).equals(("No"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(", (String)")")))).equals(("Yes"))); assert(matchParens((new ArrayList<String>(Arrays.asList((String)")", (String)"(")))).equals(("Yes"))); } }
humaneval-HumanEval_96_count_up_to.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 { // Implement a function that takes an non-negative integer and returns an array array list of the first n // integers that are prime numbers and less than n. // for example: // >>> countUpTo((5l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l))) // >>> countUpTo((11l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l))) // >>> countUpTo((0l)) // (new ArrayList<Long>(Arrays.asList())) // >>> countUpTo((20l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l))) // >>> countUpTo((1l)) // (new ArrayList<Long>(Arrays.asList())) // >>> countUpTo((18l)) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l))) public static ArrayList<Long> countUpTo(long n) { ArrayList<Long> arrayList = new ArrayList<Long>(); for (long i = 2l; i < n; i++) {
for (long j = 2l; j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) { arrayList.add(i); } } return arrayList; } }
boolean isPrime = true;
} public static void main(String[] args) { assert(countUpTo((5l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l))))); assert(countUpTo((6l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l))))); assert(countUpTo((7l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l))))); assert(countUpTo((10l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l))))); assert(countUpTo((0l)).equals((new ArrayList<Long>(Arrays.asList())))); assert(countUpTo((22l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l))))); assert(countUpTo((1l)).equals((new ArrayList<Long>(Arrays.asList())))); assert(countUpTo((18l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l))))); assert(countUpTo((47l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l, (long)23l, (long)29l, (long)31l, (long)37l, (long)41l, (long)43l))))); assert(countUpTo((101l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l, (long)19l, (long)23l, (long)29l, (long)31l, (long)37l, (long)41l, (long)43l, (long)47l, (long)53l, (long)59l, (long)61l, (long)67l, (long)71l, (long)73l, (long)79l, (long)83l, (long)89l, (long)97l))))); } }
humaneval-HumanEval_115_max_fill.json-L31
js
//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: // >>> max_fill([[0, 0, 1, 0], [0, 1, 0, 0], [1, 1, 1, 1]], 1) // 6 // Example 2: // >>> max_fill([[0, 0, 1, 1], [0, 0, 0, 0], [1, 1, 1, 1], [0, 1, 1, 1]], 2) // 5 // Example 3: // >>> max_fill([[0, 0, 0], [0, 0, 0]], 5) // 0 // 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 function max_fill(grid, capacity){ var maxFill = 0; var sum = 0; var c = 1; var r = grid.length; var c = grid[0].length; for(var i = 0; i < r; i++){ sum = 0; for(var j = 0; j < c; j++){
} maxFill += Math.ceil(sum/capacity); } return maxFill; }
sum += grid[i][j];
const assert = require('node:assert'); function test() { let candidate = max_fill; assert.deepEqual(candidate([[0, 0, 1, 0], [0, 1, 0, 0], [1, 1, 1, 1]], 1),6); assert.deepEqual(candidate([[0, 0, 1, 1], [0, 0, 0, 0], [1, 1, 1, 1], [0, 1, 1, 1]], 2),5); assert.deepEqual(candidate([[0, 0, 0], [0, 0, 0]], 5),0); assert.deepEqual(candidate([[1, 1, 1, 1], [1, 1, 1, 1]], 2),4); assert.deepEqual(candidate([[1, 1, 1, 1], [1, 1, 1, 1]], 9),2); } test();
humaneval-HumanEval_82_prime_length.json-L19
js
//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 function prime_length(string){ let length = string.length; let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]; if (primes.includes(length)){ return true; } else { return false;
}
}
const assert = require('node:assert'); function test() { let candidate = prime_length; assert.deepEqual(candidate("Hello"),true); assert.deepEqual(candidate("abcdcba"),true); assert.deepEqual(candidate("kittens"),true); assert.deepEqual(candidate("orange"),false); assert.deepEqual(candidate("wow"),true); assert.deepEqual(candidate("world"),true); assert.deepEqual(candidate("MadaM"),true); assert.deepEqual(candidate("Wow"),true); assert.deepEqual(candidate(""),false); assert.deepEqual(candidate("HI"),true); assert.deepEqual(candidate("go"),true); assert.deepEqual(candidate("gogo"),false); assert.deepEqual(candidate("aaaaaaaaaaaaaaa"),false); assert.deepEqual(candidate("Madam"),true); assert.deepEqual(candidate("M"),false); assert.deepEqual(candidate("0"),false); } test();
humaneval-HumanEval_19_sort_numbers.json-L10
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,
"two": 2, "three": 3, "four": 4, "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; }
"one": 1,
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_111_histogram.json-L48
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given a string representing a space separated lowercase letters, return a hash map // of the letter with the most repetition and containing the corresponding count. // If several letters have the same occurrence, return all of them. // Example: // >>> histogram(("a b c")) // (new HashMap<String,Long>(Map.of("a", 1l, "b", 1l, "c", 1l))) // >>> histogram(("a b b a")) // (new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))) // >>> histogram(("a b c a b")) // (new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))) // >>> histogram(("b b b b a")) // (new HashMap<String,Long>(Map.of("b", 4l))) // >>> histogram(("")) // (new HashMap<String,Long>()) public static HashMap<String,Long> histogram(String test) { HashMap<String,Long> s = new HashMap<String,Long>(); if (test.length() > 0) { HashMap<String,Long> hm = new HashMap<String,Long>(); String[] ss = test.split(" "); for (String sss : ss) { if (hm.containsKey(sss)) { hm.put(sss, hm.get(sss) + 1); } else { hm.put(sss, 1l); } } long max = 0; for (String key : hm.keySet()) { if (hm.get(key) > max) { max = hm.get(key); } } for (String key : hm.keySet()) { if (hm.get(key) == max) { s.put(key, hm.get(key)); } } } return s;
}
}
} public static void main(String[] args) { assert(histogram(("a b b a")).equals((new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))))); assert(histogram(("a b c a b")).equals((new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))))); assert(histogram(("a b c d g")).equals((new HashMap<String,Long>(Map.of("a", 1l, "b", 1l, "c", 1l, "d", 1l, "g", 1l))))); assert(histogram(("r t g")).equals((new HashMap<String,Long>(Map.of("r", 1l, "t", 1l, "g", 1l))))); assert(histogram(("b b b b a")).equals((new HashMap<String,Long>(Map.of("b", 4l))))); assert(histogram(("r t g")).equals((new HashMap<String,Long>(Map.of("r", 1l, "t", 1l, "g", 1l))))); assert(histogram(("")).equals((new HashMap<String,Long>()))); assert(histogram(("a")).equals((new HashMap<String,Long>(Map.of("a", 1l))))); } }
humaneval-HumanEval_6_parse_nested_parens.json-L22
py
from typing import List def parse_nested_parens(paren_string: str) -> List[int]: """ Input to this function is a string represented multiple groups for nested parentheses separated by spaces. For each of the group, output the deepest level of nesting of parentheses. E.g. (()()) has maximum two levels of nesting while ((())) has three. >>> parse_nested_parens('(()()) ((())) () ((())()())') [2, 3, 1, 3] """ result = [] for parens in paren_string.split(' '): max_depth = 0 current_depth = 0 for paren in parens: if paren == '(': current_depth += 1 max_depth = max(max_depth, current_depth) elif paren == ')': current_depth -= 1 result.append(max_depth)
return result
def check(candidate): assert candidate('(()()) ((())) () ((())()())') == [2, 3, 1, 3] assert candidate('() (()) ((())) (((())))') == [1, 2, 3, 4] assert candidate('(()(())((())))') == [4] def test_check(): check(parse_nested_parens) test_check()
humaneval-HumanEval_156_int_to_mini_roman.json-L27
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" };
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; }
var 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_105_by_length.json-L40
js
//Given an array of integers, sort the integers that are between 1 and 9 inclusive, // reverse the resulting array, and then replace each digit by its corresponding name from // "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine". // For example: // >>> by_length([2, 1, 1, 4, 5, 8, 2, 3]) // ["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"] // If the array is empty, return an empty array: // >>> by_length([]) // [] // If the array has any strange number ignore it: // >>> by_length([1, -1, 55]) // ["One"] function by_length(arr){ var arr2 = []; for(var i = 0; i < arr.length; i++){ var num = arr[i]; if(num > 0 && num < 10){ arr2.push(num); } } arr2.sort(); arr2.reverse(); var arr3 = []; for(var i = 0; i < arr2.length; i++){ var num = arr2[i]; if(num == 1){ arr3.push("One"); } else if(num == 2){ arr3.push("Two"); } else if(num == 3){ arr3.push("Three"); } else if(num == 4){ arr3.push("Four"); } else if(num == 5){ arr3.push("Five");
else if(num == 6){ arr3.push("Six"); } else if(num == 7){ arr3.push("Seven"); } else if(num == 8){ arr3.push("Eight"); } else if(num == 9){ arr3.push("Nine"); } } return arr3; }
}
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_160_do_algebra.json-L29
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);
count = 0; do_again = true; break; case '//': operand[count] = Math.floor(operand[count] / operand[count + 1]); operand.splice(count + 1, 1); operator.splice(count, 1); count = 0; do_again = true; break; case '**': operand[count] = Math.pow(operand[count], operand[count + 1]); operand.splice(count + 1, 1); operator.splice(count, 1); count = 0; do_again = true; break; default: count++; do_again = false; } } answer = operand[0]; operand.splice(0, 1); while(operand.length > 0){ switch(operator[0]){ case '+': answer += operand[0]; break; case '-': answer -= operand[0]; break; } operand.splice(0, 1); operator.splice(0, 1); } return answer; }
operator.splice(count, 1);
const assert = require('node:assert'); function test() { let candidate = do_algebra; assert.deepEqual(candidate(["**", "*", "+"], [2, 3, 4, 5]),37); assert.deepEqual(candidate(["+", "*", "-"], [2, 3, 4, 5]),9); assert.deepEqual(candidate(["//", "*"], [7, 3, 4]),8); } test();
humaneval-HumanEval_108_count_nums.json-L33
js
//Write a function count_nums which takes an array of integers and returns // the number of elements which has a sum of digits > 0. // If a number is negative, then its first signed digit will be negative: // e.g. -123 has signed digits -1, 2, and 3. // >>> count_nums([]) // 0 // >>> count_nums([-1, 11, -11]) // 1 // >>> count_nums([1, 1, 2]) // 3 function count_nums(arr){ if(!arr.length) { return 0; } var count = 0; for(var i = 0; i < arr.length; i++) { var sum = 0; var str = arr[i].toString(); if(str[0] === '-') { sum += Number(str[0] + str[1]); for(var j = 2; j < str.length; j++) { sum += Number(str[j]); } } else { for(var k = 0; k < str.length; k++) { sum += Number(str[k]); } } if(sum > 0) { count++; } }
}
return count;
const assert = require('node:assert'); function test() { let candidate = count_nums; assert.deepEqual(candidate([]),0); assert.deepEqual(candidate([-1, -2, 0]),0); assert.deepEqual(candidate([1, 1, 2, -2, 3, 4, 5]),6); assert.deepEqual(candidate([1, 6, 9, -6, 0, 1, 5]),5); assert.deepEqual(candidate([1, 100, 98, -7, 1, -1]),4); assert.deepEqual(candidate([12, 23, 34, -45, -56, 0]),5); assert.deepEqual(candidate([0, 1]),1); assert.deepEqual(candidate([1]),1); } test();
humaneval-HumanEval_81_numerical_letter_grade.json-L63
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){
} } return grades_array; }
grades_array.push("E");
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_124_valid_date.json-L27
js
//You have to write a function which validates a given date string and // returns true if the date is valid otherwise false. // The date is valid if all of the following rules are satisfied: // 1. The date string is not empty. // 2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the number of days is not less than 1 or higher than 30 days for months 4,6,9,11. And, the number of days is not less than 1 or higher than 29 for the month 2. // 3. The months should not be less than 1 or higher than 12. // 4. The date should be in the format: mm-dd-yyyy // >>> valid_date("03-11-2000") // true // >>> valid_date("15-01-2012") // false // >>> valid_date("04-0-2040") // false // >>> valid_date("06-04-2020") // true // >>> valid_date("06/04/2020") // false function valid_date(date){ var date_arr = date.split("-"); if(date_arr.length == 3){ var month = parseInt(date_arr[0]); var day = parseInt(date_arr[1]); var year = parseInt(date_arr[2]); if(month < 1 || month > 12){ return false; }else if(month == 2){
return false; } }else if((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day < 1 || day > 31)){ return false; }else if((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)){ return false; } return true; } return false; }
if(day < 1 || day > 29){
const assert = require('node:assert'); function test() { let candidate = valid_date; assert.deepEqual(candidate("03-11-2000"),true); assert.deepEqual(candidate("15-01-2012"),false); assert.deepEqual(candidate("04-0-2040"),false); assert.deepEqual(candidate("06-04-2020"),true); assert.deepEqual(candidate("01-01-2007"),true); assert.deepEqual(candidate("03-32-2011"),false); assert.deepEqual(candidate(""),false); assert.deepEqual(candidate("04-31-3000"),false); assert.deepEqual(candidate("06-06-2005"),true); assert.deepEqual(candidate("21-31-2000"),false); assert.deepEqual(candidate("04-12-2003"),true); assert.deepEqual(candidate("04122003"),false); assert.deepEqual(candidate("20030412"),false); assert.deepEqual(candidate("2003-04"),false); assert.deepEqual(candidate("2003-04-12"),false); assert.deepEqual(candidate("04-2003"),false); } test();
humaneval-HumanEval_129_minPath.json-L54
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){
} if(y > 0){ neighbours.push(grid[x][y - 1]); } if(y < n - 1){ neighbours.push(grid[x][y + 1]); } let nextCell = neighbours[0]; for(let i = 0; i < neighbours.length; i++){ let curr = neighbours[i]; if(curr < nextCell){ nextCell = curr; } } currCell = nextCell; } return path; }
neighbours.push(grid[x + 1][y]);
const assert = require('node:assert'); function test() { let candidate = minPath; assert.deepEqual(candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3),[1, 2, 1]); assert.deepEqual(candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1),[1]); assert.deepEqual(candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4),[1, 2, 1, 2]); assert.deepEqual(candidate([[6, 4, 13, 10], [5, 7, 12, 1], [3, 16, 11, 15], [8, 14, 9, 2]], 7),[1, 10, 1, 10, 1, 10, 1]); assert.deepEqual(candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5),[1, 7, 1, 7, 1]); assert.deepEqual(candidate([[11, 8, 7, 2], [5, 16, 14, 4], [9, 3, 15, 6], [12, 13, 10, 1]], 9),[1, 6, 1, 6, 1, 6, 1, 6, 1]); assert.deepEqual(candidate([[12, 13, 10, 1], [9, 3, 15, 6], [5, 16, 14, 4], [11, 8, 7, 2]], 12),[1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6]); assert.deepEqual(candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8),[1, 3, 1, 3, 1, 3, 1, 3]); assert.deepEqual(candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8),[1, 5, 1, 5, 1, 5, 1, 5]); assert.deepEqual(candidate([[1, 2], [3, 4]], 10),[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]); assert.deepEqual(candidate([[1, 3], [3, 2]], 10),[1, 3, 1, 3, 1, 3, 1, 3, 1, 3]); } test();
humaneval-HumanEval_115_max_fill.json-L24
js
//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: // >>> max_fill([[0, 0, 1, 0], [0, 1, 0, 0], [1, 1, 1, 1]], 1) // 6 // Example 2: // >>> max_fill([[0, 0, 1, 1], [0, 0, 0, 0], [1, 1, 1, 1], [0, 1, 1, 1]], 2) // 5 // Example 3: // >>> max_fill([[0, 0, 0], [0, 0, 0]], 5) // 0 // 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 function max_fill(grid, capacity){ var maxFill = 0;
var c = 1; var r = grid.length; var c = grid[0].length; for(var i = 0; i < r; i++){ sum = 0; for(var j = 0; j < c; j++){ sum += grid[i][j]; } maxFill += Math.ceil(sum/capacity); } return maxFill; }
var sum = 0;
const assert = require('node:assert'); function test() { let candidate = max_fill; assert.deepEqual(candidate([[0, 0, 1, 0], [0, 1, 0, 0], [1, 1, 1, 1]], 1),6); assert.deepEqual(candidate([[0, 0, 1, 1], [0, 0, 0, 0], [1, 1, 1, 1], [0, 1, 1, 1]], 2),5); assert.deepEqual(candidate([[0, 0, 0], [0, 0, 0]], 5),0); assert.deepEqual(candidate([[1, 1, 1, 1], [1, 1, 1, 1]], 2),4); assert.deepEqual(candidate([[1, 1, 1, 1], [1, 1, 1, 1]], 9),2); } test();
humaneval-HumanEval_148_bf.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 { // There are eight planets in our solar system: the closerst to the Sun // is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, // Uranus, Neptune. // Write a function that takes two planet names as strings planet1 and planet2. // The function should return a pair containing all planets whose orbits are // located between the orbit of planet1 and the orbit of planet2, sorted by // the proximity to the sun. // The function should return an empty pair if planet1 or planet2 // are not correct planet names. // Examples // >>> bf(("Jupiter"), ("Neptune")) // (new ArrayList<String>(Arrays.asList((String)"Saturn", (String)"Uranus"))) // >>> bf(("Earth"), ("Mercury")) // (ArrayList<String>("Venus")) // >>> bf(("Mercury"), ("Uranus")) // (new ArrayList<String>(Arrays.asList((String)"Venus", (String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn"))) public static ArrayList<String> bf(String planet1, String planet2) { ArrayList<String> planets = new ArrayList<String>(Arrays.asList((String)"Mercury", (String)"Venus", (String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn", (String)"Uranus", (String)"Neptune")); int indexPlanet1 = planets.indexOf(planet1); int indexPlanet2 = planets.indexOf(planet2); int indexMin = 0; int indexMax = 0; if (indexPlanet1 >= 0 && indexPlanet2 >= 0) {
indexMin = indexPlanet1; indexMax = indexPlanet2; } else { indexMin = indexPlanet2; indexMax = indexPlanet1; } ArrayList<String> planetsInBetween = new ArrayList<String>(); for (int i = indexMin + 1; i < indexMax; i++) { planetsInBetween.add(planets.get(i)); } return planetsInBetween; } else { return new ArrayList<String>(); } } }
if (indexPlanet1 < indexPlanet2) {
} public static void main(String[] args) { assert(bf(("Jupiter"), ("Neptune")).equals((new ArrayList<String>(Arrays.asList((String)"Saturn", (String)"Uranus"))))); assert(bf(("Earth"), ("Mercury")).equals((new ArrayList<String>(Arrays.asList((String)"Venus"))))); assert(bf(("Mercury"), ("Uranus")).equals((new ArrayList<String>(Arrays.asList((String)"Venus", (String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn"))))); assert(bf(("Neptune"), ("Venus")).equals((new ArrayList<String>(Arrays.asList((String)"Earth", (String)"Mars", (String)"Jupiter", (String)"Saturn", (String)"Uranus"))))); assert(bf(("Earth"), ("Earth")).equals((new ArrayList<String>(Arrays.asList())))); assert(bf(("Mars"), ("Earth")).equals((new ArrayList<String>(Arrays.asList())))); assert(bf(("Jupiter"), ("Makemake")).equals((new ArrayList<String>(Arrays.asList())))); } }
humaneval-HumanEval_111_histogram.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 a string representing a space separated lowercase letters, return a hash map // of the letter with the most repetition and containing the corresponding count. // If several letters have the same occurrence, return all of them. // Example: // >>> histogram(("a b c")) // (new HashMap<String,Long>(Map.of("a", 1l, "b", 1l, "c", 1l))) // >>> histogram(("a b b a")) // (new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))) // >>> histogram(("a b c a b")) // (new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))) // >>> histogram(("b b b b a")) // (new HashMap<String,Long>(Map.of("b", 4l))) // >>> histogram(("")) // (new HashMap<String,Long>()) public static HashMap<String,Long> histogram(String test) { HashMap<String,Long> s = new HashMap<String,Long>(); if (test.length() > 0) { HashMap<String,Long> hm = new HashMap<String,Long>(); String[] ss = test.split(" "); for (String sss : ss) { if (hm.containsKey(sss)) { hm.put(sss, hm.get(sss) + 1); } else { hm.put(sss, 1l); } } long max = 0; for (String key : hm.keySet()) {
max = hm.get(key); } } for (String key : hm.keySet()) { if (hm.get(key) == max) { s.put(key, hm.get(key)); } } } return s; } }
if (hm.get(key) > max) {
} public static void main(String[] args) { assert(histogram(("a b b a")).equals((new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))))); assert(histogram(("a b c a b")).equals((new HashMap<String,Long>(Map.of("a", 2l, "b", 2l))))); assert(histogram(("a b c d g")).equals((new HashMap<String,Long>(Map.of("a", 1l, "b", 1l, "c", 1l, "d", 1l, "g", 1l))))); assert(histogram(("r t g")).equals((new HashMap<String,Long>(Map.of("r", 1l, "t", 1l, "g", 1l))))); assert(histogram(("b b b b a")).equals((new HashMap<String,Long>(Map.of("b", 4l))))); assert(histogram(("r t g")).equals((new HashMap<String,Long>(Map.of("r", 1l, "t", 1l, "g", 1l))))); assert(histogram(("")).equals((new HashMap<String,Long>()))); assert(histogram(("a")).equals((new HashMap<String,Long>(Map.of("a", 1l))))); } }
humaneval-HumanEval_49_modp.json-L14
js
//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 function modp(n, p){ var result = 1;
result = (result*2)%p; n--; } return result; }
while(n>0){
const assert = require('node:assert'); function test() { let candidate = modp; assert.deepEqual(candidate(3, 5),3); assert.deepEqual(candidate(1101, 101),2); assert.deepEqual(candidate(0, 101),1); assert.deepEqual(candidate(3, 11),8); assert.deepEqual(candidate(100, 101),1); assert.deepEqual(candidate(30, 5),4); assert.deepEqual(candidate(31, 5),3); } test();
humaneval-HumanEval_94_skjkasdkd.json-L42
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // You are given an array array list of integers. // You need to find the largest prime value and return the sum of its digits. // Examples: // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)3l, (long)2l, (long)1l, (long)3l, (long)5l, (long)7l, (long)4l, (long)5l, (long)5l, (long)5l, (long)2l, (long)181l, (long)32l, (long)4l, (long)32l, (long)3l, (long)2l, (long)32l, (long)324l, (long)4l, (long)3l)))) // (10l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)1l, (long)8l, (long)2l, (long)4597l, (long)2l, (long)1l, (long)3l, (long)40l, (long)1l, (long)2l, (long)1l, (long)2l, (long)4l, (long)2l, (long)5l, (long)1l)))) // (25l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)1l, (long)32l, (long)5107l, (long)34l, (long)83278l, (long)109l, (long)163l, (long)23l, (long)2323l, (long)32l, (long)30l, (long)1l, (long)9l, (long)3l)))) // (13l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)724l, (long)32l, (long)71l, (long)99l, (long)32l, (long)6l, (long)0l, (long)5l, (long)91l, (long)83l, (long)0l, (long)5l, (long)6l)))) // (11l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)81l, (long)12l, (long)3l, (long)1l, (long)21l)))) // (3l) // >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)8l, (long)1l, (long)2l, (long)1l, (long)7l)))) // (7l) public static long skjkasdkd(ArrayList<Long> lst) { long largestPrime = 0l; for (Long item : lst) { long number = item; int counter = 0; if (item == 1) { continue; } if (item == 2) { counter = 1; } else { int l = 1; while (l <= (int) number) { if (number % l == 0) { counter++; } l++;
} if (counter == 2) { if (number > largestPrime) { largestPrime = number; } } } if (largestPrime == 0) { return 0l; } else { long output = 0l; while (largestPrime > 0) { output += largestPrime % 10; largestPrime /= 10; } return output; } } }
}
} public static void main(String[] args) { assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)3l, (long)2l, (long)1l, (long)3l, (long)5l, (long)7l, (long)4l, (long)5l, (long)5l, (long)5l, (long)2l, (long)181l, (long)32l, (long)4l, (long)32l, (long)3l, (long)2l, (long)32l, (long)324l, (long)4l, (long)3l)))) == (10l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)1l, (long)8l, (long)2l, (long)4597l, (long)2l, (long)1l, (long)3l, (long)40l, (long)1l, (long)2l, (long)1l, (long)2l, (long)4l, (long)2l, (long)5l, (long)1l)))) == (25l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)1l, (long)32l, (long)5107l, (long)34l, (long)83278l, (long)109l, (long)163l, (long)23l, (long)2323l, (long)32l, (long)30l, (long)1l, (long)9l, (long)3l)))) == (13l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)724l, (long)32l, (long)71l, (long)99l, (long)32l, (long)6l, (long)0l, (long)5l, (long)91l, (long)83l, (long)0l, (long)5l, (long)6l)))) == (11l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)81l, (long)12l, (long)3l, (long)1l, (long)21l)))) == (3l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)8l, (long)1l, (long)2l, (long)1l, (long)7l)))) == (7l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)8191l)))) == (19l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)8191l, (long)123456l, (long)127l, (long)7l)))) == (19l)); assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)127l, (long)97l, (long)8192l)))) == (10l)); } }
humaneval-HumanEval_158_find_max.json-L39
js
//Write a function that accepts an array of strings. // The array contains different words. Return the word with maximum number // of unique characters. If multiple strings have maximum number of unique // characters, return the one which comes first in lexicographical order. // >>> find_max(["name", "of", "string"]) // "string" // >>> find_max(["name", "enam", "game"]) // "enam" // >>> find_max(["aaaaaaa", "bb", "cc"]) // "aaaaaaa" function find_max(words){ var max_unique = {}; var max_unique_word = ""; for(var i = 0; i < words.length; i++){ var unique_word = {}; var word = words[i]; for(var j = 0; j < word.length; j++){ var char = word[j]; if(unique_word[char] === undefined){ unique_word[char] = 1; } } if(max_unique_word === ""){ max_unique_word = word; max_unique = unique_word; } else if(Object.keys(unique_word).length > Object.keys(max_unique).length){ max_unique_word = word; max_unique = unique_word; } else if(Object.keys(unique_word).length === Object.keys(max_unique).length){ if(word < max_unique_word){ max_unique_word = word; max_unique = unique_word; } } } return max_unique_word;
}
const assert = require('node:assert'); function test() { let candidate = find_max; assert.deepEqual(candidate(["name", "of", "string"]),"string"); assert.deepEqual(candidate(["name", "enam", "game"]),"enam"); assert.deepEqual(candidate(["aaaaaaa", "bb", "cc"]),"aaaaaaa"); assert.deepEqual(candidate(["abc", "cba"]),"abc"); assert.deepEqual(candidate(["play", "this", "game", "of", "footbott"]),"footbott"); assert.deepEqual(candidate(["we", "are", "gonna", "rock"]),"gonna"); assert.deepEqual(candidate(["we", "are", "a", "mad", "nation"]),"nation"); assert.deepEqual(candidate(["this", "is", "a", "prrk"]),"this"); assert.deepEqual(candidate(["b"]),"b"); assert.deepEqual(candidate(["play", "play", "play"]),"play"); } test();
humaneval-HumanEval_39_prime_fib.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 { // 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 prev = 0; long count = 0; while (count != n) { long tmp = fib; fib += prev; prev = tmp; if (BigInteger.valueOf(fib).isProbablePrime(1)) { count++; } } return fib; } }
long fib = 1;
} 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_123_get_odd_collatz.json-L27
py
from typing import List def get_odd_collatz(n: int) -> List[int]: """ Given a positive integer n, return a sorted list that has the odd numbers in collatz sequence. The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1. Note: 1. Collatz(1) is [1]. 2. returned list sorted in increasing order. For example: get_odd_collatz(5) returns [1, 5] # The collatz sequence for 5 is [5, 16, 8, 4, 2, 1], so the odd numbers are only 1, and 5. >>> get_odd_collatz(5) [1, 5] """ collatz_list = [] current_num = n while (current_num > 1): collatz_list.append(current_num) if (current_num % 2 == 0):
else: current_num = current_num * 3 + 1 collatz_list.append(1) return sorted(list(set([x for x in collatz_list if x % 2 != 0])))
current_num = current_num // 2
def check(candidate): assert candidate(14) == [1, 5, 7, 11, 13, 17] assert candidate(5) == [1, 5] assert candidate(12) == [1, 3, 5] assert candidate(1) == [1] def test_check(): check(get_odd_collatz) test_check()
humaneval-HumanEval_54_same_chars.json-L25
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Check if two words have the same characters. // >>> sameChars(("eabcdzzzz"), ("dddzzzzzzzddeddabc")) // (true) // >>> sameChars(("abcd"), ("dddddddabc")) // (true) // >>> sameChars(("dddddddabc"), ("abcd")) // (true) // >>> sameChars(("eabcd"), ("dddddddabc")) // (false) // >>> sameChars(("abcd"), ("dddddddabce")) // (false) // >>> sameChars(("eabcdzzzz"), ("dddzzzzzzzddddabc")) // (false) public static boolean sameChars(String s0, String s1) { return s0.chars().distinct().sorted().boxed().collect(Collectors.toList()).equals(s1.chars().distinct().sorted().boxed().collect(Collectors.toList())); }
}
} public static void main(String[] args) { assert(sameChars(("eabcdzzzz"), ("dddzzzzzzzddeddabc")) == (true)); assert(sameChars(("abcd"), ("dddddddabc")) == (true)); assert(sameChars(("dddddddabc"), ("abcd")) == (true)); assert(sameChars(("eabcd"), ("dddddddabc")) == (false)); assert(sameChars(("abcd"), ("dddddddabcf")) == (false)); assert(sameChars(("eabcdzzzz"), ("dddzzzzzzzddddabc")) == (false)); assert(sameChars(("aabb"), ("aaccc")) == (false)); } }
humaneval-HumanEval_64_vowels_count.json-L16
js
//Write a function vowels_count which takes a string representing // a word as input and returns the number of vowels in the string. // Vowels in this case are 'a', 'e', 'i', 'o', 'u'. Here, 'y' is also a // vowel, but only when it is at the end of the given word. // Example: // >>> vowels_count("abcde") // 2 // >>> vowels_count("ACEDY") // 3 function vowels_count(s){ s=s.toLowerCase(); var vowels = ['a','e','i','o','u']; var count = 0; for(var i=0; i<s.length; i++){ if(vowels.indexOf(s[i])!=-1){
} } if(s[s.length-1]=='y'){ count++; } return count; }
count++;
const assert = require('node:assert'); function test() { let candidate = vowels_count; assert.deepEqual(candidate("abcde"),2); assert.deepEqual(candidate("Alone"),3); assert.deepEqual(candidate("key"),2); assert.deepEqual(candidate("bye"),1); assert.deepEqual(candidate("keY"),2); assert.deepEqual(candidate("bYe"),1); assert.deepEqual(candidate("ACEDY"),3); } test();
humaneval-HumanEval_150_x_or_y.json-L19
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // A simple program which should return the value of x if n is // a prime number and should return the value of y otherwise. // Examples: // >>> xOrY((7l), (34l), (12l)) // (34l) // >>> xOrY((15l), (8l), (5l)) // (5l) public static long xOrY(long n, long x, long y) { BigInteger nBig = BigInteger.valueOf(n); if (nBig.isProbablePrime(100)) {
} else { return y; } } }
return x;
} public static void main(String[] args) { assert(xOrY((7l), (34l), (12l)) == (34l)); assert(xOrY((15l), (8l), (5l)) == (5l)); assert(xOrY((3l), (33l), (5212l)) == (33l)); assert(xOrY((1259l), (3l), (52l)) == (3l)); assert(xOrY((7919l), (-1l), (12l)) == (-1l)); assert(xOrY((3609l), (1245l), (583l)) == (583l)); assert(xOrY((91l), (56l), (129l)) == (129l)); assert(xOrY((6l), (34l), (1234l)) == (1234l)); assert(xOrY((1l), (2l), (0l)) == (0l)); assert(xOrY((2l), (2l), (0l)) == (2l)); } }
humaneval-HumanEval_63_fibfib.json-L23
js
//The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: // fibfib(0) == 0 // fibfib(1) == 0 // fibfib(2) == 1 // fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3). // Please write a function to efficiently compute the n-th element of the fibfib number sequence. // >>> fibfib(1) // 0 // >>> fibfib(5) // 4 // >>> fibfib(8) // 24 function fibfib(n){ if(n <= 0){ return 0; } if(n == 1){ return 0; } if(n == 2){ return 1; }
}
return fibfib(n-1) + fibfib(n-2) + fibfib(n-3);
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_17_parse_music.json-L12
js
//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 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 // >>> parse_music("o o| .| o| o| .| .| .| .| o o") // [4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4] function parse_music(music_string){ let songs = music_string.split(' ');
songs.forEach(s => { if(s === 'o'){ song.push(4); }else if(s === 'o|'){ song.push(2); }else if(s === '.|'){ song.push(1); } }); return song; }
let song = [];
const assert = require('node:assert'); function test() { let candidate = parse_music; assert.deepEqual(candidate(""),[]); assert.deepEqual(candidate("o o o o"),[4, 4, 4, 4]); assert.deepEqual(candidate(".| .| .| .|"),[1, 1, 1, 1]); assert.deepEqual(candidate("o| o| .| .| o o o o"),[2, 2, 1, 1, 4, 4, 4, 4]); assert.deepEqual(candidate("o| .| o| .| o o| o o|"),[2, 1, 2, 1, 4, 2, 4, 2]); } test();
humaneval-HumanEval_62_derivative.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 { // xs represent coefficients of a polynomial. // xs[0] + xs[1] * x + xs[2] * x^2 + .... // Return derivative of this polynomial in the same form. // >>> derivative((new ArrayList<Long>(Arrays.asList((long)3l, (long)1l, (long)2l, (long)4l, (long)5l)))) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)12l, (long)20l))) // >>> derivative((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))) // (new ArrayList<Long>(Arrays.asList((long)2l, (long)6l))) public static ArrayList<Long> derivative(ArrayList<Long> xs) { ArrayList<Long> ans = new ArrayList<Long>(); for (int i = 1; i < xs.size(); i++) { ans.add(xs.get(i) * i); }
} }
return ans;
} public static void main(String[] args) { assert(derivative((new ArrayList<Long>(Arrays.asList((long)3l, (long)1l, (long)2l, (long)4l, (long)5l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)12l, (long)20l))))); assert(derivative((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l))))); assert(derivative((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l))))); assert(derivative((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)1l, (long)0l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l, (long)0l, (long)16l))))); assert(derivative((new ArrayList<Long>(Arrays.asList((long)1l)))).equals((new ArrayList<Long>(Arrays.asList())))); } }
humaneval-HumanEval_40_triples_sum_to_zero.json-L16
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 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; }
for (var j = 0; j < l.length; j++) {
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_122_add_elements.json-L11
js
//Given a non-empty array of integers arr and an integer k, return // the sum of the elements with at most two digits from the first k elements of arr. // Example: // >>> add_elements([111, 21, 3, 4000, 5, 6, 7, 8, 9], 4) // 24 // Constraints: // 1. 1 <= len(arr) <= 100 // 2. 1 <= k <= len(arr) function add_elements(arr, k){ var sum = 0;
if(arr[i] < 100){ sum += arr[i]; } } return sum; }
for(var i = 0; i < k; i++){
const assert = require('node:assert'); function test() { let candidate = add_elements; assert.deepEqual(candidate([1, -2, -3, 41, 57, 76, 87, 88, 99], 3),-4); assert.deepEqual(candidate([111, 121, 3, 4000, 5, 6], 2),0); assert.deepEqual(candidate([11, 21, 3, 90, 5, 6, 7, 8, 9], 4),125); assert.deepEqual(candidate([111, 21, 3, 4000, 5, 6, 7, 8, 9], 4),24); assert.deepEqual(candidate([1], 1),1); } test();
humaneval-HumanEval_1_separate_paren_groups.json-L25
js
//Input to this function is a string containing multiple groups of nested parentheses. Your goal is to // separate those group into separate strings and return the array of those. // Separate groups are balanced (each open brace is properly closed) and not nested within each other // Ignore any spaces in the input string. // >>> separate_paren_groups("( ) (( )) (( )( ))") // ["()", "(())", "(()())"] function separate_paren_groups(paren_string){ var count = 0; var group = ''; var groups = []; for (var i = 0; i < paren_string.length; i++){ if (paren_string[i] === '('){ count++; group += paren_string[i]; } else if (paren_string[i] === ')'){ count--; group += paren_string[i]; } if (count === 0 && group){ groups.push(group); group = ''; } } return groups;
}
const assert = require('node:assert'); function test() { let candidate = separate_paren_groups; assert.deepEqual(candidate("(()()) ((())) () ((())()())"),["(()())", "((()))", "()", "((())()())"]); assert.deepEqual(candidate("() (()) ((())) (((())))"),["()", "(())", "((()))", "(((())))"]); assert.deepEqual(candidate("(()(())((())))"),["(()(())((())))"]); assert.deepEqual(candidate("( ) (( )) (( )( ))"),["()", "(())", "(()())"]); } test();
humaneval-HumanEval_117_select_words.json-L49
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given a string s and a natural number n, you have been tasked to implement // a function that returns an array array list of all words from string s that contain exactly // n consonants, in order these words appear in the string s. // If the string s is empty then the function should return an empty array list. // Note: you may assume the input string contains only letters and spaces. // Examples: // >>> selectWords(("Mary had a little lamb"), (4l)) // (new ArrayList<String>(Arrays.asList((String)"little"))) // >>> selectWords(("Mary had a little lamb"), (3l)) // (new ArrayList<String>(Arrays.asList((String)"Mary", (String)"lamb"))) // >>> selectWords(("simple white space"), (2l)) // (new ArrayList<String>(Arrays.asList())) // >>> selectWords(("Hello world"), (4l)) // (new ArrayList<String>(Arrays.asList((String)"world"))) // >>> selectWords(("Uncle sam"), (3l)) // (new ArrayList<String>(Arrays.asList((String)"Uncle"))) public static ArrayList<String> selectWords(String s, long n) { ArrayList<String> ret = new ArrayList<String>(); String[] words = s.split(" "); for (String word : words) { String wordInLowerCase = word.toLowerCase(); long consonants = 0l; for (char c : wordInLowerCase.toCharArray()) { switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': break; default: consonants++; break; } } if (consonants == n) { ret.add(word); } } return ret;
}
}
} public static void main(String[] args) { assert(selectWords(("Mary had a little lamb"), (4l)).equals((new ArrayList<String>(Arrays.asList((String)"little"))))); assert(selectWords(("Mary had a little lamb"), (3l)).equals((new ArrayList<String>(Arrays.asList((String)"Mary", (String)"lamb"))))); assert(selectWords(("simple white space"), (2l)).equals((new ArrayList<String>(Arrays.asList())))); assert(selectWords(("Hello world"), (4l)).equals((new ArrayList<String>(Arrays.asList((String)"world"))))); assert(selectWords(("Uncle sam"), (3l)).equals((new ArrayList<String>(Arrays.asList((String)"Uncle"))))); assert(selectWords((""), (4l)).equals((new ArrayList<String>(Arrays.asList())))); assert(selectWords(("a b c d e f"), (1l)).equals((new ArrayList<String>(Arrays.asList((String)"b", (String)"c", (String)"d", (String)"f"))))); } }
humaneval-HumanEval_141_file_name_check.json-L16
js
//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" function file_name_check(file_name){
let str_2 = file_name.slice(file_name.indexOf('.')+1); let str_3 = file_name.split(''); if (file_name.includes('.') && /[a-zA-Z]/.test(str_1[0]) && (str_2 == 'txt' || str_2 == 'exe' || str_2 == 'dll') && str_1.length != 0 && str_3.filter(item => /[0-9]/.test(item)).length < 4) { return 'Yes'; } return 'No'; }
let str_1 = file_name.slice(0,file_name.indexOf('.'));
const assert = require('node:assert'); function test() { let candidate = file_name_check; assert.deepEqual(candidate("example.txt"),"Yes"); assert.deepEqual(candidate("1example.dll"),"No"); assert.deepEqual(candidate("s1sdf3.asd"),"No"); assert.deepEqual(candidate("K.dll"),"Yes"); assert.deepEqual(candidate("MY16FILE3.exe"),"Yes"); assert.deepEqual(candidate("His12FILE94.exe"),"No"); assert.deepEqual(candidate("_Y.txt"),"No"); assert.deepEqual(candidate("?aREYA.exe"),"No"); assert.deepEqual(candidate("/this_is_valid.dll"),"No"); assert.deepEqual(candidate("this_is_valid.wow"),"No"); assert.deepEqual(candidate("this_is_valid.txt"),"Yes"); assert.deepEqual(candidate("this_is_valid.txtexe"),"No"); assert.deepEqual(candidate("#this2_i4s_5valid.ten"),"No"); assert.deepEqual(candidate("@this1_is6_valid.exe"),"No"); assert.deepEqual(candidate("this_is_12valid.6exe4.txt"),"No"); assert.deepEqual(candidate("all.exe.txt"),"No"); assert.deepEqual(candidate("I563_No.exe"),"Yes"); assert.deepEqual(candidate("Is3youfault.txt"),"Yes"); assert.deepEqual(candidate("no_one#knows.dll"),"Yes"); assert.deepEqual(candidate("1I563_Yes3.exe"),"No"); assert.deepEqual(candidate("I563_Yes3.txtt"),"No"); assert.deepEqual(candidate("final..txt"),"No"); assert.deepEqual(candidate("final132"),"No"); assert.deepEqual(candidate("_f4indsartal132."),"No"); assert.deepEqual(candidate(".txt"),"No"); assert.deepEqual(candidate("s."),"No"); } test();
humaneval-HumanEval_113_odd_count.json-L30
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given an array array list of strings, where each string consists of only digits, return an array array list. // 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. // >>> oddCount((new ArrayList<String>(Arrays.asList((String)"1234567")))) // (new ArrayList<String>(Arrays.asList((String)"the number of odd elements 4n the str4ng 4 of the 4nput."))) // >>> oddCount((new ArrayList<String>(Arrays.asList((String)"3", (String)"11111111")))) // (new ArrayList<String>(Arrays.asList((String)"the number of odd elements 1n the str1ng 1 of the 1nput.", (String)"the number of odd elements 8n the str8ng 8 of the 8nput."))) public static ArrayList<String> oddCount(ArrayList<String> lst) { ArrayList<String> res = new ArrayList<String>(); for (int i = 0; i < lst.size(); i++) { int count = 0; for (int j = 0; j < lst.get(i).length(); j++) { if (Integer.parseInt(lst.get(i).substring(j, j + 1)) % 2 == 1) { count++; } } res.add("the number of odd elements " + count + "n the str" + count + "ng " + count + " of the " + count + "nput."); } return res; }
}
} public static void main(String[] args) { assert(oddCount((new ArrayList<String>(Arrays.asList((String)"1234567")))).equals((new ArrayList<String>(Arrays.asList((String)"the number of odd elements 4n the str4ng 4 of the 4nput."))))); assert(oddCount((new ArrayList<String>(Arrays.asList((String)"3", (String)"11111111")))).equals((new ArrayList<String>(Arrays.asList((String)"the number of odd elements 1n the str1ng 1 of the 1nput.", (String)"the number of odd elements 8n the str8ng 8 of the 8nput."))))); assert(oddCount((new ArrayList<String>(Arrays.asList((String)"271", (String)"137", (String)"314")))).equals((new ArrayList<String>(Arrays.asList((String)"the number of odd elements 2n the str2ng 2 of the 2nput.", (String)"the number of odd elements 3n the str3ng 3 of the 3nput.", (String)"the number of odd elements 2n the str2ng 2 of the 2nput."))))); } }
humaneval-HumanEval_143_words_in_sentence.json-L31
py
def words_in_sentence(sentence: str) -> str: """ You are given a string representing a sentence, the sentence contains some words separated by a space, and you have to return a string that contains the words from the original sentence, whose lengths are prime numbers, the order of the words in the new string should be the same as the original one. Example 1: >>> words_in_sentence('This is a test') 'is' Example 2: >>> words_in_sentence('lets go for swimming') 'go for' Constraints: * 1 <= len(sentence) <= 100 * sentence contains only letters """ def is_prime(n: int) -> bool: if n <= 1: return False for i in range(2, n): if n % i == 0: return False return True words = sentence.split() prime_words = []
if is_prime(len(word)): prime_words.append(word) return ' '.join(prime_words)
for word in words:
def check(candidate): assert candidate('This is a test') == 'is' assert candidate('lets go for swimming') == 'go for' assert candidate('there is no place available here') == 'there is no place' assert candidate('Hi I am Hussein') == 'Hi am Hussein' assert candidate('go for it') == 'go for it' assert candidate('here') == '' assert candidate('here is') == 'is' def test_check(): check(words_in_sentence) test_check()
humaneval-HumanEval_115_max_fill.json-L30
js
//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: // >>> max_fill([[0, 0, 1, 0], [0, 1, 0, 0], [1, 1, 1, 1]], 1) // 6 // Example 2: // >>> max_fill([[0, 0, 1, 1], [0, 0, 0, 0], [1, 1, 1, 1], [0, 1, 1, 1]], 2) // 5 // Example 3: // >>> max_fill([[0, 0, 0], [0, 0, 0]], 5) // 0 // 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 function max_fill(grid, capacity){ var maxFill = 0; var sum = 0; var c = 1; var r = grid.length; var c = grid[0].length; for(var i = 0; i < r; i++){ sum = 0;
sum += grid[i][j]; } maxFill += Math.ceil(sum/capacity); } return maxFill; }
for(var j = 0; j < c; j++){
const assert = require('node:assert'); function test() { let candidate = max_fill; assert.deepEqual(candidate([[0, 0, 1, 0], [0, 1, 0, 0], [1, 1, 1, 1]], 1),6); assert.deepEqual(candidate([[0, 0, 1, 1], [0, 0, 0, 0], [1, 1, 1, 1], [0, 1, 1, 1]], 2),5); assert.deepEqual(candidate([[0, 0, 0], [0, 0, 0]], 5),0); assert.deepEqual(candidate([[1, 1, 1, 1], [1, 1, 1, 1]], 2),4); assert.deepEqual(candidate([[1, 1, 1, 1], [1, 1, 1, 1]], 9),2); } test();
humaneval-HumanEval_40_triples_sum_to_zero.json-L42
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 { // triples_sum_to_zero takes an array array list of integers as an input. // it returns true if there are three distinct elements in the array list that // sum to zero, and false otherwise. // >>> triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)5l, (long)0l)))) // (false) // >>> triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)-2l, (long)1l)))) // (true) // >>> triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)7l)))) // (false) // >>> triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)-5l, (long)3l, (long)9l, (long)7l)))) // (true) // >>> triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)1l)))) // (false) public static boolean triplesSumToZero(ArrayList<Long> l) { if (l.size() < 3) { return false; } ArrayList<Long> sortedList = new ArrayList<Long>(l); Collections.sort(sortedList); for (int i = 0; i < sortedList.size() - 2; i++) { int j = i + 1; int k = sortedList.size() - 1; while (j < k) { long currSum = sortedList.get(i) + sortedList.get(j) + sortedList.get(k); if (currSum == 0) { return true; } else if (currSum < 0) { j++; } else { k--; } } }
} }
return false;
} public static void main(String[] args) { assert(triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)5l, (long)0l)))) == (false)); assert(triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)5l, (long)-1l)))) == (false)); assert(triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)-2l, (long)1l)))) == (true)); assert(triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)7l)))) == (false)); assert(triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)5l, (long)7l)))) == (false)); assert(triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)-5l, (long)3l, (long)9l, (long)7l)))) == (true)); assert(triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)1l)))) == (false)); assert(triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)5l, (long)-100l)))) == (false)); assert(triplesSumToZero((new ArrayList<Long>(Arrays.asList((long)100l, (long)3l, (long)5l, (long)-100l)))) == (false)); } }
humaneval-HumanEval_104_unique_digits.json-L18
js
//Given an array of positive integers x. return a sorted array of all // elements that hasn't any even digit. // Note: Returned array should be sorted in increasing order. // For example: // >>> unique_digits([15, 33, 1422, 1]) // [1, 15, 33] // >>> unique_digits([152, 323, 1422, 10]) // [] function unique_digits(x){ let result = [] for(let i = 0; i < x.length; i++){ let num = x[i].toString() if(num.indexOf('2') === -1 && num.indexOf('4') === -1 && num.indexOf('6') === -1 && num.indexOf('8') === -1 && num.indexOf('0') === -1){ result.push(x[i]) } } return result.sort(function(a, b){return a-b})
}
const assert = require('node:assert'); function test() { let candidate = unique_digits; assert.deepEqual(candidate([15, 33, 1422, 1]),[1, 15, 33]); assert.deepEqual(candidate([152, 323, 1422, 10]),[]); assert.deepEqual(candidate([12345, 2033, 111, 151]),[111, 151]); assert.deepEqual(candidate([135, 103, 31]),[31, 135]); } test();
humaneval-HumanEval_56_correct_bracketing.json-L21
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 = 0; for (var i = 0; i < brackets.length; i++) { if (brackets[i] === "<") { opens++; } else if (brackets[i] === ">") { opens--; } if (opens < 0) {
} } return opens === 0; }
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_31_is_prime.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 { // Return true if a given number is prime, and false otherwise. // >>> isPrime((6l)) // (false) // >>> isPrime((101l)) // (true) // >>> isPrime((11l)) // (true) // >>> isPrime((13441l)) // (true) // >>> isPrime((61l)) // (true) // >>> isPrime((4l)) // (false) // >>> isPrime((1l)) // (false) public static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; long i = 5; while (i * i <= n) {
i += 6; } return true; } }
if (n % i == 0 || n % (i + 2) == 0) return false;
} public static void main(String[] args) { assert(isPrime((6l)) == (false)); assert(isPrime((101l)) == (true)); assert(isPrime((11l)) == (true)); assert(isPrime((13441l)) == (true)); assert(isPrime((61l)) == (true)); assert(isPrime((4l)) == (false)); assert(isPrime((1l)) == (false)); assert(isPrime((5l)) == (true)); assert(isPrime((11l)) == (true)); assert(isPrime((17l)) == (true)); assert(isPrime((85l)) == (false)); assert(isPrime((77l)) == (false)); assert(isPrime((255379l)) == (false)); } }
humaneval-HumanEval_49_modp.json-L24
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Return 2^n modulo p (be aware of numerics). // >>> modp((3l), (5l)) // (3l) // >>> modp((1101l), (101l)) // (2l) // >>> modp((0l), (101l)) // (1l) // >>> modp((3l), (11l)) // (8l) // >>> modp((100l), (101l)) // (1l) public static long modp(long n, long p) { if (n == 0) return 1; long sqrt = modp(n / 2, p); sqrt = (sqrt * sqrt) % p;
return sqrt; } return (sqrt * 2) % p; } }
if (n % 2 == 0) {
} public static void main(String[] args) { assert(modp((3l), (5l)) == (3l)); assert(modp((1101l), (101l)) == (2l)); assert(modp((0l), (101l)) == (1l)); assert(modp((3l), (11l)) == (8l)); assert(modp((100l), (101l)) == (1l)); assert(modp((30l), (5l)) == (4l)); assert(modp((31l), (5l)) == (3l)); } }
humaneval-HumanEval_129_minPath.json-L63
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){ neighbours.push(grid[x][y + 1]); } let nextCell = neighbours[0];
let curr = neighbours[i]; if(curr < nextCell){ nextCell = curr; } } currCell = nextCell; } return path; }
for(let i = 0; i < neighbours.length; i++){
const assert = require('node:assert'); function test() { let candidate = minPath; assert.deepEqual(candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3),[1, 2, 1]); assert.deepEqual(candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1),[1]); assert.deepEqual(candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4),[1, 2, 1, 2]); assert.deepEqual(candidate([[6, 4, 13, 10], [5, 7, 12, 1], [3, 16, 11, 15], [8, 14, 9, 2]], 7),[1, 10, 1, 10, 1, 10, 1]); assert.deepEqual(candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5),[1, 7, 1, 7, 1]); assert.deepEqual(candidate([[11, 8, 7, 2], [5, 16, 14, 4], [9, 3, 15, 6], [12, 13, 10, 1]], 9),[1, 6, 1, 6, 1, 6, 1, 6, 1]); assert.deepEqual(candidate([[12, 13, 10, 1], [9, 3, 15, 6], [5, 16, 14, 4], [11, 8, 7, 2]], 12),[1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6]); assert.deepEqual(candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8),[1, 3, 1, 3, 1, 3, 1, 3]); assert.deepEqual(candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8),[1, 5, 1, 5, 1, 5, 1, 5]); assert.deepEqual(candidate([[1, 2], [3, 4]], 10),[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]); assert.deepEqual(candidate([[1, 3], [3, 2]], 10),[1, 3, 1, 3, 1, 3, 1, 3, 1, 3]); } test();
humaneval-HumanEval_8_sum_product.json-L13
py
from typing import List, Tuple def sum_product(numbers: List[int]) -> Tuple[int, int]: """ For a given list of integers, return a tuple consisting of a sum and a product of all the integers in a list. Empty sum should be equal to 0 and empty product should be equal to 1. >>> sum_product([]) (0, 1) >>> sum_product([1, 2, 3, 4]) (10, 24) """ sum = 0 product = 1
sum += i product *= i return (sum, product)
for i in numbers:
def check(candidate): assert candidate([]) == (0, 1) assert candidate([1, 1, 1]) == (3, 1) assert candidate([100, 0]) == (100, 0) assert candidate([3, 5, 7]) == (15, 105) assert candidate([10]) == (10, 10) def test_check(): check(sum_product) test_check()
humaneval-HumanEval_57_monotonic.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 { // Return true is array list elements are monotonically increasing or decreasing. // >>> monotonic((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)4l, (long)20l)))) // (true) // >>> monotonic((new ArrayList<Long>(Arrays.asList((long)1l, (long)20l, (long)4l, (long)10l)))) // (false) // >>> monotonic((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)0l, (long)-10l)))) // (true) public static boolean monotonic(ArrayList<Long> l) { long a = l.get(0); boolean inc = true; boolean dec = true; for (int i = 1; i < l.size(); i++) { if (l.get(i) < a) { inc = false; } if (l.get(i) > a) { dec = false; } a = l.get(i);
return inc || dec; } }
}
} public static void main(String[] args) { assert(monotonic((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)4l, (long)10l)))) == (true)); assert(monotonic((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)4l, (long)20l)))) == (true)); assert(monotonic((new ArrayList<Long>(Arrays.asList((long)1l, (long)20l, (long)4l, (long)10l)))) == (false)); assert(monotonic((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)0l, (long)-10l)))) == (true)); assert(monotonic((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)1l, (long)0l)))) == (true)); assert(monotonic((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)2l, (long)5l, (long)60l)))) == (false)); assert(monotonic((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l, (long)60l)))) == (true)); assert(monotonic((new ArrayList<Long>(Arrays.asList((long)9l, (long)9l, (long)9l, (long)9l)))) == (true)); } }
humaneval-HumanEval_107_even_odd_palindrome.json-L26
js
//Given a positive integer n, return an array that has the number of even and odd // integer palindromes that fall within the range(1, n), inclusive. // Example 1: // >>> even_odd_palindrome(3) // [1, 2] // Explanation: // Integer palindrome are 1, 2, 3. one of them is even, and two of them are odd. // Example 2: // >>> even_odd_palindrome(12) // [4, 6] // Explanation: // Integer palindrome are 1, 2, 3, 4, 5, 6, 7, 8, 9, 11. four of them are even, and 6 of them are odd. // Note: // 1. 1 <= n <= 10^3 // 2. returned array has the number of even and odd integer palindromes respectively. function even_odd_palindrome(n){ var pal = []; var isPalindrome = function(x){ var y = x.toString().split('').reverse().join(''); return y == x; } for(var i = 1; i <= n; i++){ if(isPalindrome(i)){ pal.push(i); }
var odd = 0; var even = 0; for(var i = 0; i < pal.length; i++){ if(pal[i] % 2 == 0){ even++; } else{ odd++; } } return [even, odd]; }
}
const assert = require('node:assert'); function test() { let candidate = even_odd_palindrome; assert.deepEqual(candidate(123),[8, 13]); assert.deepEqual(candidate(12),[4, 6]); assert.deepEqual(candidate(3),[1, 2]); assert.deepEqual(candidate(63),[6, 8]); assert.deepEqual(candidate(25),[5, 6]); assert.deepEqual(candidate(19),[4, 6]); assert.deepEqual(candidate(9),[4, 5]); assert.deepEqual(candidate(1),[0, 1]); } test();
humaneval-HumanEval_104_unique_digits.json-L17
js
//Given an array of positive integers x. return a sorted array of all // elements that hasn't any even digit. // Note: Returned array should be sorted in increasing order. // For example: // >>> unique_digits([15, 33, 1422, 1]) // [1, 15, 33] // >>> unique_digits([152, 323, 1422, 10]) // [] function unique_digits(x){ let result = [] for(let i = 0; i < x.length; i++){ let num = x[i].toString() if(num.indexOf('2') === -1 && num.indexOf('4') === -1 && num.indexOf('6') === -1 && num.indexOf('8') === -1 && num.indexOf('0') === -1){ result.push(x[i]) } }
}
return result.sort(function(a, b){return a-b})
const assert = require('node:assert'); function test() { let candidate = unique_digits; assert.deepEqual(candidate([15, 33, 1422, 1]),[1, 15, 33]); assert.deepEqual(candidate([152, 323, 1422, 10]),[]); assert.deepEqual(candidate([12345, 2033, 111, 151]),[111, 151]); assert.deepEqual(candidate([135, 103, 31]),[31, 135]); } test();
humaneval-HumanEval_8_sum_product.json-L15
py
from typing import List, Tuple def sum_product(numbers: List[int]) -> Tuple[int, int]: """ For a given list of integers, return a tuple consisting of a sum and a product of all the integers in a list. Empty sum should be equal to 0 and empty product should be equal to 1. >>> sum_product([]) (0, 1) >>> sum_product([1, 2, 3, 4]) (10, 24) """ sum = 0 product = 1 for i in numbers: sum += i
return (sum, product)
product *= i
def check(candidate): assert candidate([]) == (0, 1) assert candidate([1, 1, 1]) == (3, 1) assert candidate([100, 0]) == (100, 0) assert candidate([3, 5, 7]) == (15, 105) assert candidate([10]) == (10, 10) def test_check(): check(sum_product) test_check()
humaneval-HumanEval_75_is_multiply_prime.json-L14
py
def is_multiply_prime(a: int) -> bool: """Write a function that returns true if the given number is the multiplication of 3 prime numbers and false otherwise. Knowing that (a) is less then 100. Example: >>> is_multiply_prime(30) True 30 = 2 * 3 * 5 """ n = a d = 2 prime_factors = [] while d * d <= n:
prime_factors.append(d) n //= d else: d += 1 if n > 1: prime_factors.append(n) if len(prime_factors) == 3: return True else: return False
if n % d == 0:
def check(candidate): assert candidate(5) == False assert candidate(30) == True assert candidate(8) == True assert candidate(10) == False assert candidate(125) == True assert candidate(105) == True assert candidate(126) == False assert candidate(729) == False assert candidate(891) == False assert candidate(1001) == True def test_check(): check(is_multiply_prime) test_check()
humaneval-HumanEval_104_unique_digits.json-L16
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=[]
temp=i while temp: if temp%2==0: s.append(i) break temp=temp//10 for i in s: x.remove(i) return sorted(x)
for i in x:
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_75_is_multiply_prime.json-L16
js
//Write a function that returns true if the given number is the multiplication of 3 prime numbers // and false otherwise. // Knowing that (a) is less then 100. // Example: // >>> is_multiply_prime(30) // true // 30 = 2 * 3 * 5 function is_multiply_prime(a){ a = Math.abs(a); const prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]; let i = 0; let check = []; while(a != 1){ if(a % prime[i] == 0){ a = a / prime[i];
}else{ i++; } } if(check.length == 3){ return true; }else{ return false; } }
check.push(prime[i]);
const assert = require('node:assert'); function test() { let candidate = is_multiply_prime; assert.deepEqual(candidate(5),false); assert.deepEqual(candidate(30),true); assert.deepEqual(candidate(8),true); assert.deepEqual(candidate(10),false); assert.deepEqual(candidate(125),true); assert.deepEqual(candidate(105),true); assert.deepEqual(candidate(126),false); assert.deepEqual(candidate(729),false); assert.deepEqual(candidate(891),false); assert.deepEqual(candidate(1001),true); } test();
humaneval-HumanEval_39_prime_fib.json-L27
js
//prime_fib returns n-th number that is a Fibonacci number and it's also prime. // >>> prime_fib(1) // 2 // >>> prime_fib(2) // 3 // >>> prime_fib(3) // 5 // >>> prime_fib(4) // 13 // >>> prime_fib(5) // 89 function prime_fib(n){ function isPrime(number){ if(number <= 3){ return number > 1; } else if(number % 2 == 0 || number % 3 == 0){ return false; } else{ for(var i = 5; i * i <= number; i += 6){ if(number % i == 0 || number % (i + 2) == 0){ return false; } } return true;
} function fib(num){ var a = 1, b = 0, temp; while (num >= 0){ temp = a; a = a + b; b = temp; num--; } return b; } var res = 0; var fibNum = 0; while(res < n){ if(isPrime(fib(fibNum))){ res++; } fibNum++; } return fib(fibNum - 1); }
}
const assert = require('node:assert'); function test() { let candidate = prime_fib; assert.deepEqual(candidate(1),2); assert.deepEqual(candidate(2),3); assert.deepEqual(candidate(3),5); assert.deepEqual(candidate(4),13); assert.deepEqual(candidate(5),89); assert.deepEqual(candidate(6),233); assert.deepEqual(candidate(7),1597); assert.deepEqual(candidate(8),28657); assert.deepEqual(candidate(9),514229); assert.deepEqual(candidate(10),433494437); } test();
humaneval-HumanEval_129_minPath.json-L42
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 j = 0; j < n; j++){ if(grid[i][j] === currCell){ x = i; y = j; } } } if(x > 0){ neighbours.push(grid[x - 1][y]); } if(x < n - 1){ neighbours.push(grid[x + 1][y]); } if(y > 0){ neighbours.push(grid[x][y - 1]); } if(y < n - 1){ neighbours.push(grid[x][y + 1]); } let nextCell = neighbours[0]; for(let i = 0; i < neighbours.length; i++){ let curr = neighbours[i]; if(curr < nextCell){ nextCell = curr; } } currCell = nextCell; } return path; }
for(let i = 0; i < n; i++){
const assert = require('node:assert'); function test() { let candidate = minPath; assert.deepEqual(candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3),[1, 2, 1]); assert.deepEqual(candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1),[1]); assert.deepEqual(candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4),[1, 2, 1, 2]); assert.deepEqual(candidate([[6, 4, 13, 10], [5, 7, 12, 1], [3, 16, 11, 15], [8, 14, 9, 2]], 7),[1, 10, 1, 10, 1, 10, 1]); assert.deepEqual(candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5),[1, 7, 1, 7, 1]); assert.deepEqual(candidate([[11, 8, 7, 2], [5, 16, 14, 4], [9, 3, 15, 6], [12, 13, 10, 1]], 9),[1, 6, 1, 6, 1, 6, 1, 6, 1]); assert.deepEqual(candidate([[12, 13, 10, 1], [9, 3, 15, 6], [5, 16, 14, 4], [11, 8, 7, 2]], 12),[1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6]); assert.deepEqual(candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8),[1, 3, 1, 3, 1, 3, 1, 3]); assert.deepEqual(candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8),[1, 5, 1, 5, 1, 5, 1, 5]); assert.deepEqual(candidate([[1, 2], [3, 4]], 10),[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]); assert.deepEqual(candidate([[1, 3], [3, 2]], 10),[1, 3, 1, 3, 1, 3, 1, 3, 1, 3]); } test();
humaneval-HumanEval_4_mean_absolute_deviation.json-L17
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // For a given array list of input numbers, calculate Mean Absolute Deviation // around the mean of this dataset. // Mean Absolute Deviation is the average absolute difference between each // element and a centerpoint (mean in this case): // MAD = average | x - x_mean | // >>> meanAbsoluteDeviation((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)2.0f, (float)3.0f, (float)4.0f)))) // (1.0f) public static float meanAbsoluteDeviation(ArrayList<Float> numbers) {
return (float)numbers.stream().mapToDouble(n -> Math.abs(n - mean)).average().getAsDouble(); } }
float mean = (float)numbers.stream().mapToDouble(Float::floatValue).average().getAsDouble();
} public static void main(String[] args) { assert(meanAbsoluteDeviation((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)2.0f)))) == (0.5f)); assert(meanAbsoluteDeviation((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)2.0f, (float)3.0f, (float)4.0f)))) == (1.0f)); assert(meanAbsoluteDeviation((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)2.0f, (float)3.0f, (float)4.0f, (float)5.0f)))) == (1.2f)); } }
humaneval-HumanEval_41_car_race_collision.json-L15
js
//Imagine a road that's a perfectly straight infinitely long line. // n cars are driving left to right; simultaneously, a different set of n cars // are driving right to left. The two sets of cars start out being very far from // each other. All cars move in the same speed. Two cars are said to collide // when a car that's moving left to right hits a car that's moving right to left. // However, the cars are infinitely sturdy and strong; as a result, they continue moving // in their trajectory as if they did not collide. // This function outputs the number of such collisions. function car_race_collision(n){ let num_collisions = 0; let left_to_right = Array(n).fill(1); let right_to_left = Array(n).fill(1); for (let i = 0; i < left_to_right.length; i++) { for (let j = 0; j < right_to_left.length; j++) {
num_collisions++; } } } return num_collisions; }
if (left_to_right[i] == right_to_left[j]) {
const assert = require('node:assert'); function test() { let candidate = car_race_collision; assert.deepEqual(candidate(2),4); assert.deepEqual(candidate(3),9); assert.deepEqual(candidate(4),16); assert.deepEqual(candidate(8),64); assert.deepEqual(candidate(10),100); } test();
humaneval-HumanEval_77_iscube.json-L25
py
def iscube(a: int) -> bool: """ Write a function that takes an integer a and returns True if this ingeger is a cube of some integer number. Note: you may assume the input is always valid. Examples: >>> iscube(1) True >>> iscube(2) False >>> iscube(-1) True >>> iscube(64) True >>> iscube(0) True >>> iscube(180) False """ import math a = abs(a) if a == 0: return True for i in range(1,int(math.sqrt(a))+1):
return True return False pass
if i**3 == a:
def check(candidate): assert candidate(1) == True assert candidate(2) == False assert candidate(-1) == True assert candidate(64) == True assert candidate(180) == False assert candidate(1000) == True assert candidate(0) == True assert candidate(1729) == False def test_check(): check(iscube) test_check()
humaneval-HumanEval_70_strange_sort_list.json-L26
js
//Given array of integers, return array in strange order. // Strange sorting, is when you start with the minimum value, // then maximum of the remaining integers, then minimum and so on. // Examples: // >>> strange_sort_list([1, 2, 3, 4]) // [1, 4, 2, 3] // >>> strange_sort_list([5, 5, 5, 5]) // [5, 5, 5, 5] // >>> strange_sort_list([]) // [] function strange_sort_list(lst){ if(lst.length === 0){ return []; } var output = []; var tmp = lst.slice(); for(var i = 0; i < lst.length; i++){ if(i % 2 === 0){ output.push(Math.min.apply(null, tmp)); } else{ output.push(Math.max.apply(null, tmp)); } tmp.splice(tmp.indexOf(output[i]), 1); }
}
return output;
const assert = require('node:assert'); function test() { let candidate = strange_sort_list; assert.deepEqual(candidate([1, 2, 3, 4]),[1, 4, 2, 3]); assert.deepEqual(candidate([5, 6, 7, 8, 9]),[5, 9, 6, 8, 7]); assert.deepEqual(candidate([1, 2, 3, 4, 5]),[1, 5, 2, 4, 3]); assert.deepEqual(candidate([5, 6, 7, 8, 9, 1]),[1, 9, 5, 8, 6, 7]); assert.deepEqual(candidate([5, 5, 5, 5]),[5, 5, 5, 5]); assert.deepEqual(candidate([]),[]); assert.deepEqual(candidate([1, 2, 3, 4, 5, 6, 7, 8]),[1, 8, 2, 7, 3, 6, 4, 5]); assert.deepEqual(candidate([0, 2, 2, 2, 5, 5, -5, -5]),[-5, 5, -5, 5, 0, 2, 2, 2]); assert.deepEqual(candidate([111111]),[111111]); } test();
humaneval-HumanEval_117_select_words.json-L36
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given a string s and a natural number n, you have been tasked to implement // a function that returns an array array list of all words from string s that contain exactly // n consonants, in order these words appear in the string s. // If the string s is empty then the function should return an empty array list. // Note: you may assume the input string contains only letters and spaces. // Examples: // >>> selectWords(("Mary had a little lamb"), (4l)) // (new ArrayList<String>(Arrays.asList((String)"little"))) // >>> selectWords(("Mary had a little lamb"), (3l)) // (new ArrayList<String>(Arrays.asList((String)"Mary", (String)"lamb"))) // >>> selectWords(("simple white space"), (2l)) // (new ArrayList<String>(Arrays.asList())) // >>> selectWords(("Hello world"), (4l)) // (new ArrayList<String>(Arrays.asList((String)"world"))) // >>> selectWords(("Uncle sam"), (3l)) // (new ArrayList<String>(Arrays.asList((String)"Uncle"))) public static ArrayList<String> selectWords(String s, long n) { ArrayList<String> ret = new ArrayList<String>(); String[] words = s.split(" "); for (String word : words) { String wordInLowerCase = word.toLowerCase(); long consonants = 0l; for (char c : wordInLowerCase.toCharArray()) { switch (c) { case 'a': case 'e': case 'i':
case 'u': break; default: consonants++; break; } } if (consonants == n) { ret.add(word); } } return ret; } }
case 'o':
} public static void main(String[] args) { assert(selectWords(("Mary had a little lamb"), (4l)).equals((new ArrayList<String>(Arrays.asList((String)"little"))))); assert(selectWords(("Mary had a little lamb"), (3l)).equals((new ArrayList<String>(Arrays.asList((String)"Mary", (String)"lamb"))))); assert(selectWords(("simple white space"), (2l)).equals((new ArrayList<String>(Arrays.asList())))); assert(selectWords(("Hello world"), (4l)).equals((new ArrayList<String>(Arrays.asList((String)"world"))))); assert(selectWords(("Uncle sam"), (3l)).equals((new ArrayList<String>(Arrays.asList((String)"Uncle"))))); assert(selectWords((""), (4l)).equals((new ArrayList<String>(Arrays.asList())))); assert(selectWords(("a b c d e f"), (1l)).equals((new ArrayList<String>(Arrays.asList((String)"b", (String)"c", (String)"d", (String)"f"))))); } }
humaneval-HumanEval_104_unique_digits.json-L34
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given an array array list of positive integers x. return a sorted array list of all // elements that hasn't any even digit. // Note: Returned array list should be sorted in increasing order. // For example: // >>> uniqueDigits((new ArrayList<Long>(Arrays.asList((long)15l, (long)33l, (long)1422l, (long)1l)))) // (new ArrayList<Long>(Arrays.asList((long)1l, (long)15l, (long)33l))) // >>> uniqueDigits((new ArrayList<Long>(Arrays.asList((long)152l, (long)323l, (long)1422l, (long)10l)))) // (new ArrayList<Long>(Arrays.asList())) public static ArrayList<Long> uniqueDigits(ArrayList<Long> x) { ArrayList<Long> b = new ArrayList<Long>(); for (int i = 0; i < x.size(); i++) { if (x.get(i) < 0) { x.set(i, x.get(i)*(long)-1); } boolean unique = true; long z = x.get(i); while (z > 0) { long y = z % 10; if (y % 2 == 0) { unique = false; break; } z = z / 10; } if (unique) {
} } Collections.sort(b); return b; } }
b.add(x.get(i));
} public static void main(String[] args) { assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)15l, (long)33l, (long)1422l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)15l, (long)33l))))); assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)152l, (long)323l, (long)1422l, (long)10l)))).equals((new ArrayList<Long>(Arrays.asList())))); assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)12345l, (long)2033l, (long)111l, (long)151l)))).equals((new ArrayList<Long>(Arrays.asList((long)111l, (long)151l))))); assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)135l, (long)103l, (long)31l)))).equals((new ArrayList<Long>(Arrays.asList((long)31l, (long)135l))))); } }
humaneval-HumanEval_78_hex_key.json-L24
js
//You have been tasked to write a function that receives // a hexadecimal number as a string and counts the number of hexadecimal // digits that are primes (prime number, or a prime, is a natural number // greater than 1 that is not a product of two smaller natural numbers). // Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. // Prime numbers are 2, 3, 5, 7, 11, 13, 17,... // So you have to determine a number of the following digits: 2, 3, 5, 7, // B (=decimal 11), D (=decimal 13). // Note: you may assume the input is always correct or empty string, // and symbols A,B,C,D,E,F are always uppercase. // Examples: // >>> hex_key("AB") // 1 // >>> hex_key("1077E") // 2 // >>> hex_key("ABED1A33") // 4 // >>> hex_key("123456789ABCDEF0") // 6 // >>> hex_key("2020") // 2 function hex_key(num){ let result = 0;
let primes = ['2', '3', '5', '7', 'B', 'D']; for(let x = 0; x < num.length; x++){ if(primes.includes(num[x])){ result += 1; } } return result; }
let hexadecimals = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
const assert = require('node:assert'); function test() { let candidate = hex_key; assert.deepEqual(candidate("AB"),1); assert.deepEqual(candidate("1077E"),2); assert.deepEqual(candidate("ABED1A33"),4); assert.deepEqual(candidate("2020"),2); assert.deepEqual(candidate("123456789ABCDEF0"),6); assert.deepEqual(candidate("112233445566778899AABBCCDDEEFF00"),12); } test();
humaneval-HumanEval_143_words_in_sentence.json-L45
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_33_sort_third.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 { // 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;
if (i % 3 == 0) { l_s.add(l_e.get(j)); j++; } else { l_s.add(l_r.get(i - j)); } } return l_s; } }
for (int i = 0; i < l.size(); i++) {
} public static void main(String[] args) { assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)5l))))); assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)8l, (long)3l, (long)4l, (long)6l, (long)9l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)8l, (long)3l, (long)4l, (long)6l, (long)9l, (long)5l))))); assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)9l, (long)4l, (long)8l, (long)3l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)9l, (long)4l, (long)8l, (long)3l, (long)5l))))); assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)2l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)5l, (long)1l))))); } }
humaneval-HumanEval_98_count_upper.json-L17
js
//Given a string s, count the number of uppercase vowels in even indices. // For example: // >>> count_upper("aBCdEf") // 1 // >>> count_upper("abcdefg") // 0 // >>> count_upper("dBBE") // 0 function count_upper(s){ var index; var result = 0; var vowel = ['A', 'E', 'I', 'O', 'U']; for (index = 0; index < s.length; index += 2){ if (vowel.indexOf(s[index]) > -1){ result += 1; }
return result; }
}
const assert = require('node:assert'); function test() { let candidate = count_upper; assert.deepEqual(candidate("aBCdEf"),1); assert.deepEqual(candidate("abcdefg"),0); assert.deepEqual(candidate("dBBE"),0); assert.deepEqual(candidate("B"),0); assert.deepEqual(candidate("U"),1); assert.deepEqual(candidate(""),0); assert.deepEqual(candidate("EEEE"),2); } test();
humaneval-HumanEval_28_concatenate.json-L10
js
//Concatenate array of strings into a single string // >>> concatenate([]) // "" // >>> concatenate(["a", "b", "c"]) // "abc" function concatenate(strings){ if(strings.length === 0){ return ""; }
return string; }
var string = strings.join("");
const assert = require('node:assert'); function test() { let candidate = concatenate; assert.deepEqual(candidate([]),""); assert.deepEqual(candidate(["x", "y", "z"]),"xyz"); assert.deepEqual(candidate(["x", "y", "z", "w", "k"]),"xyzwk"); } test();
humaneval-HumanEval_17_parse_music.json-L17
js
//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 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 // >>> parse_music("o o| .| o| o| .| .| .| .| o o") // [4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4] function parse_music(music_string){ let songs = music_string.split(' '); let song = []; songs.forEach(s => { if(s === 'o'){ song.push(4); }else if(s === 'o|'){
}else if(s === '.|'){ song.push(1); } }); return song; }
song.push(2);
const assert = require('node:assert'); function test() { let candidate = parse_music; assert.deepEqual(candidate(""),[]); assert.deepEqual(candidate("o o o o"),[4, 4, 4, 4]); assert.deepEqual(candidate(".| .| .| .|"),[1, 1, 1, 1]); assert.deepEqual(candidate("o| o| .| .| o o o o"),[2, 2, 1, 1, 4, 4, 4, 4]); assert.deepEqual(candidate("o| .| o| .| o o| o o|"),[2, 1, 2, 1, 4, 2, 4, 2]); } test();
humaneval-HumanEval_124_valid_date.json-L37
py
def valid_date(date: str) -> bool: """You have to write a function which validates a given date string and returns True if the date is valid otherwise False. The date is valid if all of the following rules are satisfied: 1. The date string is not empty. 2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the number of days is not less than 1 or higher than 30 days for months 4,6,9,11. And, the number of days is not less than 1 or higher than 29 for the month 2. 3. The months should not be less than 1 or higher than 12. 4. The date should be in the format: mm-dd-yyyy >>> valid_date('03-11-2000') True >>> valid_date('15-01-2012') False >>> valid_date('04-0-2040') False >>> valid_date('06-04-2020') True >>> valid_date('06/04/2020') False """ if not date: return False days_per_month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] date_list = [int(d) for d in date.split('-')] if len(date_list) != 3: return False if date_list[0] < 1 or date_list[0] > 12: return False if date_list[1] < 1 or date_list[1] > days_per_month[date_list[0] - 1]: return False if len(str(date_list[2])) != 4: return False
return True
def check(candidate): assert candidate('03-11-2000') == True assert candidate('15-01-2012') == False assert candidate('04-0-2040') == False assert candidate('06-04-2020') == True assert candidate('01-01-2007') == True assert candidate('03-32-2011') == False assert candidate('') == False assert candidate('04-31-3000') == False assert candidate('06-06-2005') == True assert candidate('21-31-2000') == False assert candidate('04-12-2003') == True assert candidate('04122003') == False assert candidate('20030412') == False assert candidate('2003-04') == False assert candidate('2003-04-12') == False assert candidate('04-2003') == False def test_check(): check(valid_date) test_check()
humaneval-HumanEval_124_valid_date.json-L32
py
def valid_date(date: str) -> bool: """You have to write a function which validates a given date string and returns True if the date is valid otherwise False. The date is valid if all of the following rules are satisfied: 1. The date string is not empty. 2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the number of days is not less than 1 or higher than 30 days for months 4,6,9,11. And, the number of days is not less than 1 or higher than 29 for the month 2. 3. The months should not be less than 1 or higher than 12. 4. The date should be in the format: mm-dd-yyyy >>> valid_date('03-11-2000') True >>> valid_date('15-01-2012') False >>> valid_date('04-0-2040') False >>> valid_date('06-04-2020') True >>> valid_date('06/04/2020') False """ if not date: return False days_per_month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] date_list = [int(d) for d in date.split('-')] if len(date_list) != 3: return False if date_list[0] < 1 or date_list[0] > 12:
if date_list[1] < 1 or date_list[1] > days_per_month[date_list[0] - 1]: return False if len(str(date_list[2])) != 4: return False return True
return False
def check(candidate): assert candidate('03-11-2000') == True assert candidate('15-01-2012') == False assert candidate('04-0-2040') == False assert candidate('06-04-2020') == True assert candidate('01-01-2007') == True assert candidate('03-32-2011') == False assert candidate('') == False assert candidate('04-31-3000') == False assert candidate('06-06-2005') == True assert candidate('21-31-2000') == False assert candidate('04-12-2003') == True assert candidate('04122003') == False assert candidate('20030412') == False assert candidate('2003-04') == False assert candidate('2003-04-12') == False assert candidate('04-2003') == False def test_check(): check(valid_date) test_check()
humaneval-HumanEval_139_special_factorial.json-L11
js
//The Brazilian factorial is defined as: // brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1! // where n > 0 // For example: // >>> special_factorial(4) // 288 // The function will receive an integer as input and should return the special // factorial of this integer. function special_factorial(n){ if (n > 0) {
for (let i = 1; i <= n; i++) { fact *= i; } return fact * special_factorial(n - 1); } else { return 1; } }
let fact = 1;
const assert = require('node:assert'); function test() { let candidate = special_factorial; assert.deepEqual(candidate(4),288); assert.deepEqual(candidate(5),34560); assert.deepEqual(candidate(7),125411328000); assert.deepEqual(candidate(1),1); } test();
humaneval-HumanEval_46_fib4.json-L23
js
//The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: // fib4(0) -> 0 // fib4(1) -> 0 // fib4(2) -> 2 // fib4(3) -> 0 // fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4). // Please write a function to efficiently compute the n-th element of the fib4 number sequence. Do not use recursion. // >>> fib4(5) // 4 // >>> fib4(6) // 8 // >>> fib4(7) // 14 function fib4(n){ var prev = [0,0,2,0]; if(n<prev.length){ return prev[n]; } for(var i=4;i<=n;i++){ var curr = prev[0]+prev[1]+prev[2]+prev[3]; prev[0] = prev[1]; prev[1] = prev[2];
prev[3] = curr; } return prev[3]; }
prev[2] = prev[3];
const assert = require('node:assert'); function test() { let candidate = fib4; assert.deepEqual(candidate(5),4); assert.deepEqual(candidate(8),28); assert.deepEqual(candidate(10),104); assert.deepEqual(candidate(12),386); } test();
humaneval-HumanEval_24_largest_divisor.json-L10
js
//For a given number n, find the largest number that divides n evenly, smaller than n // >>> largest_divisor(15) // 5 function largest_divisor(n){ let ld = 0; for(i=1; i<n; i++){ if(n%i===0){ if(i>ld){ ld = i;
} } return ld; }
}
const assert = require('node:assert'); function test() { let candidate = largest_divisor; assert.deepEqual(candidate(3),1); assert.deepEqual(candidate(7),1); assert.deepEqual(candidate(10),5); assert.deepEqual(candidate(100),50); assert.deepEqual(candidate(49),7); } test();
humaneval-HumanEval_0_has_close_elements.json-L8
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 j = i + 1; j < numbers.length; j++){ if(Math.abs(numbers[i] - numbers[j]) < threshold){ return true; } } } return false; }
for(var i = 0; i < numbers.length; i++){
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_160_do_algebra.json-L36
java
import java.util.*; import java.lang.reflect.*; import org.javatuples.*; import java.security.*; import java.math.*; import java.io.*; import java.util.stream.*; class Problem { // Given two array lists operator, and operand. The first array list has basic algebra operations, and // the second array list is an array array list of integers. Use the two given array lists 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 array list = [2, 3, 4, 5] // result = 2 + 3 * 4 - 5 // => result = 9 // Note: // The length of operator array list is equal to the length of operand array list minus one. // Operand is an array array list of of non-negative integers. // Operator array list has at least one operator, and operand array list has at least two operands. public static long doAlgebra(ArrayList<String> op, ArrayList<Long> operand) { ArrayList<Long> list = new ArrayList<>(); list.add(operand.get(0)); for (int i = 0; i < op.size(); i++) { if (op.get(i).equals("+")) list.add(operand.get(i + 1)); else if (op.get(i).equals("-")) list.add(-operand.get(i + 1)); else if (op.get(i).equals("*"))
else if (op.get(i).equals("//")) list.set(list.size() - 1, list.get(list.size() - 1) / operand.get(i + 1)); else if (op.get(i).equals("**")) list.set(list.size() - 1, (long) Math.pow(list.get(list.size() - 1), operand.get(i + 1))); } return list.stream().mapToLong(x -> x).sum(); } }
list.set(list.size() - 1, list.get(list.size() - 1) * operand.get(i + 1));
} public static void main(String[] args) { assert(doAlgebra((new ArrayList<String>(Arrays.asList((String)"**", (String)"*", (String)"+"))), (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)5l)))) == (37l)); assert(doAlgebra((new ArrayList<String>(Arrays.asList((String)"+", (String)"*", (String)"-"))), (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)5l)))) == (9l)); assert(doAlgebra((new ArrayList<String>(Arrays.asList((String)"//", (String)"*"))), (new ArrayList<Long>(Arrays.asList((long)7l, (long)3l, (long)4l)))) == (8l)); } }
humaneval-HumanEval_81_numerical_letter_grade.json-L41
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"); }
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; }
else if (grades[i] > 2.3){
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_155_even_odd_count.json-L19
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 ++; } else{ odds ++; } }
}
return [evens, odds];
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();

Dataset Card for "santacoder-fim-task"

This is a dataset of prompts and solutions to the fill-in-the-middle (FIM) task presented in the SantaCoder paper.

This dataset was generated using this notebook.

Downloads last month
117
Edit dataset card