def __init__(self, sequence): # check that sequence is an iterable type try: _ = [x for x in sequence] except: raise TypeError('Expected sequence to be iterable.') # check that the sequence contains only integers for x in sequence: if not float(x).is_integer(): raise TypeError('Sequence must contain only integers.') # make sure all entries in the sequence are of type int and sort in non-increasing order S = [int(x) for x in sequence] S.sort(reverse=True) process_sequence = [ list(S) ] # keeps track of resulting sequences at each step elim_sequence = [ ] # keeps track of the elements eliminated at each step while S[0] > 0 and S[0] < len(S): # so long as the largest element d of the sequence is positive, remove d from the sequence and subtract 1 from the next d elements d = S.pop(0) for i in range(d): S[i] = S[i] - 1 S.sort(reverse=True) # append the resulting sequence to the process sequence process_sequence.append(list(S)) # append the removed element to the elimination sequence elim_sequence.append(d) # finally, append the 0s in the last step of the Havel Hakimi process to the elimination sequence if contains_only_zeros(process_sequence[-1]): elim_sequence.extend(S) # set class attributes self.process = process_sequence self.eliminationSequence = elim_sequence
def is_graphic(self): """Return whether or not the initial sequence is graphic. Returns ------- bool True if the initial sequence is graphic. False otherwise. """ return contains_only_zeros(self.process[-1])
def __init__(self, lSequence): lSequence.sort(reverse=True) process_sequence = [ list(lSequence) ] # keeps track of resulting sequences at each step elim_sequence = [ ] # keeps track of the elements eliminated at each step while lSequence[0] > 0 and lSequence[0] < len(lSequence): # so long as the largest element d of the sequence is positive, remove d from the sequence and subtract 1 from the next d elements d = lSequence.pop(0) for i in range(d): lSequence[i] = lSequence[i] - 1 lSequence.sort(reverse=True) # append the resulting sequence to the process sequence process_sequence.append(list(lSequence)) # append the removed element to the elimination sequence elim_sequence.append(d) # finally, append the 0s in the last step of the Havel Hakimi process to the elimination sequence if contains_only_zeros(process_sequence[-1]): elim_sequence.extend(lSequence) # set class attributes self.process = process_sequence self.eliminationSequence = elim_sequence
def is_graphic(self): return contains_only_zeros(self.process[-1])