Skip to content

Instantly share code, notes, and snippets.

@mortymacs
mortymacs / textbattery.lua
Last active February 16, 2017 17:37
Textbattery for awesome awful widget (modified version of textclock widget)
---------------------------------------------------------------------------
-- @copyright 2009 Julien Danjou
---------------------------------------------------------------------------
local setmetatable = setmetatable
local os = os
local textbox = require("wibox.widget.textbox")
local capi = { timer = timer }
--- Text battery widget.
@mortymacs
mortymacs / binary_search.py
Created April 20, 2017 17:51
Binary search in Python
#!/usr/bin/env python
"""Search algorithm implementations."""
class Search:
"""Search algorithms."""
def binary_search(self, data: list, search_value: int):
"""Binary search implementation."""
low = 0
@mortymacs
mortymacs / linkedlist.py
Created May 24, 2017 19:19
Sample LinkedList in Python
class Node:
_data = None
_next = None
def __init__(self, data = None, next_node = None):
self._data = data
self._next = next_node
# DATA PROPERTY
@property
@mortymacs
mortymacs / stack.py
Last active May 25, 2017 06:44
Simple Stack in Python (Last-in-First-out)
class Stack:
def __init__(self):
self._items = []
def push(self, data):
self._items.insert(0, data)
def pop(self):
return self._items.pop(0)
@mortymacs
mortymacs / queue.py
Created May 25, 2017 06:50
Simple Queue in Python (First-in-First-out)
class Queue:
def __init__(self):
self._items = []
def enqueue(self, data):
self._items.append(data)
def dequeue(self):
return self._items.pop(0)
@mortymacs
mortymacs / bubblesort.py
Last active May 25, 2017 07:54
Simple Bubble Sort in Python
def bubble_sort(data):
i, j, swap = 0, 0, 0
while i < len(data) - 1:
while j < len(data) - 1:
if data[j] > data[j+1]:
data[j], data[j+1] = data[j+1], data[j]
swap += 1
j += 1
if swap == 0: # If no change find, so array is already sorted
break
@mortymacs
mortymacs / anagram.py
Last active May 26, 2017 08:22
Simple Anagram in Python
def anagram(str1, str2):
str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()
if str1 == str2:
return True
if len(str1) != len(str2):
return False
for l in list(str1):
str2 = str2.replace(l, "")
str1 = str1.replace(l, "")
@mortymacs
mortymacs / find_imported_files.py
Last active May 31, 2017 05:47
Find all lines of import packages and files in a python file
pyfile = open("sample.py")
ml = False # multiline import flag
for line in pyfile:
line = line.strip()
if line.startswith("import"):
print(line)
elif line.startswith("from"):
@mortymacs
mortymacs / substring.py
Last active June 2, 2017 22:40
Generate all substring
def generate_substring(s):
t = [s, ]
for i in list(s):
if i not in t:
t.append(i)
for j in t:
if j.find(i) >= 0:
continue
x = i + j if i < j else j + i
@mortymacs
mortymacs / calc_string_ord.py
Created June 4, 2017 20:30
Calculate String ord in Python
def calc_string_ord(s, duplicate=1):
n = ord(s[-1]) - ord(s[0])
total = int( ord(s[0]) * (len(s) // duplicate) + ((n * (n + 1)) / 2) ) * duplicate
return total
# Example usage
print (calc_string_ord("ABCD"))
# Result: 266
print(calc_string_ord("aabbccdd", 2))