riddle 4 (python)

Aside

a python riddle – figure out what each line does without running the code. Bonus points for being able to clearly explain what happens, and why.

test.py:

```import test
class Test:
pass

t = Test()
print "test.Test: " + str(isinstance(t, test.Test))
print "Test: " + str(isinstance(t, Test))```

`[More programming riddles]`

project euler problem #1

Aside

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

http://projecteuler.net/problem=1

• try solving it as a python one-liner
• try optimizing your solution for speed

highlight below for my solution:

```
sum(range(0,1000,3)) + sum(range(0,1000,5)) - sum(range(0,1000,15))
```

`[More programming riddles]`

project euler problem #4

Aside

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 Ã— 99.

Find the largest palindrome made from the product of two 3-digit numbers.

http://projecteuler.net/problem=4

highlight below for my solution:

```
def is_palindrome(num):
return str(num) == str(num)[::-1]

def fn(n):
max_palindrome = 1
for x in range(n,1,-1):
if x*n < max_palindrome:
break
for y in range(n,x-1,-1):
if is_palindrome(x*y) and x*y > max_palindrome:
max_palindrome = x*y
elif x * y < max_palindrome:
break
return max_palindrome

print fn(999)

```

`[More programming riddles]`

sudoku solution verifier – python

Aside

write a sudoku solution verifier in python.

that is, given a list of lists, representing a solution to an nxn sudoku puzzle, verify that the solution is a correct sudoku solution:

```fn ([[1,2],[2,1]]) # True
fn ([[1,3],[2,1]]) # False```

(question seen as a Udacity programming course homework question)

highlight to see a solution:

```
def fn(x):
b = [(j + 1) for j in range(0,len(x))]
for i in x:
if sorted(i) != b: return False
return True
```

`[More programming riddles]`

python counters

in my pythonic trolling of the internet, i found a character counter, written by ashoksk. given a string of text, return a count for each character in the string (a tool that might be used when attempting to crack an encrypted message)

so i wondered, could this be a python one liner? turns out that, from python 2.7 on, it can be (highlight to see the code below):

```
from collections import Counter
def fn(x): c = Counter(x); return {x: c[x] for x in c.keys()}
```

it’s a cheat, though. anyone have a real one-line implementation of this?

`[More programming riddles]`

riddle 3 (javascript)

Aside

javascript riddle.

without executing the code, write down what you expect the results of the following two expressions to be:

```>1 + "41"
>1 + + "41"```

check your answers. bonus points if you can explain what happens in each case, and why.

`[More programming riddles]`

recursive touch

Aside

i just moved a bunch of media files from one device, to the other, and the file creation dates for a small number of my directories (and their contents) moved a few years into the future.

this problem smelled like a python 1 liner; i came pretty close – highlight to see the code:

```
import os, sys, subprocess

for d,s,fs in os.walk(sys.argv[1]):
for p in (s+fs):
subprocess.call(["touch",os.path.join(d,p)])
```

(source online, in case I improve it)

`[More programming riddles]`

riddle 2 (python)

Aside

same as last time, highlight to see answer(s), in python, below.

```
def fn(*args):
x = '/'.join(str(i) for i in args[:3])
y = '/'.join(str(i) for i in args[3:])
return abs((datetime.strptime(x,"%Y/%m/%d")-datetime.strptime(y,"%Y/%m/%d")).days)

fn(2001,1,1,2002,2,2)

#depending on input format, could do a one-liner:

def fn(x,y): abs((datetime.strptime(x,"%Y/%m/%d")-datetime.strptime(y,"%Y/%m/%d")).days)

#it's probably possible to replace "%Y/%m/%d" with "%c"
```

`[More programming riddles]`

Javascript FizzBuzz

Aside

just overheard FizzBuzz at a coffee-shop interview with a programmer. old school! highlightÂ below for a javascript one-liner:

```
for(i=1;;++i)console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)

```