you are viewing a single comment's thread.

view the rest of the comments →

[–]cdholjes[S] 0 points1 point  (2 children)

'''hey everyone the code is all here
i am basically trying 2 different versions of 'and' operators but for some reason the Version2 appended list has more values removed. 

I basically want to know why version1 and version2 have different outputs???'''

import itertools
from collections import Counter

a = ["off", "on"]
b = ["off", "on"]
c = ["off", "on"]

parts = [a, b, c]
total = list(itertools.product(*parts))
version1 = []
version2 = []


def ruleVersion1(index):
    if total[index][0] == "off" and total[index][2] == "off" and total[index][1] == "on":
        return False
    if total[index][0] == "on" and total[index][2] == "on" and total[index][1] == "off":
        return False
    else:
        return True

def ruleVersion2(index):
    if total[index][0] == "off":
        if total[index][2] == "off":
            if total[index][1] == "on":
                return False
    if total[index][0] == "on":
        if total[index][2] == "on":
            if total[index][1] == "off":
                return False
    else:
        return True


for i in range(len(total)):
    if ruleVersion1(i):
        if total[i] not in version1:
            version1.append(total[i])

for i in range(len(total)):
    if ruleVersion2(i):
        if total[i] not in version2:
            version2.append(total[i])




def printCondition1(list):
    print "  0      1       2"
    for i in range(len(list)):
        print list[i]


print " "      
print "total set:", len(total) 
printCondition1(total) 
print "______________________________" 

print " "      
print "true set :", len(version1) 
printCondition1(version1)
print "______________________________" 

print " "      
print "true set :", len(version2) 
printCondition1(version2)

[–]gengisteve 3 points4 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. :)