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_0_has_close_elements.json-L13 | py | from typing import List
def has_close_elements(numbers: List[float], threshold: float) -> bool:
""" Check if in given list of numbers, are any two numbers closer to each other than
given threshold.
>>> has_close_elements([1.0, 2.0, 3.0], 0.5)
False
>>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)
True
"""
for i in range(len(numbers)):
for j in range(i + 1, len(numbers)): | return True
return False
| if abs(numbers[i] - numbers[j]) < threshold: | def check(candidate):
assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.3) == True
assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.05) == False
assert candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.95) == True
assert candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.8) == False
assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0], 0.1) == True
assert candidate([1.1, 2.2, 3.1, 4.1, 5.1], 1.0) == True
assert candidate([1.1, 2.2, 3.1, 4.1, 5.1], 0.5) == False
def test_check():
check(has_close_elements)
test_check()
|
humaneval-HumanEval_33_sort_third.json-L12 | js | //This function takes an array l and returns an array l' such that
// l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal
// to the values of the corresponding indicies of l, but sorted.
// >>> sort_third([1, 2, 3])
// [1, 2, 3]
// >>> sort_third([5, 6, 3, 4, 8, 9, 2])
// [2, 6, 3, 4, 8, 9, 5]
function sort_third(l){
let x=[];
let y=[];
let k=0; | if(i%3==0){
y[k]=l[i];
k=k+1;
}
else{
x[i]=l[i];
}
}
y.sort();
k=0;
for(let i=0;i<l.length;i++){
if(i%3==0){
x[i]=y[k];
k=k+1;
}
}
return x;
} | for(let i=0;i<l.length;i++){ | const assert = require('node:assert');
function test() {
let candidate = sort_third;
assert.deepEqual(candidate([5, 6, 3, 4, 8, 9, 2]),[2, 6, 3, 4, 8, 9, 5]);
assert.deepEqual(candidate([5, 8, 3, 4, 6, 9, 2]),[2, 8, 3, 4, 6, 9, 5]);
assert.deepEqual(candidate([5, 6, 9, 4, 8, 3, 2]),[2, 6, 9, 4, 8, 3, 5]);
assert.deepEqual(candidate([5, 6, 3, 4, 8, 9, 2, 1]),[2, 6, 3, 4, 8, 9, 5, 1]);
}
test(); |
humaneval-HumanEval_68_pluck.json-L42 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// "Given an array array list representing a branch of a tree that has non-negative integer nodes
// your task is to pluck one of the nodes and return it.
// The plucked node should be the node with the smallest even value.
// If multiple nodes with the same smallest even value are found return the node that has smallest index.
// The plucked node should be returned in an array array list, [ smalest_value, its index ],
// If there are no even values or the given array array list is empty, return [].
// Example 1:
// >>> pluck((new ArrayList<Long>(Arrays.asList((long)4l, (long)2l, (long)3l))))
// (new ArrayList<Long>(Arrays.asList((long)2l, (long)1l)))
// Explanation: 2 has the smallest even value, and 2 has the smallest index.
// Example 2:
// >>> pluck((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))))
// (new ArrayList<Long>(Arrays.asList((long)2l, (long)1l)))
// Explanation: 2 has the smallest even value, and 2 has the smallest index.
// Example 3:
// >>> pluck((new ArrayList<Long>(Arrays.asList())))
// (new ArrayList<Long>(Arrays.asList()))
// Example 4:
// >>> pluck((new ArrayList<Long>(Arrays.asList((long)5l, (long)0l, (long)3l, (long)0l, (long)4l, (long)2l))))
// (new ArrayList<Long>(Arrays.asList((long)0l, (long)1l)))
// Explanation: 0 is the smallest value, but there are two zeros,
// so we will choose the first zero, which has the smallest index.
// Constraints:
// * 1 <= nodes.length <= 10000
// * 0 <= node.value
public static ArrayList<Long> pluck(ArrayList<Long> arr) {
int minIndex = -1;
long minValue = -1;
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) % 2 == 0) {
if (minIndex == -1 || arr.get(i) < minValue) {
minIndex = i;
minValue = arr.get(i); | }
}
ArrayList<Long> newArr = new ArrayList<Long>();
if (minIndex == -1) {
return newArr;
}
newArr.add(minValue);
newArr.add((long)minIndex);
return newArr;
}
} | } | }
public static void main(String[] args) {
assert(pluck((new ArrayList<Long>(Arrays.asList((long)4l, (long)2l, (long)3l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)1l)))));
assert(pluck((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)1l)))));
assert(pluck((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<Long>(Arrays.asList()))));
assert(pluck((new ArrayList<Long>(Arrays.asList((long)5l, (long)0l, (long)3l, (long)0l, (long)4l, (long)2l)))).equals((new ArrayList<Long>(Arrays.asList((long)0l, (long)1l)))));
assert(pluck((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)0l, (long)5l, (long)3l)))).equals((new ArrayList<Long>(Arrays.asList((long)0l, (long)3l)))));
assert(pluck((new ArrayList<Long>(Arrays.asList((long)5l, (long)4l, (long)8l, (long)4l, (long)8l)))).equals((new ArrayList<Long>(Arrays.asList((long)4l, (long)1l)))));
assert(pluck((new ArrayList<Long>(Arrays.asList((long)7l, (long)6l, (long)7l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)6l, (long)1l)))));
assert(pluck((new ArrayList<Long>(Arrays.asList((long)7l, (long)9l, (long)7l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList()))));
}
}
|
humaneval-HumanEval_122_add_elements.json-L18 | py | from typing import List
def add_elements(arr: List[int], k: int) -> int:
"""
Given a non-empty array of integers arr and an integer k, return
the sum of the elements with at most two digits from the first k elements of arr.
Example:
>>> add_elements([111, 21, 3, 4000, 5, 6, 7, 8, 9], 4)
24
Constraints:
1. 1 <= len(arr) <= 100
2. 1 <= k <= len(arr)
"""
total = 0 | if num < 100:
total += num
return total
| for num in arr[:k]: | def check(candidate):
assert candidate([1, -2, -3, 41, 57, 76, 87, 88, 99], 3) == -4
assert candidate([111, 121, 3, 4000, 5, 6], 2) == 0
assert candidate([11, 21, 3, 90, 5, 6, 7, 8, 9], 4) == 125
assert candidate([111, 21, 3, 4000, 5, 6, 7, 8, 9], 4) == 24
assert candidate([1], 1) == 1
def test_check():
check(add_elements)
test_check()
|
humaneval-HumanEval_59_largest_prime_factor.json-L14 | js | //Return the largest prime factor of n. Assume n > 1 and is not a prime.
// >>> largest_prime_factor(13195)
// 29
// >>> largest_prime_factor(2048)
// 2
function largest_prime_factor(n){
var result = 2;
while(n > result){
if(n % result == 0){
n = n / result;
}
else{
result = result + 1; | }
return result;
} | } | const assert = require('node:assert');
function test() {
let candidate = largest_prime_factor;
assert.deepEqual(candidate(15),5);
assert.deepEqual(candidate(27),3);
assert.deepEqual(candidate(63),7);
assert.deepEqual(candidate(330),11);
assert.deepEqual(candidate(13195),29);
}
test(); |
humaneval-HumanEval_81_numerical_letter_grade.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 {
// It is the last week of the semester and the teacher has to give the grades
// to students. The teacher has been making her own algorithm for grading.
// The only problem is, she has lost the code she used for grading.
// She has given you an array array list of GPAs for some students and you have to write
// a function that can output an array array list of letter grades using the following table:
// GPA | Letter grade
// 4.0 A+
// > 3.7 A
// > 3.3 A-
// > 3.0 B+
// > 2.7 B
// > 2.3 B-
// > 2.0 C+
// > 1.7 C
// > 1.3 C-
// > 1.0 D+
// > 0.7 D
// > 0.0 D-
// 0.0 E
// Example:
// >>> gradeEquation((new ArrayList<Float>(Arrays.asList((float)4.0f, (float)3l, (float)1.7f, (float)2l, (float)3.5f))))
// (new ArrayList<String>(Arrays.asList((String)"A+", (String)"B", (String)"C-", (String)"C", (String)"A-")))
public static ArrayList<String> numericalLetterGrade(ArrayList<Float> grades) {
ArrayList<String> letterGrades = new ArrayList<String>();
for (int i = 0; i < grades.size(); i++) {
float currentGrade = grades.get(i);
if (currentGrade == 4.0f) {
letterGrades.add("A+");
} else if (currentGrade > 3.7f) {
letterGrades.add("A");
} else if (currentGrade > 3.3f) {
letterGrades.add("A-");
} else if (currentGrade > 3.0f) {
letterGrades.add("B+");
} else if (currentGrade > 2.7f) {
letterGrades.add("B");
} else if (currentGrade > 2.3f) {
letterGrades.add("B-");
} else if (currentGrade > 2.0f) {
letterGrades.add("C+"); | letterGrades.add("C");
} else if (currentGrade > 1.3f) {
letterGrades.add("C-");
} else if (currentGrade > 1.0f) {
letterGrades.add("D+");
} else if (currentGrade > 0.7f) {
letterGrades.add("D");
} else if (currentGrade > 0.0f) {
letterGrades.add("D-");
} else {
letterGrades.add("E");
}
}
return letterGrades;
}
} | } else if (currentGrade > 1.7f) { | }
public static void main(String[] args) {
assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)4.0f, (float)3l, (float)1.7f, (float)2l, (float)3.5f)))).equals((new ArrayList<String>(Arrays.asList((String)"A+", (String)"B", (String)"C-", (String)"C", (String)"A-")))));
assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)1.2f)))).equals((new ArrayList<String>(Arrays.asList((String)"D+")))));
assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.5f)))).equals((new ArrayList<String>(Arrays.asList((String)"D-")))));
assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.0f)))).equals((new ArrayList<String>(Arrays.asList((String)"E")))));
assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)1.0f, (float)0.3f, (float)1.5f, (float)2.8f, (float)3.3f)))).equals((new ArrayList<String>(Arrays.asList((String)"D", (String)"D-", (String)"C-", (String)"B", (String)"B+")))));
assert(numericalLetterGrade((new ArrayList<Float>(Arrays.asList((float)0.0f, (float)0.7f)))).equals((new ArrayList<String>(Arrays.asList((String)"E", (String)"D-")))));
}
}
|
humaneval-HumanEval_129_minPath.json-L68 | js | //Given a grid with N rows and N columns (N >= 2) and a positive integer k,
// each cell of the grid contains a value. Every integer in the range [1, N * N]
// inclusive appears exactly once on the cells of the grid.
// You have to find the minimum path of length k in the grid. You can start
// from any cell, and in each step you can move to any of the neighbor cells,
// in other words, you can go to cells which share an edge with you current
// cell.
// Please note that a path of length k means visiting exactly k cells (not
// necessarily distinct).
// You CANNOT go off the grid.
// A path A (of length k) is considered less than a path B (of length k) if
// after making the ordered arrays of the values on the cells that A and B go
// through (let's call them lst_A and lst_B), lst_A is lexicographically less
// than lst_B, in other words, there exist an integer index i (1 <= i <= k)
// such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have
// lst_A[j] = lst_B[j].
// It is guaranteed that the answer is unique.
// Return an ordered array of the values on the cells that the minimum path go through.
// Examples:
// >>> minPath([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3)
// [1, 2, 1]
// >>> minPath([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1)
// [1]
function minPath(grid, k){
let n = grid.length;
let path = [];
let startCell = grid[0][0];
for(let i = 0; i < n; i++){
for(let j = 0; j < n; j++){
let curr = grid[i][j];
if(curr < startCell){
startCell = curr;
}
}
}
let currCell = startCell;
for(let l = 0; l < k; l++){
path.push(currCell);
let neighbours = [];
let x = null;
let y = null;
for(let i = 0; i < n; i++){
for(let j = 0; j < n; j++){
if(grid[i][j] === currCell){
x = i;
y = j;
}
}
}
if(x > 0){
neighbours.push(grid[x - 1][y]);
}
if(x < n - 1){
neighbours.push(grid[x + 1][y]);
}
if(y > 0){
neighbours.push(grid[x][y - 1]);
}
if(y < n - 1){
neighbours.push(grid[x][y + 1]);
}
let nextCell = neighbours[0];
for(let i = 0; i < neighbours.length; i++){
let curr = neighbours[i];
if(curr < nextCell){
nextCell = curr;
} | currCell = nextCell;
}
return path;
} | } | 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_13_greatest_common_divisor.json-L8 | js | //Return a greatest common divisor of two integers a and b
// >>> greatest_common_divisor(3, 5)
// 1
// >>> greatest_common_divisor(25, 15)
// 5
function greatest_common_divisor(a, b){
let max = Math.max(a, b) | if (max % min == 0) {
return min
} else {
return greatest_common_divisor(min, max % min)
}
}
| let min = Math.min(a, b) | const assert = require('node:assert');
function test() {
let candidate = greatest_common_divisor;
assert.deepEqual(candidate(3, 7),1);
assert.deepEqual(candidate(10, 15),5);
assert.deepEqual(candidate(49, 14),7);
assert.deepEqual(candidate(144, 60),12);
}
test(); |
humaneval-HumanEval_13_greatest_common_divisor.json-L9 | py | def greatest_common_divisor(a: int, b: int) -> int:
""" Return a greatest common divisor of two integers a and b
>>> greatest_common_divisor(3, 5)
1
>>> greatest_common_divisor(25, 15)
5
"""
if a > b: | if a == 0:
return b
return greatest_common_divisor(b % a, a)
| a, b = b, a | def check(candidate):
assert candidate(3, 7) == 1
assert candidate(10, 15) == 5
assert candidate(49, 14) == 7
assert candidate(144, 60) == 12
def test_check():
check(greatest_common_divisor)
test_check()
|
humaneval-HumanEval_73_smallest_change.json-L18 | js | //Given an array arr of integers, find the minimum number of elements that
// need to be changed to make the array palindromic. A palindromic array is an array that
// is read the same backwards and forwards. In one change, you can change one element to any other element.
// For example:
// >>> smallest_change([1, 2, 3, 5, 4, 7, 9, 6])
// 4
// >>> smallest_change([1, 2, 3, 4, 3, 2, 2])
// 1
// >>> smallest_change([1, 2, 3, 2, 1])
// 0
function smallest_change(arr){
let changes = 0;
for(let i = 0; i < arr.length/2; i++){
if(arr[i] != arr[arr.length - 1 - i]){
changes++;
}
} | } | return changes; | const assert = require('node:assert');
function test() {
let candidate = smallest_change;
assert.deepEqual(candidate([1, 2, 3, 5, 4, 7, 9, 6]),4);
assert.deepEqual(candidate([1, 2, 3, 4, 3, 2, 2]),1);
assert.deepEqual(candidate([1, 4, 2]),1);
assert.deepEqual(candidate([1, 4, 4, 2]),1);
assert.deepEqual(candidate([1, 2, 3, 2, 1]),0);
assert.deepEqual(candidate([3, 1, 1, 3]),0);
assert.deepEqual(candidate([1]),0);
assert.deepEqual(candidate([0, 1]),1);
}
test(); |
humaneval-HumanEval_134_check_if_last_char_is_a_letter.json-L18 | py | def check_if_last_char_is_a_letter(txt: str) -> bool:
"""
Create a function that returns True if the last character
of a given string is an alphabetical character and is not
a part of a word, and False otherwise.
Note: "word" is a group of characters separated by space.
Examples:
>>> check_if_last_char_is_a_letter('apple pie')
False
>>> check_if_last_char_is_a_letter('apple pi e')
True
>>> check_if_last_char_is_a_letter('apple pi e ')
False
>>> check_if_last_char_is_a_letter('')
False
"""
| return False
elif txt[-1].isalpha() and (len(txt) == 1 or not txt[-2].isalpha()):
return True
else:
return False
| if len(txt) == 0: | def check(candidate):
assert candidate('apple') == False
assert candidate('apple pi e') == True
assert candidate('eeeee') == False
assert candidate('A') == True
assert candidate('Pumpkin pie ') == False
assert candidate('Pumpkin pie 1') == False
assert candidate('') == False
assert candidate('eeeee e ') == False
assert candidate('apple pie') == False
assert candidate('apple pi e ') == False
def test_check():
check(check_if_last_char_is_a_letter)
test_check()
|
humaneval-HumanEval_93_encode.json-L24 | py | def encode(message: str) -> str:
"""
Write a function that takes a message, and encodes in such a
way that it swaps case of all letters, replaces all vowels in
the message with the letter that appears 2 places ahead of that
vowel in the english alphabet.
Assume only letters.
Examples:
>>> encode('test')
'TGST'
>>> encode('This is a message')
'tHKS KS C MGSSCGG'
"""
def swap_case(ch: str) -> str:
if ch.isupper():
return ch.lower()
else:
return ch.upper()
def encode_vowel(ch: str) -> str:
if ch.lower() in 'aeiou':
if ch.lower() in 'wxyz':
return ch.lower() | return ch
return ''.join(map(lambda ch: encode_vowel(swap_case(ch)), message))
| return chr(ord(ch) + 2) | def check(candidate):
assert candidate('TEST') == 'tgst'
assert candidate('Mudasir') == 'mWDCSKR'
assert candidate('YES') == 'ygs'
assert candidate('This is a message') == 'tHKS KS C MGSSCGG'
assert candidate('I DoNt KnOw WhAt tO WrItE') == 'k dQnT kNqW wHcT Tq wRkTg'
def test_check():
check(encode)
test_check()
|
humaneval-HumanEval_106_f.json-L11 | 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 factorial = 1;
for(var i = 1; i <= n; i++){
if(i % 2 == 0){
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;
} | var sum = 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_85_add.json-L20 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given a non-empty array list of integers lst. add the even elements that are at odd indices..
// Examples:
// >>> add((new ArrayList<Long>(Arrays.asList((long)4l, (long)2l, (long)6l, (long)7l))))
// (2l)
public static long add(ArrayList<Long> lst) {
long sum = 0;
for (int i = 0; i < lst.size(); i++) {
if (i % 2 == 1 && lst.get(i) % 2 == 0) {
sum += lst.get(i);
}
} | }
} | return sum; | }
public static void main(String[] args) {
assert(add((new ArrayList<Long>(Arrays.asList((long)4l, (long)88l)))) == (88l));
assert(add((new ArrayList<Long>(Arrays.asList((long)4l, (long)5l, (long)6l, (long)7l, (long)2l, (long)122l)))) == (122l));
assert(add((new ArrayList<Long>(Arrays.asList((long)4l, (long)0l, (long)6l, (long)7l)))) == (0l));
assert(add((new ArrayList<Long>(Arrays.asList((long)4l, (long)4l, (long)6l, (long)8l)))) == (12l));
}
}
|
humaneval-HumanEval_40_triples_sum_to_zero.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 {
// 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; | while (j < k) {
long currSum = sortedList.get(i) + sortedList.get(j) + sortedList.get(k);
if (currSum == 0) {
return true;
} else if (currSum < 0) {
j++;
} else {
k--;
}
}
}
return false;
}
} | int k = sortedList.size() - 1; | }
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_56_correct_bracketing.json-L18 | py | def correct_bracketing(brackets: str) -> bool:
""" 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
"""
num_left_brackets = 0
for bracket in brackets:
if bracket == '<':
num_left_brackets += 1 | num_left_brackets -= 1
if num_left_brackets < 0:
return False
return num_left_brackets == 0
| elif bracket == '>': | def check(candidate):
assert candidate('<>') == True
assert candidate('<<><>>') == True
assert candidate('<><><<><>><>') == True
assert candidate('<><><<<><><>><>><<><><<>>>') == True
assert candidate('<<<><>>>>') == False
assert candidate('><<>') == False
assert candidate('<') == False
assert candidate('<<<<') == False
assert candidate('>') == False
assert candidate('<<>') == False
assert candidate('<><><<><>><>><<>') == False
assert candidate('<><><<><>><>>><>') == False
def test_check():
check(correct_bracketing)
test_check()
|
humaneval-HumanEval_39_prime_fib.json-L48 | js | //prime_fib returns n-th number that is a Fibonacci number and it's also prime.
// >>> prime_fib(1)
// 2
// >>> prime_fib(2)
// 3
// >>> prime_fib(3)
// 5
// >>> prime_fib(4)
// 13
// >>> prime_fib(5)
// 89
function prime_fib(n){
function isPrime(number){
if(number <= 3){
return number > 1;
}
else if(number % 2 == 0 || number % 3 == 0){
return false;
}
else{
for(var i = 5; i * i <= number; i += 6){
if(number % i == 0 || number % (i + 2) == 0){
return false;
}
}
return true;
}
}
function fib(num){
var a = 1, b = 0, temp;
while (num >= 0){
temp = a;
a = a + b;
b = temp;
num--;
}
return b;
}
var res = 0;
var fibNum = 0;
while(res < n){
if(isPrime(fib(fibNum))){
res++;
}
fibNum++; | return fib(fibNum - 1);
} | } | const assert = require('node:assert');
function test() {
let candidate = prime_fib;
assert.deepEqual(candidate(1),2);
assert.deepEqual(candidate(2),3);
assert.deepEqual(candidate(3),5);
assert.deepEqual(candidate(4),13);
assert.deepEqual(candidate(5),89);
assert.deepEqual(candidate(6),233);
assert.deepEqual(candidate(7),1597);
assert.deepEqual(candidate(8),28657);
assert.deepEqual(candidate(9),514229);
assert.deepEqual(candidate(10),433494437);
}
test(); |
humaneval-HumanEval_63_fibfib.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 {
// The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
// fibfib(0) == 0
// fibfib(1) == 0
// fibfib(2) == 1
// fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3).
// Please write a function to efficiently compute the n-th element of the fibfib number sequence.
// >>> fibfib((1l))
// (0l)
// >>> fibfib((5l))
// (4l)
// >>> fibfib((8l))
// (24l)
public static long fibfib(long n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 0;
}
if (n == 2) {
return 1;
}
long[] memo = new long[(int) n + 1];
memo[0] = 0; | memo[2] = 1;
for (int i = 3; i <= n; i++) {
memo[i] = memo[i - 1] + memo[i - 2] + memo[i - 3];
}
return memo[(int) n];
}
} | memo[1] = 0; | }
public static void main(String[] args) {
assert(fibfib((2l)) == (1l));
assert(fibfib((1l)) == (0l));
assert(fibfib((5l)) == (4l));
assert(fibfib((8l)) == (24l));
assert(fibfib((10l)) == (81l));
assert(fibfib((12l)) == (274l));
assert(fibfib((14l)) == (927l));
}
}
|
humaneval-HumanEval_128_prod_signs.json-L22 | js | //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 undefined for empty arr.
// Example:
// >>> prod_signs([1, 2, 2, -4])
// 9
// >>> prod_signs([0, 1])
// 0
// >>> prod_signs([])
// undefined
function prod_signs(arr){
var total = 0;
var len = arr.length;
var pro = 1;
if(len == 0){
return undefined;
}
else{
for(var i = 0; i < len; i++){
total += Math.abs(arr[i]); | pro *= -1;
}
else if(arr[i] == 0){
pro *= 0;
}
}
total *= pro;
return total;
}
}
| if(arr[i] < 0){ | const assert = require('node:assert');
function test() {
let candidate = prod_signs;
assert.deepEqual(candidate([1, 2, 2, -4]),-9);
assert.deepEqual(candidate([0, 1]),0);
assert.deepEqual(candidate([1, 1, 1, 2, 3, -1, 1]),-10);
assert.deepEqual(candidate([]),undefined);
assert.deepEqual(candidate([2, 4, 1, 2, -1, -1, 9]),20);
assert.deepEqual(candidate([-1, 1, -1, 1]),4);
assert.deepEqual(candidate([-1, 1, 1, 1]),-4);
assert.deepEqual(candidate([-1, 1, 1, 0]),0);
}
test(); |
humaneval-HumanEval_108_count_nums.json-L34 | js | //Write a function count_nums which takes an array of integers and returns
// the number of elements which has a sum of digits > 0.
// If a number is negative, then its first signed digit will be negative:
// e.g. -123 has signed digits -1, 2, and 3.
// >>> count_nums([])
// 0
// >>> count_nums([-1, 11, -11])
// 1
// >>> count_nums([1, 1, 2])
// 3
function count_nums(arr){
if(!arr.length) {
return 0;
}
var count = 0;
for(var i = 0; i < arr.length; i++) {
var sum = 0;
var str = arr[i].toString();
if(str[0] === '-') {
sum += Number(str[0] + str[1]);
for(var j = 2; j < str.length; j++) {
sum += Number(str[j]);
}
} else {
for(var k = 0; k < str.length; k++) {
sum += Number(str[k]);
}
}
if(sum > 0) {
count++;
}
}
return count; | } | const assert = require('node:assert');
function test() {
let candidate = count_nums;
assert.deepEqual(candidate([]),0);
assert.deepEqual(candidate([-1, -2, 0]),0);
assert.deepEqual(candidate([1, 1, 2, -2, 3, 4, 5]),6);
assert.deepEqual(candidate([1, 6, 9, -6, 0, 1, 5]),5);
assert.deepEqual(candidate([1, 100, 98, -7, 1, -1]),4);
assert.deepEqual(candidate([12, 23, 34, -45, -56, 0]),5);
assert.deepEqual(candidate([0, 1]),1);
assert.deepEqual(candidate([1]),1);
}
test(); |
|
humaneval-HumanEval_104_unique_digits.json-L36 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given an array array list of positive integers x. return a sorted array list of all
// elements that hasn't any even digit.
// Note: Returned array list should be sorted in increasing order.
// For example:
// >>> uniqueDigits((new ArrayList<Long>(Arrays.asList((long)15l, (long)33l, (long)1422l, (long)1l))))
// (new ArrayList<Long>(Arrays.asList((long)1l, (long)15l, (long)33l)))
// >>> uniqueDigits((new ArrayList<Long>(Arrays.asList((long)152l, (long)323l, (long)1422l, (long)10l))))
// (new ArrayList<Long>(Arrays.asList()))
public static ArrayList<Long> uniqueDigits(ArrayList<Long> x) {
ArrayList<Long> b = new ArrayList<Long>();
for (int i = 0; i < x.size(); i++) {
if (x.get(i) < 0) {
x.set(i, x.get(i)*(long)-1);
}
boolean unique = true;
long z = x.get(i);
while (z > 0) {
long y = z % 10;
if (y % 2 == 0) {
unique = false;
break;
}
z = z / 10;
}
if (unique) {
b.add(x.get(i));
} | Collections.sort(b);
return b;
}
} | } | }
public static void main(String[] args) {
assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)15l, (long)33l, (long)1422l, (long)1l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)15l, (long)33l)))));
assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)152l, (long)323l, (long)1422l, (long)10l)))).equals((new ArrayList<Long>(Arrays.asList()))));
assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)12345l, (long)2033l, (long)111l, (long)151l)))).equals((new ArrayList<Long>(Arrays.asList((long)111l, (long)151l)))));
assert(uniqueDigits((new ArrayList<Long>(Arrays.asList((long)135l, (long)103l, (long)31l)))).equals((new ArrayList<Long>(Arrays.asList((long)31l, (long)135l)))));
}
}
|
humaneval-HumanEval_108_count_nums.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 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));
}
} | result++;
}
}
return result;
}
} | if (sum > 0) { | }
public static void main(String[] args) {
assert(countNums((new ArrayList<Long>(Arrays.asList()))) == (0l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-2l, (long)0l)))) == (0l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)2l, (long)-2l, (long)3l, (long)4l, (long)5l)))) == (6l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l, (long)6l, (long)9l, (long)-6l, (long)0l, (long)1l, (long)5l)))) == (5l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l, (long)100l, (long)98l, (long)-7l, (long)1l, (long)-1l)))) == (4l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)12l, (long)23l, (long)34l, (long)-45l, (long)-56l, (long)0l)))) == (5l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)0l, (long)1l)))) == (1l));
assert(countNums((new ArrayList<Long>(Arrays.asList((long)1l)))) == (1l));
}
}
|
humaneval-HumanEval_5_intersperse.json-L16 | py | from typing import List
def intersperse(numbers: List[int], delimeter: int) -> List[int]:
""" Insert a number 'delimeter' between every two consecutive elements of input list `numbers'
>>> intersperse([], 4)
[]
>>> intersperse([1, 2, 3], 4)
[1, 4, 2, 4, 3]
"""
result = []
if len(numbers) == 0:
return result
result.append(numbers[0])
for num in numbers[1:]:
result.append(delimeter) | return result
| result.append(num) | def check(candidate):
assert candidate([], 7) == []
assert candidate([5, 6, 3, 2], 8) == [5, 8, 6, 8, 3, 8, 2]
assert candidate([2, 2, 2], 2) == [2, 2, 2, 2, 2]
def test_check():
check(intersperse)
test_check()
|
humaneval-HumanEval_24_largest_divisor.json-L7 | js | //For a given number n, find the largest number that divides n evenly, smaller than n
// >>> largest_divisor(15)
// 5
function largest_divisor(n){
let ld = 0;
for(i=1; i<n; i++){ | if(i>ld){
ld = i;
}
}
}
return ld;
} | if(n%i===0){ | const assert = require('node:assert');
function test() {
let candidate = largest_divisor;
assert.deepEqual(candidate(3),1);
assert.deepEqual(candidate(7),1);
assert.deepEqual(candidate(10),5);
assert.deepEqual(candidate(100),50);
assert.deepEqual(candidate(49),7);
}
test(); |
humaneval-HumanEval_110_exchange.json-L23 | 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: | count -= 1
if count > 0:
return "NO"
else:
return "YES" | if y % 2 == 0: | 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_93_encode.json-L19 | py | def encode(message: str) -> str:
"""
Write a function that takes a message, and encodes in such a
way that it swaps case of all letters, replaces all vowels in
the message with the letter that appears 2 places ahead of that
vowel in the english alphabet.
Assume only letters.
Examples:
>>> encode('test')
'TGST'
>>> encode('This is a message')
'tHKS KS C MGSSCGG'
"""
def swap_case(ch: str) -> str:
if ch.isupper():
return ch.lower()
else: | def encode_vowel(ch: str) -> str:
if ch.lower() in 'aeiou':
if ch.lower() in 'wxyz':
return ch.lower()
return chr(ord(ch) + 2)
return ch
return ''.join(map(lambda ch: encode_vowel(swap_case(ch)), message))
| return ch.upper() | def check(candidate):
assert candidate('TEST') == 'tgst'
assert candidate('Mudasir') == 'mWDCSKR'
assert candidate('YES') == 'ygs'
assert candidate('This is a message') == 'tHKS KS C MGSSCGG'
assert candidate('I DoNt KnOw WhAt tO WrItE') == 'k dQnT kNqW wHcT Tq wRkTg'
def test_check():
check(encode)
test_check()
|
humaneval-HumanEval_59_largest_prime_factor.json-L8 | 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
"""
| factor = 2
while factor < n:
if n % factor == 0:
largest = factor
while n % factor == 0:
n = n // factor
factor += 1
return max(largest, n)
| largest = 1 | 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_114_minSubArraySum.json-L30 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given an array array list of 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) { | }
if (currSum < minSum) {
minSum = currSum;
}
}
}
return minSum;
}
} | prevMinSum = 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_143_words_in_sentence.json-L42 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given a string representing a sentence,
// the sentence contains some words separated by a space,
// and you have to return a string that contains the words from the original sentence,
// whose lengths are prime numbers,
// the order of the words in the new string should be the same as the original one.
// Example 1:
// >>> wordsInSentence(("This is a test"))
// ("is")
// Example 2:
// >>> wordsInSentence(("lets go for swimming"))
// ("go for")
// Constraints:
// * 1 <= len(sentence) <= 100
// * sentence contains only letters
public static String wordsInSentence(String sentence) {
StringBuilder ans = new StringBuilder();
String[] words = sentence.split(" ");
int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
boolean isPrime;
for (String word : words) {
isPrime = false;
for (int prime : primes) {
if (word.length() == prime) {
isPrime = true;
break;
}
}
if (isPrime) {
ans.append(word + " ");
}
}
if (ans.length() > 0) {
ans.deleteCharAt(ans.length() - 1); | return ans.toString();
}
} | } | }
public static void main(String[] args) {
assert(wordsInSentence(("This is a test")).equals(("is")));
assert(wordsInSentence(("lets go for swimming")).equals(("go for")));
assert(wordsInSentence(("there is no place available here")).equals(("there is no place")));
assert(wordsInSentence(("Hi I am Hussein")).equals(("Hi am Hussein")));
assert(wordsInSentence(("go for it")).equals(("go for it")));
assert(wordsInSentence(("here")).equals(("")));
assert(wordsInSentence(("here is")).equals(("is")));
}
}
|
humaneval-HumanEval_124_valid_date.json-L31 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You 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; | 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;
}
} | try { | }
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_69_search.json-L20 | js | //You are given a non-empty array of positive integers. Return the greatest integer that is greater than
// zero, and has a frequency greater than or equal to the value of the integer itself.
// The frequency of an integer is the number of times it appears in the array.
// If no such a value exist, return -1.
// Examples:
// >>> search([4, 1, 2, 2, 3, 1])
// 2
// >>> search([1, 2, 2, 3, 3, 3, 4, 4, 4])
// 3
// >>> search([5, 5, 4, 4, 4])
// -1
function search(lst){
var h = {};
for (var i=0; i<lst.length; i++){
var x = lst[i];
if (x in h){
h[x] += 1;
}
else{ | }
}
var keys = Object.keys(h);
var max = 0;
for (var i=0; i<keys.length; i++){
if (keys[i] > max && h[keys[i]] >= keys[i]){
max = keys[i];
}
}
if (max == 0){
return -1;
}
return max;
}
| h[x] = 1; | const assert = require('node:assert');
function test() {
let candidate = search;
assert.deepEqual(candidate([5, 5, 5, 5, 1]),1);
assert.deepEqual(candidate([4, 1, 4, 1, 4, 4]),4);
assert.deepEqual(candidate([3, 3]),-1);
assert.deepEqual(candidate([8, 8, 8, 8, 8, 8, 8, 8]),8);
assert.deepEqual(candidate([2, 3, 3, 2, 2]),2);
assert.deepEqual(candidate([2, 7, 8, 8, 4, 8, 7, 3, 9, 6, 5, 10, 4, 3, 6, 7, 1, 7, 4, 10, 8, 1]),1);
assert.deepEqual(candidate([3, 2, 8, 2]),2);
assert.deepEqual(candidate([6, 7, 1, 8, 8, 10, 5, 8, 5, 3, 10]),1);
assert.deepEqual(candidate([8, 8, 3, 6, 5, 6, 4]),-1);
assert.deepEqual(candidate([6, 9, 6, 7, 1, 4, 7, 1, 8, 8, 9, 8, 10, 10, 8, 4, 10, 4, 10, 1, 2, 9, 5, 7, 9]),1);
assert.deepEqual(candidate([1, 9, 10, 1, 3]),1);
assert.deepEqual(candidate([6, 9, 7, 5, 8, 7, 5, 3, 7, 5, 10, 10, 3, 6, 10, 2, 8, 6, 5, 4, 9, 5, 3, 10]),5);
assert.deepEqual(candidate([1]),1);
assert.deepEqual(candidate([8, 8, 10, 6, 4, 3, 5, 8, 2, 4, 2, 8, 4, 6, 10, 4, 2, 1, 10, 2, 1, 1, 5]),4);
assert.deepEqual(candidate([2, 10, 4, 8, 2, 10, 5, 1, 2, 9, 5, 5, 6, 3, 8, 6, 4, 10]),2);
assert.deepEqual(candidate([1, 6, 10, 1, 6, 9, 10, 8, 6, 8, 7, 3]),1);
assert.deepEqual(candidate([9, 2, 4, 1, 5, 1, 5, 2, 5, 7, 7, 7, 3, 10, 1, 5, 4, 2, 8, 4, 1, 9, 10, 7, 10, 2, 8, 10, 9, 4]),4);
assert.deepEqual(candidate([2, 6, 4, 2, 8, 7, 5, 6, 4, 10, 4, 6, 3, 7, 8, 8, 3, 1, 4, 2, 2, 10, 7]),4);
assert.deepEqual(candidate([9, 8, 6, 10, 2, 6, 10, 2, 7, 8, 10, 3, 8, 2, 6, 2, 3, 1]),2);
assert.deepEqual(candidate([5, 5, 3, 9, 5, 6, 3, 2, 8, 5, 6, 10, 10, 6, 8, 4, 10, 7, 7, 10, 8]),-1);
assert.deepEqual(candidate([10]),-1);
assert.deepEqual(candidate([9, 7, 7, 2, 4, 7, 2, 10, 9, 7, 5, 7, 2]),2);
assert.deepEqual(candidate([5, 4, 10, 2, 1, 1, 10, 3, 6, 1, 8]),1);
assert.deepEqual(candidate([7, 9, 9, 9, 3, 4, 1, 5, 9, 1, 2, 1, 1, 10, 7, 5, 6, 7, 6, 7, 7, 6]),1);
assert.deepEqual(candidate([3, 10, 10, 9, 2]),-1);
}
test(); |
humaneval-HumanEval_27_flip_case.json-L9 | py | def flip_case(string: str) -> str:
""" For a given string, flip lowercase characters to uppercase and uppercase to lowercase.
>>> flip_case('Hello')
'hELLO'
"""
new_string = ""
for character in string:
if character.isupper(): | else:
new_string += character.upper()
return new_string
| new_string += character.lower() | def check(candidate):
assert candidate('') == ''
assert candidate('Hello!') == 'hELLO!'
assert candidate('These violent delights have violent ends') == 'tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS'
def test_check():
check(flip_case)
test_check()
|
humaneval-HumanEval_105_by_length.json-L41 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given an array array list of integers, sort the integers that are between 1 and 9 inclusive,
// reverse the resulting array array list, and then replace each digit by its corresponding name from
// "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine".
// For example:
// >>> byLength((new ArrayList<Long>(Arrays.asList((long)2l, (long)1l, (long)1l, (long)4l, (long)5l, (long)8l, (long)2l, (long)3l))))
// (new ArrayList<String>(Arrays.asList((String)"Eight", (String)"Five", (String)"Four", (String)"Three", (String)"Two", (String)"Two", (String)"One", (String)"One")))
// If the array array list is empty, return an empty array array list:
// >>> byLength((new ArrayList<Long>(Arrays.asList())))
// (new ArrayList<String>(Arrays.asList()))
// If the array array list has any strange number ignore it:
// >>> byLength((new ArrayList<Long>(Arrays.asList((long)1l, (long)-1l, (long)55l))))
// (new ArrayList<String>(Arrays.asList((String)"One")))
public static ArrayList<String> byLength(ArrayList<Long> arr) {
String[] a = new String[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
ArrayList<String> res = new ArrayList<String>();
ArrayList<Long> good = new ArrayList<Long>();
for (Long i : arr) {
if (i >= 1 && i <= 9) {
good.add(i);
}
}
good.sort(new Comparator<Long>() {
public int compare(Long i1, Long i2) {
return i1.compareTo(i2);
}
});
Collections.reverse(good);
for (Long i : good) {
res.add(a[i.intValue() - 1]);
}
return res;
} | } | }
public static void main(String[] args) {
assert(byLength((new ArrayList<Long>(Arrays.asList((long)2l, (long)1l, (long)1l, (long)4l, (long)5l, (long)8l, (long)2l, (long)3l)))).equals((new ArrayList<String>(Arrays.asList((String)"Eight", (String)"Five", (String)"Four", (String)"Three", (String)"Two", (String)"Two", (String)"One", (String)"One")))));
assert(byLength((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<String>(Arrays.asList()))));
assert(byLength((new ArrayList<Long>(Arrays.asList((long)1l, (long)-1l, (long)55l)))).equals((new ArrayList<String>(Arrays.asList((String)"One")))));
assert(byLength((new ArrayList<Long>(Arrays.asList((long)1l, (long)-1l, (long)3l, (long)2l)))).equals((new ArrayList<String>(Arrays.asList((String)"Three", (String)"Two", (String)"One")))));
assert(byLength((new ArrayList<Long>(Arrays.asList((long)9l, (long)4l, (long)8l)))).equals((new ArrayList<String>(Arrays.asList((String)"Nine", (String)"Eight", (String)"Four")))));
}
}
|
|
humaneval-HumanEval_19_sort_numbers.json-L16 | js | //Input is a space-delimited string of numberals from 'zero' to 'nine'.
// Valid choices are 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight' and 'nine'.
// Return the string with numbers sorted from smallest to largest
// >>> sort_numbers("three one five")
// "one three five"
function sort_numbers(numbers){
var numString = numbers.split(" ");
var numMap = {
"zero": 0,
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6, | "eight": 8,
"nine": 9
};
numString.sort(function(a, b){
return numMap[a] - numMap[b];
});
numString = numString.join(" ");
return numString;
} | "seven": 7, | const assert = require('node:assert');
function test() {
let candidate = sort_numbers;
assert.deepEqual(candidate(""),"");
assert.deepEqual(candidate("three"),"three");
assert.deepEqual(candidate("three five nine"),"three five nine");
assert.deepEqual(candidate("five zero four seven nine eight"),"zero four five seven eight nine");
assert.deepEqual(candidate("six five four three two one zero"),"zero one two three four five six");
}
test(); |
humaneval-HumanEval_160_do_algebra.json-L33 | 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; | operand[count] = Math.floor(operand[count] / operand[count + 1]);
operand.splice(count + 1, 1);
operator.splice(count, 1);
count = 0;
do_again = true;
break;
case '**':
operand[count] = Math.pow(operand[count], operand[count + 1]);
operand.splice(count + 1, 1);
operator.splice(count, 1);
count = 0;
do_again = true;
break;
default:
count++;
do_again = false;
}
}
answer = operand[0];
operand.splice(0, 1);
while(operand.length > 0){
switch(operator[0]){
case '+':
answer += operand[0];
break;
case '-':
answer -= operand[0];
break;
}
operand.splice(0, 1);
operator.splice(0, 1);
}
return answer;
}
| case '//': | const assert = require('node:assert');
function test() {
let candidate = do_algebra;
assert.deepEqual(candidate(["**", "*", "+"], [2, 3, 4, 5]),37);
assert.deepEqual(candidate(["+", "*", "-"], [2, 3, 4, 5]),9);
assert.deepEqual(candidate(["//", "*"], [7, 3, 4]),8);
}
test(); |
humaneval-HumanEval_142_sum_squares.json-L20 | js | //"
// This function will take an array of integers. For all entries in the array, the function shall square the integer entry if its index is a
// multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not
// change the entries in the array whose indexes are not a multiple of 3 or 4. The function shall then return the sum of all entries.
// Examples:
// >>> lst
// [1, 2, 3]
// >>> lst
// []
// >>> lst
// [-1, -5, 2, -1, -5]
function sum_squares(lst){
var sum = 0;
var len = lst.length;
for (var i = 0; i < len; i++){
if (i % 3 == 0){
sum += lst[i]*lst[i];
} else if (i % 4 == 0){
sum += lst[i]*lst[i]*lst[i]; | sum += lst[i];
}
}
return sum;
} | } else { | const assert = require('node:assert');
function test() {
let candidate = sum_squares;
assert.deepEqual(candidate([1, 2, 3]),6);
assert.deepEqual(candidate([1, 4, 9]),14);
assert.deepEqual(candidate([]),0);
assert.deepEqual(candidate([1, 1, 1, 1, 1, 1, 1, 1, 1]),9);
assert.deepEqual(candidate([-1, -1, -1, -1, -1, -1, -1, -1, -1]),-3);
assert.deepEqual(candidate([0]),0);
assert.deepEqual(candidate([-1, -5, 2, -1, -5]),-126);
assert.deepEqual(candidate([-56, -99, 1, 0, -2]),3030);
assert.deepEqual(candidate([-1, 0, 0, 0, 0, 0, 0, 0, -1]),0);
assert.deepEqual(candidate([-16, -9, -2, 36, 36, 26, -20, 25, -40, 20, -4, 12, -26, 35, 37]),-14196);
assert.deepEqual(candidate([-1, -3, 17, -1, -15, 13, -1, 14, -14, -12, -5, 14, -14, 6, 13, 11, 16, 16, 4, 10]),-1448);
}
test(); |
humaneval-HumanEval_118_get_closest_vowel.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 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++) { | 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 "";
}
} | if (vowels.indexOf(word.charAt(i)) != -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_73_smallest_change.json-L25 | py | from typing import List
def smallest_change(arr: List[int]) -> int:
"""
Given an array arr of integers, find the minimum number of elements that
need to be changed to make the array palindromic. A palindromic array is an array that
is read the same backwards and forwards. In one change, you can change one element to any other element.
For example:
>>> smallest_change([1, 2, 3, 5, 4, 7, 9, 6])
4
>>> smallest_change([1, 2, 3, 4, 3, 2, 2])
1
>>> smallest_change([1, 2, 3, 2, 1])
0
"""
start = 0
end = len(arr) - 1
count = 0
while start < end:
if arr[start] != arr[end]:
count += 1
start += 1
end -= 1 | return count | def check(candidate):
assert candidate([1, 2, 3, 5, 4, 7, 9, 6]) == 4
assert candidate([1, 2, 3, 4, 3, 2, 2]) == 1
assert candidate([1, 4, 2]) == 1
assert candidate([1, 4, 4, 2]) == 1
assert candidate([1, 2, 3, 2, 1]) == 0
assert candidate([3, 1, 1, 3]) == 0
assert candidate([1]) == 0
assert candidate([0, 1]) == 1
def test_check():
check(smallest_change)
test_check()
|
|
humaneval-HumanEval_158_find_max.json-L29 | js | //Write a function that accepts an array of strings.
// The array contains different words. Return the word with maximum number
// of unique characters. If multiple strings have maximum number of unique
// characters, return the one which comes first in lexicographical order.
// >>> find_max(["name", "of", "string"])
// "string"
// >>> find_max(["name", "enam", "game"])
// "enam"
// >>> find_max(["aaaaaaa", "bb", "cc"])
// "aaaaaaa"
function find_max(words){
var max_unique = {};
var max_unique_word = "";
for(var i = 0; i < words.length; i++){
var unique_word = {};
var word = words[i];
for(var j = 0; j < word.length; j++){
var char = word[j];
if(unique_word[char] === undefined){
unique_word[char] = 1;
}
}
if(max_unique_word === ""){
max_unique_word = word;
max_unique = unique_word;
}
else if(Object.keys(unique_word).length > Object.keys(max_unique).length){
max_unique_word = word; | }
else if(Object.keys(unique_word).length === Object.keys(max_unique).length){
if(word < max_unique_word){
max_unique_word = word;
max_unique = unique_word;
}
}
}
return max_unique_word;
} | max_unique = unique_word; | const assert = require('node:assert');
function test() {
let candidate = find_max;
assert.deepEqual(candidate(["name", "of", "string"]),"string");
assert.deepEqual(candidate(["name", "enam", "game"]),"enam");
assert.deepEqual(candidate(["aaaaaaa", "bb", "cc"]),"aaaaaaa");
assert.deepEqual(candidate(["abc", "cba"]),"abc");
assert.deepEqual(candidate(["play", "this", "game", "of", "footbott"]),"footbott");
assert.deepEqual(candidate(["we", "are", "gonna", "rock"]),"gonna");
assert.deepEqual(candidate(["we", "are", "a", "mad", "nation"]),"nation");
assert.deepEqual(candidate(["this", "is", "a", "prrk"]),"this");
assert.deepEqual(candidate(["b"]),"b");
assert.deepEqual(candidate(["play", "play", "play"]),"play");
}
test(); |
humaneval-HumanEval_162_string_to_md5.json-L13 | py | from typing import Optional
def string_to_md5(text: str) -> Optional[str]:
"""
Given a string 'text', return its md5 hash equivalent string.
If 'text' is an empty string, return None.
>>> string_to_md5('Hello world')
'3e25960a79dbc69b674cd4ec67a72c62'
"""
import hashlib
if not text: | else:
return hashlib.md5(text.encode('utf-8')).hexdigest()
| return None | def check(candidate):
assert candidate('Hello world') == '3e25960a79dbc69b674cd4ec67a72c62'
assert candidate('') == None
assert candidate('A B C') == '0ef78513b0cb8cef12743f5aeb35f888'
assert candidate('password') == '5f4dcc3b5aa765d61d8327deb882cf99'
def test_check():
check(string_to_md5)
test_check()
|
humaneval-HumanEval_120_maximum.json-L36 | py | from typing import List
def maximum(arr: List[int], k: int) -> List[int]:
"""
Given an array arr of integers and a positive integer k, return a sorted list
of length k with the maximum k numbers in arr.
Example 1:
>>> maximum([-3, -4, 5], 3)
[-4, -3, 5]
Example 2:
>>> maximum([4, -4, 4], 2)
[4, 4]
Example 3:
>>> maximum([-3, 2, 1, 2, -1, -2, 1], 1)
[2]
Note:
1. The length of the array will be in the range of [1, 1000].
2. The elements in the array will be in the range of [-1000, 1000].
3. 0 <= k <= len(arr)
"""
def get_max(arr: List[int], k: int) -> List[int]:
"""
Returns the maximum k numbers in the given array
"""
if k == 0:
return []
elif k == 1:
return [max(arr)] | max_num = max(arr)
arr.remove(max_num)
return [max_num] + get_max(arr, k - 1)
return sorted(get_max(arr, k))
| else: | def check(candidate):
assert candidate([-3, -4, 5], 3) == [-4, -3, 5]
assert candidate([4, -4, 4], 2) == [4, 4]
assert candidate([-3, 2, 1, 2, -1, -2, 1], 1) == [2]
assert candidate([123, -123, 20, 0, 1, 2, -3], 3) == [2, 20, 123]
assert candidate([-123, 20, 0, 1, 2, -3], 4) == [0, 1, 2, 20]
assert candidate([5, 15, 0, 3, -13, -8, 0], 7) == [-13, -8, 0, 0, 3, 5, 15]
assert candidate([-1, 0, 2, 5, 3, -10], 2) == [3, 5]
assert candidate([1, 0, 5, -7], 1) == [5]
assert candidate([4, -4], 2) == [-4, 4]
assert candidate([-10, 10], 2) == [-10, 10]
assert candidate([1, 2, 3, -23, 243, -400, 0], 0) == []
def test_check():
check(maximum)
test_check()
|
humaneval-HumanEval_81_numerical_letter_grade.json-L45 | 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){ | }
else if (grades[i] > 1.7){
grades_array.push("C");
}
else if (grades[i] > 1.3){
grades_array.push("C-");
}
else if (grades[i] > 1.0){
grades_array.push("D+");
}
else if (grades[i] > 0.7){
grades_array.push("D");
}
else if (grades[i] > 0.0){
grades_array.push("D-");
}
else if (grades[i] == 0.0){
grades_array.push("E");
}
}
return grades_array;
} | grades_array.push("C+"); | 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_89_encrypt.json-L21 | js | //Create a function encrypt that takes a string as an argument and
// returns a string encrypted with the alphabet being rotated.
// The alphabet should be rotated in a manner such that the letters
// shift down by two multiplied to two places.
// For example:
// >>> encrypt("hi")
// "lm"
// >>> encrypt("asdfghjkl")
// "ewhjklnop"
// >>> encrypt("gf")
// "kj"
// >>> encrypt("et")
// "ix"
function encrypt(s){
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var newString = "";
var shiftBy = 2 * 2;
for(var i = 0; i < s.length; i++){
var index = alphabet.indexOf(s[i]);
newString += alphabet[(index + shiftBy) % 26]; | return newString;
}
| } | const assert = require('node:assert');
function test() {
let candidate = encrypt;
assert.deepEqual(candidate("hi"),"lm");
assert.deepEqual(candidate("asdfghjkl"),"ewhjklnop");
assert.deepEqual(candidate("gf"),"kj");
assert.deepEqual(candidate("et"),"ix");
assert.deepEqual(candidate("faewfawefaewg"),"jeiajeaijeiak");
assert.deepEqual(candidate("hellomyfriend"),"lippsqcjvmirh");
assert.deepEqual(candidate("dxzdlmnilfuhmilufhlihufnmlimnufhlimnufhfucufh"),"hbdhpqrmpjylqmpyjlpmlyjrqpmqryjlpmqryjljygyjl");
assert.deepEqual(candidate("a"),"e");
}
test(); |
humaneval-HumanEval_10_make_palindrome.json-L21 | js | //Find the shortest palindrome that begins with a supplied string.
// Algorithm idea is simple:
// - Find the longest postfix of supplied string that is a palindrome.
// - Append to the end of the string reverse of a string prefix that comes before the palindromic suffix.
// >>> make_palindrome("")
// ""
// >>> make_palindrome("cat")
// "catac"
// >>> make_palindrome("cata")
// "catac"
function make_palindrome(string){
var string_length = string.length;
var is_palindrome = function(s){
return s == s.split("").reverse().join("");
};
var i = string_length;
while(i > 0){
if(is_palindrome(string.substring(0, i))){
return string + string.substring(0, string_length - i).split("").reverse().join("");
} | }
return string;
}
| i--; | const assert = require('node:assert');
function test() {
let candidate = make_palindrome;
assert.deepEqual(candidate(""),"");
assert.deepEqual(candidate("x"),"x");
assert.deepEqual(candidate("xyz"),"xyzyx");
assert.deepEqual(candidate("xyx"),"xyx");
assert.deepEqual(candidate("jerry"),"jerryrrej");
}
test(); |
humaneval-HumanEval_116_sort_array.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 {
// In this Kata, you have to sort an array array list of non-negative integers according to
// number of ones in their binary representation in ascending order.
// For similar number of ones, sort based on decimal value.
// It must be implemented like this:
// >>> sortArray((new ArrayList<Long>(Arrays.asList((long)1l, (long)5l, (long)2l, (long)3l, (long)4l))))
// (new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)4l, (long)5l)))
// >>> sortArray((new ArrayList<Long>(Arrays.asList((long)-2l, (long)-3l, (long)-4l, (long)-5l, (long)-6l))))
// (new ArrayList<Long>(Arrays.asList((long)-6l, (long)-5l, (long)-4l, (long)-3l, (long)-2l)))
// >>> sortArray((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)2l, (long)3l, (long)4l))))
// (new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)2l, (long)3l, (long)4l)))
public static ArrayList<Long> sortArray(ArrayList<Long> arr) {
Collections.sort(arr, (a, b) -> {
long a1 = a, b1 = b;
if (a1 < 0) {
a1 = -a1;
}
if (b1 < 0) {
b1 = -b1;
}
return Long.bitCount(a1) == Long.bitCount(b1) ? Long.compare(a, b) : Integer.compare(Long.bitCount(a1), Long.bitCount(b1));
});
return arr; | } | } | }
public static void main(String[] args) {
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)1l, (long)5l, (long)2l, (long)3l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)4l, (long)3l, (long)5l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)-2l, (long)-3l, (long)-4l, (long)-5l, (long)-6l)))).equals((new ArrayList<Long>(Arrays.asList((long)-4l, (long)-2l, (long)-6l, (long)-5l, (long)-3l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)1l, (long)0l, (long)2l, (long)3l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)0l, (long)1l, (long)2l, (long)4l, (long)3l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList()))).equals((new ArrayList<Long>(Arrays.asList()))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)2l, (long)5l, (long)77l, (long)4l, (long)5l, (long)3l, (long)5l, (long)7l, (long)2l, (long)3l, (long)4l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l, (long)4l, (long)4l, (long)3l, (long)3l, (long)5l, (long)5l, (long)5l, (long)7l, (long)77l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)3l, (long)6l, (long)44l, (long)12l, (long)32l, (long)5l)))).equals((new ArrayList<Long>(Arrays.asList((long)32l, (long)3l, (long)5l, (long)6l, (long)12l, (long)44l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)8l, (long)16l, (long)32l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)8l, (long)16l, (long)32l)))));
assert(sortArray((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)8l, (long)16l, (long)32l)))).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)8l, (long)16l, (long)32l)))));
}
}
|
humaneval-HumanEval_27_flip_case.json-L9 | js | //For a given string, flip lowercase characters to uppercase and uppercase to lowercase.
// >>> flip_case("Hello")
// "hELLO"
function flip_case(string){
var result = "";
for(var i = 0; i < string.length; i++){
if(string[i] == string[i].toUpperCase()){
result += string[i].toLowerCase(); | result += string[i].toUpperCase();
}
}
return result;
}
| }else{ | const assert = require('node:assert');
function test() {
let candidate = flip_case;
assert.deepEqual(candidate(""),"");
assert.deepEqual(candidate("Hello!"),"hELLO!");
assert.deepEqual(candidate("These violent delights have violent ends"),"tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS");
}
test(); |
humaneval-HumanEval_147_get_max_triples.json-L22 | 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
count = 0
while (i < n - 2):
while (j < n - 1): | 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
| while (k < n): | 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_74_total_match.json-L24 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Write a function that accepts two array lists of strings and returns the array list that has
// total number of chars in the all strings of the array list less than the other array list.
// if the two array lists have the same number of chars, return the first array list.
// Examples
// >>> totalMatch((new ArrayList<String>(Arrays.asList())), (new ArrayList<String>(Arrays.asList())))
// (new ArrayList<String>(Arrays.asList()))
// >>> totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hI", (String)"Hi"))))
// (new ArrayList<String>(Arrays.asList((String)"hI", (String)"Hi")))
// >>> totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hi", (String)"hi", (String)"admin", (String)"project"))))
// (new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin")))
// >>> totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hI", (String)"hi", (String)"hi"))))
// (new ArrayList<String>(Arrays.asList((String)"hI", (String)"hi", (String)"hi")))
// >>> totalMatch((new ArrayList<String>(Arrays.asList((String)"4"))), (new ArrayList<String>(Arrays.asList((String)"1", (String)"2", (String)"3", (String)"4", (String)"5"))))
// (new ArrayList<String>(Arrays.asList((String)"4")))
public static ArrayList<String> totalMatch(ArrayList<String> lst1, ArrayList<String> lst2) {
| int sum2 = 0;
for (String s : lst1) {
sum1 += s.length();
}
for (String s : lst2) {
sum2 += s.length();
}
if (sum1 <= sum2) {
return lst1;
} else {
return lst2;
}
}
} | int sum1 = 0; | }
public static void main(String[] args) {
assert(totalMatch((new ArrayList<String>(Arrays.asList())), (new ArrayList<String>(Arrays.asList()))).equals((new ArrayList<String>(Arrays.asList()))));
assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hi", (String)"hi")))).equals((new ArrayList<String>(Arrays.asList((String)"hi", (String)"hi")))));
assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hi", (String)"hi", (String)"admin", (String)"project")))).equals((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin")))));
assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"4"))), (new ArrayList<String>(Arrays.asList((String)"1", (String)"2", (String)"3", (String)"4", (String)"5")))).equals((new ArrayList<String>(Arrays.asList((String)"4")))));
assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hI", (String)"Hi")))).equals((new ArrayList<String>(Arrays.asList((String)"hI", (String)"Hi")))));
assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hI", (String)"hi", (String)"hi")))).equals((new ArrayList<String>(Arrays.asList((String)"hI", (String)"hi", (String)"hi")))));
assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin"))), (new ArrayList<String>(Arrays.asList((String)"hI", (String)"hi", (String)"hii")))).equals((new ArrayList<String>(Arrays.asList((String)"hi", (String)"admin")))));
assert(totalMatch((new ArrayList<String>(Arrays.asList())), (new ArrayList<String>(Arrays.asList((String)"this")))).equals((new ArrayList<String>(Arrays.asList()))));
assert(totalMatch((new ArrayList<String>(Arrays.asList((String)"this"))), (new ArrayList<String>(Arrays.asList()))).equals((new ArrayList<String>(Arrays.asList()))));
}
}
|
humaneval-HumanEval_129_minPath.json-L51 | 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){ | }
if(x < n - 1){
neighbours.push(grid[x + 1][y]);
}
if(y > 0){
neighbours.push(grid[x][y - 1]);
}
if(y < n - 1){
neighbours.push(grid[x][y + 1]);
}
let nextCell = neighbours[0];
for(let i = 0; i < neighbours.length; i++){
let curr = neighbours[i];
if(curr < nextCell){
nextCell = curr;
}
}
currCell = nextCell;
}
return path;
} | neighbours.push(grid[x - 1][y]); | const assert = require('node:assert');
function test() {
let candidate = minPath;
assert.deepEqual(candidate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3),[1, 2, 1]);
assert.deepEqual(candidate([[5, 9, 3], [4, 1, 6], [7, 8, 2]], 1),[1]);
assert.deepEqual(candidate([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 4),[1, 2, 1, 2]);
assert.deepEqual(candidate([[6, 4, 13, 10], [5, 7, 12, 1], [3, 16, 11, 15], [8, 14, 9, 2]], 7),[1, 10, 1, 10, 1, 10, 1]);
assert.deepEqual(candidate([[8, 14, 9, 2], [6, 4, 13, 15], [5, 7, 1, 12], [3, 10, 11, 16]], 5),[1, 7, 1, 7, 1]);
assert.deepEqual(candidate([[11, 8, 7, 2], [5, 16, 14, 4], [9, 3, 15, 6], [12, 13, 10, 1]], 9),[1, 6, 1, 6, 1, 6, 1, 6, 1]);
assert.deepEqual(candidate([[12, 13, 10, 1], [9, 3, 15, 6], [5, 16, 14, 4], [11, 8, 7, 2]], 12),[1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6]);
assert.deepEqual(candidate([[2, 7, 4], [3, 1, 5], [6, 8, 9]], 8),[1, 3, 1, 3, 1, 3, 1, 3]);
assert.deepEqual(candidate([[6, 1, 5], [3, 8, 9], [2, 7, 4]], 8),[1, 5, 1, 5, 1, 5, 1, 5]);
assert.deepEqual(candidate([[1, 2], [3, 4]], 10),[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]);
assert.deepEqual(candidate([[1, 3], [3, 2]], 10),[1, 3, 1, 3, 1, 3, 1, 3, 1, 3]);
}
test(); |
humaneval-HumanEval_142_sum_squares.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 {
// "
// This function will take an array array list of integers. For all entries in the array list, the function shall square the integer entry if its index is a
// multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not
// change the entries in the array list whose indexes are not a multiple of 3 or 4. The function shall then return the sum of all entries.
// Examples:
// >>> lst
// (long)new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l))
// >>> lst
// (long)new ArrayList<Long>(Arrays.asList())
// >>> lst
// (long)new ArrayList<Long>(Arrays.asList((long)-1l, (long)-5l, (long)2l, (long)-1l, (long)-5l))
public static long sumSquares(ArrayList<Long> lst) {
int sz = lst.size();
for (int i = 0; i < sz; i++) {
if (i % 3 == 0) {
lst.set(i, lst.get(i) * lst.get(i));
} else if (i % 4 == 0) {
lst.set(i, lst.get(i) * lst.get(i) * lst.get(i));
}
}
long ans = 0;
for (int i = 0; i < sz; i++) {
ans += lst.get(i);
} | }
} | return ans; | }
public static void main(String[] args) {
assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l)))) == (6l));
assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)1l, (long)4l, (long)9l)))) == (14l));
assert(sumSquares((new ArrayList<Long>(Arrays.asList()))) == (0l));
assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l, (long)1l)))) == (9l));
assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l, (long)-1l)))) == (-3l));
assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)0l)))) == (0l));
assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-5l, (long)2l, (long)-1l, (long)-5l)))) == (-126l));
assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-56l, (long)-99l, (long)1l, (long)0l, (long)-2l)))) == (3030l));
assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)0l, (long)-1l)))) == (0l));
assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-16l, (long)-9l, (long)-2l, (long)36l, (long)36l, (long)26l, (long)-20l, (long)25l, (long)-40l, (long)20l, (long)-4l, (long)12l, (long)-26l, (long)35l, (long)37l)))) == (-14196l));
assert(sumSquares((new ArrayList<Long>(Arrays.asList((long)-1l, (long)-3l, (long)17l, (long)-1l, (long)-15l, (long)13l, (long)-1l, (long)14l, (long)-14l, (long)-12l, (long)-5l, (long)14l, (long)-14l, (long)6l, (long)13l, (long)11l, (long)16l, (long)16l, (long)4l, (long)10l)))) == (-1448l));
}
}
|
humaneval-HumanEval_108_count_nums.json-L26 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Write a function 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)); | 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;
}
} | for (int j = 2; j < str.length(); j++) { | }
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_80_is_happy.json-L24 | py | def is_happy(s: str) -> bool:
"""You are given a string s.
Your task is to check if the string is happy or not.
A string is happy if its length is at least 3 and every 3 consecutive letters are distinct
For example:
>>> is_happy(a)
False
>>> is_happy(aa)
False
>>> is_happy(abcd)
True
>>> is_happy(aabb)
False
>>> is_happy(adb)
True
>>> is_happy(xyy)
False
"""
n = len(s)
if n < 3:
return False
else:
for i in range(n - 2): | return False
return True
| if s[i] == s[i + 1] or s[i] == s[i + 2] or s[i + 1] == s[i + 2]: | def check(candidate):
assert candidate('a') == False
assert candidate('aa') == False
assert candidate('abcd') == True
assert candidate('aabb') == False
assert candidate('adb') == True
assert candidate('xyy') == False
assert candidate('iopaxpoi') == True
assert candidate('iopaxioi') == False
def test_check():
check(is_happy)
test_check()
|
humaneval-HumanEval_0_has_close_elements.json-L12 | js | //Check if in given array of numbers, are any two numbers closer to each other than
// given threshold.
// >>> has_close_elements([1.0, 2.0, 3.0], 0.5)
// false
// >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)
// true
function has_close_elements(numbers, threshold){
for(var i = 0; i < numbers.length; i++){
for(var j = i + 1; j < numbers.length; j++){
if(Math.abs(numbers[i] - numbers[j]) < threshold){
return true; | }
}
return false;
}
| } | const assert = require('node:assert');
function test() {
let candidate = has_close_elements;
assert.deepEqual(candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.3),true);
assert.deepEqual(candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.05),false);
assert.deepEqual(candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.95),true);
assert.deepEqual(candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.8),false);
assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0], 0.1),true);
assert.deepEqual(candidate([1.1, 2.2, 3.1, 4.1, 5.1], 1.0),true);
assert.deepEqual(candidate([1.1, 2.2, 3.1, 4.1, 5.1], 0.5),false);
}
test(); |
humaneval-HumanEval_154_cycpattern_check.json-L23 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given 2 words. You need to return true if the second word or any of its rotations is a substring in the first word
// >>> cycpatternCheck(("abcd"), ("abd"))
// (false)
// >>> cycpatternCheck(("hello"), ("ell"))
// (true)
// >>> cycpatternCheck(("whassup"), ("psus"))
// (false)
// >>> cycpatternCheck(("abab"), ("baa"))
// (true)
// >>> cycpatternCheck(("efef"), ("eeff"))
// (false)
// >>> cycpatternCheck(("himenss"), ("simen"))
// (true)
public static boolean cycpatternCheck(String a, String b) {
| boolean found = false;
for (int i = 0; i < len; i++) {
b = b.substring(1) + b.charAt(0);
found = a.contains(b);
if (found) break;
}
return found;
}
} | int len = b.length(); | }
public static void main(String[] args) {
assert(cycpatternCheck(("xyzw"), ("xyw")) == (false));
assert(cycpatternCheck(("yello"), ("ell")) == (true));
assert(cycpatternCheck(("whattup"), ("ptut")) == (false));
assert(cycpatternCheck(("efef"), ("fee")) == (true));
assert(cycpatternCheck(("abab"), ("aabb")) == (false));
assert(cycpatternCheck(("winemtt"), ("tinem")) == (true));
}
}
|
humaneval-HumanEval_126_is_sorted.json-L48 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given 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;
for (Long number : lst) {
if (first_time) {
first_time = false;
previous_number = number.intValue();
running_max = number.intValue();
}
else {
if (running_max > number.intValue()) {
return false;
} | 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;
}
}
} | else { | }
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_21_rescale_to_unit.json-L13 | js | //Given array of numbers (of at least two elements), apply a linear transform to that array,
// such that the smallest number will become 0 and the largest will become 1
// >>> rescale_to_unit([1.0, 2.0, 3.0, 4.0, 5.0])
// [0.0, 0.25, 0.5, 0.75, 1.0]
function rescale_to_unit(numbers){
var min = Math.min.apply(Math, numbers);
var max = Math.max.apply(Math, numbers);
var sub = max - min;
var unit = [];
for (var i = 0; i < numbers.length; i++) {
unit.push((numbers[i] - min) / sub);
} | }
| return unit; | const assert = require('node:assert');
function test() {
let candidate = rescale_to_unit;
assert.deepEqual(candidate([2.0, 49.9]),[0.0, 1.0]);
assert.deepEqual(candidate([100.0, 49.9]),[1.0, 0.0]);
assert.deepEqual(candidate([1.0, 2.0, 3.0, 4.0, 5.0]),[0.0, 0.25, 0.5, 0.75, 1.0]);
assert.deepEqual(candidate([2.0, 1.0, 5.0, 3.0, 4.0]),[0.25, 0.0, 1.0, 0.5, 0.75]);
assert.deepEqual(candidate([12.0, 11.0, 15.0, 13.0, 14.0]),[0.25, 0.0, 1.0, 0.5, 0.75]);
}
test(); |
humaneval-HumanEval_93_encode.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 takes a message, and encodes in such a
// way that it swaps case of all letters, replaces all vowels in
// the message with the letter that appears 2 places ahead of that
// vowel in the english alphabet.
// Assume only letters.
// Examples:
// >>> encode(("test"))
// ("TGST")
// >>> encode(("This is a message"))
// ("tHKS KS C MGSSCGG")
public static String encode(String message) {
char[] arr = message.toCharArray();
char[] vowels = "AEIOUaeiou".toCharArray();
for (int i = 0; i < arr.length; i++) {
if (Arrays.binarySearch(vowels, arr[i]) > -1) {
arr[i] = (char) (arr[i] + 2);
}
if (Character.isUpperCase(arr[i])) { | } else {
arr[i] = Character.toUpperCase(arr[i]);
}
}
return new String(arr);
}
} | arr[i] = Character.toLowerCase(arr[i]); | }
public static void main(String[] args) {
assert(encode(("TEST")).equals(("tgst")));
assert(encode(("Mudasir")).equals(("mWDCSKR")));
assert(encode(("YES")).equals(("ygs")));
assert(encode(("This is a message")).equals(("tHKS KS C MGSSCGG")));
assert(encode(("I DoNt KnOw WhAt tO WrItE")).equals(("k dQnT kNqW wHcT Tq wRkTg")));
}
}
|
humaneval-HumanEval_141_file_name_check.json-L48 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Create a function which takes a string representing a file's name, and returns
// 'Yes' if the the file's name is valid, and returns 'No' otherwise.
// A file's name is considered to be valid if and only if all the following conditions
// are met:
// - There should not be more than three digits ('0'-'9') in the file's name.
// - The file's name contains exactly one dot '.'
// - The substring before the dot should not be empty, and it starts with a letter from
// the latin alphapet ('a'-'z' and 'A'-'Z').
// - The substring after the dot should be one of these: ['txt', 'exe', 'dll']
// Examples:
// >>> fileNameCheck(("example.txt"))
// ("Yes")
// >>> fileNameCheck(("1example.dll"))
// ("No")
public static String fileNameCheck(String file_name) {
String result = "No";
String[] tokens = file_name.split("\\.");
if (tokens.length != 2) {
return result;
}
String extension = tokens[1];
if (!(extension.equals("txt") || extension.equals("exe") || extension.equals("dll"))) {
return result;
}
String name = tokens[0];
int count = 0;
for (int i = 0; i < name.length(); i++) {
if (Character.isDigit(name.charAt(i))) {
count++;
}
}
if (count > 3) {
return result;
}
if (name.length() == 0) {
return result;
}
if (!Character.isLetter(name.charAt(0))) {
return result; | return "Yes";
}
} | } | }
public static void main(String[] args) {
assert(fileNameCheck(("example.txt")).equals(("Yes")));
assert(fileNameCheck(("1example.dll")).equals(("No")));
assert(fileNameCheck(("s1sdf3.asd")).equals(("No")));
assert(fileNameCheck(("K.dll")).equals(("Yes")));
assert(fileNameCheck(("MY16FILE3.exe")).equals(("Yes")));
assert(fileNameCheck(("His12FILE94.exe")).equals(("No")));
assert(fileNameCheck(("_Y.txt")).equals(("No")));
assert(fileNameCheck(("?aREYA.exe")).equals(("No")));
assert(fileNameCheck(("/this_is_valid.dll")).equals(("No")));
assert(fileNameCheck(("this_is_valid.wow")).equals(("No")));
assert(fileNameCheck(("this_is_valid.txt")).equals(("Yes")));
assert(fileNameCheck(("this_is_valid.txtexe")).equals(("No")));
assert(fileNameCheck(("#this2_i4s_5valid.ten")).equals(("No")));
assert(fileNameCheck(("@this1_is6_valid.exe")).equals(("No")));
assert(fileNameCheck(("this_is_12valid.6exe4.txt")).equals(("No")));
assert(fileNameCheck(("all.exe.txt")).equals(("No")));
assert(fileNameCheck(("I563_No.exe")).equals(("Yes")));
assert(fileNameCheck(("Is3youfault.txt")).equals(("Yes")));
assert(fileNameCheck(("no_one#knows.dll")).equals(("Yes")));
assert(fileNameCheck(("1I563_Yes3.exe")).equals(("No")));
assert(fileNameCheck(("I563_Yes3.txtt")).equals(("No")));
assert(fileNameCheck(("final..txt")).equals(("No")));
assert(fileNameCheck(("final132")).equals(("No")));
assert(fileNameCheck(("_f4indsartal132.")).equals(("No")));
assert(fileNameCheck((".txt")).equals(("No")));
assert(fileNameCheck(("s.")).equals(("No")));
}
}
|
humaneval-HumanEval_75_is_multiply_prime.json-L20 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Write a function that returns true if the given number is the multiplication of 3 prime numbers
// and false otherwise.
// Knowing that (a) is less then 100.
// Example:
// >>> isMultiplyPrime((30l))
// (true)
// 30 = 2 * 3 * 5
public static boolean isMultiplyPrime(long a) {
if (a == 1) {
return false;
} | return true;
}
long copyOfInput = a;
ArrayList<Long> arr = new ArrayList<Long>();
while (a % 2 == 0) {
arr.add(2l);
a /= 2;
}
for (long i = 3; i <= Math.sqrt(a); i += 2) {
while (a % i == 0) {
arr.add(i);
a /= i;
}
}
if (a > 2) {
arr.add(a);
}
return arr.size() == 3;
}
} | if (a == 2) { | }
public static void main(String[] args) {
assert(isMultiplyPrime((5l)) == (false));
assert(isMultiplyPrime((30l)) == (true));
assert(isMultiplyPrime((8l)) == (true));
assert(isMultiplyPrime((10l)) == (false));
assert(isMultiplyPrime((125l)) == (true));
assert(isMultiplyPrime((105l)) == (true));
assert(isMultiplyPrime((126l)) == (false));
assert(isMultiplyPrime((729l)) == (false));
assert(isMultiplyPrime((891l)) == (false));
assert(isMultiplyPrime((1001l)) == (true));
}
}
|
humaneval-HumanEval_81_numerical_letter_grade.json-L24 | 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){
| 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;
} | var 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_16_count_distinct_characters.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 {
// Given a string, find out how many distinct characters (regardless of case) does it consist of
// >>> countDistinctCharacters(("xyzXYZ"))
// (3l)
// >>> countDistinctCharacters(("Jerry"))
// (4l)
public static long countDistinctCharacters(String string) {
long result = 0;
string = string.toLowerCase(); | for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
set.add(c);
}
result = set.size();
return result;
}
} | Set<Character> set = new HashSet<Character>(); | }
public static void main(String[] args) {
assert(countDistinctCharacters(("")) == (0l));
assert(countDistinctCharacters(("abcde")) == (5l));
assert(countDistinctCharacters(("abcdecadeCADE")) == (5l));
assert(countDistinctCharacters(("aaaaAAAAaaaa")) == (1l));
assert(countDistinctCharacters(("Jerry jERRY JeRRRY")) == (5l));
}
}
|
humaneval-HumanEval_156_int_to_mini_roman.json-L75 | 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_33_sort_third.json-L12 | py | from typing import List
def sort_third(l: List[int]) -> List[int]:
"""This function takes a list l and returns a list l' such that
l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal
to the values of the corresponding indicies of l, but sorted.
>>> sort_third([1, 2, 3])
[1, 2, 3]
>>> sort_third([5, 6, 3, 4, 8, 9, 2])
[2, 6, 3, 4, 8, 9, 5]
"""
| j = i - 2
while j >= 0 and l[j] > l[j + 3]:
l[j], l[j + 3] = l[j + 3], l[j]
j -= 3
return l
| for i in range(2, len(l), 3): | def check(candidate):
assert candidate([5, 6, 3, 4, 8, 9, 2]) == [2, 6, 3, 4, 8, 9, 5]
assert candidate([5, 8, 3, 4, 6, 9, 2]) == [2, 8, 3, 4, 6, 9, 5]
assert candidate([5, 6, 9, 4, 8, 3, 2]) == [2, 6, 9, 4, 8, 3, 5]
assert candidate([5, 6, 3, 4, 8, 9, 2, 1]) == [2, 6, 3, 4, 8, 9, 5, 1]
def test_check():
check(sort_third)
test_check()
|
humaneval-HumanEval_106_f.json-L17 | py | from typing import List
def f(n: int) -> List[int]:
""" Implement the function f that takes n as a parameter,
and returns a list of size n, such that the value of the element at index i is the factorial of i if i is even
or the sum of numbers from 1 to i otherwise.
i starts from 1.
the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i).
Example:
>>> f(5)
[1, 2, 6, 24, 15]
"""
import math
a=[]
for i in range(n):
i=i+1 | a.append(math.factorial(i))
else:
a.append(sum(range(1,i+1)))
return a
pass | if i%2==0: | def check(candidate):
assert candidate(5) == [1, 2, 6, 24, 15]
assert candidate(7) == [1, 2, 6, 24, 15, 720, 28]
assert candidate(1) == [1]
assert candidate(3) == [1, 2, 6]
def test_check():
check(f)
test_check()
|
humaneval-HumanEval_47_median.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 {
// Return median of elements in the array list l.
// >>> median((new ArrayList<Long>(Arrays.asList((long)3l, (long)1l, (long)2l, (long)4l, (long)5l))))
// (float)3l
// >>> median((new ArrayList<Long>(Arrays.asList((long)-10l, (long)4l, (long)6l, (long)1000l, (long)10l, (long)20l))))
// (15.0f)
public static float median(ArrayList<Long> l) {
float ret = 0;
Collections.sort(l);
int size = l.size();
if (size % 2 == 0) {
ret = (l.get(size / 2) + l.get(size / 2 - 1)) / 2f;
} else { | }
return ret;
}
} | ret = l.get(size / 2); | }
public static void main(String[] args) {
assert(median((new ArrayList<Long>(Arrays.asList((long)3l, (long)1l, (long)2l, (long)4l, (long)5l)))) == (float)3l);
assert(median((new ArrayList<Long>(Arrays.asList((long)-10l, (long)4l, (long)6l, (long)1000l, (long)10l, (long)20l)))) == (8.0f));
assert(median((new ArrayList<Long>(Arrays.asList((long)5l)))) == (float)5l);
assert(median((new ArrayList<Long>(Arrays.asList((long)6l, (long)5l)))) == (5.5f));
assert(median((new ArrayList<Long>(Arrays.asList((long)8l, (long)1l, (long)3l, (long)9l, (long)9l, (long)2l, (long)7l)))) == (float)7l);
}
}
|
humaneval-HumanEval_18_how_many_times.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 {
// Find how many times a given substring can be found in the original string. Count overlaping cases.
// >>> howManyTimes((""), ("a"))
// (0l)
// >>> howManyTimes(("aaa"), ("a"))
// (3l)
// >>> howManyTimes(("aaaa"), ("aa"))
// (3l)
public static long howManyTimes(String string, String substring) {
long count = 0;
int index = 0;
while (index != -1) {
index = string.indexOf(substring, index);
if (index != -1) {
index++;
count++;
}
} | }
} | return count; | }
public static void main(String[] args) {
assert(howManyTimes((""), ("x")) == (0l));
assert(howManyTimes(("xyxyxyx"), ("x")) == (4l));
assert(howManyTimes(("cacacacac"), ("cac")) == (4l));
assert(howManyTimes(("john doe"), ("john")) == (1l));
}
}
|
humaneval-HumanEval_160_do_algebra.json-L40 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given two array lists operator, and operand. The first array list has basic algebra operations, and
// the second array list is an array array list of integers. Use the two given array lists to build the algebric
// expression and return the evaluation of this expression.
// The basic algebra operations:
// Addition ( + )
// Subtraction ( - )
// Multiplication ( * )
// Floor division ( // )
// Exponentiation ( ** )
// Example:
// operator['+', '*', '-']
// array array list = [2, 3, 4, 5]
// result = 2 + 3 * 4 - 5
// => result = 9
// Note:
// The length of operator array list is equal to the length of operand array list minus one.
// Operand is an array array list of of non-negative integers.
// Operator array list has at least one operator, and operand array list has at least two operands.
public static long doAlgebra(ArrayList<String> op, ArrayList<Long> operand) {
ArrayList<Long> list = new ArrayList<>();
list.add(operand.get(0));
for (int i = 0; i < op.size(); i++) {
if (op.get(i).equals("+"))
list.add(operand.get(i + 1));
else if (op.get(i).equals("-"))
list.add(-operand.get(i + 1));
else if (op.get(i).equals("*"))
list.set(list.size() - 1, list.get(list.size() - 1) * operand.get(i + 1));
else if (op.get(i).equals("//"))
list.set(list.size() - 1, list.get(list.size() - 1) / operand.get(i + 1));
else if (op.get(i).equals("**")) | }
return list.stream().mapToLong(x -> x).sum();
}
} | list.set(list.size() - 1, (long) Math.pow(list.get(list.size() - 1), operand.get(i + 1))); | }
public static void main(String[] args) {
assert(doAlgebra((new ArrayList<String>(Arrays.asList((String)"**", (String)"*", (String)"+"))), (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)5l)))) == (37l));
assert(doAlgebra((new ArrayList<String>(Arrays.asList((String)"+", (String)"*", (String)"-"))), (new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)4l, (long)5l)))) == (9l));
assert(doAlgebra((new ArrayList<String>(Arrays.asList((String)"//", (String)"*"))), (new ArrayList<Long>(Arrays.asList((long)7l, (long)3l, (long)4l)))) == (8l));
}
}
|
humaneval-HumanEval_25_factorize.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 {
// Return array list of prime factors of given integer in the order from smallest to largest.
// Each of the factors should be array listed number of times corresponding to how many times it appeares in factorization.
// Input number should be equal to the product of all factors
// >>> factorize((8l))
// (new ArrayList<Long>(Arrays.asList((long)2l, (long)2l, (long)2l)))
// >>> factorize((25l))
// (new ArrayList<Long>(Arrays.asList((long)5l, (long)5l)))
// >>> factorize((70l))
// (new ArrayList<Long>(Arrays.asList((long)2l, (long)5l, (long)7l)))
public static ArrayList<Long> factorize(long n) {
ArrayList<Long> factors = new ArrayList<Long>();
for (long d = 2; d*d <= n; d++) {
while (n % d == 0) { | n /= d;
}
}
if (n > 1)
factors.add(n);
return factors;
}
} | factors.add(d); | }
public static void main(String[] args) {
assert(factorize((2l)).equals((new ArrayList<Long>(Arrays.asList((long)2l)))));
assert(factorize((4l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l)))));
assert(factorize((8l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)2l, (long)2l)))));
assert(factorize((57l)).equals((new ArrayList<Long>(Arrays.asList((long)3l, (long)19l)))));
assert(factorize((3249l)).equals((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l, (long)19l, (long)19l)))));
assert(factorize((185193l)).equals((new ArrayList<Long>(Arrays.asList((long)3l, (long)3l, (long)3l, (long)19l, (long)19l, (long)19l)))));
assert(factorize((20577l)).equals((new ArrayList<Long>(Arrays.asList((long)3l, (long)19l, (long)19l, (long)19l)))));
assert(factorize((18l)).equals((new ArrayList<Long>(Arrays.asList((long)2l, (long)3l, (long)3l)))));
}
}
|
humaneval-HumanEval_94_skjkasdkd.json-L29 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// You are given an array array list of 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; | continue;
}
if (item == 2) {
counter = 1;
}
else {
int l = 1;
while (l <= (int) number) {
if (number % l == 0) {
counter++;
}
l++;
}
}
if (counter == 2) {
if (number > largestPrime) {
largestPrime = number;
}
}
}
if (largestPrime == 0) {
return 0l;
}
else {
long output = 0l;
while (largestPrime > 0) {
output += largestPrime % 10;
largestPrime /= 10;
}
return output;
}
}
} | if (item == 1) { | }
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_43_pairs_sum_to_zero.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 {
// pairs_sum_to_zero takes an array array list of integers as an input.
// it returns true if there are two distinct elements in the array list that
// sum to zero, and false otherwise.
// >>> pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)5l, (long)0l))))
// (false)
// >>> pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)-2l, (long)1l))))
// (false)
// >>> pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)7l))))
// (false)
// >>> pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)-5l, (long)3l, (long)5l, (long)7l))))
// (true)
// >>> pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)1l))))
// (false)
public static boolean pairsSumToZero(ArrayList<Long> l) {
for (int i = 0; i < l.size(); i++) {
for (int j = i + 1; j < l.size(); j++) { | return true;
}
}
}
return false;
}
} | if (l.get(i) + l.get(j) == 0) { | }
public static void main(String[] args) {
assert(pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)5l, (long)0l)))) == (false));
assert(pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)3l, (long)-2l, (long)1l)))) == (false));
assert(pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)1l, (long)2l, (long)3l, (long)7l)))) == (false));
assert(pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)2l, (long)4l, (long)-5l, (long)3l, (long)5l, (long)7l)))) == (true));
assert(pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)1l)))) == (false));
assert(pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)-3l, (long)9l, (long)-1l, (long)3l, (long)2l, (long)30l)))) == (true));
assert(pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)-3l, (long)9l, (long)-1l, (long)3l, (long)2l, (long)31l)))) == (true));
assert(pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)-3l, (long)9l, (long)-1l, (long)4l, (long)2l, (long)30l)))) == (false));
assert(pairsSumToZero((new ArrayList<Long>(Arrays.asList((long)-3l, (long)9l, (long)-1l, (long)4l, (long)2l, (long)31l)))) == (false));
}
}
|
humaneval-HumanEval_64_vowels_count.json-L17 | py | def vowels_count(s: str) -> int:
"""Write a function vowels_count which takes a string representing
a word as input and returns the number of vowels in the string.
Vowels in this case are 'a', 'e', 'i', 'o', 'u'. Here, 'y' is also a
vowel, but only when it is at the end of the given word.
Example:
>>> vowels_count('abcde')
2
>>> vowels_count('ACEDY')
3
"""
s = s.lower()
i = 0
count = 0
while i < len(s): | count += 1
if (s[i] == 'y' and i == len(s) - 1):
count += 1
i += 1
return count
| if (s[i] == 'a' or s[i] == 'e' or s[i] == 'i' or s[i] == 'o' or s[i] == 'u'): | def check(candidate):
assert candidate('abcde') == 2
assert candidate('Alone') == 3
assert candidate('key') == 2
assert candidate('bye') == 1
assert candidate('keY') == 2
assert candidate('bYe') == 1
assert candidate('ACEDY') == 3
def test_check():
check(vowels_count)
test_check()
|
humaneval-HumanEval_137_compare_one.json-L19 | 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; | return undefined;
}
}
else if (typeof a === "string" && typeof b === "number"){
if (a[0] > b){
return a;
} else if (a[0] < b){
return b;
} else {
return undefined;
}
}
else if (typeof a === "number" && typeof b === "string"){
if (a > b[0]){
return a;
} else if (a < b[0]){
return b;
} else {
return undefined;
}
}
}
| } else { | 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_39_prime_fib.json-L18 | js | //prime_fib returns n-th number that is a Fibonacci number and it's also prime.
// >>> prime_fib(1)
// 2
// >>> prime_fib(2)
// 3
// >>> prime_fib(3)
// 5
// >>> prime_fib(4)
// 13
// >>> prime_fib(5)
// 89
function prime_fib(n){
function isPrime(number){
if(number <= 3){
return number > 1;
}
else if(number % 2 == 0 || number % 3 == 0){ | }
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 false; | 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_7_filter_by_substring.json-L10 | py | from typing import List
def filter_by_substring(strings: List[str], substring: str) -> List[str]:
""" Filter an input list of strings only for ones that contain given substring
>>> filter_by_substring([], 'a')
[]
>>> filter_by_substring(['abc', 'bacd', 'cde', 'array'], 'a')
['abc', 'bacd', 'array']
"""
| return [s for s in strings if substring in s] | def check(candidate):
assert candidate([], 'john') == []
assert candidate(['xxx', 'asd', 'xxy', 'john doe', 'xxxAAA', 'xxx'], 'xxx') == ['xxx', 'xxxAAA', 'xxx']
assert candidate(['xxx', 'asd', 'aaaxxy', 'john doe', 'xxxAAA', 'xxx'], 'xx') == ['xxx', 'aaaxxy', 'xxxAAA', 'xxx']
assert candidate(['grunt', 'trumpet', 'prune', 'gruesome'], 'run') == ['grunt', 'prune']
def test_check():
check(filter_by_substring)
test_check()
|
|
humaneval-HumanEval_45_triangle_area.json-L15 | java | import java.util.*;
import java.lang.reflect.*;
import org.javatuples.*;
import java.security.*;
import java.math.*;
import java.io.*;
import java.util.stream.*;
class Problem {
// Given length of a side and high return area for a triangle.
// >>> triangleArea((5l), (3l))
// (7.5f)
public static float triangleArea(long a, long h) {
return (a*h)/2f;
} | } | }
public static void main(String[] args) {
assert(triangleArea((5l), (3l)) == (7.5f));
assert(triangleArea((2l), (2l)) == (2.0f));
assert(triangleArea((10l), (8l)) == (40.0f));
}
}
|
|
humaneval-HumanEval_64_vowels_count.json-L15 | py | def vowels_count(s: str) -> int:
"""Write a function vowels_count which takes a string representing
a word as input and returns the number of vowels in the string.
Vowels in this case are 'a', 'e', 'i', 'o', 'u'. Here, 'y' is also a
vowel, but only when it is at the end of the given word.
Example:
>>> vowels_count('abcde')
2
>>> vowels_count('ACEDY')
3
"""
s = s.lower()
i = 0 | while i < len(s):
if (s[i] == 'a' or s[i] == 'e' or s[i] == 'i' or s[i] == 'o' or s[i] == 'u'):
count += 1
if (s[i] == 'y' and i == len(s) - 1):
count += 1
i += 1
return count
| count = 0 | def check(candidate):
assert candidate('abcde') == 2
assert candidate('Alone') == 3
assert candidate('key') == 2
assert candidate('bye') == 1
assert candidate('keY') == 2
assert candidate('bYe') == 1
assert candidate('ACEDY') == 3
def test_check():
check(vowels_count)
test_check()
|
humaneval-HumanEval_11_string_xor.json-L6 | 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 arrayB = [];
var arrayXOR = [];
var result = '';
for (var i = 0; i < a.length; i++) {
arrayA.push(parseInt(a[i]));
}
for (var j = 0; j < b.length; j++) {
arrayB.push(parseInt(b[j]));
}
for (var k = 0; k < arrayA.length; k++) {
if (arrayA[k] !== arrayB[k]) {
arrayXOR.push(1);
}
else {
arrayXOR.push(0);
}
}
result = arrayXOR.join('');
return result;
} | var arrayA = []; | 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_65_circular_shift.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 {
// 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) {
| if (shift > digits.length()) {
return new StringBuilder(digits).reverse().toString();
}
return (digits.substring(digits.length() - (int)shift, digits.length()) + digits.substring(0, digits.length() - (int)shift));
}
} | String digits = String.valueOf(x); | }
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_106_f.json-L26 | 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++){
if(i % 2 == 0){
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;
} | } | 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_40_triples_sum_to_zero.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 {
// 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 k = sortedList.size() - 1;
while (j < k) {
long currSum = sortedList.get(i) + sortedList.get(j) + sortedList.get(k);
if (currSum == 0) {
return true;
} else if (currSum < 0) {
j++;
} else {
k--;
}
}
}
return false;
}
} | int j = i + 1; | }
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_64_vowels_count.json-L23 | js | //Write a function vowels_count which takes a string representing
// a word as input and returns the number of vowels in the string.
// Vowels in this case are 'a', 'e', 'i', 'o', 'u'. Here, 'y' is also a
// vowel, but only when it is at the end of the given word.
// Example:
// >>> vowels_count("abcde")
// 2
// >>> vowels_count("ACEDY")
// 3
function vowels_count(s){
s=s.toLowerCase();
var vowels = ['a','e','i','o','u'];
var count = 0;
for(var i=0; i<s.length; i++){
if(vowels.indexOf(s[i])!=-1){
count++;
}
}
if(s[s.length-1]=='y'){
count++;
}
return count; | } | const assert = require('node:assert');
function test() {
let candidate = vowels_count;
assert.deepEqual(candidate("abcde"),2);
assert.deepEqual(candidate("Alone"),3);
assert.deepEqual(candidate("key"),2);
assert.deepEqual(candidate("bye"),1);
assert.deepEqual(candidate("keY"),2);
assert.deepEqual(candidate("bYe"),1);
assert.deepEqual(candidate("ACEDY"),3);
}
test(); |
|
humaneval-HumanEval_56_correct_bracketing.json-L14 | py | def correct_bracketing(brackets: str) -> bool:
""" 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
"""
| for bracket in brackets:
if bracket == '<':
num_left_brackets += 1
elif bracket == '>':
num_left_brackets -= 1
if num_left_brackets < 0:
return False
return num_left_brackets == 0
| num_left_brackets = 0 | def check(candidate):
assert candidate('<>') == True
assert candidate('<<><>>') == True
assert candidate('<><><<><>><>') == True
assert candidate('<><><<<><><>><>><<><><<>>>') == True
assert candidate('<<<><>>>>') == False
assert candidate('><<>') == False
assert candidate('<') == False
assert candidate('<<<<') == False
assert candidate('>') == False
assert candidate('<<>') == False
assert candidate('<><><<><>><>><<>') == False
assert candidate('<><><<><>><>>><>') == False
def test_check():
check(correct_bracketing)
test_check()
|
humaneval-HumanEval_113_odd_count.json-L23 | js | //Given an array of strings, where each string consists of only digits, return an array.
// Each element i of the output should be "the number of odd elements in the
// string i of the input." where all the i's should be replaced by the number
// of odd digits in the i'th string of the input.
// >>> odd_count(["1234567"])
// ["the number of odd elements 4n the str4ng 4 of the 4nput."]
// >>> odd_count(["3", "11111111"])
// ["the number of odd elements 1n the str1ng 1 of the 1nput.", "the number of odd elements 8n the str8ng 8 of the 8nput."]
function odd_count(lst){
var new_lst = [];
for (var i = 0; i < lst.length; i++){
var word = lst[i];
var odd_sum = 0;
for (var j = 0; j < word.length; j++){
if (word[j] % 2 == 1){
odd_sum += 1;
}
}
var odd_word = "the number of odd elements " + odd_sum + "n the str" + odd_sum + "ng " + odd_sum + " of the " + odd_sum + "nput.";
new_lst.push(odd_word);
}
return new_lst; | } | const assert = require('node:assert');
function test() {
let candidate = odd_count;
assert.deepEqual(candidate(["1234567"]),["the number of odd elements 4n the str4ng 4 of the 4nput."]);
assert.deepEqual(candidate(["3", "11111111"]),["the number of odd elements 1n the str1ng 1 of the 1nput.", "the number of odd elements 8n the str8ng 8 of the 8nput."]);
assert.deepEqual(candidate(["271", "137", "314"]),["the number of odd elements 2n the str2ng 2 of the 2nput.", "the number of odd elements 3n the str3ng 3 of the 3nput.", "the number of odd elements 2n the str2ng 2 of the 2nput."]);
}
test(); |
|
humaneval-HumanEval_56_correct_bracketing.json-L25 | js | //brackets is a string of "<" and ">".
// return true if every opening bracket has a corresponding closing bracket.
// >>> correct_bracketing("<")
// false
// >>> correct_bracketing("<>")
// true
// >>> correct_bracketing("<<><>>")
// true
// >>> correct_bracketing("><<>")
// false
function correct_bracketing(brackets){
var opens = 0;
for (var i = 0; i < brackets.length; i++) {
if (brackets[i] === "<") {
opens++;
}
else if (brackets[i] === ">") {
opens--;
}
if (opens < 0) {
return false;
}
}
return opens === 0; | } | const assert = require('node:assert');
function test() {
let candidate = correct_bracketing;
assert.deepEqual(candidate("<>"),true);
assert.deepEqual(candidate("<<><>>"),true);
assert.deepEqual(candidate("<><><<><>><>"),true);
assert.deepEqual(candidate("<><><<<><><>><>><<><><<>>>"),true);
assert.deepEqual(candidate("<<<><>>>>"),false);
assert.deepEqual(candidate("><<>"),false);
assert.deepEqual(candidate("<"),false);
assert.deepEqual(candidate("<<<<"),false);
assert.deepEqual(candidate(">"),false);
assert.deepEqual(candidate("<<>"),false);
assert.deepEqual(candidate("<><><<><>><>><<>"),false);
assert.deepEqual(candidate("<><><<><>><>>><>"),false);
}
test(); |
|
humaneval-HumanEval_44_change_base.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 {
// Change numerical base of input number x to base.
// return string representation after the conversion.
// base numbers are less than 10.
// >>> changeBase((8l), (3l))
// ("22")
// >>> changeBase((8l), (2l))
// ("1000")
// >>> changeBase((7l), (2l))
// ("111")
public static String changeBase(long x, long base) {
if (x<0 || base<2 || base>9) {
throw new IllegalArgumentException("bad input");
}
if (x==0) { | }
StringBuilder sb = new StringBuilder();
while (x>0) {
sb.append(x%base);
x = x/base;
}
return sb.reverse().toString();
}
} | return "0"; | }
public static void main(String[] args) {
assert(changeBase((8l), (3l)).equals(("22")));
assert(changeBase((9l), (3l)).equals(("100")));
assert(changeBase((234l), (2l)).equals(("11101010")));
assert(changeBase((16l), (2l)).equals(("10000")));
assert(changeBase((8l), (2l)).equals(("1000")));
assert(changeBase((7l), (2l)).equals(("111")));
assert(changeBase((2l), (3l)).equals(("2")));
assert(changeBase((3l), (4l)).equals(("3")));
assert(changeBase((4l), (5l)).equals(("4")));
assert(changeBase((5l), (6l)).equals(("5")));
assert(changeBase((6l), (7l)).equals(("6")));
assert(changeBase((7l), (8l)).equals(("7")));
}
}
|
humaneval-HumanEval_156_int_to_mini_roman.json-L64 | 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;
} | res += "v";
num -= 5;
}
if (num >= 4) {
res += "iv";
num -= 4;
}
while (num >= 1) {
res += "i";
num -= 1;
}
return res;
}
} | if (num >= 5) { | }
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_124_valid_date.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 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; | 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;
}
} | String[] parts = date.split("-"); | }
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_25_factorize.json-L19 | 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);
n = n / factor;
} else {
factor++; | }
return arr;
}
| } | 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_139_special_factorial.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 {
// 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) {
| while (n > 0) {
long local_result = 1;
long temp = n;
while (temp > 0) {
local_result *= temp;
temp--;
}
result *= local_result;
n--;
}
return result;
}
} | long result = 1; | }
public static void main(String[] args) {
assert(specialFactorial((4l)) == (288l));
assert(specialFactorial((5l)) == (34560l));
assert(specialFactorial((7l)) == (125411328000l));
assert(specialFactorial((1l)) == (1l));
}
}
|
humaneval-HumanEval_137_compare_one.json-L51 | py | from typing import Union
def compare_one(a: Union[int, float, str], b: Union[int, float, str]) -> Union[int, float, str, None]:
"""
Create a function that takes integers, floats, or strings representing
real numbers, and returns the larger variable in its given variable type.
Return None if the values are equal.
Note: If a real number is represented as a string, the floating point might be . or ,
>>> compare_one(1, 2.5)
2.5
>>> compare_one(1, '2,3')
'2,3'
>>> compare_one('5,1', '6')
'6'
>>> compare_one('1', 1)
None
"""
if isinstance(a, int) and isinstance(b, int):
if a > b:
return a
elif a < b:
return b
else:
return None
elif isinstance(a, float) and isinstance(b, float):
if a > b:
return a
elif a < b:
return b
else:
return None
elif isinstance(a, str) and isinstance(b, str):
a_tmp = a.replace(',', '.')
b_tmp = b.replace(',', '.')
if float(a_tmp) > float(b_tmp):
return a
elif float(a_tmp) < float(b_tmp):
return b
else:
return None
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: | 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 | return a | 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_72_will_it_fly.json-L27 | js | //Write a function that returns true if the object q will fly, and false otherwise.
// The object q will fly if it's balanced (it is a palindromic array) and the sum of its elements is less than or equal the maximum possible weight w.
// Example:
// >>> will_it_fly([1, 2], 5)
// false
// # 1+2 is less than the maximum possible weight, but it's unbalanced.
// >>> will_it_fly([3, 2, 3], 1)
// false
// # it's balanced, but 3+2+3 is more than the maximum possible weight.
// >>> will_it_fly([3, 2, 3], 9)
// true
// # 3+2+3 is less than the maximum possible weight, and it's balanced.
// >>> will_it_fly([3], 5)
// true
// # 3 is less than the maximum possible weight, and it's balanced.
function will_it_fly(q, w){
let sum = 0;
let balanced = true;
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;
} | } | 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_144_simplify.json-L15 | js | //Your task is to implement a function that will simplify the expression
// x * n. The function returns true if x * n evaluates to a whole number and false
// otherwise. Both x and n, are string representation of a fraction, and have the following format,
// <numerator>/<denominator> where both numerator and denominator are positive whole numbers.
// You can assume that x, and n are valid fractions, and do not have zero as denominator.
// >>> simplify("1/5", "5/1")
// true
// >>> simplify("1/6", "2/1")
// false
// >>> simplify("7/10", "10/2")
// false
function simplify(x, n){
let xNumerator = parseInt(x.split('/')[0]);
let xDenominator = parseInt(x.split('/')[1]); | let nDenominator = parseInt(n.split('/')[1]);
let xTimesN = xNumerator * nNumerator / (xDenominator * nDenominator);
return Number.isInteger(xTimesN);
}
| let nNumerator = parseInt(n.split('/')[0]); | const assert = require('node:assert');
function test() {
let candidate = simplify;
assert.deepEqual(candidate("1/5", "5/1"),true);
assert.deepEqual(candidate("1/6", "2/1"),false);
assert.deepEqual(candidate("5/1", "3/1"),true);
assert.deepEqual(candidate("7/10", "10/2"),false);
assert.deepEqual(candidate("2/10", "50/10"),true);
assert.deepEqual(candidate("7/2", "4/2"),true);
assert.deepEqual(candidate("11/6", "6/1"),true);
assert.deepEqual(candidate("2/3", "5/2"),false);
assert.deepEqual(candidate("5/2", "3/5"),false);
assert.deepEqual(candidate("2/4", "8/4"),true);
assert.deepEqual(candidate("2/4", "4/2"),true);
assert.deepEqual(candidate("1/5", "5/1"),true);
assert.deepEqual(candidate("1/5", "1/5"),false);
}
test(); |
humaneval-HumanEval_110_exchange.json-L22 | 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 | if y % 2 == 0:
count -= 1
if count > 0:
return "NO"
else:
return "YES" | for y in lst2: | 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_126_is_sorted.json-L53 | 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;
for (Long number : lst) {
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;
} | seen_2_in_a_row = true;
}
}
else {
seen_2_in_a_row = false;
}
previous_number = number.intValue();
running_max = number.intValue();
}
}
}
return true;
}
}
} | else { | }
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_6_parse_nested_parens.json-L19 | js | //Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
// For each of the group, output the deepest level of nesting of parentheses.
// E.g. (()()) has maximum two levels of nesting while ((())) has three.
// >>> parse_nested_parens("(()()) ((())) () ((())()())")
// [2, 3, 1, 3]
function parse_nested_parens(paren_string){
var level = 0;
var maxlevel = 0;
var result = [];
for (var i = 0; i < paren_string.length; i++){
if (paren_string[i] == "("){
level += 1;
if (level > maxlevel){
maxlevel = level;
}
}
else if (paren_string[i] == ")"){
level -= 1; | else if (paren_string[i] == " "){
result.push(maxlevel);
maxlevel = 0;
level = 0;
}
}
result.push(maxlevel);
return 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_154_cycpattern_check.json-L18 | js | //You are given 2 words. You need to return true if the second word or any of its rotations is a substring in the first word
// >>> cycpattern_check("abcd", "abd")
// false
// >>> cycpattern_check("hello", "ell")
// true
// >>> cycpattern_check("whassup", "psus")
// false
// >>> cycpattern_check("abab", "baa")
// true
// >>> cycpattern_check("efef", "eeff")
// false
// >>> cycpattern_check("himenss", "simen")
// true
function cycpattern_check(a, b){
var result = false;
for(var i = 0; i < b.length; i++){
result = result || a.includes(b); | }
return result;
}
| b = b.slice(1) + b[0]; | const assert = require('node:assert');
function test() {
let candidate = cycpattern_check;
assert.deepEqual(candidate("xyzw", "xyw"),false);
assert.deepEqual(candidate("yello", "ell"),true);
assert.deepEqual(candidate("whattup", "ptut"),false);
assert.deepEqual(candidate("efef", "fee"),true);
assert.deepEqual(candidate("abab", "aabb"),false);
assert.deepEqual(candidate("winemtt", "tinem"),true);
}
test(); |
humaneval-HumanEval_109_move_one_ball.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 {
// We have an array array list 'arr' of N integers arr[1], arr[2], ..., arr[N].The
// numbers in the array array list will be randomly ordered. Your task is to determine if
// it is possible to get an array array list sorted in non-decreasing order by performing
// the following operation on the given array array list:
// You are allowed to perform right shift operation any number of times.
// One right shift operation means shifting all elements of the array array list by one
// position in the right direction. The last element of the array array list will be moved to
// the starting position in the array array list i.e. 0th index.
// If it is possible to obtain the sorted array array list by performing the above operation
// then return true else return false.
// If the given array array list is empty then return true.
// Note: The given array list is guaranteed to have unique elements.
// For Example:
// >>> moveOneBall((new ArrayList<Long>(Arrays.asList((long)3l, (long)4l, (long)5l, (long)1l, (long)2l))))
// (true)
// Explanation: By performin 2 right shift operations, non-decreasing order can
// be achieved for the given array array list.
// >>> moveOneBall((new ArrayList<Long>(Arrays.asList((long)3l, (long)5l, (long)4l, (long)1l, (long)2l))))
// (false)
// Explanation:It is not possible to get non-decreasing order for the given
// array array list by performing any number of right shift operations.
public static boolean moveOneBall(ArrayList<Long> arr) {
if (arr.size() == 0) return true;
ArrayList<Long> sorted = new ArrayList<Long>(arr); | for (int i = 0; i < arr.size(); i++) {
ArrayList<Long> copy = new ArrayList<Long>(arr);
long last = copy.remove(copy.size() - 1);
copy.add(0, last);
if (copy.equals(sorted)) return true;
arr = copy;
}
return false;
}
} | Collections.sort(sorted); | }
public static void main(String[] args) {
assert(moveOneBall((new ArrayList<Long>(Arrays.asList((long)3l, (long)4l, (long)5l, (long)1l, (long)2l)))) == (true));
assert(moveOneBall((new ArrayList<Long>(Arrays.asList((long)3l, (long)5l, (long)10l, (long)1l, (long)2l)))) == (true));
assert(moveOneBall((new ArrayList<Long>(Arrays.asList((long)4l, (long)3l, (long)1l, (long)2l)))) == (false));
assert(moveOneBall((new ArrayList<Long>(Arrays.asList((long)3l, (long)5l, (long)4l, (long)1l, (long)2l)))) == (false));
assert(moveOneBall((new ArrayList<Long>(Arrays.asList()))) == (true));
}
}
|
humaneval-HumanEval_102_choose_num.json-L17 | js | //This function takes two positive numbers x and y and returns the
// biggest even integer number that is in the range [x, y] inclusive. If
// there's no such number, then the function should return -1.
// For example:
// >>> choose_num(12, 15)
// 14
// >>> choose_num(13, 12)
// -1
function choose_num(x, y){
var num = -1;
for(var i = x; i <= y; i++){
if(i % 2 == 0){
num = i;
}
}
return num; | } | const assert = require('node:assert');
function test() {
let candidate = choose_num;
assert.deepEqual(candidate(12, 15),14);
assert.deepEqual(candidate(13, 12),-1);
assert.deepEqual(candidate(33, 12354),12354);
assert.deepEqual(candidate(5234, 5233),-1);
assert.deepEqual(candidate(6, 29),28);
assert.deepEqual(candidate(27, 10),-1);
assert.deepEqual(candidate(7, 7),-1);
assert.deepEqual(candidate(546, 546),546);
}
test(); |
|
humaneval-HumanEval_58_common.json-L8 | js | //Return sorted unique common elements for two arrays.
// >>> common([1, 4, 3, 34, 653, 2, 5], [5, 7, 1, 5, 9, 653, 121])
// [1, 5, 653]
// >>> common([5, 3, 2, 8], [3, 2])
// [2, 3]
function common(l1, l2){
return [...new Set(l1.filter(n=>l2.includes(n)))].sort(); | } | const assert = require('node:assert');
function test() {
let candidate = common;
assert.deepEqual(candidate([1, 4, 3, 34, 653, 2, 5], [5, 7, 1, 5, 9, 653, 121]),[1, 5, 653]);
assert.deepEqual(candidate([5, 3, 2, 8], [3, 2]),[2, 3]);
assert.deepEqual(candidate([4, 3, 2, 8], [3, 2, 4]),[2, 3, 4]);
assert.deepEqual(candidate([4, 3, 2, 8], []),[]);
}
test(); |
|
humaneval-HumanEval_44_change_base.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 {
// Change numerical base of input number x to base.
// return string representation after the conversion.
// base numbers are less than 10.
// >>> changeBase((8l), (3l))
// ("22")
// >>> changeBase((8l), (2l))
// ("1000")
// >>> changeBase((7l), (2l))
// ("111")
public static String changeBase(long x, long base) {
if (x<0 || base<2 || base>9) {
throw new IllegalArgumentException("bad input");
} | return "0";
}
StringBuilder sb = new StringBuilder();
while (x>0) {
sb.append(x%base);
x = x/base;
}
return sb.reverse().toString();
}
} | if (x==0) { | }
public static void main(String[] args) {
assert(changeBase((8l), (3l)).equals(("22")));
assert(changeBase((9l), (3l)).equals(("100")));
assert(changeBase((234l), (2l)).equals(("11101010")));
assert(changeBase((16l), (2l)).equals(("10000")));
assert(changeBase((8l), (2l)).equals(("1000")));
assert(changeBase((7l), (2l)).equals(("111")));
assert(changeBase((2l), (3l)).equals(("2")));
assert(changeBase((3l), (4l)).equals(("3")));
assert(changeBase((4l), (5l)).equals(("4")));
assert(changeBase((5l), (6l)).equals(("5")));
assert(changeBase((6l), (7l)).equals(("6")));
assert(changeBase((7l), (8l)).equals(("7")));
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.