The code works so far, but all these functions of split, new-list, and map just make it harder. Is there a mixture of set comparisons/differences to be able to do the same function that my code is doing?
Edit: All these functions such as new_list is intended to prepare the list of sets in C to be able to pass it through a loop. The loop makes sure that
1.All elements in s exist in c.
2.The 2nd part make sure there is no repeating elements in the solution. (eg. if len(c) == len(set(new_list)
I think I just found a semantic bug here.
3.Use set(new_list) == set(s)
to make sure that both sets are equal no matter what order.
import re
s = [2,4,8]
c = [[2], [4], [2, 4]]
new_list = (re.sub("[^0-9,]", "", str(c)))
new_list = new_list.split(',')
new_list = list(map(str, new_list))
if ('') in new_list:
del new_list[new_list.index('')]
new_list = list(map(int, new_list))
if all(elem in new_list for elem in s):
if len(c) == len(set(new_list)):
if set(new_list) == set(s):
print('This is a Yes solution')
else:
print('This is a No solution')
You're doing way too much work.
all elem
check separately since it's already guaranteed if the sets are equal. def exact_cover(s, c):
c_flat = [item for sub in c for item in sub]
c_set = set(c_flat)
return len(c_set) == len(c_flat) and c_set == set(s)