Skip to content

Instantly share code, notes, and snippets.

@arzon94
arzon94 / main.dart
Created August 24, 2020 21:32
calculate number of selected week days between dates with no loops
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;
@arzon94
arzon94 / .block
Last active May 23, 2019 17:40
impact analysis
license: mit
@arzon94
arzon94 / namedtuples
Last active November 15, 2018 15:35
tuples/named tuples are immutable, quick way of making a class
# 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'
# 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')
# 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:
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = {**x, **y}
@arzon94
arzon94 / numanagrams
Created November 6, 2018 16:21
frozenset, collections counter, dictionary methods
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
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
@arzon94
arzon94 / nginx
Created October 24, 2018 17:40
useful nginx commands and snippets
#reload
sudo nginx -s reload