Untitled
unknown
plain_text
10 months ago
6.4 kB
22
Indexable
#
# ⚙️ Problem: Gear Train Speed Calculation
#
# --- Problem Statement ---
#
# You are tasked with analyzing a complex gear system. The system is composed of
# several gears connected in a network. When one gear turns, it causes the gears
# it's connected to to turn as well, but at a different speed determined by the
# gear ratio.
#
# You are given a list of connections between gears, the initial speed of a
# single input gear, and the names of the input and output gears. Your goal is
# to calculate the final speed of the output gear.
#
# The connections are given as a list of tuples `(gear_A, gear_B, ratio)`,
# which means that **Gear B rotates `ratio` times as fast as Gear A**. Note that
# this relationship is bidirectional: if Gear B's speed is `ratio` * Gear A's
# speed, then Gear A's speed is `(1 / ratio)` * Gear B's speed.
#
# If the output gear is not connected to the input gear through any sequence
# of connections, it will not turn.
#
# --- Task ---
#
# Write a function `calculate_output_speed` that takes the gear connections,
# an initial speed, an input gear, and an output gear, and returns the speed of
# the output gear. If there is no path of connections from the input to the
# output gear, return -1.0.
#
# --- Example ---
#
# Input:
# connections = [('A', 'B', 2.0), ('B', 'C', 1.5)]
# initial_speed = 100.0
# input_gear = 'A'
# output_gear = 'C'
#
# Expected Output: 300.0
#
# --- Constraints ---
#
# - The number of connections will be between 0 and 500.
# - Gear names are strings containing uppercase letters.
# - `initial_speed` and all `ratio` values are positive floating-point numbers.
# - The `input_gear` and `output_gear` might not be present in the connections list.
#
from collections import defaultdict, deque
from typing import List, Tuple
def calculate_output_speed(connections: List[Tuple[str, str, float]], initial_speed: float, input_gear: str, output_gear: str) -> float:
"""
Calculates the speed of an output gear in a gear train.
Args:
connections: A list of tuples, where each tuple is (gear_A, gear_B, ratio).
initial_speed: The rotational speed (RPM) of the input gear.
input_gear: The name of the gear connected to the motor.
output_gear: The name of the final gear whose speed needs to be calculated.
Returns:
The calculated speed of the output gear, or -1.0 if it's not connected.
"""
# ==========================================
# --- START of your implementation area ---
pass # Replace this with your code
# --- END of your implementation area ---
# ========================================
# =======================================================================
# ================= !!! DO NOT EDIT BELOW THIS LINE !!! =================
# =======================================================================
if __name__ == '__main__':
test_cases = [
# Test Case 1: Provided example (A -> B -> C)
{
"name": "Simple Multi-Step Path",
"connections": [('A', 'B', 2.0), ('B', 'C', 1.5)],
"initial_speed": 100.0,
"input_gear": 'A',
"output_gear": 'C',
"expected": 300.0
},
# Test Case 2: Direct connection
{
"name": "Direct Connection",
"connections": [('MOTOR', 'WHEEL', 0.25)],
"initial_speed": 1000.0,
"input_gear": 'MOTOR',
"output_gear": 'WHEEL',
"expected": 250.0
},
# Test Case 3: Reverse path
{
"name": "Reverse Path",
"connections": [('A', 'B', 2.0)],
"initial_speed": 200.0,
"input_gear": 'B',
"output_gear": 'A',
"expected": 100.0 # 200 * (1 / 2.0)
},
# Test Case 4: No path exists
{
"name": "No Connection Path",
"connections": [('A', 'B', 2.0), ('C', 'D', 3.0)],
"initial_speed": 100.0,
"input_gear": 'A',
"output_gear": 'D',
"expected": -1.0
},
# Test Case 5: Same input and output gear
{
"name": "Same Input and Output Gear",
"connections": [('A', 'B', 2.0)],
"initial_speed": 50.0,
"input_gear": 'A',
"output_gear": 'A',
"expected": 50.0
},
# Test Case 6: More complex path with dead ends
{
"name": "Complex Path with Branches",
"connections": [
('A', 'B', 0.5), ('B', 'C', 4.0), ('A', 'D', 3.0),
('D', 'E', 0.2), ('C', 'F', 2.5)
],
"initial_speed": 10.0,
"input_gear": 'A',
"output_gear": 'F',
"expected": 50.0 # 10 * 0.5 * 4.0 * 2.5
},
# Test Case 7: Path through a cycle
{
"name": "Path Involving a Cycle",
"connections": [
('A', 'B', 2.0), ('B', 'C', 2.0), ('C', 'A', 0.25), ('C', 'D', 3.0)
],
"initial_speed": 10.0,
"input_gear": 'A',
"output_gear": 'D',
"expected": 120.0 # 10 * 2.0 * 2.0 * 3.0
},
# Test Case 8: Input gear does not exist
{
"name": "Non-existent Input Gear",
"connections": [('A', 'B', 2.0)],
"initial_speed": 100.0,
"input_gear": 'X',
"output_gear": 'B',
"expected": -1.0
}
]
for i, tc in enumerate(test_cases):
result = calculate_output_speed(
tc["connections"], tc["initial_speed"],
tc["input_gear"], tc["output_gear"]
)
# Check if result is a float for comparison
if not isinstance(result, float):
print(f"Test Case {i+1}: {tc['name']}... FAILED ❌")
print(f" -> Expected a float, but got {type(result)}")
continue
# Using a small tolerance for float comparison
is_correct = abs(result - tc['expected']) < 1e-5
if is_correct:
print(f"Test Case {i+1}: {tc['name']}... PASSED ✅")
else:
print(f"Test Case {i+1}: {tc['name']}... FAILED ❌")
print(f" -> Expected: {tc['expected']:.2f}, Got: {result:.2f}")
print("-" * 40)Editor is loading...
Leave a Comment