Category Archives: Python

How to fix PyCharm @staticmethod Error

When you run PyCharm with Python 2.7.2, you may see errors for @staticmethod annotations.  PyCharm marks your @staticmethod annotation as an error:

Screen Shot 2015-01-07 at 12.43.49 AM

When you mouse over, you see a failing the unresolved reference inspection:

Screen Shot 2015-01-07 at 12.44.10 AM

You can fix this issue by configuring Python 2.7.6 as the Project Interpreter:

Screen Shot 2015-01-07 at 12.40.57 AM

Screen Shot 2015-01-07 at 12.44.51 AM

To configure your Python Interpreter, select: PyCharm > Preferences… > Project: your_project > Project Interpreter.  You may have to install 2.7.6 or a newer version, for example, by clicking “+” in the OS X version of PyCharm.  Once you configure 2.7.6 PyCharm will update your indexes and, once that process completes, you will see a clean @staticmethod annotation:

Screen Shot 2015-01-07 at 12.45.14 AM


Lexicographic Enumeration

Screen Shot 2014-12-23 at 1.12.04 AM

I recently implemented Dijkstra’s algorithm, published in 1976, for lexicographically enumerating permutations in Python for Project Euler’s problem 32:

“Pandigital products
Problem 32
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand / multiplier / product identity can be written as a 1 through 9 pandigital.”

Here is the Python code that implements Dijkstra’s algorithm for enumerating the pandigital numbers:

Given this code we can then check which numbers are unusual, as defined above.

By the way, friend me on Project Euler if you are also a fan of this site.

My friend code is: 704085_afc937d56367db90098e60535d34393a

DRY Number Word Phrases

I recently ran into a piece of code that contained repetitive sections and I thought it could use refactoring to DRY it.

This Python code passage converts an integer expressed in digits into the the same integer expressed as words.  The method “in_words” uses two dictionaries:

The repetition occurs when we apply a similar sequence of steps for the number orders 1000, 100, 20, and 1:

The repeated steps serve to:

  1. determine the first digit of the number by dividing by the number order,
  2. get the remainder after taking away the first digit by calculating the modulo,
  3. adding the appropriate words to the word phrase, and
  4. adding a separator if there are more words to be added to the word phrase.

The DRY, refactored code is as follows:

In this passage, we have extracted the above steps into a method “process_digit”, and we call this method for each number order: 1000, 100, 10, and 1.

The problem is, because there are special cases to be handled, when the number is greater than 100 or when the number is greater than 20, the resulting code is a little harder to understand.  The added complexity of nesting two methods instead of one linear method adds to the perceived difficulty of the new code passage.

Which version of the code is better?  Leave your comment by hitting the link below the title.