Advertisement
Guest User

x3c-solver

a guest
Apr 8th, 2020
4,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. import re
  2.  
  3. X =[2,1,3,5,9,0]
  4. C = (2,1,3),(5),(9,0)
  5.  
  6. # This algorithim belongs to Hope1995x on Reddit.
  7.  
  8.  
  9. if len(X) % 3 > 0:
  10.   print('no')
  11.   quit()
  12.  
  13. # This will check for no's
  14.  
  15. convert_to_string = str(C)
  16. new_list = (re.sub("[^0-9,]", "", convert_to_string))
  17. new_list_one = new_list.split(',')
  18. new_list_one = list(map(int, new_list_one))
  19.  
  20. if len(new_list_one) < len(X):
  21.  print('no')
  22.  quit()
  23.  
  24. using_this_for_second_loop=[];
  25. what_if_there_is_no_repeating_elements = 0
  26.  
  27. # The first part of trying to solve the X3C problem
  28. # is to loop through each indidvual index of C.
  29.  
  30. # I will then use a nested loop so that I can loop
  31. # thorugh indexes as if they were lists.
  32.  
  33. # Indexes that contain elements that don't exist in X
  34. # will not be appended to the new list.
  35.  
  36. # Indexes with repeating elements
  37. # will not be appended to the new list.
  38.  
  39.  
  40. # I used a function from https://thispointer.com/python-3-ways-to-check-if-there-are-duplicates-in-a-list/
  41.  
  42.  
  43.  
  44. def checkIfDuplicates_3(listOfElems):
  45.     for j in range(0, len(C)):
  46.         convert_index_into_list = str(C[j])
  47.         new_list = (re.sub("[^0-9,]", "", convert_index_into_list))
  48.         new_list = new_list.split(',')
  49.         new_list = list(map(int, new_list))
  50.         make_sure_all_elements_exist_in_X = all(elem in X for elem in new_list)
  51.         print(new_list)
  52.         if make_sure_all_elements_exist_in_X:
  53.             for elem in new_list:
  54.                 if listOfElems.count(elem) < 2:
  55.                     using_this_for_second_loop.append(C[j])
  56.                     what_if_there_is_no_repeating_elements = 1
  57.    
  58. if what_if_there_is_no_repeating_elements == 0:
  59.     using_this_for_second_loop = C
  60.  
  61.  
  62. # I will then modify the list that will be used for the 2nd loop.
  63. # This is to prevent semantic bugs.
  64.  
  65. # Here the only elements that do exist are the one's THAT DON'T REPEAT
  66. # AND THE ONE'S THAT EXIST IN X.
  67.  
  68.  
  69. preparing_the_answer = str(using_this_for_second_loop)
  70. new_list_two = (re.sub("[^0-9,]", "", preparing_the_answer))
  71. new_list_thr = new_list_two.split(',')
  72.  
  73. new_list_thr = list(map(int, new_list_thr))
  74. making_sure_all_elements_in_X = all(elem in X for elem in new_list_thr)
  75.  
  76. if making_sure_all_elements_in_X:
  77.     print('yes')
  78. else:
  79.     print('no')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement