version stringclasses 1
value | style stringclasses 2
values | subset stringclasses 12
values | question_id stringlengths 9 23 | question stringlengths 27 8.13k | solution stringlengths 99 5.97k | test stringlengths 105 4.45k | test_info listlengths 0 13 | gpt_pass_sequence listlengths 1 10 | gpt_pass_trial_num int64 1 10 | gpt_difficulty stringclasses 3
values | gpt_pass_percentage float64 0.1 1 | trials dict | chosen_trial stringclasses 10
values | metadata dict | benchmark_similarity float64 0.16 0.93 | benchmark_instruction stringclasses 1
value | benchmark_task_id stringclasses 1
value | filter_reason stringclasses 1
value | base_id stringlengths 7 21 | variant stringclasses 2
values | top_function_names listlengths 2 13 | n_top_functions int64 2 13 | helper_call_edges listlengths 1 16 | n_helper_call_edges int64 1 16 | harness_functions listlengths 0 3 | structural_wrapper bool 1
class | class_names listlengths 0 5 | has_class bool 2
classes | test_kind stringclasses 1
value | test_has_solution_import bool 2
classes | third_party_imports listlengths 0 5 | stdlib_only bool 2
classes | question_norm_sha1 stringlengths 40 40 | solution_ast_sha1 stringlengths 40 40 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
v1.0 | instruct | Leetcode | Leetcode_30_I | Write a function that receives two integers, `start` and `end`, and returns the **sum of all prime numbers** in the inclusive range `[start, end]`. If there are no prime numbers in the given range, the function should return `0`. | def is_prime(n):
"""
Returns True if n is a prime number, otherwise False.
"""
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
... | from solution import sum_of_primes
def test_sum_of_primes_with_primes():
assert sum_of_primes(10, 20) == 60 # primes are 11, 13, 17, 19
def test_sum_of_primes_without_primes():
assert sum_of_primes(14, 16) == 0 # no primes in this range
def test_sum_of_primes_with_large_range():
assert sum_of_primes(1... | [
{
"docstring": "Returns True if n is a prime number, otherwise False.",
"function_declaration": "def is_prime(n):",
"function_name": "is_prime",
"parameter_list": "(n)"
},
{
"docstring": "Returns the sum of all prime numbers in the inclusive range [start, end].\nIf there are no primes in the... | [
1,
0,
1,
1,
1,
1,
0,
1,
1,
0
] | 7 | easy | 0.7 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_prime(n):\n if n <= 1:\n return False\n if n <= 3:\n return True\n if n % 2 == 0 or n % 3 == 0:\n return False\n i = 5\n while i * ... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000000030",
"row_id": 30,
"seed_ids": "['28', '1887', '2518']"
} | 0.637759 | Leetcode_30 | I | [
"is_prime",
"sum_of_primes"
] | 2 | [
[
"sum_of_primes",
"is_prime"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | a7435892c33110bd599a1edb525b16247023e01e | bf2ed0282978810fc123ec748148dbb41fa753cc | |||
v1.0 | instruct | Leetcode | Leetcode_31_I | You are given a binary tree with `n` nodes where each node has a value represented by an integer. You need to perform `q` queries on this tree. Each query consists of two integers, `u` and `v`, representing two nodes in the tree. For each query, find and return the sum of values of all nodes that lie on the path betwee... | class TreeNode:
def __init__(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right
def find_lca(root, u, v):
if root is None or root.value == u or root.value == v:
return root
left_lca = find_lca(root.left, u, v)
right_lca = find_l... | from solution import TreeNode, path_sum
def create_test_tree():
# Create a simple tree for testing:
# 1
# / \
# 2 3
# /| |\
# 4 5 6 7
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.lef... | [] | [
0,
0,
0,
0,
0,
1,
0,
0,
0,
1
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "class TreeNode:\n def __init__(self, value=0, left=None, right=None):\n self.value = value\n self.left = left\n self.right = right\n \ndef find... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000000031",
"row_id": 31,
"seed_ids": "['1675', '245', '803']"
} | 0.577608 | Leetcode_31 | I | [
"find_lca",
"path_sum",
"sum_values_on_path"
] | 3 | [
[
"path_sum",
"find_lca"
],
[
"path_sum",
"sum_values_on_path"
]
] | 2 | [] | false | [
"TreeNode"
] | true | pytest | true | [] | true | 393fef7fde58b7591d3b1f2ed45e83d9aaff1076 | 256ad21511a10108f256ac83486876a1dfba659e | |||
v1.0 | instruct | Leetcode | Leetcode_141_I | You are given a list of strings `words` where each string consists of lowercase English letters. Two strings `s1` and `s2` are said to be **similar** if:
* They are of the same length.
* For every letter at position `i` in `s1`, the letter at position `i` in `s2` is either the same or belongs to the same equivale... | from collections import defaultdict
def find_representative(graph, char):
# Find the representative parent of the character
if graph[char] != char:
graph[char] = find_representative(graph, graph[char])
return graph[char]
def union(graph, a, b):
# Union the sets based on two characters a and b
... | from solution import num_similar_groups
def test_no_equivalences():
words = ["abc", "def", "ghi"]
equiv = []
assert num_similar_groups(words, equiv) == 3
def test_all_equivalent_chars():
words = ["abc", "def", "abf"]
equiv = [['a', 'd'], ['b', 'e'], ['c', 'f']]
assert num_similar_groups(words,... | [
{
"docstring": null,
"function_declaration": "def find_representative(graph, char):",
"function_name": "find_representative",
"parameter_list": "(graph, char)"
},
{
"docstring": null,
"function_declaration": "def union(graph, a, b):",
"function_name": "union",
"parameter_list": "... | [
1,
0,
0,
0,
0,
0,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "from collections import defaultdict\n\ndef find_representative(graph, char):\n # Find the representative parent of the character\n if graph[char] != char:\n grap... | trial_gpt4o_0 | {
"original_instruction": "",
"prompt_id": "0000000141",
"row_id": 141,
"seed_ids": "['1372', '2248', '646']"
} | 0.709274 | Leetcode_141 | I | [
"find_representative",
"union",
"num_similar_groups"
] | 3 | [
[
"num_similar_groups",
"find_representative"
],
[
"num_similar_groups",
"union"
],
[
"union",
"find_representative"
]
] | 3 | [] | false | [] | false | pytest | true | [] | true | 9af00427ce2261706498ee6c1c66745710237451 | 57562604a30481052b4082066a2997f77d89920c | |||
v1.0 | instruct | Leetcode | Leetcode_151_I | You are given a list of strings `words` and a string `s`. We define a **palindrome pair** to be a pair of indices `(i, j)` such that the concatenation of `words[i]` and `words[j]` forms a palindrome. For example, if `words = ["bat", "tab", "cat"]` and `s = "battab"`, the valid palindrome pairs are `(0, 1)` and `(1, 0)`... | def is_palindrome(word):
"""
Helper function to check if a word is a palindrome.
"""
return word == word[::-1]
def palindrome_pairs(words):
"""
Returns the number of distinct palindrome pairs in the list of words.
"""
count = 0
for i in range(len(words)):
for j in range(len(... | from solution import palindrome_pairs, is_palindrome
def test_is_palindrome():
assert is_palindrome("racecar") == True
assert is_palindrome("hello") == False
assert is_palindrome("civic") == True
assert is_palindrome("level") == True
assert is_palindrome("noon") == True
assert is_palindrome("wo... | [
{
"docstring": "Helper function to check if a word is a palindrome.",
"function_declaration": "def is_palindrome(word):",
"function_name": "is_palindrome",
"parameter_list": "(word)"
},
{
"docstring": "Returns the number of distinct palindrome pairs in the list of words.",
"function_decl... | [
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_palindrome(s):\n \"\"\"\n Helper function to check if a given string is a palindrome.\n \"\"\"\n return s == s[::-1]\n\ndef count_palindrome_pairs(words):\... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000000151",
"row_id": 151,
"seed_ids": "['1994', '1284', '1178']"
} | 0.758302 | Leetcode_151 | I | [
"is_palindrome",
"palindrome_pairs"
] | 2 | [
[
"palindrome_pairs",
"is_palindrome"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | e2a6622f5c286368e0c41b88999204c75652acaa | 9d08e6eae500d8ca474bf2b6ad36e3746dafcc30 | |||
v1.0 | instruct | Leetcode | Leetcode_157_I | You are given a list of strings `words` where each word consists of lowercase English letters. Your task is to find the longest string in `words` that can be constructed by concatenating other strings from the list. If there are multiple possible results, return the lexicographically smallest one. Each word in the list... | def can_construct(word, word_set):
if word in word_set:
return True
for i in range(1, len(word)):
prefix = word[:i]
suffix = word[i:]
if prefix in word_set and can_construct(suffix, word_set):
return True
return False
def find_longest_concatenated_word(words):
... | from solution import find_longest_concatenated_word
def test_basic_case():
words = ["cat", "banana", "dog", "nana", "walk", "walker", "dogwalker"]
assert find_longest_concatenated_word(words) == "dogwalker"
def test_multiple_possible_results():
words = ["rat", "bat", "cat", "ratbatcat", "batcatrat"]
a... | [
{
"docstring": null,
"function_declaration": "def can_construct(word, word_set):",
"function_name": "can_construct",
"parameter_list": "(word, word_set)"
},
{
"docstring": null,
"function_declaration": "def find_longest_concatenated_word(words):",
"function_name": "find_longest_conca... | [
1,
1,
1,
1,
0,
0,
1,
0,
1,
1
] | 7 | easy | 0.7 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def find_longest_concatenated_word(words):\n \"\"\"\n Finds the longest word that can be constructed by concatenating other words in the list.\n If there are multipl... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000000157",
"row_id": 157,
"seed_ids": "['332', '1992', '2053']"
} | 0.708783 | Leetcode_157 | I | [
"can_construct",
"find_longest_concatenated_word"
] | 2 | [
[
"find_longest_concatenated_word",
"can_construct"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | fd99e6c48139d6a530019c91070edb9ca5cd52b6 | 06927af33ed121ea9130817d05cb95c2405430f7 | |||
v1.0 | instruct | Leetcode | Leetcode_226_I | You are given an array `points` where `points[i] = [xi, yi]` represents the coordinates of a point on a 2D plane. You need to find the pair of points that are the farthest from each other based on Euclidean distance. Return the coordinates of these two points. If multiple pairs of points have the same maximum distance,... | from itertools import combinations
from math import sqrt
def euclidean_distance(point1, point2):
return sqrt((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2)
def farthest_points(points):
max_distance = -1
result_pair = ()
for point1, point2 in combinations(points, 2):
distance = eu... | from solution import farthest_points
def test_farthest_points_simple():
points = [[0, 0], [1, 1], [2, 2]]
assert farthest_points(points) == ([0, 0], [2, 2])
def test_farthest_points_single_pair():
points = [[0, 0], [3, 4]]
assert farthest_points(points) == ([0, 0], [3, 4])
def test_farthest_points_sa... | [
{
"docstring": null,
"function_declaration": "def euclidean_distance(point1, point2):",
"function_name": "euclidean_distance",
"parameter_list": "(point1, point2)"
},
{
"docstring": null,
"function_declaration": "def farthest_points(points):",
"function_name": "farthest_points",
... | [
1,
0,
1,
0,
1,
0,
0,
0,
0,
0
] | 3 | hard | 0.3 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "import math\n\ndef euclidean_distance(point1, point2):\n \"\"\"\n Calculate and return the Euclidean distance between two points.\n \"\"\"\n return math.sqrt((poi... | trial_gpt4o_4 | {
"original_instruction": "",
"prompt_id": "0000000226",
"row_id": 226,
"seed_ids": "['1333', '1960', '860']"
} | 0.758344 | Leetcode_226 | I | [
"euclidean_distance",
"farthest_points"
] | 2 | [
[
"farthest_points",
"euclidean_distance"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | a38a541dd6c70376f21ee43ffd1a7f53b905c9e3 | e1a5e8a1ae6236e26f5fad1773bafc219fe4d178 | |||
v1.0 | instruct | Leetcode | Leetcode_243_I | You are given a list of **weighted edges** representing an undirected graph and an integer `n`, which corresponds to the number of nodes (numbered from `0` to `n-1`). Each edge is represented as a triplet `[u, v, w]` where `u` and `v` denote the two nodes the edge connects, and `w` denotes the weight of that edge. Retu... | def find(parent, i):
# A utility function to find the parent of an element i
if parent[i] == i:
return i
else:
return find(parent, parent[i])
def union(parent, rank, x, y):
# A utility function to do union of two subsets x and y
xroot = find(parent, x)
yroot = find(parent, y)
... | from solution import kruskal_mst
def test_kruskal_mst_basic():
edges = [
[0, 1, 10],
[0, 2, 6],
[0, 3, 5],
[1, 3, 15],
[2, 3, 4]
]
n = 4
assert kruskal_mst(edges, n) == 19
def test_kruskal_mst_disconnected():
edges = [
[0, 1, 10],
[2, 3, 5]
... | [
{
"docstring": null,
"function_declaration": "def find(parent, i):",
"function_name": "find",
"parameter_list": "(parent, i)"
},
{
"docstring": null,
"function_declaration": "def union(parent, rank, x, y):",
"function_name": "union",
"parameter_list": "(parent, rank, x, y)"
},
... | [
1,
0,
0,
0,
1,
0,
0,
1
] | 3 | medium | 0.375 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def find(parent, i):\n # A utility function to find the parent of an element i\n if parent[i] == i:\n return i\n else:\n return find(parent, parent[i])... | trial_gpt4o_0 | {
"original_instruction": "",
"prompt_id": "0000000243",
"row_id": 243,
"seed_ids": "['1855', '241', '1183']"
} | 0.776708 | Leetcode_243 | I | [
"find",
"union",
"kruskal_mst"
] | 3 | [
[
"kruskal_mst",
"find"
],
[
"kruskal_mst",
"union"
],
[
"union",
"find"
]
] | 3 | [] | false | [] | false | pytest | true | [] | true | f523de6228ef47e27f92c5b79a3da56f9dccfce0 | 1bdcc005395bbdb91a1628404ac396f13b8e4a1b | |||
v1.0 | instruct | Leetcode | Leetcode_281_I | You are given a binary tree with `n` nodes labeled from `1` to `n`. Each node has a value associated with it, represented by an array `values` where `values[i]` is the value of the `i-th` node. You are also given the `edges` array where `edges[i] = [u, v]` indicates there is an edge between nodes `u` and `v` in the tre... | class TreeNode:
def __init__(self, value):
self.value = value
self.adj = []
def build_tree(n, values, edges):
nodes = [TreeNode(values[i]) for i in range(n)]
for u, v in edges:
nodes[u-1].adj.append(nodes[v-1])
nodes[v-1].adj.append(nodes[u-1])
return nodes[0]
def max_p... | from solution import max_path_sum
def test_example_case_1():
values = [0, 1, 2, 3, 4]
edges = [[1, 2], [1, 3], [3, 4], [4, 5]]
assert max_path_sum(5, values, edges) == 10
def test_single_node():
values = [5]
edges = []
assert max_path_sum(1, values, edges) == 5
def test_disconnected_nodes():
... | [] | [
0,
1,
0,
0,
0,
0,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def max_path_sum(values, edges):\n from collections import defaultdict\n import sys\n\n sys.setrecursionlimit(10**6)\n\n # Build the graph from the edges\n gra... | trial_gpt4o_1 | {
"original_instruction": "",
"prompt_id": "0000000281",
"row_id": 281,
"seed_ids": "['518', '834', '425']"
} | 0.737154 | Leetcode_281 | I | [
"build_tree",
"max_path_sum"
] | 2 | [
[
"max_path_sum",
"build_tree"
]
] | 1 | [] | false | [
"TreeNode"
] | true | pytest | true | [] | true | a13f112596e2a5feb546bb1b4d85994da6a2b641 | 3bb84319db1e1f1f67a705d91d13da412c4015d6 | |||
v1.0 | instruct | Leetcode | Leetcode_282_I | Given two strings `s` and `t` consisting of only lowercase alphabets, write a function to determine if `s` can be transformed into `t` by performing at most one edit. An edit is defined as:
1. Inserting a character
2. Removing a character
3. Replacing a character | def is_one_edit_away(s, t):
"""
Returns True if string s can be transformed into string t by performing at most one edit.
An edit is defined as: inserting a character, removing a character, or replacing a character.
"""
len_s = len(s)
len_t = len(t)
# If the length difference is more th... | from solution import is_one_edit_away
def test_no_edits_needed():
assert is_one_edit_away('abc', 'abc') == True
assert is_one_edit_away('', '') == True
def test_one_replacement():
assert is_one_edit_away('abc', 'abx') == True
assert is_one_edit_away('abcd', 'abcf') == True
def test_one_insertion():
... | [
{
"docstring": "Returns True if string s can be transformed into string t by performing at most one edit.\nAn edit is defined as: inserting a character, removing a character, or replacing a character.",
"function_declaration": "def is_one_edit_away(s, t):",
"function_name": "is_one_edit_away",
"para... | [
0,
0,
1,
0,
0,
0,
0,
1,
1,
0
] | 3 | hard | 0.3 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_one_edit_distance(s, t):\n \"\"\"\n Returns True if string s can be transformed into string t by performing at most one edit, otherwise False.\n \"\"\"\n l... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000000282",
"row_id": 282,
"seed_ids": "['100', '1602', '419']"
} | 0.770701 | Leetcode_282 | I | [
"is_one_edit_away",
"check_one_edit_insert_remove"
] | 2 | [
[
"is_one_edit_away",
"check_one_edit_insert_remove"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 03126e63d5302fa14b32982adfcf65fdbd02abe7 | 5ed14284146980785d1e2a6c63c66a783961546e | |||
v1.0 | instruct | Leetcode | Leetcode_336_I | Given a `m x n` grid containing integers, you need to find the maximum sum of any rectangle within the grid. The rectangle must be made up of at least one element. Your task is to write a function that returns the maximum possible sum considering all possible rectangles that can be formed within the grid. | def max_sum_rectangle(grid):
"""
Returns the maximum sum of any rectangle within the given grid.
"""
if not grid or not grid[0]:
return 0
max_sum = float('-inf')
rows, cols = len(grid), len(grid[0])
for left in range(cols):
row_sum = [0] * rows
for right in ... | from solution import max_sum_rectangle
def test_single_element():
assert max_sum_rectangle([[2]]) == 2
def test_all_negative_elements():
grid = [
[-1, -2],
[-3, -4]
]
assert max_sum_rectangle(grid) == -1
def test_mixed_elements():
grid = [
[2, 1, -3, -4, 5],
[0, 6,... | [
{
"docstring": "Returns the maximum sum of any rectangle within the given grid.",
"function_declaration": "def max_sum_rectangle(grid):",
"function_name": "max_sum_rectangle",
"parameter_list": "(grid)"
},
{
"docstring": "Helper function to find the maximum sum of any contiguous subarray.",
... | [
0,
0,
0,
0,
1,
0,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def max_sum_subgrid(grid):\n \"\"\"\n Given a m x n grid containing integers, find the maximum sum of any rectangle within the grid.\n \n :param grid: List[List[i... | trial_gpt4o_4 | {
"original_instruction": "",
"prompt_id": "0000000336",
"row_id": 336,
"seed_ids": "['327', '1216', '1754']"
} | 0.753827 | Leetcode_336 | I | [
"max_sum_rectangle",
"max_subarray_sum"
] | 2 | [
[
"max_sum_rectangle",
"max_subarray_sum"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | be54124904e961f7676bc421cf8f76283a3e525c | 1e2b7d7e8270b526d5ffaa6647abda02b1959b13 | |||
v1.0 | instruct | Leetcode | Leetcode_468_I | You are given a **0-indexed** array `words` of strings. A string is considered "balanced" if every letter it contains appears the same number of times. Return an array of the same length as `words` where each element is `true` if the corresponding string in `words` is balanced or `false` otherwise. | def is_balanced_word(word):
"""
Checks if a word is balanced.
A word is balanced if each character appears the same number of times.
"""
from collections import Counter
char_count = Counter(word)
counts = list(char_count.values())
return all(count == counts[0] for count in counts)
... | from solution import check_balanced_words, is_balanced_word
def test_is_balanced_word():
assert is_balanced_word("aabbcc") == True
assert is_balanced_word("abac") == False
assert is_balanced_word("xyzxyz") == True
assert is_balanced_word("abcab") == False
assert is_balanced_word("") == True # Empt... | [
{
"docstring": "Checks if a word is balanced.\n\nA word is balanced if each character appears the same number of times.",
"function_declaration": "def is_balanced_word(word):",
"function_name": "is_balanced_word",
"parameter_list": "(word)"
},
{
"docstring": "Given a list of words, this func... | [
0,
1,
0,
1,
0,
0,
0,
0,
0,
0
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_balanced(word):\n \"\"\"\n Check if a word is balanced. A word is balanced if every letter it contains appears the same \n number of times.\n \"\"\"\n f... | trial_gpt4o_3 | {
"original_instruction": "",
"prompt_id": "0000000468",
"row_id": 468,
"seed_ids": "['1207', '2283', '2438']"
} | 0.685849 | Leetcode_468 | I | [
"is_balanced_word",
"check_balanced_words"
] | 2 | [
[
"check_balanced_words",
"is_balanced_word"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 2c9fe1b611f9cb4f57aef883aa8db9966f5008fb | 4f59cd776fd1a1cbb51ca3d7c218046ea62c79ac | |||
v1.0 | instruct | Leetcode | Leetcode_598_I | You are given a string `s` consisting of lowercase alphabets and a dictionary of words `dict` consisting of unique lowercase words. Your task is to find and return the length of the longest word in the dictionary that is a subsequence of `s`. A subsequence is a sequence that can be derived from another sequence by dele... | def is_subsequence(s, t):
"""
Check if t is a subsequence of s.
"""
it = iter(s)
return all(char in it for char in t)
def longest_subsequence(s, words):
"""
Returns the length of the longest word in the dictionary that is a subsequence of s.
"""
max_length = 0
for word in words:... | from solution import is_subsequence, longest_subsequence
def test_is_subsequence():
assert is_subsequence("abcde", "ace")
assert not is_subsequence("abcde", "aec")
assert is_subsequence("abc", "abc")
assert is_subsequence("abracadabra", "ada")
def test_longest_subsequence():
s = "abcde"
words ... | [
{
"docstring": "Check if t is a subsequence of s.",
"function_declaration": "def is_subsequence(s, t):",
"function_name": "is_subsequence",
"parameter_list": "(s, t)"
},
{
"docstring": "Returns the length of the longest word in the dictionary that is a subsequence of s.",
"function_decla... | [
1,
1,
0,
1,
1,
0,
0,
1,
1,
1
] | 7 | easy | 0.7 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_subsequence(s, word):\n \"\"\"\n Checks if `word` is a subsequence of `s`.\n \"\"\"\n it = iter(s)\n return all(char in it for char in word)\n\ndef long... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000000598",
"row_id": 598,
"seed_ids": "['2288', '2517', '2125']"
} | 0.735206 | Leetcode_598 | I | [
"is_subsequence",
"longest_subsequence"
] | 2 | [
[
"longest_subsequence",
"is_subsequence"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 29b9522b5872b4175985e6cf078232305e58359e | 831eb9f860b729d0d74e8d086bd143163a70227e | |||
v1.0 | instruct | Leetcode | Leetcode_754_I | Given a list of integers `heights`, where each element represents the height of a person, return the minimum number of moves required to arrange the list in increasing order by height. A **move** consists of picking any person from the list and moving them to any other position in the list. | def minimum_moves_to_sort(heights):
"""
Returns the minimum number of moves required to sort the list in increasing order by height.
"""
sorted_heights = sorted(heights)
lcs_length = longest_common_subsequence(heights, sorted_heights)
return len(heights) - lcs_length
def longest_common_subseque... | from solution import minimum_moves_to_sort
def test_sorted_list():
assert minimum_moves_to_sort([1, 2, 3, 4, 5]) == 0
def test_reverse_sorted_list():
assert minimum_moves_to_sort([5, 4, 3, 2, 1]) == 4
def test_unsorted_list():
assert minimum_moves_to_sort([3, 1, 2, 5, 4]) == 2
def test_single_element_li... | [
{
"docstring": "Returns the minimum number of moves required to sort the list in increasing order by height.",
"function_declaration": "def minimum_moves_to_sort(heights):",
"function_name": "minimum_moves_to_sort",
"parameter_list": "(heights)"
},
{
"docstring": "Helper function to find the... | [
0,
0,
0,
0,
0,
0,
0,
0,
1,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def min_moves_to_sort_heights(heights):\n \"\"\"\n Returns the minimum number of moves required to arrange the list in increasing order by height.\n \"\"\"\n sort... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000000754",
"row_id": 754,
"seed_ids": "['1246', '770', '1262']"
} | 0.580357 | Leetcode_754 | I | [
"minimum_moves_to_sort",
"longest_common_subsequence"
] | 2 | [
[
"minimum_moves_to_sort",
"longest_common_subsequence"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 28e2984e8734a068ebf06fb67d16468ec5ee5115 | c9b4b42c60f744cf56866eef8b93ce2ba71cdfe6 | |||
v1.0 | instruct | Leetcode | Leetcode_865_I | You are given a list of cities, and between certain pairs of cities, there is a direct road. Each city is represented as a node in a graph, and each road is a bidirectional edge between two nodes. Unfortunately, some roads are unreliable and may be temporarily unusable. Given the graph represented as an adjacency list ... | def find(parent, i):
if parent[i] == i:
return i
return find(parent, parent[i])
def union(parent, rank, x, y):
xroot = find(parent, x)
yroot = find(parent, y)
if rank[xroot] < rank[yroot]:
parent[xroot] = yroot
elif rank[xroot] > rank[yroot]:
parent[yroot] = xroot
e... | from solution import minimal_edges_to_add
def test_no_new_edges_needed():
num_cities = 4
edges = [(0, 1), (1, 2), (2, 3)]
unreliable_edges = []
assert minimal_edges_to_add(num_cities, edges, unreliable_edges) == 0
def test_single_unreliable_edge():
num_cities = 4
edges = [(0, 1), (1, 2), (2, 3... | [
{
"docstring": null,
"function_declaration": "def find(parent, i):",
"function_name": "find",
"parameter_list": "(parent, i)"
},
{
"docstring": null,
"function_declaration": "def union(parent, rank, x, y):",
"function_name": "union",
"parameter_list": "(parent, rank, x, y)"
},
... | [
0,
0,
0,
0,
0,
0,
0,
0,
1,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def find(parent, i):\n if parent[i] == i:\n return i\n else:\n return find(parent, parent[i])\n\ndef union(parent, x, y):\n rootX = find(parent, x)\n ... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000000865",
"row_id": 865,
"seed_ids": "['1547', '1416', '2274']"
} | 0.587638 | Leetcode_865 | I | [
"find",
"union",
"minimal_edges_to_add"
] | 3 | [
[
"minimal_edges_to_add",
"find"
],
[
"minimal_edges_to_add",
"union"
],
[
"union",
"find"
]
] | 3 | [] | false | [] | false | pytest | true | [] | true | a153c6032e9fabdb7e539457c452c3cdb970a8b1 | fbe0030fe52d99ead3ce7794102360b66f1bf439 | |||
v1.0 | instruct | Leetcode | Leetcode_1253_I | You are given a string `s` which represents a sequence of characters. You need to create a **balanced binary tree** from the characters of the string such that the characters appear in the same order. A binary tree is balanced if the length of the left and right subtrees of every node differs by at most one.
The char... | class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def build_balanced_tree(s):
if not s:
return None
nodes = [TreeNode(char) for char in s]
root = nodes[0]
queue = [root]
index = 1
while in... | from solution import preorder_from_string
def test_balanced_tree_single_char():
assert preorder_from_string("a") == "a"
def test_balanced_tree_two_chars():
assert preorder_from_string("ab") == "ab"
def test_balanced_tree_three_chars():
assert preorder_from_string("abc") == "abc"
def test_balanced_tr... | [] | [
0,
0,
0,
0,
0,
1,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "class TreeNode:\n def __init__(self, val=''):\n self.val = val\n self.left = None\n self.right = None\n\ndef build_balanced_tree(s):\n if not s:\n ... | trial_gpt4o_5 | {
"original_instruction": "",
"prompt_id": "0000001253",
"row_id": 1253,
"seed_ids": "['756', '1688', '2001']"
} | 0.618731 | Leetcode_1253 | I | [
"build_balanced_tree",
"preorder_traversal",
"preorder_from_string"
] | 3 | [
[
"preorder_from_string",
"build_balanced_tree"
],
[
"preorder_from_string",
"preorder_traversal"
]
] | 2 | [] | false | [
"TreeNode"
] | true | pytest | true | [] | true | 7e04e4941c162d1218b9ded262da557ab3e9eb00 | f008ec55ba8274424960a8137fcf5fcc161602a8 | |||
v1.0 | instruct | Leetcode | Leetcode_1259_I | You are given two integers `a` and `b`, and you need to find the sum of all prime numbers between `a` and `b` (both inclusive). Write a function that accepts two integers as input and returns the sum of the prime numbers within the given range.
For example, if the input integers are `a = 10` and `b = 20`, the prime nu... | def is_prime(n):
"""
Check if a number is prime.
"""
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
for i in range(3, int(n**0.5) + 1, 2):
if n % i == 0:
return False
return True
def sum_of_primes(a, b):
"""
... | from solution import sum_of_primes
def test_sum_of_primes_basic():
assert sum_of_primes(10, 20) == 60 # Primes: 11, 13, 17, 19 => Sum: 60
def test_sum_of_primes_no_primes():
assert sum_of_primes(8, 10) == 0 # No primes between 8 and 10
def test_sum_of_primes_single_prime():
assert sum_of_primes(17, 17)... | [
{
"docstring": "Check if a number is prime.",
"function_declaration": "def is_prime(n):",
"function_name": "is_prime",
"parameter_list": "(n)"
},
{
"docstring": "Returns the sum of all prime numbers between a and b (inclusive).",
"function_declaration": "def sum_of_primes(a, b):",
"f... | [
1,
1,
0,
0,
0,
0,
1,
0,
0,
0
] | 3 | hard | 0.3 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_prime(n):\n \"\"\"\n Check if a number is a prime.\n \"\"\"\n if n <= 1:\n return False\n if n <= 3:\n return True\n if n % 2 == 0 or n... | trial_gpt4o_6 | {
"original_instruction": "",
"prompt_id": "0000001259",
"row_id": 1259,
"seed_ids": "['190', '2094', '1727']"
} | 0.616231 | Leetcode_1259 | I | [
"is_prime",
"sum_of_primes"
] | 2 | [
[
"sum_of_primes",
"is_prime"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | dfb6b0abaa62d109e0aefeb44e7e76811a9975c7 | ae062fae16b365cf7dec7ccf2cc37d26911a0c95 | |||
v1.0 | instruct | Leetcode | Leetcode_1326_I | You are given a list of strings `words` and a string `pattern`. Determine if the pattern appears as a subsequence in all of the strings within the list. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. Return a li... | def is_subsequence(pattern, word):
"""
Determine if the pattern is a subsequence of the word.
"""
it = iter(word)
return all(char in it for char in pattern)
def is_pattern_in_all_words(words, pattern):
"""
Determine if the pattern is a subsequence in each word in the words list.
"""
... | from solution import is_pattern_in_all_words
def test_is_pattern_in_all_words():
words = ["abcde", "ace", "aec", "bdf"]
pattern = "ace"
assert is_pattern_in_all_words(words, pattern) == [True, True, False, False]
def test_is_pattern_in_all_words_empty_pattern():
words = ["abcde", "ace", "aec", "bdf"]
... | [
{
"docstring": "Determine if the pattern is a subsequence of the word.",
"function_declaration": "def is_subsequence(pattern, word):",
"function_name": "is_subsequence",
"parameter_list": "(pattern, word)"
},
{
"docstring": "Determine if the pattern is a subsequence in each word in the words... | [
1,
1,
0,
1,
0,
0,
1,
1,
0
] | 5 | medium | 0.556 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_subsequence(pattern, word):\n \"\"\"\n Helper function that checks if pattern is a subsequence of word.\n \"\"\"\n iter_word = iter(word)\n return all(c... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000001326",
"row_id": 1326,
"seed_ids": "['419', '1567', '1473']"
} | 0.636843 | Leetcode_1326 | I | [
"is_subsequence",
"is_pattern_in_all_words"
] | 2 | [
[
"is_pattern_in_all_words",
"is_subsequence"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 4cd30de7bd2d8bd14651e3c0969030324ad9feb2 | fbe5e59b806744e6a6d680b93db3d3ae890cea34 | |||
v1.0 | instruct | Leetcode | Leetcode_1457_I | You are given an array of integers `nums` and an integer `k`. Return the maximum possible sum of a non-empty subsequence of `nums` such that the greatest common divisor (GCD) of the elements of the subsequence is exactly `k`. A **subsequence** is a sequence derived from the array by deleting some or no elements without... | from math import gcd
from functools import reduce
def gcd_of_list(lst):
return reduce(gcd, lst)
def max_sum_subsequence_with_gcd(nums, k):
valid_nums = [num for num in nums if num % k == 0]
max_sum = 0
def helper(temp, index):
nonlocal max_sum
if temp and gcd_of_list(temp) == k:
... | from solution import max_sum_subsequence_with_gcd
def test_case_1():
nums = [2, 4, 8, 16]
k = 2
assert max_sum_subsequence_with_gcd(nums, k) == 30 # 2 + 4 + 8 + 16 = 30
def test_case_2():
nums = [12, 15, 18, 30, 27]
k = 3
assert max_sum_subsequence_with_gcd(nums, k) == 102 # 12 + 15 + 18 + 3... | [
{
"docstring": null,
"function_declaration": "def gcd_of_list(lst):",
"function_name": "gcd_of_list",
"parameter_list": "(lst)"
},
{
"docstring": null,
"function_declaration": "def max_sum_subsequence_with_gcd(nums, k):",
"function_name": "max_sum_subsequence_with_gcd",
"paramete... | [
0,
0,
1,
0,
1,
0,
1,
1,
1,
1
] | 6 | medium | 0.6 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "import math\nfrom typing import List\n\ndef max_sum_subsequence_with_gcd_k(nums: List[int], k: int) -> int:\n \"\"\"\n Returns the maximum possible sum of a non-empty s... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000001457",
"row_id": 1457,
"seed_ids": "['2484', '1419', '1284']"
} | 0.796906 | Leetcode_1457 | I | [
"gcd_of_list",
"max_sum_subsequence_with_gcd"
] | 2 | [
[
"max_sum_subsequence_with_gcd",
"gcd_of_list"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | d3668f2cc960762d65d46660f69273f5693b393a | 8e556a17348d60e0dc36ddceadb20c3dc4d6d0bf | |||
v1.0 | instruct | Leetcode | Leetcode_1559_I | You are given an array `nums` of integers, where `nums[i]` represents the position of the `i`-th person in a line. Each person can only move to a position such that the distance between the person's original and new positions is at most `k` units. Calculate the minimum number of moves required to sort the array in non-... | def count_inversions_and_sort(nums):
if len(nums) < 2:
return nums, 0
mid = len(nums) // 2
left, left_inversions = count_inversions_and_sort(nums[:mid])
right, right_inversions = count_inversions_and_sort(nums[mid:])
merged, split_inversions = merge_and_count(left, right)
total... | from solution import min_moves_to_sort
def test_min_moves_to_sort_already_sorted():
assert min_moves_to_sort([1, 2, 3, 4], 1) == 0
def test_min_moves_to_sort_reverse_ordered():
assert min_moves_to_sort([4, 3, 2, 1], 1) == 6
def test_min_moves_to_sort_random_order():
assert min_moves_to_sort([3, 1, 2, 4, ... | [
{
"docstring": null,
"function_declaration": "def count_inversions_and_sort(nums):",
"function_name": "count_inversions_and_sort",
"parameter_list": "(nums)"
},
{
"docstring": null,
"function_declaration": "def merge_and_count(left, right):",
"function_name": "merge_and_count",
"... | [
0,
0,
0,
0,
0,
1,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def min_moves_to_sort(nums, k):\n \"\"\"\n Returns the minimum number of moves required to sort the array nums in non-decreasing order.\n Each move consists of swapp... | trial_gpt4o_5 | {
"original_instruction": "",
"prompt_id": "0000001559",
"row_id": 1559,
"seed_ids": "['2375', '1461', '1078']"
} | 0.648993 | Leetcode_1559 | I | [
"count_inversions_and_sort",
"merge_and_count",
"min_moves_to_sort"
] | 3 | [
[
"count_inversions_and_sort",
"merge_and_count"
],
[
"min_moves_to_sort",
"count_inversions_and_sort"
]
] | 2 | [] | false | [] | false | pytest | true | [] | true | fe268a6536a1fc5df213499e098ed245365c3a47 | 4dfeba03de573f83ef1c2f8e332fc9624f1ea39d | |||
v1.0 | instruct | Leetcode | Leetcode_1809_I | Given two integers `x` and `y`, find a pair of integers `(a, b)` such that `1 <= a <= x` and `1 <= b <= y`, where the values of `a` and `b` are prime numbers and the value of `a * b` is maximum. Return the pair `(a, b)`. If there are multiple pairs with the same product, return the pair with the smallest `a`. If no suc... | def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def find_max_prime_pair(x, y):
primes_x = ... | from solution import find_max_prime_pair
def test_find_max_prime_pair():
assert find_max_prime_pair(10, 10) == (7, 7)
assert find_max_prime_pair(5, 5) == (5, 5)
assert find_max_prime_pair(0, 0) == (-1, -1)
assert find_max_prime_pair(1, 10) == (-1, -1)
assert find_max_prime_pair(13, 2) == (13, 2)
... | [
{
"docstring": null,
"function_declaration": "def is_prime(n):",
"function_name": "is_prime",
"parameter_list": "(n)"
},
{
"docstring": null,
"function_declaration": "def find_max_prime_pair(x, y):",
"function_name": "find_max_prime_pair",
"parameter_list": "(x, y)"
}
] | [
0,
0,
0,
0,
1,
1,
0,
0,
0,
0
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_prime(n):\n if n <= 1:\n return False\n if n <= 3:\n return True\n if n % 2 == 0 or n % 3 == 0:\n return False\n i = 5\n while i * ... | trial_gpt4o_5 | {
"original_instruction": "",
"prompt_id": "0000001809",
"row_id": 1809,
"seed_ids": "['2510', '1933', '1424']"
} | 0.676932 | Leetcode_1809 | I | [
"is_prime",
"find_max_prime_pair"
] | 2 | [
[
"find_max_prime_pair",
"is_prime"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 72550eb3f5fc8ab79b85d2f99590a716fb9f7c0e | 7faff3218add3512b50f0ca9941c80138ea419ad | |||
v1.0 | instruct | Leetcode | Leetcode_1816_I | You are given a linked list where each node contains an integer value. Write a function that returns the head of the linked list after sorting it in ascending order using merge sort. The function should preserve the original structure and only rearrange the node values. | class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def merge_sort_linked_list(head):
if not head or not head.next:
return head
# Find the middle of the linked list
slow, fast = head, head.next
while fast and fast.next:
slow = slow.... | from solution import ListNode, merge_sort_linked_list
def list_to_linked_list(lst):
dummy = ListNode()
current = dummy
for num in lst:
current.next = ListNode(num)
current = current.next
return dummy.next
def linked_list_to_list(node):
lst = []
while node:
lst.append(no... | [] | [
1,
1,
1,
1,
1,
1,
1,
1,
1
] | 9 | easy | 1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "class ListNode:\n def __init__(self, value=0, next=None):\n self.value = value\n self.next = next\n\ndef merge_sort_list(head):\n if not head or not head.... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000001816",
"row_id": 1816,
"seed_ids": "['1302', '1354', '2196']"
} | 0.547565 | Leetcode_1816 | I | [
"merge_sort_linked_list",
"merge"
] | 2 | [
[
"merge_sort_linked_list",
"merge"
]
] | 1 | [] | false | [
"ListNode"
] | true | pytest | true | [] | true | cb764733eac13af3a3c4042edc7221b457637371 | 9ead42c90770bd61eb5d93069fbc14b745add91c | |||
v1.0 | instruct | Leetcode | Leetcode_1819_I | You are given a list of `n` books, where each book has a unique integer `pages[i]` representing the number of pages in the `i`th book. You want to distribute these books to `k` students such that each student gets at least one book and the maximum number of pages assigned to any student is minimized. Each book must be ... | def is_feasible(pages, n, k, max_pages):
students_required = 1
current_pages = 0
for i in range(n):
if current_pages + pages[i] > max_pages:
students_required += 1
current_pages = pages[i]
if students_required > k:
return False
else:
... | from solution import min_max_pages
def test_min_max_pages_case1():
pages = [12, 34, 67, 90]
n = 4
k = 2
assert min_max_pages(pages, n, k) == 113
def test_min_max_pages_case2():
pages = [10, 20, 30, 40]
n = 4
k = 2
assert min_max_pages(pages, n, k) == 60
def test_min_max_pages_case3():... | [
{
"docstring": null,
"function_declaration": "def is_feasible(pages, n, k, max_pages):",
"function_name": "is_feasible",
"parameter_list": "(pages, n, k, max_pages)"
},
{
"docstring": null,
"function_declaration": "def min_max_pages(pages, n, k):",
"function_name": "min_max_pages",
... | [
1,
1,
1,
0,
0,
0,
0,
1,
0,
1
] | 5 | medium | 0.5 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_valid_distribution(pages, n, k, max_pages):\n student_count = 1\n current_pages = 0\n \n for i in range(n):\n if current_pages + pages[i] > max_page... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000001819",
"row_id": 1819,
"seed_ids": "['1659', '1772', '875']"
} | 0.666133 | Leetcode_1819 | I | [
"is_feasible",
"min_max_pages"
] | 2 | [
[
"min_max_pages",
"is_feasible"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | adfc04c89c71b853304c2b57cd5d39cc11824036 | bae09363c461b140241cf82ec583146ee0a18f3f | |||
v1.0 | instruct | Leetcode | Leetcode_2016_I | Given an array of strings `words`, each string consisting of lowercase English letters, return _the number of pairs of distinct indices_ `(i, j)` _such that the concatenation of_ `words[i] + words[j]` _forms a palindrome_. | def is_palindrome(s):
"""
Check if the given string s is a palindrome.
"""
return s == s[::-1]
def palindrome_pairs(words):
"""
Returns the number of pairs of distinct indices (i, j) such that the
concatenation of words[i] + words[j] forms a palindrome.
"""
count = 0
n = len(wor... | from solution import palindrome_pairs
def test_no_palindromic_pairs():
assert palindrome_pairs(["abc", "def", "ghi"]) == 0
def test_single_pair():
assert palindrome_pairs(["bat", "tab", "cat"]) == 2
def test_multiple_pairs():
assert palindrome_pairs(["a", ""]) == 2
def test_custom_case():
assert pal... | [
{
"docstring": "Check if the given string s is a palindrome.",
"function_declaration": "def is_palindrome(s):",
"function_name": "is_palindrome",
"parameter_list": "(s)"
},
{
"docstring": "Returns the number of pairs of distinct indices (i, j) such that the\nconcatenation of words[i] + words... | [
0,
1,
0,
0,
0,
0,
0,
1,
0,
0
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_palindrome(s):\n \"\"\"\n Checks if a given string s is a palindrome.\n \"\"\"\n return s == s[::-1]\n\ndef count_palindromic_pairs(words):\n \"\"\"\n ... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000002016",
"row_id": 2016,
"seed_ids": "['1895', '829', '275']"
} | 0.774273 | Leetcode_2016 | I | [
"is_palindrome",
"palindrome_pairs"
] | 2 | [
[
"palindrome_pairs",
"is_palindrome"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | f4c0c461c8bb3448aa9423fe4da65aaffe430d48 | b8951fdd8406c3c37842e789e59528d4bdd82747 | |||
v1.0 | instruct | Leetcode | Leetcode_2188_I | You are given a 2D grid of size `m x n` representing a map where `1` represents land and `0` represents water. A **path** from the top-left corner to the bottom-right corner of the grid is a series of adjacent cells (horizontally or vertically) starting from `(0, 0)` and ending at `(m-1, n-1)` where all the cells on th... | from collections import deque
# Helper function to check if position is valid and within bounds
def is_valid(x, y, m, n, grid, visited):
return 0 <= x < m and 0 <= y < n and not visited[x][y] and grid[x][y] == 1
def bfs(grid, m, n, change_pos=None):
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
visited ... | from solution import min_flips
def test_no_change_needed():
grid = [
[1, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 1],
[0, 1, 1, 1]
]
assert min_flips(grid) == 0
def test_single_change_needed():
grid = [
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 1, 0],
... | [
{
"docstring": null,
"function_declaration": "def is_valid(x, y, m, n, grid, visited):",
"function_name": "is_valid",
"parameter_list": "(x, y, m, n, grid, visited)"
},
{
"docstring": null,
"function_declaration": "def bfs(grid, m, n, change_pos=None):",
"function_name": "bfs",
"... | [
1,
0,
0,
0,
0,
0,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "from collections import deque\n\n# Helper function to check if position is valid and within bounds\ndef is_valid(x, y, m, n, grid, visited):\n return 0 <= x < m and 0 <= y... | trial_gpt4o_0 | {
"original_instruction": "",
"prompt_id": "0000002188",
"row_id": 2188,
"seed_ids": "['2079', '632', '1638']"
} | 0.674128 | Leetcode_2188 | I | [
"is_valid",
"bfs",
"min_flips"
] | 3 | [
[
"bfs",
"is_valid"
],
[
"min_flips",
"bfs"
]
] | 2 | [] | false | [] | false | pytest | true | [] | true | 40d22ceccb94b455a5c41c13185b7e1e54201227 | 673b35e15645f9d9dd6c2ff50856b3bbaaf1030b | |||
v1.0 | instruct | Leetcode | Leetcode_2292_I | You are given an array `words` consisting of strings made up of lowercase English letters. A word is considered a **palindromic word** if it can be read the same forwards and backwards. Your task is to determine the **maximum length** of a palindromic word that can be formed by concatenating any two different words fro... | def is_palindrome(word):
return word == word[::-1]
def max_palindromic_concatenation(words):
max_length = 0
n = len(words)
for i in range(n):
for j in range(i + 1, n):
if is_palindrome(words[i] + words[j]):
max_length = max(max_length, len(words[i]) + len(words[... | from solution import max_palindromic_concatenation
def test_no_palindromic_concatenation_possible():
assert max_palindromic_concatenation(['abc', 'def', 'ghi']) == 0
def test_single_palindromic_concatenation():
assert max_palindromic_concatenation(['abc', 'cba', 'def']) == 6
def test_multiple_palindromic_com... | [
{
"docstring": null,
"function_declaration": "def is_palindrome(word):",
"function_name": "is_palindrome",
"parameter_list": "(word)"
},
{
"docstring": null,
"function_declaration": "def max_palindromic_concatenation(words):",
"function_name": "max_palindromic_concatenation",
"pa... | [
1,
0,
0,
0,
0,
0,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_palindrome(word):\n return word == word[::-1]\n\ndef max_palindromic_concatenation(words):\n max_length = 0\n n = len(words)\n \n for i in range(n):\n ... | trial_gpt4o_0 | {
"original_instruction": "",
"prompt_id": "0000002292",
"row_id": 2292,
"seed_ids": "['1773', '2197', '461']"
} | 0.802012 | Leetcode_2292 | I | [
"is_palindrome",
"max_palindromic_concatenation"
] | 2 | [
[
"max_palindromic_concatenation",
"is_palindrome"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 128169593c000014f603c963fea1fc5e16a420df | e564ba23e0bcbf69f86295aed6863273009033a0 | |||
v1.0 | instruct | Leetcode | Leetcode_2308_I | Given a list of `n` words, you need to find the longest word that can be constructed by concatenating copies of any of the other words in the list. The word must be formed using at least one of the other words in the list. If there are multiple possible results, return the word that is lexicographically smallest. If no... | def can_form(word, word_set, memo):
if word in memo:
return memo[word]
for i in range(1, len(word)):
left, right = word[:i], word[i:]
if left in word_set and (right in word_set or can_form(right, word_set, memo)):
memo[word] = True
return True
memo[word] = Fal... | from solution import longest_concatenated_word
def test_longest_concatenated_word():
assert longest_concatenated_word(["cat", "dog", "catdog"]) == "catdog"
assert longest_concatenated_word(["cat", "cats", "catsdogcats", "dog", "dogs"]) == "catsdogcats"
assert longest_concatenated_word(["rat", "cat", "ratca... | [
{
"docstring": null,
"function_declaration": "def can_form(word, word_set, memo):",
"function_name": "can_form",
"parameter_list": "(word, word_set, memo)"
},
{
"docstring": null,
"function_declaration": "def longest_concatenated_word(words):",
"function_name": "longest_concatenated_... | [
0,
0,
0,
0,
1,
0,
0,
0,
0,
1
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def can_form(word, word_set):\n if word in word_set:\n return True\n dp = [False] * (len(word) + 1)\n dp[0] = True\n for i in range(1, len(word) + 1):\n ... | trial_gpt4o_4 | {
"original_instruction": "",
"prompt_id": "0000002308",
"row_id": 2308,
"seed_ids": "['1450', '979', '86']"
} | 0.664546 | Leetcode_2308 | I | [
"can_form",
"longest_concatenated_word"
] | 2 | [
[
"longest_concatenated_word",
"can_form"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 3b8d8e71c862c1526754d1d5698032f28d8967c7 | d9496ac9a86fc6b30d9a9b4bbb175e1897118088 | |||
v1.0 | instruct | Leetcode | Leetcode_2473_I | You are given a grid of characters `board` representing a Scrabble board where each cell contains either an English letter or an empty string. You are also given a list of words `words` to search for on the board. A word can be constructed from letters of sequentially adjacent cells, where "adjacent" cells are those ho... | def exist(board, word):
rows, cols = len(board), len(board[0])
def backtrack(r, c, suffix):
if len(suffix) == 0:
return True
if r < 0 or r >= rows or c < 0 or c >= cols or board[r][c] != suffix[0]:
return False
ret = False
board[r][c... | from solution import find_words
def test_find_words_single_word():
board = [
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
words = ["oath"]
assert find_words(board, words) == ["oath"]
def test_find_words_multiple_words():
board = [
... | [
{
"docstring": null,
"function_declaration": "def exist(board, word):",
"function_name": "exist",
"parameter_list": "(board, word)"
},
{
"docstring": null,
"function_declaration": "def find_words(board, words):",
"function_name": "find_words",
"parameter_list": "(board, words)"
... | [
1,
0,
0,
1,
0,
1,
1,
0,
1,
1
] | 6 | medium | 0.6 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def find_words(board, words):\n def backtrack(row, col, word, index):\n if index == len(word):\n return True\n if row < 0 or row >= len(board) or ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000002473",
"row_id": 2473,
"seed_ids": "['2300', '2284', '2237']"
} | 0.612707 | Leetcode_2473 | I | [
"exist",
"find_words"
] | 2 | [
[
"find_words",
"exist"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | fbbe85bc1c13f90083841ba8984e2cb0b6c23f54 | e742a44baa806d024484e708eac6956d407a7b68 | |||
v1.0 | instruct | Leetcode | Leetcode_2563_I | You are given a list of strings `words` and another string `s`. A word is defined as a subsequence of `s` if it can be derived by deleting some or no characters of `s` without changing the order of the remaining characters. Implement a function `findLongestSubsequence(words, s)` that returns the longest word in `words`... | def is_subsequence(word, target):
it = iter(target)
return all(char in it for char in word)
def findLongestSubsequence(words, s):
longest_word = ""
for word in words:
if is_subsequence(word, s) and (len(word) > len(longest_word) or (len(word) == len(longest_word) and words.index(word) < words.i... | from solution import findLongestSubsequence
def test_findLongestSubsequence_single_match():
words = ["apple", "banana", "cherry"]
s = "abpcplea"
assert findLongestSubsequence(words, s) == "apple"
def test_findLongestSubsequence_no_match():
words = ["xyz", "lmn"]
s = "abcdefg"
assert findLonges... | [
{
"docstring": null,
"function_declaration": "def is_subsequence(word, target):",
"function_name": "is_subsequence",
"parameter_list": "(word, target)"
},
{
"docstring": null,
"function_declaration": "def findLongestSubsequence(words, s):",
"function_name": "findLongestSubsequence",
... | [
0,
1,
1,
0,
0,
1,
0,
0,
0,
1
] | 4 | medium | 0.4 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_subsequence(word, s):\n \"\"\"\n Returns True if `word` is a subsequence of `s`, False otherwise.\n \"\"\"\n it = iter(s)\n return all(char in it for ch... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000002563",
"row_id": 2563,
"seed_ids": "['1756', '506', '1333']"
} | 0.70117 | Leetcode_2563 | I | [
"is_subsequence",
"findLongestSubsequence"
] | 2 | [
[
"findLongestSubsequence",
"is_subsequence"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 8bf13c03ff36bfc717acb5b22488e41e94abe6e5 | c4043e376c93de7c13cf92e142ba3603e282800d | |||
v1.0 | instruct | Leetcode | Leetcode_2622_I | You are given a list of strings `words`. A string `s` is called a **valid password candidate** if it meets the following criteria:
- `s` contains at least one digit.
- `s` contains both uppercase and lowercase letters.
- `s` contains at least one special character from the set `['@', '#', '$', '%', '&', '*', '!']`.
Re... | def is_valid_password_candidate(s):
"""
Checks if the given string s is a valid password candidate.
"""
contains_digit = any(c.isdigit() for c in s)
contains_uppercase = any(c.isupper() for c in s)
contains_lowercase = any(c.islower() for c in s)
contains_special = any(c in ['@', '#', '$', '... | from solution import valid_password_candidates
def test_valid_password_candidates_with_all_valid():
assert valid_password_candidates(['Abc1@', 'Xyz2$', 'Pwd3!']) == ['Abc1@', 'Xyz2$', 'Pwd3!']
def test_valid_password_candidates_with_none_valid():
assert valid_password_candidates(['abc', 'XYZ123', 'password!',... | [
{
"docstring": "Checks if the given string s is a valid password candidate.",
"function_declaration": "def is_valid_password_candidate(s):",
"function_name": "is_valid_password_candidate",
"parameter_list": "(s)"
},
{
"docstring": "Returns the list of valid password candidates from the given... | [
0,
0,
0,
0,
0,
0,
0,
1,
0,
1
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def has_digit(s):\n return any(char.isdigit() for char in s)\n\ndef has_upper_and_lower(s):\n has_upper = any(char.isupper() for char in s)\n has_lower = any(char.is... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000002622",
"row_id": 2622,
"seed_ids": "['1749', '228', '2129']"
} | 0.612923 | Leetcode_2622 | I | [
"is_valid_password_candidate",
"valid_password_candidates"
] | 2 | [
[
"valid_password_candidates",
"is_valid_password_candidate"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 0b95c722b0535e80a92fe79720782d9ef81ed117 | bec01c432470a205d391a6d3138e46b2cad2f4fc | |||
v1.0 | instruct | Leetcode | Leetcode_2741_I | A game is played on a 2D grid with `m` rows and `n` columns. Each cell in this grid can either be land, represented by the integer `1`, or water, represented by the integer `0`. You are given several queries, each represented by a tuple `(r, c)`, where `(r, c)` denotes a cell in the grid. For each query, you need to de... | def bfs(grid, r, c, visited):
queue = [(r, c)]
visited[r][c] = True
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
size = 0
while queue:
x, y = queue.pop(0)
size += 1
for dx, dy in directions:
nx, ny = x + dx, y + dy
if 0 <=... | from solution import max_land_area
def test_single_query_land():
grid = [
[1, 1, 0],
[1, 0, 0],
[0, 0, 1]
]
queries = [(0, 0)]
assert max_land_area(grid, queries) == [3]
def test_single_query_water():
grid = [
[1, 1, 0],
[1, 0, 0],
[0, 0, 1]
]
... | [
{
"docstring": null,
"function_declaration": "def bfs(grid, r, c, visited):",
"function_name": "bfs",
"parameter_list": "(grid, r, c, visited)"
},
{
"docstring": null,
"function_declaration": "def max_land_area(grid, queries):",
"function_name": "max_land_area",
"parameter_list":... | [
0,
0,
0,
1,
0,
0,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def find_largest_land_area(grid, queries):\n def dfs(r, c):\n if (0 <= r < m) and (0 <= c < n) and grid[r][c] == 1 and not visited[r][c]:\n visited[r][c]... | trial_gpt4o_3 | {
"original_instruction": "",
"prompt_id": "0000002741",
"row_id": 2741,
"seed_ids": "['1953', '702', '110']"
} | 0.614424 | Leetcode_2741 | I | [
"bfs",
"max_land_area"
] | 2 | [
[
"max_land_area",
"bfs"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 77e9546afc85c2485e914b4de023b00c6b02e45a | 9d48000ffbbb010df798cb6f9c526350cf2f01b6 | |||
v1.0 | instruct | Leetcode | Leetcode_3087_I | Given a list of words `words` and a word `target`, return the length of the largest word in the list that can be constructed using the letters of the `target` word exactly once. Each letter in the `target` word can only be used once per word construction. If no words can be constructed, return `-1`. You may assume the ... | def can_construct(word, target):
"""
Helper function to check if a word can be constructed from the letters of the target word.
"""
from collections import Counter
word_count = Counter(word)
target_count = Counter(target)
for char in word_count:
if word_count[char] > target... | from solution import largest_constructible_length
def test_largest_constructible_length_case_1():
words = ["apple", "plea", "ple", "dog"]
target = "apple"
assert largest_constructible_length(words, target) == 5
def test_largest_constructible_length_no_match():
words = ["cat", "dog", "elephant"]
ta... | [
{
"docstring": "Helper function to check if a word can be constructed from the letters of the target word.",
"function_declaration": "def can_construct(word, target):",
"function_name": "can_construct",
"parameter_list": "(word, target)"
},
{
"docstring": "Returns the length of the largest w... | [
0,
0,
0,
0,
0,
0,
1,
0,
1,
1
] | 3 | hard | 0.3 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def can_construct(word, target):\n \"\"\"\n Determines if a word can be constructed from the target letters.\n \"\"\"\n from collections import Counter\n \n ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000003087",
"row_id": 3087,
"seed_ids": "['2217', '657', '1490']"
} | 0.683621 | Leetcode_3087 | I | [
"can_construct",
"largest_constructible_length"
] | 2 | [
[
"largest_constructible_length",
"can_construct"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | e8f2ed5fe9ba5aeaec141a92388d331de20a9b3a | bd6312956c5f3edcb83880795761a28a52600edd | |||
v1.0 | instruct | Leetcode | Leetcode_3161_I | Given an integer array `nums`, return the number of **reverse pairs** in the array. A reverse pair is a pair `(i, j)` where `0 <= i < j < nums.length` and `nums[i] > 2 * nums[j]`.
For example:
- Input: `nums = [1,3,2,3,1]`
- Output: `2`
Explanation: The reverse pairs are `(1, 4)` and `(3, 4)`. | def merge_count_rev_pairs(nums, left, mid, right):
# Count the reverse pairs
count = 0
j = mid + 1
for i in range(left, mid + 1):
while j <= right and nums[i] > 2 * nums[j]:
j += 1
count += (j - (mid + 1))
# Merge the two sorted halves
temp = []
i, j = left, mid ... | from solution import reverse_pairs
def test_reverse_pairs_example():
assert reverse_pairs([1, 3, 2, 3, 1]) == 2
def test_reverse_pairs_no_pairs():
assert reverse_pairs([1, 2, 3, 4, 5]) == 0
def test_reverse_pairs_all_pairs():
assert reverse_pairs([5, 4, 3, 2, 1]) == 4
def test_reverse_pairs_mixed_pairs(... | [
{
"docstring": null,
"function_declaration": "def merge_count_rev_pairs(nums, left, mid, right):",
"function_name": "merge_count_rev_pairs",
"parameter_list": "(nums, left, mid, right)"
},
{
"docstring": null,
"function_declaration": "def count_reverse_pairs(nums, left, right):",
"fu... | [
1,
0,
0,
0,
0,
0,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def merge_count_rev_pairs(nums, left, mid, right):\n # Count the reverse pairs\n count = 0\n j = mid + 1\n for i in range(left, mid + 1):\n while j <= righ... | trial_gpt4o_0 | {
"original_instruction": "",
"prompt_id": "0000003161",
"row_id": 3161,
"seed_ids": "['1482', '1021', '1433']"
} | 0.656241 | Leetcode_3161 | I | [
"merge_count_rev_pairs",
"count_reverse_pairs",
"reverse_pairs"
] | 3 | [
[
"count_reverse_pairs",
"merge_count_rev_pairs"
],
[
"reverse_pairs",
"count_reverse_pairs"
]
] | 2 | [] | false | [] | false | pytest | true | [] | true | 158301c21708552721e8be1f964c48d3cf6efe39 | f57f361f96be40833d9c582b901adb6d1c085bf1 | |||
v1.0 | instruct | Leetcode | Leetcode_3188_I | Given a string containing digits `0-9`, return the number of possible valid IP address combinations that can be formed by inserting exactly three dots into the string. Note that an IP address consists of four sections of numbers separated by dots, and each section must fall between `0` and `255` inclusively. Leading ze... | def is_valid_section(section):
"""
Checks if a given section of an IP address is valid.
A valid section must be an integer between 0 and 255 and
cannot have leading zeros (except for the number 0 itself).
"""
if len(section) > 1 and section[0] == '0':
return False
return 0 <= int(sec... | def test_valid_ip_addresses():
assert valid_ip_addresses("25525511135") == 2 # Possible IPs: ["255.255.11.135", "255.255.111.35"]
assert valid_ip_addresses("0000") == 1 # Possible IP: ["0.0.0.0"]
assert valid_ip_addresses("1111") == 1 # Possible IP: ["1.1.1.1"]
assert valid_ip_addresses... | [
{
"docstring": "Checks if a given section of an IP address is valid.\nA valid section must be an integer between 0 and 255 and\ncannot have leading zeros (except for the number 0 itself).",
"function_declaration": "def is_valid_section(section):",
"function_name": "is_valid_section",
"parameter_list... | [
0,
0,
0,
1,
0,
0,
0,
1,
1,
1
] | 4 | medium | 0.4 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def validate_segment(segment):\n \"\"\"\n Validates if the given segment is a valid section of an IP address.\n \"\"\"\n if len(segment) > 1 and segment[0] == '0'... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000003188",
"row_id": 3188,
"seed_ids": "['211', '841', '462']"
} | 0.497008 | Leetcode_3188 | I | [
"is_valid_section",
"valid_ip_addresses"
] | 2 | [
[
"valid_ip_addresses",
"is_valid_section"
]
] | 1 | [] | false | [] | false | pytest | false | [] | true | fd9c5c86f077722e88615776e70f827df461b9c1 | c31b791e553b185831785d5b14a4a1832b7ab6c4 | |||
v1.0 | instruct | Leetcode | Leetcode_3427_I | You are given a binary tree with `n` nodes labeled from `1` to `n`. Each node has a value assigned to it represented by a **0-indexed** integer array `values`, where `values[i]` is the value of the `i-th` node (0 <= values[i] <= 1). Your task is to prune the tree such that all the nodes with value `0` that do not have ... | class TreeNode:
def __init__(self, value=0):
self.value = value
self.left = None
self.right = None
def build_tree(parent, values):
n = len(parent)
nodes = [TreeNode(values[i]) for i in range(n)]
root = None
for i in range(n):
if parent[i] == -1:
root... | from solution import prune_binary_tree
def test_single_node_1():
assert prune_binary_tree([-1], [1]) == [1]
def test_single_node_0():
assert prune_binary_tree([-1], [0]) == []
def test_tree_complex_case():
parent = [-1, 0, 0, 1, 1, 2, 2, 3]
values = [1, 0, 1, 0, 0, 0, 1, 0]
assert prune_binary_tr... | [] | [
0,
1,
0,
0,
0,
0,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "class TreeNode:\n def __init__(self, value=0):\n self.value = value\n self.left = None\n self.right = None\n \n def __repr__(self):\n ... | trial_gpt4o_1 | {
"original_instruction": "",
"prompt_id": "0000003427",
"row_id": 3427,
"seed_ids": "['849', '2141', '1605']"
} | 0.653001 | Leetcode_3427 | I | [
"build_tree",
"prune_tree",
"level_order_traversal",
"prune_binary_tree"
] | 4 | [
[
"prune_binary_tree",
"build_tree"
],
[
"prune_binary_tree",
"level_order_traversal"
],
[
"prune_binary_tree",
"prune_tree"
]
] | 3 | [] | false | [
"TreeNode"
] | true | pytest | true | [] | true | b06b6c6e0aad06278c15b615cb6c64249236ecd9 | e22c010bc6763e863e16e759b4a6069ae08648d7 | |||
v1.0 | instruct | Leetcode | Leetcode_3481_I | You are given an **m x n** 2D grid of integers representing a matrix, and an integer `target`. Your task is to determine if there is a rectangular sub-matrix within the given matrix such that the sum of all the integers in the sub-matrix is equal to `target`. Return `true` if such a sub-matrix exists, and `false` other... | def submatrix_sum_target(matrix, target):
"""
Determines if there is a sub-matrix with a sum equal to the target.
Args:
matrix: List of lists of integers, representing the 2D grid.
target: Integer, the target sum for the sub-matrix.
Returns:
A boolean indicating whether a sub-matri... | from solution import submatrix_sum_target
def test_submatrix_sum_target_1():
matrix = [
[1, 2, -1],
[-3, 4, 2],
[2, -1, 3]
]
target = 6
assert submatrix_sum_target(matrix, target) == True
def test_submatrix_sum_target_2():
matrix = [
[1, 2],
[3, 4]
]
... | [
{
"docstring": "Determines if there is a sub-matrix with a sum equal to the target.\n\nArgs:\nmatrix: List of lists of integers, representing the 2D grid.\ntarget: Integer, the target sum for the sub-matrix.\n\nReturns:\nA boolean indicating whether a sub-matrix exists with sum equal to the target.",
"funct... | [
1,
1,
1,
0,
1,
0,
1,
0,
0,
1
] | 6 | medium | 0.6 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def submatrix_sum(matrix, target):\n \"\"\"\n Determines if there is a sub-matrix within the given matrix such that the sum\n of all the integers in the sub-matrix i... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000003481",
"row_id": 3481,
"seed_ids": "['1989', '51', '2023']"
} | 0.633303 | Leetcode_3481 | I | [
"submatrix_sum_target",
"find_subarray_with_sum"
] | 2 | [
[
"submatrix_sum_target",
"find_subarray_with_sum"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 7547fdfe91d20a853a2445d543d284c2b9b00138 | c95e220ffd9ba017b1cd0d58537585e79e5b549d | |||
v1.0 | instruct | Leetcode | Leetcode_3622_I | Given a list of non-negative integers `nums`, find and return the length of the longest strictly increasing subsequence. An increasing subsequence is a sequence where each subsequent number is greater than the preceding one, and is not necessarily contiguous. Your solution should have a time complexity better than \(O(... | def length_of_lis(nums):
"""
Finds the length of the longest strictly increasing subsequence in a list of non-negative integers.
Args:
nums: List of non-negative integers.
Returns:
Length of the longest strictly increasing subsequence.
"""
if not nums:
return 0
# array... | from solution import length_of_lis
def test_length_of_lis_empty():
assert length_of_lis([]) == 0
def test_length_of_lis_single_element():
assert length_of_lis([10]) == 1
def test_length_of_lis_increasing_sequence():
assert length_of_lis([1, 2, 3, 4, 5]) == 5
def test_length_of_lis_decreasing_sequence():... | [
{
"docstring": "Finds the length of the longest strictly increasing subsequence in a list of non-negative integers.\n\nArgs:\nnums: List of non-negative integers.\n\nReturns:\nLength of the longest strictly increasing subsequence.",
"function_declaration": "def length_of_lis(nums):",
"function_name": "l... | [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
] | 10 | easy | 1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "import bisect\n\ndef length_of_LIS(nums):\n \"\"\"\n Finds the length of the longest strictly increasing subsequence in the list of non-negative integers nums.\n Use... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000003622",
"row_id": 3622,
"seed_ids": "['616', '296', '2411']"
} | 0.790416 | Leetcode_3622 | I | [
"length_of_lis",
"binary_search"
] | 2 | [
[
"length_of_lis",
"binary_search"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | eea2c915d7f3f297d1b2caea21b51010d91166d4 | 34acf446a557dab60e3131b931f352741af73ecd | |||
v1.0 | instruct | Leetcode | Leetcode_3637_I | Given an integer matrix `grid`, where `grid[i][j]` represents the elevation of a cell `(i, j)`, and an integer `target`, write a function to find the length of the shortest path from the top-left corner to the bottom-right corner. The path must ensure that any elevation difference between two adjacent cells is less tha... | from collections import deque
import sys
def valid_move(grid, x, y, next_x, next_y, target):
"""
Checks if the move from (x, y) to (next_x, next_y) is valid given the target elevation difference.
"""
if 0 <= next_x < len(grid) and 0 <= next_y < len(grid[0]) and abs(grid[next_x][next_y] - grid[x][y]) <=... | from solution import shortest_path
def test_shortest_path_exists():
grid = [
[1, 3, 5],
[2, 8, 3],
[6, 2, 1]
]
assert shortest_path(grid, 3) == 4
def test_no_path_due_to_elevation_difference():
grid = [
[1, 10, 1],
[10, 10, 10],
[1, 10, 1]
]
asse... | [
{
"docstring": "Checks if the move from (x, y) to (next_x, next_y) is valid given the target elevation difference.",
"function_declaration": "def valid_move(grid, x, y, next_x, next_y, target):",
"function_name": "valid_move",
"parameter_list": "(grid, x, y, next_x, next_y, target)"
},
{
"do... | [
0,
0,
1,
0,
0,
0,
1,
0,
1,
0
] | 3 | hard | 0.3 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "from collections import deque\n\ndef shortest_path(grid, target):\n \"\"\"\n Finds the length of the shortest path from the top-left corner to the bottom-right corner\n... | trial_gpt4o_2 | {
"original_instruction": "",
"prompt_id": "0000003637",
"row_id": 3637,
"seed_ids": "['2040', '1218', '2262']"
} | 0.685924 | Leetcode_3637 | I | [
"valid_move",
"shortest_path"
] | 2 | [
[
"shortest_path",
"valid_move"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 6358b3bac1362291b298aa5f81bbad20f7a1555a | ccd5769875d8c5c43cdcb0680f155cd83b90213c | |||
v1.0 | instruct | Leetcode | Leetcode_3838_I | Given an array of integers, sort the array in ascending order using the heap sort algorithm. You should not use any built-in sort functions or libraries. Output the sorted array. | def heapify(arr, n, i):
"""
Converts a subtree rooted with node i into a max heap.
n is the size of the heap
i is the index in arr[]
"""
largest = i # Initialize largest as root
left = 2 * i + 1 # left = 2*i + 1
right = 2 * i + 2 # right = 2*i + 2
# See if left child of root exist... | from solution import heap_sort
def test_heap_sort_empty():
assert heap_sort([]) == []
def test_heap_sort_single_element():
assert heap_sort([1]) == [1]
def test_heap_sort_sorted_array():
assert heap_sort([1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5]
def test_heap_sort_reverse_sorted_array():
assert heap_sort(... | [
{
"docstring": "Converts a subtree rooted with node i into a max heap.\nn is the size of the heap\ni is the index in arr[]",
"function_declaration": "def heapify(arr, n, i):",
"function_name": "heapify",
"parameter_list": "(arr, n, i)"
},
{
"docstring": "Performs heap sort on the input array... | [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
] | 10 | easy | 1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def heapify(arr, n, i):\n \"\"\"\n To heapify a subtree rooted with node i which is an index in arr[]\n n is size of heap\n \"\"\"\n largest = i\n left = 2 ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000003838",
"row_id": 3838,
"seed_ids": "['101', '2268', '2']"
} | 0.731339 | Leetcode_3838 | I | [
"heapify",
"heap_sort"
] | 2 | [
[
"heap_sort",
"heapify"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 7b5759a10f5d2f7f0f1cb8fe3507b4c47d28b0d8 | ac1ad7e3f8da1bbe06c08ec1af320cda7c014e81 | |||
v1.0 | instruct | Leetcode | Leetcode_3856_I | A company is designing a network that connects several servers. The network is represented by a 2D integer array `connections` of length `m` where `connections[i] = [ui, vi]` denotes a bidirectional connection between servers `ui` and `vi`. To ensure that the network can handle increased traffic, they want to determine... | def find(parent, i):
if parent[i] == i:
return i
return find(parent, parent[i])
def union(parent, rank, x, y):
rootX = find(parent, x)
rootY = find(parent, y)
if rootX != rootY:
if rank[rootX] > rank[rootY]:
parent[rootY] = rootX
elif rank[rootX] < rank[root... | from solution import make_connected
def test_make_connected_example_1():
n = 4
connections = [[0, 1], [0, 2], [1, 2]]
assert make_connected(n, connections) == 1
def test_make_connected_example_2():
n = 6
connections = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3]]
assert make_connected(n, connection... | [
{
"docstring": null,
"function_declaration": "def find(parent, i):",
"function_name": "find",
"parameter_list": "(parent, i)"
},
{
"docstring": null,
"function_declaration": "def union(parent, rank, x, y):",
"function_name": "union",
"parameter_list": "(parent, rank, x, y)"
},
... | [
0,
0,
0,
0,
0,
0,
0,
1,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def minimal_connections_needed(n, connections):\n \"\"\"\n Returns the minimal number of additional connections needed to fully connect all servers,\n or -1 if it's ... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000003856",
"row_id": 3856,
"seed_ids": "['2581', '450', '2520']"
} | 0.632739 | Leetcode_3856 | I | [
"find",
"union",
"make_connected"
] | 3 | [
[
"make_connected",
"find"
],
[
"make_connected",
"union"
],
[
"union",
"find"
]
] | 3 | [] | false | [] | false | pytest | true | [] | true | 01360879ac0aedcca8f2d6352754fcc82fd0b184 | 1579e7710a8deda2dde2cfab5bcddf1e6635a5c0 | |||
v1.0 | instruct | Leetcode | Leetcode_3998_I | You are given an unsorted integer array `arr` and a number `k`. Find the **k**th smallest element in the array. Note that it is the **k**th smallest element in the sorted order, not the **k**th distinct element. You must implement a solution with a linear time complexity on average. Return _the **k**th smallest element... | import random
def partition(arr, left, right, pivot_index):
pivot_value = arr[pivot_index]
# Move pivot to end
arr[pivot_index], arr[right] = arr[right], arr[pivot_index]
store_index = left
# Move all smaller elements to the left
for i in range(left, right):
if arr[i] < pivot_value:
... | from solution import find_kth_smallest
def test_single_element_array():
assert find_kth_smallest([1], 1) == 1
def test_small_array():
arr = [3, 1, 2, 5, 4]
assert find_kth_smallest(arr, 1) == 1
assert find_kth_smallest(arr, 3) == 3
assert find_kth_smallest(arr, 5) == 5
def test_multiple_same_elem... | [
{
"docstring": null,
"function_declaration": "def partition(arr, left, right, pivot_index):",
"function_name": "partition",
"parameter_list": "(arr, left, right, pivot_index)"
},
{
"docstring": "Returns the k-th smallest element of list within left..right",
"function_declaration": "def q... | [
1,
1,
1,
1,
0,
0,
1,
1,
0,
1
] | 7 | easy | 0.7 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "import random\n\ndef kth_smallest(arr, k):\n \"\"\"\n Returns the k-th smallest element in the unsorted array.\n Uses Quickselect algorithm to achieve average linear... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000003998",
"row_id": 3998,
"seed_ids": "['438', '1317', '2599']"
} | 0.614502 | Leetcode_3998 | I | [
"partition",
"quickselect",
"find_kth_smallest"
] | 3 | [
[
"find_kth_smallest",
"quickselect"
],
[
"quickselect",
"partition"
]
] | 2 | [] | false | [] | false | pytest | true | [] | true | 057fe1a0a2c627c6cab7fd7f61d41d8e3e815b0f | 8fe9933e416f1079d4c4d23492be10a931d470d1 | |||
v1.0 | instruct | Leetcode | Leetcode_4032_I | Given an integer array `arr` and an integer `k`, shift the entire array to the right by `k` steps, where `k` is non-negative. Implement a function that achieves this in-place with O(1) extra space complexity. For example, given `arr = [1,2,3,4,5,6,7]` and `k = 3`, the array should be modified to `[5,6,7,1,2,3,4]`. | def reverse(arr, start, end):
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
def rotate(arr, k):
"""
Rotates the array to the right by k steps in-place.
"""
n = len(arr)
k = k % n # To handle cases where k > n
reverse(arr, 0, n-1... | from solution import rotate
def arrays_equal(arr1, arr2):
return arr1 == arr2
def test_rotate_normal_case():
arr = [1, 2, 3, 4, 5, 6, 7]
rotate(arr, 3)
assert arrays_equal(arr, [5, 6, 7, 1, 2, 3, 4])
def test_rotate_zero():
arr = [1, 2, 3, 4, 5, 6, 7]
rotate(arr, 0)
assert arrays_equal(ar... | [
{
"docstring": null,
"function_declaration": "def reverse(arr, start, end):",
"function_name": "reverse",
"parameter_list": "(arr, start, end)"
},
{
"docstring": "Rotates the array to the right by k steps in-place.",
"function_declaration": "def rotate(arr, k):",
"function_name": "ro... | [
1,
0,
1,
0,
1,
0,
1,
1,
0,
1
] | 6 | medium | 0.6 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def rotate_array(arr, k):\n \"\"\"\n Rotates the array `arr` to the right by `k` steps.\n \"\"\"\n n = len(arr)\n k = k % n # in case k is greater than the le... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000004032",
"row_id": 4032,
"seed_ids": "['2057', '1172', '2495']"
} | 0.659061 | Leetcode_4032 | I | [
"reverse",
"rotate"
] | 2 | [
[
"rotate",
"reverse"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | d9939bb618b667608ceda76f585886599ee27390 | 715c6c38655e4cf164604f80aaac321601d0890f | |||
v1.0 | instruct | Leetcode | Leetcode_4122_I | You are given an array `arr` consisting of n integers. Your task is to implement a function `inversionCount(arr)` that returns the number of inversions in the array. An inversion is defined as a pair of indices `(i, j)` such that `i < j` and `arr[i] > arr[j]`. For example, for the array `[2, 4, 1, 3, 5]`, the function ... | def merge_and_count(arr, temp_arr, left, mid, right):
"""
Helper function to count inversions across subarrays and merge them.
"""
i = left # Starting index for left subarray
j = mid + 1 # Starting index for right subarray
k = left # Starting index to be sorted
inv_count = 0
# Co... | from solution import inversionCount
def test_inversionCount_example():
assert inversionCount([2, 4, 1, 3, 5]) == 3
def test_inversionCount_no_inversions():
assert inversionCount([1, 2, 3, 4, 5]) == 0
def test_inversionCount_max_inversions():
assert inversionCount([5, 4, 3, 2, 1]) == 10
def test_inversio... | [
{
"docstring": "Helper function to count inversions across subarrays and merge them.",
"function_declaration": "def merge_and_count(arr, temp_arr, left, mid, right):",
"function_name": "merge_and_count",
"parameter_list": "(arr, temp_arr, left, mid, right)"
},
{
"docstring": "Helper function... | [
0,
1,
0,
1,
1,
1,
1,
1,
1,
1
] | 8 | easy | 0.8 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def merge_and_count(arr, temp, left, mid, right):\n i = left # Starting index for left subarray\n j = mid + 1 # Starting index for right subarray\n k = left # ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000004122",
"row_id": 4122,
"seed_ids": "['2250', '705', '2422']"
} | 0.799822 | Leetcode_4122 | I | [
"merge_and_count",
"merge_sort_and_count",
"inversionCount"
] | 3 | [
[
"inversionCount",
"merge_sort_and_count"
],
[
"merge_sort_and_count",
"merge_and_count"
]
] | 2 | [] | false | [] | false | pytest | true | [] | true | 7bf2d7793b8af2112763d31cac4a92678f861fed | 48a579518abbff45c61417263b88b513957b5e94 | |||
v1.0 | instruct | Leetcode | Leetcode_4211_I | You are given a matrix `mat` of integers with dimensions `m x n`. Each cell in the matrix contains either a 0 or a 1. A cell is called “reachable” if it has a path on row or column comprised entirely of 1s that connects it to any edge of the matrix. Return _the number of “reachable” cells in the matrix._ | def is_reachable(mat, i, j, m, n):
# Check if cell mat[i][j] is reachable by moving horizontally or vertically to the matrix edge
for c in range(n):
if mat[i][c] == 1 and any(mat[i][k] == 1 for k in range(c+1)):
return True
for r in range(m):
if mat[r][j] == 1 and any(mat[k][j] =... | from solution import count_reachable_cells
def test_all_ones():
mat = [
[1, 1],
[1, 1]
]
assert count_reachable_cells(mat) == 4
def test_no_ones():
mat = [
[0, 0],
[0, 0]
]
assert count_reachable_cells(mat) == 0
def test_mixed_matrix():
mat = [
[1, ... | [
{
"docstring": null,
"function_declaration": "def is_reachable(mat, i, j, m, n):",
"function_name": "is_reachable",
"parameter_list": "(mat, i, j, m, n)"
},
{
"docstring": null,
"function_declaration": "def count_reachable_cells(mat):",
"function_name": "count_reachable_cells",
"... | [
0,
0,
0,
0,
0,
0,
0,
1,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def count_reachable_cells(mat):\n \"\"\"\n Returns the number of \"reachable\" cells in the matrix.\n \n A cell is \"reachable\" if it has a path on row or column... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000004211",
"row_id": 4211,
"seed_ids": "['1688', '1836', '249']"
} | 0.658283 | Leetcode_4211 | I | [
"is_reachable",
"count_reachable_cells"
] | 2 | [
[
"count_reachable_cells",
"is_reachable"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | b365e71f032874d78207cedb51b41e3df708b3eb | 6f8461c59c90f6e7fe405344572f2609a9db4d89 | |||
v1.0 | instruct | Leetcode | Leetcode_4222_I | You are given a string `s` consisting only of letters 'a' and 'b'. In one move, you can swap any two characters in the string. Your goal is to make the string a palindrome by swapping the minimum number of pairs of characters. Return _an integer_ representing the minimum number of swaps required to make the string a pa... | def min_swaps_to_make_palindrome(s):
"""
Returns the minimum number of swaps required to make the string a palindrome.
If it is impossible to form a palindrome, returns -1.
"""
if not can_form_palindrome(s):
return -1
s = list(s)
n = len(s)
swaps = 0
for i in range(... | from solution import min_swaps_to_make_palindrome
def test_example_case_1():
assert min_swaps_to_make_palindrome("aabb") == 2
def test_example_case_2():
assert min_swaps_to_make_palindrome("ab") == -1
def test_example_case_3():
assert min_swaps_to_make_palindrome("aa") == 0
def test_palindrome_already()... | [
{
"docstring": "Returns the minimum number of swaps required to make the string a palindrome.\nIf it is impossible to form a palindrome, returns -1.",
"function_declaration": "def min_swaps_to_make_palindrome(s):",
"function_name": "min_swaps_to_make_palindrome",
"parameter_list": "(s)"
},
{
... | [
0,
0,
0,
0,
0,
1,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def min_swaps_to_palindrome(s):\n def can_form_palindrome(s):\n from collections import Counter\n freq = Counter(s)\n odd_count = sum(1 for count in f... | trial_gpt4o_5 | {
"original_instruction": "",
"prompt_id": "0000004222",
"row_id": 4222,
"seed_ids": "['1509', '1765', '1366']"
} | 0.795295 | Leetcode_4222 | I | [
"min_swaps_to_make_palindrome",
"can_form_palindrome"
] | 2 | [
[
"min_swaps_to_make_palindrome",
"can_form_palindrome"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | baf776a579b2179730375fc2f3f61f8caf738212 | fb6197a78484a1fc6d6e55eb33ce481db2e6fc17 | |||
v1.0 | instruct | Leetcode | Leetcode_4360_I | Given a string `s` consisting of lowercase letters, you are allowed to remove at most one substring of characters that are consecutive and form a palindrome. Return the length of the longest non-palindromic substring that can be obtained after making at most one such removal.
A substring is a contiguous sequence of c... | def is_palindrome(s):
return s == s[::-1]
def longest_non_palindromic_substring_length(s):
n = len(s)
if not s or n == 1:
return 0
if not is_palindrome(s):
return n
# Since s is a palindrome, the result is n - 1:
return n - 1 | from solution import longest_non_palindromic_substring_length
def test_with_entire_palindrome():
assert longest_non_palindromic_substring_length("aaaa") == 3
assert longest_non_palindromic_substring_length("racecar") == 6
def test_with_non_palindrome():
assert longest_non_palindromic_substring_length("abc... | [
{
"docstring": null,
"function_declaration": "def is_palindrome(s):",
"function_name": "is_palindrome",
"parameter_list": "(s)"
},
{
"docstring": null,
"function_declaration": "def longest_non_palindromic_substring_length(s):",
"function_name": "longest_non_palindromic_substring_leng... | [
1,
0,
0,
1,
0,
1,
0,
0,
0,
0
] | 3 | hard | 0.3 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def longest_non_palindromic_substring(s):\n \"\"\"\n Given a string `s`, returns the length of the longest non-palindromic substring\n that can be obtained after rem... | trial_gpt4o_5 | {
"original_instruction": "",
"prompt_id": "0000004360",
"row_id": 4360,
"seed_ids": "['2524', '1240', '2521']"
} | 0.818308 | Leetcode_4360 | I | [
"is_palindrome",
"longest_non_palindromic_substring_length"
] | 2 | [
[
"longest_non_palindromic_substring_length",
"is_palindrome"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | a8873ba0b1e35283a50b169d3d81b8f63da63c04 | f658188028fad8d07d8d29043f961628d6329cd0 | |||
v1.0 | instruct | Leetcode | Leetcode_4363_I | You are given a **2D grid** of size `m x n` representing a field. Each cell in the grid contains either a `1` (representing land) or a `0` (representing water). You can perform an **unlimited** number of operations, and in each operation, you can convert a `0` (water) cell to a `1` (land) cell.
Your task is to determ... | def num_islands(grid):
def dfs(x, y):
if x < 0 or x >= m or y < 0 or y >= n or grid[x][y] != '1':
return
grid[x][y] = '#'
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
dfs(x + dx, y + dy)
if not grid:
return 0
m, n = len(grid), len(grid[0])
... | from solution import min_operations_to_connect
def test_all_water():
grid = [
['0', '0'],
['0', '0']
]
assert min_operations_to_connect(grid) == 0
def test_single_connected_land():
grid = [
['1', '1'],
['1', '1']
]
assert min_operations_to_connect(grid) == 0
de... | [
{
"docstring": null,
"function_declaration": "def num_islands(grid):",
"function_name": "num_islands",
"parameter_list": "(grid)"
},
{
"docstring": null,
"function_declaration": "def min_operations_to_connect(grid):",
"function_name": "min_operations_to_connect",
"parameter_list"... | [
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def min_operations_to_connect(grid):\n from collections import deque\n \n def bfs(start, visited):\n q = deque([start])\n visited[start[0]][start[1]] =... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000004363",
"row_id": 4363,
"seed_ids": "['2208', '2139', '2497']"
} | 0.59422 | Leetcode_4363 | I | [
"num_islands",
"min_operations_to_connect"
] | 2 | [
[
"min_operations_to_connect",
"num_islands"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 8ed774812205779fab5690517a3406d317f5e234 | 852d5734a6a49489d0a78911850b6fb036cba0af | |||
v1.0 | instruct | Leetcode | Leetcode_4380_I | You are given a binary tree represented by a list of node values in level-order traversal where `None` represents a missing node. Implement a function that returns the level-order traversal of the binary tree as a list of lists. Each sub-list should contain the values of the tree nodes at each level from left to right... | from typing import List, Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def list_to_binary_tree(nodes: List[Optional[int]]) -> Optional[TreeNode]:
if not nodes:
return None
n = len(nodes)
... | from solution import get_level_order
def test_get_level_order():
assert get_level_order([3, 9, 20, None, None, 15, 7]) == [[3], [9, 20], [15, 7]]
assert get_level_order([1, 2, 3, 4, 5, 6, 7]) == [[1], [2, 3], [4, 5, 6, 7]]
assert get_level_order([1]) == [[1]]
assert get_level_order([1, None, 2, None, N... | [] | [
0,
0,
0,
0,
0,
0,
1,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "from collections import deque\n\nclass TreeNode:\n def __init__(self, value=0, left=None, right=None):\n self.value = value\n self.left = left\n self.... | trial_gpt4o_6 | {
"original_instruction": "",
"prompt_id": "0000004380",
"row_id": 4380,
"seed_ids": "['1298', '69', '514']"
} | 0.527321 | Leetcode_4380 | I | [
"list_to_binary_tree",
"level_order_traversal",
"get_level_order"
] | 3 | [
[
"get_level_order",
"level_order_traversal"
],
[
"get_level_order",
"list_to_binary_tree"
]
] | 2 | [] | false | [
"TreeNode"
] | true | pytest | true | [] | true | 8cd01ecf8c8c9d543abb1d97212444d3b9a62c21 | 24a21a826c3a6059bd0fdb79c42ad53f3d0fa426 | |||
v1.0 | instruct | Leetcode | Leetcode_4673_I | Given a string `s` which represents a complex number in the form `"a+bi"`, where `a` and `b` are integers, perform the multiplication of this complex number with another complex number represented by the string `t`, also in the form `"c+di"`. Return the result of the multiplication as a string in the same format `"x+yi... | def parse_complex(complex_str):
"""
Parses a complex number string in the form 'a+bi' and returns a tuple (a, b).
"""
a, b = complex_str[:-1].split('+')
return int(a), int(b)
def multiply_complex_numbers(s, t):
"""
Multiplies two complex numbers given as strings 's' and 't' in the form 'a+b... | from solution import multiply_complex_numbers
def test_multiply_complex_numbers():
assert multiply_complex_numbers("1+1i", "1+1i") == "0+2i"
assert multiply_complex_numbers("1+-1i", "1+-1i") == "0+-2i"
assert multiply_complex_numbers("1+0i", "0+1i") == "0+1i"
assert multiply_complex_numbers("2+3i", "4+... | [
{
"docstring": "Parses a complex number string in the form 'a+bi' and returns a tuple (a, b).",
"function_declaration": "def parse_complex(complex_str):",
"function_name": "parse_complex",
"parameter_list": "(complex_str)"
},
{
"docstring": "Multiplies two complex numbers given as strings 's... | [
0,
0,
1,
0,
0,
1,
1,
0,
0,
0
] | 3 | hard | 0.3 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def parse_complex_number(s):\n real, imaginary = s.split('+')\n return int(real), int(imaginary[:-1])\n\ndef multiply_complex_numbers(s, t):\n a, b = parse_complex_n... | trial_gpt4o_6 | {
"original_instruction": "",
"prompt_id": "0000004673",
"row_id": 4673,
"seed_ids": "['789', '1043', '456']"
} | 0.456605 | Leetcode_4673 | I | [
"parse_complex",
"multiply_complex_numbers"
] | 2 | [
[
"multiply_complex_numbers",
"parse_complex"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 99525dce1b348923b5e03c0af051faa4cbdc1dc4 | f9ff33d4834a684ddf1d474f63d129b2c1296337 | |||
v1.0 | instruct | Leetcode | Leetcode_4791_I | You are given a string `s` and a list of words `words`. Your task is to find which words from the list `words` can be formed by characters from the string `s`. Each character in `s` can be used only once for each word. Return the list of all words that can be formed, sorted in lexicographical order. If no word can be f... | def can_form_word(word, s):
s_char_count = {}
for char in s:
if char in s_char_count:
s_char_count[char] += 1
else:
s_char_count[char] = 1
for char in word:
if char in s_char_count and s_char_count[char] > 0:
s_char_count[char] -= 1
el... | from solution import words_from_string
def test_single_character_words():
assert words_from_string("a", ["a", "b"]) == ["a"]
def test_multiple_character_words():
assert words_from_string("abc", ["a", "ab", "abc", "abcd"]) == ["a", "ab", "abc"]
def test_no_matches():
assert words_from_string("xyz", ["a", ... | [
{
"docstring": null,
"function_declaration": "def can_form_word(word, s):",
"function_name": "can_form_word",
"parameter_list": "(word, s)"
},
{
"docstring": "Returns the list of words from the input list that can be formed by characters from string s.\nThe result list is sorted in lexicogra... | [
0,
0,
0,
0,
0,
0,
0,
0,
1,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def can_form_word(word, available_chars):\n \"\"\"\n Helper function to check if a word can be formed from available characters.\n \"\"\"\n for char in word:\n ... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000004791",
"row_id": 4791,
"seed_ids": "['1477', '754', '1418']"
} | 0.667132 | Leetcode_4791 | I | [
"can_form_word",
"words_from_string"
] | 2 | [
[
"words_from_string",
"can_form_word"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | dfaa7dca23bbcd6a219cbf9e0dd16eb778d7bb45 | 1bbb137d2f793bf5c5068b4afd32e7b112fbf5a8 | |||
v1.0 | instruct | Leetcode | Leetcode_5015_I | Given an array of integers `nums` and an integer `k`, your task is to find the `k-th` smallest element in the array. Note that it is the `k-th` smallest element in the sorted order, not the `k-th` distinct element. You are asked to solve it in such a way that the time complexity is better than O(n log n). Return the `k... | def quickSelect(nums, low, high, k):
if low == high:
return nums[low]
pivot_index = partition(nums, low, high)
if k == pivot_index:
return nums[k]
elif k < pivot_index:
return quickSelect(nums, low, pivot_index - 1, k)
else:
return quickSelect(nums, pivot_index + 1,... | from solution import findKthSmallest
def test_findKthSmallest():
assert findKthSmallest([3, 2, 1, 5, 6, 4], 2) == 2
assert findKthSmallest([3, 2, 1, 5, 6, 4], 5) == 5
assert findKthSmallest([7, 10, 4, 3, 20, 15], 3) == 7
assert findKthSmallest([7, 10, 4, 3, 20, 15], 4) == 10
assert findKthSmallest(... | [
{
"docstring": null,
"function_declaration": "def quickSelect(nums, low, high, k):",
"function_name": "quickSelect",
"parameter_list": "(nums, low, high, k)"
},
{
"docstring": null,
"function_declaration": "def partition(nums, low, high):",
"function_name": "partition",
"paramete... | [
1,
1,
1,
1,
1,
1,
1,
1,
0,
1
] | 9 | easy | 0.9 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "import heapq\n\ndef find_kth_smallest(nums, k):\n \"\"\"\n Returns the k-th smallest element in the array nums.\n \"\"\"\n # Use a min-heap to find the k-th small... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000005015",
"row_id": 5015,
"seed_ids": "['1580', '896', '1397']"
} | 0.639845 | Leetcode_5015 | I | [
"quickSelect",
"partition",
"findKthSmallest"
] | 3 | [
[
"findKthSmallest",
"quickSelect"
],
[
"quickSelect",
"partition"
]
] | 2 | [] | false | [] | false | pytest | true | [] | true | bc9b0cceaa39cb03c6a050f29896d3ead0316d0d | d5a6993ecff44f1825123c2832ef33c627289765 | |||
v1.0 | instruct | Leetcode | Leetcode_5044_I | You are given a 2D grid `grid` of size `m x n`. Each cell of the grid contains a value representing the height of that cell. You're also given an integer `height_threshold`. You can move from a cell to its neighboring cell horizontally or vertically if and only if the height of the neighboring cell does not exceed the ... | from collections import deque
def can_traverse(grid, m, n, sr, sc, threshold):
visited = [[False] * n for _ in range(m)]
queue = deque([(sr, sc)])
visited[sr][sc] = True
count = 1
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
while queue:
r, c = queue.popleft()
for dr, dc i... | def test_find_best_starting_point():
grid = [
[1, 2, 3],
[6, 5, 4],
[7, 8, 7],
]
assert find_best_starting_point(grid, 3, 3, 5) == 5
grid = [
[10, 12, 10],
[8, 9, 11],
[7, 9, 9],
]
assert find_best_starting_point(grid, 3, 3, 9) == 9
grid = [
... | [
{
"docstring": null,
"function_declaration": "def can_traverse(grid, m, n, sr, sc, threshold):",
"function_name": "can_traverse",
"parameter_list": "(grid, m, n, sr, sc, threshold)"
},
{
"docstring": null,
"function_declaration": "def find_best_starting_point(grid, m, n, height_threshold... | [
0,
1,
0,
0,
1,
0,
1,
1,
0
] | 4 | medium | 0.444 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def find_starting_height(grid, height_threshold):\n \"\"\"\n Determines the smallest value in the grid that is greater than or equal to height_threshold which,\n if ... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000005044",
"row_id": 5044,
"seed_ids": "['1330', '2255', '808']"
} | 0.712009 | Leetcode_5044 | I | [
"can_traverse",
"find_best_starting_point"
] | 2 | [
[
"find_best_starting_point",
"can_traverse"
]
] | 1 | [] | false | [] | false | pytest | false | [] | true | 8c8be790db9e21f58f3f6a5d749e13eabd2a7e22 | e9c01523de5e6f2bd6bf41a7a45f71b00a312f06 | |||
v1.0 | instruct | Leetcode | Leetcode_5061_I | You are given an integer array `cards` of length `n`, where `cards[i]` represents the value of the `ith` card. Two players, Alice and Bob, are playing a game where they take turns drawing cards from the `cards` array. Alice always goes first, followed by Bob, and they both take one card on each turn, either from the be... | def max_score_recursive(cards, start, end, memo):
if start > end:
return 0
if memo[start][end] != -1:
return memo[start][end]
pick_start = cards[start] + min(max_score_recursive(cards, start + 2, end, memo),
max_score_recursive(cards, start + 1, end - 1... | from solution import max_score
def test_max_score_single_element():
assert max_score([10]) == 10
def test_max_score_two_elements():
assert max_score([1, 2]) == 2
def test_max_score_equal_elements():
assert max_score([5, 5, 5, 5]) == 10
def test_max_score_alternating_elements():
assert max_score([1, ... | [
{
"docstring": null,
"function_declaration": "def max_score_recursive(cards, start, end, memo):",
"function_name": "max_score_recursive",
"parameter_list": "(cards, start, end, memo)"
},
{
"docstring": null,
"function_declaration": "def max_score(cards):",
"function_name": "max_score... | [
0,
0,
0,
0,
0,
0,
0,
1,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def max_score(cards):\n n = len(cards)\n dp = [[0] * n for _ in range(n)]\n \n for i in range(n):\n dp[i][i] = cards[i]\n\n for length in range(2, n + 1... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000005061",
"row_id": 5061,
"seed_ids": "['1599', '1761', '2037']"
} | 0.714468 | Leetcode_5061 | I | [
"max_score_recursive",
"max_score"
] | 2 | [
[
"max_score",
"max_score_recursive"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | d12d2eae41700ee2ad9425c52e0ba67f52212ac8 | b8c10c64afa008bb2f743fbf334c63836bc7bc23 | |||
v1.0 | instruct | Leetcode | Leetcode_5105_I | Given a linked list where each node contains a unique integer value, implement a function to find the length of the longest sublist that forms an increasing sequence. The sublist can contain consecutive or non-consecutive nodes but must preserve the original order found in the linked list. Return the length of this lon... | class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def longest_increasing_sublist(head):
"""
Given the head of a linked list, return the length of the longest increasing sublist that preserves
the original order of nodes.
"""
if not head:
return 0
... | from solution import ListNode, longest_increasing_sublist
def test_longest_increasing_sublist():
# Case 1: Example case
# Linked list: 1 -> 3 -> 2 -> 5 -> 4 -> 6
# Longest increasing sublist: 1 -> 3 -> 5 -> 6
# Output: 4
head = ListNode(1)
head.next = ListNode(3)
head.next.next = ListNode(2... | [] | [
1,
1,
1,
0,
0,
1,
0,
1,
1,
1
] | 7 | easy | 0.7 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "class ListNode:\n def __init__(self, x):\n self.val = x\n self.next = None\n\ndef longest_increasing_sublist_length(head):\n \"\"\"\n Returns the lengt... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000005105",
"row_id": 5105,
"seed_ids": "['200', '2458', '755']"
} | 0.633822 | Leetcode_5105 | I | [
"longest_increasing_sublist",
"nth_value"
] | 2 | [
[
"longest_increasing_sublist",
"nth_value"
]
] | 1 | [] | false | [
"ListNode"
] | true | pytest | true | [] | true | 9c5ad45222c33c8d056b84b1c375394cfc9fc9ba | 2d31993bde3e233725de19f09da470b441253fd8 | |||
v1.0 | instruct | Leetcode | Leetcode_5197_I | A permutation of an array of integers is an arrangement of its members into a sequence or linear order. Given an integer array `nums` of distinct integers and an integer `k`, return _all the permutations of the array_ `nums` _such that the absolute difference between each adjacent element in the permutation is at least... | import itertools
def is_valid_permutation(perm, k):
for i in range(1, len(perm)):
if abs(perm[i] - perm[i - 1]) < k:
return False
return True
def valid_permutations(nums, k):
"""
Returns all permutations of nums where the absolute difference
between each adjacent element in th... | from solution import valid_permutations, is_valid_permutation
def test_is_valid_permutation():
assert is_valid_permutation([1, 4, 7], 3) == True
assert is_valid_permutation([1, 2, 3], 2) == False
assert is_valid_permutation([5, 1, 8], 4) == True
assert is_valid_permutation([6, 1, 3], 5) == False
def t... | [
{
"docstring": null,
"function_declaration": "def is_valid_permutation(perm, k):",
"function_name": "is_valid_permutation",
"parameter_list": "(perm, k)"
},
{
"docstring": "Returns all permutations of nums where the absolute difference \nbetween each adjacent element in the permutation is at... | [
0,
0,
0,
0,
1,
0,
0,
1,
0,
0
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "from itertools import permutations\n\ndef is_valid_permutation(permutation, k):\n \"\"\"\n Check if the permutation is valid based on the given k constraint.\n \"\"\... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000005197",
"row_id": 5197,
"seed_ids": "['2381', '1121', '2150']"
} | 0.727664 | Leetcode_5197 | I | [
"is_valid_permutation",
"valid_permutations"
] | 2 | [
[
"valid_permutations",
"is_valid_permutation"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | f40c1f73342f0727548779fe93b8e71bc7776e3f | 235839444a55e2037ec6d7918050d30d1fcd7b1a | |||
v1.0 | instruct | Leetcode | Leetcode_5249_I | A company wants to create a shipping schedule to minimize the total shipping cost. You are given an integer array `weights` where `weights[i]` represents the weight of the `ith` shipment. You are also given an integer `D` which represents the number of days you have to ship all the packages. Every day, you can ship any... | def can_ship_in_days(weights, D, capacity):
total_days = 1
current_weight = 0
for weight in weights:
if current_weight + weight > capacity:
total_days += 1
current_weight = 0
current_weight += weight
return total_days <= D
def find_min_capacity(weights, D):
l... | from solution import can_ship_in_days, find_min_capacity
def test_can_ship_in_days():
assert can_ship_in_days([1,2,3,4,5,6,7,8,9,10], 5, 15) == True
assert can_ship_in_days([1,2,3,4,5,6,7,8,9,10], 5, 14) == False
assert can_ship_in_days([3,2,2,4,1,4], 3, 6) == True
assert can_ship_in_days([3,2,2,4,1,4]... | [
{
"docstring": null,
"function_declaration": "def can_ship_in_days(weights, D, capacity):",
"function_name": "can_ship_in_days",
"parameter_list": "(weights, D, capacity)"
},
{
"docstring": null,
"function_declaration": "def find_min_capacity(weights, D):",
"function_name": "find_min... | [
1,
0,
1,
1,
0,
1,
0,
1,
0,
0
] | 5 | medium | 0.5 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def can_ship(weights, D, capacity):\n days = 1\n current_load = 0\n for weight in weights:\n if current_load + weight > capacity:\n days += 1\n ... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000005249",
"row_id": 5249,
"seed_ids": "['1227', '1686', '746']"
} | 0.70523 | Leetcode_5249 | I | [
"can_ship_in_days",
"find_min_capacity"
] | 2 | [
[
"find_min_capacity",
"can_ship_in_days"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | ede9090fd4f39447b3ffe5ec4367458b4dfc5fa2 | 5047b5bdb48f9addf0cb7336d03505e44dedd61d | |||
v1.0 | instruct | Leetcode | Leetcode_5508_I | You are given a 2D grid `grid` of size `m x n` consisting of only `0`s and `1`s. An island is a group of contiguous `1`s connected horizontally or vertically. The size of an island is the number of `1`s it contains. Return _the size of the smallest island_ in `grid`_. If there are no islands, return_ `0`. | def in_bounds(grid, x, y):
return 0 <= x < len(grid) and 0 <= y < len(grid[0])
def dfs(grid, x, y):
if not in_bounds(grid, x, y) or grid[x][y] == 0:
return 0
grid[x][y] = 0 # Mark as visited
size = 1
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
size += dfs(grid, x + dx, y + dy... | from solution import smallest_island
def test_empty_grid():
assert smallest_island([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) == 0
def test_single_island():
grid = [
[1, 0, 0],
[0, 0, 1],
[0, 1, 0]
]
assert smallest_island(grid) == 1
def test_multiple_islands():
grid = [
[... | [
{
"docstring": null,
"function_declaration": "def in_bounds(grid, x, y):",
"function_name": "in_bounds",
"parameter_list": "(grid, x, y)"
},
{
"docstring": null,
"function_declaration": "def dfs(grid, x, y):",
"function_name": "dfs",
"parameter_list": "(grid, x, y)"
},
{
... | [
1,
1,
1,
0,
0,
0,
0,
0,
0,
1
] | 4 | medium | 0.4 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def smallest_island(grid):\n \"\"\"\n Returns the size of the smallest island in the given grid of 0s and 1s.\n An island is a group of contiguous 1s connected horiz... | trial_gpt4o_1 | {
"original_instruction": "",
"prompt_id": "0000005508",
"row_id": 5508,
"seed_ids": "['1903', '1938', '2031']"
} | 0.642973 | Leetcode_5508 | I | [
"in_bounds",
"dfs",
"smallest_island"
] | 3 | [
[
"dfs",
"in_bounds"
],
[
"smallest_island",
"dfs"
]
] | 2 | [] | false | [] | false | pytest | true | [] | true | 8c031833a9a8151792a281b3aec18fe0967491e5 | e9185c733af7b2a9a7fb4fadb8cc49a4e434051c | |||
v1.0 | instruct | Leetcode | Leetcode_5674_I | Imagine you are given an array of positive integers representing the positions of cars on a circular track of length `n`. Each car can only move to a position ahead (wrap-around allowed), and the distance each car can move per unit time is fixed at `d` units. The cars race around the track for an indefinite amount of t... | import math
def gcd(a, b):
"""
Returns the greatest common divisor of a and b.
"""
while b:
a, b = b, a % b
return a
def lcm(a, b):
"""
Returns the least common multiple of a and b.
"""
return abs(a * b) // gcd(a, b)
def minimum_time_to_meet(n, d):
"""
Returns the ... | from solution import minimum_time_to_meet
def test_case_1():
assert minimum_time_to_meet(12, 3) == 12
def test_case_2():
assert minimum_time_to_meet(10, 4) == 20
def test_case_3():
assert minimum_time_to_meet(15, 5) == 15
def test_case_4():
assert minimum_time_to_meet(24, 6) == 24
def test_case... | [
{
"docstring": "Returns the greatest common divisor of a and b.",
"function_declaration": "def gcd(a, b):",
"function_name": "gcd",
"parameter_list": "(a, b)"
},
{
"docstring": "Returns the least common multiple of a and b.",
"function_declaration": "def lcm(a, b):",
"function_name":... | [
0,
0,
1,
0,
0,
0,
0,
1,
0,
0
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "import math\nfrom functools import reduce\n\ndef min_time_to_meet(n, d, positions):\n \"\"\"\n Returns the minimum time required for all cars to meet at the same positi... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000005674",
"row_id": 5674,
"seed_ids": "['1240', '1488', '583']"
} | 0.601639 | Leetcode_5674 | I | [
"gcd",
"lcm",
"minimum_time_to_meet"
] | 3 | [
[
"lcm",
"gcd"
],
[
"minimum_time_to_meet",
"lcm"
]
] | 2 | [] | false | [] | false | pytest | true | [] | true | c8a2d51ef4ea90de472c4539d9ffd64bf8d4d147 | a106df8e3558c5f77c2acae10a162446289bd3f3 | |||
v1.0 | instruct | Leetcode | Leetcode_5713_I | You are designing a logging system for a server where each log entry records a timestamp and an event. The logs are given in a list where each log is a string formatted as "HH:MM event_description". The logs are not guaranteed to be in chronological order.
Write a function that takes the list of logs and an event des... | def time_to_minutes(time_str):
"""
Converts a time string in HH:MM format to total minutes.
"""
hours, minutes = map(int, time_str.split(":"))
return hours * 60 + minutes
def event_time_difference(logs, event):
"""
Returns the delay in minutes between the first and last occurrence of the ev... | from solution import event_time_difference
def test_event_time_difference_example():
logs = ["12:30 start", "14:00 process", "12:35 end", "14:15 start", "15:45 end"]
assert event_time_difference(logs, "start") == 105
def test_multiple_event_differences():
logs = ["09:00 start", "10:00 process", "11:15 pro... | [
{
"docstring": "Converts a time string in HH:MM format to total minutes.",
"function_declaration": "def time_to_minutes(time_str):",
"function_name": "time_to_minutes",
"parameter_list": "(time_str)"
},
{
"docstring": "Returns the delay in minutes between the first and last occurrence of the... | [
1,
1,
0,
0,
0,
1,
1,
0,
0,
1
] | 5 | medium | 0.5 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def calculate_event_delay(logs, event):\n \"\"\"\n Returns the delay in minutes between the first and last occurrence of the given event.\n \n Args:\n logs (li... | trial_gpt4o_6 | {
"original_instruction": "",
"prompt_id": "0000005713",
"row_id": 5713,
"seed_ids": "['1904', '1009', '17']"
} | 0.588262 | Leetcode_5713 | I | [
"time_to_minutes",
"event_time_difference"
] | 2 | [
[
"event_time_difference",
"time_to_minutes"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | a4938da6479808260d8c215bd9038187a2bcd5aa | 1c356fcb07fb50efb7812da0c8d29fda67b61220 | |||
v1.0 | instruct | Leetcode | Leetcode_5739_I | A company has a legacy software system with a list of tasks to complete, each of which is represented by an integer array `tasks`. A task can either be approved or denied based on a strict approval criterion. Each task is approved only if it has more than `k` unique prime factors. The company wants to filter out all ta... | from math import isqrt
def prime_factors_count(n):
"""
Returns the count of unique prime factors of n.
"""
count = 0
i = 2
while i <= isqrt(n):
if n % i == 0:
count += 1
while n % i == 0:
n //= i
i += 1
if n > 1:
count += 1
... | from solution import filter_tasks_by_prime_factors
def test_filter_tasks_no_tasks_meet_criterion():
assert filter_tasks_by_prime_factors([6, 10, 14, 15], 2) == []
def test_filter_tasks_all_tasks_meet_criterion():
assert filter_tasks_by_prime_factors([30, 42, 60, 70], 2) == [30, 42, 60, 70]
def test_filter_ta... | [
{
"docstring": "Returns the count of unique prime factors of n.",
"function_declaration": "def prime_factors_count(n):",
"function_name": "prime_factors_count",
"parameter_list": "(n)"
},
{
"docstring": "Returns a list of tasks that have more than k unique prime factors.",
"function_decl... | [
0,
0,
0,
0,
1,
0,
0,
0,
1,
0
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "import math\n\ndef prime_factors(n):\n \"\"\"\n Returns a set of prime factors of the given number n.\n \"\"\"\n factors = set()\n \n # Check for number of ... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000005739",
"row_id": 5739,
"seed_ids": "['412', '1201', '489']"
} | 0.620323 | Leetcode_5739 | I | [
"prime_factors_count",
"filter_tasks_by_prime_factors"
] | 2 | [
[
"filter_tasks_by_prime_factors",
"prime_factors_count"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | dc344b04725590c6af8516ad63ea9c754c746e3c | 26c766e11139110b5631122448e8e0e38c0e9bb3 | |||
v1.0 | instruct | Leetcode | Leetcode_6060_I | You are given a rectangular grid with `m` rows and `n` columns, represented as a matrix where each cell contains an integer representing the height of the terrain at that cell. The task is to find the minimum height needed to cut through to ensure water flow from the top-left corner to the bottom-right corner. Water ca... | from queue import PriorityQueue
def can_flow(grid, x, y, visited, max_height):
"""
Helper function to perform DFS and check if water can flow from (0,0) to (m-1, n-1) at a given max height.
"""
if x == len(grid) - 1 and y == len(grid[0]) - 1:
return True
directions = [(0,1), (1,0), (0,... | from solution import find_min_height
def test_find_min_height_simple():
grid = [
[1, 2, 2],
[3, 2, 3],
[2, 2, 2]
]
assert find_min_height(grid) == 2
def test_find_min_height_flat_grid():
grid = [
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]
]
assert find_min_... | [
{
"docstring": "Helper function to perform DFS and check if water can flow from (0,0) to (m-1, n-1) at a given max height.",
"function_declaration": "def can_flow(grid, x, y, visited, max_height):",
"function_name": "can_flow",
"parameter_list": "(grid, x, y, visited, max_height)"
},
{
"docs... | [
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "from collections import deque\n\ndef can_flow(heights, m, n, max_height):\n directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]\n visited = [[False] * n for _ in range(m)]\... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000006060",
"row_id": 6060,
"seed_ids": "['1377', '938', '2259']"
} | 0.57974 | Leetcode_6060 | I | [
"can_flow",
"find_min_height"
] | 2 | [
[
"find_min_height",
"can_flow"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 839013cd5a308e05ef1ac5c2bb7a58ddfdb1eeb6 | 3f72691b4fddb13f87212d9bb443495dc5581ecc | |||
v1.0 | instruct | Leetcode | Leetcode_6108_I | You are given a list of non-negative integers representing the amount of money of each house. Each house is arranged in a circular street such that the first house is the neighbor of the last one. You can't rob two adjacent houses, meaning after you rob a house, the next adjacent house can't be robbed.
Find the maxim... | from typing import List
def rob_linear(houses: List[int]) -> int:
if not houses:
return 0
if len(houses) == 1:
return houses[0]
if len(houses) == 2:
return max(houses)
dp = [0] * len(houses)
dp[0] = houses[0]
dp[1] = max(houses[0], houses[1])
for i in range(2, l... | from solution import circularRob
def test_circular_rob_empty():
assert circularRob([]) == 0
def test_circular_rob_one_house():
assert circularRob([5]) == 5
def test_circular_rob_two_houses():
assert circularRob([2, 3]) == 3
def test_circular_rob_three_houses():
assert circularRob([2, 3, 2]) == 3
de... | [
{
"docstring": null,
"function_declaration": "def rob_linear(houses):",
"function_name": "rob_linear",
"parameter_list": "(houses)"
},
{
"docstring": null,
"function_declaration": "def circularRob(houses):",
"function_name": "circularRob",
"parameter_list": "(houses)"
}
] | [
0,
0,
0,
0,
1,
0,
1,
0,
1,
0
] | 3 | hard | 0.3 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "from typing import List\n\ndef rob_linear(houses: List[int]) -> int:\n if not houses:\n return 0\n if len(houses) == 1:\n return houses[0]\n \n prev... | trial_gpt4o_6 | {
"original_instruction": "",
"prompt_id": "0000006108",
"row_id": 6108,
"seed_ids": "['1343', '1600', '501']"
} | 0.518935 | Leetcode_6108 | I | [
"rob_linear",
"circularRob"
] | 2 | [
[
"circularRob",
"rob_linear"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 450e4e5eaea1de22722591cf08b04d3bd3da6350 | b021afab74fecfa233a148e5b3c24060c7fc64c1 | |||
v1.0 | instruct | Leetcode | Leetcode_6130_I | Given a binary tree, implement an algorithm to return the level order traversal of its nodes' values but in reverse (from the bottom level to the top and from left to right within each level). The tree is represented as an array in level order where `null` indicates the absence of a node. Return the result as a list of... | from collections import deque
class TreeNode:
def __init__(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right
def build_tree_from_level_order(values):
if not values:
return None
root = TreeNode(values[0])
queue = deque([root])... | from solution import traverse_tree_from_array
def test_empty_tree():
assert traverse_tree_from_array([]) == []
def test_single_node_tree():
assert traverse_tree_from_array([1]) == [[1]]
def test_binary_tree():
values = [3, 9, 20, None, None, 15, 7]
assert traverse_tree_from_array(values) == [[15, 7],... | [] | [
1,
1,
0,
0,
1,
0,
0,
1,
1,
0
] | 5 | medium | 0.5 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "from collections import deque\n\nclass TreeNode:\n def __init__(self, val=0, left=None, right=None):\n self.val = val\n self.left = left\n self.right ... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000006130",
"row_id": 6130,
"seed_ids": "['18', '159', '895']"
} | 0.521737 | Leetcode_6130 | I | [
"build_tree_from_level_order",
"reverse_level_order_traversal",
"traverse_tree_from_array"
] | 3 | [
[
"traverse_tree_from_array",
"build_tree_from_level_order"
],
[
"traverse_tree_from_array",
"reverse_level_order_traversal"
]
] | 2 | [] | false | [
"TreeNode"
] | true | pytest | true | [] | true | 23fbae040fa5e8da9a5692e6a95ab5f2559eae7a | 9a9e6aa8bf807eb2a74b6c1cffeccfb699629b95 | |||
v1.0 | instruct | Leetcode | Leetcode_6136_I | You are given an array of integers `arr` of length `n`. Your task is to form the largest number possible by concatenating the elements of the array. Write a function `largestNumber` which accepts an integer array `arr` and returns a string representing the largest number formed by concatenating all elements in `arr`. E... | from typing import List
from functools import cmp_to_key
def compare(x, y):
# Comparator function to decide the order based on concatenated string comparison
if x + y > y + x:
return -1
elif x + y < y + x:
return 1
else:
return 0
def largestNumber(arr: List[int]) -> str:
# ... | from solution import largestNumber
def test_single_digit_numbers():
assert largestNumber([3, 30, 34, 5, 9]) == '9534330'
def test_all_equal_numbers():
assert largestNumber([1, 1, 1]) == '111'
def test_large_numbers():
assert largestNumber([10, 2]) == '210'
assert largestNumber([3, 30, 34, 5, 91]) == ... | [
{
"docstring": null,
"function_declaration": "def compare(x, y):",
"function_name": "compare",
"parameter_list": "(x, y)"
},
{
"docstring": null,
"function_declaration": "def largestNumber(arr):",
"function_name": "largestNumber",
"parameter_list": "(arr)"
}
] | [
0,
1,
0,
1,
0,
0,
1,
0,
1,
0
] | 4 | medium | 0.4 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "from typing import List\nfrom functools import cmp_to_key\n\ndef custom_compare(x: str, y: str) -> int:\n \"\"\"\n Custom comparator for sorting the numbers.\n \"\"\... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000006136",
"row_id": 6136,
"seed_ids": "['1153', '478', '2445']"
} | 0.709871 | Leetcode_6136 | I | [
"compare",
"largestNumber"
] | 2 | [
[
"largestNumber",
"compare"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 1462fa9888edacc044b290ee2c6ed5f0ca587dbc | 1bc5df48e192a89ba85e043787701456329e07e9 | |||
v1.0 | instruct | Leetcode | Leetcode_6275_I | You are given an array `arr` of `n` integers and an integer `m`. You need to partition the array into **exactly** `m` non-overlapping, contiguous subarrays, such that the sum of the maximum subarray sum among all the partitions is minimized. Return _the minimized sum of the maximum subarray sum of the partition_.
Not... | def can_partition(arr, n, m, max_sum):
count, current_sum = 1, 0
for num in arr:
current_sum += num
if current_sum > max_sum:
count += 1
current_sum = num
if count > m:
return False
return True
def minimize_max_sum_of_partitions(arr, m):
... | from solution import minimize_max_sum_of_partitions
def test_minimize_max_sum_of_partitions():
assert minimize_max_sum_of_partitions([7, 2, 5, 10, 8], 2) == 18
assert minimize_max_sum_of_partitions([1, 2, 3, 4, 5], 2) == 9
assert minimize_max_sum_of_partitions([1, 4, 4], 3) == 4
assert minimize_max_sum... | [
{
"docstring": null,
"function_declaration": "def can_partition(arr, n, m, max_sum):",
"function_name": "can_partition",
"parameter_list": "(arr, n, m, max_sum)"
},
{
"docstring": null,
"function_declaration": "def minimize_max_sum_of_partitions(arr, m):",
"function_name": "minimize_... | [
1,
0,
0,
1,
0,
1,
0,
0,
0
] | 3 | medium | 0.333 | {
"trial_gpt4o_0": null,
"trial_gpt4o_1": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_1_results_combined.jsonl",
"solution_code": "def splitArray(nums, m):\n def valid(mid):\n count, total = 1, 0\n for num in nums:\n total += num\n if total ... | trial_gpt4o_6 | {
"original_instruction": "",
"prompt_id": "0000006275",
"row_id": 6275,
"seed_ids": "['778', '395', '2361']"
} | 0.755055 | Leetcode_6275 | I | [
"can_partition",
"minimize_max_sum_of_partitions"
] | 2 | [
[
"minimize_max_sum_of_partitions",
"can_partition"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | d2697f056864f29ab04b8e786c612984ba4de3f3 | 57546262da1842dbc65ec774239819015d975e05 | |||
v1.0 | instruct | Leetcode | Leetcode_6286_I | A factory is working on an assembly line where each task is assigned a specific amount of time to complete. You are given an integer array `tasks` where `tasks[i]` represents the time required to complete the `ith` task. You are also given an integer `n` that represents the number of workers available. The tasks are as... | def canComplete(tasks, workers, maxTime):
current_time = 0
needed_workers = 1
for task in tasks:
if current_time + task <= maxTime:
current_time += task
else:
needed_workers += 1
current_time = task
if needed_workers > workers:
... | from solution import minimizeMaxWorkingTime
def test_minimizeMaxWorkingTime_single_task():
assert minimizeMaxWorkingTime([5], 1) == 5
def test_minimizeMaxWorkingTime_single_worker():
assert minimizeMaxWorkingTime([1, 2, 3, 4, 5], 1) == 15
def test_minimizeMaxWorkingTime_equal_tasks_workers():
assert mini... | [
{
"docstring": null,
"function_declaration": "def canComplete(tasks, workers, maxTime):",
"function_name": "canComplete",
"parameter_list": "(tasks, workers, maxTime)"
},
{
"docstring": null,
"function_declaration": "def minimizeMaxWorkingTime(tasks, n):",
"function_name": "minimizeM... | [
0,
0,
0,
0,
0,
0,
0,
1,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def can_complete(tasks, n, max_work_time):\n current_sum = 0\n workers_needed = 1\n\n for task in tasks:\n if current_sum + task <= max_work_time:\n ... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000006286",
"row_id": 6286,
"seed_ids": "['2555', '2260', '872']"
} | 0.748522 | Leetcode_6286 | I | [
"canComplete",
"minimizeMaxWorkingTime"
] | 2 | [
[
"minimizeMaxWorkingTime",
"canComplete"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | fb4836ff46575521d3b224cad058c640cd184837 | 646deab540db2284bdb7221bec2132efdde15f23 | |||
v1.0 | instruct | Leetcode | Leetcode_6406_I | You are given a list of strings `words`. A string is considered a **subsequence** of another string if it can be derived from the other string by deleting some or no characters without changing the order of the remaining characters. Return _an integer_ indicating the **length of the longest string** in `words` that is ... | def is_subsequence(s, t):
"""
Helper function to determine if s is a subsequence of t.
"""
it = iter(t)
return all(c in it for c in s)
def longest_common_subsequence_length(words):
"""
Returns the length of the longest string in `words` that is a subsequence
of all other strings in `wor... | from solution import longest_common_subsequence_length
def test_common_subsequence_exists():
assert longest_common_subsequence_length(["abc", "ab", "abcd"]) == 2
assert longest_common_subsequence_length(["a", "a", "a"]) == 1
assert longest_common_subsequence_length(["xyz", "xy", "x"]) == 1
def test_no_com... | [
{
"docstring": "Helper function to determine if s is a subsequence of t.",
"function_declaration": "def is_subsequence(s, t):",
"function_name": "is_subsequence",
"parameter_list": "(s, t)"
},
{
"docstring": "Returns the length of the longest string in `words` that is a subsequence\nof all o... | [
0,
1,
1,
0,
0,
0,
0,
0,
0,
0
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_subsequence(s, t):\n \"\"\"\n Returns True if `s` is a subsequence of `t`, otherwise False.\n \"\"\"\n it = iter(t)\n return all(char in it for char in ... | trial_gpt4o_2 | {
"original_instruction": "",
"prompt_id": "0000006406",
"row_id": 6406,
"seed_ids": "['1128', '1782', '819']"
} | 0.764352 | Leetcode_6406 | I | [
"is_subsequence",
"longest_common_subsequence_length"
] | 2 | [
[
"longest_common_subsequence_length",
"is_subsequence"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | c5fc3f55297324bcd9bfd648c21401e269ad608d | fa417f6e9feee8d605693129edbc859089299d3f | |||
v1.0 | instruct | Leetcode | Leetcode_6427_I | You are given a binary tree with `n` nodes. Each node is labeled uniquely with integers from `1` to `n`. You are also given two integers, `a` and `b`. Your task is to determine the length of the shortest path between the nodes labeled `a` and `b`. You can assume that the tree is rooted at node `1`. Return the length of... | class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def findLCA(root, n1, n2):
if root is None:
return None
if root.val == n1 or root.val == n2:
return root
left_lca = findLCA(root.left, n1, n2)
right_lca = findLCA(root.righ... | from solution import TreeNode, shortestPath
def test_shortest_path_basic():
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.right.left = TreeNode(6)
root.right.right = TreeNode(7)
assert shortestPat... | [] | [
1,
1,
1,
0,
1,
1,
1,
1,
1,
1
] | 9 | easy | 0.9 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "class TreeNode:\n def __init__(self, x):\n self.val = x\n self.left = None\n self.right = None\n\ndef find_LCA(root, n1, n2):\n if root is None:\n ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000006427",
"row_id": 6427,
"seed_ids": "['1397', '1743', '2077']"
} | 0.550643 | Leetcode_6427 | I | [
"findLCA",
"findLevel",
"shortestPath"
] | 3 | [
[
"shortestPath",
"findLCA"
],
[
"shortestPath",
"findLevel"
]
] | 2 | [] | false | [
"TreeNode"
] | true | pytest | true | [] | true | b399449337c6a5a26af56268675b6dbffd0c3fd9 | fbd35cf95945a3923590140704afac5194fd8607 | |||
v1.0 | instruct | Leetcode | Leetcode_6536_I | You are given a list of strings `words` where each word consists of lowercase English letters. You need to determine if there exists a pair of distinct indices `(i, j)` such that the concatenation of `words[i] + words[j]` forms a palindrome. Return _a boolean value indicating the existence of such a pair._ | def is_palindrome(s: str) -> bool:
"""
Helper function to check if a string is a palindrome.
"""
return s == s[::-1]
def pairs_forming_palindrome(words):
"""
Returns True if there exists a pair of distinct indices (i, j) such that
the concatenation of words[i] + words[j] forms a palindrome.... | from solution import pairs_forming_palindrome
def test_no_palindrome_pair():
assert pairs_forming_palindrome(["abc", "def", "ghi"]) == False
def test_simple_palindrome_pair():
assert pairs_forming_palindrome(["abc", "cba", "def"]) == True
def test_single_char_palindrome_pair():
assert pairs_forming_palin... | [
{
"docstring": "Helper function to check if a string is a palindrome.",
"function_declaration": "def is_palindrome(s):",
"function_name": "is_palindrome",
"parameter_list": "(s)"
},
{
"docstring": "Returns True if there exists a pair of distinct indices (i, j) such that\nthe concatenation of... | [
1,
0,
0,
1,
1,
1,
1,
1,
1,
1
] | 8 | easy | 0.8 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_palindrome(s):\n \"\"\"\n Returns True if the string s is a palindrome, otherwise False.\n \"\"\"\n return s == s[::-1]\n\ndef can_form_palindrome(words):\... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000006536",
"row_id": 6536,
"seed_ids": "['2410', '290', '787']"
} | 0.788746 | Leetcode_6536 | I | [
"is_palindrome",
"pairs_forming_palindrome"
] | 2 | [
[
"pairs_forming_palindrome",
"is_palindrome"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | b9ad1a1ebfb928af92d168fb91ea2378237985f7 | aec2d570dc151ac2edd87c7d475d2910eccae03b | |||
v1.0 | instruct | Leetcode | Leetcode_6648_I | You are given an array `books` where `books[i]` represents the number of pages in the `i-th` book. You are also given an integer `k` which represents the number of students. The books need to be divided among the `k` students in such a way that each student gets at least one book and the maximum number of pages assigne... | def is_feasible(books, k, max_pages):
students_required = 1
current_sum = 0
for pages in books:
if current_sum + pages > max_pages:
students_required += 1
current_sum = pages
if students_required > k:
return False
else:
curren... | from solution import min_max_pages
def test_single_book():
assert min_max_pages([100], 1) == 100
def test_multiple_books_single_student():
assert min_max_pages([10, 20, 30, 40], 1) == 100
def test_multiple_books_multiple_students():
assert min_max_pages([10, 20, 30, 40], 2) == 60
assert min_max_pages... | [
{
"docstring": null,
"function_declaration": "def is_feasible(books, k, max_pages):",
"function_name": "is_feasible",
"parameter_list": "(books, k, max_pages)"
},
{
"docstring": null,
"function_declaration": "def min_max_pages(books, k):",
"function_name": "min_max_pages",
"param... | [
0,
1,
0,
0,
1,
0,
1,
0,
1,
1
] | 5 | medium | 0.5 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_feasible(books, k, curr_min):\n students_required = 1\n curr_sum = 0\n \n for pages in books:\n if pages > curr_min:\n return False\n\n ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000006648",
"row_id": 6648,
"seed_ids": "['1310', '2397', '1136']"
} | 0.638246 | Leetcode_6648 | I | [
"is_feasible",
"min_max_pages"
] | 2 | [
[
"min_max_pages",
"is_feasible"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 51168c11b126168484f405746b74aa52b4c8ef5e | ddeef97779657a5d48d08911a22af2614c85bfaf | |||
v1.0 | instruct | Leetcode | Leetcode_6689_I | Given three integers `x`, `y`, and `n`, return an array of the first `n` integers that are multiples of both `x` and `y`. The resulting integers should be in ascending order. If `n` is less than or equal to 0, return an empty array. | def common_multiples(x, y, n):
"""
Returns an array of the first n integers that are multiples of both x and y.
Parameters:
- x (int): the first integer
- y (int): the second integer
- n (int): the number of multiples to return
Returns:
- List[int]: an array of the first n comm... | from solution import common_multiples
def test_common_multiples_basic():
assert common_multiples(3, 4, 5) == [12, 24, 36, 48, 60]
def test_common_multiples_zero():
assert common_multiples(3, 4, 0) == []
assert common_multiples(3, 4, -1) == []
def test_common_multiples_one():
assert common_multiples(3... | [
{
"docstring": "Returns an array of the first n integers that are multiples of both x and y.\n\nParameters:\n- x (int): the first integer\n- y (int): the second integer\n- n (int): the number of multiples to return\n\nReturns:\n- List[int]: an array of the first n common multiples of x and y",
"function_dec... | [
1,
1,
0,
1,
1,
1,
1,
1,
1,
1
] | 9 | easy | 0.9 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def find_multiples(x, y, n):\n \"\"\"\n Returns an array of the first n integers that are multiples of both x and y.\n \"\"\"\n if n <= 0:\n return []\n ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000006689",
"row_id": 6689,
"seed_ids": "['1999', '2407', '1624']"
} | 0.620912 | Leetcode_6689 | I | [
"common_multiples",
"gcd"
] | 2 | [
[
"common_multiples",
"gcd"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | c73ac9c3093f95649100712c6aa52a864d513fb1 | 732029fff62b387335687da96f8f0373154568f4 | |||
v1.0 | instruct | Leetcode | Leetcode_7578_I | You are given a string `word` and an integer `n`. Your task is to shift each letter in the string `word` by `n` positions forward in the alphabet. If the shift moves past 'z', it should wrap around to the beginning of the alphabet. Maintain the case of the letters (i.e., lowercase and uppercase) and ensure that non-let... | def shift_letter(ch, n):
if 'a' <= ch <= 'z':
return chr((ord(ch) - ord('a') + n) % 26 + ord('a'))
elif 'A' <= ch <= 'Z':
return chr((ord(ch) - ord('A') + n) % 26 + ord('A'))
return ch
def shift_string(word, n):
"""
Shifts each letter in the string `word` by `n` positions forward in... | from solution import shift_string
def test_shift_string_no_shift():
assert shift_string('abcXYZ', 0) == 'abcXYZ'
def test_shift_string_basic_shift():
assert shift_string('abc', 1) == 'bcd'
assert shift_string('xyz', 1) == 'yza'
def test_shift_string_with_wrap_around():
assert shift_string('xyz', 3) =... | [
{
"docstring": null,
"function_declaration": "def shift_letter(ch, n):",
"function_name": "shift_letter",
"parameter_list": "(ch, n)"
},
{
"docstring": "Shifts each letter in the string `word` by `n` positions forward in the alphabet.\nNon-letter characters remain unchanged.",
"function_... | [
0,
1,
0,
1,
1,
1,
1,
1,
1
] | 7 | easy | 0.778 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def shift_letters(word, n):\n \"\"\"\n Shifts each letter in the word by n positions forward in the alphabet. \n If the shift moves past 'z', it wraps around to the ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000007578",
"row_id": 7578,
"seed_ids": "['1309', '1642', '2053']"
} | 0.673692 | Leetcode_7578 | I | [
"shift_letter",
"shift_string"
] | 2 | [
[
"shift_string",
"shift_letter"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 0310e0977ced60df4ea9ea7b139896306edf6d9d | 42c82d3aca4cc586def63b58f53f1680ffac900c | |||
v1.0 | instruct | Leetcode | Leetcode_7754_I | You are given a tree, which is a connected and acyclic undirected graph, with `n` nodes labeled from `1` to `n` and `n-1` edges. You need to find **two nodes** such that their **distance** (defined as the number of edges in the shortest path connecting them) is maximized. Return _this maximum distance_. | from collections import deque
def bfs_farthest_node(start, graph, n):
"""
Performs BFS to find the farthest node from the start node.
"""
visited = [False] * (n + 1)
distance = [0] * (n + 1)
queue = deque([start])
visited[start] = True
farthest_node = start
while queue:
nod... | from solution import max_distance
def test_single_node():
assert max_distance(1, []) == 0
def test_two_nodes():
assert max_distance(2, [[1, 2]]) == 1
def test_three_nodes_in_line():
assert max_distance(3, [[1, 2], [2, 3]]) == 2
def test_five_nodes_star():
assert max_distance(5, [[1, 2], [1, 3], [1, ... | [
{
"docstring": "Performs BFS to find the farthest node from the start node.",
"function_declaration": "def bfs_farthest_node(start, graph, n):",
"function_name": "bfs_farthest_node",
"parameter_list": "(start, graph, n)"
},
{
"docstring": "Finds the maximum distance between two nodes in a tr... | [
0,
0,
0,
1,
1,
0,
0,
0,
1,
1
] | 4 | medium | 0.4 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def tree_diameter(n, edges):\n from collections import defaultdict, deque\n \n def bfs(start_node):\n visited = [-1] * (n + 1)\n queue = deque([(start_... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000007754",
"row_id": 7754,
"seed_ids": "['2560', '1258', '2435']"
} | 0.637038 | Leetcode_7754 | I | [
"bfs_farthest_node",
"max_distance"
] | 2 | [
[
"max_distance",
"bfs_farthest_node"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | fdfb3532f6e4ad1a08502e78b44a7b60decf0801 | b17ed8ddb71de611a3a96b50f4601002d16417fd | |||
v1.0 | instruct | Leetcode | Leetcode_7793_I | You are given a list of strings `words` and another string `targetWord`. The goal is to determine the minimum number of changes (insertions, deletions, or substitutions) required to convert any one of the strings in `words` to `targetWord`. Each change counts as one operation. Return the minimum number of operations ne... | def min_distance(word1, word2):
"""
Helper function to calculate the minimum edit distance between two strings using dynamic programming.
"""
m, n = len(word1), len(word2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0:... | from solution import min_changes
def test_min_changes():
assert min_changes(["cat", "bat", "rat"], "hat") == 1
assert min_changes(["hello", "world"], "hold") == 2
assert min_changes(["a", "b", "c"], "d") == 1
assert min_changes(["abc", "def", "ghi"], "xyz") == 3
assert min_changes([], "any") == -1
... | [
{
"docstring": "Helper function to calculate the minimum edit distance between two strings using dynamic programming.",
"function_declaration": "def min_distance(word1, word2):",
"function_name": "min_distance",
"parameter_list": "(word1, word2)"
},
{
"docstring": "Returns the minimum number... | [
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def min_operations_to_transform(words, targetWord):\n if not words:\n return -1\n \n def edit_distance(word1, word2):\n m, n = len(word1), len(word2)\n... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000007793",
"row_id": 7793,
"seed_ids": "['417', '1800', '1599']"
} | 0.738656 | Leetcode_7793 | I | [
"min_distance",
"min_changes"
] | 2 | [
[
"min_changes",
"min_distance"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | ec68e63588901852f6e2f648dfcb0324207009d9 | 5222d9e72e223bbf69fd64a65ffcf8ce1ece4c40 | |||
v1.0 | instruct | Leetcode | Leetcode_7809_I | Given a list of integers `nums` and an integer `m`, partition the list into `m` contiguous subarrays such that the **maximum** sum of the subarrays is minimized. Return the minimized maximum sum. The partitioning must be non-empty and contiguous, and there must be exactly `m` subarrays. | def can_partition(nums, m, max_sum):
"""
Helper function to determine if we can partition the list into m subarrays
with the given maximum sum.
"""
current_sum = 0
count = 1
for num in nums:
if current_sum + num > max_sum:
count += 1
current_sum = num
... | from solution import minimize_max_sum
def test_example_case():
nums = [7, 2, 5, 10, 8]
m = 2
assert minimize_max_sum(nums, m) == 18
def test_single_element_partitions():
nums = [1, 2, 3, 4, 5]
m = 5
assert minimize_max_sum(nums, m) == 5
def test_entire_array_one_partition():
nums = [1, 2,... | [
{
"docstring": "Helper function to determine if we can partition the list into m subarrays\nwith the given maximum sum.",
"function_declaration": "def can_partition(nums, m, max_sum):",
"function_name": "can_partition",
"parameter_list": "(nums, m, max_sum)"
},
{
"docstring": "Returns the mi... | [
0,
0,
1,
0,
1,
1,
0,
1,
1,
1
] | 6 | medium | 0.6 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def can_partition(nums, m, max_sum):\n current_subarray_sum = 0\n subarray_count = 1\n \n for num in nums:\n if current_subarray_sum + num > max_sum:\n ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000007809",
"row_id": 7809,
"seed_ids": "['1397', '257', '2233']"
} | 0.747399 | Leetcode_7809 | I | [
"can_partition",
"minimize_max_sum"
] | 2 | [
[
"minimize_max_sum",
"can_partition"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 292d980661130a6a4ca43132adfadbaad43dc3e5 | 874252cd9fe4d8880db6df5dc93667195fc9515f | |||
v1.0 | instruct | Leetcode | Leetcode_7979_I | Write a function that takes the `head` of a singly linked list and reverses the nodes in `k`-group chunks, where `k` is a given positive integer. If the number of nodes is not a multiple of `k`, leave the last remaining nodes as they are. Return the modified head of the list. | class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverseKGroup(head, k):
"""
Reverses nodes in the k-group chunks in the linked list.
:param head: ListNode, the head of the linked list
:param k: int, the size of the group to reverse
:retu... | from solution import ListNode, reverseKGroup
def list_to_array(head):
array = []
while head:
array.append(head.val)
head = head.next
return array
def array_to_list(array):
dummy = ListNode(0)
curr = dummy
for val in array:
curr.next = ListNode(val)
curr = curr.n... | [] | [
1,
1,
1,
1,
1,
1,
1,
1,
1,
0
] | 9 | easy | 0.9 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "class ListNode:\n def __init__(self, val=0, next=None):\n self.val = val\n self.next = next\n\ndef reverseKGroup(head, k):\n if k == 1 or not head:\n ... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000007979",
"row_id": 7979,
"seed_ids": "['606', '144', '340']"
} | 0.592914 | Leetcode_7979 | I | [
"reverseKGroup",
"reverse"
] | 2 | [
[
"reverseKGroup",
"reverse"
]
] | 1 | [] | false | [
"ListNode"
] | true | pytest | true | [] | true | 80cd34ebdae8725662d345df1654ed990bb35a41 | a44ec32d06f143a7f039301460ba7d82e08f0a68 | |||
v1.0 | instruct | Leetcode | Leetcode_8017_I | You are given a string s and an array of strings words. Determine if s contains all the words as subsequences in the same order as they appear in the array. A string b is a subsequence of string a if b can be formed by deleting some (or none) of the characters from a without changing the order of the remaining characte... | def is_subsequence(s, words):
"""
Determines if the string s contains all the words as subsequences in the same order.
:param s: The main string
:param words: List of words to check as subsequences
:return: Boolean value indicating if s contains all the words as subsequences in order
"""
... | from solution import is_subsequence
def test_single_word():
assert is_subsequence("abcde", ["abc"]) == True
def test_multiple_words():
assert is_subsequence("abcde", ["abc", "de"]) == True
def test_non_subsequence_word():
assert is_subsequence("abcde", ["abc", "ef"]) == False
def test_out_of_order_words... | [
{
"docstring": "Determines if the string s contains all the words as subsequences in the same order.\n\n:param s: The main string\n:param words: List of words to check as subsequences\n:return: Boolean value indicating if s contains all the words as subsequences in order",
"function_declaration": "def is_su... | [
1,
0,
0,
0,
0,
0,
1,
0,
0,
1
] | 3 | hard | 0.3 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_subsequence(s, words):\n \"\"\"\n Determine if the string s contains all the words as subsequences in the same order as they appear in words.\n \n Args:\n ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000008017",
"row_id": 8017,
"seed_ids": "['350', '790', '499']"
} | 0.728246 | Leetcode_8017 | I | [
"is_subsequence",
"find_subsequence_from_index"
] | 2 | [
[
"is_subsequence",
"find_subsequence_from_index"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 2b202d3e9c93dbe618c7e73ef7ded89cd4ae32e5 | b51003e67a40262ef648e7bd8b73bcb82be9ba31 | |||
v1.0 | instruct | Leetcode | Leetcode_8058_I | Write a function that determines whether a given positive integer `n` is a super palindrome. A super palindrome is an integer whose square is a palindrome. Return `true` if `n` is a super palindrome and `false` otherwise. | def is_palindrome(x):
"""
Check if a given number x is a palindrome.
"""
return str(x) == str(x)[::-1]
def is_super_palindrome(n):
"""
Determines whether a given positive integer n is a super palindrome.
A super palindrome is an integer whose square is a palindrome.
"""
if n < 1:
... | from solution import is_super_palindrome
def test_is_super_palindrome_positive_cases():
# 5^2 = 25 (not a palindrome)
assert not is_super_palindrome(5)
# 9^2 = 81 (not a palindrome)
assert not is_super_palindrome(9)
# 11^2 = 121 (a palindrome)
assert is_super_palindrome(11)
# 22^2 = 484 (a ... | [
{
"docstring": "Check if a given number x is a palindrome.",
"function_declaration": "def is_palindrome(x):",
"function_name": "is_palindrome",
"parameter_list": "(x)"
},
{
"docstring": "Determines whether a given positive integer n is a super palindrome.\nA super palindrome is an integer wh... | [
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_palindrome(x):\n \"\"\"\n Checks if a given integer or string x is a palindrome.\n \"\"\"\n str_x = str(x)\n return str_x == str_x[::-1]\n\ndef is_super... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000008058",
"row_id": 8058,
"seed_ids": "['2517', '2124', '1861']"
} | 0.667634 | Leetcode_8058 | I | [
"is_palindrome",
"is_super_palindrome"
] | 2 | [
[
"is_super_palindrome",
"is_palindrome"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 5b359ba0046bd054d229995ce5166f5f0b0e1d59 | c0c0bd22a02b2b885663ff20abbe1161b86e4364 | |||
v1.0 | instruct | Leetcode | Leetcode_8405_I | You are given an array `points` containing `n` elements where each element is a tuple representing the x and y coordinates of a point on a 2D plane. You are required to find the pair of points that forms the line segment with the maximum length. Return the coordinates of these two points. Note that the answer should be... | import math
def distance(point1, point2):
"""
Returns the Euclidean distance between two points.
"""
return math.sqrt((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2)
def max_length_line_segment(points):
"""
Returns the pair of points that form the line segment with the maximum length.... | from solution import distance, max_length_line_segment
def test_distance():
assert distance((0, 0), (3, 4)) == 5.0
assert distance((1, 1), (4, 5)) == 5.0
assert distance((2, 3), (2, 3)) == 0.0
def test_max_length_line_segment():
points = [(0, 0), (1, 1), (2, 2)]
assert max_length_line_segment(poin... | [
{
"docstring": "Returns the Euclidean distance between two points.",
"function_declaration": "def distance(point1, point2):",
"function_name": "distance",
"parameter_list": "(point1, point2)"
},
{
"docstring": "Returns the pair of points that form the line segment with the maximum length.\n\... | [
1,
0,
1,
1,
1,
1,
1,
1,
1,
1
] | 9 | easy | 0.9 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "import math\n\ndef distance(point1, point2):\n \"\"\"\n Returns the Euclidean distance between two points on the 2D plane.\n \"\"\"\n return math.sqrt((point1[0] ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000008405",
"row_id": 8405,
"seed_ids": "['1190', '1986', '808']"
} | 0.656107 | Leetcode_8405 | I | [
"distance",
"max_length_line_segment"
] | 2 | [
[
"max_length_line_segment",
"distance"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 7ede00b2740813b74e3307afb89881aa4d66d325 | 858d869560f771ea8be1989da53c1680ba143aa8 | |||
v1.0 | instruct | Leetcode | Leetcode_8531_I | You are given a list of `n` integers representing the number of pages in `n` consecutive books. You need to allocate these books to `k` students in such a way that each student gets at least one book, and the maximum number of pages assigned to any student is minimized. Return _the minimized maximum number of pages ass... | def is_valid_distribution(books, n, k, max_pages):
count_students = 1
pages_assigned = 0
for i in range(n):
if pages_assigned + books[i] > max_pages:
count_students += 1
pages_assigned = books[i]
if count_students > k:
return False
els... | from solution import find_minimum_pages
def test_find_minimum_pages_case1():
assert find_minimum_pages([12, 34, 67, 90], 4, 2) == 113
def test_find_minimum_pages_case2():
assert find_minimum_pages([10, 20, 30, 40], 4, 2) == 60
def test_find_minimum_pages_case3():
assert find_minimum_pages([10, 20, 30, 40... | [
{
"docstring": null,
"function_declaration": "def is_valid_distribution(books, n, k, max_pages):",
"function_name": "is_valid_distribution",
"parameter_list": "(books, n, k, max_pages)"
},
{
"docstring": null,
"function_declaration": "def find_minimum_pages(books, n, k):",
"function_... | [
1,
0,
1,
1,
1,
0,
1,
1,
1,
1
] | 8 | easy | 0.8 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_feasible(books, n, k, curr_min):\n \"\"\"\n Helper function to check if a given minimum is feasible.\n \"\"\"\n students_required = 1\n curr_sum = 0\n\n... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000008531",
"row_id": 8531,
"seed_ids": "['1727', '1937', '227']"
} | 0.675476 | Leetcode_8531 | I | [
"is_valid_distribution",
"find_minimum_pages"
] | 2 | [
[
"find_minimum_pages",
"is_valid_distribution"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 1224f9d0b987e45f625fb2ba9a5b65ecd4831970 | f13c60b02793fb934c15fdd8d1351b8c820c971c | |||
v1.0 | instruct | Leetcode | Leetcode_8744_I | You are given a string `s` consisting of only lowercase letters and an integer `n`. You need to transform the string by moving each letter to a new position dictated by the given integer `n`. When transforming, the new position of each character is determined by moving to the right by `n` places in the alphabetical ord... | def shift_character(c, n):
"""
Shifts a character c by n positions in alphabetical order, wrapping around if necessary.
"""
return chr((ord(c) - ord('a') + n) % 26 + ord('a'))
def transform_string(s, n):
"""
Transforms the string s by moving each letter to a new position dictated by the given i... | from solution import transform_string
def test_transform_string_positive_shift():
assert transform_string("abc", 1) == "bcd"
assert transform_string("xyz", 1) == "yza"
assert transform_string("hello", 2) == "jgnnq"
def test_transform_string_negative_shift():
assert transform_string("abc", -1) == "zab"... | [
{
"docstring": "Shifts a character c by n positions in alphabetical order, wrapping around if necessary.",
"function_declaration": "def shift_character(c, n):",
"function_name": "shift_character",
"parameter_list": "(c, n)"
},
{
"docstring": "Transforms the string s by moving each letter to ... | [
1,
1,
1,
1,
1,
1,
1,
0,
1,
0
] | 8 | easy | 0.8 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def transform_string(s, n):\n \"\"\"\n Transforms the string `s` by shifting each letter `n` positions in the alphabet.\n \n Args:\n s (str): The input string ... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000008744",
"row_id": 8744,
"seed_ids": "['390', '2233', '1073']"
} | 0.715999 | Leetcode_8744 | I | [
"shift_character",
"transform_string"
] | 2 | [
[
"transform_string",
"shift_character"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | bdd0a1c210849f57eee23cbeb6b691d134ef0cfa | 20299f4bebb6c6085ec2587dda8fb89715f201ca | |||
v1.0 | instruct | Leetcode | Leetcode_8797_I | You are given `n` strings, each string representing a binary number. You need to find the maximum XOR value between any two of the given binary numbers. Return _the maximum XOR value that can be obtained by selecting exactly two of the given binary numbers_. | def binary_to_int(binary_str):
"""
Convert a binary string to an integer.
"""
return int(binary_str, 2)
def max_xor_of_two_binaries(binaries):
"""
Finds the maximum XOR value between any two binary numbers from the list.
"""
max_xor = 0
num_ints = [binary_to_int(b) for b in binaries... | from solution import max_xor_of_two_binaries
def test_max_xor_of_two_binaries():
# Test with simple binary numbers
assert max_xor_of_two_binaries(["1010", "0101"]) == 15
# Test where maximum XOR comes from the last pair
assert max_xor_of_two_binaries(["0000", "1111", "0101"]) == 15
# Test where the... | [
{
"docstring": "Convert a binary string to an integer.",
"function_declaration": "def binary_to_int(binary_str):",
"function_name": "binary_to_int",
"parameter_list": "(binary_str)"
},
{
"docstring": "Finds the maximum XOR value between any two binary numbers from the list.",
"function_d... | [
0,
0,
0,
0,
0,
0,
0,
0,
1,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def find_maximum_xor(nums):\n \"\"\"\n Returns the maximum XOR value among any two binary numbers represented as strings in the list nums.\n \"\"\"\n # Convert ea... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000008797",
"row_id": 8797,
"seed_ids": "['1977', '956', '1402']"
} | 0.710961 | Leetcode_8797 | I | [
"binary_to_int",
"max_xor_of_two_binaries"
] | 2 | [
[
"max_xor_of_two_binaries",
"binary_to_int"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 0fdb8aacec01c24164a2f9700972c8cde6b90cc0 | 187e328f34a183c2239f4d28ac6a163ed1fadf9b | |||
v1.0 | instruct | Leetcode | Leetcode_8896_I | You are given an integer array `arr` of length `n` and an integer `k`. Split the integer array into exactly `k` non-empty subsets such that the maximum sum of any subset is as small as possible. Return the minimum possible value of the maximum sum of the `k` subsets. | def can_partition(nums, k, max_sum):
subset_sums = [0] * k
return backtrack(0, nums, subset_sums, k, max_sum)
def backtrack(index, nums, subset_sums, k, max_sum):
if index == len(nums):
return True
for i in range(k):
if subset_sums[i] + nums[index] <= max_sum:
subset_su... | from solution import split_array_into_subsets
def test_example_case():
assert split_array_into_subsets([1, 2, 3, 4], 3) == 4
def test_single_element_case():
assert split_array_into_subsets([10], 1) == 10
def test_equal_distribution_case():
assert split_array_into_subsets([1, 1, 1, 1], 2) == 2
def test_u... | [
{
"docstring": null,
"function_declaration": "def can_partition(nums, k, max_sum):",
"function_name": "can_partition",
"parameter_list": "(nums, k, max_sum)"
},
{
"docstring": null,
"function_declaration": "def backtrack(index, nums, subset_sums, k, max_sum):",
"function_name": "back... | [
0,
0,
0,
0,
1,
1,
1,
1,
0,
0
] | 4 | medium | 0.4 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def can_partition(arr, n, k, target):\n subset_sums = [0] * k\n arr.sort(reverse=True)\n\n def backtrack(index):\n if index == n:\n return True\n ... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000008896",
"row_id": 8896,
"seed_ids": "['2322', '525', '1849']"
} | 0.736548 | Leetcode_8896 | I | [
"can_partition",
"backtrack",
"split_array_into_subsets"
] | 3 | [
[
"can_partition",
"backtrack"
],
[
"split_array_into_subsets",
"can_partition"
]
] | 2 | [] | false | [] | false | pytest | true | [] | true | e8b6cb728729855ed4d19bb6704cd88c8474c7e2 | f8a3293d5d6fc102d51c4c26bd37c3892cbea24b | |||
v1.0 | instruct | Leetcode | Leetcode_9247_I | Given a list of non-negative integers representing the amount of work at each task, and an integer `k` representing the number of workers, distribute the tasks among the workers such that the maximum work any single worker has to do is minimized. Return this minimum possible maximum work. | def canDistribute(tasks, workers, maxLoad):
currentWorker = 1
currentSum = 0
for task in tasks:
currentSum += task
if currentSum > maxLoad:
currentWorker += 1
currentSum = task
if currentWorker > workers:
return False
return True
def ... | from solution import minMaxWorkLoad
def test_no_tasks():
assert minMaxWorkLoad([], 3) == 0
def test_single_task():
assert minMaxWorkLoad([10], 3) == 10
def test_equal_distribution():
assert minMaxWorkLoad([10, 10, 10], 3) == 10
def test_more_workers_than_tasks():
assert minMaxWorkLoad([10, 10, 10], ... | [
{
"docstring": null,
"function_declaration": "def canDistribute(tasks, workers, maxLoad):",
"function_name": "canDistribute",
"parameter_list": "(tasks, workers, maxLoad)"
},
{
"docstring": null,
"function_declaration": "def minMaxWorkLoad(tasks, k):",
"function_name": "minMaxWorkLoa... | [
0,
1,
0,
1,
0,
0,
0,
1,
0,
1
] | 4 | medium | 0.4 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def canDistribute(nums, k, maxWork):\n \"\"\"\n Helper function to check if it's possible to distribute work such that\n no worker has more than `maxWork` amount of ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000009247",
"row_id": 9247,
"seed_ids": "['1131', '1666', '513']"
} | 0.622836 | Leetcode_9247 | I | [
"canDistribute",
"minMaxWorkLoad"
] | 2 | [
[
"minMaxWorkLoad",
"canDistribute"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | fb6f85ac8c2fc7b48392397e2eb544561c478cb5 | 29d3bd044fdb8fea339f50e97b6ee06795018f31 | |||
v1.0 | instruct | Leetcode | Leetcode_9270_I | A company wants to optimize the time employees spend on tasks. Each employee is assigned a list of tasks, where each task has a certain duration. The goal is to minimize the maximum duration any one employee spends on their tasks.
Given an array of integers `tasks`, where `tasks[i]` represents the duration of the `i-t... | def is_valid_distribution(tasks, k, max_task_time):
current_sum = 0
required_employees = 1
for task in tasks:
if current_sum + task <= max_task_time:
current_sum += task
else:
required_employees += 1
current_sum = task
if required_employee... | from solution import min_max_task_time
def test_single_employee():
assert min_max_task_time([7, 2, 5, 10, 8], 1) == 32
def test_more_employees_than_tasks():
assert min_max_task_time([7, 2, 5, 10, 8], 10) == 10
def test_equal_distribution():
assert min_max_task_time([7, 2, 5, 10, 8], 2) == 18
def test_tw... | [
{
"docstring": null,
"function_declaration": "def is_valid_distribution(tasks, k, max_task_time):",
"function_name": "is_valid_distribution",
"parameter_list": "(tasks, k, max_task_time)"
},
{
"docstring": null,
"function_declaration": "def min_max_task_time(tasks, k):",
"function_na... | [
1,
1,
1,
1,
1,
1,
0,
1,
1,
1
] | 9 | easy | 0.9 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def min_max_task_duration(tasks, k):\n def canSplit(max_duration):\n current_sum = 0\n needed_splits = 1\n for task in tasks:\n if current_... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000009270",
"row_id": 9270,
"seed_ids": "['204', '1441', '649']"
} | 0.674901 | Leetcode_9270 | I | [
"is_valid_distribution",
"min_max_task_time"
] | 2 | [
[
"min_max_task_time",
"is_valid_distribution"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | f4a7a736e5b0914d35195bbf9e3d3ce509fa4b5c | 12367eb79755177fbcbcf5798c1da56e5d9553aa | |||
v1.0 | instruct | Leetcode | Leetcode_9373_I | A city is planning to build a new network of `m` roads to connect `n` buildings, where the buildings are numbered from `0` to `n - 1`. Each road can only connect two different buildings and has a certain length. The goal is to minimize the total length of the roads built while ensuring that there is a path between any ... | def find(parent, i):
if parent[i] == i:
return i
return find(parent, parent[i])
def union(parent, rank, x, y):
xroot = find(parent, x)
yroot = find(parent, y)
if rank[xroot] < rank[yroot]:
parent[xroot] = yroot
elif rank[xroot] > rank[yroot]:
parent[yroot] = xroot
e... | from solution import minimum_road_length
def test_minimum_road_length():
assert minimum_road_length(4, [(0, 1, 1), (1, 2, 2), (0, 2, 2), (2, 3, 1)]) == 4
assert minimum_road_length(5, [(0, 1, 2), (1, 2, 3), (0, 2, 3), (1, 3, 4), (3, 4, 3), (2, 4, 1)]) == 9
assert minimum_road_length(3, [(0, 1, 10), (1, 2, ... | [
{
"docstring": null,
"function_declaration": "def find(parent, i):",
"function_name": "find",
"parameter_list": "(parent, i)"
},
{
"docstring": null,
"function_declaration": "def union(parent, rank, x, y):",
"function_name": "union",
"parameter_list": "(parent, rank, x, y)"
},
... | [
0,
0,
0,
0,
0,
0,
1,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def find(parent, i):\n if parent[i] == i:\n return i\n return find(parent, parent[i])\n\ndef union(parent, rank, x, y):\n xroot = find(parent, x)\n yroot =... | trial_gpt4o_6 | {
"original_instruction": "",
"prompt_id": "0000009373",
"row_id": 9373,
"seed_ids": "['2127', '2599', '1478']"
} | 0.722962 | Leetcode_9373 | I | [
"find",
"union",
"minimum_road_length"
] | 3 | [
[
"minimum_road_length",
"find"
],
[
"minimum_road_length",
"union"
],
[
"union",
"find"
]
] | 3 | [] | false | [] | false | pytest | true | [] | true | 347f4844faee0cb4a9005e738809afbbbc2fb51f | f433e4712c176f3d1148c03f3e02ae29c1641b5e | |||
v1.0 | instruct | Leetcode | Leetcode_9464_I | You are given a binary tree with `n` nodes, where each node is labeled from `1` to `n`. The tree is represented as a list of `n-1` pairs of integers, where each pair `(u, v)` denotes an edge between nodes `u` and `v`. Implement a function to determine whether the binary tree is balanced. A binary tree is defined to be ... | from collections import defaultdict
class TreeNode:
def __init__(self, val=0):
self.val = val
self.left = None
self.right = None
def build_tree(edges, n):
if n == 0:
return None
nodes = {i: TreeNode(i) for i in range(1, n + 1)}
children = defaultdict(list)
for... | from solution import is_balanced
def test_balanced_tree():
edges = [(1, 2), (1, 3), (2, 4), (2, 5)]
n = 5
assert is_balanced(edges, n)
def test_unbalanced_tree():
edges = [(1, 2), (1, 3), (2, 4), (4, 5)]
n = 5
assert not is_balanced(edges, n)
def test_single_node_tree():
edges = []
n ... | [] | [
0,
1,
1,
0,
1,
1,
1,
1,
1,
1
] | 8 | easy | 0.8 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "class TreeNode:\n def __init__(self, x):\n self.val = x\n self.left = None\n self.right = None\n\ndef construct_tree(edges, n):\n from collections ... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000009464",
"row_id": 9464,
"seed_ids": "['2022', '553', '1807']"
} | 0.867153 | Leetcode_9464 | I | [
"build_tree",
"is_balanced_tree",
"is_balanced"
] | 3 | [
[
"is_balanced",
"build_tree"
],
[
"is_balanced",
"is_balanced_tree"
]
] | 2 | [] | false | [
"TreeNode"
] | true | pytest | true | [] | true | ab3bf3f69c0536f6176f8accd9e5837431ef6f4e | 32193c2fc0605c02919f11c7a7625b3e8ca5b225 | |||
v1.0 | instruct | Leetcode | Leetcode_9478_I | Given an array of non-negative integers `tasks`, where `tasks[i]` represents the amount of time required to complete the `i`th task, and an integer `n` representing the number of identical machines available to process these tasks, devise a strategy to assign all tasks such that the total time to complete all tasks is ... | def can_complete(tasks, machines, max_time):
current_machines = 1
current_load = 0
for task in tasks:
if current_load + task <= max_time:
current_load += task
else:
current_machines += 1
if current_machines > machines:
return False
... | from solution import minimum_total_time
def test_minimum_total_time():
assert minimum_total_time([1, 2, 3, 4, 5], 2) == 9
assert minimum_total_time([1, 5, 6, 3], 3) == 6
assert minimum_total_time([7, 2, 5, 10, 8], 2) == 18
assert minimum_total_time([6, 3, 3, 4], 3) == 6
assert minimum_total_time([1... | [
{
"docstring": null,
"function_declaration": "def can_complete(tasks, machines, max_time):",
"function_name": "can_complete",
"parameter_list": "(tasks, machines, max_time)"
},
{
"docstring": null,
"function_declaration": "def minimum_total_time(tasks, machines):",
"function_name": "... | [
0,
0,
0,
0,
0,
1,
1,
1,
0,
0
] | 3 | hard | 0.3 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "import heapq\n\ndef min_completion_time(tasks, n):\n \"\"\"\n Returns the minimum total time required to complete all tasks\n using n identical machines.\n \n ... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000009478",
"row_id": 9478,
"seed_ids": "['1400', '1723', '826']"
} | 0.769279 | Leetcode_9478 | I | [
"can_complete",
"minimum_total_time"
] | 2 | [
[
"minimum_total_time",
"can_complete"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | ceb5b214f7e6ffcbf9b8b2421f25945d79e0caba | 5306aadd49d5f07a7c2eee22dc1c77474e7ac10b | |||
v1.0 | instruct | Leetcode | Leetcode_9553_I | You are given an array `points` containing the coordinates of some points on a 2D plane, where points[i] = [xi, yi]. The points form a simple polygon when joined sequentially by straight lines. You are also given an integer `k`. Your task is to find the k-th smallest distance from a point to its nearest neighbor in th... | import math
def euclidean_distance(p1, p2):
"""
Calculate the Euclidean distance between two points p1 and p2.
"""
return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
def k_th_smallest_distance(points, k):
"""
Find the k-th smallest distance from a point to its nearest neighbor in th... | from solution import k_th_smallest_distance
def test_basic_polygon():
points = [[0,0], [0,1], [1,1], [1,0]]
k = 2
assert k_th_smallest_distance(points, k) == 1.0
def test_larger_polygon():
points = [[0,0], [0,2], [2,2], [2,0]]
k = 2
assert k_th_smallest_distance(points, k) == 2.0
def test... | [
{
"docstring": "Calculate the Euclidean distance between two points p1 and p2.",
"function_declaration": "def euclidean_distance(p1, p2):",
"function_name": "euclidean_distance",
"parameter_list": "(p1, p2)"
},
{
"docstring": "Find the k-th smallest distance from a point to its nearest neigh... | [
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "import math\nfrom heapq import nsmallest\n\ndef euclidean_distance(p1, p2):\n \"\"\"\n Calculates the Euclidean distance between points p1 and p2.\n \"\"\"\n retu... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000009553",
"row_id": 9553,
"seed_ids": "['1326', '1223', '711']"
} | 0.629653 | Leetcode_9553 | I | [
"euclidean_distance",
"k_th_smallest_distance"
] | 2 | [
[
"k_th_smallest_distance",
"euclidean_distance"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | d77aa5a023a70b15d20d01d12d74111c72e45c3d | b5e1fcb0a1fc58e2d92b7059ab43cc613f582b6b | |||
v1.0 | instruct | Leetcode | Leetcode_9690_I | You are given an array of strings `words` representing a list of words. Two words are considered to be "similar" if they can be made identical by swapping at most two characters within one word. Write a function to determine the number of distinct sets of "similar" words in the given list. A set of "similar" words is d... | def are_similar(word1, word2):
"""
Check if two words are similar by swapping at most two characters.
"""
if word1 == word2:
return True
if len(word1) != len(word2):
return False
# Finding the positions where the characters differ
different_chars = [(w1, w2) for w1,... | from solution import find_similar_word_sets
def test_find_similar_word_sets_basic():
assert find_similar_word_sets(["abc", "acb", "bac", "xyz", "xzy"]) == 2
def test_find_similar_word_sets_no_similar():
assert find_similar_word_sets(["abc", "def", "ghi"]) == 3
def test_find_similar_word_sets_identical_words(... | [
{
"docstring": "Check if two words are similar by swapping at most two characters.",
"function_declaration": "def are_similar(word1, word2):",
"function_name": "are_similar",
"parameter_list": "(word1, word2)"
},
{
"docstring": "Determine the number of distinct sets of similar words in the g... | [
0,
1,
0,
1,
0,
0,
0,
0,
0,
0
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def are_similar(word1, word2):\n \"\"\"\n Check if two words are similar by the given definition.\n Two words are similar if they can be made identical by swapping a... | trial_gpt4o_3 | {
"original_instruction": "",
"prompt_id": "0000009690",
"row_id": 9690,
"seed_ids": "['1042', '864', '2245']"
} | 0.648716 | Leetcode_9690 | I | [
"are_similar",
"find_similar_word_sets"
] | 2 | [
[
"find_similar_word_sets",
"are_similar"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 7f58f69e43106358f51ec14e730ef87449801cde | 28547cec32277e6396fddded317008745bf0441f | |||
v1.0 | instruct | Leetcode | Leetcode_9865_I | You are given a list of strings `words` and an integer `m`. A word is considered "m-desirable" if no two adjacent characters in the word are the same and the length of the word is at least `m`. Return the list of all "m-desirable" words from the input list. If no such word exists, return an empty list. | def is_m_desirable(word, m):
"""
Checks if a word is m-desirable.
A word is m-desirable if no two adjacent characters in the word are the same
and the length of the word is at least m.
"""
if len(word) < m:
return False
for i in range(len(word) - 1):
if word[i] == word[i + 1]... | from solution import is_m_desirable, m_desirable_words
def test_is_m_desirable_short_word():
assert is_m_desirable("abc", 4) == False
def test_is_m_desirable_length_criteria():
assert is_m_desirable("abcd", 4) == True
def test_is_m_desirable_adjacent_chars():
assert is_m_desirable("aabbc", 5) == Fals... | [
{
"docstring": "Checks if a word is m-desirable.\nA word is m-desirable if no two adjacent characters in the word are the same\nand the length of the word is at least m.",
"function_declaration": "def is_m_desirable(word, m):",
"function_name": "is_m_desirable",
"parameter_list": "(word, m)"
},
... | [
0,
0,
1,
0,
0,
1,
1,
0,
0,
0
] | 3 | hard | 0.3 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_m_desirable(word, m):\n \"\"\"\n Checks if the word is m-desirable.\n A word is m-desirable if no two adjacent characters in the word are the same\n and th... | trial_gpt4o_6 | {
"original_instruction": "",
"prompt_id": "0000009865",
"row_id": 9865,
"seed_ids": "['556', '1425', '1761']"
} | 0.614079 | Leetcode_9865 | I | [
"is_m_desirable",
"m_desirable_words"
] | 2 | [
[
"m_desirable_words",
"is_m_desirable"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | af40d21f671363535cdcadc6a4aeab3dcd298fa9 | b6b2a62390710ae5f93cbdd3fbcc565c11090cdb | |||
v1.0 | instruct | Leetcode | Leetcode_9902_I | You are given an m x n integer matrix `heights` representing the height of each unit cell in a continent. The continent is surrounded by a body of water represented by cells with height `0`. From any cell, you can travel to another if and only if the destination cell has a height equal to or lower than the current cell... | from collections import deque
def find_highest_peak(heights):
"""
Finds the highest peak and its coordinates in the matrix.
"""
max_height = float('-inf')
peak_coords = []
for i in range(len(heights)):
for j in range(len(heights[0])):
if heights[i][j] > max_height:
... | from solution import min_moves_to_water
def test_single_peak_single_path():
heights = [
[0, 0, 0, 0],
[0, 3, 2, 0],
[0, 3, 1, 0],
[0, 0, 0, 0]
]
assert min_moves_to_water(heights) == 1
def test_multiple_peaks_different_paths():
heights = [
[0, 0, 0, 0],
... | [
{
"docstring": "Finds the highest peak and its coordinates in the matrix.",
"function_declaration": "def find_highest_peak(heights):",
"function_name": "find_highest_peak",
"parameter_list": "(heights)"
},
{
"docstring": "Breadth-First Search (BFS) to find shortest path from the highest peak... | [
0,
0,
0,
0,
0,
0,
0,
1,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "from collections import deque\n\ndef highest_peak_to_water(heights):\n \"\"\"\n Returns the minimum number of moves required to reach the water body starting from the h... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000009902",
"row_id": 9902,
"seed_ids": "['685', '1851', '374']"
} | 0.632987 | Leetcode_9902 | I | [
"find_highest_peak",
"bfs_to_water",
"min_moves_to_water"
] | 3 | [
[
"min_moves_to_water",
"bfs_to_water"
],
[
"min_moves_to_water",
"find_highest_peak"
]
] | 2 | [] | false | [] | false | pytest | true | [] | true | 330b2980b597e33f37a4aecd3f6f753c681174bc | 27543dec453e25503062735e1ad7b10830f3ca25 | |||
v1.0 | instruct | Leetcode | Leetcode_9948_I | You are given a list of strings `words` and a search query string `pattern`. Write a function to find and return all strings from `words` that match the pattern. A string matches the pattern if there is a bijective mapping between the characters of the pattern and the characters of the string. Bijective mapping means t... | from typing import List
def matches(word: str, pattern: str) -> bool:
if len(word) != len(pattern):
return False
mapping_w2p = {}
mapping_p2w = {}
for w_char, p_char in zip(word, pattern):
if w_char in mapping_w2p and mapping_w2p[w_char] != p_char:
return False
... | from solution import find_and_replace_pattern
def test_find_and_replace_pattern():
# Example test cases
words = ["abc", "deq", "mee", "aqq", "dkd", "ccc"]
pattern = "abb"
result = find_and_replace_pattern(words, pattern)
expected = ["mee", "aqq"]
assert result == expected
def test_all_mat... | [
{
"docstring": null,
"function_declaration": "def matches(word, pattern):",
"function_name": "matches",
"parameter_list": "(word, pattern)"
},
{
"docstring": null,
"function_declaration": "def find_and_replace_pattern(words, pattern):",
"function_name": "find_and_replace_pattern",
... | [
0,
0,
0,
0,
0,
0,
0,
0,
1,
1
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "from typing import List\n\ndef find_and_replace_pattern(words: List[str], pattern: str) -> List[str]:\n def matches(word, pattern):\n if len(word) != len(pattern):\... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000009948",
"row_id": 9948,
"seed_ids": "['519', '1909', '1869']"
} | 0.636197 | Leetcode_9948 | I | [
"matches",
"find_and_replace_pattern"
] | 2 | [
[
"find_and_replace_pattern",
"matches"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 63dedfad2bbe38710631c589783014e5eae2180c | 1efe3d98f7861e923a4c66ce6f860def3bd5ba51 | |||
v1.0 | instruct | Leetcode | Leetcode_10198_I | Given a string `s` and a dictionary of words `dict`, return _the number of words in_ `dict` _that are subsequences of_ `s`. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. For example,... | def is_subsequence(s, word):
"""
Checks if word is a subsequence of s.
"""
it = iter(s)
return all(char in it for char in word)
def count_subsequences(s, dict_words):
"""
Returns the number of words in dict_words that are subsequences of s.
"""
return sum(1 for word in dict_words if... | from solution import is_subsequence, count_subsequences
def test_is_subsequence():
assert is_subsequence("abcdefg", "abc") == True
assert is_subsequence("abcdefg", "abg") == True
assert is_subsequence("abcdefg", "bg") == True
assert is_subsequence("abcdefg", "bfc") == False
assert is_subsequence("a... | [
{
"docstring": "Checks if word is a subsequence of s.",
"function_declaration": "def is_subsequence(s, word):",
"function_name": "is_subsequence",
"parameter_list": "(s, word)"
},
{
"docstring": "Returns the number of words in dict_words that are subsequences of s.",
"function_declaratio... | [
0,
0,
1,
0,
0,
0,
1,
1,
1,
1
] | 5 | medium | 0.5 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_subsequence(word, s):\n \"\"\"\n Helper function to determine if `word` is a subsequence of `s`.\n \"\"\"\n it = iter(s)\n return all(char in it for cha... | trial_gpt4o_9 | {
"original_instruction": "",
"prompt_id": "0000010198",
"row_id": 10198,
"seed_ids": "['1086', '885', '2057']"
} | 0.701012 | Leetcode_10198 | I | [
"is_subsequence",
"count_subsequences"
] | 2 | [
[
"count_subsequences",
"is_subsequence"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 02495cb793cbf5932643ef79fa979693fd43dc62 | 1db9f1a7b6a8b77f5532a998ee3d412b997132ea | |||
v1.0 | instruct | Leetcode | Leetcode_10219_I | You are given an array of integers `arr` representing the amount of time taken to complete each task. Each task can be assigned to exactly one worker. However, each worker can only work on one task at a time. You have a limited number of workers, `k`, and you need to minimize the maximum time taken by any one worker to... | def can_assign_tasks_within_max_time(arr, k, max_time):
assigned_workers = 1
current_time = 0
for task in arr:
if current_time + task <= max_time:
current_time += task
else:
assigned_workers += 1
current_time = task
if assigned_workers > k... | from solution import minimize_max_time
def test_minimize_max_time_single_task():
assert minimize_max_time([5], 1) == 5
def test_minimize_max_time_single_worker():
assert minimize_max_time([1, 2, 3, 4, 5], 1) == 15
def test_minimize_max_time_multiple_workers():
assert minimize_max_time([1, 2, 3, 4, 5], 2)... | [
{
"docstring": null,
"function_declaration": "def can_assign_tasks_within_max_time(arr, k, max_time):",
"function_name": "can_assign_tasks_within_max_time",
"parameter_list": "(arr, k, max_time)"
},
{
"docstring": null,
"function_declaration": "def minimize_max_time(arr, k):",
"funct... | [
1,
0,
0,
0,
0,
0,
0,
1,
0,
0
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def is_valid(arr, k, mid):\n # Function to check if the tasks can be divided among workers such that\n # no worker takes more than 'mid' amount of time\n workers = 1... | trial_gpt4o_7 | {
"original_instruction": "",
"prompt_id": "0000010219",
"row_id": 10219,
"seed_ids": "['838', '1552', '207']"
} | 0.728228 | Leetcode_10219 | I | [
"can_assign_tasks_within_max_time",
"minimize_max_time"
] | 2 | [
[
"minimize_max_time",
"can_assign_tasks_within_max_time"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 2915e70cb461891683202c20f8b1013e2d01bd09 | 3727e1d4479a21ccb010c38be2dada48f4dbb6c4 | |||
v1.0 | instruct | Leetcode | Leetcode_10305_I | You are given a list of `n` words, `words`, where each word consists of lowercase English letters. Two words are considered connected if they differ by exactly one character. For example, "bat" and "cat" are connected, while "bat" and "cats" are not. Determine the _largest set of connected words_ in `words`. Return the... | def differ_by_one_char(word1, word2):
"""
Returns True if word1 and word2 differ by exactly one character, otherwise False.
"""
if len(word1) != len(word2):
return False
differences = sum(1 for a, b in zip(word1, word2) if a != b)
return differences == 1
def build_graph(words):
... | from solution import largest_connected_set
def test_single_word():
assert largest_connected_set(["cat"]) == 1
def test_no_connections():
assert largest_connected_set(["cat", "dog", "fish"]) == 1
def test_all_connected():
assert largest_connected_set(["cat", "bat", "rat", "tat"]) == 4
def test_some_conne... | [
{
"docstring": "Returns True if word1 and word2 differ by exactly one character, otherwise False.",
"function_declaration": "def differ_by_one_char(word1, word2):",
"function_name": "differ_by_one_char",
"parameter_list": "(word1, word2)"
},
{
"docstring": "Builds an adjacency list for the w... | [
0,
0,
0,
0,
1,
0,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def differ_by_one_char(word1, word2):\n \"\"\"\n Helper function to check if two words differ by exactly one character.\n \"\"\"\n if len(word1) != len(word2):\n ... | trial_gpt4o_4 | {
"original_instruction": "",
"prompt_id": "0000010305",
"row_id": 10305,
"seed_ids": "['465', '316', '2078']"
} | 0.567015 | Leetcode_10305 | I | [
"differ_by_one_char",
"build_graph",
"dfs",
"largest_connected_set"
] | 4 | [
[
"build_graph",
"differ_by_one_char"
],
[
"largest_connected_set",
"build_graph"
],
[
"largest_connected_set",
"dfs"
]
] | 3 | [] | false | [] | false | pytest | true | [] | true | 62177c6647979286e6caf1f55b73843c3919597b | 2aabd1c76bfdcddb24fe1a382173771e29658a31 | |||
v1.0 | instruct | Leetcode | Leetcode_10519_I | Given a list of non-negative integers representing the amount of time each task takes, and an integer `d` representing the maximum number of days you can work, return _the minimum possible maximum working time for any day_ if you want to schedule the tasks over `d` days to minimize the maximum amount of time spent on a... | def can_split(tasks, d, max_work):
current_sum = 0
required_days = 1
for task in tasks:
if current_sum + task > max_work:
required_days += 1
current_sum = task
if required_days > d:
return False
else:
current_sum += task
ret... | from solution import minimize_max_work_time
def test_example_case_1():
assert minimize_max_work_time([7, 2, 5, 10, 8], 2) == 18
def test_example_case_2():
assert minimize_max_work_time([1, 2, 3, 4, 5], 2) == 9
def test_single_task():
assert minimize_max_work_time([10], 1) == 10
def test_many_days():
... | [
{
"docstring": null,
"function_declaration": "def can_split(tasks, d, max_work):",
"function_name": "can_split",
"parameter_list": "(tasks, d, max_work)"
},
{
"docstring": "Return the minimum possible maximum working time for any day\ngiven the list of tasks and the maximum number of days.",... | [
0,
0,
0,
0,
1,
0,
0,
0,
0,
0
] | 1 | hard | 0.1 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def splitArray(nums, m):\n \"\"\"\n Returns the minimum possible maximum working time for any day when \n tasks are scheduled over m days.\n \"\"\"\n\n def can... | trial_gpt4o_4 | {
"original_instruction": "",
"prompt_id": "0000010519",
"row_id": 10519,
"seed_ids": "['1319', '2176', '5']"
} | 0.643486 | Leetcode_10519 | I | [
"can_split",
"minimize_max_work_time"
] | 2 | [
[
"minimize_max_work_time",
"can_split"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 64be28c3c9022120426128398a1f90c79b3633c3 | df3a23a98d3f369ad307899cdb8791fe2b936b22 | |||
v1.0 | instruct | Leetcode | Leetcode_10581_I | You are given two arrays, `a` and `b`, both containing `n` unique integers. The arrays represent the initial and target arrangement of marbles on a table. You can perform the following operation any number of times:
* Choose any marble from array `a` and relocate it to any valid position in array `b`, provided that th... | def min_operations_to_transform(a, b):
"""
Determines the minimum number of operations required to transform array a into array b.
"""
n = len(a)
position_in_b = {value: idx for idx, value in enumerate(b)}
lis = []
for value in a:
if value in position_in_b:
index_b =... | from solution import min_operations_to_transform
def test_example_case():
a = [1, 3, 2, 4]
b = [1, 2, 3, 4]
assert min_operations_to_transform(a, b) == 1
def test_already_identical():
a = [1, 2, 3, 4]
b = [1, 2, 3, 4]
assert min_operations_to_transform(a, b) == 0
def test_reverse_order():
... | [
{
"docstring": "Determines the minimum number of operations required to transform array a into array b.",
"function_declaration": "def min_operations_to_transform(a, b):",
"function_name": "min_operations_to_transform",
"parameter_list": "(a, b)"
},
{
"docstring": "Helper function to perform... | [
0,
0,
0,
0,
1,
0,
0,
0,
0
] | 1 | hard | 0.111 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def min_operations(a, b):\n \"\"\"\n Determines the minimum number of operations required to transform array `a` into array `b`\n by relocating marbles while preserv... | trial_gpt4o_4 | {
"original_instruction": "",
"prompt_id": "0000010581",
"row_id": 10581,
"seed_ids": "['1655', '2450', '505']"
} | 0.792382 | Leetcode_10581 | I | [
"min_operations_to_transform",
"find_position_to_replace"
] | 2 | [
[
"min_operations_to_transform",
"find_position_to_replace"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 2d9a333a0ac0d38ee7a414dba62cf2c501f6fc56 | 933d6647071a2e93df9829d05fbd5b670dab37e7 | |||
v1.0 | instruct | Leetcode | Leetcode_10609_I | You are given a string `s` and an integer `k`. Return the number of substrings of `s` of length `k` that are palindrome. A string is palindrome if it reads the same forward and backward. Substrings are consecutive segments of the string, so different starting positions of the same palindromic sequence are considered di... | def is_palindrome(s):
"""
Helper function to check if a given string is a palindrome.
"""
return s == s[::-1]
def count_palindromic_substrings(s, k):
"""
Returns the number of palindromic substrings of length k in the string s.
"""
n = len(s)
count = 0
# Iterate through the str... | from solution import count_palindromic_substrings
def test_palindromic_substrings_all_same():
assert count_palindromic_substrings("aaaaa", 2) == 4
assert count_palindromic_substrings("aaaaa", 3) == 3
assert count_palindromic_substrings("aaaaa", 4) == 2
assert count_palindromic_substrings("aaaaa", 5) ==... | [
{
"docstring": "Helper function to check if a given string is a palindrome.",
"function_declaration": "def is_palindrome(s):",
"function_name": "is_palindrome",
"parameter_list": "(s)"
},
{
"docstring": "Returns the number of palindromic substrings of length k in the string s.",
"functio... | [
0,
0,
0,
0,
1,
1,
0,
0,
0,
0
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def count_palindromic_substrings(s, k):\n \"\"\"\n Returns the number of substrings of length k that are palindromes.\n \n Parameters:\n s (str): The input str... | trial_gpt4o_5 | {
"original_instruction": "",
"prompt_id": "0000010609",
"row_id": 10609,
"seed_ids": "['1089', '2328', '878']"
} | 0.793558 | Leetcode_10609 | I | [
"is_palindrome",
"count_palindromic_substrings"
] | 2 | [
[
"count_palindromic_substrings",
"is_palindrome"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | cbed66b138101a5205cb6ba160ca481985bc3680 | 4d1547ac6e5d8e32fb6e32a857995514a7f8cdbe | |||
v1.0 | instruct | Leetcode | Leetcode_10696_I | Given a rectangular `matrix` of integers, you must find a submatrix that has the largest sum among all possible submatrices. A **submatrix** is a contiguous block of cells within the matrix. Return _the sum of the elements in this largest submatrix_. | def max_sum_submatrix(matrix):
"""
This function finds the sum of the submatrix that has the largest sum
within the given matrix.
"""
if not matrix or not matrix[0]:
return 0
rows = len(matrix)
columns = len(matrix[0])
max_sum = float('-inf')
# We use Kadane's algo... | from solution import max_sum_submatrix
def test_empty_matrix():
assert max_sum_submatrix([]) == 0
def test_single_element_matrix():
assert max_sum_submatrix([[5]]) == 5
assert max_sum_submatrix([[-1]]) == -1
def test_all_positive_elements_matrix():
assert max_sum_submatrix([[1,2,3],[4,5,6],[7,8,9]]) ... | [
{
"docstring": "This function finds the sum of the submatrix that has the largest sum \nwithin the given matrix.",
"function_declaration": "def max_sum_submatrix(matrix):",
"function_name": "max_sum_submatrix",
"parameter_list": "(matrix)"
},
{
"docstring": "Helper function to implement Kada... | [
0,
0,
1,
1,
0,
0,
0,
0,
0,
0
] | 2 | hard | 0.2 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def max_submatrix_sum(matrix):\n \"\"\"\n Find the submatrix with the largest sum within the given rectangular matrix.\n Returns the sum of the elements in this subm... | trial_gpt4o_3 | {
"original_instruction": "",
"prompt_id": "0000010696",
"row_id": 10696,
"seed_ids": "['1363', '883', '1140']"
} | 0.635824 | Leetcode_10696 | I | [
"max_sum_submatrix",
"kadane_algorithm"
] | 2 | [
[
"max_sum_submatrix",
"kadane_algorithm"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | fb01ef9d0111d85f370526eb3d2eb11a4e01c05e | c84e80db3dc3fb2420651ec4c54c1e8d2ba5336b | |||
v1.0 | instruct | Leetcode | Leetcode_10739_I | Given a string `s` and an array of words `words`, return _the length of the longest word in_ `words` _that can be constructed from the characters in_ `s`. Each character in `s` can only be used once per word. If no word can be constructed, return 0. | def can_construct_from_chars(word, char_count):
"""
Helper function to check if a word can be constructed from the given characters.
"""
word_count = {}
for char in word:
if char not in word_count:
word_count[char] = 0
word_count[char] += 1
for char, count in wor... | from solution import longest_word_length
def test_longest_word_length_all_words_constructible():
s = "abacadae"
words = ["aa", "ab", "ba", "bad"]
assert longest_word_length(s, words) == 3
def test_longest_word_length_some_words_constructible():
s = "abacadae"
words = ["aa", "abc", "bad", "edc"]
... | [
{
"docstring": "Helper function to check if a word can be constructed from the given characters.",
"function_declaration": "def can_construct_from_chars(word, char_count):",
"function_name": "can_construct_from_chars",
"parameter_list": "(word, char_count)"
},
{
"docstring": "Returns the len... | [
1,
1,
1,
1,
1,
1,
1,
1,
1,
0
] | 9 | easy | 0.9 | {
"trial_gpt4o_0": {
"file_source": "CodeGen_instruction_50000_combined_sanitized_prepared_0_results_combined.jsonl",
"solution_code": "def can_construct(word, s_chars):\n s_chars_copy = s_chars.copy()\n for char in word:\n if char in s_chars_copy:\n s_chars_copy.remove(char)\n ... | trial_gpt4o_8 | {
"original_instruction": "",
"prompt_id": "0000010739",
"row_id": 10739,
"seed_ids": "['383', '2021', '1288']"
} | 0.728832 | Leetcode_10739 | I | [
"can_construct_from_chars",
"longest_word_length"
] | 2 | [
[
"longest_word_length",
"can_construct_from_chars"
]
] | 1 | [] | false | [] | false | pytest | true | [] | true | 9b4e395c017a390ce86af13a0dd1c0700fb346da | 6e74d5fabc95ef206b7f97c639f7fb91209be2a9 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.