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_63_fibfib.json-L15 | js | //The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
// fibfib(0) == 0
// fibfib(1) == 0
// fibfib(2) == 1
// fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3).
// Please write a function to efficiently compute the n-th element of the fibfib number sequence.
// >>> fibfib(1)
// 0
// >>> fibfib(5)
// 4
// >>> fibfib(8)
// 24
function fibfib(n){
if(n <= 0){ | }
if(n == 1){
return 0;
}
if(n == 2){
return 1;
}
return fibfib(n-1) + fibfib(n-2) + fibfib(n-3);
}
| return 0; | const assert = require('node:assert');
function test() {
let candidate = fibfib;
assert.deepEqual(candidate(2),1);
assert.deepEqual(candidate(1),0);
assert.deepEqual(candidate(5),4);
assert.deepEqual(candidate(8),24);
assert.deepEqual(candidate(10),81);
assert.deepEqual(candidate(12),274);
assert.deepEqual(candidate(14),927);
}
test(); |
humaneval-HumanEval_121_solution.json-L19 | 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){
if (lst[i] % 2 === 1){
result += lst[i];
}
}
}
return result; | } | 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_91_is_bored.json-L12 | js | //You'll be given a string of words, and your task is to count the number
// of boredoms. A boredom is a sentence that starts with the word "I".
// Sentences are delimited by '.', '?' or '!'.
// For example:
// >>> is_bored("Hello world")
// 0
// >>> is_bored("The sky is blue. The sun is shining. I love this weather")
// 1
function is_bored(S){
let lines = S.split(/[.?!]/);
let boredoms = 0; | if(line.match(/^\s*I /)){
boredoms++;
}
}
return boredoms;
}
| for (let line of lines){ | const assert = require('node:assert');
function test() {
let candidate = is_bored;
assert.deepEqual(candidate("Hello world"),0);
assert.deepEqual(candidate("Is the sky blue?"),0);
assert.deepEqual(candidate("I love It !"),1);
assert.deepEqual(candidate("bIt"),0);
assert.deepEqual(candidate("I feel good today. I will be productive. will kill It"),2);
assert.deepEqual(candidate("You and I are going for a walk"),0);
}
test(); |
humaneval-HumanEval_110_exchange.json-L26 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// 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++) { | lst1.set(i, lst2.get(j));
lst2.set(j, (long) 1);
break;
}
}
}
}
return (lst1.stream().allMatch(x -> x % 2 == 0)) ? "YES" : "NO";
}
} | if (lst2.get(j) % 2 == 0) { | }
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_34_unique.json-L8 | js | //Return sorted unique elements in an array
// >>> unique([5, 3, 5, 2, 3, 3, 9, 0, 123])
// [0, 2, 3, 5, 9, 123]
function unique(l){
return l.filter(function(e, i, a){
return a.indexOf(e) === i;
}).sort(function(a, b){return a-b}); | } | const assert = require('node:assert');
function test() {
let candidate = unique;
assert.deepEqual(candidate([5, 3, 5, 2, 3, 3, 9, 0, 123]),[0, 2, 3, 5, 9, 123]);
}
test(); |
|
humaneval-HumanEval_129_minPath.json-L59 | js | //Given a grid with N rows and N columns (N >= 2) and a positive integer k,
// each cell of the grid contains a value. Every integer in the range [1, N * N]
// inclusive appears exactly once on the cells of the grid.
// You have to find the minimum path of length k in the grid. You can start
// from any cell, and in each step you can move to any of the neighbor cells,
// in other words, you can go to cells which share an edge with you current
// cell.
// Please note that a path of length k means visiting exactly k cells (not
// necessarily distinct).
// You CANNOT go off the grid.
// A path A (of length k) is considered less than a path B (of length k) if
// after making the ordered arrays of the values on the cells that A and B go
// through (let's call them lst_A and lst_B), lst_A is lexicographically less
// than lst_B, in other words, there exist an integer index i (1 <= i <= k)
// such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have
// lst_A[j] = lst_B[j].
// It is guaranteed that the answer is unique.
// Return an ordered array of the values on the cells that the minimum path go through.
// Examples:
// >>> minPath([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3)
// [1, 2, 1]
// >>> minPath([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1)
// [1]
function minPath(grid, k){
let n = grid.length;
let path = [];
let startCell = grid[0][0];
for(let i = 0; i < n; i++){
for(let j = 0; j < n; j++){
let curr = grid[i][j];
if(curr < startCell){
startCell = curr;
}
}
}
let currCell = startCell;
for(let l = 0; l < k; l++){
path.push(currCell);
let neighbours = [];
let x = null;
let y = null;
for(let i = 0; i < n; i++){
for(let j = 0; j < n; j++){
if(grid[i][j] === currCell){
x = i;
y = j;
}
}
}
if(x > 0){
neighbours.push(grid[x - 1][y]);
}
if(x < n - 1){
neighbours.push(grid[x + 1][y]);
}
if(y > 0){
neighbours.push(grid[x][y - 1]);
} | neighbours.push(grid[x][y + 1]);
}
let nextCell = neighbours[0];
for(let i = 0; i < neighbours.length; i++){
let curr = neighbours[i];
if(curr < nextCell){
nextCell = curr;
}
}
currCell = nextCell;
}
return path;
} | if(y < n - 1){ | const assert = require('node:assert');
function test() {
let candidate = minPath;
assert.deepEqual(candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3),[1, 2, 1]);
assert.deepEqual(candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1),[1]);
assert.deepEqual(candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4),[1, 2, 1, 2]);
assert.deepEqual(candidate([[6, 4, 13, 10], [5, 7, 12, 1], [3, 16, 11, 15], [8, 14, 9, 2]], 7),[1, 10, 1, 10, 1, 10, 1]);
assert.deepEqual(candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5),[1, 7, 1, 7, 1]);
assert.deepEqual(candidate([[11, 8, 7, 2], [5, 16, 14, 4], [9, 3, 15, 6], [12, 13, 10, 1]], 9),[1, 6, 1, 6, 1, 6, 1, 6, 1]);
assert.deepEqual(candidate([[12, 13, 10, 1], [9, 3, 15, 6], [5, 16, 14, 4], [11, 8, 7, 2]], 12),[1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6]);
assert.deepEqual(candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8),[1, 3, 1, 3, 1, 3, 1, 3]);
assert.deepEqual(candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8),[1, 5, 1, 5, 1, 5, 1, 5]);
assert.deepEqual(candidate([[1, 2], [3, 4]], 10),[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]);
assert.deepEqual(candidate([[1, 3], [3, 2]], 10),[1, 3, 1, 3, 1, 3, 1, 3, 1, 3]);
}
test(); |
humaneval-HumanEval_115_max_fill.json-L39 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given a rectangular grid of wells. Each row represents a single well,
// and each 1 in a row represents a single unit of water.
// Each well has a corresponding bucket that can be used to extract water from it,
// and all buckets have the same capacity.
// Your task is to use the buckets to empty the wells.
// Output the number of times you need to lower the buckets.
// Example 1:
// >>> maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (1l))
// (6l)
// Example 2:
// >>> maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)1l, (long)1l))))), (2l))
// (5l)
// Example 3:
// >>> maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l))))), (5l))
// (0l)
// Constraints:
// * all wells have the same length
// * 1 <= grid.length <= 10^2
// * 1 <= grid[:,1].length <= 10^2
// * grid[i][j] -> 0 | 1
// * 1 <= capacity <= 10
public static long maxFill(ArrayList<ArrayList<Long>> grid, long capacity) {
long count = 0l;
for (ArrayList<Long> well : grid) {
long level = 0l;
for (Long unit : well) {
if (unit == 1l) {
level += 1l;
}
} | if (level % capacity != 0l) {
units += 1l;
}
count += units;
}
return count;
}
} | long units = level / capacity; | }
public static void main(String[] args) {
assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (1l)) == (6l));
assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)1l, (long)1l))))), (2l)) == (5l));
assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l))))), (5l)) == (0l));
assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (2l)) == (4l));
assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (9l)) == (2l));
}
}
|
humaneval-HumanEval_95_check_dict_case.json-L30 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given a hash map, return true if all keys are strings in lower
// case or all keys are strings in upper case, else return false.
// The function should return false is the given hash map is empty.
// Examples:
// >>> checkDictCase((new HashMap<String,String>(Map.of("a", "apple", "b", "banana"))))
// (true)
// >>> checkDictCase((new HashMap<String,String>(Map.of("a", "apple", "A", "banana", "B", "banana"))))
// (false)
// >>> checkDictCase((new HashMap<String,String>(Map.of("a", "apple", 8l, "banana", "a", "apple"))))
// (false)
// >>> checkDictCase((new HashMap<String,String>(Map.of("Name", "John", "Age", "36", "City", "Houston"))))
// (false)
// >>> checkDictCase((new HashMap<String,String>(Map.of("STATE", "NC", "ZIP", "12345"))))
// (true)
public static boolean checkDictCase(HashMap<String,String> dict) {
if (dict.isEmpty()) {
return false;
}
boolean allLowerCase = dict.keySet().stream().allMatch(k -> k.matches("^[a-z]+$"));
boolean allUpperCase = dict.keySet().stream().allMatch(k -> k.matches("^[A-Z]+$"));
return allLowerCase || allUpperCase; | } | } | }
public static void main(String[] args) {
assert(checkDictCase((new HashMap<String,String>(Map.of("p", "pineapple", "b", "banana")))) == (true));
assert(checkDictCase((new HashMap<String,String>(Map.of("p", "pineapple", "A", "banana", "B", "banana")))) == (false));
assert(checkDictCase((new HashMap<String,String>(Map.of("p", "pineapple", "5", "banana", "a", "apple")))) == (false));
assert(checkDictCase((new HashMap<String,String>(Map.of("Name", "John", "Age", "36", "City", "Houston")))) == (false));
assert(checkDictCase((new HashMap<String,String>(Map.of("STATE", "NC", "ZIP", "12345")))) == (true));
assert(checkDictCase((new HashMap<String,String>(Map.of("fruit", "Orange", "taste", "Sweet")))) == (true));
assert(checkDictCase((new HashMap<String,String>())) == (false));
}
}
|
humaneval-HumanEval_25_factorize.json-L16 | js | //Return array of prime factors of given integer in the order from smallest to largest.
// Each of the factors should be arrayed number of times corresponding to how many times it appeares in factorization.
// Input number should be equal to the product of all factors
// >>> factorize(8)
// [2, 2, 2]
// >>> factorize(25)
// [5, 5]
// >>> factorize(70)
// [2, 5, 7]
function factorize(n){
let factor = 2;
let arr = [];
while (n > 1) {
if (n % factor == 0) {
arr.push(factor); | } else {
factor++;
}
}
return arr;
}
| n = n / factor; | const assert = require('node:assert');
function test() {
let candidate = factorize;
assert.deepEqual(candidate(2),[2]);
assert.deepEqual(candidate(4),[2, 2]);
assert.deepEqual(candidate(8),[2, 2, 2]);
assert.deepEqual(candidate(57),[3, 19]);
assert.deepEqual(candidate(3249),[3, 3, 19, 19]);
assert.deepEqual(candidate(185193),[3, 3, 3, 19, 19, 19]);
assert.deepEqual(candidate(20577),[3, 19, 19, 19]);
assert.deepEqual(candidate(18),[2, 3, 3]);
}
test(); |
humaneval-HumanEval_110_exchange.json-L27 | 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" | return "YES" | else: | 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_54_same_chars.json-L16 | js | //Check if two words have the same characters.
// >>> same_chars("eabcdzzzz", "dddzzzzzzzddeddabc")
// true
// >>> same_chars("abcd", "dddddddabc")
// true
// >>> same_chars("dddddddabc", "abcd")
// true
// >>> same_chars("eabcd", "dddddddabc")
// false
// >>> same_chars("abcd", "dddddddabce")
// false
// >>> same_chars("eabcdzzzz", "dddzzzzzzzddddabc")
// false
function same_chars(s0, s1){
let res = true; | if(s1.indexOf(s0[i]) === -1){
res = false;
break;
}
}
for(let i = 0; i < s1.length; i++){
if(s0.indexOf(s1[i]) === -1){
res = false;
break;
}
}
return res;
}
| for(let i = 0; i < s0.length; i++){ | const assert = require('node:assert');
function test() {
let candidate = same_chars;
assert.deepEqual(candidate("eabcdzzzz", "dddzzzzzzzddeddabc"),true);
assert.deepEqual(candidate("abcd", "dddddddabc"),true);
assert.deepEqual(candidate("dddddddabc", "abcd"),true);
assert.deepEqual(candidate("eabcd", "dddddddabc"),false);
assert.deepEqual(candidate("abcd", "dddddddabcf"),false);
assert.deepEqual(candidate("eabcdzzzz", "dddzzzzzzzddddabc"),false);
assert.deepEqual(candidate("aabb", "aaccc"),false);
}
test(); |
humaneval-HumanEval_36_fizz_buzz.json-L29 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// 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_59_largest_prime_factor.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 the largest prime factor of n. Assume n > 1 and is not a prime.
// >>> largestPrimeFactor((13195l))
// (29l)
// >>> largestPrimeFactor((2048l))
// (2l)
public static long largestPrimeFactor(long n) {
List<Long> primes = new ArrayList<>();
long x = n;
for (long i = 2; i * i <= x; i++) {
if (x % i == 0) { | x /= i;
i--;
}
}
if (x > 1) {
primes.add(x);
}
return primes.get(primes.size() - 1);
}
} | primes.add(i); | }
public static void main(String[] args) {
assert(largestPrimeFactor((15l)) == (5l));
assert(largestPrimeFactor((27l)) == (3l));
assert(largestPrimeFactor((63l)) == (7l));
assert(largestPrimeFactor((330l)) == (11l));
assert(largestPrimeFactor((13195l)) == (29l));
}
}
|
humaneval-HumanEval_95_check_dict_case.json-L31 | js | //Given an object, return true if all keys are strings in lower
// case or all keys are strings in upper case, else return false.
// The function should return false is the given object is empty.
// Examples:
// >>> check_dict_case({"a": "apple", "b": "banana"})
// true
// >>> check_dict_case({"a": "apple", "A": "banana", "B": "banana"})
// false
// >>> check_dict_case({"a": "apple", 8: "banana", "a": "apple"})
// false
// >>> check_dict_case({"Name": "John", "Age": "36", "City": "Houston"})
// false
// >>> check_dict_case({"STATE": "NC", "ZIP": "12345"})
// true
function check_dict_case(dict){
if (Object.keys(dict).length == 0){
return false;
}
var case_type = -1;
for (let key in dict){
if (typeof key == "string"){
if (case_type == -1){
if (key == key.toUpperCase()){
case_type = 1;
}
else if (key == key.toLowerCase()){
case_type = 0;
}
else{
return false; | }
else if (case_type == 1){
if (key != key.toUpperCase()){
return false;
}
}
else if (case_type == 0){
if (key != key.toLowerCase()){
return false;
}
}
}
else{
return false;
}
}
return true;
} | } | const assert = require('node:assert');
function test() {
let candidate = check_dict_case;
assert.deepEqual(candidate({"p": "pineapple", "b": "banana"}),true);
assert.deepEqual(candidate({"p": "pineapple", "A": "banana", "B": "banana"}),false);
assert.deepEqual(candidate({"p": "pineapple", "5": "banana", "a": "apple"}),false);
assert.deepEqual(candidate({"Name": "John", "Age": "36", "City": "Houston"}),false);
assert.deepEqual(candidate({"STATE": "NC", "ZIP": "12345"}),true);
assert.deepEqual(candidate({"fruit": "Orange", "taste": "Sweet"}),true);
assert.deepEqual(candidate({}),false);
}
test(); |
humaneval-HumanEval_104_unique_digits.json-L10 | 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){
| for(let i = 0; i < x.length; i++){
let num = x[i].toString()
if(num.indexOf('2') === -1 && num.indexOf('4') === -1 && num.indexOf('6') === -1 && num.indexOf('8') === -1 && num.indexOf('0') === -1){
result.push(x[i])
}
}
return result.sort(function(a, b){return a-b})
}
| let result = [] | 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_11_string_xor.json-L10 | 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 = ''; | 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;
} | for (var i = 0; i < a.length; i++) { | 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_6_parse_nested_parens.json-L29 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
// For each of the group, output the deepest level of nesting of parentheses.
// E.g. (()()) has maximum two levels of nesting while ((())) has three.
// >>> parseNestedParens(("(()()) ((())) () ((())()())"))
// (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)1l, (long)3l)))
public static ArrayList<Long> parseNestedParens(String paren_string) {
ArrayList<Long> result = new ArrayList<Long>();
String[] paren_strings = paren_string.split(" ");
for (String paren : paren_strings) {
long max_depth = 0;
long current_depth = 0;
for (char c : paren.toCharArray()) {
if (c == '(') {
current_depth += 1;
max_depth = Math.max(max_depth, current_depth);
} else if (c == ')') {
current_depth -= 1;
}
}
result.add(max_depth); | return result;
}
} | } | }
public static void main(String[] args) {
assert(parseNestedParens(("(()()) ((())) () ((())()())")).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)1l, (long)3l)))));
assert(parseNestedParens(("() (()) ((())) (((())))")).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l)))));
assert(parseNestedParens(("(()(())((())))")).equals((new ArrayList<Long>(Arrays.asList((long)4l)))));
}
}
|
humaneval-HumanEval_114_minSubArraySum.json-L37 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given an array array list of integers nums, find the minimum sum of any non-empty sub-array array list
// of nums.
// Example
// >>> minSubArraySum((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)1l, (long)2l, (long)4l))))
// (1l)
// >>> minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l))))
// (-6l)
public static long minSubArraySum(ArrayList<Long> nums) {
long minSum = Long.MAX_VALUE;
long prevMinSum = Long.MAX_VALUE;
for (int i = 0; i < nums.size(); i++) {
long currSum = nums.get(i);
if (currSum < prevMinSum) {
prevMinSum = currSum;
}
if (currSum < minSum) {
minSum = currSum;
}
for (int j = i + 1; j < nums.size(); j++) {
currSum += nums.get(j);
if (currSum < prevMinSum) {
prevMinSum = currSum;
}
if (currSum < minSum) {
minSum = currSum;
}
}
} | }
} | return minSum; | }
public static void main(String[] args) {
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)1l, (long)2l, (long)4l)))) == (1l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l)))) == (-6l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l, (long)2l, (long)-10l)))) == (-14l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-9999999999999999l)))) == (-9999999999999999l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)0l, (long)10l, (long)20l, (long)1000000l)))) == (0l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l, (long)10l, (long)-5l)))) == (-6l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)100l, (long)-1l, (long)-2l, (long)-3l, (long)10l, (long)-5l)))) == (-6l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)10l, (long)11l, (long)13l, (long)8l, (long)3l, (long)4l)))) == (3l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)100l, (long)-33l, (long)32l, (long)-1l, (long)0l, (long)-2l)))) == (-33l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-10l)))) == (-10l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)7l)))) == (7l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)1l, (long)-1l)))) == (-1l));
}
}
|
humaneval-HumanEval_152_compare.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 {
// I think we all remember that feeling when the result of some long-awaited
// event is finally known. The feelings and thoughts you have at that moment are
// definitely worth noting down and comparing.
// Your task is to determine if a person correctly guessed the results of a number of matches.
// You are given two array array lists of scores and guesses of equal length, where each index shows a match.
// Return an array array list of the same length denoting how far off each guess was. If they have guessed correctly,
// the value is 0, and if not, the value is the absolute difference between the guess and the score.
// example:
// >>> compare((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l, (long)1l))), (new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)2l, (long)-2l))))
// (new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l, (long)0l, (long)3l, (long)3l)))
// >>> compare((new ArrayList<Long>(Arrays.asList((long)0l, (long)5l, (long)0l, (long)0l, (long)0l, (long)4l))), (new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)1l, (long)0l, (long)0l, (long)-2l))))
// (new ArrayList<Long>(Arrays.asList((long)4l, (long)4l, (long)1l, (long)0l, (long)0l, (long)6l)))
public static ArrayList<Long> compare(ArrayList<Long> game, ArrayList<Long> guess) {
ArrayList<Long> result = new ArrayList<Long>();
for (int i = 0; i < game.size(); i++) {
if (game.get(i) != guess.get(i)) {
result.add(Math.abs(game.get(i) - guess.get(i)));
} else { | }
}
return result;
}
} | result.add((long)0); | }
public static void main(String[] args) {
assert(compare((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l, (long)1l))), (new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)2l, (long)-2l)))).equals((new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l, (long)0l, (long)3l, (long)3l)))));
assert(compare((new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l))), (new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l)))).equals((new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l)))));
assert(compare((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))), (new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)6l)))));
assert(compare((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)5l))), (new ArrayList<Long>(Arrays.asList((long)-1l, (long)2l, (long)3l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)0l, (long)0l, (long)1l)))));
}
}
|
humaneval-HumanEval_105_by_length.json-L36 | js | //Given an array of integers, sort the integers that are between 1 and 9 inclusive,
// reverse the resulting array, and then replace each digit by its corresponding name from
// "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine".
// For example:
// >>> by_length([2, 1, 1, 4, 5, 8, 2, 3])
// ["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"]
// If the array is empty, return an empty array:
// >>> by_length([])
// []
// If the array has any strange number ignore it:
// >>> by_length([1, -1, 55])
// ["One"]
function by_length(arr){
var arr2 = [];
for(var i = 0; i < arr.length; i++){
var num = arr[i];
if(num > 0 && num < 10){
arr2.push(num);
}
}
arr2.sort();
arr2.reverse();
var arr3 = [];
for(var i = 0; i < arr2.length; i++){
var num = arr2[i];
if(num == 1){
arr3.push("One");
}
else if(num == 2){
arr3.push("Two");
}
else if(num == 3){
arr3.push("Three");
}
else if(num == 4){ | }
else if(num == 5){
arr3.push("Five");
}
else if(num == 6){
arr3.push("Six");
}
else if(num == 7){
arr3.push("Seven");
}
else if(num == 8){
arr3.push("Eight");
}
else if(num == 9){
arr3.push("Nine");
}
}
return arr3;
} | arr3.push("Four"); | const assert = require('node:assert');
function test() {
let candidate = by_length;
assert.deepEqual(candidate([2, 1, 1, 4, 5, 8, 2, 3]),["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"]);
assert.deepEqual(candidate([]),[]);
assert.deepEqual(candidate([1, -1, 55]),["One"]);
assert.deepEqual(candidate([1, -1, 3, 2]),["Three", "Two", "One"]);
assert.deepEqual(candidate([9, 4, 8]),["Nine", "Eight", "Four"]);
}
test(); |
humaneval-HumanEval_109_move_one_ball.json-L36 | py | from typing import List
def move_one_ball(arr: List[int]) -> bool:
"""We have an array 'arr' of N integers arr[1], arr[2], ..., arr[N].The
numbers in the array will be randomly ordered. Your task is to determine if
it is possible to get an array sorted in non-decreasing order by performing
the following operation on the given array:
You are allowed to perform right shift operation any number of times.
One right shift operation means shifting all elements of the array by one
position in the right direction. The last element of the array will be moved to
the starting position in the array i.e. 0th index.
If it is possible to obtain the sorted array by performing the above operation
then return True else return False.
If the given array is empty then return True.
Note: The given list is guaranteed to have unique elements.
For Example:
>>> move_one_ball([3, 4, 5, 1, 2])
True
Explanation: By performin 2 right shift operations, non-decreasing order can
be achieved for the given array.
>>> move_one_ball([3, 5, 4, 1, 2])
False
Explanation:It is not possible to get non-decreasing order for the given
array by performing any number of right shift operations.
"""
flag = False
for i in range(1, len(arr)):
if arr[i-1] > arr[i]:
if flag == True: | flag = True
elif arr[i-1] < arr[i]:
flag = False
return True
| return False | def check(candidate):
assert candidate([3, 4, 5, 1, 2]) == True
assert candidate([3, 5, 10, 1, 2]) == True
assert candidate([4, 3, 1, 2]) == False
assert candidate([3, 5, 4, 1, 2]) == False
assert candidate([]) == True
def test_check():
check(move_one_ball)
test_check()
|
humaneval-HumanEval_48_is_palindrome.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 {
// 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) { | isPalindrome = false;
break;
}
left++;
right--;
}
return isPalindrome;
}
} | if (text.charAt(left) != text.charAt(right)) { | }
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_126_is_sorted.json-L38 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given an array array list of numbers, return whether or not they are sorted
// in ascending order. If array list has more than 1 duplicate of the same
// number, return false. Assume no negative numbers and only integers.
// Examples
// >>> isSorted((new ArrayList<Long>(Arrays.asList((long)5l))))
// (true)
// >>> isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l))))
// (true)
// >>> isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)2l, (long)4l, (long)5l))))
// (false)
// >>> isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l, (long)6l))))
// (true)
// >>> isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l, (long)6l, (long)7l))))
// (true)
// >>> isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)2l, (long)4l, (long)5l, (long)6l, (long)7l))))
// (false)
// >>> isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)2l, (long)3l, (long)3l, (long)4l))))
// (true)
// >>> isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)2l, (long)2l, (long)3l, (long)4l))))
// (false)
public static boolean isSorted(ArrayList<Long> lst) {
if (lst.size() == 0) {
return true;
}
else {
int running_max = 0;
int previous_number = 0;
boolean first_time = true;
boolean seen_2_in_a_row = false; | if (first_time) {
first_time = false;
previous_number = number.intValue();
running_max = number.intValue();
}
else {
if (running_max > number.intValue()) {
return false;
}
else {
if (previous_number == number.intValue()) {
if (seen_2_in_a_row) {
return false;
}
else {
seen_2_in_a_row = true;
}
}
else {
seen_2_in_a_row = false;
}
previous_number = number.intValue();
running_max = number.intValue();
}
}
}
return true;
}
}
} | for (Long number : lst) { | }
public static void main(String[] args) {
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)5l)))) == (true));
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l)))) == (true));
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)2l, (long)4l, (long)5l)))) == (false));
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l, (long)6l)))) == (true));
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l, (long)6l, (long)7l)))) == (true));
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)2l, (long)4l, (long)5l, (long)6l, (long)7l)))) == (false));
assert(isSorted((new ArrayList<Long>(Arrays.asList()))) == (true));
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)1l)))) == (true));
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)1l)))) == (false));
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)2l, (long)2l, (long)3l, (long)4l)))) == (false));
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)3l, (long)3l, (long)4l)))) == (false));
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)2l, (long)3l, (long)3l, (long)4l)))) == (true));
assert(isSorted((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l)))) == (true));
}
}
|
humaneval-HumanEval_19_sort_numbers.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 {
// Input is a space-delimited string of numberals from 'zero' to 'nine'.
// Valid choices are 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight' and 'nine'.
// Return the string with numbers sorted from smallest to largest
// >>> sortNumbers(("three one five"))
// ("one three five")
public static String sortNumbers(String numbers) {
String [] nums = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
HashMap<String, Integer> indexMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
indexMap.put(nums[i], i);
}
String [] split = numbers.split(" "); | @Override
public int compare(String s1, String s2) {
return indexMap.get(s1) - indexMap.get(s2);
}
});
return String.join(" ", split);
}
} | Arrays.sort(split, new Comparator<String>() { | }
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_16_count_distinct_characters.json-L7 | js | //Given a string, find out how many distinct characters (regardless of case) does it consist of
// >>> count_distinct_characters("xyzXYZ")
// 3
// >>> count_distinct_characters("Jerry")
// 4
function count_distinct_characters(string){
| var result = [];
for(var i = 0; i < arr.length; i++){
if(result.indexOf(arr[i].toLowerCase()) < 0){
result.push(arr[i].toLowerCase());
}
}
return result.length;
}
| var arr = string.split(""); | const assert = require('node:assert');
function test() {
let candidate = count_distinct_characters;
assert.deepEqual(candidate(""),0);
assert.deepEqual(candidate("abcde"),5);
assert.deepEqual(candidate("abcdecadeCADE"),5);
assert.deepEqual(candidate("aaaaAAAAaaaa"),1);
assert.deepEqual(candidate("Jerry jERRY JeRRRY"),5);
}
test(); |
humaneval-HumanEval_94_skjkasdkd.json-L54 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given an array array list of integers.
// You need to find the largest prime value and return the sum of its digits.
// Examples:
// >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)3l, (long)2l, (long)1l, (long)3l, (long)5l, (long)7l, (long)4l, (long)5l, (long)5l, (long)5l, (long)2l, (long)181l, (long)32l, (long)4l, (long)32l, (long)3l, (long)2l, (long)32l, (long)324l, (long)4l, (long)3l))))
// (10l)
// >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)1l, (long)8l, (long)2l, (long)4597l, (long)2l, (long)1l, (long)3l, (long)40l, (long)1l, (long)2l, (long)1l, (long)2l, (long)4l, (long)2l, (long)5l, (long)1l))))
// (25l)
// >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)1l, (long)32l, (long)5107l, (long)34l, (long)83278l, (long)109l, (long)163l, (long)23l, (long)2323l, (long)32l, (long)30l, (long)1l, (long)9l, (long)3l))))
// (13l)
// >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)724l, (long)32l, (long)71l, (long)99l, (long)32l, (long)6l, (long)0l, (long)5l, (long)91l, (long)83l, (long)0l, (long)5l, (long)6l))))
// (11l)
// >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)81l, (long)12l, (long)3l, (long)1l, (long)21l))))
// (3l)
// >>> skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)8l, (long)1l, (long)2l, (long)1l, (long)7l))))
// (7l)
public static long skjkasdkd(ArrayList<Long> lst) {
long largestPrime = 0l;
for (Long item : lst) {
long number = item;
int counter = 0;
if (item == 1) {
continue;
}
if (item == 2) {
counter = 1;
}
else {
int l = 1;
while (l <= (int) number) {
if (number % l == 0) {
counter++;
}
l++;
}
}
if (counter == 2) {
if (number > largestPrime) {
largestPrime = number;
}
}
}
if (largestPrime == 0) {
return 0l;
}
else { | while (largestPrime > 0) {
output += largestPrime % 10;
largestPrime /= 10;
}
return output;
}
}
} | long output = 0l; | }
public static void main(String[] args) {
assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)3l, (long)2l, (long)1l, (long)3l, (long)5l, (long)7l, (long)4l, (long)5l, (long)5l, (long)5l, (long)2l, (long)181l, (long)32l, (long)4l, (long)32l, (long)3l, (long)2l, (long)32l, (long)324l, (long)4l, (long)3l)))) == (10l));
assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)1l, (long)8l, (long)2l, (long)4597l, (long)2l, (long)1l, (long)3l, (long)40l, (long)1l, (long)2l, (long)1l, (long)2l, (long)4l, (long)2l, (long)5l, (long)1l)))) == (25l));
assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)1l, (long)32l, (long)5107l, (long)34l, (long)83278l, (long)109l, (long)163l, (long)23l, (long)2323l, (long)32l, (long)30l, (long)1l, (long)9l, (long)3l)))) == (13l));
assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)724l, (long)32l, (long)71l, (long)99l, (long)32l, (long)6l, (long)0l, (long)5l, (long)91l, (long)83l, (long)0l, (long)5l, (long)6l)))) == (11l));
assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)81l, (long)12l, (long)3l, (long)1l, (long)21l)))) == (3l));
assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)0l, (long)8l, (long)1l, (long)2l, (long)1l, (long)7l)))) == (7l));
assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)8191l)))) == (19l));
assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)8191l, (long)123456l, (long)127l, (long)7l)))) == (19l));
assert(skjkasdkd((new ArrayList<Long>(Arrays.asList((long)127l, (long)97l, (long)8192l)))) == (10l));
}
}
|
humaneval-HumanEval_42_incr_list.json-L11 | py | from typing import List
def incr_list(l: List[int]) -> List[int]:
"""Return list with elements incremented by 1.
>>> incr_list([1, 2, 3])
[2, 3, 4]
>>> incr_list([5, 3, 5, 2, 3, 3, 9, 0, 123])
[6, 4, 6, 3, 4, 4, 10, 1, 124]
"""
res = [] | res.append(i + 1)
return res
| for i in l: | def check(candidate):
assert candidate([]) == []
assert candidate([3, 2, 1]) == [4, 3, 2]
assert candidate([5, 2, 5, 2, 3, 3, 9, 0, 123]) == [6, 3, 6, 3, 4, 4, 10, 1, 124]
def test_check():
check(incr_list)
test_check()
|
humaneval-HumanEval_119_match_parens.json-L34 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given 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;
} | if (s4.charAt(i) == '(') c2 += 1;
else c2 -= 1;
if (c2 < 0) break;
}
if (c1 == 0 || c2 == 0) return "Yes";
else return "No";
}
} | for (int i = 0; i < s4.length(); i++) { | }
public static void main(String[] args) {
assert(matchParens((new ArrayList<String>(Arrays.asList((String)"()(", (String)")")))).equals(("Yes")));
assert(matchParens((new ArrayList<String>(Arrays.asList((String)")", (String)")")))).equals(("No")));
assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(()(())", (String)"())())")))).equals(("No")));
assert(matchParens((new ArrayList<String>(Arrays.asList((String)")())", (String)"(()()(")))).equals(("Yes")));
assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(())))", (String)"(()())((")))).equals(("Yes")));
assert(matchParens((new ArrayList<String>(Arrays.asList((String)"()", (String)"())")))).equals(("No")));
assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(()(", (String)"()))()")))).equals(("Yes")));
assert(matchParens((new ArrayList<String>(Arrays.asList((String)"((((", (String)"((())")))).equals(("No")));
assert(matchParens((new ArrayList<String>(Arrays.asList((String)")(()", (String)"(()(")))).equals(("No")));
assert(matchParens((new ArrayList<String>(Arrays.asList((String)")(", (String)")(")))).equals(("No")));
assert(matchParens((new ArrayList<String>(Arrays.asList((String)"(", (String)")")))).equals(("Yes")));
assert(matchParens((new ArrayList<String>(Arrays.asList((String)")", (String)"(")))).equals(("Yes")));
}
}
|
humaneval-HumanEval_40_triples_sum_to_zero.json-L33 | 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); | return true;
} else if (currSum < 0) {
j++;
} else {
k--;
}
}
}
return false;
}
} | if (currSum == 0) { | }
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_87_get_row.json-L23 | py | from typing import List, Tuple
def get_row(lst: List[List[int]], x: int) -> List[Tuple[int, int]]:
"""
You are given a 2 dimensional data, as a nested lists,
which is similar to matrix, however, unlike matrices,
each row may contain a different number of columns.
Given lst, and integer x, find integers x in the list,
and return list of tuples, [(x1, y1), (x2, y2) ...] such that
each tuple is a coordinate - (row, columns), starting with 0.
Sort coordinates initially by rows in ascending order.
Also, sort coordinates of the row by columns in descending order.
Examples:
>>> get_row([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 1, 6], [1, 2, 3, 4, 5, 1]], 1)
[(0, 0), (1, 4), (1, 0), (2, 5), (2, 0)]
>>> get_row([], 1)
[]
>>> get_row([[], [1], [1, 2, 3]], 3)
[(2, 2)]
"""
res = [(i, j) for i in range(len(lst)) for j in range(len(lst[i])) if lst[i][j] == x] | return res
| res.sort(key=lambda t: (t[0], -t[1])) | def check(candidate):
assert candidate([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 1, 6], [1, 2, 3, 4, 5, 1]], 1) == [(0, 0), (1, 4), (1, 0), (2, 5), (2, 0)]
assert candidate([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]], 2) == [(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1)]
assert candidate([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 1, 3, 4, 5, 6], [1, 2, 1, 4, 5, 6], [1, 2, 3, 1, 5, 6], [1, 2, 3, 4, 1, 6], [1, 2, 3, 4, 5, 1]], 1) == [(0, 0), (1, 0), (2, 1), (2, 0), (3, 2), (3, 0), (4, 3), (4, 0), (5, 4), (5, 0), (6, 5), (6, 0)]
assert candidate([], 1) == []
assert candidate([[1]], 2) == []
assert candidate([[], [1], [1, 2, 3]], 3) == [(2, 2)]
def test_check():
check(get_row)
test_check()
|
humaneval-HumanEval_95_check_dict_case.json-L29 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given a hash map, return true if all keys are strings in lower
// case or all keys are strings in upper case, else return false.
// The function should return false is the given hash map is empty.
// Examples:
// >>> checkDictCase((new HashMap<String,String>(Map.of("a", "apple", "b", "banana"))))
// (true)
// >>> checkDictCase((new HashMap<String,String>(Map.of("a", "apple", "A", "banana", "B", "banana"))))
// (false)
// >>> checkDictCase((new HashMap<String,String>(Map.of("a", "apple", 8l, "banana", "a", "apple"))))
// (false)
// >>> checkDictCase((new HashMap<String,String>(Map.of("Name", "John", "Age", "36", "City", "Houston"))))
// (false)
// >>> checkDictCase((new HashMap<String,String>(Map.of("STATE", "NC", "ZIP", "12345"))))
// (true)
public static boolean checkDictCase(HashMap<String,String> dict) {
if (dict.isEmpty()) {
return false;
}
boolean allLowerCase = dict.keySet().stream().allMatch(k -> k.matches("^[a-z]+$"));
boolean allUpperCase = dict.keySet().stream().allMatch(k -> k.matches("^[A-Z]+$")); | }
} | return allLowerCase || allUpperCase; | }
public static void main(String[] args) {
assert(checkDictCase((new HashMap<String,String>(Map.of("p", "pineapple", "b", "banana")))) == (true));
assert(checkDictCase((new HashMap<String,String>(Map.of("p", "pineapple", "A", "banana", "B", "banana")))) == (false));
assert(checkDictCase((new HashMap<String,String>(Map.of("p", "pineapple", "5", "banana", "a", "apple")))) == (false));
assert(checkDictCase((new HashMap<String,String>(Map.of("Name", "John", "Age", "36", "City", "Houston")))) == (false));
assert(checkDictCase((new HashMap<String,String>(Map.of("STATE", "NC", "ZIP", "12345")))) == (true));
assert(checkDictCase((new HashMap<String,String>(Map.of("fruit", "Orange", "taste", "Sweet")))) == (true));
assert(checkDictCase((new HashMap<String,String>())) == (false));
}
}
|
humaneval-HumanEval_61_correct_bracketing.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 {
// brackets is a string of "(" and ")".
// return true if every opening bracket has a corresponding closing bracket.
// >>> correctBracketing(("("))
// (false)
// >>> correctBracketing(("()"))
// (true)
// >>> correctBracketing(("(()())"))
// (true)
// >>> correctBracketing((")(()"))
// (false)
public static boolean correctBracketing(String brackets) {
int balance = 0;
for (int i = 0; i < brackets.length(); i++) {
if (brackets.charAt(i) == '(') {
balance++;
} else {
balance--;
}
if (balance < 0) {
return false;
}
} | }
} | return balance == 0; | }
public static void main(String[] args) {
assert(correctBracketing(("()")) == (true));
assert(correctBracketing(("(()())")) == (true));
assert(correctBracketing(("()()(()())()")) == (true));
assert(correctBracketing(("()()((()()())())(()()(()))")) == (true));
assert(correctBracketing(("((()())))")) == (false));
assert(correctBracketing((")(()")) == (false));
assert(correctBracketing(("(")) == (false));
assert(correctBracketing(("((((")) == (false));
assert(correctBracketing((")")) == (false));
assert(correctBracketing(("(()")) == (false));
assert(correctBracketing(("()()(()())())(()")) == (false));
assert(correctBracketing(("()()(()())()))()")) == (false));
}
}
|
humaneval-HumanEval_129_minPath.json-L45 | py | from typing import List
def minPath(grid: List[List[int]], k: int) -> List[int]:
"""
Given a grid with N rows and N columns (N >= 2) and a positive integer k,
each cell of the grid contains a value. Every integer in the range [1, N * N]
inclusive appears exactly once on the cells of the grid.
You have to find the minimum path of length k in the grid. You can start
from any cell, and in each step you can move to any of the neighbor cells,
in other words, you can go to cells which share an edge with you current
cell.
Please note that a path of length k means visiting exactly k cells (not
necessarily distinct).
You CANNOT go off the grid.
A path A (of length k) is considered less than a path B (of length k) if
after making the ordered lists of the values on the cells that A and B go
through (let's call them lst_A and lst_B), lst_A is lexicographically less
than lst_B, in other words, there exist an integer index i (1 <= i <= k)
such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have
lst_A[j] = lst_B[j].
It is guaranteed that the answer is unique.
Return an ordered list of the values on the cells that the minimum path go through.
Examples:
>>> minPath([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3)
[1, 2, 1]
>>> minPath([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1)
[1]
"""
min_val = float('inf')
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] < min_val:
min_val = grid[i][j]
row = i
col = j
path = [min_val]
while len(path) < k:
min_val = float('inf')
for i, j in (row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1):
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] < min_val:
min_val = grid[i][j] | col = j
path.append(min_val)
return path | row = i | 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_61_correct_bracketing.json-L27 | js | //brackets is a string of "(" and ")".
// return true if every opening bracket has a corresponding closing bracket.
// >>> correct_bracketing("(")
// false
// >>> correct_bracketing("()")
// true
// >>> correct_bracketing("(()())")
// true
// >>> correct_bracketing(")(()")
// false
function correct_bracketing(brackets){
var opens = [];
for (var i = 0; i < brackets.length; i++){
if (brackets[i] == "("){
opens.push(brackets[i]);
}
else if (brackets[i] == ")"){
if (opens.length > 0){
opens.pop();
}
else {
return false;
}
}
}
if (opens.length == 0){ | }
else {
return false;
}
} | return true; | 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_1_separate_paren_groups.json-L11 | py | from typing import List
def separate_paren_groups(paren_string: str) -> List[str]:
""" Input to this function is a string containing multiple groups of nested parentheses. Your goal is to
separate those group into separate strings and return the list of those.
Separate groups are balanced (each open brace is properly closed) and not nested within each other
Ignore any spaces in the input string.
>>> separate_paren_groups('( ) (( )) (( )( ))')
['()', '(())', '(()())']
"""
| groups = []
group = ''
open_count = 0
for c in paren_string:
if c == '(':
open_count += 1
elif c == ')':
open_count -= 1
group += c
if open_count == 0:
groups.append(group)
group = ''
return groups
| paren_string = paren_string.replace(' ', '') | def check(candidate):
assert candidate('(()()) ((())) () ((())()())') == ['(()())', '((()))', '()', '((())()())']
assert candidate('() (()) ((())) (((())))') == ['()', '(())', '((()))', '(((())))']
assert candidate('(()(())((())))') == ['(()(())((())))']
assert candidate('( ) (( )) (( )( ))') == ['()', '(())', '(()())']
def test_check():
check(separate_paren_groups)
test_check()
|
humaneval-HumanEval_133_sum_squares.json-L28 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given an array array list of numbers.
// You need to return the sum of squared numbers in the given array list,
// round each element in the array list to the upper int(Ceiling) first.
// Examples:
// >>> lst((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)2.0f, (float)3.0f))))
// (14l)
// >>> lst((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)4.0f, (float)9.0f))))
// (98l)
// >>> lst((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)3.0f, (float)5.0f, (float)7.0f))))
// (84l)
// >>> lst((new ArrayList<Float>(Arrays.asList((float)1.4f, (float)4.2f, (float)0.0f))))
// (29l)
// >>> lst((new ArrayList<Float>(Arrays.asList((float)-2.4f, (float)1.0f, (float)1.0f))))
// (6l)
public static long sumSquares(ArrayList<Float> lst) {
if (lst.size() == 0) {
return 0;
}
long sum = 0; | sum += (long)Math.ceil(lst.get(i)) * (long)Math.ceil(lst.get(i));
}
return sum;
}
} | for (int i = 0; i < lst.size(); i++) { | }
public static void main(String[] args) {
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)2.0f, (float)3.0f)))) == (14l));
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)2.0f, (float)3.0f)))) == (14l));
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)3.0f, (float)5.0f, (float)7.0f)))) == (84l));
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)1.4f, (float)4.2f, (float)0.0f)))) == (29l));
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)-2.4f, (float)1.0f, (float)1.0f)))) == (6l));
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)100.0f, (float)1.0f, (float)15.0f, (float)2.0f)))) == (10230l));
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)10000.0f, (float)10000.0f)))) == (200000000l));
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)-1.4f, (float)4.6f, (float)6.3f)))) == (75l));
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)-1.4f, (float)17.9f, (float)18.9f, (float)19.9f)))) == (1086l));
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)0.0f)))) == (0l));
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)-1.0f)))) == (1l));
assert(sumSquares((new ArrayList<Float>(Arrays.asList((float)-1.0f, (float)1.0f, (float)0.0f)))) == (2l));
}
}
|
humaneval-HumanEval_69_search.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 a non-empty array list of positive integers. Return the greatest integer that is greater than
// zero, and has a frequency greater than or equal to the value of the integer itself.
// The frequency of an integer is the number of times it appears in the array list.
// If no such a value exist, return -1.
// Examples:
// >>> search((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)2l, (long)2l, (long)3l, (long)1l))))
// (2l)
// >>> search((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)2l, (long)3l, (long)3l, (long)3l, (long)4l, (long)4l, (long)4l))))
// (3l)
// >>> search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)4l, (long)4l, (long)4l))))
// (-1l)
public static long search(ArrayList<Long> lst) {
HashMap<Long, Long> map = new HashMap<Long, Long>();
Long max = (long)-1;
for (int i = 0; i < lst.size(); i++) {
long val = lst.get(i);
if (map.containsKey(val)) {
map.put(val, map.get(val) + 1);
} else {
map.put(val, (long)1);
}
if (map.get(val) >= val && val > max) {
max = val;
}
}
return max; | } | } | }
public static void main(String[] args) {
assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)5l, (long)5l, (long)1l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)4l, (long)1l, (long)4l, (long)4l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l)))) == (8l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)3l, (long)2l, (long)2l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)7l, (long)8l, (long)8l, (long)4l, (long)8l, (long)7l, (long)3l, (long)9l, (long)6l, (long)5l, (long)10l, (long)4l, (long)3l, (long)6l, (long)7l, (long)1l, (long)7l, (long)4l, (long)10l, (long)8l, (long)1l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)8l, (long)2l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)7l, (long)1l, (long)8l, (long)8l, (long)10l, (long)5l, (long)8l, (long)5l, (long)3l, (long)10l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)3l, (long)6l, (long)5l, (long)6l, (long)4l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)9l, (long)6l, (long)7l, (long)1l, (long)4l, (long)7l, (long)1l, (long)8l, (long)8l, (long)9l, (long)8l, (long)10l, (long)10l, (long)8l, (long)4l, (long)10l, (long)4l, (long)10l, (long)1l, (long)2l, (long)9l, (long)5l, (long)7l, (long)9l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)1l, (long)9l, (long)10l, (long)1l, (long)3l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)9l, (long)7l, (long)5l, (long)8l, (long)7l, (long)5l, (long)3l, (long)7l, (long)5l, (long)10l, (long)10l, (long)3l, (long)6l, (long)10l, (long)2l, (long)8l, (long)6l, (long)5l, (long)4l, (long)9l, (long)5l, (long)3l, (long)10l)))) == (5l));
assert(search((new ArrayList<Long>(Arrays.asList((long)1l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)10l, (long)6l, (long)4l, (long)3l, (long)5l, (long)8l, (long)2l, (long)4l, (long)2l, (long)8l, (long)4l, (long)6l, (long)10l, (long)4l, (long)2l, (long)1l, (long)10l, (long)2l, (long)1l, (long)1l, (long)5l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)10l, (long)4l, (long)8l, (long)2l, (long)10l, (long)5l, (long)1l, (long)2l, (long)9l, (long)5l, (long)5l, (long)6l, (long)3l, (long)8l, (long)6l, (long)4l, (long)10l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)1l, (long)6l, (long)10l, (long)1l, (long)6l, (long)9l, (long)10l, (long)8l, (long)6l, (long)8l, (long)7l, (long)3l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)2l, (long)4l, (long)1l, (long)5l, (long)1l, (long)5l, (long)2l, (long)5l, (long)7l, (long)7l, (long)7l, (long)3l, (long)10l, (long)1l, (long)5l, (long)4l, (long)2l, (long)8l, (long)4l, (long)1l, (long)9l, (long)10l, (long)7l, (long)10l, (long)2l, (long)8l, (long)10l, (long)9l, (long)4l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)4l, (long)2l, (long)8l, (long)7l, (long)5l, (long)6l, (long)4l, (long)10l, (long)4l, (long)6l, (long)3l, (long)7l, (long)8l, (long)8l, (long)3l, (long)1l, (long)4l, (long)2l, (long)2l, (long)10l, (long)7l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)8l, (long)6l, (long)10l, (long)2l, (long)6l, (long)10l, (long)2l, (long)7l, (long)8l, (long)10l, (long)3l, (long)8l, (long)2l, (long)6l, (long)2l, (long)3l, (long)1l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)3l, (long)9l, (long)5l, (long)6l, (long)3l, (long)2l, (long)8l, (long)5l, (long)6l, (long)10l, (long)10l, (long)6l, (long)8l, (long)4l, (long)10l, (long)7l, (long)7l, (long)10l, (long)8l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)10l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)7l, (long)7l, (long)2l, (long)4l, (long)7l, (long)2l, (long)10l, (long)9l, (long)7l, (long)5l, (long)7l, (long)2l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)4l, (long)10l, (long)2l, (long)1l, (long)1l, (long)10l, (long)3l, (long)6l, (long)1l, (long)8l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)7l, (long)9l, (long)9l, (long)9l, (long)3l, (long)4l, (long)1l, (long)5l, (long)9l, (long)1l, (long)2l, (long)1l, (long)1l, (long)10l, (long)7l, (long)5l, (long)6l, (long)7l, (long)6l, (long)7l, (long)7l, (long)6l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)10l, (long)10l, (long)9l, (long)2l)))) == (-1l));
}
}
|
humaneval-HumanEval_9_rolling_max.json-L13 | 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 max = [];
let tempMax = 0;
for(let i = 0; i < numbers.length; i++){
if(tempMax < numbers[i]){
tempMax = numbers[i];
}
max.push(tempMax); | return 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_23_strlen.json-L8 | js | //Return length of given string
// >>> strlen("")
// 0
// >>> strlen("abc")
// 3
function strlen(string){
return string.length; | } | const assert = require('node:assert');
function test() {
let candidate = strlen;
assert.deepEqual(candidate(""),0);
assert.deepEqual(candidate("x"),1);
assert.deepEqual(candidate("asdasnakj"),9);
}
test(); |
|
humaneval-HumanEval_59_largest_prime_factor.json-L10 | js | //Return the largest prime factor of n. Assume n > 1 and is not a prime.
// >>> largest_prime_factor(13195)
// 29
// >>> largest_prime_factor(2048)
// 2
function largest_prime_factor(n){
var result = 2;
while(n > result){
if(n % result == 0){ | }
else{
result = result + 1;
}
}
return result;
} | n = n / result; | const assert = require('node:assert');
function test() {
let candidate = largest_prime_factor;
assert.deepEqual(candidate(15),5);
assert.deepEqual(candidate(27),3);
assert.deepEqual(candidate(63),7);
assert.deepEqual(candidate(330),11);
assert.deepEqual(candidate(13195),29);
}
test(); |
humaneval-HumanEval_113_odd_count.json-L18 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given an array array list of strings, where each string consists of only digits, return an array array list.
// Each element i of the output should be "the number of odd elements in the
// string i of the input." where all the i's should be replaced by the number
// of odd digits in the i'th string of the input.
// >>> oddCount((new ArrayList<String>(Arrays.asList((String)"1234567"))))
// (new ArrayList<String>(Arrays.asList((String)"the number of odd elements 4n the str4ng 4 of the 4nput.")))
// >>> oddCount((new ArrayList<String>(Arrays.asList((String)"3", (String)"11111111"))))
// (new ArrayList<String>(Arrays.asList((String)"the number of odd elements 1n the str1ng 1 of the 1nput.", (String)"the number of odd elements 8n the str8ng 8 of the 8nput.")))
public static ArrayList<String> oddCount(ArrayList<String> lst) {
| for (int i = 0; i < lst.size(); i++) {
int count = 0;
for (int j = 0; j < lst.get(i).length(); j++) {
if (Integer.parseInt(lst.get(i).substring(j, j + 1)) % 2 == 1) {
count++;
}
}
res.add("the number of odd elements " + count + "n the str" + count + "ng " + count + " of the " + count + "nput.");
}
return res;
}
} | ArrayList<String> res = new ArrayList<String>(); | }
public static void main(String[] args) {
assert(oddCount((new ArrayList<String>(Arrays.asList((String)"1234567")))).equals((new ArrayList<String>(Arrays.asList((String)"the number of odd elements 4n the str4ng 4 of the 4nput.")))));
assert(oddCount((new ArrayList<String>(Arrays.asList((String)"3", (String)"11111111")))).equals((new ArrayList<String>(Arrays.asList((String)"the number of odd elements 1n the str1ng 1 of the 1nput.", (String)"the number of odd elements 8n the str8ng 8 of the 8nput.")))));
assert(oddCount((new ArrayList<String>(Arrays.asList((String)"271", (String)"137", (String)"314")))).equals((new ArrayList<String>(Arrays.asList((String)"the number of odd elements 2n the str2ng 2 of the 2nput.", (String)"the number of odd elements 3n the str3ng 3 of the 3nput.", (String)"the number of odd elements 2n the str2ng 2 of the 2nput.")))));
}
}
|
humaneval-HumanEval_67_fruit_distribution.json-L18 | py | def fruit_distribution(s: str, n: int) -> int:
"""
In this task, you will be given a string that represents a number of apples and oranges
that are distributed in a basket of fruit this basket contains
apples, oranges, and mango fruits. Given the string that represents the total number of
the oranges and apples and an integer that represent the total number of the fruits
in the basket return the number of the mango fruits in the basket.
for examble:
>>> fruit_distribution('5 apples and 6 oranges', 19)
8
>>> fruit_distribution('0 apples and 1 oranges', 3)
2
>>> fruit_distribution('2 apples and 3 oranges', 100)
95
>>> fruit_distribution('100 apples and 1 oranges', 120)
19
"""
| for fruit in s.split(' '):
try:
fruits.append(int(fruit))
except:
pass
return n - sum(fruits)
| fruits = [] | def check(candidate):
assert candidate('5 apples and 6 oranges', 19) == 8
assert candidate('5 apples and 6 oranges', 21) == 10
assert candidate('0 apples and 1 oranges', 3) == 2
assert candidate('1 apples and 0 oranges', 3) == 2
assert candidate('2 apples and 3 oranges', 100) == 95
assert candidate('2 apples and 3 oranges', 5) == 0
assert candidate('1 apples and 100 oranges', 120) == 19
def test_check():
check(fruit_distribution)
test_check()
|
humaneval-HumanEval_118_get_closest_vowel.json-L36 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// 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 right = word.charAt(j + 1);
if (consonants.indexOf(left) != -1 && consonants.indexOf(right) != -1) {
return String.valueOf(word.charAt(j));
}
}
return "";
}
} | char left = word.charAt(j - 1); | }
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_76_is_simple_power.json-L19 | js | //Your task is to write a function that returns true if a number x is a simple
// power of n and false in other cases.
// x is a simple power of n if n**int=x
// For example:
// >>> is_simple_power(1, 4)
// true
// >>> is_simple_power(2, 2)
// true
// >>> is_simple_power(8, 2)
// true
// >>> is_simple_power(3, 2)
// false
// >>> is_simple_power(3, 1)
// false
// >>> is_simple_power(5, 3)
// false
function is_simple_power(x, n){
if(x===1){ | }
if(x < n){
return false;
}
if(x % n !== 0){
return false;
}
return is_simple_power(x/n, n);
}
| return true; | const assert = require('node:assert');
function test() {
let candidate = is_simple_power;
assert.deepEqual(candidate(16, 2),true);
assert.deepEqual(candidate(143214, 16),false);
assert.deepEqual(candidate(4, 2),true);
assert.deepEqual(candidate(9, 3),true);
assert.deepEqual(candidate(16, 4),true);
assert.deepEqual(candidate(24, 2),false);
assert.deepEqual(candidate(128, 4),false);
assert.deepEqual(candidate(12, 6),false);
assert.deepEqual(candidate(1, 1),true);
assert.deepEqual(candidate(1, 12),true);
}
test(); |
humaneval-HumanEval_108_count_nums.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 {
// Write a function count_nums which takes an array array list of integers and returns
// the number of elements which has a sum of digits > 0.
// If a number is negative, then its first signed digit will be negative:
// e.g. -123 has signed digits -1, 2, and 3.
// >>> countNums((new ArrayList<Long>(Arrays.asList())))
// (0l)
// >>> countNums((new ArrayList<Long>(Arrays.asList((long)-1l, (long)11l, (long)-11l))))
// (1l)
// >>> countNums((new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)2l))))
// (3l)
public static long countNums(ArrayList<Long> arr) {
long result = 0l;
for (long i : arr) {
String str = String.valueOf(i);
int sum = 0;
if (str.startsWith("-")) {
sum = sum - (int) Character.getNumericValue(str.charAt(1));
for (int j = 2; j < str.length(); j++) {
sum = sum + (int) Character.getNumericValue(str.charAt(j));
}
} else {
for (int j = 0; j < str.length(); j++) {
sum = sum + (int) Character.getNumericValue(str.charAt(j));
}
}
if (sum > 0) {
result++;
} | return result;
}
} | } | }
public static void main(String[] args) {
assert(countNums((new ArrayList<Long>(Arrays.asList()))) == (0l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)0l)))) == (0l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)2l, (long)-2l, (long)3l, (long)4l, (long)5l)))) == (6l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l, (long)6l, (long)9l, (long)-6l, (long)0l, (long)1l, (long)5l)))) == (5l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l, (long)100l, (long)98l, (long)-7l, (long)1l, (long)-1l)))) == (4l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)12l, (long)23l, (long)34l, (long)-45l, (long)-56l, (long)0l)))) == (5l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)0l, (long)1l)))) == (1l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l)))) == (1l));
}
}
|
humaneval-HumanEval_124_valid_date.json-L49 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// 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_105_by_length.json-L17 | js | //Given an array of integers, sort the integers that are between 1 and 9 inclusive,
// reverse the resulting array, and then replace each digit by its corresponding name from
// "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine".
// For example:
// >>> by_length([2, 1, 1, 4, 5, 8, 2, 3])
// ["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"]
// If the array is empty, return an empty array:
// >>> by_length([])
// []
// If the array has any strange number ignore it:
// >>> by_length([1, -1, 55])
// ["One"]
function by_length(arr){
var arr2 = [];
for(var i = 0; i < arr.length; i++){
var num = arr[i]; | arr2.push(num);
}
}
arr2.sort();
arr2.reverse();
var arr3 = [];
for(var i = 0; i < arr2.length; i++){
var num = arr2[i];
if(num == 1){
arr3.push("One");
}
else if(num == 2){
arr3.push("Two");
}
else if(num == 3){
arr3.push("Three");
}
else if(num == 4){
arr3.push("Four");
}
else if(num == 5){
arr3.push("Five");
}
else if(num == 6){
arr3.push("Six");
}
else if(num == 7){
arr3.push("Seven");
}
else if(num == 8){
arr3.push("Eight");
}
else if(num == 9){
arr3.push("Nine");
}
}
return arr3;
} | if(num > 0 && num < 10){ | const assert = require('node:assert');
function test() {
let candidate = by_length;
assert.deepEqual(candidate([2, 1, 1, 4, 5, 8, 2, 3]),["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"]);
assert.deepEqual(candidate([]),[]);
assert.deepEqual(candidate([1, -1, 55]),["One"]);
assert.deepEqual(candidate([1, -1, 3, 2]),["Three", "Two", "One"]);
assert.deepEqual(candidate([9, 4, 8]),["Nine", "Eight", "Four"]);
}
test(); |
humaneval-HumanEval_56_correct_bracketing.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 {
// brackets is a string of "<" and ">".
// return true if every opening bracket has a corresponding closing bracket.
// >>> correctBracketing(("<"))
// (false)
// >>> correctBracketing(("<>"))
// (true)
// >>> correctBracketing(("<<><>>"))
// (true)
// >>> correctBracketing(("><<>"))
// (false)
public static boolean correctBracketing(String brackets) {
int open = 0;
for (int i = 0; i < brackets.length(); i++) { | if (c == '<') {
open++;
} else {
open--;
}
if (open < 0) {
return false;
}
}
return open == 0;
}
} | char c = brackets.charAt(i); | }
public static void main(String[] args) {
assert(correctBracketing(("<>")) == (true));
assert(correctBracketing(("<<><>>")) == (true));
assert(correctBracketing(("<><><<><>><>")) == (true));
assert(correctBracketing(("<><><<<><><>><>><<><><<>>>")) == (true));
assert(correctBracketing(("<<<><>>>>")) == (false));
assert(correctBracketing(("><<>")) == (false));
assert(correctBracketing(("<")) == (false));
assert(correctBracketing(("<<<<")) == (false));
assert(correctBracketing((">")) == (false));
assert(correctBracketing(("<<>")) == (false));
assert(correctBracketing(("<><><<><>><>><<>")) == (false));
assert(correctBracketing(("<><><<><>><>>><>")) == (false));
}
}
|
humaneval-HumanEval_128_prod_signs.json-L35 | py | from typing import List, Optional
def prod_signs(arr: List[int]) -> Optional[int]:
"""
You are given an array arr of integers and you need to return
sum of magnitudes of integers multiplied by product of all signs
of each number in the array, represented by 1, -1 or 0.
Note: return None for empty arr.
Example:
>>> prod_signs([1, 2, 2, -4])
9
>>> prod_signs([0, 1])
0
>>> prod_signs([])
None
"""
def product(lst: List[int]) -> int:
out = 1
for item in lst:
out *= item
return out
if len(arr) == 0:
return None
else:
sign_arr = []
for num in arr:
if num > 0:
sign_arr.append(1)
elif num < 0:
sign_arr.append(-1)
else:
sign_arr.append(0) | return sum(map(abs, arr)) * product(sign_arr) | 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_107_even_odd_palindrome.json-L28 | py | from typing import Tuple
def even_odd_palindrome(n: int) -> Tuple[int, int]:
"""
Given a positive integer n, return a tuple 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 tuple has the number of even and odd integer palindromes respectively.
"""
return (
sum(1 for i in range(1, n + 1) if str(i) == str(i)[::-1] and i % 2 == 0), | )
| sum(1 for i in range(1, n + 1) if str(i) == str(i)[::-1] and i % 2 != 0) | def check(candidate):
assert candidate(123) == (8, 13)
assert candidate(12) == (4, 6)
assert candidate(3) == (1, 2)
assert candidate(63) == (6, 8)
assert candidate(25) == (5, 6)
assert candidate(19) == (4, 6)
assert candidate(9) == (4, 5)
assert candidate(1) == (0, 1)
def test_check():
check(even_odd_palindrome)
test_check()
|
humaneval-HumanEval_19_sort_numbers.json-L29 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Input is a space-delimited string of numberals from 'zero' to 'nine'.
// Valid choices are 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight' and 'nine'.
// Return the string with numbers sorted from smallest to largest
// >>> sortNumbers(("three one five"))
// ("one three five")
public static String sortNumbers(String numbers) {
String [] nums = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
HashMap<String, Integer> indexMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
indexMap.put(nums[i], i);
}
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);
} | } | }
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_57_monotonic.json-L11 | 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_59_largest_prime_factor.json-L11 | py | def largest_prime_factor(n: int) -> int:
"""Return the largest prime factor of n. Assume n > 1 and is not a prime.
>>> largest_prime_factor(13195)
29
>>> largest_prime_factor(2048)
2
"""
largest = 1
factor = 2
while factor < n: | largest = factor
while n % factor == 0:
n = n // factor
factor += 1
return max(largest, n)
| if n % factor == 0: | def check(candidate):
assert candidate(15) == 5
assert candidate(27) == 3
assert candidate(63) == 7
assert candidate(330) == 11
assert candidate(13195) == 29
def test_check():
check(largest_prime_factor)
test_check()
|
humaneval-HumanEval_139_special_factorial.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 {
// The Brazilian factorial is defined as:
// brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1!
// where n > 0
// For example:
// >>> specialFactorial((4l))
// (288l)
// The function will receive an integer as input and should return the special
// factorial of this integer.
public static long specialFactorial(long n) {
long result = 1;
while (n > 0) {
long local_result = 1;
long temp = n;
while (temp > 0) {
local_result *= temp;
temp--;
}
result *= local_result;
n--; | return result;
}
} | } | }
public static void main(String[] args) {
assert(specialFactorial((4l)) == (288l));
assert(specialFactorial((5l)) == (34560l));
assert(specialFactorial((7l)) == (125411328000l));
assert(specialFactorial((1l)) == (1l));
}
}
|
humaneval-HumanEval_115_max_fill.json-L34 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given a rectangular grid of wells. Each row represents a single well,
// and each 1 in a row represents a single unit of water.
// Each well has a corresponding bucket that can be used to extract water from it,
// and all buckets have the same capacity.
// Your task is to use the buckets to empty the wells.
// Output the number of times you need to lower the buckets.
// Example 1:
// >>> maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (1l))
// (6l)
// Example 2:
// >>> maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)1l, (long)1l))))), (2l))
// (5l)
// Example 3:
// >>> maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l))))), (5l))
// (0l)
// Constraints:
// * all wells have the same length
// * 1 <= grid.length <= 10^2
// * 1 <= grid[:,1].length <= 10^2
// * grid[i][j] -> 0 | 1
// * 1 <= capacity <= 10
public static long maxFill(ArrayList<ArrayList<Long>> grid, long capacity) {
long count = 0l;
for (ArrayList<Long> well : grid) {
long level = 0l; | if (unit == 1l) {
level += 1l;
}
}
long units = level / capacity;
if (level % capacity != 0l) {
units += 1l;
}
count += units;
}
return count;
}
} | for (Long unit : well) { | }
public static void main(String[] args) {
assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (1l)) == (6l));
assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)1l, (long)1l))))), (2l)) == (5l));
assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)0l, (long)0l, (long)0l))))), (5l)) == (0l));
assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (2l)) == (4l));
assert(maxFill((new ArrayList<ArrayList<Long>>(Arrays.asList((ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l)), (ArrayList<Long>)new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l))))), (9l)) == (2l));
}
}
|
humaneval-HumanEval_39_prime_fib.json-L15 | js | //prime_fib returns n-th number that is a Fibonacci number and it's also prime.
// >>> prime_fib(1)
// 2
// >>> prime_fib(2)
// 3
// >>> prime_fib(3)
// 5
// >>> prime_fib(4)
// 13
// >>> prime_fib(5)
// 89
function prime_fib(n){
function isPrime(number){
if(number <= 3){ | }
else if(number % 2 == 0 || number % 3 == 0){
return false;
}
else{
for(var i = 5; i * i <= number; i += 6){
if(number % i == 0 || number % (i + 2) == 0){
return false;
}
}
return true;
}
}
function fib(num){
var a = 1, b = 0, temp;
while (num >= 0){
temp = a;
a = a + b;
b = temp;
num--;
}
return b;
}
var res = 0;
var fibNum = 0;
while(res < n){
if(isPrime(fib(fibNum))){
res++;
}
fibNum++;
}
return fib(fibNum - 1);
} | return number > 1; | const assert = require('node:assert');
function test() {
let candidate = prime_fib;
assert.deepEqual(candidate(1),2);
assert.deepEqual(candidate(2),3);
assert.deepEqual(candidate(3),5);
assert.deepEqual(candidate(4),13);
assert.deepEqual(candidate(5),89);
assert.deepEqual(candidate(6),233);
assert.deepEqual(candidate(7),1597);
assert.deepEqual(candidate(8),28657);
assert.deepEqual(candidate(9),514229);
assert.deepEqual(candidate(10),433494437);
}
test(); |
humaneval-HumanEval_20_find_closest_elements.json-L16 | py | from typing import List, Tuple
def find_closest_elements(numbers: List[float]) -> Tuple[float, float]:
""" From a supplied list of numbers (of length at least two) select and return two that are the closest to each
other and return them in order (smaller number, larger number).
>>> find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.2])
(2.0, 2.2)
>>> find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.0])
(2.0, 2.0)
"""
temp, temp2 = abs(numbers[0] - numbers[1]), abs(numbers[0] - numbers[1])
temp_index, temp2_index = 0, 1
if numbers[0] > numbers[1]:
temp, temp2 = temp2, temp
temp_index, temp2_index = temp2_index, temp_index | for j in range(i+1, len(numbers)):
if abs(numbers[i] - numbers[j]) < temp:
temp = abs(numbers[i] - numbers[j])
temp_index = i
temp2_index = j
elif abs(numbers[i] - numbers[j]) == temp:
if numbers[i] > numbers[j]:
temp = abs(numbers[i] - numbers[j])
temp_index = i
temp2_index = j
if numbers[temp_index] > numbers[temp2_index]:
temp, temp2 = numbers[temp2_index], numbers[temp_index]
else:
temp, temp2 = numbers[temp_index], numbers[temp2_index]
return temp, temp2
print(find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.2])) | for i in range(len(numbers)): | def check(candidate):
assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2]) == (3.9, 4.0)
assert candidate([1.0, 2.0, 5.9, 4.0, 5.0]) == (5.0, 5.9)
assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.2]) == (2.0, 2.2)
assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0]) == (2.0, 2.0)
assert candidate([1.1, 2.2, 3.1, 4.1, 5.1]) == (2.2, 3.1)
def test_check():
check(find_closest_elements)
test_check()
|
humaneval-HumanEval_146_specialFilter.json-L34 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Write a function that takes an array array list of numbers as input and returns
// the number of elements in the array array list that are greater than 10 and both
// first and last digits of a number are odd (1, 3, 5, 7, 9).
// For example:
// >>> specialFilter((new ArrayList<Long>(Arrays.asList((long)15l, (long)-73l, (long)14l, (long)-15l))))
// (1l)
// >>> specialFilter((new ArrayList<Long>(Arrays.asList((long)33l, (long)-2l, (long)-3l, (long)45l, (long)21l, (long)109l))))
// (2l)
public static long specialFilter(ArrayList<Long> nums) {
class helper {
public long getFirstDigit(long n) {
long[] digits = getDigits(n);
return digits[0];
}
public long getLastDigit(long n) {
long[] digits = getDigits(n);
return digits[digits.length - 1];
}
public long[] getDigits(long n) {
ArrayList<Long> digits = new ArrayList<Long>();
while (n != 0) {
digits.add(n % 10);
n /= 10;
}
Collections.reverse(digits); | }
}
helper h = new helper();
return nums.stream().filter(x -> x > 10).filter(x -> {
long firstDigit = h.getFirstDigit(x);
long lastDigit = h.getLastDigit(x);
return firstDigit % 2 != 0 && lastDigit % 2 != 0;
}).count();
}
} | return digits.stream().mapToLong(i -> i).toArray(); | }
public static void main(String[] args) {
assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)5l, (long)-2l, (long)1l, (long)-5l)))) == (0l));
assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)15l, (long)-73l, (long)14l, (long)-15l)))) == (1l));
assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)33l, (long)-2l, (long)-3l, (long)45l, (long)21l, (long)109l)))) == (2l));
assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)43l, (long)-12l, (long)93l, (long)125l, (long)121l, (long)109l)))) == (4l));
assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)71l, (long)-2l, (long)-33l, (long)75l, (long)21l, (long)19l)))) == (3l));
assert(specialFilter((new ArrayList<Long>(Arrays.asList((long)1l)))) == (0l));
assert(specialFilter((new ArrayList<Long>(Arrays.asList()))) == (0l));
}
}
|
humaneval-HumanEval_139_special_factorial.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 {
// The Brazilian factorial is defined as:
// brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1!
// where n > 0
// For example:
// >>> specialFactorial((4l))
// (288l)
// The function will receive an integer as input and should return the special
// factorial of this integer.
public static long specialFactorial(long n) {
long result = 1;
while (n > 0) {
long local_result = 1;
long temp = n; | local_result *= temp;
temp--;
}
result *= local_result;
n--;
}
return result;
}
} | while (temp > 0) { | }
public static void main(String[] args) {
assert(specialFactorial((4l)) == (288l));
assert(specialFactorial((5l)) == (34560l));
assert(specialFactorial((7l)) == (125411328000l));
assert(specialFactorial((1l)) == (1l));
}
}
|
humaneval-HumanEval_147_get_max_triples.json-L19 | py | def get_max_triples(n: int) -> int:
"""
You are given a positive integer n. You have to create an integer array a of length n.
For each i (1 ≤ i ≤ n), the value of a[i] = i * i - i + 1.
Return the number of triples (a[i], a[j], a[k]) of a where i < j < k,
and a[i] + a[j] + a[k] is a multiple of 3.
Example :
>>> get_max_triples(5)
1
Explanation:
a = [1, 3, 7, 13, 21]
The only valid triple is (1, 7, 13).
"""
a = [i * i - i + 1 for i in range(1, n + 1)]
i = 0
j = 1
k = 2 | while (i < n - 2):
while (j < n - 1):
while (k < n):
if (a[i] + a[j] + a[k]) % 3 == 0:
count += 1
k += 1
j += 1
k = j + 1
i += 1
j = i + 1
k = j + 1
return count
| count = 0 | def check(candidate):
assert candidate(5) == 1
assert candidate(6) == 4
assert candidate(10) == 36
assert candidate(100) == 53361
def test_check():
check(get_max_triples)
test_check()
|
humaneval-HumanEval_56_correct_bracketing.json-L33 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// brackets is a string of "<" and ">".
// return true if every opening bracket has a corresponding closing bracket.
// >>> correctBracketing(("<"))
// (false)
// >>> correctBracketing(("<>"))
// (true)
// >>> correctBracketing(("<<><>>"))
// (true)
// >>> correctBracketing(("><<>"))
// (false)
public static boolean correctBracketing(String brackets) {
int open = 0;
for (int i = 0; i < brackets.length(); i++) {
char c = brackets.charAt(i);
if (c == '<') {
open++;
} else {
open--;
}
if (open < 0) {
return false;
}
}
return open == 0; | } | } | }
public static void main(String[] args) {
assert(correctBracketing(("<>")) == (true));
assert(correctBracketing(("<<><>>")) == (true));
assert(correctBracketing(("<><><<><>><>")) == (true));
assert(correctBracketing(("<><><<<><><>><>><<><><<>>>")) == (true));
assert(correctBracketing(("<<<><>>>>")) == (false));
assert(correctBracketing(("><<>")) == (false));
assert(correctBracketing(("<")) == (false));
assert(correctBracketing(("<<<<")) == (false));
assert(correctBracketing((">")) == (false));
assert(correctBracketing(("<<>")) == (false));
assert(correctBracketing(("<><><<><>><>><<>")) == (false));
assert(correctBracketing(("<><><<><>><>>><>")) == (false));
}
}
|
humaneval-HumanEval_46_fib4.json-L26 | js | //The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
// fib4(0) -> 0
// fib4(1) -> 0
// fib4(2) -> 2
// fib4(3) -> 0
// fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4).
// Please write a function to efficiently compute the n-th element of the fib4 number sequence. Do not use recursion.
// >>> fib4(5)
// 4
// >>> fib4(6)
// 8
// >>> fib4(7)
// 14
function fib4(n){
var prev = [0,0,2,0];
if(n<prev.length){
return prev[n];
}
for(var i=4;i<=n;i++){
var curr = prev[0]+prev[1]+prev[2]+prev[3];
prev[0] = prev[1];
prev[1] = prev[2];
prev[2] = prev[3];
prev[3] = curr;
} | }
| return prev[3]; | const assert = require('node:assert');
function test() {
let candidate = fib4;
assert.deepEqual(candidate(5),4);
assert.deepEqual(candidate(8),28);
assert.deepEqual(candidate(10),104);
assert.deepEqual(candidate(12),386);
}
test(); |
humaneval-HumanEval_72_will_it_fly.json-L27 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Write a function that 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 list) and the sum of its elements is less than or equal the maximum possible weight w.
// Example:
// >>> willItFly((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l))), (5l))
// (false)
// # 1+2 is less than the maximum possible weight, but it's unbalanced.
// >>> willItFly((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)3l))), (1l))
// (false)
// # it's balanced, but 3+2+3 is more than the maximum possible weight.
// >>> willItFly((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)3l))), (9l))
// (true)
// # 3+2+3 is less than the maximum possible weight, and it's balanced.
// >>> willItFly((new ArrayList<Long>(Arrays.asList((long)3l))), (5l))
// (true)
// # 3 is less than the maximum possible weight, and it's balanced.
public static boolean willItFly(ArrayList<Long> q, long w) {
if (q.size() == 0) {
return false; | if (q.size() == 1) {
return true;
}
Long sum = 0l;
for (int i = 0; i < q.size(); i++) {
sum += q.get(i);
}
if (sum > w) {
return false;
}
for (int i = 0; i < q.size() / 2; i++) {
if (q.get(i) != q.get(q.size() - 1 - i)) {
return false;
}
}
return true;
}
} | } | }
public static void main(String[] args) {
assert(willItFly((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)3l))), (9l)) == (true));
assert(willItFly((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l))), (5l)) == (false));
assert(willItFly((new ArrayList<Long>(Arrays.asList((long)3l))), (5l)) == (true));
assert(willItFly((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)3l))), (1l)) == (false));
assert(willItFly((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))), (6l)) == (false));
assert(willItFly((new ArrayList<Long>(Arrays.asList((long)5l))), (5l)) == (true));
}
}
|
humaneval-HumanEval_105_by_length.json-L24 | py | from typing import List
def by_length(arr: List[int]) -> List[str]:
"""
Given an array of integers, sort the integers that are between 1 and 9 inclusive,
reverse the resulting array, and then replace each digit by its corresponding name from
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine".
For example:
>>> by_length([2, 1, 1, 4, 5, 8, 2, 3])
['Eight', 'Five', 'Four', 'Three', 'Two', 'Two', 'One', 'One']
If the array is empty, return an empty array:
>>> by_length([])
[]
If the array has any strange number ignore it:
>>> by_length([1, -1, 55])
['One']
"""
digits = [x for x in arr if 1 <= x <= 9]
digits.sort()
digits.reverse() | digits[i] = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'][digits[i] - 1]
return digits
| for i in range(len(digits)): | def check(candidate):
assert candidate([2, 1, 1, 4, 5, 8, 2, 3]) == ['Eight', 'Five', 'Four', 'Three', 'Two', 'Two', 'One', 'One']
assert candidate([]) == []
assert candidate([1, -1, 55]) == ['One']
assert candidate([1, -1, 3, 2]) == ['Three', 'Two', 'One']
assert candidate([9, 4, 8]) == ['Nine', 'Eight', 'Four']
def test_check():
check(by_length)
test_check()
|
humaneval-HumanEval_137_compare_one.json-L24 | js | //Create a function that takes integers, floats, or strings representing
// real numbers, and returns the larger variable in its given variable type.
// Return undefined if the values are equal.
// Note: If a real number is represented as a string, the floating point might be . or ,
// >>> compare_one(1, 2.5)
// 2.5
// >>> compare_one(1, "2,3")
// "2,3"
// >>> compare_one("5,1", "6")
// "6"
// >>> compare_one("1", 1)
// undefined
function compare_one(a, b){
if (typeof a === typeof b){
if (a > b) {
return a;
} else if (a < b) {
return b;
} else {
return undefined;
}
}
else if (typeof a === "string" && typeof b === "number"){ | return a;
} else if (a[0] < b){
return b;
} else {
return undefined;
}
}
else if (typeof a === "number" && typeof b === "string"){
if (a > b[0]){
return a;
} else if (a < b[0]){
return b;
} else {
return undefined;
}
}
}
| if (a[0] > b){ | const assert = require('node:assert');
function test() {
let candidate = compare_one;
assert.deepEqual(candidate(1, 2),2);
assert.deepEqual(candidate(1, 2.5),2.5);
assert.deepEqual(candidate(2, 3),3);
assert.deepEqual(candidate(5, 6),6);
assert.deepEqual(candidate(1, "2,3"),"2,3");
assert.deepEqual(candidate("5,1", "6"),"6");
assert.deepEqual(candidate("1", "2"),"2");
assert.deepEqual(candidate("1", 1),undefined);
}
test(); |
humaneval-HumanEval_60_sum_to_n.json-L17 | js | //sum_to_n is a function that sums numbers from 1 to n.
// >>> sum_to_n(30)
// 465
// >>> sum_to_n(100)
// 5050
// >>> sum_to_n(5)
// 15
// >>> sum_to_n(10)
// 55
// >>> sum_to_n(1)
// 1
function sum_to_n(n){
var sum = 0;
for(var i = 1; i <= n; i++){
sum += i;
} | }
| return sum; | const assert = require('node:assert');
function test() {
let candidate = sum_to_n;
assert.deepEqual(candidate(1),1);
assert.deepEqual(candidate(6),21);
assert.deepEqual(candidate(11),66);
assert.deepEqual(candidate(30),465);
assert.deepEqual(candidate(100),5050);
}
test(); |
humaneval-HumanEval_55_fib.json-L17 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Return n-th Fibonacci number.
// >>> fib((10l))
// (55l)
// >>> fib((1l))
// (1l)
// >>> fib((8l))
// (21l)
public static long fib(long n) {
| return 0l;
}
else if (n == 1) {
return 1l;
}
else {
return Problem.fib((n - 1)) + Problem.fib((n - 2));
}
}
} | if (n == 0) { | }
public static void main(String[] args) {
assert(fib((10l)) == (55l));
assert(fib((1l)) == (1l));
assert(fib((8l)) == (21l));
assert(fib((11l)) == (89l));
assert(fib((12l)) == (144l));
}
}
|
humaneval-HumanEval_27_flip_case.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 {
// For a given string, flip lowercase characters to uppercase and uppercase to lowercase.
// >>> flipCase(("Hello"))
// ("hELLO")
public static String flipCase(String string) {
| for (int i = 0; i < string.length(); i++) {
if (Character.isLowerCase(string.charAt(i))) {
answer += Character.toUpperCase(string.charAt(i));
} else {
answer += Character.toLowerCase(string.charAt(i));
}
}
return answer;
}
} | String answer = ""; | }
public static void main(String[] args) {
assert(flipCase(("")).equals(("")));
assert(flipCase(("Hello!")).equals(("hELLO!")));
assert(flipCase(("These violent delights have violent ends")).equals(("tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS")));
}
}
|
humaneval-HumanEval_27_flip_case.json-L17 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// For a given string, flip lowercase characters to uppercase and uppercase to lowercase.
// >>> flipCase(("Hello"))
// ("hELLO")
public static String flipCase(String string) {
String answer = "";
for (int i = 0; i < string.length(); i++) {
if (Character.isLowerCase(string.charAt(i))) {
answer += Character.toUpperCase(string.charAt(i)); | answer += Character.toLowerCase(string.charAt(i));
}
}
return answer;
}
} | } else { | }
public static void main(String[] args) {
assert(flipCase(("")).equals(("")));
assert(flipCase(("Hello!")).equals(("hELLO!")));
assert(flipCase(("These violent delights have violent ends")).equals(("tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS")));
}
}
|
humaneval-HumanEval_33_sort_third.json-L27 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// This function takes an array array list l and returns an array array list l' such that
// l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal
// to the values of the corresponding indicies of l, but sorted.
// >>> sortThird((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))))
// (new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))
// >>> sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)2l))))
// (new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)5l)))
public static ArrayList<Long> sortThird(ArrayList<Long> l) {
ArrayList<Long> l_r = new ArrayList<Long>();
ArrayList<Long> l_e = new ArrayList<Long>();
ArrayList<Long> l_s = new ArrayList<Long>();
for (int i = 0; i < l.size(); i++) {
if (i % 3 == 0) {
l_e.add(l.get(i));
} else {
l_r.add(l.get(i));
}
} | long min = l_e.get(i);
for (int j = i; j < l_e.size(); j++) {
if (l_e.get(j) < min) {
min = l_e.get(j);
l_e.set(j, l_e.get(i));
l_e.set(i, min);
}
}
}
int j = 0;
for (int i = 0; i < l.size(); i++) {
if (i % 3 == 0) {
l_s.add(l_e.get(j));
j++;
} else {
l_s.add(l_r.get(i - j));
}
}
return l_s;
}
} | for (int i = 0; i < l_e.size(); i++) { | }
public static void main(String[] args) {
assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)5l)))));
assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)8l, (long)3l, (long)4l, (long)6l, (long)9l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)8l, (long)3l, (long)4l, (long)6l, (long)9l, (long)5l)))));
assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)9l, (long)4l, (long)8l, (long)3l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)9l, (long)4l, (long)8l, (long)3l, (long)5l)))));
assert(sortThird((new ArrayList<Long>(Arrays.asList((long)5l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)2l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)3l, (long)4l, (long)8l, (long)9l, (long)5l, (long)1l)))));
}
}
|
humaneval-HumanEval_141_file_name_check.json-L29 | py | def file_name_check(file_name: str) -> str:
"""Create a function which takes a string representing a file's name, and returns
'Yes' if the the file's name is valid, and returns 'No' otherwise.
A file's name is considered to be valid if and only if all the following conditions
are met:
- There should not be more than three digits ('0'-'9') in the file's name.
- The file's name contains exactly one dot '.'
- The substring before the dot should not be empty, and it starts with a letter from
the latin alphapet ('a'-'z' and 'A'-'Z').
- The substring after the dot should be one of these: ['txt', 'exe', 'dll']
Examples:
>>> file_name_check('example.txt')
'Yes'
>>> file_name_check('1example.dll')
'No'
"""
if not file_name:
return 'No'
s_count = 0
e_count = 0
for i in file_name:
if i.isdigit():
s_count += 1
if i == '.':
e_count += 1
if s_count > 3 or e_count != 1:
return 'No'
s_name = file_name.split('.')[0] | if not s_name or not e_name or not s_name[0].isalpha():
return 'No'
if e_name not in ['txt', 'exe', 'dll']:
return 'No'
return 'Yes'
| e_name = file_name.split('.')[1] | def check(candidate):
assert candidate('example.txt') == 'Yes'
assert candidate('1example.dll') == 'No'
assert candidate('s1sdf3.asd') == 'No'
assert candidate('K.dll') == 'Yes'
assert candidate('MY16FILE3.exe') == 'Yes'
assert candidate('His12FILE94.exe') == 'No'
assert candidate('_Y.txt') == 'No'
assert candidate('?aREYA.exe') == 'No'
assert candidate('/this_is_valid.dll') == 'No'
assert candidate('this_is_valid.wow') == 'No'
assert candidate('this_is_valid.txt') == 'Yes'
assert candidate('this_is_valid.txtexe') == 'No'
assert candidate('#this2_i4s_5valid.ten') == 'No'
assert candidate('@this1_is6_valid.exe') == 'No'
assert candidate('this_is_12valid.6exe4.txt') == 'No'
assert candidate('all.exe.txt') == 'No'
assert candidate('I563_No.exe') == 'Yes'
assert candidate('Is3youfault.txt') == 'Yes'
assert candidate('no_one#knows.dll') == 'Yes'
assert candidate('1I563_Yes3.exe') == 'No'
assert candidate('I563_Yes3.txtt') == 'No'
assert candidate('final..txt') == 'No'
assert candidate('final132') == 'No'
assert candidate('_f4indsartal132.') == 'No'
assert candidate('.txt') == 'No'
assert candidate('s.') == 'No'
def test_check():
check(file_name_check)
test_check()
|
humaneval-HumanEval_118_get_closest_vowel.json-L32 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given a 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_152_compare.json-L16 | js | //I think we all remember that feeling when the result of some long-awaited
// event is finally known. The feelings and thoughts you have at that moment are
// definitely worth noting down and comparing.
// Your task is to determine if a person correctly guessed the results of a number of matches.
// You are given two arrays of scores and guesses of equal length, where each index shows a match.
// Return an array of the same length denoting how far off each guess was. If they have guessed correctly,
// the value is 0, and if not, the value is the absolute difference between the guess and the score.
// example:
// >>> compare([1, 2, 3, 4, 5, 1], [1, 2, 3, 4, 2, -2])
// [0, 0, 0, 0, 3, 3]
// >>> compare([0, 5, 0, 0, 0, 4], [4, 1, 1, 0, 0, -2])
// [4, 4, 1, 0, 0, 6]
function compare(game, guess){
let result = [];
for(let i = 0; i < game.length; i++){ | result.push(0);
} else {
result.push(Math.abs(game[i] - guess[i]));
}
}
return result;
}
| if(game[i] === guess[i]){ | const assert = require('node:assert');
function test() {
let candidate = compare;
assert.deepEqual(candidate([1, 2, 3, 4, 5, 1], [1, 2, 3, 4, 2, -2]),[0, 0, 0, 0, 3, 3]);
assert.deepEqual(candidate([0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]),[0, 0, 0, 0, 0, 0]);
assert.deepEqual(candidate([1, 2, 3], [-1, -2, -3]),[2, 4, 6]);
assert.deepEqual(candidate([1, 2, 3, 5], [-1, 2, 3, 4]),[2, 0, 0, 1]);
}
test(); |
humaneval-HumanEval_144_simplify.json-L29 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// 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-L68 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given a positive integer, obtain its roman numeral equivalent as a string,
// and return it in lowercase.
// Restrictions: 1 <= num <= 1000
// Examples:
// >>> intToMiniRoman((19l))
// ("xix")
// >>> intToMiniRoman((152l))
// ("clii")
// >>> intToMiniRoman((426l))
// ("cdxxvi")
public static String intToMiniRoman(long number) {
int num = (int) number;
String res = "";
if (num == 0)
return "";
while (num >= 1000) {
res += "m";
num -= 1000;
}
if (num >= 900) {
res += "cm";
num -= 900;
}
if (num >= 500) {
res += "d";
num -= 500;
}
if (num >= 400) {
res += "cd";
num -= 400;
}
while (num >= 100) {
res += "c";
num -= 100;
}
if (num >= 90) {
res += "xc";
num -= 90;
}
if (num >= 50) {
res += "l";
num -= 50;
}
if (num >= 40) {
res += "xl";
num -= 40;
}
while (num >= 10) {
res += "x";
num -= 10;
}
if (num >= 9) {
res += "ix";
num -= 9;
}
if (num >= 5) {
res += "v";
num -= 5;
} | res += "iv";
num -= 4;
}
while (num >= 1) {
res += "i";
num -= 1;
}
return res;
}
} | if (num >= 4) { | }
public static void main(String[] args) {
assert(intToMiniRoman((19l)).equals(("xix")));
assert(intToMiniRoman((152l)).equals(("clii")));
assert(intToMiniRoman((251l)).equals(("ccli")));
assert(intToMiniRoman((426l)).equals(("cdxxvi")));
assert(intToMiniRoman((500l)).equals(("d")));
assert(intToMiniRoman((1l)).equals(("i")));
assert(intToMiniRoman((4l)).equals(("iv")));
assert(intToMiniRoman((43l)).equals(("xliii")));
assert(intToMiniRoman((90l)).equals(("xc")));
assert(intToMiniRoman((94l)).equals(("xciv")));
assert(intToMiniRoman((532l)).equals(("dxxxii")));
assert(intToMiniRoman((900l)).equals(("cm")));
assert(intToMiniRoman((994l)).equals(("cmxciv")));
assert(intToMiniRoman((1000l)).equals(("m")));
}
}
|
humaneval-HumanEval_65_circular_shift.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 {
// 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.
// >>> circularShift((12l), (1l))
// ("21")
// >>> circularShift((12l), (2l))
// ("12")
public static String circularShift(long x, long shift) {
String digits = String.valueOf(x);
if (shift > digits.length()) { | }
return (digits.substring(digits.length() - (int)shift, digits.length()) + digits.substring(0, digits.length() - (int)shift));
}
} | return new StringBuilder(digits).reverse().toString(); | }
public static void main(String[] args) {
assert(circularShift((100l), (2l)).equals(("001")));
assert(circularShift((12l), (2l)).equals(("12")));
assert(circularShift((97l), (8l)).equals(("79")));
assert(circularShift((12l), (1l)).equals(("21")));
assert(circularShift((11l), (101l)).equals(("11")));
}
}
|
humaneval-HumanEval_81_numerical_letter_grade.json-L66 | js | //It is the last week of the semester and the teacher has to give the grades
// to students. The teacher has been making her own algorithm for grading.
// The only problem is, she has lost the code she used for grading.
// She has given you an array of GPAs for some students and you have to write
// a function that can output an array of letter grades using the following table:
// GPA | Letter grade
// 4.0 A+
// > 3.7 A
// > 3.3 A-
// > 3.0 B+
// > 2.7 B
// > 2.3 B-
// > 2.0 C+
// > 1.7 C
// > 1.3 C-
// > 1.0 D+
// > 0.7 D
// > 0.0 D-
// 0.0 E
// Example:
// >>> grade_equation([4.0, 3, 1.7, 2, 3.5])
// ["A+", "B", "C-", "C", "A-"]
function numerical_letter_grade(grades){
var grades_array = [];
for (var i = 0; i < grades.length; i++){
if (grades[i] >= 4.0){
grades_array.push("A+");
}
else if (grades[i] > 3.7){
grades_array.push("A");
}
else if (grades[i] > 3.3){
grades_array.push("A-");
}
else if (grades[i] > 3.0){
grades_array.push("B+");
}
else if (grades[i] > 2.7){
grades_array.push("B");
}
else if (grades[i] > 2.3){
grades_array.push("B-");
}
else if (grades[i] > 2.0){
grades_array.push("C+");
}
else if (grades[i] > 1.7){
grades_array.push("C");
}
else if (grades[i] > 1.3){
grades_array.push("C-");
}
else if (grades[i] > 1.0){
grades_array.push("D+");
}
else if (grades[i] > 0.7){
grades_array.push("D");
}
else if (grades[i] > 0.0){
grades_array.push("D-");
}
else if (grades[i] == 0.0){
grades_array.push("E");
}
} | } | return grades_array; | const assert = require('node:assert');
function test() {
let candidate = numerical_letter_grade;
assert.deepEqual(candidate([4.0, 3, 1.7, 2, 3.5]),["A+", "B", "C-", "C", "A-"]);
assert.deepEqual(candidate([1.2]),["D+"]);
assert.deepEqual(candidate([0.5]),["D-"]);
assert.deepEqual(candidate([0.0]),["E"]);
assert.deepEqual(candidate([1.0, 0.3, 1.5, 2.8, 3.3]),["D", "D-", "C-", "B", "B+"]);
assert.deepEqual(candidate([0.0, 0.7]),["E", "D-"]);
}
test(); |
humaneval-HumanEval_6_parse_nested_parens.json-L9 | 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; | 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);
maxlevel = 0;
level = 0;
}
}
result.push(maxlevel);
return result;
}
| var result = []; | 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_72_will_it_fly.json-L17 | 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 balanced = true;
for(let i = 0; i < q.length; i++){
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;
} | let sum = 0; | 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_109_move_one_ball.json-L34 | py | from typing import List
def move_one_ball(arr: List[int]) -> bool:
"""We have an array 'arr' of N integers arr[1], arr[2], ..., arr[N].The
numbers in the array will be randomly ordered. Your task is to determine if
it is possible to get an array sorted in non-decreasing order by performing
the following operation on the given array:
You are allowed to perform right shift operation any number of times.
One right shift operation means shifting all elements of the array by one
position in the right direction. The last element of the array will be moved to
the starting position in the array i.e. 0th index.
If it is possible to obtain the sorted array by performing the above operation
then return True else return False.
If the given array is empty then return True.
Note: The given list is guaranteed to have unique elements.
For Example:
>>> move_one_ball([3, 4, 5, 1, 2])
True
Explanation: By performin 2 right shift operations, non-decreasing order can
be achieved for the given array.
>>> move_one_ball([3, 5, 4, 1, 2])
False
Explanation:It is not possible to get non-decreasing order for the given
array by performing any number of right shift operations.
"""
flag = False
for i in range(1, len(arr)): | if flag == True:
return False
flag = True
elif arr[i-1] < arr[i]:
flag = False
return True
| if arr[i-1] > arr[i]: | def check(candidate):
assert candidate([3, 4, 5, 1, 2]) == True
assert candidate([3, 5, 10, 1, 2]) == True
assert candidate([4, 3, 1, 2]) == False
assert candidate([3, 5, 4, 1, 2]) == False
assert candidate([]) == True
def test_check():
check(move_one_ball)
test_check()
|
humaneval-HumanEval_137_compare_one.json-L35 | py | from typing import Union
def compare_one(a: Union[int, float, str], b: Union[int, float, str]) -> Union[int, float, str, None]:
"""
Create a function that takes integers, floats, or strings representing
real numbers, and returns the larger variable in its given variable type.
Return None if the values are equal.
Note: If a real number is represented as a string, the floating point might be . or ,
>>> compare_one(1, 2.5)
2.5
>>> compare_one(1, '2,3')
'2,3'
>>> compare_one('5,1', '6')
'6'
>>> compare_one('1', 1)
None
"""
if isinstance(a, int) and isinstance(b, int):
if a > b:
return a
elif a < b:
return b
else:
return None
elif isinstance(a, float) and isinstance(b, float):
if a > b:
return a
elif a < b:
return b
else:
return None
elif isinstance(a, str) and isinstance(b, str):
a_tmp = a.replace(',', '.') | if float(a_tmp) > float(b_tmp):
return a
elif float(a_tmp) < float(b_tmp):
return b
else:
return None
elif isinstance(a, int) and isinstance(b, float):
if a > b:
return a
elif a < b:
return b
else:
return None
elif isinstance(a, float) and isinstance(b, int):
if a > b:
return a
elif a < b:
return b
else:
return None
elif isinstance(a, str) and isinstance(b, int):
a_tmp = a.replace(',', '.')
if float(a_tmp) > b:
return a
elif float(a_tmp) < b:
return b
else:
return None
elif isinstance(a, int) and isinstance(b, str):
b_tmp = b.replace(',', '.')
if a > float(b_tmp):
return a
elif a < float(b_tmp):
return b
else:
return None
elif isinstance(a, str) and isinstance(b, float):
a_tmp = a.replace(',', '.')
if float(a_tmp) > b:
return a
elif float(a_tmp) < b:
return b | b_tmp = b.replace(',', '.') | def check(candidate):
assert candidate(1, 2) == 2
assert candidate(1, 2.5) == 2.5
assert candidate(2, 3) == 3
assert candidate(5, 6) == 6
assert candidate(1, '2,3') == '2,3'
assert candidate('5,1', '6') == '6'
assert candidate('1', '2') == '2'
assert candidate('1', 1) == None
def test_check():
check(compare_one)
test_check()
|
humaneval-HumanEval_119_match_parens.json-L15 | js | //You are given an array of two strings, both strings consist of open
// parentheses '(' or close parentheses ')' only.
// Your job is to check if it is possible to concatenate the two strings in
// some order, that the resulting string will be good.
// A string S is considered to be good if and only if all parentheses in S
// are balanced. For example: the string '(())()' is good, while the string
// '())' is not.
// Return 'Yes' if there's a way to make a good string, and return 'No' otherwise.
// Examples:
// >>> match_parens(["()(", ")"])
// "Yes"
// >>> match_parens([")", ")"])
// "No"
function match_parens(lst){
| const close = ')';
const is_good = (s) => {
let stack = [];
for (let p of s){
if (p === open){
stack.push(p);
} else if (p === close){
if (stack.length === 0){
return false;
}
stack.pop();
}
}
return stack.length === 0;
};
const str1 = lst[0];
const str2 = lst[1];
if (is_good(str1 + str2)){
return 'Yes';
}
if (is_good(str2 + str1)){
return 'Yes';
}
return 'No';
} | const open = '('; | const assert = require('node:assert');
function test() {
let candidate = match_parens;
assert.deepEqual(candidate(["()(", ")"]),"Yes");
assert.deepEqual(candidate([")", ")"]),"No");
assert.deepEqual(candidate(["(()(())", "())())"]),"No");
assert.deepEqual(candidate([")())", "(()()("]),"Yes");
assert.deepEqual(candidate(["(())))", "(()())(("]),"Yes");
assert.deepEqual(candidate(["()", "())"]),"No");
assert.deepEqual(candidate(["(()(", "()))()"]),"Yes");
assert.deepEqual(candidate(["((((", "((())"]),"No");
assert.deepEqual(candidate([")(()", "(()("]),"No");
assert.deepEqual(candidate([")(", ")("]),"No");
assert.deepEqual(candidate(["(", ")"]),"Yes");
assert.deepEqual(candidate([")", "("]),"Yes");
}
test(); |
humaneval-HumanEval_156_int_to_mini_roman.json-L59 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given a positive integer, obtain its roman numeral equivalent as a string,
// and return it in lowercase.
// Restrictions: 1 <= num <= 1000
// Examples:
// >>> intToMiniRoman((19l))
// ("xix")
// >>> intToMiniRoman((152l))
// ("clii")
// >>> intToMiniRoman((426l))
// ("cdxxvi")
public static String intToMiniRoman(long number) {
int num = (int) number;
String res = "";
if (num == 0)
return "";
while (num >= 1000) {
res += "m";
num -= 1000;
}
if (num >= 900) {
res += "cm";
num -= 900;
}
if (num >= 500) {
res += "d";
num -= 500;
}
if (num >= 400) {
res += "cd";
num -= 400;
}
while (num >= 100) {
res += "c";
num -= 100;
}
if (num >= 90) {
res += "xc";
num -= 90;
}
if (num >= 50) {
res += "l";
num -= 50;
}
if (num >= 40) {
res += "xl";
num -= 40;
}
while (num >= 10) {
res += "x";
num -= 10; | if (num >= 9) {
res += "ix";
num -= 9;
}
if (num >= 5) {
res += "v";
num -= 5;
}
if (num >= 4) {
res += "iv";
num -= 4;
}
while (num >= 1) {
res += "i";
num -= 1;
}
return res;
}
} | } | }
public static void main(String[] args) {
assert(intToMiniRoman((19l)).equals(("xix")));
assert(intToMiniRoman((152l)).equals(("clii")));
assert(intToMiniRoman((251l)).equals(("ccli")));
assert(intToMiniRoman((426l)).equals(("cdxxvi")));
assert(intToMiniRoman((500l)).equals(("d")));
assert(intToMiniRoman((1l)).equals(("i")));
assert(intToMiniRoman((4l)).equals(("iv")));
assert(intToMiniRoman((43l)).equals(("xliii")));
assert(intToMiniRoman((90l)).equals(("xc")));
assert(intToMiniRoman((94l)).equals(("xciv")));
assert(intToMiniRoman((532l)).equals(("dxxxii")));
assert(intToMiniRoman((900l)).equals(("cm")));
assert(intToMiniRoman((994l)).equals(("cmxciv")));
assert(intToMiniRoman((1000l)).equals(("m")));
}
}
|
humaneval-HumanEval_31_is_prime.json-L18 | js | //Return true if a given number is prime, and false otherwise.
// >>> is_prime(6)
// false
// >>> is_prime(101)
// true
// >>> is_prime(11)
// true
// >>> is_prime(13441)
// true
// >>> is_prime(61)
// true
// >>> is_prime(4)
// false
// >>> is_prime(1)
// false
function is_prime(n){
if (n==1) { | }
if (n==2) {
return true;
}
if (n%2==0) {
return false;
}
var limit = Math.sqrt(n);
for (var i = 3; i <= limit; i += 2) {
if (n%i==0) {
return false;
}
}
return true;
}
| return false; | const assert = require('node:assert');
function test() {
let candidate = is_prime;
assert.deepEqual(candidate(6),false);
assert.deepEqual(candidate(101),true);
assert.deepEqual(candidate(11),true);
assert.deepEqual(candidate(13441),true);
assert.deepEqual(candidate(61),true);
assert.deepEqual(candidate(4),false);
assert.deepEqual(candidate(1),false);
assert.deepEqual(candidate(5),true);
assert.deepEqual(candidate(11),true);
assert.deepEqual(candidate(17),true);
assert.deepEqual(candidate(85),false);
assert.deepEqual(candidate(77),false);
assert.deepEqual(candidate(255379),false);
}
test(); |
humaneval-HumanEval_69_search.json-L29 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given a non-empty array list of positive integers. Return the greatest integer that is greater than
// zero, and has a frequency greater than or equal to the value of the integer itself.
// The frequency of an integer is the number of times it appears in the array list.
// If no such a value exist, return -1.
// Examples:
// >>> search((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)2l, (long)2l, (long)3l, (long)1l))))
// (2l)
// >>> search((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)2l, (long)3l, (long)3l, (long)3l, (long)4l, (long)4l, (long)4l))))
// (3l)
// >>> search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)4l, (long)4l, (long)4l))))
// (-1l)
public static long search(ArrayList<Long> lst) {
HashMap<Long, Long> map = new HashMap<Long, Long>();
Long max = (long)-1;
for (int i = 0; i < lst.size(); i++) {
long val = lst.get(i);
if (map.containsKey(val)) {
map.put(val, map.get(val) + 1);
} else {
map.put(val, (long)1); | if (map.get(val) >= val && val > max) {
max = val;
}
}
return max;
}
} | } | }
public static void main(String[] args) {
assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)5l, (long)5l, (long)1l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l, (long)4l, (long)1l, (long)4l, (long)4l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l, (long)8l)))) == (8l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)3l, (long)2l, (long)2l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)7l, (long)8l, (long)8l, (long)4l, (long)8l, (long)7l, (long)3l, (long)9l, (long)6l, (long)5l, (long)10l, (long)4l, (long)3l, (long)6l, (long)7l, (long)1l, (long)7l, (long)4l, (long)10l, (long)8l, (long)1l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)2l, (long)8l, (long)2l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)7l, (long)1l, (long)8l, (long)8l, (long)10l, (long)5l, (long)8l, (long)5l, (long)3l, (long)10l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)3l, (long)6l, (long)5l, (long)6l, (long)4l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)9l, (long)6l, (long)7l, (long)1l, (long)4l, (long)7l, (long)1l, (long)8l, (long)8l, (long)9l, (long)8l, (long)10l, (long)10l, (long)8l, (long)4l, (long)10l, (long)4l, (long)10l, (long)1l, (long)2l, (long)9l, (long)5l, (long)7l, (long)9l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)1l, (long)9l, (long)10l, (long)1l, (long)3l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)6l, (long)9l, (long)7l, (long)5l, (long)8l, (long)7l, (long)5l, (long)3l, (long)7l, (long)5l, (long)10l, (long)10l, (long)3l, (long)6l, (long)10l, (long)2l, (long)8l, (long)6l, (long)5l, (long)4l, (long)9l, (long)5l, (long)3l, (long)10l)))) == (5l));
assert(search((new ArrayList<Long>(Arrays.asList((long)1l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)8l, (long)8l, (long)10l, (long)6l, (long)4l, (long)3l, (long)5l, (long)8l, (long)2l, (long)4l, (long)2l, (long)8l, (long)4l, (long)6l, (long)10l, (long)4l, (long)2l, (long)1l, (long)10l, (long)2l, (long)1l, (long)1l, (long)5l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)10l, (long)4l, (long)8l, (long)2l, (long)10l, (long)5l, (long)1l, (long)2l, (long)9l, (long)5l, (long)5l, (long)6l, (long)3l, (long)8l, (long)6l, (long)4l, (long)10l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)1l, (long)6l, (long)10l, (long)1l, (long)6l, (long)9l, (long)10l, (long)8l, (long)6l, (long)8l, (long)7l, (long)3l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)2l, (long)4l, (long)1l, (long)5l, (long)1l, (long)5l, (long)2l, (long)5l, (long)7l, (long)7l, (long)7l, (long)3l, (long)10l, (long)1l, (long)5l, (long)4l, (long)2l, (long)8l, (long)4l, (long)1l, (long)9l, (long)10l, (long)7l, (long)10l, (long)2l, (long)8l, (long)10l, (long)9l, (long)4l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)2l, (long)6l, (long)4l, (long)2l, (long)8l, (long)7l, (long)5l, (long)6l, (long)4l, (long)10l, (long)4l, (long)6l, (long)3l, (long)7l, (long)8l, (long)8l, (long)3l, (long)1l, (long)4l, (long)2l, (long)2l, (long)10l, (long)7l)))) == (4l));
assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)8l, (long)6l, (long)10l, (long)2l, (long)6l, (long)10l, (long)2l, (long)7l, (long)8l, (long)10l, (long)3l, (long)8l, (long)2l, (long)6l, (long)2l, (long)3l, (long)1l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)5l, (long)3l, (long)9l, (long)5l, (long)6l, (long)3l, (long)2l, (long)8l, (long)5l, (long)6l, (long)10l, (long)10l, (long)6l, (long)8l, (long)4l, (long)10l, (long)7l, (long)7l, (long)10l, (long)8l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)10l)))) == (-1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)9l, (long)7l, (long)7l, (long)2l, (long)4l, (long)7l, (long)2l, (long)10l, (long)9l, (long)7l, (long)5l, (long)7l, (long)2l)))) == (2l));
assert(search((new ArrayList<Long>(Arrays.asList((long)5l, (long)4l, (long)10l, (long)2l, (long)1l, (long)1l, (long)10l, (long)3l, (long)6l, (long)1l, (long)8l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)7l, (long)9l, (long)9l, (long)9l, (long)3l, (long)4l, (long)1l, (long)5l, (long)9l, (long)1l, (long)2l, (long)1l, (long)1l, (long)10l, (long)7l, (long)5l, (long)6l, (long)7l, (long)6l, (long)7l, (long)7l, (long)6l)))) == (1l));
assert(search((new ArrayList<Long>(Arrays.asList((long)3l, (long)10l, (long)10l, (long)9l, (long)2l)))) == (-1l));
}
}
|
humaneval-HumanEval_114_minSubArraySum.json-L33 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given an array array list of integers nums, find the minimum sum of any non-empty sub-array array list
// of nums.
// Example
// >>> minSubArraySum((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)1l, (long)2l, (long)4l))))
// (1l)
// >>> minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l))))
// (-6l)
public static long minSubArraySum(ArrayList<Long> nums) {
long minSum = Long.MAX_VALUE;
long prevMinSum = Long.MAX_VALUE;
for (int i = 0; i < nums.size(); i++) {
long currSum = nums.get(i);
if (currSum < prevMinSum) {
prevMinSum = currSum;
}
if (currSum < minSum) {
minSum = currSum;
}
for (int j = i + 1; j < nums.size(); j++) {
currSum += nums.get(j);
if (currSum < prevMinSum) {
prevMinSum = currSum;
}
if (currSum < minSum) { | }
}
}
return minSum;
}
} | minSum = currSum; | }
public static void main(String[] args) {
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)1l, (long)2l, (long)4l)))) == (1l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l)))) == (-6l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l, (long)2l, (long)-10l)))) == (-14l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-9999999999999999l)))) == (-9999999999999999l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)0l, (long)10l, (long)20l, (long)1000000l)))) == (0l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)-3l, (long)10l, (long)-5l)))) == (-6l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)100l, (long)-1l, (long)-2l, (long)-3l, (long)10l, (long)-5l)))) == (-6l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)10l, (long)11l, (long)13l, (long)8l, (long)3l, (long)4l)))) == (3l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)100l, (long)-33l, (long)32l, (long)-1l, (long)0l, (long)-2l)))) == (-33l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)-10l)))) == (-10l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)7l)))) == (7l));
assert(minSubArraySum((new ArrayList<Long>(Arrays.asList((long)1l, (long)-1l)))) == (-1l));
}
}
|
humaneval-HumanEval_86_anti_shuffle.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 {
// Write a function that takes a string and returns an ordered version of it.
// Ordered version of string, is a string where all words (separated by space)
// are replaced by a new word where all the characters arranged in
// ascending order based on ascii value.
// Note: You should keep the order of words and blank spaces in the sentence.
// For example:
// >>> antiShuffle(("Hi"))
// ("Hi")
// >>> antiShuffle(("hello"))
// ("ehllo")
// >>> antiShuffle(("Hello World!!!"))
// ("Hello !!!Wdlor")
public static String antiShuffle(String s) {
StringBuilder sb = new StringBuilder();
String[] words = s.split(" ");
for (String word : words) { | Arrays.sort(chars);
sb.append(new String(chars));
sb.append(" ");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
} | char[] chars = word.toCharArray(); | }
public static void main(String[] args) {
assert(antiShuffle(("Hi")).equals(("Hi")));
assert(antiShuffle(("hello")).equals(("ehllo")));
assert(antiShuffle(("number")).equals(("bemnru")));
assert(antiShuffle(("abcd")).equals(("abcd")));
assert(antiShuffle(("Hello World!!!")).equals(("Hello !!!Wdlor")));
assert(antiShuffle(("")).equals(("")));
assert(antiShuffle(("Hi. My name is Mister Robot. How are you?")).equals((".Hi My aemn is Meirst .Rboot How aer ?ouy")));
}
}
|
humaneval-HumanEval_56_correct_bracketing.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 {
// brackets is a string of "<" and ">".
// return true if every opening bracket has a corresponding closing bracket.
// >>> correctBracketing(("<"))
// (false)
// >>> correctBracketing(("<>"))
// (true)
// >>> correctBracketing(("<<><>>"))
// (true)
// >>> correctBracketing(("><<>"))
// (false)
public static boolean correctBracketing(String brackets) {
int open = 0;
for (int i = 0; i < brackets.length(); i++) {
char c = brackets.charAt(i);
if (c == '<') {
open++;
} else {
open--;
}
if (open < 0) {
return false;
} | return open == 0;
}
} | } | }
public static void main(String[] args) {
assert(correctBracketing(("<>")) == (true));
assert(correctBracketing(("<<><>>")) == (true));
assert(correctBracketing(("<><><<><>><>")) == (true));
assert(correctBracketing(("<><><<<><><>><>><<><><<>>>")) == (true));
assert(correctBracketing(("<<<><>>>>")) == (false));
assert(correctBracketing(("><<>")) == (false));
assert(correctBracketing(("<")) == (false));
assert(correctBracketing(("<<<<")) == (false));
assert(correctBracketing((">")) == (false));
assert(correctBracketing(("<<>")) == (false));
assert(correctBracketing(("<><><<><>><>><<>")) == (false));
assert(correctBracketing(("<><><<><>><>>><>")) == (false));
}
}
|
humaneval-HumanEval_59_largest_prime_factor.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 {
// Return the largest prime factor of n. Assume n > 1 and is not a prime.
// >>> largestPrimeFactor((13195l))
// (29l)
// >>> largestPrimeFactor((2048l))
// (2l)
public static long largestPrimeFactor(long n) {
List<Long> primes = new ArrayList<>();
long x = n;
for (long i = 2; i * i <= x; i++) {
if (x % i == 0) {
primes.add(i);
x /= i;
i--;
}
}
if (x > 1) {
primes.add(x);
} | }
} | return primes.get(primes.size() - 1); | }
public static void main(String[] args) {
assert(largestPrimeFactor((15l)) == (5l));
assert(largestPrimeFactor((27l)) == (3l));
assert(largestPrimeFactor((63l)) == (7l));
assert(largestPrimeFactor((330l)) == (11l));
assert(largestPrimeFactor((13195l)) == (29l));
}
}
|
humaneval-HumanEval_36_fizz_buzz.json-L10 | js | //Return the number of times the digit 7 appears in integers less than n which are divisible by 11 or 13.
// >>> fizz_buzz(50)
// 0
// >>> fizz_buzz(78)
// 2
// >>> fizz_buzz(79)
// 3
function fizz_buzz(n){
let result = 0; | if(i % 11 === 0 || i % 13 === 0){
let arr = i.toString().split('');
for(let j = 0; j < arr.length; j++){
if(arr[j] === '7'){
result++;
}
}
}
}
return result;
}
| for(let i = 0; i < n; i++){ | const assert = require('node:assert');
function test() {
let candidate = fizz_buzz;
assert.deepEqual(candidate(50),0);
assert.deepEqual(candidate(78),2);
assert.deepEqual(candidate(79),3);
assert.deepEqual(candidate(100),3);
assert.deepEqual(candidate(200),6);
assert.deepEqual(candidate(4000),192);
assert.deepEqual(candidate(10000),639);
assert.deepEqual(candidate(100000),8026);
}
test(); |
humaneval-HumanEval_31_is_prime.json-L20 | py | def is_prime(n: int) -> bool:
"""Return true if a given number is prime, and false otherwise.
>>> is_prime(6)
False
>>> is_prime(101)
True
>>> is_prime(11)
True
>>> is_prime(13441)
True
>>> is_prime(61)
True
>>> is_prime(4)
False
>>> is_prime(1)
False
"""
if n <= 1:
return False | if n % i == 0:
return False
return True
| for i in range(2, n//2 + 1): | def check(candidate):
assert candidate(6) == False
assert candidate(101) == True
assert candidate(11) == True
assert candidate(13441) == True
assert candidate(61) == True
assert candidate(4) == False
assert candidate(1) == False
assert candidate(5) == True
assert candidate(11) == True
assert candidate(17) == True
assert candidate(85) == False
assert candidate(77) == False
assert candidate(255379) == False
def test_check():
check(is_prime)
test_check()
|
humaneval-HumanEval_46_fib4.json-L22 | 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:
return 2 | return 0
else:
return fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4)
| elif n == 3: | 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_97_multiply.json-L15 | js | //Complete the function that takes two integers and returns
// the product of their unit digits.
// Assume the input is always valid.
// Examples:
// >>> multiply(148, 412)
// 16
// >>> multiply(19, 28)
// 72
// >>> multiply(2020, 1851)
// 0
// >>> multiply(14, -15)
// 20
function multiply(a, b){
let aStr = a.toString() | return parseInt(aStr[aStr.length - 1]) * parseInt(bStr[bStr.length - 1])
}
| let bStr = b.toString() | const assert = require('node:assert');
function test() {
let candidate = multiply;
assert.deepEqual(candidate(148, 412),16);
assert.deepEqual(candidate(19, 28),72);
assert.deepEqual(candidate(2020, 1851),0);
assert.deepEqual(candidate(14, -15),20);
assert.deepEqual(candidate(76, 67),42);
assert.deepEqual(candidate(17, 27),49);
assert.deepEqual(candidate(0, 1),0);
assert.deepEqual(candidate(0, 0),0);
}
test(); |
humaneval-HumanEval_56_correct_bracketing.json-L18 | js | //brackets is a string of "<" and ">".
// return true if every opening bracket has a corresponding closing bracket.
// >>> correct_bracketing("<")
// false
// >>> correct_bracketing("<>")
// true
// >>> correct_bracketing("<<><>>")
// true
// >>> correct_bracketing("><<>")
// false
function correct_bracketing(brackets){
var opens = 0;
for (var i = 0; i < brackets.length; i++) {
if (brackets[i] === "<") {
opens++;
}
else if (brackets[i] === ">") { | }
if (opens < 0) {
return false;
}
}
return opens === 0;
}
| opens--; | 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_106_f.json-L14 | js | //Implement the function f that takes n as a parameter,
// and returns an array of size n, such that the value of the element at index i is the factorial of i if i is even
// or the sum of numbers from 1 to i otherwise.
// i starts from 1.
// the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i).
// Example:
// >>> f(5)
// [1, 2, 6, 24, 15]
function f(n){
var result = [];
var sum = 0;
var factorial = 1;
for(var i = 1; i <= n; i++){ | for(var j = 1; j <= i; j++){
factorial *= j;
}
result.push(factorial);
factorial = 1;
}else{
for(var k = 1; k <= i; k++){
sum += k;
}
result.push(sum);
sum = 0;
}
}
return result;
} | if(i % 2 == 0){ | const assert = require('node:assert');
function test() {
let candidate = f;
assert.deepEqual(candidate(5),[1, 2, 6, 24, 15]);
assert.deepEqual(candidate(7),[1, 2, 6, 24, 15, 720, 28]);
assert.deepEqual(candidate(1),[1]);
assert.deepEqual(candidate(3),[1, 2, 6]);
}
test(); |
humaneval-HumanEval_160_do_algebra.json-L38 | 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; | break;
case '**':
operand[count] = Math.pow(operand[count], operand[count + 1]);
operand.splice(count + 1, 1);
operator.splice(count, 1);
count = 0;
do_again = true;
break;
default:
count++;
do_again = false;
}
}
answer = operand[0];
operand.splice(0, 1);
while(operand.length > 0){
switch(operator[0]){
case '+':
answer += operand[0];
break;
case '-':
answer -= operand[0];
break;
}
operand.splice(0, 1);
operator.splice(0, 1);
}
return answer;
}
| do_again = true; | 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_61_correct_bracketing.json-L19 | 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){ | }
else {
return false;
}
}
}
if (opens.length == 0){
return true;
}
else {
return false;
}
} | opens.pop(); | 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_139_special_factorial.json-L18 | py | def special_factorial(n: int) -> int:
"""The Brazilian factorial is defined as:
brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1!
where n > 0
For example:
>>> special_factorial(4)
288
The function will receive an integer as input and should return the special
factorial of this integer.
"""
def factorial(n):
if n <= 1:
return 1
else:
return n * factorial(n-1) | return 1
else:
return n * special_factorial(n-1) * factorial(n-1)
| if n <= 1: | def check(candidate):
assert candidate(4) == 288
assert candidate(5) == 34560
assert candidate(7) == 125411328000
assert candidate(1) == 1
def test_check():
check(special_factorial)
test_check()
|
humaneval-HumanEval_111_histogram.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 {
// Given a string representing a space separated lowercase letters, return a hash map
// of the letter with the most repetition and containing the corresponding count.
// If several letters have the same occurrence, return all of them.
// Example:
// >>> histogram(("a b c"))
// (new HashMap<String,Long>(Map.of("a", 1l, "b", 1l, "c", 1l)))
// >>> histogram(("a b b a"))
// (new HashMap<String,Long>(Map.of("a", 2l, "b", 2l)))
// >>> histogram(("a b c a b"))
// (new HashMap<String,Long>(Map.of("a", 2l, "b", 2l)))
// >>> histogram(("b b b b a"))
// (new HashMap<String,Long>(Map.of("b", 4l)))
// >>> histogram((""))
// (new HashMap<String,Long>())
public static HashMap<String,Long> histogram(String test) {
HashMap<String,Long> s = new HashMap<String,Long>();
if (test.length() > 0) {
HashMap<String,Long> hm = new HashMap<String,Long>(); | for (String sss : ss) {
if (hm.containsKey(sss)) {
hm.put(sss, hm.get(sss) + 1);
} else {
hm.put(sss, 1l);
}
}
long max = 0;
for (String key : hm.keySet()) {
if (hm.get(key) > max) {
max = hm.get(key);
}
}
for (String key : hm.keySet()) {
if (hm.get(key) == max) {
s.put(key, hm.get(key));
}
}
}
return s;
}
} | String[] ss = test.split(" "); | }
public static void main(String[] args) {
assert(histogram(("a b b a")).equals((new HashMap<String,Long>(Map.of("a", 2l, "b", 2l)))));
assert(histogram(("a b c a b")).equals((new HashMap<String,Long>(Map.of("a", 2l, "b", 2l)))));
assert(histogram(("a b c d g")).equals((new HashMap<String,Long>(Map.of("a", 1l, "b", 1l, "c", 1l, "d", 1l, "g", 1l)))));
assert(histogram(("r t g")).equals((new HashMap<String,Long>(Map.of("r", 1l, "t", 1l, "g", 1l)))));
assert(histogram(("b b b b a")).equals((new HashMap<String,Long>(Map.of("b", 4l)))));
assert(histogram(("r t g")).equals((new HashMap<String,Long>(Map.of("r", 1l, "t", 1l, "g", 1l)))));
assert(histogram(("")).equals((new HashMap<String,Long>())));
assert(histogram(("a")).equals((new HashMap<String,Long>(Map.of("a", 1l)))));
}
}
|
humaneval-HumanEval_110_exchange.json-L24 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// In this 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++) { | 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";
}
} | if (lst1.get(i) % 2 == 1) { | }
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")));
}
}
|