Python Dictionaries: Not even a space-time tradeoff If you could choose to store things that you’d want to look up later in a Python dictionary or in a Python list, which would you choose? It turns out that looking up items in a Python dictionary is much faster than looking up items in a Python list. If you search for … Read More

## What makes Numpy Arrays Fast: Memory and Strides

How is Numpy so fast? In this post we find out how Numpy’s ndarray is stored and how it is usually manipulated by Numpy functions using strides. Getting to know the ndarray A NumPy ndarray is a N-dimensional array. You can create one like this:

1 |
X = np.array([[0,1,2],[3,4,5]], dtype='int16') |

These arrays are homogenous arrays of fixed-sized items. That is, all the items in … Read More

## List Comprehensions in Python

1. What are list comprehensions? List comprehensions construct lists in natural-to-express ways. They can replace map-filter combinations and many for loops. They’re just syntactic sugar. That means they make your code easier to read (and prettier). Example 1: For loops -> List comprehension

1 2 3 4 5 6 7 8 9 10 11 12 |
# Make a list of squares of 1 to 10 >>> squares = [] >>> for i in range(1,11): ... squares.append(i**2) ... >>> squares [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] # List comprehension >>> squares_lc = [i**2 for i in range(1,11)] >>> squares_lc [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] |

Example 2: Map-filter -> List Comprehension

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Map and filter >>> doubled_odd_numbers = list(map( lambda n: n * 2, filter(lambda n: n % 2 == 1, range(1,10)) )) >>> doubled_odd_numbers [2, 6, 10, 14, 18] # List comprehension >>> doubled_odd_numbers_lc = [ n * 2 for n in range(1,10) if n % 2 == 1 ] >>> doubled_odd_numbers_lc [2, 6, 10, 14, 18] |

(I will do a post on map and filter and … Read More

## Unit Tests and Doctests in PyCharm

Yesterday I thought it was finally time to try an IDE (Integrated Development Environment. I downloaded PyCharm Community (free) which many friends use and recommend. What’s an IDE? An IDE in an integrated development environment. It contains tools that make developers’ lives easier. PyCharm, an IDE for Python, contains a source code editor with code refactoring, build automation tools, a debugger, … Read More

## Code Wars: Consecutive Strings Programming Problem

Woe is me, I am a terrible programmer. Spoiler alert: Scroll down for terrible code followed by elegant code. Question You are given an array strarr of strings and an integer k. Your task is to return the first longest string consisting of k consecutive strings taken in the array. Example: longest_consec([“zone”, “abigail”, “theta”, “form”, “libe”, “zas”, “theta”, “abigail”], 2) … Read More

## Big O Notation: A Common Mistake and Documentation

A Question What’s the time complexity of the following algorithm? (Don’t know how to calculate that? Here’s a nice intro to Big-O Notation from InterviewCake.)

1 2 3 4 5 6 7 |
def all_interarray_pairs(array_x, array_y): for i in range(len(array_x)): for j in range(len(array_y)): print(array_x[i],array_y[j]) |

O(n^2)? Nope. If array_x has length x and array_y has length y, the algorithm has time complexity O(xy) since one loop with a constant number of operations is run y times for each iteration of … Read More