You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Basic for loops and enumerate loops fail when trying to iterate over list literals.
Error Examples
# This currently fails%%thinkpy--explainobjective"Test For Loop"task"loop":
step"simple range loop":
forindex, valueinenumerate([1,2,3]):
print(index, value)
endrun"loop"# This also fails%%thinkpy--explainobjective"Test For Loop"task"loop":
step"simple range loop":
foritemin [1,2,3]:
print(item)
endrun"loop"
Expected Behavior
Both patterns above should work:
Direct iteration over list literals for item in [1,2,3]
Enumeration over list literals for index, value in enumerate([1,2,3])
Test Cases
class TestListLiterals:
def test_basic_list_iteration(self, parser, interpreter):
"""Test simple iteration over list literal"""
code = '''
objective "Test Basic List Iteration"
task "Basic List":
step "Iterate":
sum = 0
for item in [1, 2, 3]:
sum = sum + item
end
run "Basic List"'''
ast = parser.parse(code)
interpreter.execute(ast)
assert interpreter.state["sum"] == 6
def test_enumerate_list_literal(self, parser, interpreter):
"""Test enumeration over list literal"""
code = '''
objective "Test Enumerate List"
task "Enumerate List":
step "Iterate":
indices = []
values = []
for idx, val in enumerate([10, 20, 30]):
indices = indices + [idx]
values = values + [val]
end
run "Enumerate List"'''
ast = parser.parse(code)
interpreter.execute(ast)
assert interpreter.state["indices"] == [0, 1, 2]
assert interpreter.state["values"] == [10, 20, 30]
def test_nested_list_literals(self, parser, interpreter):
"""Test iteration over nested list literals"""
code = '''
objective "Test Nested Lists"
task "Nested Lists":
step "Iterate":
flattened = []
for sublist in [[1, 2], [3, 4]]:
for item in sublist:
flattened = flattened + [item]
end
end
run "Nested Lists"'''
ast = parser.parse(code)
interpreter.execute(ast)
assert interpreter.state["flattened"] == [1, 2, 3, 4]
def test_mixed_variable_and_literal(self, parser, interpreter):
"""Test mixing variable references and list literals in loops"""
code = '''
objective "Test Mixed Variables and Literals"
task "Mixed":
step "Setup":
nums = [1, 2]
step "Iterate":
results = []
for x in nums:
for y in [10, 20]:
results = results + [x * y]
end
end
run "Mixed"'''
ast = parser.parse(code)
interpreter.execute(ast)
assert interpreter.state["results"] == [10, 20, 20, 40]
def test_enumerate_nested_lists(self, parser, interpreter):
"""Test enumeration over nested list literals"""
code = '''
objective "Test Enumerate Nested Lists"
task "Enumerate Nested":
step "Iterate":
positions = []
sums = []
for pos, pair in enumerate([[1, 2], [3, 4]]):
positions = positions + [pos]
sums = sums + [pair[0] + pair[1]]
end
run "Enumerate Nested"'''
ast = parser.parse(code)
interpreter.execute(ast)
assert interpreter.state["positions"] == [0, 1]
assert interpreter.state["sums"] == [3, 7]
The text was updated successfully, but these errors were encountered:
Current Behavior
Basic for loops and enumerate loops fail when trying to iterate over list literals.
Error Examples
Expected Behavior
Both patterns above should work:
for item in [1,2,3]
for index, value in enumerate([1,2,3])
Test Cases
The text was updated successfully, but these errors were encountered: