Advertisement
Guest User

x3c-solver-complete

a guest
Apr 6th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. from itertools import combinations
  2. import re
  3.  
  4. X =[1,7,3,4,5,6]
  5. C = (1,2,3),(3,4,5),(1,2,5),(1,5,6),(1,7,6)
  6.  
  7. if len(X) % 3 > 0:
  8.   print('no')
  9.   quit()
  10.  
  11. # I will check each possible set of "len(X)//3-combinations" from
  12. # the list C.
  13. # This algorithim belongs to Hopee1995x on Reddit
  14.  
  15. trip_wire_for_no = 0
  16. for jj in combinations(C, len(X)//3):
  17.     if len(jj) > 1:
  18.         one_by_one = str(jj)
  19.         # Below, I then convert each combination
  20.         # of jj into an indidvudual list.
  21.         loop_one_by_one = (re.sub("[^0-9,]", "", one_by_one))
  22.         loop_one_by_one = loop_one_by_one.split(',')
  23.         # Below, I convert "strings-of-integers"
  24.         # into int's.
  25.         # (eg. '2' is now converted into 2 in the list)
  26.         convert = list(map(int, loop_one_by_one))
  27.         # Below, I make sure that ALL OF JJ's elements exist in X.
  28.         make_sure_all_elements_exist_in_X =  all(elem in convert for elem in X)
  29.         # if make_sure_all_elements_exist_in_X == true, I then loop through the new list. (convert)
  30.         if make_sure_all_elements_exist_in_X:
  31.             for yy in range(0, len(convert)):
  32.                 # Below, this statement makes sure that
  33.                 #every element in "convert" occurs less than 2-times.
  34.                 # So that our X3C solution only contains
  35.                 # ONE-ELEMENT-OF-EACH.
  36.                 if convert.count(int(convert[yy])) < 2:
  37.                     # This statement makes sure that all elements
  38.                     #in convert occur once in the list X.
  39.                     if X.count(int(convert[yy])) == 1:
  40.                         # This statement makes sure that
  41.                         # the len(convert) == len(X).
  42.                         if len(convert) == len(X):
  43.                             print(convert, 'yes')
  44.                             trip_wire_for_no = 1
  45.                             break
  46.                            
  47.  
  48. if trip_wire_for_no == 0:
  49.   print('no')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement