A 5-post collection

Quick Tip: Using Memoization To Speed Up Recursive Functions

A recent visit to hackerrank reminded me of a nice trick to make our recursive functions run faster - and it's called memoization. The Question A hackerrank question asked to find the number of ways a child can climb a staircase in a given height, provided that child can climb 1, 2 or 3 stairs at a time. A recursive solution in Python easy to write: import sys def ways(n): if n < 0: return 0 if n == 0: return 1 return ways(n-1) + ways(

Read more

Recursion Exercises

Recursions are one of those things that are easy to understand in theory, but in practice can take some getting used to. If you're struggling with it here are some exercises that might help you along. Implement Loops Using Recursion Write a function that prints Hello World n times without using loops. Usage example: say_hi(5) # Prints: # 0: Hello World # 1: Hello World # 2: Hello World # 3: Hello World # 4: Hello World What is the maximum value you can pass to the function without raising

Read more

How To: Encrypt Large Files with Python and PyNacl

One limitation of pynacl's concise API is its lack of support for buffered reading. When it comes to large files we can't always load all data to memory in one chunk. This is how I dealt with the problem in a recent project. The Mechanism Pynacl can encrypt and authenticate short blocks (<=16kb is the recommended size). With larger files we'll want to read the file in chunks, encrypt and sign each using pynacl's secret box and then HMAC the entire encrypted data. These are

Read more

Python Exercises

Most python exercises I found online were either too easy or tested things that were not directly related to python (mostly data structures or algorithms). Neither is a good choice when you're teaching Python courses. This following list has helped me learn and later teach various topics in Python. They focus on the langauge itself and the standard library. Hope you'll find it useful, and feel free to add yours in comments. Basic Syntax Write a program that asks the user for a number (integer only)

Read more