Skip to content

Instantly share code, notes, and snippets.

@Gravifer
Created November 26, 2024 09:35
Show Gist options
  • Save Gravifer/0338745624edba51c627fc40daa1a0e1 to your computer and use it in GitHub Desktop.
Save Gravifer/0338745624edba51c627fc40daa1a0e1 to your computer and use it in GitHub Desktop.
Bucket stack value
# https://chatgpt.com/share/674595bf-959c-8007-ab19-7a9d55328526
class BucketStack:
def __init__(self):
self.stack = []
def push(self, bucket):
self.stack.append(bucket)
def pop(self):
if self.stack:
return self.stack.pop()
return None
def peek(self):
if self.stack:
return self.stack[-1]
return None
def push_into_bucket(self, item):
if self.stack:
self.stack[-1].append(item)
else:
raise Exception("No bucket to push into")
def pop_from_bucket(self):
if self.stack:
return self.stack[-1].pop()
return None
def clear_top_bucket(self):
if self.stack:
self.stack[-1] = []
else:
raise Exception("No bucket to clear")
def is_empty(self):
return len(self.stack) == 0
def BucketStackValue(self):
# Generate a unique hash value for the current bucket stack state
# Here, we are using tuple and frozenset to ensure order independence
stack_tuple = tuple(frozenset(bucket) for bucket in self.stack)
return hash(stack_tuple)
"""
## Example usage
# Create a BucketStack object
bs = BucketStack()
# Push some buckets with items
bs.push([1, 2, 3])
bs.push([4, 5])
bs.push([6, 7, 8])
# Get the unique hash value for the current BucketStack state
print(bs.BucketStackValue()) # Will print a unique hash for this configuration
# Modify the stack
bs.pop()
# Get the unique hash value again after modification
print(bs.BucketStackValue()) # The hash will be different after the pop operation
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment