This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --------------------------------------------------------------------------- | |
| -- @copyright 2009 Julien Danjou | |
| --------------------------------------------------------------------------- | |
| local setmetatable = setmetatable | |
| local os = os | |
| local textbox = require("wibox.widget.textbox") | |
| local capi = { timer = timer } | |
| --- Text battery widget. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node: | |
| _data = None | |
| _next = None | |
| def __init__(self, data = None, next_node = None): | |
| self._data = data | |
| self._next = next_node | |
| # DATA PROPERTY | |
| @property |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Stack: | |
| def __init__(self): | |
| self._items = [] | |
| def push(self, data): | |
| self._items.insert(0, data) | |
| def pop(self): | |
| return self._items.pop(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Queue: | |
| def __init__(self): | |
| self._items = [] | |
| def enqueue(self, data): | |
| self._items.append(data) | |
| def dequeue(self): | |
| return self._items.pop(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, "") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |