Skip to content

Instantly share code, notes, and snippets.

View Clivern's full-sized avatar

Ahmed Clivern

View GitHub Profile
@Clivern
Clivern / python_deque.py
Created March 21, 2021 18:43
Python Deque
class Deque():
"""Deque DST"""
def __init__(self):
self._items = []
def add_front(self, item):
"""
Add to the front
"""
@Clivern
Clivern / python_linked_lists.py
Created March 21, 2021 18:42
Python Linked Lists
import bisect
from heapq import *
"""
Linked Lists
A linked list is an ordered collection of values.
Linked lists are similar to arrays in the sense that they contain objects in a linear order.
However they differ from arrays in their memory layout.
@Clivern
Clivern / python_strings.py
Created March 21, 2021 18:41
Python Strings
"""
Strings are immutable sequence of characters
http://thomas-cokelaer.info/tutorials/python/strings.html
https://docs.python.org/3/library/stdtypes.html#string-methods
"""
class Strings():
def __init__(self, elem):
self.elem = elem
@Clivern
Clivern / python_dict.py
Created March 21, 2021 18:41
Python Dicts
class Dicts():
def __init__(self, elem):
self.elem = elem
def len(self):
return len(self.elem)
def keys(self):
return self.elem.keys()
@Clivern
Clivern / python_tuple.py
Created March 21, 2021 18:40
Python Tuple
"""
A tuple is similar to a list. The difference between the two is that we cannot change the elements of a tuple once
it is assigned whereas in a list, elements can be changed.
- We generally use tuple for heterogeneous (different) datatypes and list for homogeneous (similar) datatypes.
- Since tuple are immutable, iterating through tuple is faster than with list. So there is a slight performance boost.
- Tuples that contain immutable elements can be used as key for a dictionary. With list, this is not possible.
- If you have data that doesn't change, implementing it as tuple will guarantee that it remains write-protected.
https://www.programiz.com/python-programming/tuple
@Clivern
Clivern / python_heap.py
Created March 21, 2021 18:40
Python Heap
from heapq import *
class Heap():
def __init__(self):
self.heap = []
def heappush(self, item):
"""Push the value item onto the heap, maintaining the heap invariant."""
heappush(self.heap, item)
@Clivern
Clivern / python_stack.py
Created March 21, 2021 18:38
Python Stack
class Stack():
def __init__(self):
self.stack = []
def pushCharacter(self, char):
self.stack.append(char)
def popCharacter(self):
return self.stack.pop()
@Clivern
Clivern / python_queue.py
Created March 21, 2021 18:38
Python Queue
class Queue():
def __init__(self):
self.queue = []
def enqueueCharacter(self, char):
self.queue.insert(0, char)
def dequeueCharacter(self):
return self.queue.pop()
@Clivern
Clivern / python_lists.py
Created March 21, 2021 18:37
Python Lists
import bisect
from heapq import *
class Lists():
def __init__(self, elem):
self.elem = elem
def append(self, x):
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.