First submission. Python 3.(3)?
Started learning python this week, advice is appreciated.
Previously I've only ever worked with scheme because of a cs course I took at my university
Loaded with comments because I worked this challenge as if I was going to submit it for marks
#Datatype definitions, imports:
#Def: A wire_sys is a dictionary of the form
#{(wire number):(wire valure (0 or 1))}
#ex: {'0':0, '1':0, '2',1}
#a wire_sys is sorted if the values decrease as keys increase (or is empty)
import itertools
#-------------------------------
# Helper Functions:
#-------------------------------
#Binseq(k)
#Int -> [str1,str2...]
#generates a list where each item is a string containing a binary combination. (such as '0101')
#generates all combinations of length k^2
#ex Binseq(2**(0.5)) -> ["00", "01" "10" "11"]
#-------------------------------
def binseq(k):
return [''.join(x) for x in itertools.product(["0","1"], repeat=(k**2))]
#-------------------------------
#break_string_to_digits(list_str_num)
#[str1, str2... strN] -> [[Int1],[Int2],...[IntN]]
#Takes a list of strings and returns a list of each string's digits
#(ex ['1000', '1001] -> [[1,0,0,0], [1,0,0,1]])
#-------------------------------
def break_string_to_digits(list_str_num):
list_of_ints= []
for item in list_str_num:
digits = []
for char in item:
digits.append(int(char))
else:
list_of_ints.append(digits)
else:
return list_of_ints
#-------------------------------
#comparitor(upper,lower)
#Int, Int -> Int, Int
#compares two values, if the value on the "upper" wire is
#less than then the "lower" wire, the valuers swap. otherwise they do not move
#in short, move 1's up and 0's down
#ex. comparitor (0,1) -> (1,0)
# comparitor (1,0) -> (1,0)
#-------------------------------
def comparitor(upper,lower):
if upper < lower:
return (upper,lower)
else:
return (lower, upper)
#-------------------------------
# is_sorted(wire_sys)
# Wire_sys -> Boolean
#checks if a wire_sys is sorted by searching
#for the first zero, then checking if there are any ones after it
# ex. is_sorted {'0':0, '1':0, '2',1} -> False
#-------------------------------
def is_sorted(wire_sys):
for i in wire_sys:
if wire_sys[i] == 0:
for j in range(i, len(wire_sys)):
if wire_sys[j] == 1:
return False
else:
return True
else:
return True
def sort(lo_connections, lo_lo_ints):
#build wire_sys
for permute in lo_lo_ints: # for each permutatin
wire_sys = {} # reset index and wire system
i = 0
for number in permute: # for each number in a given permutation
wire_sys[i] = number # assign ith number key "i"
i += 1 # increase index
#compare pair of wires at each connection
for connection in lo_connections:
wire_sys[connection[0]], wire_sys[connection[1]] = comparitor(wire_sys[connection[0]], wire_sys[connection[1]]) #compare values at connectors and swap values if upper wire is lower then the lower wire
else:
if is_sorted(wire_sys): #determine if the wire system is sorted
continue
else:
print("Not verified")
else:
print ("verified")
#Main Function:
def verify(num_wires, lo_connections):
lo_lo_ints = binseq(num_wires)
sort(lo_connections, lo_lo_ints)