Advertisement
Guest User

uniqueCycles.py

a guest
May 5th, 2021
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # Unique circles of squares.
  4. #
  5. # This script reads paths from stdin, like a filter.
  6. # It removes duplicates and mirrors; and prints only cycles that
  7. # are unique.
  8. #
  9. # Only paths that contain values from {1, 2 ... n-1, n} are allowed.
  10. # A path is NOT checked if it is valid circle of squares. Only
  11. # if it is unique.
  12. # Numbers must be space or tab seperated.
  13.  
  14.  
  15. with open('/dev/stdin', 'r') as f:
  16. mylist = []
  17. stringlist = []
  18.  
  19. for i in f:
  20. mylist.append(i.strip().split())
  21. stringlist.append(' '.join(i.strip().split()))
  22.  
  23. intlist = []
  24. for i in mylist:
  25. if i not in intlist:
  26. intlist.append(list(map(int, i)))
  27.  
  28. # Check range of sequences.
  29. # Only ranges from 1..N are allowed.
  30.  
  31. intrangechecked = []
  32. stringrangechecked = []
  33. for i, j in enumerate(intlist[:]):
  34. if sorted(j) == list(range(1, len(j)+1)):
  35. intrangechecked.append(j)
  36. stringrangechecked.append(stringlist[i])
  37.  
  38. doublestringlist =[]
  39. for i in stringrangechecked[:]:
  40. doublestringlist.append(i + ' ' + i)
  41.  
  42.  
  43. # Remove duplicates and mirrors
  44.  
  45. final = []
  46. error = []
  47. for i,k in enumerate(stringrangechecked):
  48. for j,l in enumerate(doublestringlist):
  49. if k in error:
  50. break
  51. if k in l and i != j:
  52. # print(i,j, 'same sequences')
  53. error.append(stringrangechecked[j])
  54. if ' '.join(k.split()[::-1]) in l:
  55. # print(i,j, 'reverse', k)
  56. error.append(stringrangechecked[j])
  57. if k not in final:
  58. final.append(k)
  59.  
  60.  
  61. # Print out all unique cycles
  62.  
  63. for i in final:
  64. for j in i.split():
  65. print(f'{j:>2} ', end='')
  66. print()
  67.  
  68. print('Paths in input: ', len(mylist))
  69. print('Unique cycles:', len(final))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement