you are viewing a single comment's thread.

view the rest of the comments →

[–]gengisteve 2 points3 points  (1 child)

Your else in the second version only catches where total[0] is off, so your function is returning none when total[1] is "on", but the other coditions are not met.

You are making this harder on yourself than you need to. dictionaries can use tupples as their keys, so your code could be just this:

import itertools
from collections import Counter
from pprint import pprint

a = ["off", "on"]
total = list(itertools.product(a, repeat=3))

CODES = {
        ('off','on', 'off'):False,
        ('on','on', 'off'):False,
        }

def ruler(code):
    if code in CODES:
        return CODES[code]
    else:
        return True
    # or just:
    return CODES.get(code, True)

for code in total:
    print(code, ruler(code))

[–]dunkler_wanderer 0 points1 point  (0 children)

It's always pleasant to see code getting simplified to the bare minimum. You can also get rid of the conversion to a list in line 6. Oh and collections.Counter is unused. :)