name
stringlengths 34
63
| language
stringclasses 3
values | prompt
stringlengths 91
2.81k
| suffix
stringlengths 0
1.57k
| canonical_solution
stringlengths 1
219
| tests
stringlengths 149
4.98k
|
---|---|---|---|---|---|
humaneval-HumanEval_69_search.json-L24 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given a non-empty array list of positive integers. Return the greatest integer that is greater than
// zero, and has a frequency greater than or equal to the value of the integer itself.
// The frequency of an integer is the number of times it appears in the array list.
// If no such a value exist, return -1.
// Examples:
// >>> search((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)2l, (long)2l, (long)3l, (long)1l))))
// (2l)
// >>> search((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)2l, (long)3l, (long)3l, (long)3l, (long)4l, (long)4l, (long)4l))))
// (3l)
// >>> search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)4l, (long)4l, (long)4l))))
// (-1l)
public static long search(ArrayList<Long> lst) {
HashMap<Long, Long> map = new HashMap<Long, Long>();
Long max = (long)-1;
for (int i = 0; i < lst.size(); i++) { | if (map.containsKey(val)) {
map.put(val, map.get(val) + 1);
} else {
map.put(val, (long)1);
}
if (map.get(val) >= val && val > max) {
max = val;
}
}
return max;
}
} | long val = lst.get(i); | }
public static void main(String[] args) {
assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)5l, (long)5l, (long)1l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)4l, (long)1l, (long)4l, (long)4l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l)))) == (8l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)3l, (long)2l, (long)2l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)7l, (long)8l, (long)8l, (long)4l, (long)8l, (long)7l, (long)3l, (long)9l, (long)6l, (long)5l, (long)10l, (long)4l, (long)3l, (long)6l, (long)7l, (long)1l, (long)7l, (long)4l, (long)10l, (long)8l, (long)1l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)8l, (long)2l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)7l, (long)1l, (long)8l, (long)8l, (long)10l, (long)5l, (long)8l, (long)5l, (long)3l, (long)10l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)3l, (long)6l, (long)5l, (long)6l, (long)4l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)9l, (long)6l, (long)7l, (long)1l, (long)4l, (long)7l, (long)1l, (long)8l, (long)8l, (long)9l, (long)8l, (long)10l, (long)10l, (long)8l, (long)4l, (long)10l, (long)4l, (long)10l, (long)1l, (long)2l, (long)9l, (long)5l, (long)7l, (long)9l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)1l, (long)9l, (long)10l, (long)1l, (long)3l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)9l, (long)7l, (long)5l, (long)8l, (long)7l, (long)5l, (long)3l, (long)7l, (long)5l, (long)10l, (long)10l, (long)3l, (long)6l, (long)10l, (long)2l, (long)8l, (long)6l, (long)5l, (long)4l, (long)9l, (long)5l, (long)3l, (long)10l)))) == (5l));
assert(search((new ArrayList<Long>(Arrays.asList((long)1l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)10l, (long)6l, (long)4l, (long)3l, (long)5l, (long)8l, (long)2l, (long)4l, (long)2l, (long)8l, (long)4l, (long)6l, (long)10l, (long)4l, (long)2l, (long)1l, (long)10l, (long)2l, (long)1l, (long)1l, (long)5l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)10l, (long)4l, (long)8l, (long)2l, (long)10l, (long)5l, (long)1l, (long)2l, (long)9l, (long)5l, (long)5l, (long)6l, (long)3l, (long)8l, (long)6l, (long)4l, (long)10l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)1l, (long)6l, (long)10l, (long)1l, (long)6l, (long)9l, (long)10l, (long)8l, (long)6l, (long)8l, (long)7l, (long)3l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)2l, (long)4l, (long)1l, (long)5l, (long)1l, (long)5l, (long)2l, (long)5l, (long)7l, (long)7l, (long)7l, (long)3l, (long)10l, (long)1l, (long)5l, (long)4l, (long)2l, (long)8l, (long)4l, (long)1l, (long)9l, (long)10l, (long)7l, (long)10l, (long)2l, (long)8l, (long)10l, (long)9l, (long)4l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)4l, (long)2l, (long)8l, (long)7l, (long)5l, (long)6l, (long)4l, (long)10l, (long)4l, (long)6l, (long)3l, (long)7l, (long)8l, (long)8l, (long)3l, (long)1l, (long)4l, (long)2l, (long)2l, (long)10l, (long)7l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)8l, (long)6l, (long)10l, (long)2l, (long)6l, (long)10l, (long)2l, (long)7l, (long)8l, (long)10l, (long)3l, (long)8l, (long)2l, (long)6l, (long)2l, (long)3l, (long)1l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)3l, (long)9l, (long)5l, (long)6l, (long)3l, (long)2l, (long)8l, (long)5l, (long)6l, (long)10l, (long)10l, (long)6l, (long)8l, (long)4l, (long)10l, (long)7l, (long)7l, (long)10l, (long)8l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)10l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)7l, (long)7l, (long)2l, (long)4l, (long)7l, (long)2l, (long)10l, (long)9l, (long)7l, (long)5l, (long)7l, (long)2l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)4l, (long)10l, (long)2l, (long)1l, (long)1l, (long)10l, (long)3l, (long)6l, (long)1l, (long)8l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)7l, (long)9l, (long)9l, (long)9l, (long)3l, (long)4l, (long)1l, (long)5l, (long)9l, (long)1l, (long)2l, (long)1l, (long)1l, (long)10l, (long)7l, (long)5l, (long)6l, (long)7l, (long)6l, (long)7l, (long)7l, (long)6l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)10l, (long)10l, (long)9l, (long)2l)))) == (-1l));
}
}
|
humaneval-HumanEval_4_mean_absolute_deviation.json-L15 | js | //For a given array of input numbers, calculate Mean Absolute Deviation
// around the mean of this dataset.
// Mean Absolute Deviation is the average absolute difference between each
// element and a centerpoint (mean in this case):
// MAD = average | x - x_mean |
// >>> mean_absolute_deviation([1.0, 2.0, 3.0, 4.0])
// 1.0
function mean_absolute_deviation(numbers){
var sum = 0;
for(var i = 0; i < numbers.length; i++){
sum += numbers[i];
}
var mean = sum / numbers.length;
var sum_abs_diff = 0; | sum_abs_diff += Math.abs(numbers[i] - mean);
}
return sum_abs_diff / numbers.length;
}
| for(var i = 0; i < numbers.length; i++){ | const assert = require('node:assert');
function test() {
let candidate = mean_absolute_deviation;
assert.deepEqual(candidate([1.0, 2.0]),0.5);
assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0]),1.0);
assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0, 5.0]),1.2);
}
test(); |
humaneval-HumanEval_0_has_close_elements.json-L15 | js | //Check if in given array of numbers, are any two numbers closer to each other than
// given threshold.
// >>> has_close_elements([1.0, 2.0, 3.0], 0.5)
// false
// >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)
// true
function has_close_elements(numbers, threshold){
for(var i = 0; i < numbers.length; i++){
for(var j = i + 1; j < numbers.length; j++){
if(Math.abs(numbers[i] - numbers[j]) < threshold){
return true;
}
}
} | }
| return false; | 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_138_is_equal_to_sum_even.json-L40 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers
// Example
// >>> isEqualToSumEven((4l))
// (false)
// >>> isEqualToSumEven((6l))
// (false)
// >>> isEqualToSumEven((8l))
// (true)
public static boolean isEqualToSumEven(long n) {
if (n < 4) {
return false;
}
long d = 2;
if (n % 2 == 1) {
d = 1;
}
long minX = d;
long maxX = n / 4;
while (minX <= maxX) {
long x = (minX + maxX) / 2;
long y = n - 2 * x;
if (y % 2 == 1) {
y--;
}
if (2 * x + y < n) {
minX = x + 1;
} else if (2 * x + y > n) {
maxX = x - 1;
} else {
return true;
} | return false;
}
} | } | }
public static void main(String[] args) {
assert(isEqualToSumEven((4l)) == (false));
assert(isEqualToSumEven((6l)) == (false));
assert(isEqualToSumEven((8l)) == (true));
assert(isEqualToSumEven((10l)) == (true));
assert(isEqualToSumEven((11l)) == (false));
assert(isEqualToSumEven((12l)) == (true));
assert(isEqualToSumEven((13l)) == (false));
assert(isEqualToSumEven((16l)) == (true));
}
}
|
humaneval-HumanEval_121_solution.json-L13 | js | //Given a non-empty array of integers, return the sum of all of the odd elements that are in even positions.
// Examples
// >>> solution([5, 8, 7, 1])
// 12
// >>> solution([3, 3, 3, 3, 3])
// 9
// >>> solution([30, 13, 24, 321])
// 0
function solution(lst){
let result = 0;
for (let i = 0; i < lst.length; i++){
if (i % 2 === 0){ | result += lst[i];
}
}
}
return result;
} | if (lst[i] % 2 === 1){ | const assert = require('node:assert');
function test() {
let candidate = solution;
assert.deepEqual(candidate([5, 8, 7, 1]),12);
assert.deepEqual(candidate([3, 3, 3, 3, 3]),9);
assert.deepEqual(candidate([30, 13, 24, 321]),0);
assert.deepEqual(candidate([5, 9]),5);
assert.deepEqual(candidate([2, 4, 8]),0);
assert.deepEqual(candidate([30, 13, 23, 32]),23);
assert.deepEqual(candidate([3, 13, 2, 9]),3);
}
test(); |
humaneval-HumanEval_14_all_prefixes.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 of all prefixes from shortest to longest of the input string
// >>> allPrefixes(("abc"))
// (new ArrayList<String>(Arrays.asList((String)"a", (String)"ab", (String)"abc")))
public static ArrayList<String> allPrefixes(String string) {
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < string.length(); i++) {
result.add(string.substring(0, i + 1));
}
return result;
} | } | }
public static void main(String[] args) {
assert(allPrefixes(("")).equals((new ArrayList<String>(Arrays.asList()))));
assert(allPrefixes(("asdfgh")).equals((new ArrayList<String>(Arrays.asList((String)"a", (String)"as", (String)"asd", (String)"asdf", (String)"asdfg", (String)"asdfgh")))));
assert(allPrefixes(("WWW")).equals((new ArrayList<String>(Arrays.asList((String)"W", (String)"WW", (String)"WWW")))));
}
}
|
|
humaneval-HumanEval_124_valid_date.json-L41 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You have to write a function which validates a given date string and
// returns true if the date is valid otherwise false.
// The date is valid if all of the following rules are satisfied:
// 1. The date string is not empty.
// 2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the number of days is not less than 1 or higher than 30 days for months 4,6,9,11. And, the number of days is not less than 1 or higher than 29 for the month 2.
// 3. The months should not be less than 1 or higher than 12.
// 4. The date should be in the format: mm-dd-yyyy
// >>> validDate(("03-11-2000"))
// (true)
// >>> validDate(("15-01-2012"))
// (false)
// >>> validDate(("04-0-2040"))
// (false)
// >>> validDate(("06-04-2020"))
// (true)
// >>> validDate(("06/04/2020"))
// (false)
public static boolean validDate(String date) {
boolean valid_date = false;
if (date.equals("")) return valid_date;
String[] parts = date.split("-");
if (parts.length != 3) return valid_date;
try {
int month = Integer.parseInt(parts[0]);
int day = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
if (month < 1 || month > 12) return valid_date;
if (month == 2) {
if (day < 1 || day > 29) return valid_date;
} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day < 1 || day > 31) return valid_date;
} else { | }
valid_date = true;
} catch (Exception e) {
valid_date = false;
}
return valid_date;
}
} | if (day < 1 || day > 30) return valid_date; | }
public static void main(String[] args) {
assert(validDate(("03-11-2000")) == (true));
assert(validDate(("15-01-2012")) == (false));
assert(validDate(("04-0-2040")) == (false));
assert(validDate(("06-04-2020")) == (true));
assert(validDate(("01-01-2007")) == (true));
assert(validDate(("03-32-2011")) == (false));
assert(validDate(("")) == (false));
assert(validDate(("04-31-3000")) == (false));
assert(validDate(("06-06-2005")) == (true));
assert(validDate(("21-31-2000")) == (false));
assert(validDate(("04-12-2003")) == (true));
assert(validDate(("04122003")) == (false));
assert(validDate(("20030412")) == (false));
assert(validDate(("2003-04")) == (false));
assert(validDate(("2003-04-12")) == (false));
assert(validDate(("04-2003")) == (false));
}
}
|
humaneval-HumanEval_75_is_multiply_prime.json-L22 | 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: | else:
return False
| return True | 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_46_fib4.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 {
// 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((5l))
// (4l)
// >>> fib4((6l))
// (8l)
// >>> fib4((7l))
// (14l)
public static long fib4(long n) {
if (n == 0) return 0; | if (n == 2) return 2;
if (n == 3) return 0;
return fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4);
}
} | if (n == 1) return 0; | }
public static void main(String[] args) {
assert(fib4((5l)) == (4l));
assert(fib4((8l)) == (28l));
assert(fib4((10l)) == (104l));
assert(fib4((12l)) == (386l));
}
}
|
humaneval-HumanEval_121_solution.json-L22 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given a non-empty array list of integers, return the sum of all of the odd elements that are in even positions.
// Examples
// >>> solution((new ArrayList<Long>(Arrays.asList((long)5l, (long)8l, (long)7l, (long)1l))))
// (12l)
// >>> solution((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l, (long)3l, (long)3l, (long)3l))))
// (9l)
// >>> solution((new ArrayList<Long>(Arrays.asList((long)30l, (long)13l, (long)24l, (long)321l))))
// (0l)
public static long solution(ArrayList<Long> lst) {
long sum = 0;
for (int i = 0; i < lst.size(); i++) {
if (i % 2 == 0 && lst.get(i) % 2 == 1) {
sum += lst.get(i); | }
return sum;
}
} | } | }
public static void main(String[] args) {
assert(solution((new ArrayList<Long>(Arrays.asList((long)5l, (long)8l, (long)7l, (long)1l)))) == (12l));
assert(solution((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l, (long)3l, (long)3l, (long)3l)))) == (9l));
assert(solution((new ArrayList<Long>(Arrays.asList((long)30l, (long)13l, (long)24l, (long)321l)))) == (0l));
assert(solution((new ArrayList<Long>(Arrays.asList((long)5l, (long)9l)))) == (5l));
assert(solution((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)8l)))) == (0l));
assert(solution((new ArrayList<Long>(Arrays.asList((long)30l, (long)13l, (long)23l, (long)32l)))) == (23l));
assert(solution((new ArrayList<Long>(Arrays.asList((long)3l, (long)13l, (long)2l, (long)9l)))) == (3l));
}
}
|
humaneval-HumanEval_46_fib4.json-L21 | py | def fib4(n: int) -> int:
"""The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
fib4(0) -> 0
fib4(1) -> 0
fib4(2) -> 2
fib4(3) -> 0
fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4).
Please write a 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
"""
if n == 0:
return 0
elif n == 1:
return 0
elif n == 2: | elif n == 3:
return 0
else:
return fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4)
| return 2 | def check(candidate):
assert candidate(5) == 4
assert candidate(8) == 28
assert candidate(10) == 104
assert candidate(12) == 386
def test_check():
check(fib4)
test_check()
|
humaneval-HumanEval_141_file_name_check.json-L22 | 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_1 = file_name.slice(0,file_name.indexOf('.'));
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 'No';
}
| return 'Yes'; | 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_155_even_odd_count.json-L8 | 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){
| 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];
}
| num = Math.abs(num); | const assert = require('node:assert');
function test() {
let candidate = even_odd_count;
assert.deepEqual(candidate(7),[0, 1]);
assert.deepEqual(candidate(-78),[1, 1]);
assert.deepEqual(candidate(3452),[2, 2]);
assert.deepEqual(candidate(346211),[3, 3]);
assert.deepEqual(candidate(-345821),[3, 3]);
assert.deepEqual(candidate(-2),[1, 0]);
assert.deepEqual(candidate(-45347),[2, 3]);
assert.deepEqual(candidate(0),[1, 0]);
}
test(); |
humaneval-HumanEval_36_fizz_buzz.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 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;
for (long i = 0; i < n; i++) {
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;
}
} | } | }
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_12_longest.json-L14 | py | from typing import List, Optional
def longest(strings: List[str]) -> Optional[str]:
""" Out of list of strings, return the longest one. Return the first one in case of multiple
strings of the same length. Return None in case the input list is empty.
>>> longest([])
None
>>> longest(['a', 'b', 'c'])
'a'
>>> longest(['a', 'bb', 'ccc'])
'ccc'
"""
if not strings: | else:
long = strings[0]
for s in strings:
if len(s) > len(long):
long = s
return long
| return None | def check(candidate):
assert candidate([]) == None
assert candidate(['x', 'y', 'z']) == 'x'
assert candidate(['x', 'yyy', 'zzzz', 'www', 'kkkk', 'abc']) == 'zzzz'
def test_check():
check(longest)
test_check()
|
humaneval-HumanEval_63_fibfib.json-L16 | 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 | a, b, c = b, c, a + b + c
n -= 1
return a
| while n > 0: | 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_37_sort_even.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 {
// 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_127_intersection.json-L22 | js | //You are given two intervals,
// where each interval is a pair of integers. For example, interval = (start, end) = (1, 2).
// The given intervals are closed which means that the interval (start, end)
// includes both start and end.
// For each given interval, it is assumed that its start is less or equal its end.
// Your task is to determine whether the length of intersection of these two
// intervals is a prime number.
// Example, the intersection of the intervals (1, 3), (2, 4) is (2, 3)
// which its length is 1, which not a prime number.
// If the length of the intersection is a prime number, return "YES",
// otherwise, return "NO".
// If the two intervals don't intersect, return "NO".
// [input/output] samples:
// >>> intersection([1, 2], [2, 3])
// "NO"
// >>> intersection([-1, 1], [0, 4])
// "NO"
// >>> intersection([-3, -1], [-5, 5])
// "YES"
function intersection(interval1, interval2){
let start = Math.max(interval1[0], interval2[0]); | if (start >= end) {
return "NO";
}
let length = end - start;
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];
return primes.includes(length) ? "YES" : "NO";
}
| let end = Math.min(interval1[1], interval2[1]); | const assert = require('node:assert');
function test() {
let candidate = intersection;
assert.deepEqual(candidate([1, 2], [2, 3]),"NO");
assert.deepEqual(candidate([-1, 1], [0, 4]),"NO");
assert.deepEqual(candidate([-3, -1], [-5, 5]),"YES");
assert.deepEqual(candidate([-2, 2], [-4, 0]),"YES");
assert.deepEqual(candidate([-11, 2], [-1, -1]),"NO");
assert.deepEqual(candidate([1, 2], [3, 5]),"NO");
assert.deepEqual(candidate([1, 2], [1, 2]),"NO");
assert.deepEqual(candidate([-2, -2], [-3, -2]),"NO");
}
test(); |
humaneval-HumanEval_112_reverse_delete.json-L21 | js | //Task
// We are given two strings s and c, you have to deleted all the characters in s that are equal to any character in c
// then check if the result string is palindrome.
// A string is called palindrome if it reads the same backward as forward.
// You should return an array containing the result string and true/false for the check.
// Example
// >>> reverse_delete("abcde", "ae")
// ["bcd", false]
// >>> reverse_delete("abcdef", "b")
// ["acdef", false]
// >>> reverse_delete("abcdedcba", "ab")
// ["cdedc", true]
function reverse_delete(s, c){
const filter = s.split('').filter(x => c.includes(x) === false);
const filterString = filter.join('');
const filterStringReverse = filter.reverse().join('');
const result = [];
if (filterString === filterStringReverse) {
result.push(filterString, true);
} else { | }
return result;
} | result.push(filterString, false); | const assert = require('node:assert');
function test() {
let candidate = reverse_delete;
assert.deepEqual(candidate("abcde", "ae"),["bcd", false]);
assert.deepEqual(candidate("abcdef", "b"),["acdef", false]);
assert.deepEqual(candidate("abcdedcba", "ab"),["cdedc", true]);
assert.deepEqual(candidate("dwik", "w"),["dik", false]);
assert.deepEqual(candidate("a", "a"),["", true]);
assert.deepEqual(candidate("abcdedcba", ""),["abcdedcba", true]);
assert.deepEqual(candidate("abcdedcba", "v"),["abcdedcba", true]);
assert.deepEqual(candidate("vabba", "v"),["abba", true]);
assert.deepEqual(candidate("mamma", "mia"),["", true]);
}
test(); |
humaneval-HumanEval_26_remove_duplicates.json-L7 | js | //From an array of integers, remove all elements that occur more than once.
// Keep order of elements left the same as in the input.
// >>> remove_duplicates([1, 2, 3, 2, 4])
// [1, 3, 4]
function remove_duplicates(numbers){
let result = []; | if(numbers.indexOf(value) === numbers.lastIndexOf(value)){
result.push(value);
}
});
return result;
}
| numbers.forEach(function(value){ | const assert = require('node:assert');
function test() {
let candidate = remove_duplicates;
assert.deepEqual(candidate([]),[]);
assert.deepEqual(candidate([1, 2, 3, 4]),[1, 2, 3, 4]);
assert.deepEqual(candidate([1, 2, 3, 2, 4, 3, 5]),[1, 4, 5]);
}
test(); |
humaneval-HumanEval_124_valid_date.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 have to write a function which validates a given date string and
// returns true if the date is valid otherwise false.
// The date is valid if all of the following rules are satisfied:
// 1. The date string is not empty.
// 2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the number of days is not less than 1 or higher than 30 days for months 4,6,9,11. And, the number of days is not less than 1 or higher than 29 for the month 2.
// 3. The months should not be less than 1 or higher than 12.
// 4. The date should be in the format: mm-dd-yyyy
// >>> validDate(("03-11-2000"))
// (true)
// >>> validDate(("15-01-2012"))
// (false)
// >>> validDate(("04-0-2040"))
// (false)
// >>> validDate(("06-04-2020"))
// (true)
// >>> validDate(("06/04/2020"))
// (false)
public static boolean validDate(String date) {
boolean valid_date = false;
if (date.equals("")) return valid_date;
String[] parts = date.split("-");
if (parts.length != 3) return valid_date;
try {
int month = Integer.parseInt(parts[0]);
int day = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
if (month < 1 || month > 12) return valid_date;
if (month == 2) {
if (day < 1 || day > 29) return valid_date;
} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day < 1 || day > 31) return valid_date;
} else {
if (day < 1 || day > 30) return valid_date; | valid_date = true;
} catch (Exception e) {
valid_date = false;
}
return valid_date;
}
} | } | }
public static void main(String[] args) {
assert(validDate(("03-11-2000")) == (true));
assert(validDate(("15-01-2012")) == (false));
assert(validDate(("04-0-2040")) == (false));
assert(validDate(("06-04-2020")) == (true));
assert(validDate(("01-01-2007")) == (true));
assert(validDate(("03-32-2011")) == (false));
assert(validDate(("")) == (false));
assert(validDate(("04-31-3000")) == (false));
assert(validDate(("06-06-2005")) == (true));
assert(validDate(("21-31-2000")) == (false));
assert(validDate(("04-12-2003")) == (true));
assert(validDate(("04122003")) == (false));
assert(validDate(("20030412")) == (false));
assert(validDate(("2003-04")) == (false));
assert(validDate(("2003-04-12")) == (false));
assert(validDate(("04-2003")) == (false));
}
}
|
humaneval-HumanEval_65_circular_shift.json-L15 | js | //Circular shift the digits of the integer x, shift the digits right by shift
// and return the result as a string.
// If shift > number of digits, return digits reversed.
// >>> circular_shift(12, 1)
// "21"
// >>> circular_shift(12, 2)
// "12"
function circular_shift(x, shift){
var str = x.toString();
var len = str.length;
var res = '';
if(len < shift){
for(var i = len-1; i >= 0; i--){
res += str[i]; | }else{
res = str.substring(len-shift) + str.substring(0, len-shift);
}
return res;
}
| } | const assert = require('node:assert');
function test() {
let candidate = circular_shift;
assert.deepEqual(candidate(100, 2),"001");
assert.deepEqual(candidate(12, 2),"12");
assert.deepEqual(candidate(97, 8),"79");
assert.deepEqual(candidate(12, 1),"21");
assert.deepEqual(candidate(11, 101),"11");
}
test(); |
humaneval-HumanEval_109_move_one_ball.json-L26 | 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; | if(arr[i] > arr[i + 1]){
diff_count += 1;
}
}
if(diff_count > 1){
return false;
}
else{
return true;
}
} | for(var i = 0; i < arr.length - 1; i++){ | 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_149_sorted_list_sum.json-L17 | js | //Write a function that accepts an array of strings as a parameter,
// deletes the strings that have odd lengths from it,
// and returns the resulted array with a sorted order,
// The array is always an array of strings and never an array of numbers,
// and it may contain duplicates.
// The order of the array should be ascending by length of each word, and you
// should return the array sorted by that rule.
// If two words have the same length, sort the array alphabetically.
// The function should return an array of strings in sorted order.
// You may assume that all words will have the same length.
// For example:
// >>> list_sort(["aa", "a", "aaa"])
// ["aa"]
// >>> list_sort(["ab", "a", "aaa", "cd"])
// ["ab", "cd"]
function sorted_list_sum(lst){
| for(var i = 0; i < lst.length; i++){
if(lst[i].length % 2 == 0){
result.push(lst[i]);
}
}
return result.sort(function(a, b){
if(a.length < b.length){
return -1;
}
else if(a.length > b.length){
return 1;
}
else{
if(a < b){
return -1;
}
else if(a > b){
return 1;
}
else{
return 0;
}
}
});
}
var a = ["aa", "a", "aaa"];
var b = ["ab", "a", "aaa", "cd"]; | var result = []; | const assert = require('node:assert');
function test() {
let candidate = sorted_list_sum;
assert.deepEqual(candidate(["aa", "a", "aaa"]),["aa"]);
assert.deepEqual(candidate(["school", "AI", "asdf", "b"]),["AI", "asdf", "school"]);
assert.deepEqual(candidate(["d", "b", "c", "a"]),[]);
assert.deepEqual(candidate(["d", "dcba", "abcd", "a"]),["abcd", "dcba"]);
assert.deepEqual(candidate(["AI", "ai", "au"]),["AI", "ai", "au"]);
assert.deepEqual(candidate(["a", "b", "b", "c", "c", "a"]),[]);
assert.deepEqual(candidate(["aaaa", "bbbb", "dd", "cc"]),["cc", "dd", "aaaa", "bbbb"]);
}
test(); |
humaneval-HumanEval_37_sort_even.json-L19 | py | from typing import List
def sort_even(l: List[int]) -> List[int]:
"""This function takes a list l and returns a list l' such that
l' is identical to l in the odd indicies, while its values at the even indicies are equal
to the values of the even indicies of l, but sorted.
>>> sort_even([1, 2, 3])
[1, 2, 3]
>>> sort_even([5, 6, 3, 4])
[3, 6, 5, 4]
"""
if l:
temp: List[int] = []
for i in range(0, len(l), 2):
temp.append(l[i])
temp.sort()
for i in range(0, len(l), 2):
l[i] = temp[i // 2] | return l | def check(candidate):
assert candidate([1, 2, 3]) == [1, 2, 3]
assert candidate([5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10]) == [-10, 3, -5, 2, -3, 3, 5, 0, 9, 1, 123]
assert candidate([5, 8, -12, 4, 23, 2, 3, 11, 12, -10]) == [-12, 8, 3, 4, 5, 2, 12, 11, 23, -10]
def test_check():
check(sort_even)
test_check()
|
|
humaneval-HumanEval_144_simplify.json-L31 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Your task is to implement a function that will simplify the expression
// x * n. The function returns true if x * n evaluates to a whole number and false
// otherwise. Both x and n, are string representation of a fraction, and have the following format,
// <numerator>/<denominator> where both numerator and denominator are positive whole numbers.
// You can assume that x, and n are valid fractions, and do not have zero as denominator.
// >>> simplify(("1/5"), ("5/1"))
// (true)
// >>> simplify(("1/6"), ("2/1"))
// (false)
// >>> simplify(("7/10"), ("10/2"))
// (false)
public static boolean simplify(String x, String n) {
String[] nums = x.split("/");
String[] n_nums = n.split("/");
int x_num = Integer.parseInt(nums[0]);
int n_num = Integer.parseInt(n_nums[0]);
int x_denom = Integer.parseInt(nums[1]);
int n_denom = Integer.parseInt(n_nums[1]);
int res_num = x_num * n_num;
int res_denom = x_denom * n_denom;
return (res_num % res_denom == 0);
} | } | }
public static void main(String[] args) {
assert(simplify(("1/5"), ("5/1")) == (true));
assert(simplify(("1/6"), ("2/1")) == (false));
assert(simplify(("5/1"), ("3/1")) == (true));
assert(simplify(("7/10"), ("10/2")) == (false));
assert(simplify(("2/10"), ("50/10")) == (true));
assert(simplify(("7/2"), ("4/2")) == (true));
assert(simplify(("11/6"), ("6/1")) == (true));
assert(simplify(("2/3"), ("5/2")) == (false));
assert(simplify(("5/2"), ("3/5")) == (false));
assert(simplify(("2/4"), ("8/4")) == (true));
assert(simplify(("2/4"), ("4/2")) == (true));
assert(simplify(("1/5"), ("5/1")) == (true));
assert(simplify(("1/5"), ("1/5")) == (false));
}
}
|
|
humaneval-HumanEval_156_int_to_mini_roman.json-L25 | 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", | };
var output = "";
while (number > 0){
var key_list = Object.keys(roman_conversion).map(Number);
key_list.sort(function(a, b){return b-a});
for (var i = 0; i < key_list.length; i++){
if (key_list[i] <= number){
output += roman_conversion[key_list[i]];
number -= key_list[i];
break;
}
}
}
return output;
}
| 1: "i" | 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_6_parse_nested_parens.json-L22 | js | //Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
// For each of the group, output the deepest level of nesting of parentheses.
// E.g. (()()) has maximum two levels of nesting while ((())) has three.
// >>> parse_nested_parens("(()()) ((())) () ((())()())")
// [2, 3, 1, 3]
function parse_nested_parens(paren_string){
var level = 0;
var maxlevel = 0;
var result = [];
for (var i = 0; i < paren_string.length; i++){
if (paren_string[i] == "("){
level += 1;
if (level > maxlevel){
maxlevel = level;
}
}
else if (paren_string[i] == ")"){
level -= 1;
}
else if (paren_string[i] == " "){
result.push(maxlevel); | level = 0;
}
}
result.push(maxlevel);
return result;
}
| maxlevel = 0; | const assert = require('node:assert');
function test() {
let candidate = parse_nested_parens;
assert.deepEqual(candidate("(()()) ((())) () ((())()())"),[2, 3, 1, 3]);
assert.deepEqual(candidate("() (()) ((())) (((())))"),[1, 2, 3, 4]);
assert.deepEqual(candidate("(()(())((())))"),[4]);
}
test(); |
humaneval-HumanEval_81_numerical_letter_grade.json-L62 | 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-");
} | grades_array.push("E");
}
}
return grades_array;
} | else if (grades[i] == 0.0){ | 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_104_unique_digits.json-L11 | 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 = [] | 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})
}
| for(let i = 0; i < x.length; i++){ | 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_63_fibfib.json-L31 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
// fibfib(0) == 0
// fibfib(1) == 0
// fibfib(2) == 1
// fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3).
// Please write a function to efficiently compute the n-th element of the fibfib number sequence.
// >>> fibfib((1l))
// (0l)
// >>> fibfib((5l))
// (4l)
// >>> fibfib((8l))
// (24l)
public static long fibfib(long n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 0;
}
if (n == 2) {
return 1;
} | memo[0] = 0;
memo[1] = 0;
memo[2] = 1;
for (int i = 3; i <= n; i++) {
memo[i] = memo[i - 1] + memo[i - 2] + memo[i - 3];
}
return memo[(int) n];
}
} | long[] memo = new long[(int) n + 1]; | }
public static void main(String[] args) {
assert(fibfib((2l)) == (1l));
assert(fibfib((1l)) == (0l));
assert(fibfib((5l)) == (4l));
assert(fibfib((8l)) == (24l));
assert(fibfib((10l)) == (81l));
assert(fibfib((12l)) == (274l));
assert(fibfib((14l)) == (927l));
}
}
|
humaneval-HumanEval_119_match_parens.json-L20 | 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'
"""
| strings = [s1 + s2, s2 + s1]
for s in strings:
open_parens = 0
close_parens = 0
for c in s:
if c == '(':
open_parens += 1
elif c == ')':
if open_parens:
open_parens -= 1
else:
close_parens += 1
if open_parens == 0 and close_parens == 0:
return 'Yes'
return 'No'
| s1, s2 = lst | 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_150_x_or_y.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 {
// 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)) {
return x;
} else {
return y;
}
} | } | }
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_107_even_odd_palindrome.json-L30 | 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++){ | even++;
}
else{
odd++;
}
}
return [even, odd];
} | if(pal[i] % 2 == 0){ | 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_129_minPath.json-L41 | py | from typing import List
def minPath(grid: List[List[int]], k: int) -> List[int]:
"""
Given a grid with N rows and N columns (N >= 2) and a positive integer k,
each cell of the grid contains a value. Every integer in the range [1, N * N]
inclusive appears exactly once on the cells of the grid.
You have to find the minimum path of length k in the grid. You can start
from any cell, and in each step you can move to any of the neighbor cells,
in other words, you can go to cells which share an edge with you current
cell.
Please note that a path of length k means visiting exactly k cells (not
necessarily distinct).
You CANNOT go off the grid.
A path A (of length k) is considered less than a path B (of length k) if
after making the ordered lists of the values on the cells that A and B go
through (let's call them lst_A and lst_B), lst_A is lexicographically less
than lst_B, in other words, there exist an integer index i (1 <= i <= k)
such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have
lst_A[j] = lst_B[j].
It is guaranteed that the answer is unique.
Return an ordered list of the values on the cells that the minimum path go through.
Examples:
>>> minPath([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3)
[1, 2, 1]
>>> minPath([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1)
[1]
"""
min_val = float('inf')
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] < min_val:
min_val = grid[i][j]
row = i
col = j
path = [min_val]
while len(path) < k: | for i, j in (row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1):
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] < min_val:
min_val = grid[i][j]
row = i
col = j
path.append(min_val)
return path | min_val = float('inf') | def check(candidate):
assert candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3) == [1, 2, 1]
assert candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1) == [1]
assert candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4) == [1, 2, 1, 2]
assert candidate([[6, 4, 13, 10], [5, 7, 12, 1], [3, 16, 11, 15], [8, 14, 9, 2]], 7) == [1, 10, 1, 10, 1, 10, 1]
assert candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5) == [1, 7, 1, 7, 1]
assert candidate([[11, 8, 7, 2], [5, 16, 14, 4], [9, 3, 15, 6], [12, 13, 10, 1]], 9) == [1, 6, 1, 6, 1, 6, 1, 6, 1]
assert candidate([[12, 13, 10, 1], [9, 3, 15, 6], [5, 16, 14, 4], [11, 8, 7, 2]], 12) == [1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6]
assert candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8) == [1, 3, 1, 3, 1, 3, 1, 3]
assert candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8) == [1, 5, 1, 5, 1, 5, 1, 5]
assert candidate([[1, 2], [3, 4]], 10) == [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
assert candidate([[1, 3], [3, 2]], 10) == [1, 3, 1, 3, 1, 3, 1, 3, 1, 3]
def test_check():
check(minPath)
test_check()
|
humaneval-HumanEval_65_circular_shift.json-L11 | py | def circular_shift(x: int, shift: int) -> str:
"""Circular shift the digits of the integer x, shift the digits right by shift
and return the result as a string.
If shift > number of digits, return digits reversed.
>>> circular_shift(12, 1)
'21'
>>> circular_shift(12, 2)
'12'
"""
temp = list(str(x)) | return ''.join(reversed(temp))
else:
return ''.join(temp[-shift:] + temp[:-shift])
| if shift > len(temp): | def check(candidate):
assert candidate(100, 2) == '001'
assert candidate(12, 2) == '12'
assert candidate(97, 8) == '79'
assert candidate(12, 1) == '21'
assert candidate(11, 101) == '11'
def test_check():
check(circular_shift)
test_check()
|
humaneval-HumanEval_19_sort_numbers.json-L15 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Input is a space-delimited string of numberals from 'zero' to 'nine'.
// Valid choices are 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight' and 'nine'.
// Return the string with numbers sorted from smallest to largest
// >>> sortNumbers(("three one five"))
// ("one three five")
public static String sortNumbers(String numbers) {
| HashMap<String, Integer> indexMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
indexMap.put(nums[i], i);
}
String [] split = numbers.split(" ");
Arrays.sort(split, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return indexMap.get(s1) - indexMap.get(s2);
}
});
return String.join(" ", split);
}
} | String [] nums = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; | }
public static void main(String[] args) {
assert(sortNumbers(("")).equals(("")));
assert(sortNumbers(("three")).equals(("three")));
assert(sortNumbers(("three five nine")).equals(("three five nine")));
assert(sortNumbers(("five zero four seven nine eight")).equals(("zero four five seven eight nine")));
assert(sortNumbers(("six five four three two one zero")).equals(("zero one two three four five six")));
}
}
|
humaneval-HumanEval_150_x_or_y.json-L14 | js | //A simple program which should return the value of x if n is
// a prime number and should return the value of y otherwise.
// Examples:
// >>> x_or_y(7, 34, 12)
// 34
// >>> x_or_y(15, 8, 5)
// 5
function x_or_y(n, x, y){
if(n<2){
return y;
}
for(var i=2; i<n; i++){
if(n%i==0){ | }
}
return x;
} | return y; | const assert = require('node:assert');
function test() {
let candidate = x_or_y;
assert.deepEqual(candidate(7, 34, 12),34);
assert.deepEqual(candidate(15, 8, 5),5);
assert.deepEqual(candidate(3, 33, 5212),33);
assert.deepEqual(candidate(1259, 3, 52),3);
assert.deepEqual(candidate(7919, -1, 12),-1);
assert.deepEqual(candidate(3609, 1245, 583),583);
assert.deepEqual(candidate(91, 56, 129),129);
assert.deepEqual(candidate(6, 34, 1234),1234);
assert.deepEqual(candidate(1, 2, 0),0);
assert.deepEqual(candidate(2, 2, 0),2);
}
test(); |
humaneval-HumanEval_99_closest_integer.json-L29 | js | //Create a function that takes a value (string) representing a number
// and returns the closest integer to it. If the number is equidistant
// from two integers, round it away from zero.
// Examples
// >>> closest_integer("10")
// 10
// >>> closest_integer("15.3")
// 15
// Note:
// Rounding away from zero means that if the given number is equidistant
// from two integers, the one you should return is the one that is the
// farthest from zero. For example closest_integer("14.5") should
// return 15 and closest_integer("-14.5") should return -15.
function closest_integer(value){
value = parseFloat(value);
if(value > 0){
if(value - Math.floor(value) < 0.5){
return Math.floor(value);
}else{
return Math.ceil(value);
}
}else{
if(value - Math.floor(value) > -0.5){
return Math.floor(value);
}else{
return Math.ceil(value);
}
} | } | const assert = require('node:assert');
function test() {
let candidate = closest_integer;
assert.deepEqual(candidate("10"),10);
assert.deepEqual(candidate("14.5"),15);
assert.deepEqual(candidate("-15.5"),-16);
assert.deepEqual(candidate("15.3"),15);
assert.deepEqual(candidate("0"),0);
}
test(); |
|
humaneval-HumanEval_97_multiply.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 {
// Complete the function that takes two integers and returns
// the product of their unit digits.
// Assume the input is always valid.
// Examples:
// >>> multiply((148l), (412l))
// (16l)
// >>> multiply((19l), (28l))
// (72l)
// >>> multiply((2020l), (1851l))
// (0l)
// >>> multiply((14l), (-15l))
// (20l)
public static long multiply(long a, long b) {
long x = Math.abs(a % 10);
long y = Math.abs(b % 10); | }
} | return x * y; | }
public static void main(String[] args) {
assert(multiply((148l), (412l)) == (16l));
assert(multiply((19l), (28l)) == (72l));
assert(multiply((2020l), (1851l)) == (0l));
assert(multiply((14l), (-15l)) == (20l));
assert(multiply((76l), (67l)) == (42l));
assert(multiply((17l), (27l)) == (49l));
assert(multiply((0l), (1l)) == (0l));
assert(multiply((0l), (0l)) == (0l));
}
}
|
humaneval-HumanEval_106_f.json-L15 | py | from typing import List
def f(n: int) -> List[int]:
""" Implement the function f that takes n as a parameter,
and returns a list of size n, such that the value of the element at index i is the factorial of i if i is even
or the sum of numbers from 1 to i otherwise.
i starts from 1.
the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i).
Example:
>>> f(5)
[1, 2, 6, 24, 15]
"""
import math
a=[] | i=i+1
if i%2==0:
a.append(math.factorial(i))
else:
a.append(sum(range(1,i+1)))
return a
pass | for i in range(n): | def check(candidate):
assert candidate(5) == [1, 2, 6, 24, 15]
assert candidate(7) == [1, 2, 6, 24, 15, 720, 28]
assert candidate(1) == [1]
assert candidate(3) == [1, 2, 6]
def test_check():
check(f)
test_check()
|
humaneval-HumanEval_86_anti_shuffle.json-L19 | js | //Write a function that takes a string and returns an ordered version of it.
// Ordered version of string, is a string where all words (separated by space)
// are replaced by a new word where all the characters arranged in
// ascending order based on ascii value.
// Note: You should keep the order of words and blank spaces in the sentence.
// For example:
// >>> anti_shuffle("Hi")
// "Hi"
// >>> anti_shuffle("hello")
// "ehllo"
// >>> anti_shuffle("Hello World!!!")
// "Hello !!!Wdlor"
function anti_shuffle(s){
var result = [];
var strings = s.split(" ");
strings.forEach(function(str){
var temp = str.split("");
temp.sort(); | });
return result.join(" ");
} | result.push(temp.join("")); | const assert = require('node:assert');
function test() {
let candidate = anti_shuffle;
assert.deepEqual(candidate("Hi"),"Hi");
assert.deepEqual(candidate("hello"),"ehllo");
assert.deepEqual(candidate("number"),"bemnru");
assert.deepEqual(candidate("abcd"),"abcd");
assert.deepEqual(candidate("Hello World!!!"),"Hello !!!Wdlor");
assert.deepEqual(candidate(""),"");
assert.deepEqual(candidate("Hi. My name is Mister Robot. How are you?"),".Hi My aemn is Meirst .Rboot How aer ?ouy");
}
test(); |
humaneval-HumanEval_57_monotonic.json-L17 | js | //Return true is array elements are monotonically increasing or decreasing.
// >>> monotonic([1, 2, 4, 20])
// true
// >>> monotonic([1, 20, 4, 10])
// false
// >>> monotonic([4, 1, 0, -10])
// true
function monotonic(l){
if(l.length <= 1){
return true;
}
if(l[0] < l[1]){
for(var i = 1; i < l.length - 1; i++){
if(l[i] > l[i + 1]){
return false;
} | }
else{
for(var i = 1; i < l.length - 1; i++){
if(l[i] < l[i + 1]){
return false;
}
}
}
return true;
}
| } | const assert = require('node:assert');
function test() {
let candidate = monotonic;
assert.deepEqual(candidate([1, 2, 4, 10]),true);
assert.deepEqual(candidate([1, 2, 4, 20]),true);
assert.deepEqual(candidate([1, 20, 4, 10]),false);
assert.deepEqual(candidate([4, 1, 0, -10]),true);
assert.deepEqual(candidate([4, 1, 1, 0]),true);
assert.deepEqual(candidate([1, 2, 3, 2, 5, 60]),false);
assert.deepEqual(candidate([1, 2, 3, 4, 5, 60]),true);
assert.deepEqual(candidate([9, 9, 9, 9]),true);
}
test(); |
humaneval-HumanEval_64_vowels_count.json-L18 | js | //Write a function vowels_count which takes a string representing
// a word as input and returns the number of vowels in the string.
// Vowels in this case are 'a', 'e', 'i', 'o', 'u'. Here, 'y' is also a
// vowel, but only when it is at the end of the given word.
// Example:
// >>> vowels_count("abcde")
// 2
// >>> vowels_count("ACEDY")
// 3
function vowels_count(s){
s=s.toLowerCase();
var vowels = ['a','e','i','o','u'];
var count = 0;
for(var i=0; i<s.length; i++){
if(vowels.indexOf(s[i])!=-1){
count++;
} | if(s[s.length-1]=='y'){
count++;
}
return 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_118_get_closest_vowel.json-L27 | js | //You are given a word. Your task is to find the closest vowel that stands between
// two consonants from the right side of the word (case sensitive).
// Vowels in the beginning and ending doesn't count. Return empty string if you didn't
// find any vowel met the above condition.
// You may assume that the given string contains English letter only.
// Example:
// >>> get_closest_vowel("yogurt")
// "u"
// >>> get_closest_vowel("FULL")
// "U"
// >>> get_closest_vowel("quick")
// ""
// >>> get_closest_vowel("ab")
// ""
function get_closest_vowel(word){
let vowels = ["a", "e", "i", "o", "u"];
let i = word.length - 1;
let result = [];
if (word.length === 1) {
return ""
} else {
while (i >= 0) {
if (vowels.includes(word[i])) {
if (i > 0 && i < word.length - 1) {
if (!vowels.includes(word[i-1]) && !vowels.includes(word[i+1])) {
result.push(word[i]); | }
}
i--;
}
if (result.length === 0) {
return ""
} else {
return result[0];
}
}
} | } | const assert = require('node:assert');
function test() {
let candidate = get_closest_vowel;
assert.deepEqual(candidate("yogurt"),"u");
assert.deepEqual(candidate("full"),"u");
assert.deepEqual(candidate("easy"),"");
assert.deepEqual(candidate("eAsy"),"");
assert.deepEqual(candidate("ali"),"");
assert.deepEqual(candidate("bad"),"a");
assert.deepEqual(candidate("most"),"o");
assert.deepEqual(candidate("ab"),"");
assert.deepEqual(candidate("ba"),"");
assert.deepEqual(candidate("quick"),"");
assert.deepEqual(candidate("anime"),"i");
assert.deepEqual(candidate("Asia"),"");
assert.deepEqual(candidate("Above"),"o");
}
test(); |
humaneval-HumanEval_110_exchange.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 {
// In this problem, you will implement a function that takes two array lists of numbers,
// and determines whether it is possible to perform an exchange of elements
// between them to make lst1 an array array list of only even numbers.
// There is no limit on the number of exchanged elements between lst1 and lst2.
// If it is possible to exchange elements between the lst1 and lst2 to make
// all the elements of lst1 to be even, return "YES".
// Otherwise, return "NO".
// For example:
// >>> exchange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l))), (new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l))))
// ("YES")
// >>> exchange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l))), (new ArrayList<Long>(Arrays.asList((long)1l, (long)5l, (long)3l, (long)4l))))
// ("NO")
// It is assumed that the input array lists will be non-empty.
public static String exchange(ArrayList<Long> lst1, ArrayList<Long> lst2) {
for (int i = 0; i < lst1.size(); i++) {
if (lst1.get(i) % 2 == 1) {
for (int j = 0; j < lst2.size(); j++) {
if (lst2.get(j) % 2 == 0) {
lst1.set(i, lst2.get(j));
lst2.set(j, (long) 1);
break; | }
}
}
return (lst1.stream().allMatch(x -> x % 2 == 0)) ? "YES" : "NO";
}
} | } | }
public static void main(String[] args) {
assert(exchange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l))), (new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l)))).equals(("YES")));
assert(exchange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l))), (new ArrayList<Long>(Arrays.asList((long)1l, (long)5l, (long)3l, (long)4l)))).equals(("NO")));
assert(exchange((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l))), (new ArrayList<Long>(Arrays.asList((long)2l, (long)1l, (long)4l, (long)3l)))).equals(("YES")));
assert(exchange((new ArrayList<Long>(Arrays.asList((long)5l, (long)7l, (long)3l))), (new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)4l)))).equals(("YES")));
assert(exchange((new ArrayList<Long>(Arrays.asList((long)5l, (long)7l, (long)3l))), (new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)3l)))).equals(("NO")));
assert(exchange((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)6l, (long)1l, (long)8l, (long)9l))), (new ArrayList<Long>(Arrays.asList((long)3l, (long)5l, (long)5l, (long)1l, (long)1l, (long)1l)))).equals(("NO")));
assert(exchange((new ArrayList<Long>(Arrays.asList((long)100l, (long)200l))), (new ArrayList<Long>(Arrays.asList((long)200l, (long)200l)))).equals(("YES")));
}
}
|
humaneval-HumanEval_117_select_words.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 {
// 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_123_get_odd_collatz.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 {
// Given a positive integer n, return a sorted array 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 array 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.
// >>> getOddCollatz((5l))
// (new ArrayList<Long>(Arrays.asList((long)1l, (long)5l)))
public static ArrayList<Long> getOddCollatz(long n) {
if (n <= 0) return new ArrayList<Long>(); | long n1 = n;
while (n1 > 1) {
if (n1 % 2 == 1) odds.add(n1);
n1 = n1 % 2 == 0 ? n1 / 2 : 3 * n1 + 1;
}
odds.add(1l);
Long[] odds_arr = odds.toArray(new Long[odds.size()]);
ArrayList<Long> ans = new ArrayList<Long>(Arrays.asList(odds_arr));
Collections.sort(ans);
return ans;
}
} | HashSet<Long> odds = new HashSet<Long>(); | }
public static void main(String[] args) {
assert(getOddCollatz((14l)).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)5l, (long)7l, (long)11l, (long)13l, (long)17l)))));
assert(getOddCollatz((5l)).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)5l)))));
assert(getOddCollatz((12l)).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)5l)))));
assert(getOddCollatz((1l)).equals((new ArrayList<Long>(Arrays.asList((long)1l)))));
}
}
|
humaneval-HumanEval_126_is_sorted.json-L27 | js | //Given an array of numbers, return whether or not they are sorted
// in ascending order. If array has more than 1 duplicate of the same
// number, return false. Assume no negative numbers and only integers.
// Examples
// >>> is_sorted([5])
// true
// >>> is_sorted([1, 2, 3, 4, 5])
// true
// >>> is_sorted([1, 3, 2, 4, 5])
// false
// >>> is_sorted([1, 2, 3, 4, 5, 6])
// true
// >>> is_sorted([1, 2, 3, 4, 5, 6, 7])
// true
// >>> is_sorted([1, 3, 2, 4, 5, 6, 7])
// false
// >>> is_sorted([1, 2, 2, 3, 3, 4])
// true
// >>> is_sorted([1, 2, 2, 2, 3, 4])
// false
function is_sorted(lst){
var last = null;
var dupe_found = false;
for (var x of lst){
if (last == null){
last = x; | return false;
} else if (x == last){
if (dupe_found){
return false;
} else {
dupe_found = true;
}
} else {
last = x;
dupe_found = false;
}
}
return true;
}
| } else if (x < last){ | const assert = require('node:assert');
function test() {
let candidate = is_sorted;
assert.deepEqual(candidate([5]),true);
assert.deepEqual(candidate([1, 2, 3, 4, 5]),true);
assert.deepEqual(candidate([1, 3, 2, 4, 5]),false);
assert.deepEqual(candidate([1, 2, 3, 4, 5, 6]),true);
assert.deepEqual(candidate([1, 2, 3, 4, 5, 6, 7]),true);
assert.deepEqual(candidate([1, 3, 2, 4, 5, 6, 7]),false);
assert.deepEqual(candidate([]),true);
assert.deepEqual(candidate([1]),true);
assert.deepEqual(candidate([3, 2, 1]),false);
assert.deepEqual(candidate([1, 2, 2, 2, 3, 4]),false);
assert.deepEqual(candidate([1, 2, 3, 3, 3, 4]),false);
assert.deepEqual(candidate([1, 2, 2, 3, 3, 4]),true);
assert.deepEqual(candidate([1, 2, 3, 4]),true);
}
test(); |
humaneval-HumanEval_14_all_prefixes.json-L13 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Return array list of all prefixes from shortest to longest of the input string
// >>> allPrefixes(("abc"))
// (new ArrayList<String>(Arrays.asList((String)"a", (String)"ab", (String)"abc")))
public static ArrayList<String> allPrefixes(String string) {
| for (int i = 0; i < string.length(); i++) {
result.add(string.substring(0, i + 1));
}
return result;
}
} | ArrayList<String> result = new ArrayList<String>(); | }
public static void main(String[] args) {
assert(allPrefixes(("")).equals((new ArrayList<String>(Arrays.asList()))));
assert(allPrefixes(("asdfgh")).equals((new ArrayList<String>(Arrays.asList((String)"a", (String)"as", (String)"asd", (String)"asdf", (String)"asdfg", (String)"asdfgh")))));
assert(allPrefixes(("WWW")).equals((new ArrayList<String>(Arrays.asList((String)"W", (String)"WW", (String)"WWW")))));
}
}
|
humaneval-HumanEval_8_sum_product.json-L18 | js | //For a given array of integers, return an array consisting of a sum and a product of all the integers in an array.
// Empty sum should be equal to 0 and empty product should be equal to 1.
// >>> sum_product([])
// [0, 1]
// >>> sum_product([1, 2, 3, 4])
// [10, 24]
function sum_product(numbers){
if(numbers.length == 0){
return [0, 1];
}
let sum = 0;
let product = 1;
for(let i = 0; i < numbers.length; i++){
sum += numbers[i];
product *= numbers[i];
}
return [sum, product]; | } | const assert = require('node:assert');
function test() {
let candidate = sum_product;
assert.deepEqual(candidate([]),[0, 1]);
assert.deepEqual(candidate([1, 1, 1]),[3, 1]);
assert.deepEqual(candidate([100, 0]),[100, 0]);
assert.deepEqual(candidate([3, 5, 7]),[15, 105]);
assert.deepEqual(candidate([10]),[10, 10]);
}
test(); |
|
humaneval-HumanEval_129_minPath.json-L36 | 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;
}
}
} | for(let l = 0; l < k; l++){
path.push(currCell);
let neighbours = [];
let x = null;
let y = null;
for(let i = 0; i < n; i++){
for(let j = 0; j < n; j++){
if(grid[i][j] === currCell){
x = i;
y = j;
}
}
}
if(x > 0){
neighbours.push(grid[x - 1][y]);
}
if(x < n - 1){
neighbours.push(grid[x + 1][y]);
}
if(y > 0){
neighbours.push(grid[x][y - 1]);
}
if(y < n - 1){
neighbours.push(grid[x][y + 1]);
}
let nextCell = neighbours[0];
for(let i = 0; i < neighbours.length; i++){
let curr = neighbours[i];
if(curr < nextCell){
nextCell = curr;
}
}
currCell = nextCell;
}
return path;
} | let currCell = startCell; | const assert = require('node:assert');
function test() {
let candidate = minPath;
assert.deepEqual(candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3),[1, 2, 1]);
assert.deepEqual(candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1),[1]);
assert.deepEqual(candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4),[1, 2, 1, 2]);
assert.deepEqual(candidate([[6, 4, 13, 10], [5, 7, 12, 1], [3, 16, 11, 15], [8, 14, 9, 2]], 7),[1, 10, 1, 10, 1, 10, 1]);
assert.deepEqual(candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5),[1, 7, 1, 7, 1]);
assert.deepEqual(candidate([[11, 8, 7, 2], [5, 16, 14, 4], [9, 3, 15, 6], [12, 13, 10, 1]], 9),[1, 6, 1, 6, 1, 6, 1, 6, 1]);
assert.deepEqual(candidate([[12, 13, 10, 1], [9, 3, 15, 6], [5, 16, 14, 4], [11, 8, 7, 2]], 12),[1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6]);
assert.deepEqual(candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8),[1, 3, 1, 3, 1, 3, 1, 3]);
assert.deepEqual(candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8),[1, 5, 1, 5, 1, 5, 1, 5]);
assert.deepEqual(candidate([[1, 2], [3, 4]], 10),[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]);
assert.deepEqual(candidate([[1, 3], [3, 2]], 10),[1, 3, 1, 3, 1, 3, 1, 3, 1, 3]);
}
test(); |
humaneval-HumanEval_81_numerical_letter_grade.json-L27 | 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){ | }
else if (grades[i] > 3.7){
grades_array.push("A");
}
else if (grades[i] > 3.3){
grades_array.push("A-");
}
else if (grades[i] > 3.0){
grades_array.push("B+");
}
else if (grades[i] > 2.7){
grades_array.push("B");
}
else if (grades[i] > 2.3){
grades_array.push("B-");
}
else if (grades[i] > 2.0){
grades_array.push("C+");
}
else if (grades[i] > 1.7){
grades_array.push("C");
}
else if (grades[i] > 1.3){
grades_array.push("C-");
}
else if (grades[i] > 1.0){
grades_array.push("D+");
}
else if (grades[i] > 0.7){
grades_array.push("D");
}
else if (grades[i] > 0.0){
grades_array.push("D-");
}
else if (grades[i] == 0.0){
grades_array.push("E");
}
}
return grades_array;
} | grades_array.push("A+"); | 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_156_int_to_mini_roman.json-L20 | 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", | 10: "x",
9: "ix",
5: "v",
4: "iv",
1: "i"
};
var output = "";
while (number > 0){
var key_list = Object.keys(roman_conversion).map(Number);
key_list.sort(function(a, b){return b-a});
for (var i = 0; i < key_list.length; i++){
if (key_list[i] <= number){
output += roman_conversion[key_list[i]];
number -= key_list[i];
break;
}
}
}
return output;
}
| 40: "xl", | 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_43_pairs_sum_to_zero.json-L17 | js | //pairs_sum_to_zero takes an array of integers as an input.
// it returns true if there are two distinct elements in the array that
// sum to zero, and false otherwise.
// >>> pairs_sum_to_zero([1, 3, 5, 0])
// false
// >>> pairs_sum_to_zero([1, 3, -2, 1])
// false
// >>> pairs_sum_to_zero([1, 2, 3, 7])
// false
// >>> pairs_sum_to_zero([2, 4, -5, 3, 5, 7])
// true
// >>> pairs_sum_to_zero([1])
// false
function pairs_sum_to_zero(l){
var i = 0
while (i < l.length){ | while (j < l.length){
if ((l[i] + l[j]) === 0){
return true
}
j = j + 1
}
i = i + 1
}
return false
}
| var j = i+1 | const assert = require('node:assert');
function test() {
let candidate = pairs_sum_to_zero;
assert.deepEqual(candidate([1, 3, 5, 0]),false);
assert.deepEqual(candidate([1, 3, -2, 1]),false);
assert.deepEqual(candidate([1, 2, 3, 7]),false);
assert.deepEqual(candidate([2, 4, -5, 3, 5, 7]),true);
assert.deepEqual(candidate([1]),false);
assert.deepEqual(candidate([-3, 9, -1, 3, 2, 30]),true);
assert.deepEqual(candidate([-3, 9, -1, 3, 2, 31]),true);
assert.deepEqual(candidate([-3, 9, -1, 4, 2, 30]),false);
assert.deepEqual(candidate([-3, 9, -1, 4, 2, 31]),false);
}
test(); |
humaneval-HumanEval_161_solve.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 {
// You are given a string s.
// if s[i] is a letter, reverse its case from lower to upper or vise versa,
// otherwise keep it as it is.
// If the string contains no letters, reverse the string.
// The function should return the resulted string.
// Examples
// >>> solve(("1234"))
// ("4321")
// >>> solve(("ab"))
// ("AB")
// >>> solve(("#a@C"))
// ("#A@c")
public static String solve(String s) {
boolean letterNotFound = true;
StringBuilder sb = new StringBuilder();
if (s.length() == 0) return s;
for (int i = 0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i))) { | if (Character.isUpperCase(s.charAt(i))) {
sb.append(Character.toLowerCase(s.charAt(i)));
} else {
sb.append(Character.toUpperCase(s.charAt(i)));
}
} else {
sb.append(s.charAt(i));
}
}
if (letterNotFound) {
return sb.reverse().toString();
}
return sb.toString();
}
} | letterNotFound = false; | }
public static void main(String[] args) {
assert(solve(("AsDf")).equals(("aSdF")));
assert(solve(("1234")).equals(("4321")));
assert(solve(("ab")).equals(("AB")));
assert(solve(("#a@C")).equals(("#A@c")));
assert(solve(("#AsdfW^45")).equals(("#aSDFw^45")));
assert(solve(("#6@2")).equals(("2@6#")));
assert(solve(("#$a^D")).equals(("#$A^d")));
assert(solve(("#ccc")).equals(("#CCC")));
}
}
|
humaneval-HumanEval_44_change_base.json-L12 | py | def change_base(x: int, base: int) -> str:
"""Change numerical base of input number x to base.
return string representation after the conversion.
base numbers are less than 10.
>>> change_base(8, 3)
'22'
>>> change_base(8, 2)
'1000'
>>> change_base(7, 2)
'111'
"""
| return '0'
ans = []
while x != 0:
ans.append(str(x % base))
x //= base
return ''.join(ans[::-1])
| if x == 0: | def check(candidate):
assert candidate(8, 3) == '22'
assert candidate(9, 3) == '100'
assert candidate(234, 2) == '11101010'
assert candidate(16, 2) == '10000'
assert candidate(8, 2) == '1000'
assert candidate(7, 2) == '111'
assert candidate(2, 3) == '2'
assert candidate(3, 4) == '3'
assert candidate(4, 5) == '4'
assert candidate(5, 6) == '5'
assert candidate(6, 7) == '6'
assert candidate(7, 8) == '7'
def test_check():
check(change_base)
test_check()
|
humaneval-HumanEval_75_is_multiply_prime.json-L9 | 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){
| 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];
check.push(prime[i]);
}else{
i++;
}
}
if(check.length == 3){
return true;
}else{
return false;
}
} | a = Math.abs(a); | 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_26_remove_duplicates.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 {
// From an array array list of integers, remove all elements that occur more than once.
// Keep order of elements left the same as in the input.
// >>> removeDuplicates((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)2l, (long)4l))))
// (new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)4l)))
public static ArrayList<Long> removeDuplicates(ArrayList<Long> numbers) {
HashMap<Long, Long> hashmap = new HashMap<Long, Long>();
for (int i = 0; i < numbers.size(); i++) {
Long key = numbers.get(i);
if (hashmap.containsKey(key)) {
hashmap.put(key, hashmap.get(key) + 1);
} else {
hashmap.put(key, 1L);
}
}
ArrayList<Long> out = new ArrayList<Long>();
for (int i = 0; i < numbers.size(); i++) { | if (hashmap.get(key) == 1) {
out.add(key);
}
}
return out;
}
} | Long key = numbers.get(i); | }
public static void main(String[] args) {
assert(removeDuplicates((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<Long>(Arrays.asList()))));
assert(removeDuplicates((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l)))));
assert(removeDuplicates((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)2l, (long)4l, (long)3l, (long)5l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)5l)))));
}
}
|
humaneval-HumanEval_140_fix_spaces.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 {
// Given a string text, replace all spaces in it with underscores,
// and if a string has more than 2 consecutive spaces,
// then replace all consecutive spaces with -
// >>> fixSpaces((" Example"))
// ("Example")
// >>> fixSpaces((" Example 1"))
// ("Example_1")
// >>> fixSpaces((" Example 2"))
// ("_Example_2")
// >>> fixSpaces((" Example 3"))
// ("_Example-3")
public static String fixSpaces(String text) {
| int i = 0;
while (i < result.length() - 2) {
if (result.charAt(i) == '_' && result.charAt(i + 1) == '_' && result.charAt(i + 2) == '_') {
result = result.substring(0, i) + "-" + result.substring(i + 3);
}
i++;
}
return result;
}
} | String result = text.replace(" ", "_"); | }
public static void main(String[] args) {
assert(fixSpaces(("Example")).equals(("Example")));
assert(fixSpaces(("Mudasir Hanif ")).equals(("Mudasir_Hanif_")));
assert(fixSpaces(("Yellow Yellow Dirty Fellow")).equals(("Yellow_Yellow__Dirty__Fellow")));
assert(fixSpaces(("Exa mple")).equals(("Exa-mple")));
assert(fixSpaces((" Exa 1 2 2 mple")).equals(("-Exa_1_2_2_mple")));
}
}
|
humaneval-HumanEval_78_hex_key.json-L26 | 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 hexadecimals = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
let primes = ['2', '3', '5', '7', 'B', 'D']; | if(primes.includes(num[x])){
result += 1;
}
}
return result;
} | for(let x = 0; x < num.length; x++){ | 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_4_mean_absolute_deviation.json-L9 | js | //For a given array of input numbers, calculate Mean Absolute Deviation
// around the mean of this dataset.
// Mean Absolute Deviation is the average absolute difference between each
// element and a centerpoint (mean in this case):
// MAD = average | x - x_mean |
// >>> mean_absolute_deviation([1.0, 2.0, 3.0, 4.0])
// 1.0
function mean_absolute_deviation(numbers){
| for(var i = 0; i < numbers.length; i++){
sum += numbers[i];
}
var mean = sum / numbers.length;
var sum_abs_diff = 0;
for(var i = 0; i < numbers.length; i++){
sum_abs_diff += Math.abs(numbers[i] - mean);
}
return sum_abs_diff / numbers.length;
}
| var sum = 0; | const assert = require('node:assert');
function test() {
let candidate = mean_absolute_deviation;
assert.deepEqual(candidate([1.0, 2.0]),0.5);
assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0]),1.0);
assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0, 5.0]),1.2);
}
test(); |
humaneval-HumanEval_111_histogram.json-L25 | py | from typing import Dict
def histogram(test: str) -> Dict[str, int]:
"""Given a string representing a space separated lowercase letters, return a dictionary
of the letter with the most repetition and containing the corresponding count.
If several letters have the same occurrence, return all of them.
Example:
>>> histogram('a b c')
{ 'a': 1, 'b': 1, 'c': 1 }
>>> histogram('a b b a')
{ 'a': 2, 'b': 2 }
>>> histogram('a b c a b')
{ 'a': 2, 'b': 2 }
>>> histogram('b b b b a')
{ 'b': 4 }
>>> histogram('')
{ }
"""
letters = {}
most_count = 0
result = {}
for letter in test.split(): | if letters[letter] >= most_count:
most_count = letters[letter]
for letter in letters:
if letters[letter] == most_count:
result[letter] = most_count
return result
| letters[letter] = letters.get(letter, 0) + 1 | def check(candidate):
assert candidate('a b b a') == { 'a': 2, 'b': 2 }
assert candidate('a b c a b') == { 'a': 2, 'b': 2 }
assert candidate('a b c d g') == { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'g': 1 }
assert candidate('r t g') == { 'r': 1, 't': 1, 'g': 1 }
assert candidate('b b b b a') == { 'b': 4 }
assert candidate('r t g') == { 'r': 1, 't': 1, 'g': 1 }
assert candidate('') == { }
assert candidate('a') == { 'a': 1 }
def test_check():
check(histogram)
test_check()
|
humaneval-HumanEval_57_monotonic.json-L20 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// 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; | if (l.get(i) < a) {
inc = false;
}
if (l.get(i) > a) {
dec = false;
}
a = l.get(i);
}
return inc || dec;
}
} | for (int i = 1; i < l.size(); i++) { | }
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_129_minPath.json-L39 | py | from typing import List
def minPath(grid: List[List[int]], k: int) -> List[int]:
"""
Given a grid with N rows and N columns (N >= 2) and a positive integer k,
each cell of the grid contains a value. Every integer in the range [1, N * N]
inclusive appears exactly once on the cells of the grid.
You have to find the minimum path of length k in the grid. You can start
from any cell, and in each step you can move to any of the neighbor cells,
in other words, you can go to cells which share an edge with you current
cell.
Please note that a path of length k means visiting exactly k cells (not
necessarily distinct).
You CANNOT go off the grid.
A path A (of length k) is considered less than a path B (of length k) if
after making the ordered lists of the values on the cells that A and B go
through (let's call them lst_A and lst_B), lst_A is lexicographically less
than lst_B, in other words, there exist an integer index i (1 <= i <= k)
such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have
lst_A[j] = lst_B[j].
It is guaranteed that the answer is unique.
Return an ordered list of the values on the cells that the minimum path go through.
Examples:
>>> minPath([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3)
[1, 2, 1]
>>> minPath([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1)
[1]
"""
min_val = float('inf')
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] < min_val:
min_val = grid[i][j]
row = i
col = j | while len(path) < k:
min_val = float('inf')
for i, j in (row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1):
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] < min_val:
min_val = grid[i][j]
row = i
col = j
path.append(min_val)
return path | path = [min_val] | def check(candidate):
assert candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3) == [1, 2, 1]
assert candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1) == [1]
assert candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4) == [1, 2, 1, 2]
assert candidate([[6, 4, 13, 10], [5, 7, 12, 1], [3, 16, 11, 15], [8, 14, 9, 2]], 7) == [1, 10, 1, 10, 1, 10, 1]
assert candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5) == [1, 7, 1, 7, 1]
assert candidate([[11, 8, 7, 2], [5, 16, 14, 4], [9, 3, 15, 6], [12, 13, 10, 1]], 9) == [1, 6, 1, 6, 1, 6, 1, 6, 1]
assert candidate([[12, 13, 10, 1], [9, 3, 15, 6], [5, 16, 14, 4], [11, 8, 7, 2]], 12) == [1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6]
assert candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8) == [1, 3, 1, 3, 1, 3, 1, 3]
assert candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8) == [1, 5, 1, 5, 1, 5, 1, 5]
assert candidate([[1, 2], [3, 4]], 10) == [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
assert candidate([[1, 3], [3, 2]], 10) == [1, 3, 1, 3, 1, 3, 1, 3, 1, 3]
def test_check():
check(minPath)
test_check()
|
humaneval-HumanEval_116_sort_array.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 {
// In this Kata, you have to sort an array array list of non-negative integers according to
// number of ones in their binary representation in ascending order.
// For similar number of ones, sort based on decimal value.
// It must be implemented like this:
// >>> sortArray((new ArrayList<Long>(Arrays.asList((long)1l, (long)5l, (long)2l, (long)3l, (long)4l))))
// (new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l)))
// >>> sortArray((new ArrayList<Long>(Arrays.asList((long)-2l, (long)-3l, (long)-4l, (long)-5l, (long)-6l))))
// (new ArrayList<Long>(Arrays.asList((long)-6l, (long)-5l, (long)-4l, (long)-3l, (long)-2l)))
// >>> sortArray((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)2l, (long)3l, (long)4l))))
// (new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)2l, (long)3l, (long)4l)))
public static ArrayList<Long> sortArray(ArrayList<Long> arr) {
Collections.sort(arr, (a, b) -> {
long a1 = a, b1 = b;
if (a1 < 0) {
a1 = -a1;
}
if (b1 < 0) {
b1 = -b1;
} | });
return arr;
}
} | return Long.bitCount(a1) == Long.bitCount(b1) ? Long.compare(a, b) : Integer.compare(Long.bitCount(a1), Long.bitCount(b1)); | }
public static void main(String[] args) {
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)1l, (long)5l, (long)2l, (long)3l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)4l, (long)3l, (long)5l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)-2l, (long)-3l, (long)-4l, (long)-5l, (long)-6l)))).equals((new ArrayList<Long>(Arrays.asList((long)-4l, (long)-2l, (long)-6l, (long)-5l, (long)-3l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)2l, (long)3l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)2l, (long)4l, (long)3l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<Long>(Arrays.asList()))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)2l, (long)5l, (long)77l, (long)4l, (long)5l, (long)3l, (long)5l, (long)7l, (long)2l, (long)3l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l, (long)4l, (long)4l, (long)3l, (long)3l, (long)5l, (long)5l, (long)5l, (long)7l, (long)77l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)3l, (long)6l, (long)44l, (long)12l, (long)32l, (long)5l)))).equals((new ArrayList<Long>(Arrays.asList((long)32l, (long)3l, (long)5l, (long)6l, (long)12l, (long)44l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)8l, (long)16l, (long)32l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)8l, (long)16l, (long)32l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)8l, (long)16l, (long)32l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)8l, (long)16l, (long)32l)))));
}
}
|
humaneval-HumanEval_40_triples_sum_to_zero.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 {
// 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++; | k--;
}
}
}
return false;
}
} | } else { | }
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_128_prod_signs.json-L19 | py | from typing import List, Optional
def prod_signs(arr: List[int]) -> Optional[int]:
"""
You are given an array arr of integers and you need to return
sum of magnitudes of integers multiplied by product of all signs
of each number in the array, represented by 1, -1 or 0.
Note: return None for empty arr.
Example:
>>> prod_signs([1, 2, 2, -4])
9
>>> prod_signs([0, 1])
0
>>> prod_signs([])
None
"""
def product(lst: List[int]) -> int: | 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) | out = 1 | 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_3_below_zero.json-L20 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You're given an array array list of deposit and withdrawal operations on a bank account that starts with
// zero balance. Your task is to detect if at any point the balance of account fallls below zero, and
// at that point function should return true. Otherwise it should return false.
// >>> belowZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))))
// (false)
// >>> belowZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)-4l, (long)5l))))
// (true)
public static boolean belowZero(ArrayList<Long> operations) {
long balance = 0;
for (long op : operations) {
balance += op; | return true;
}
}
return false;
}
} | if (balance < 0) { | }
public static void main(String[] args) {
assert(belowZero((new ArrayList<Long>(Arrays.asList()))) == (false));
assert(belowZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)-3l, (long)1l, (long)2l, (long)-3l)))) == (false));
assert(belowZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)-4l, (long)5l, (long)6l)))) == (true));
assert(belowZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)-1l, (long)2l, (long)-2l, (long)5l, (long)-5l, (long)4l, (long)-4l)))) == (false));
assert(belowZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)-1l, (long)2l, (long)-2l, (long)5l, (long)-5l, (long)4l, (long)-5l)))) == (true));
assert(belowZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)-2l, (long)2l, (long)-2l, (long)5l, (long)-5l, (long)4l, (long)-4l)))) == (true));
}
}
|
humaneval-HumanEval_20_find_closest_elements.json-L21 | js | //From a supplied array of numbers (of length at least two) select and return two that are the closest to each
// other and return them in order (smaller number, larger number).
// >>> find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.2])
// [2.0, 2.2]
// >>> find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.0])
// [2.0, 2.0]
function find_closest_elements(numbers){
let closest = Math.abs(numbers[0] - numbers[1]);
let a = numbers[0];
let b = numbers[1];
for (let i = 0; i < numbers.length; i++) {
for (let j = i + 1; j < numbers.length; j++) {
if (Math.abs(numbers[i] - numbers[j]) < closest) {
closest = Math.abs(numbers[i] - numbers[j]);
a = numbers[i];
b = numbers[j];
}
}
}
return a > b ? [b, a] : [a, b]; | } | const assert = require('node:assert');
function test() {
let candidate = find_closest_elements;
assert.deepEqual(candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2]),[3.9, 4.0]);
assert.deepEqual(candidate([1.0, 2.0, 5.9, 4.0, 5.0]),[5.0, 5.9]);
assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.2]),[2.0, 2.2]);
assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0]),[2.0, 2.0]);
assert.deepEqual(candidate([1.1, 2.2, 3.1, 4.1, 5.1]),[2.2, 3.1]);
}
test(); |
|
humaneval-HumanEval_119_match_parens.json-L35 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given an array array list of two strings, both strings consist of open
// parentheses '(' or close parentheses ')' only.
// Your job is to check if it is possible to concatenate the two strings in
// some order, that the resulting string will be good.
// A string S is considered to be good if and only if all parentheses in S
// are balanced. For example: the string '(())()' is good, while the string
// '())' is not.
// Return 'Yes' if there's a way to make a good string, and return 'No' otherwise.
// Examples:
// >>> matchParens((new ArrayList<String>(Arrays.asList((String)"()(", (String)")"))))
// ("Yes")
// >>> matchParens((new ArrayList<String>(Arrays.asList((String)")", (String)")"))))
// ("No")
public static String matchParens(ArrayList<String> lst) {
String s1 = lst.get(0);
String s2 = lst.get(1);
String s3 = s1+s2;
String s4 = s2+s1;
int c1 = 0;
int c2 = 0;
for (int i = 0; i < s3.length(); i++) {
if (s3.charAt(i) == '(') c1 += 1;
else c1 -= 1;
if (c1 < 0) break;
}
for (int i = 0; i < s4.length(); i++) { | else c2 -= 1;
if (c2 < 0) break;
}
if (c1 == 0 || c2 == 0) return "Yes";
else return "No";
}
} | if (s4.charAt(i) == '(') c2 += 1; | }
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_63_fibfib.json-L35 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
// fibfib(0) == 0
// fibfib(1) == 0
// fibfib(2) == 1
// fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3).
// Please write a function to efficiently compute the n-th element of the fibfib number sequence.
// >>> fibfib((1l))
// (0l)
// >>> fibfib((5l))
// (4l)
// >>> fibfib((8l))
// (24l)
public static long fibfib(long n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 0;
}
if (n == 2) {
return 1;
}
long[] memo = new long[(int) n + 1];
memo[0] = 0;
memo[1] = 0;
memo[2] = 1; | memo[i] = memo[i - 1] + memo[i - 2] + memo[i - 3];
}
return memo[(int) n];
}
} | for (int i = 3; i <= n; i++) { | }
public static void main(String[] args) {
assert(fibfib((2l)) == (1l));
assert(fibfib((1l)) == (0l));
assert(fibfib((5l)) == (4l));
assert(fibfib((8l)) == (24l));
assert(fibfib((10l)) == (81l));
assert(fibfib((12l)) == (274l));
assert(fibfib((14l)) == (927l));
}
}
|
humaneval-HumanEval_69_search.json-L24 | py | from typing import List
def search(lst: List[int]) -> int:
"""
You are given a non-empty list of positive integers. Return the greatest integer that is greater than
zero, and has a frequency greater than or equal to the value of the integer itself.
The frequency of an integer is the number of times it appears in the list.
If no such a value exist, return -1.
Examples:
>>> search([4, 1, 2, 2, 3, 1])
2
>>> search([1, 2, 2, 3, 3, 3, 4, 4, 4])
3
>>> search([5, 5, 4, 4, 4])
-1
"""
dic = dict()
for i in lst:
if i in dic:
dic[i] += 1
else:
dic[i] = 1
maxValue = -1 | if value >= key and key > maxValue:
maxValue = key
return maxValue
| for key, value in dic.items(): | def check(candidate):
assert candidate([5, 5, 5, 5, 1]) == 1
assert candidate([4, 1, 4, 1, 4, 4]) == 4
assert candidate([3, 3]) == -1
assert candidate([8, 8, 8, 8, 8, 8, 8, 8]) == 8
assert candidate([2, 3, 3, 2, 2]) == 2
assert candidate([2, 7, 8, 8, 4, 8, 7, 3, 9, 6, 5, 10, 4, 3, 6, 7, 1, 7, 4, 10, 8, 1]) == 1
assert candidate([3, 2, 8, 2]) == 2
assert candidate([6, 7, 1, 8, 8, 10, 5, 8, 5, 3, 10]) == 1
assert candidate([8, 8, 3, 6, 5, 6, 4]) == -1
assert candidate([6, 9, 6, 7, 1, 4, 7, 1, 8, 8, 9, 8, 10, 10, 8, 4, 10, 4, 10, 1, 2, 9, 5, 7, 9]) == 1
assert candidate([1, 9, 10, 1, 3]) == 1
assert candidate([6, 9, 7, 5, 8, 7, 5, 3, 7, 5, 10, 10, 3, 6, 10, 2, 8, 6, 5, 4, 9, 5, 3, 10]) == 5
assert candidate([1]) == 1
assert candidate([8, 8, 10, 6, 4, 3, 5, 8, 2, 4, 2, 8, 4, 6, 10, 4, 2, 1, 10, 2, 1, 1, 5]) == 4
assert candidate([2, 10, 4, 8, 2, 10, 5, 1, 2, 9, 5, 5, 6, 3, 8, 6, 4, 10]) == 2
assert candidate([1, 6, 10, 1, 6, 9, 10, 8, 6, 8, 7, 3]) == 1
assert candidate([9, 2, 4, 1, 5, 1, 5, 2, 5, 7, 7, 7, 3, 10, 1, 5, 4, 2, 8, 4, 1, 9, 10, 7, 10, 2, 8, 10, 9, 4]) == 4
assert candidate([2, 6, 4, 2, 8, 7, 5, 6, 4, 10, 4, 6, 3, 7, 8, 8, 3, 1, 4, 2, 2, 10, 7]) == 4
assert candidate([9, 8, 6, 10, 2, 6, 10, 2, 7, 8, 10, 3, 8, 2, 6, 2, 3, 1]) == 2
assert candidate([5, 5, 3, 9, 5, 6, 3, 2, 8, 5, 6, 10, 10, 6, 8, 4, 10, 7, 7, 10, 8]) == -1
assert candidate([10]) == -1
assert candidate([9, 7, 7, 2, 4, 7, 2, 10, 9, 7, 5, 7, 2]) == 2
assert candidate([5, 4, 10, 2, 1, 1, 10, 3, 6, 1, 8]) == 1
assert candidate([7, 9, 9, 9, 3, 4, 1, 5, 9, 1, 2, 1, 1, 10, 7, 5, 6, 7, 6, 7, 7, 6]) == 1
assert candidate([3, 10, 10, 9, 2]) == -1
def test_check():
check(search)
test_check()
|
humaneval-HumanEval_118_get_closest_vowel.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 are given a word. Your task is to find the closest vowel that stands between
// two consonants from the right side of the word (case sensitive).
// Vowels in the beginning and ending doesn't count. Return empty string if you didn't
// find any vowel met the above condition.
// You may assume that the given string contains English letter only.
// Example:
// >>> getClosestVowel(("yogurt"))
// ("u")
// >>> getClosestVowel(("FULL"))
// ("U")
// >>> getClosestVowel(("quick"))
// ("")
// >>> getClosestVowel(("ab"))
// ("")
public static String getClosestVowel(String word) {
final String vowels = "aeiou"; | List<Integer> idx = new ArrayList<>();
word = word.toLowerCase();
for (int i = 0; i < word.length(); i++) {
if (vowels.indexOf(word.charAt(i)) != -1) {
idx.add(i);
}
}
for (int i = idx.size() - 1; i >= 0; i--) {
int j = idx.get(i);
if (j == 0 || j == word.length() - 1) continue;
char left = word.charAt(j - 1);
char right = word.charAt(j + 1);
if (consonants.indexOf(left) != -1 && consonants.indexOf(right) != -1) {
return String.valueOf(word.charAt(j));
}
}
return "";
}
} | final String consonants = "bcdfghjklmnpqrstvwxyz"; | }
public static void main(String[] args) {
assert(getClosestVowel(("yogurt")).equals(("u")));
assert(getClosestVowel(("full")).equals(("u")));
assert(getClosestVowel(("easy")).equals(("")));
assert(getClosestVowel(("eAsy")).equals(("")));
assert(getClosestVowel(("ali")).equals(("")));
assert(getClosestVowel(("bad")).equals(("a")));
assert(getClosestVowel(("most")).equals(("o")));
assert(getClosestVowel(("ab")).equals(("")));
assert(getClosestVowel(("ba")).equals(("")));
assert(getClosestVowel(("quick")).equals(("")));
assert(getClosestVowel(("anime")).equals(("i")));
assert(getClosestVowel(("Asia")).equals(("")));
assert(getClosestVowel(("Above")).equals(("o")));
}
}
|
humaneval-HumanEval_101_words_string.json-L23 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You will be given a string of words separated by commas or spaces. Your task is
// to split the string into words and return an array array list of the words.
// For example:
// >>> wordsString(("Hi, my name is John"))
// (new ArrayList<String>(Arrays.asList((String)"Hi", (String)"my", (String)"name", (String)"is", (String)"John")))
// >>> wordsString(("One, two, three, four, five, six"))
// (new ArrayList<String>(Arrays.asList((String)"One", (String)"two", (String)"three", (String)"four", (String)"five", (String)"six")))
public static ArrayList<String> wordsString(String s) {
ArrayList<String> al = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(s, " , ");
while (st.hasMoreTokens()) {
al.add(st.nextToken());
}
return al; | } | } | }
public static void main(String[] args) {
assert(wordsString(("Hi, my name is John")).equals((new ArrayList<String>(Arrays.asList((String)"Hi", (String)"my", (String)"name", (String)"is", (String)"John")))));
assert(wordsString(("One, two, three, four, five, six")).equals((new ArrayList<String>(Arrays.asList((String)"One", (String)"two", (String)"three", (String)"four", (String)"five", (String)"six")))));
assert(wordsString(("Hi, my name")).equals((new ArrayList<String>(Arrays.asList((String)"Hi", (String)"my", (String)"name")))));
assert(wordsString(("One,, two, three, four, five, six,")).equals((new ArrayList<String>(Arrays.asList((String)"One", (String)"two", (String)"three", (String)"four", (String)"five", (String)"six")))));
assert(wordsString(("")).equals((new ArrayList<String>(Arrays.asList()))));
assert(wordsString(("ahmed , gamal")).equals((new ArrayList<String>(Arrays.asList((String)"ahmed", (String)"gamal")))));
}
}
|
humaneval-HumanEval_158_find_max.json-L13 | 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 = {}; | 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;
} | var 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_110_exchange.json-L28 | py | from typing import List
def exchange(lst1: List[int], lst2: List[int]) -> str:
"""In this problem, you will implement a function that takes two lists of numbers,
and determines whether it is possible to perform an exchange of elements
between them to make lst1 a list of only even numbers.
There is no limit on the number of exchanged elements between lst1 and lst2.
If it is possible to exchange elements between the lst1 and lst2 to make
all the elements of lst1 to be even, return "YES".
Otherwise, return "NO".
For example:
>>> exchange([1, 2, 3, 4], [1, 2, 3, 4])
'YES'
>>> exchange([1, 2, 3, 4], [1, 5, 3, 4])
'NO'
It is assumed that the input lists will be non-empty.
"""
count = 0
for x in lst1:
if x % 2 == 1:
count += 1
for y in lst2:
if y % 2 == 0:
count -= 1
if count > 0:
return "NO"
else: | return "YES" | def check(candidate):
assert candidate([1, 2, 3, 4], [1, 2, 3, 4]) == 'YES'
assert candidate([1, 2, 3, 4], [1, 5, 3, 4]) == 'NO'
assert candidate([1, 2, 3, 4], [2, 1, 4, 3]) == 'YES'
assert candidate([5, 7, 3], [2, 6, 4]) == 'YES'
assert candidate([5, 7, 3], [2, 6, 3]) == 'NO'
assert candidate([3, 2, 6, 1, 8, 9], [3, 5, 5, 1, 1, 1]) == 'NO'
assert candidate([100, 200], [200, 200]) == 'YES'
def test_check():
check(exchange)
test_check()
|
|
humaneval-HumanEval_160_do_algebra.json-L44 | js | //Given two arrays operator, and operand. The first array has basic algebra operations, and
// the second array is an array of integers. Use the two given arrays to build the algebric
// expression and return the evaluation of this expression.
// The basic algebra operations:
// Addition ( + )
// Subtraction ( - )
// Multiplication ( * )
// Floor division ( // )
// Exponentiation ( ** )
// Example:
// operator['+', '*', '-']
// array = [2, 3, 4, 5]
// result = 2 + 3 * 4 - 5
// => result = 9
// Note:
// The length of operator array is equal to the length of operand array minus one.
// Operand is an array of of non-negative integers.
// Operator array has at least one operator, and operand array has at least two operands.
function do_algebra(operator, operand){
let do_again;
let count = 0;
let temp;
let answer;
while(count < operator.length){
switch(operator[count]){
case '*':
operand[count] = operand[count] * operand[count + 1];
operand.splice(count + 1, 1);
operator.splice(count, 1);
count = 0;
do_again = true;
break;
case '//':
operand[count] = Math.floor(operand[count] / operand[count + 1]);
operand.splice(count + 1, 1);
operator.splice(count, 1);
count = 0;
do_again = true;
break;
case '**':
operand[count] = Math.pow(operand[count], operand[count + 1]);
operand.splice(count + 1, 1);
operator.splice(count, 1); | 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;
}
| count = 0; | const assert = require('node:assert');
function test() {
let candidate = do_algebra;
assert.deepEqual(candidate(["**", "*", "+"], [2, 3, 4, 5]),37);
assert.deepEqual(candidate(["+", "*", "-"], [2, 3, 4, 5]),9);
assert.deepEqual(candidate(["//", "*"], [7, 3, 4]),8);
}
test(); |
humaneval-HumanEval_108_count_nums.json-L18 | 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; | 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;
} | var str = arr[i].toString(); | 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_6_parse_nested_parens.json-L15 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
// For each of the group, output the deepest level of nesting of parentheses.
// E.g. (()()) has maximum two levels of nesting while ((())) has three.
// >>> parseNestedParens(("(()()) ((())) () ((())()())"))
// (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)1l, (long)3l)))
public static ArrayList<Long> parseNestedParens(String paren_string) {
| String[] paren_strings = paren_string.split(" ");
for (String paren : paren_strings) {
long max_depth = 0;
long current_depth = 0;
for (char c : paren.toCharArray()) {
if (c == '(') {
current_depth += 1;
max_depth = Math.max(max_depth, current_depth);
} else if (c == ')') {
current_depth -= 1;
}
}
result.add(max_depth);
}
return result;
}
} | ArrayList<Long> result = new ArrayList<Long>(); | }
public static void main(String[] args) {
assert(parseNestedParens(("(()()) ((())) () ((())()())")).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)1l, (long)3l)))));
assert(parseNestedParens(("() (()) ((())) (((())))")).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l)))));
assert(parseNestedParens(("(()(())((())))")).equals((new ArrayList<Long>(Arrays.asList((long)4l)))));
}
}
|
humaneval-HumanEval_37_sort_even.json-L23 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// 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_93_encode.json-L15 | js | //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"
function encode(message){
const vowels = "aeiou";
let result = "";
for(let i = 0; i < message.length; i++){ | if(vowels.includes(letter.toLowerCase())){
let newIndex = (letter.charCodeAt(0) + 2 - (letter.toLowerCase() === letter ? 'a'.charCodeAt(0) : 'A'.charCodeAt(0))) % 26;
letter = String.fromCharCode(newIndex + (letter.toLowerCase() === letter ? 'a'.charCodeAt(0) : 'A'.charCodeAt(0)));
}
result += letter.toUpperCase() === letter ? letter.toLowerCase() : letter.toUpperCase();
}
return result;
} | let letter = message[i]; | const assert = require('node:assert');
function test() {
let candidate = encode;
assert.deepEqual(candidate("TEST"),"tgst");
assert.deepEqual(candidate("Mudasir"),"mWDCSKR");
assert.deepEqual(candidate("YES"),"ygs");
assert.deepEqual(candidate("This is a message"),"tHKS KS C MGSSCGG");
assert.deepEqual(candidate("I DoNt KnOw WhAt tO WrItE"),"k dQnT kNqW wHcT Tq wRkTg");
}
test(); |
humaneval-HumanEval_63_fibfib.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 {
// The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
// fibfib(0) == 0
// fibfib(1) == 0
// fibfib(2) == 1
// fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3).
// Please write a function to efficiently compute the n-th element of the fibfib number sequence.
// >>> fibfib((1l))
// (0l)
// >>> fibfib((5l))
// (4l)
// >>> fibfib((8l))
// (24l)
public static long fibfib(long n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 0;
}
if (n == 2) {
return 1; | long[] memo = new long[(int) n + 1];
memo[0] = 0;
memo[1] = 0;
memo[2] = 1;
for (int i = 3; i <= n; i++) {
memo[i] = memo[i - 1] + memo[i - 2] + memo[i - 3];
}
return memo[(int) n];
}
} | } | }
public static void main(String[] args) {
assert(fibfib((2l)) == (1l));
assert(fibfib((1l)) == (0l));
assert(fibfib((5l)) == (4l));
assert(fibfib((8l)) == (24l));
assert(fibfib((10l)) == (81l));
assert(fibfib((12l)) == (274l));
assert(fibfib((14l)) == (927l));
}
}
|
humaneval-HumanEval_61_correct_bracketing.json-L26 | js | //brackets is a string of "(" and ")".
// return true if every opening bracket has a corresponding closing bracket.
// >>> correct_bracketing("(")
// false
// >>> correct_bracketing("()")
// true
// >>> correct_bracketing("(()())")
// true
// >>> correct_bracketing(")(()")
// false
function correct_bracketing(brackets){
var opens = [];
for (var i = 0; i < brackets.length; i++){
if (brackets[i] == "("){
opens.push(brackets[i]);
}
else if (brackets[i] == ")"){
if (opens.length > 0){
opens.pop();
}
else {
return false;
}
}
} | return true;
}
else {
return false;
}
} | if (opens.length == 0){ | 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_11_string_xor.json-L12 | js | //Input are two strings a and b consisting only of 1s and 0s.
// Perform binary XOR on these inputs and return result also as a string.
// >>> string_xor("010", "110")
// "100"
function string_xor(a, b){
var arrayA = [];
var arrayB = [];
var arrayXOR = [];
var result = '';
for (var i = 0; i < a.length; i++) {
arrayA.push(parseInt(a[i])); | for (var j = 0; j < b.length; j++) {
arrayB.push(parseInt(b[j]));
}
for (var k = 0; k < arrayA.length; k++) {
if (arrayA[k] !== arrayB[k]) {
arrayXOR.push(1);
}
else {
arrayXOR.push(0);
}
}
result = arrayXOR.join('');
return result;
} | } | const assert = require('node:assert');
function test() {
let candidate = string_xor;
assert.deepEqual(candidate("111000", "101010"),"010010");
assert.deepEqual(candidate("1", "1"),"0");
assert.deepEqual(candidate("0101", "0000"),"0101");
}
test(); |
humaneval-HumanEval_85_add.json-L13 | js | //Given a non-empty array of integers lst. add the even elements that are at odd indices..
// Examples:
// >>> add([4, 2, 6, 7])
// 2
function add(lst){
var add = 0;
for(var i = 0; i < lst.length; i++) {
if(lst[i] % 2 == 0 && i % 2 != 0) {
add += lst[i];
}
}
return add; | } | const assert = require('node:assert');
function test() {
let candidate = add;
assert.deepEqual(candidate([4, 88]),88);
assert.deepEqual(candidate([4, 5, 6, 7, 2, 122]),122);
assert.deepEqual(candidate([4, 0, 6, 7]),0);
assert.deepEqual(candidate([4, 4, 6, 8]),12);
}
test(); |
|
humaneval-HumanEval_9_rolling_max.json-L6 | js | //From a given array of integers, generate an array of rolling maximum element found until given moment
// in the sequence.
// >>> rolling_max([1, 2, 3, 2, 3, 4, 2])
// [1, 2, 3, 3, 3, 4, 4]
function rolling_max(numbers){
| let tempMax = 0;
for(let i = 0; i < numbers.length; i++){
if(tempMax < numbers[i]){
tempMax = numbers[i];
}
max.push(tempMax);
}
return max;
}
| let max = []; | const assert = require('node:assert');
function test() {
let candidate = rolling_max;
assert.deepEqual(candidate([]),[]);
assert.deepEqual(candidate([1, 2, 3, 4]),[1, 2, 3, 4]);
assert.deepEqual(candidate([4, 3, 2, 1]),[4, 4, 4, 4]);
assert.deepEqual(candidate([3, 2, 3, 100, 3]),[3, 3, 3, 100, 100]);
}
test(); |
humaneval-HumanEval_75_is_multiply_prime.json-L15 | 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){ | check.push(prime[i]);
}else{
i++;
}
}
if(check.length == 3){
return true;
}else{
return false;
}
} | a = a / 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_118_get_closest_vowel.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 {
// You are given a word. Your task is to find the closest vowel that stands between
// two consonants from the right side of the word (case sensitive).
// Vowels in the beginning and ending doesn't count. Return empty string if you didn't
// find any vowel met the above condition.
// You may assume that the given string contains English letter only.
// Example:
// >>> getClosestVowel(("yogurt"))
// ("u")
// >>> getClosestVowel(("FULL"))
// ("U")
// >>> getClosestVowel(("quick"))
// ("")
// >>> getClosestVowel(("ab"))
// ("")
public static String getClosestVowel(String word) {
final String vowels = "aeiou";
final String consonants = "bcdfghjklmnpqrstvwxyz";
List<Integer> idx = new ArrayList<>();
word = word.toLowerCase();
for (int i = 0; i < word.length(); i++) {
if (vowels.indexOf(word.charAt(i)) != -1) {
idx.add(i);
}
}
for (int i = idx.size() - 1; i >= 0; i--) {
int j = idx.get(i);
if (j == 0 || j == word.length() - 1) continue;
char left = word.charAt(j - 1);
char right = word.charAt(j + 1);
if (consonants.indexOf(left) != -1 && consonants.indexOf(right) != -1) {
return String.valueOf(word.charAt(j));
}
}
return "";
} | } | }
public static void main(String[] args) {
assert(getClosestVowel(("yogurt")).equals(("u")));
assert(getClosestVowel(("full")).equals(("u")));
assert(getClosestVowel(("easy")).equals(("")));
assert(getClosestVowel(("eAsy")).equals(("")));
assert(getClosestVowel(("ali")).equals(("")));
assert(getClosestVowel(("bad")).equals(("a")));
assert(getClosestVowel(("most")).equals(("o")));
assert(getClosestVowel(("ab")).equals(("")));
assert(getClosestVowel(("ba")).equals(("")));
assert(getClosestVowel(("quick")).equals(("")));
assert(getClosestVowel(("anime")).equals(("i")));
assert(getClosestVowel(("Asia")).equals(("")));
assert(getClosestVowel(("Above")).equals(("o")));
}
}
|
|
humaneval-HumanEval_13_greatest_common_divisor.json-L12 | py | def greatest_common_divisor(a: int, b: int) -> int:
""" Return a greatest common divisor of two integers a and b
>>> greatest_common_divisor(3, 5)
1
>>> greatest_common_divisor(25, 15)
5
"""
if a > b:
a, b = b, a
if a == 0:
return b | return greatest_common_divisor(b % a, a) | def check(candidate):
assert candidate(3, 7) == 1
assert candidate(10, 15) == 5
assert candidate(49, 14) == 7
assert candidate(144, 60) == 12
def test_check():
check(greatest_common_divisor)
test_check()
|
|
humaneval-HumanEval_72_will_it_fly.json-L19 | js | //Write a function that returns true if the object q will fly, and false otherwise.
// The object q will fly if it's balanced (it is a palindromic array) and the sum of its elements is less than or equal the maximum possible weight w.
// Example:
// >>> will_it_fly([1, 2], 5)
// false
// # 1+2 is less than the maximum possible weight, but it's unbalanced.
// >>> will_it_fly([3, 2, 3], 1)
// false
// # it's balanced, but 3+2+3 is more than the maximum possible weight.
// >>> will_it_fly([3, 2, 3], 9)
// true
// # 3+2+3 is less than the maximum possible weight, and it's balanced.
// >>> will_it_fly([3], 5)
// true
// # 3 is less than the maximum possible weight, and it's balanced.
function will_it_fly(q, w){
let sum = 0;
let balanced = true; | sum += q[i];
}
for(let i = 0; i < q.length; i++){
if(q[i] !== q[q.length - 1 - i]){
balanced = false;
break;
}
}
return balanced && sum <= w;
} | for(let i = 0; i < q.length; i++){ | const assert = require('node:assert');
function test() {
let candidate = will_it_fly;
assert.deepEqual(candidate([3, 2, 3], 9),true);
assert.deepEqual(candidate([1, 2], 5),false);
assert.deepEqual(candidate([3], 5),true);
assert.deepEqual(candidate([3, 2, 3], 1),false);
assert.deepEqual(candidate([1, 2, 3], 6),false);
assert.deepEqual(candidate([5], 5),true);
}
test(); |
humaneval-HumanEval_73_smallest_change.json-L22 | py | from typing import List
def smallest_change(arr: List[int]) -> int:
"""
Given an array arr of integers, find the minimum number of elements that
need to be changed to make the array palindromic. A palindromic array is an array that
is read the same backwards and forwards. In one change, you can change one element to any other element.
For example:
>>> smallest_change([1, 2, 3, 5, 4, 7, 9, 6])
4
>>> smallest_change([1, 2, 3, 4, 3, 2, 2])
1
>>> smallest_change([1, 2, 3, 2, 1])
0
"""
start = 0
end = len(arr) - 1
count = 0
while start < end:
if arr[start] != arr[end]: | start += 1
end -= 1
return count
| count += 1 | def check(candidate):
assert candidate([1, 2, 3, 5, 4, 7, 9, 6]) == 4
assert candidate([1, 2, 3, 4, 3, 2, 2]) == 1
assert candidate([1, 4, 2]) == 1
assert candidate([1, 4, 4, 2]) == 1
assert candidate([1, 2, 3, 2, 1]) == 0
assert candidate([3, 1, 1, 3]) == 0
assert candidate([1]) == 0
assert candidate([0, 1]) == 1
def test_check():
check(smallest_change)
test_check()
|
humaneval-HumanEval_46_fib4.json-L19 | py | def fib4(n: int) -> int:
"""The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
fib4(0) -> 0
fib4(1) -> 0
fib4(2) -> 2
fib4(3) -> 0
fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4).
Please write a 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
"""
if n == 0:
return 0
elif n == 1: | elif n == 2:
return 2
elif n == 3:
return 0
else:
return fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4)
| return 0 | def check(candidate):
assert candidate(5) == 4
assert candidate(8) == 28
assert candidate(10) == 104
assert candidate(12) == 386
def test_check():
check(fib4)
test_check()
|
humaneval-HumanEval_26_remove_duplicates.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 {
// From an array array list of integers, remove all elements that occur more than once.
// Keep order of elements left the same as in the input.
// >>> removeDuplicates((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)2l, (long)4l))))
// (new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)4l)))
public static ArrayList<Long> removeDuplicates(ArrayList<Long> numbers) {
HashMap<Long, Long> hashmap = new HashMap<Long, Long>();
for (int i = 0; i < numbers.size(); i++) {
Long key = numbers.get(i);
if (hashmap.containsKey(key)) {
hashmap.put(key, hashmap.get(key) + 1);
} else {
hashmap.put(key, 1L); | }
ArrayList<Long> out = new ArrayList<Long>();
for (int i = 0; i < numbers.size(); i++) {
Long key = numbers.get(i);
if (hashmap.get(key) == 1) {
out.add(key);
}
}
return out;
}
} | } | }
public static void main(String[] args) {
assert(removeDuplicates((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<Long>(Arrays.asList()))));
assert(removeDuplicates((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l)))));
assert(removeDuplicates((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)2l, (long)4l, (long)3l, (long)5l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)5l)))));
}
}
|
humaneval-HumanEval_43_pairs_sum_to_zero.json-L19 | py | from typing import List
def pairs_sum_to_zero(l: List[int]) -> bool:
"""
pairs_sum_to_zero takes a list of integers as an input.
it returns True if there are two distinct elements in the list that
sum to zero, and False otherwise.
>>> pairs_sum_to_zero([1, 3, 5, 0])
False
>>> pairs_sum_to_zero([1, 3, -2, 1])
False
>>> pairs_sum_to_zero([1, 2, 3, 7])
False
>>> pairs_sum_to_zero([2, 4, -5, 3, 5, 7])
True
>>> pairs_sum_to_zero([1])
False
"""
| for y in l:
if x+y == 0 and x != y:
return True
return False
| for x in l: | def check(candidate):
assert candidate([1, 3, 5, 0]) == False
assert candidate([1, 3, -2, 1]) == False
assert candidate([1, 2, 3, 7]) == False
assert candidate([2, 4, -5, 3, 5, 7]) == True
assert candidate([1]) == False
assert candidate([-3, 9, -1, 3, 2, 30]) == True
assert candidate([-3, 9, -1, 3, 2, 31]) == True
assert candidate([-3, 9, -1, 4, 2, 30]) == False
assert candidate([-3, 9, -1, 4, 2, 31]) == False
def test_check():
check(pairs_sum_to_zero)
test_check()
|
humaneval-HumanEval_154_cycpattern_check.json-L19 | py | def cycpattern_check(a: str, b: str) -> bool:
"""You are given 2 words. You need to return True if the second word or any of its rotations is a substring in the first word
>>> cycpattern_check('abcd', 'abd')
False
>>> cycpattern_check('hello', 'ell')
True
>>> cycpattern_check('whassup', 'psus')
False
>>> cycpattern_check('abab', 'baa')
True
>>> cycpattern_check('efef', 'eeff')
False
>>> cycpattern_check('himenss', 'simen')
True
"""
for i in range(len(b)):
if a.find(b) != -1: | b = b[1:] + b[0]
return False
| return True | def check(candidate):
assert candidate('xyzw', 'xyw') == False
assert candidate('yello', 'ell') == True
assert candidate('whattup', 'ptut') == False
assert candidate('efef', 'fee') == True
assert candidate('abab', 'aabb') == False
assert candidate('winemtt', 'tinem') == True
def test_check():
check(cycpattern_check)
test_check()
|
humaneval-HumanEval_83_starts_one_ends.json-L13 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given a positive integer n, return the count of the numbers of n-digit
// positive integers that start or end with 1.
public static long startsOneEnds(long n) {
long lower = (long) Math.pow(10, n - 1); | long count = 0;
for (long i = lower; i <= upper; i++) {
String s = "" + i;
if (s.startsWith("1") || s.endsWith("1"))
count++;
}
return count;
}
} | long upper = (long) Math.pow(10, n) - 1; | }
public static void main(String[] args) {
assert(startsOneEnds((1l)) == (1l));
assert(startsOneEnds((2l)) == (18l));
assert(startsOneEnds((3l)) == (180l));
assert(startsOneEnds((4l)) == (1800l));
assert(startsOneEnds((5l)) == (18000l));
}
}
|
humaneval-HumanEval_48_is_palindrome.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 {
// Checks if given string is a palindrome
// >>> isPalindrome((""))
// (true)
// >>> isPalindrome(("aba"))
// (true)
// >>> isPalindrome(("aaaaa"))
// (true)
// >>> isPalindrome(("zbcd"))
// (false)
public static boolean isPalindrome(String text) {
boolean isPalindrome = true;
int left = 0;
int right = text.length() - 1;
while (left < right) {
if (text.charAt(left) != text.charAt(right)) {
isPalindrome = false;
break;
} | right--;
}
return isPalindrome;
}
} | left++; | }
public static void main(String[] args) {
assert(isPalindrome(("")) == (true));
assert(isPalindrome(("aba")) == (true));
assert(isPalindrome(("aaaaa")) == (true));
assert(isPalindrome(("zbcd")) == (false));
assert(isPalindrome(("xywyx")) == (true));
assert(isPalindrome(("xywyz")) == (false));
assert(isPalindrome(("xywzx")) == (false));
}
}
|
humaneval-HumanEval_149_sorted_list_sum.json-L40 | js | //Write a function that accepts an array of strings as a parameter,
// deletes the strings that have odd lengths from it,
// and returns the resulted array with a sorted order,
// The array is always an array of strings and never an array of numbers,
// and it may contain duplicates.
// The order of the array should be ascending by length of each word, and you
// should return the array sorted by that rule.
// If two words have the same length, sort the array alphabetically.
// The function should return an array of strings in sorted order.
// You may assume that all words will have the same length.
// For example:
// >>> list_sort(["aa", "a", "aaa"])
// ["aa"]
// >>> list_sort(["ab", "a", "aaa", "cd"])
// ["ab", "cd"]
function sorted_list_sum(lst){
var result = [];
for(var i = 0; i < lst.length; i++){
if(lst[i].length % 2 == 0){
result.push(lst[i]);
}
}
return result.sort(function(a, b){
if(a.length < b.length){
return -1;
}
else if(a.length > b.length){
return 1;
}
else{
if(a < b){
return -1;
}
else if(a > b){
return 1;
}
else{
return 0;
} | });
}
var a = ["aa", "a", "aaa"];
var b = ["ab", "a", "aaa", "cd"]; | } | const assert = require('node:assert');
function test() {
let candidate = sorted_list_sum;
assert.deepEqual(candidate(["aa", "a", "aaa"]),["aa"]);
assert.deepEqual(candidate(["school", "AI", "asdf", "b"]),["AI", "asdf", "school"]);
assert.deepEqual(candidate(["d", "b", "c", "a"]),[]);
assert.deepEqual(candidate(["d", "dcba", "abcd", "a"]),["abcd", "dcba"]);
assert.deepEqual(candidate(["AI", "ai", "au"]),["AI", "ai", "au"]);
assert.deepEqual(candidate(["a", "b", "b", "c", "c", "a"]),[]);
assert.deepEqual(candidate(["aaaa", "bbbb", "dd", "cc"]),["cc", "dd", "aaaa", "bbbb"]);
}
test(); |
humaneval-HumanEval_34_unique.json-L14 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Return sorted unique elements in an array array list
// >>> unique((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)0l, (long)2l, (long)3l, (long)5l, (long)9l, (long)123l)))
public static ArrayList<Long> unique(ArrayList<Long> l) {
TreeSet<Long> tree = new TreeSet<Long>(l); | }
} | return new ArrayList<Long>(tree); | }
public static void main(String[] args) {
assert(unique((new ArrayList<Long>(Arrays.asList((long)5l, (long)3l, (long)5l, (long)2l, (long)3l, (long)3l, (long)9l, (long)0l, (long)123l)))).equals((new ArrayList<Long>(Arrays.asList((long)0l, (long)2l, (long)3l, (long)5l, (long)9l, (long)123l)))));
}
}
|
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.