qid
stringlengths 2
5
| ground_truth_solution
stringlengths 249
2.65k
| image_description
stringlengths 136
2.27k
| test_script
stringlengths 444
3.15k
| function_signature
stringlengths 218
961
| image
imagewidth (px) 596
1.02k
|
---|---|---|---|---|---|
q7-2 | from typing import Tuple
import math
def solution(line1: Tuple[int, int], line2: Tuple[int, int]) -> bool:
"""
Determines if two line segments ultimately intersect.
Parameters:
line1: A tuple containing the numbers at both ends of the first line segment.
line2: A tuple containing the numbers at both ends of the second line segment.
Returns:
bool: True if the line segments intersect, False otherwise.
"""
mapping = {
1: (math.cos(math.radians(45)), math.sin(math.radians(45))),
2: (math.cos(math.radians(45)), -math.sin(math.radians(45))),
3: (1, 0),
4: (-math.cos(math.radians(45)), -math.sin(math.radians(45))),
5: (0, -1),
6: (-math.cos(math.radians(45)), math.sin(math.radians(45))),
7: (-1, 0),
8: (0, 1)
}
def calculate_vector(p1, p2):
x1, y1 = p1
x2, y2 = p2
return (x2 - x1, y2 - y1)
def are_parallel(line1, line2):
v1 = calculate_vector(*line1)
v2 = calculate_vector(*line2)
return round(v1[0] * v2[1], 2) == round(v1[1] * v2[0], 2)
line1 = tuple(mapping[x] for x in line1)
line2 = tuple(mapping[x] for x in line2)
return not are_parallel(line1, line2)
| This image shows a circle with numbers 1 to 8 marked, representing 8 evenly spaced points. The numbers, arranged clockwise, are 8, 1, 3, 2, 5, 4, 7, 6. Two lines connect different points on the circumference:
- The blue line connects point 7 and point 4.
- The orange line connects point 3 and point 5.
These two lines eventually intersect outside the circle. | def test():
test_cases = [
((3, 7), (1, 5), True),
((2, 8), (1, 3), False),
((2, 8), (5, 8), True),
((3, 6), (1, 8), False),
((1, 4), (5, 8), True),
((1, 6), (2, 8), True),
((3, 7), (2, 4), False),
((1, 7), (3, 2), True),
((1, 8), (3, 2), True),
((1, 6), (3, 8), True),
((1, 7), (4, 3), False),
((4, 5), (1, 8), False),
((2, 7), (3, 4), True),
((1, 3), (6, 7), True),
((7, 8), (4, 1), False)
]
for line1, line2, expected in test_cases:
result = solution(line1, line2)
assert result == expected, f"Assert Error: Test failed for lines {line1} and {line2}. Expected {expected}, got {result}"
test() | from typing import Tuple
def solution(line1: Tuple[int, int], line2: Tuple[int, int]) -> bool:
"""
Determines if two line segments ultimately intersect.
Parameters:
line1: A tuple containing the numbers at both ends of the first line segment.
line2: A tuple containing the numbers at both ends of the second line segment.
Returns:
bool: True if the line segments intersect, False otherwise.
"""
| |
q7-3 | from typing import Tuple
def solution(line1: Tuple[int, int], line2: Tuple[int, int]) -> bool:
"""
Determines if two line segments intersect within the circle.
Parameters:
line1: A tuple containing the numbers at both ends of the first line segment.
line2: A tuple containing the numbers at both ends of the second line segment.
Returns:
bool: True if the line segments intersect, False otherwise.
"""
mapping = {
1: 1,
2: 5,
3: 7,
4: 3,
5: 8,
6: 2,
7: 6,
8: 4
}
line1 = tuple(mapping[x] for x in line1)
line2 = tuple(mapping[x] for x in line2)
a, b = sorted(line1)
c, d = sorted(line2)
# Check if the points (a, b) and (c, d) intersect on the circle
return (a < c < b < d) or (c < a < d < b)
| This image shows a circle with numbers 1 to 8 marked, representing 8 evenly spaced points. The numbers, arranged clockwise, are 5, 1, 6, 4, 8, 2, 7, 3. Two diagonal line segments connect different points on the circumference:
- The blue line connects point 5 and point 4.
- The orange line connects point 1 and point 8.
These lines intersect inside the circle. | def test():
test_cases = [
((3, 7), (1, 5), False),
((2, 8), (1, 3), False),
((2, 8), (5, 8), False),
((3, 6), (1, 8), True),
((1, 4), (5, 8), False),
((1, 6), (2, 8), False),
((3, 7), (2, 4), False),
((1, 7), (3, 2), True),
((1, 8), (3, 2), False),
((1, 2), (3, 4), True),
((5, 8), (6, 7), True),
((5, 7), (3, 6), True)
]
for line1, line2, expected in test_cases:
result = solution(line1, line2)
assert result == expected, f"Assert Error: Test failed for lines {line1} and {line2}. Expected {expected}, got {result}"
test()
| from typing import Tuple
def solution(line1: Tuple[int, int], line2: Tuple[int, int]) -> bool:
"""
Determines if two line segments intersect within the circle.
Parameters:
line1: A tuple containing the numbers at both ends of the first line segment.
line2: A tuple containing the numbers at both ends of the second line segment.
Returns:
bool: True if the line segments intersect, False otherwise.
"""
| |
q8 | def solution(passwd: str) -> int:
"""
Calculate the total angle needed to rotate the lock to unlock it. The lock can only rotate in one direction.
Args:
passwd (str): A string representing the passwd to unlock the lock, consisting of a sequence of
characters 'a', 'b', 'c', and 'd'. The passwd does not contain repeated adjacent characters.
Returns:
int: The total angle needed to spin the lock to unlock it, following the given passwd.
"""
order = ['d', 'c', 'b', 'a']
angle_per_step = 90 # 360 degrees divided by 4 positions
original_angle = [45, 45+90, 45+180, 45+270]
# Initialize total angle
total_angle = original_angle[order.index(passwd[0])]
# Start from the first character in the passwd
for i in range(1, len(passwd)):
# Find the current and previous positions
current_position = order.index(passwd[i])
previous_position = order.index(passwd[i-1])
# Calculate the angle difference in clockwise direction
steps = (current_position - previous_position) % 4
total_angle += steps * angle_per_step
return total_angle
| This image illustrates a process of generating a password by rotating in a single direction from the initial position 0.
The left image shows the initial state, where the password box is empty (''). The circle contains four characters, and the pointer is at the initial position 0. The angles of a, b, c, and d relative to the initial position are 315°, 225°, 135°, and 45°, respectively.
To generate the password 'ad', the following steps were taken:
- First, starting from position 0, the pointer rotates 315° clockwise, pointing to the character a, which is then added to the password, updating it to 'a'.
- Next, the pointer rotates another 90° clockwise, moving from a to d. The character d is added to the password, resulting in the final password 'ad'. | def test():
test_cases = [
('d', 45),
('b', 225),
('cb', 225),
('ad', 405),
('ac', 495),
('aca', 675),
('adba', 675),
('abcd', 1125),
('abcabcd', 1845),
('acbdacbd', 1485)
]
for passwd, expected in test_cases:
result = solution(passwd)
assert result == expected, f"Assert Error: Test failed for '{passwd}', expected {expected} but got {result}"
test() | def solution(passwd: str) -> int:
"""
Calculate the total angle needed to rotate the lock to unlock it. The lock can only rotate in one direction.
Args:
passwd (str): A string representing the passwd to unlock the lock, consisting of a sequence of
characters 'a', 'b', 'c', and 'd'. The passwd does not contain repeated adjacent characters.
Returns:
int: The total angle needed to spin the lock to unlock it, following the given passwd.
"""
| |
q8-2 | def solution(passwd: str) -> int:
"""
Calculate the total angle needed to rotate the lock to unlock it. The lock can only rotate in one direction.
Args:
passwd (str): A string representing the passwd to unlock the lock, consisting of a sequence of
characters 'a', 'b', 'c', and 'd'. The passwd does not contain repeated adjacent characters.
Returns:
int: The total angle needed to spin the lock to unlock it, following the given passwd.
"""
order = ['a', 'b', 'c', 'd']
angle_per_step = 90 # 360 degrees divided by 4 positions
original_angle = [45, 45+90, 45+180, 45+270]
# Initialize total angle
total_angle = original_angle[order.index(passwd[0])]
# Start from the first character in the passwd
for i in range(1, len(passwd)):
# Find the current and previous positions
current_position = order.index(passwd[i])
previous_position = order.index(passwd[i-1])
# Calculate the angle difference in clockwise direction
steps = (current_position - previous_position) % 4
total_angle += steps * angle_per_step
return total_angle | This image illustrates a process of generating a password by rotating in a single direction from the initial position 0.
The left image shows the initial state, where the password box is empty (''). The circle contains four characters, and the pointer is at the initial position 0. The angles of a, b, c, and d relative to the initial position are 45°, 135°, 225°, and 315°, respectively.
To generate the password 'ad', the following steps were taken:
- First, starting from position 0, the pointer rotates 45° counter-clockwise, pointing to the character a, which is then added to the password, updating it to 'a'.
- Next, the pointer rotates another 270° counter-clockwise, moving from a to d. The character d is added to the password, resulting in the final password 'ad'. | def test():
test_cases = [
('a', 45),
('ab', 135),
('bc', 225),
('d', 315),
('b', 135),
('cb', 495),
('ad', 315),
('ac', 225),
('aca', 405),
('adba', 765),
('abcd', 315),
('abcabcd', 675),
('acbdacbd', 1395)
]
for passwd, expected in test_cases:
result = solution(passwd)
assert result == expected, f"Assert Error: Test failed for '{passwd}', expected {expected} but got {result}"
test() | def solution(passwd: str) -> int:
"""
Calculate the total angle needed to rotate the lock to unlock it. The lock can only rotate in one direction.
Args:
passwd (str): A string representing the passwd to unlock the lock, consisting of a sequence of
characters 'a', 'b', 'c', and 'd'. The passwd does not contain repeated adjacent characters.
Returns:
int: The total angle needed to spin the lock to unlock it, following the given passwd.
"""
| |
q8-3 | def solution(passwd: str) -> int:
"""
Calculate the total angle needed to rotate the lock to unlock it. The lock can only rotate in one direction.
Args:
passwd (str): A string representing the passwd to unlock the lock, consisting of a sequence of
characters 'a', 'b', 'c', and 'd'. The passwd does not contain repeated adjacent characters.
Returns:
int: The total angle needed to spin the lock to unlock it, following the given passwd.
"""
order = ['b', 'a', 'd', 'c']
angle_per_step = 90 # 360 degrees divided by 4 positions
original_angle = [45, 45+90, 45+180, 45+270]
# Initialize total angle
total_angle = original_angle[order.index(passwd[0])]
# Start from the first character in the passwd
for i in range(1, len(passwd)):
# Find the current and previous positions
current_position = order.index(passwd[i])
previous_position = order.index(passwd[i-1])
# Calculate the angle difference in clockwise direction
steps = (current_position - previous_position) % 4
total_angle += steps * angle_per_step
return total_angle
| This image illustrates a process of generating a password by rotating in a single direction from the initial position 0.
The left image shows the initial state, where the password box is empty (''). The circle contains four characters, and the pointer is at the initial position 0. The angles of b, a, d, and c relative to the initial position are 45°, 135°, 225°, and 315°, respectively.
To generate the password 'cb', the following steps were taken:
- First, starting from position 0, the pointer rotates 315° clockwise, pointing to the character c, which is then added to the password, updating it to 'c'.
- Next, the pointer rotates another 90° clockwise, moving from c to b. The character b is added to the password, resulting in the final password 'cb'. | def test():
test_cases = [
('b', 45),
('d', 225),
('ad', 225),
('cb', 405),
('ca', 495),
('cac', 675),
('cbdc', 675),
('cdab', 1125),
('cdacdab', 1845),
('cadbcadb', 1485)
]
for passwd, expected in test_cases:
result = solution(passwd)
assert result == expected, f"Assert Error: Test failed for '{passwd}', expected {expected} but got {result}"
test() | def solution(passwd: str) -> int:
"""
Calculate the total angle needed to rotate the lock to unlock it. The lock can only rotate in one direction.
Args:
passwd (str): A string representing the passwd to unlock the lock, consisting of a sequence of
characters 'a', 'b', 'c', and 'd'. The passwd does not contain repeated adjacent characters.
Returns:
int: The total angle needed to spin the lock to unlock it, following the given passwd.
"""
| |
q9 | def solution(matrix: list[list[int]]) -> list[int]:
"""
Traverses a given matrix according to the snake pattern shown in the figure.
Parameters:
matrix (list[list[int]]): A 2D list (N x N matrix) containing integers that need to be traversed.
Returns:
list[int]: A list of integers representing the traversed elements.
"""
result = []
N = len(matrix)
for col in range(N):
if col % 2 == 0:
for row in range(N):
result.append(matrix[row][col])
else:
for row in range(N-1, -1, -1):
result.append(matrix[row][col])
return result
| This image shows the traversal process of a two-dimensional matrix, with the traversal path indicated by arrows.
The matrix is:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
The specific sequence is as follows:
First, start from the top-left corner and traverse from 1 to 7, then traverse from 8 to 2, and finally traverse from 3 to 9.
This pattern continues until the entire matrix is traversed. | def test():
test_cases = [
([[1, 2], [3, 4]], [1, 3, 4, 2]),
([[9, 9, 9], [9, 9, 9], [9, 9, 9]], [9, 9, 9, 9, 9, 9, 9, 9, 9]),
([[99, 102], [672, 5]], [99, 672, 5, 102]),
([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [1, 4, 7, 8, 5, 2, 3, 6, 9]),
([[63, 22, 17], [98, 21, 3], [76, 33, 9]], [63, 98, 76, 33, 21, 22, 17, 3, 9]),
([[53, 54, 55], [56, 57, 58], [59, 60, 61]], [53, 56, 59, 60, 57, 54, 55, 58, 61]),
([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], [1, 5, 9, 13, 14, 10, 6, 2, 3, 7, 11, 15, 16, 12, 8, 4]),
([[54, 39, 74, 11], [8, 43, 13, 27], [73, 18, 45, 9], [63, 67, 30, 40]], [54, 8, 73, 63, 67, 18, 43, 39, 74, 13, 45, 30, 40, 9, 27, 11]),
([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]],
[1, 6, 11, 16, 21, 22, 17, 12, 7, 2, 3, 8, 13, 18, 23, 24, 19, 14, 9, 4, 5, 10, 15, 20, 25]),
([[21, 23, 98, 76, 33], [63, 67, 30, 40, 5], [73, 18, 45, 9, 2], [8, 43, 13, 27, 1], [98, 77, 23, 11, 9]],
[21, 63, 73, 8, 98, 77, 43, 18, 67, 23, 98, 30, 45, 13, 23, 11, 27, 9, 40, 76, 33, 5, 2, 1, 9]),
]
for i, (matrix, expected) in enumerate(test_cases):
result = solution(matrix)
assert result == expected, f"Assert Error: Input {matrix}, Expected {expected}, Got: {result}"
test() | def solution(matrix: list[list[int]]) -> list[int]:
"""
Traverses a given matrix according to the snake pattern shown in the figure.
Parameters:
matrix (list[list[int]]): A 2D list (N x N matrix) containing integers that need to be traversed.
Returns:
list[int]: A list of integers representing the traversed elements.
"""
| |
q9-2 | def solution(matrix: list[list[int]]) -> list[int]:
"""
Traverses a given matrix according to the snake pattern shown in the figure.
Parameters:
matrix (list[list[int]]): A 2D list (N x N matrix) containing integers that need to be traversed.
Returns:
list[int]: A list of integers representing the traversed elements.
"""
result = []
N = len(matrix)
i = 0
for col in range(N-1, -1, -1):
if i % 2 == 0:
for row in range(N-1, -1, -1):
result.append(matrix[row][col])
else:
for row in range(N):
result.append(matrix[row][col])
i += 1
return result
| This image shows the traversal process of a two-dimensional matrix, with the traversal path indicated by arrows.
The matrix is:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
The specific sequence is as follows:
First, start from the bottom-right corner and traverse up from 9 to 3, then traverse up from 2 to 8, and finally traverse down from 7 to 1.
This pattern continues, alternating directions, until the entire matrix is traversed. | def test():
test_cases = [
([[1, 2], [3, 4]], [4, 2, 1, 3]),
([[9, 9, 9], [9, 9, 9], [9, 9, 9]], [9, 9, 9, 9, 9, 9, 9, 9, 9]),
([[99, 102], [672, 5]], [5, 102, 99, 672]),
([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [9, 6, 3, 2, 5, 8, 7, 4, 1]),
([[63, 22, 17], [98, 21, 3], [76, 33, 9]], [9, 3, 17, 22, 21, 33, 76, 98, 63]),
([[53, 54, 55], [56, 57, 58], [59, 60, 61]], [61, 58, 55, 54, 57, 60, 59, 56, 53]),
([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], [16, 12, 8, 4, 3, 7, 11, 15, 14, 10, 6, 2, 1, 5, 9, 13]),
([[54, 39, 74, 11], [8, 43, 13, 27], [73, 18, 45, 9], [63, 67, 30, 40]], [40, 9, 27, 11, 74, 13, 45, 30, 67, 18, 43, 39, 54, 8, 73, 63]),
([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]],
[25, 20, 15, 10, 5, 4, 9, 14, 19, 24, 23, 18, 13, 8, 3, 2, 7, 12, 17, 22, 21, 16, 11, 6, 1]),
([[21, 23, 98, 76, 33], [63, 67, 30, 40, 5], [73, 18, 45, 9, 2], [8, 43, 13, 27, 1], [98, 77, 23, 11, 9]],
[9, 1, 2, 5, 33, 76, 40, 9, 27, 11, 23, 13, 45, 30, 98, 23, 67, 18, 43, 77, 98, 8, 73, 63, 21]),
]
for i, (matrix, expected) in enumerate(test_cases):
result = solution(matrix)
assert result == expected, f"Assert Error: Input {matrix}, Expected {expected}, Got: {result}"
test() | def solution(matrix: list[list[int]]) -> list[int]:
"""
Traverses a given matrix according to the snake pattern shown in the figure.
Parameters:
matrix (list[list[int]]): A 2D list (N x N matrix) containing integers that need to be traversed.
Returns:
list[int]: A list of integers representing the traversed elements.
"""
| |
q9-3 | def solution(matrix: list[list[int]]) -> list[int]:
"""
Traverses a given matrix according to the snake pattern shown in the figure.
Parameters:
matrix (list[list[int]]): A 2D list (N x N matrix) containing integers that need to be traversed.
Returns:
list[int]: A list of integers representing the traversed elements.
"""
result = []
N = len(matrix)
i = 0
for row in range(N-1, -1, -1):
if i % 2 == 0:
for col in range(N-1, -1, -1):
result.append(matrix[row][col])
else:
for col in range(N):
result.append(matrix[row][col])
i += 1
return result
| This image shows the traversal process of a two-dimensional matrix, with the traversal path indicated by arrows.
The matrix is:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
The specific sequence is as follows:
First, start from the bottom-right corner and traverse left from 9 to 7, then traverse right from 4 to 6, and finally traverse left from 3 to 1.
This pattern continues, until the entire matrix is traversed. | def test():
test_cases = [
([[1, 2], [3, 4]], [4, 3, 1, 2]),
([[9, 9, 9], [9, 9, 9], [9, 9, 9]], [9, 9, 9, 9, 9, 9, 9, 9, 9]),
([[99, 102], [672, 5]], [5, 672, 99, 102]),
([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [9, 8, 7, 4, 5, 6, 3, 2, 1]),
([[63, 22, 17], [98, 21, 3], [76, 33, 9]], [9, 33, 76, 98, 21, 3, 17, 22, 63]),
([[53, 54, 55], [56, 57, 58], [59, 60, 61]], [61, 60, 59, 56, 57, 58, 55, 54, 53]),
([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], [16, 15, 14, 13, 9, 10, 11, 12, 8, 7, 6, 5, 1, 2, 3, 4]),
([[54, 39, 74, 11], [8, 43, 13, 27], [73, 18, 45, 9], [63, 67, 30, 40]], [40, 30, 67, 63, 73, 18, 45, 9, 27, 13, 43, 8, 54, 39, 74, 11]),
([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]],
[25, 24, 23, 22, 21, 16, 17, 18, 19, 20, 15, 14, 13, 12, 11, 6, 7, 8, 9, 10, 5, 4, 3, 2, 1]),
([[21, 23, 98, 76, 33], [63, 67, 30, 40, 5], [73, 18, 45, 9, 2], [8, 43, 13, 27, 1], [98, 77, 23, 11, 9]],
[9, 11, 23, 77, 98, 8, 43, 13, 27, 1, 2, 9, 45, 18, 73, 63, 67, 30, 40, 5, 33, 76, 98, 23, 21]),
]
for i, (matrix, expected) in enumerate(test_cases):
result = solution(matrix)
assert result == expected, f"Assert Error: Input {matrix}, Expected {expected}, Got: {result}"
test() | def solution(matrix: list[list[int]]) -> list[int]:
"""
Traverses a given matrix according to the snake pattern shown in the figure.
Parameters:
matrix (list[list[int]]): A 2D list (N x N matrix) containing integers that need to be traversed.
Returns:
list[int]: A list of integers representing the traversed elements.
"""
|