2
$\begingroup$

Here's a puzzle for all you python programmers out there:

I defined a function that goes like this:

def checkers(num):
    magic = [_____________________________________________________________]
    print('\n'.join(magic + magic[num-2::-1]))

Here are the results from calling the function with different numbers:


checkers(1)

Output:

_|
_|

checkers(2)

Output:

  _|  
_|_|_|
  _|  

checkers(3)

Output:

    _|    
  _|_|_|  
_|_|_|_|_|
  _|_|_|  
    _|    

checkers(4)

Output:

      _|      
    _|_|_|    
  _|_|_|_|_|  
_|_|_|_|_|_|_|
  _|_|_|_|_|  
    _|_|_|    
      _|      

checkers(5)

Output:

        _|        
      _|_|_|      
    _|_|_|_|_|    
  _|_|_|_|_|_|_|  
_|_|_|_|_|_|_|_|_|
  _|_|_|_|_|_|_|  
    _|_|_|_|_|    
      _|_|_|      
        _|        

I hope you get the gist as to what the function does, but if not:
Whatever number we pass into the brackets, it will print out a checker board rotated 45 degrees, with the number we passed in as each of its dimensions.

For your challenge, find out how magic is defined.

You don't have to define it the same way I did, but just for a little hint on one way to define it, each underscore between the two square-brackets represents a character, with no unnecessary whitespaces (I do love PEP-8, don't get me wrong).

$\endgroup$
4
  • $\begingroup$ Are the trailing whitespaces on each row significant? $\endgroup$ – Bubbler Oct 21 '20 at 1:34
  • 1
    $\begingroup$ The 1x1 checkerboard usually has fewer than two squares though. $\endgroup$ – Bass Oct 21 '20 at 4:06
  • $\begingroup$ @Bubbler The trailing spaces were conclusive, but I guess I accidentally removed some from the last one. I'll add them in. $\endgroup$ – risky mysteries Oct 21 '20 at 15:32
  • $\begingroup$ @Bass It is one square if you look at it sideways and squint really hard. $\endgroup$ – risky mysteries Oct 21 '20 at 17:04
4
$\begingroup$

From a fellow abuser of listcomps, magic is

" "*(num-i-1)+"_|"*(2*i+1)+" "*(num-i-1)for i in range(num)
It's a list of the rows in the upper half.

The hint is

magic(1) not being square: this occurs because num-2 is -1 and so what would be all but the last row in reverse order becomes the last (and only) row

$\endgroup$
5
$\begingroup$

49 bytes, respecting the spaces on the right side:

('_|'*(2*i+1)).center(num*4-2)for i in range(num)
Try it online!

That said, it isn't a very interesting puzzle since string formatting tasks are common in regular programming, except the bit of inferring what the magic should evaluate to. I hope you come up with more interesting ones later :)

$\endgroup$
0

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.