Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # Unique circles of squares.
- #
- # This script reads paths from stdin, like a filter.
- # It removes duplicates and mirrors; and prints only cycles that
- # are unique.
- #
- # Only paths that contain values from {1, 2 ... n-1, n} are allowed.
- # A path is NOT checked if it is valid circle of squares. Only
- # if it is unique.
- # Numbers must be space or tab seperated.
- with open('/dev/stdin', 'r') as f:
- mylist = []
- stringlist = []
- for i in f:
- mylist.append(i.strip().split())
- stringlist.append(' '.join(i.strip().split()))
- intlist = []
- for i in mylist:
- if i not in intlist:
- intlist.append(list(map(int, i)))
- # Check range of sequences.
- # Only ranges from 1..N are allowed.
- intrangechecked = []
- stringrangechecked = []
- for i, j in enumerate(intlist[:]):
- if sorted(j) == list(range(1, len(j)+1)):
- intrangechecked.append(j)
- stringrangechecked.append(stringlist[i])
- doublestringlist =[]
- for i in stringrangechecked[:]:
- doublestringlist.append(i + ' ' + i)
- # Remove duplicates and mirrors
- final = []
- error = []
- for i,k in enumerate(stringrangechecked):
- for j,l in enumerate(doublestringlist):
- if k in error:
- break
- if k in l and i != j:
- # print(i,j, 'same sequences')
- error.append(stringrangechecked[j])
- if ' '.join(k.split()[::-1]) in l:
- # print(i,j, 'reverse', k)
- error.append(stringrangechecked[j])
- if k not in final:
- final.append(k)
- # Print out all unique cycles
- for i in final:
- for j in i.split():
- print(f'{j:>2} ', end='')
- print()
- print('Paths in input: ', len(mylist))
- print('Unique cycles:', len(final))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement