# Messed up function needs uncovering!

You spent 5 hours to make a simple function that takes in a number, and it will print out a grid with the given number as each of its dimensions. Each square of the grid will have a single circle in the center.

Satisfied, you save the code in grid.py and go to bed.

Later, you accidentally run your scrambler program (a program that will randomly choose one character in a file, and swap its position with another random character in that file) on your grid.py... three times! Frantically, you run grid.py to see if any damage has been done.

You input 1, the program returns

-----
| O |
-----


You breathe a sigh of relief, and casually run the program again.

You input 2, the program returns

------
| O ||
------
------


You freeze. You expected to see this instead:

---------
| O | O |
---------
| O | O |
---------


Shaking, you try 3.

The program returns

-------
| O |||
-------
-------
-------


You hoped to see this:

-------------
| O | O | O |
-------------
| O | O | O |
-------------
| O | O | O |
-------------


This was the function:

def grid(num):
secret = ...
print('\n'.join(secret))


For you challenge, uncover how secret was defined, and how it's defined after the accident.

• @Bass If you say so :) Oct 22, 2020 at 13:30

Here's one possibility:

(lambda l,O,num: [l,O]*num+[l])("----"*num+"-","| O "*num+"|",num)

Demonstration:

secret_str = """(lambda l,O,num: [l,O]*num+[l])("----"*num+"-","| O "*num+"|",num)"""
accidict = {ord("*"):"+",ord("+"):"*"}

No spoilers in code below.

def grid(num):
secret = eval(secret_str)
print('\n'.join(secret))

grid(1)
# -----
# | O |
# -----
grid(2)
# ---------
# | O | O |
# ---------
# | O | O |
# ---------
grid(3)
# -------------
# | O | O | O |
# -------------
# | O | O | O |
# -------------
# | O | O | O |
# -------------

# accident
secret_str = secret_str.translate(accidict)

grid(1)
# -----
# | O |
# -----
grid(2)
# ------
# | O ||
# ------
# ------
grid(3)
# -------
# | O |||
# -------
# -------
# -------

• Oct 21, 2020 at 22:58
• Thanks @riskymysteries. Good job we don't have to post any real code with that. Oct 21, 2020 at 23:16
• I don't understand your comment, can you explain? Oct 22, 2020 at 1:05
• @riskymysteries I mean it's good enough for very small snippets but a pain in the backside for anything with more than 5 lines or so. Oct 22, 2020 at 1:16