View Post

10 Best Python Puzzles to Discover Your True Skill Level

Do you belong to the top 50% of Python coders? This article poses you 10 intermediate-level Python puzzles to test yourself. By solving all 10 Python puzzles and tracking your success rate, you will discover your exact Python skill level. How to do this? The idea of solving rated Python puzzles to measure your skill level is inspired by the …

[Basics Computer Science] A Recursive Pathfinder Algorithm in Python

def find_path(graph, v_start, v_end, path_len=0): '''Is there a path between vertex v_start and vertex v_end?''' # Traverse each vertex only once if path_len >= len(graph): return False # Direct path from v_start to v_end? if graph[v_start][v_end]: return True # Indirect path via neighbor v_nbor? for v_nbor, edge in enumerate(graph[v_start]): if edge: # between v_start and v_nbor if find_path(graph, v_nbor, v_end, …

A Case for Puzzle-based Learning Python

Overcome the Knowledge Gap The great teacher Sokrates delivered complex knowledge by asking a sequence of questions. Each question was building on answers to previous questions provided by the student. This more than 2400 year old teaching technique is still in wide-spread use today. A good teacher opens a gap between their’s and the learner’s knowledge. This knowledge gap makes …

Daily Data Science Puzzle: How to Get the Shape of a Numpy Matrix?

What is the output of this puzzle?   import numpy as np # salary in ($1000) [2015, 2016, 2017] dataScientist = [133, 132, 137] productManager = [127, 140, 145] designer = [118, 118, 127] softwareEngineer = [129, 131, 137] a = np.array([dataScientist, productManager, designer, softwareEngineer]) print(a.shape[0]) print(a.shape[1])   Numpy is a popular Python library for data science. The focus of …

Daily Python Puzzle: How to Intersect two Sets in Python?

What is the output of this puzzle?   # cancer-fighting foods cf = {"basil", "berries", "broccoli", "curcume", "garlic", "kale", "oranges", "seeds", "spinach", "sprouts"} # blood pressure reducing foods bf = {"bananas", "berries", "fish", "garlic", "kale", "red beets", "salmon", "seeds", "spinach", "yogurt"} print(len(cf & bf))   This puzzle is about two pieces of basic knowledge. The first piece of basic knowledge …

Daily Python Puzzle: How to Find the Maximum in a Dictionary?

What is the output of this puzzle?   # mg per 100g omega3_table = { "Salmon" : 2260, "Hering" : 1729, "Sardines" : 1480, "Flaxseeds" : 53400, "Eggs" : 400 } y = max(omega3_table, key=lambda x : omega3_table[x]) print(y)   In this puzzle, we learn two things. First, we can retrieve the maximal element of a dictionary using the max …

How to Sort Dictionaries in Python?

What is the output of this puzzle?   # mg per 100g omega3_table = { "Salmon" : 2260, "Hering" : 1729, "Sardines" : 1480, "Flaxseeds" : 53400, "Eggs" : 400 } y = sorted(omega3_table, key=lambda x : omega3_table[x]) print(y[-1])   In this puzzle, we show how to sort a dictionary not after the keys but after the values. Recap, a …

How to Boost your Logic Skills (Series)? The “in” and “is” Keywords.

What is the output of this puzzle?   meal_1 = "Meat" meal_2 = "Flaxseeds" meal_3 = "Marshmallows" healthyFoods = ["Kale", "Apple", "Strawberry", "Banana", "Flaxseeds"] def isHealthy(food): return food in healthyFoods m_1 = isHealthy(meal_1) m_2 = isHealthy(meal_2) m_3 = isHealthy(meal_3) print((not m_1 or m_2) and (meal_2 is "Flaxseeds") and isHealthy("Kale"))   This puzzle shows two new Python concepts: the ‘in’ and …

Boost your Logic Skills Series

What is the output of this puzzle?   # Logic Statements A = True # Temperatures are high B = True # Arctic ice is melting C = True # Sea levels are rising D = False # All regions become hot def follows(A,B): """ Returns True if 'A -> B' in words: 'B follows from A' """ return not …

The Python Map and Ord Functions – or How to Create Your Own Secret Language in Python

What is the output of this puzzle?   def encrypt(s1): s2 = map(lambda c : chr(ord(c) + 2), s1) return ''.join(s2) def decrypt(s1): s2 = map(lambda c : chr(ord(c) – 2), s1) return ''.join(s2) s = "xtherussiansarecomingx" print(decrypt(encrypt(encrypt(s)))==encrypt(s))   You already know that computers only operate on 0s and 1s. Every single character in a string is encoded as a …