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
| void main() { | |
| DateTime t1 = DateTime.now(); | |
| DateTime t2 = t1.add(Duration(days: 12)); | |
| List<bool> activeDays = [true, true, true, true, true, false, false]; | |
| int startday = t1.weekday, endday = t2.weekday; | |
| print("starts on $startday of week and ${t1.day} of month"); | |
| print("ends on $endday of week and ${t2.day} of month"); | |
| double numActiveDays = 0, extraneousDays = 0; | |
| int totalDays = t2.difference(t1).inDays; |
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
| license: mit |
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
| # Why Python is Great: Namedtuples | |
| # Using namedtuple is way shorter than | |
| # defining a class manually: | |
| >>> from collections import namedtuple | |
| >>> Car = namedtuple('Car', 'color mileage') | |
| # Our new "Car" class works as expected: | |
| >>> my_car = Car('red', 3812.4) | |
| >>> my_car.color | |
| 'red' |
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
| # Different ways to test multiple | |
| # flags at once in Python | |
| x, y, z = 0, 1, 0 | |
| if x == 1 or y == 1 or z == 1: | |
| print('passed') | |
| if 1 in (x, y, z): | |
| print('passed') |
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
| # How to sort a Python dict by value | |
| # (== get a representation sorted by value) | |
| >>> xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1} | |
| >>> sorted(xs.items(), key=lambda x: x[1]) | |
| [('d', 1), ('c', 2), ('b', 3), ('a', 4)] | |
| # Or: |
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
| x = {'a': 1, 'b': 2} | |
| y = {'b': 3, 'c': 4} | |
| z = {**x, **y} |
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
| from collections import Counter | |
| # Complete the sherlockAndAnagrams function below. | |
| def sherlockAndAnagrams(s): | |
| count = 0 | |
| anagramString = s | |
| dict1 = {} | |
| for i in range(len(s)): | |
| for j in range(1, len(s) - i + 1): | |
| key = frozenset(Counter(s[i:i+j]).items()) | |
| dict1[key] = dict1.get(key, 0) + 1 |
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 sort(arr): | |
| # corrected = False | |
| sorted = False | |
| while not sorted: | |
| sorted = True | |
| for i in range(len(arr)-1): | |
| if arr[i] > arr[i+1]: | |
| sorted = False | |
| arr[i], arr[i+1] = arr[i+1], arr[i] | |
| return arr |
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 binary_search(arr): | |
| low, high, mid = 0, len(arr) - 1, 0 | |
| best = low | |
| while low <= high: | |
| mid = low + (high - low) / 2 | |
| if arr[mid] < val: | |
| low = mid + 1 | |
| elif arr[mid] > val: | |
| high = mid - 1 |
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
| #reload | |
| sudo nginx -s reload |
NewerOlder