2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ @Bass If you say so :) $\endgroup$ Oct 22 '20 at 13:30
4
$\begingroup$

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 |||
# -------
# -------
# -------
$\endgroup$
4
  • $\begingroup$ Here meta.stackexchange.com/a/105736/773001 $\endgroup$ Oct 21 '20 at 22:58
  • $\begingroup$ Thanks @riskymysteries. Good job we don't have to post any real code with that. $\endgroup$ Oct 21 '20 at 23:16
  • $\begingroup$ I don't understand your comment, can you explain? $\endgroup$ Oct 22 '20 at 1:05
  • 1
    $\begingroup$ @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. $\endgroup$ Oct 22 '20 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.