Skip to content

Instantly share code, notes, and snippets.

View terror's full-sized avatar

liam terror

View GitHub Profile
@terror
terror / hof.py
Last active June 8, 2021 17:25
Code for my `Higher order functions` blog post
def filter(func, iterable):
return [el for el in iterable if func(el)]
def map(func, iterable):
return [func(x) for x in iterable]
def reduce(func, iterable, init=None):
it = iter(iterable)
val = next(it) if init is None else init
@terror
terror / meta.py
Last active June 8, 2021 17:25
Python metaprogramming example
class Meta(type):
def __new__(cls, what, bases=None, dict=None):
if 'area' not in dict:
raise Exception("Area not found!")
return type.__new__(cls, what, bases, dict)
class Shape(metaclass=Meta):
def __init__(self, l, w):
self.l = l
self.w = w
_='_=%r;print (_%%_)';print (_%_)
import time
import functools
class Foo:
def __init__(self, n = 1):
self.n = n
@functools.cached_property
def expensive(self):
return fib(self.n)
def diff_a(a, b):
return list(set(a) - set(b)) + list(set(b) - set(a))
def diff_b(lists):
x = [list(set(i) - set(j)) for i in lists for j in lists]
return list(set([item for sublist in x for item in sublist]))
def main():
a = [1, 2, 3]; b = [1, 2, 5, 4]; c = [1, 3, 5, 6]
print(diff_a(a, b)) # [3, 5, 4]
# python functions are objects
# can be passed stored in variables, passed as args, returned, etc
def wrap(func):
print("wow!")
func()
print("oo")
def hello():
print("hello!")
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
List<List<int>> arr = new List<List<int>>();
// -> []
for(int i = 0; i < 10; ++i) {
using System;
using static System.Console;
class Program {
static void Main(string[] args) {
Write("Number of rows: ");
int r = int.Parse(ReadLine()); // 5
Write("\nNumebr of cols: ");
int c = int.Parse(ReadLine()); // 3
@terror
terror / tictactoe.py
Created December 8, 2020 19:14
Tic Tac Toe
from typing import List
R, C = 3, 3
def construct(board: List[List[int]]) -> str:
"""
Constructs a tic tac toe board string based
on a 2D list
@terror
terror / me.gif
Last active August 20, 2020 15:31
me.gif