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
When I try to shorten this code with all options enabled it only removes the spaces.
equation = input().split("=")[1]
grid = [["."]*10 for i in range(10)]
for y in range(10):
x = int(eval(equation))
if 0 <= x <= 9:
grid[y][x] = "o"
print('\n'.join("".join(j for j in i) for i in grid))
Result: Reduction from 217 to 192
equation=input().split('=')[1]
grid=[['.']*10 for i in range(10)]
for y in range(10):
x=int(eval(equation))
if 0<=x<=9:grid[y][x]='o'
print('\n'.join((''.join((j for j in i))for i in grid)))
When the eval() is removed from the code it gets shortened properly (203 to 162)
D=range
E=input().split('=')[1]
A=[['.']*10 for A in D(10)]
for C in D(10):
B=int()
if 0<=B<=9:A[C][B]='o'
print('\n'.join((''.join((A for A in B))for B in A)))
The text was updated successfully, but these errors were encountered:
eval() executes a python expression using the local and global variables in it's current scope. We can't rename those without renaming references to them in the expression, which is not generally possible as we don't know what the expression will be.
You could technically do it if the expression was a static string, but you wouldn't need to eval it in that case.
I've thought about this some more, and I think there are improvements that could be made.
For usage of eval() (and var(), locals(), globals(), and maybe exec()), they only need to prevent renaming in their own local and global namespace. If a local or global mapping is provided for var() or eval() (and exec()?) then they don't need to prevent renaming at all.
This wouldn't help for your short example, but I expect it would make a difference in most situations.
When I try to shorten this code with all options enabled it only removes the spaces.
Result: Reduction from 217 to 192
When the eval() is removed from the code it gets shortened properly (203 to 162)
The text was updated successfully, but these errors were encountered: