Here's a challenge — try to solve the following 5 Python programming puzzles WITHOUT googling for the answer.
1) Magic Square
A magic square is a 3x3 grid containing the numbers 1 to 9. Every 3 consecutive numbers (row, column or diagonal) must add up to 15. Here's a valid magic square
[ [2, 7, 6],
[9, 5, 1],
[4, 3, 8] ]
In this puzzle, you are given an incomplete magic square.
[ [2, 0, 0],
[0, 0, 0],
[0, 3, 8] ]
Magic squares are not supposed to have 0's. Here, a 0 means that you need to fill it in with a number between 1 and 9.
- Each number from 1 to 9 can only appear once
- Every row, column and diagonal of 3 numbers MUST add up to 15
def solve(incomplete_square):
# CODE HERE
square = [
[2, 0, 0],
[0, 0, 0],
[0, 3, 8]
]
solve(square)
# [ [2, 7, 6],
# [9, 5, 1],
# [4, 3, 8] ]
2) Uppercasing A Screwed Up Dictionary
You are given a messy screwed up dictionary with multiple unstructured layers of nesting. For instance:
d = {
"a": {"b":"c"},
"d": {
"e":"f",
"g": {
"h":"i",
"j":"k",
"l":"m"
},
"n": {
"o": {
"p": {
"q": {"r":"s"}
}
}
}
}
}
Write a function that takes in this screwed up dictionary, and returns a COPY of this dictionary where all keys and values are converted into uppercase.
d = {
"A": {"B":"C"},
"D": {
"E":"F",
"G": {
"H":"I",
"J":"K",
"L":"M"
},
"N": {
"O": {
"P": {
"Q": {"R":"S"}
}
}
}
}
}
3) Dictionary From String
You are given a string representing a dictionary.
string = '{"name":"rocky", "age":5, "imported":True}'
Assume that key-value pairs are simple, and there are no nested lists, dictionaries and other collections. Values will simply be either numbers (in/float), strings, or booleans.
Without using the json
library, the exec
or eval
functions, or any other built-in parsers, write a function that takes in this string, and returns an actual dictionary represented by the string.
4) Shortest Path In Maze
You are given a list of strings representing a maze.
maze = [
"P#----",
"-#-##-",
"------",
"-#-##-",
"-#-#--",
"---#-X",
P
represents the player-
represents an empty space. The player can stand on the empty space#
represents a wall. The player cannot stand on a wallX
represents the objective. The player wins if he reaches the objective.
Write a function that takes in this maze, and returns the shortest path from the player to the objective. The player can only move one step at a time, and in only 4 directions — upwards, downwards, leftwards and rightwards. A sample of what the function returns:
["down", "down", "right", "right", "right", "right", "right", "down", "down", "down"]
5) Screwed Up Dictionary To Screwed Up List
Once again, you are given a messy screwed up dictionary with multiple levels of nesting.
d = {
"a": "b",
"c": "d",
"e": {
"f": "g",
"h": "i"
},
"j": {
"k": {
"l": {"m":"n"}
}
}
}
Write a function to convert every single nested dictionary inside into a list. An example of the output:
d = [
["a", "b"],
["c", "d"],
["e", [
["f", "g"],
["h", "i"]
]],
["j", [
"k", [
"l", ["m", "n"]
]
]]
]
Conclusion
How many did you manage to solve? And how long did it take you?
Some Final words
If this article provided value and you wish to support me, do consider signing up for a Medium membership — It's $5 a month, and you get unlimited access to articles on Medium. If you sign up using my link below, I'll earn a tiny commission at zero additional cost to you.
Sign up using my link here to read unlimited Medium articles.
Get my free Ebooks: https://zlliu.co/books
I write programming articles (mainly Python) that would have probably helped the younger me a lot. Do join my email list to get notified whenever I publish.
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.