Skip to content

Instantly share code, notes, and snippets.

View opethe1st's full-sized avatar
🕶️
.

Ope opethe1st

🕶️
.
View GitHub Profile
@opethe1st
opethe1st / python_tweaks.md
Last active June 1, 2019 11:26
Tweaks to Python + other thoughts on programming

Tweaks to Python + other accompanying thoughts

This is based on my current (emphasis on current!) thoughts about what's best for code.

Dive in!

  • remove support for class and static methods. I believe it is neater for classes to be just prototypes or schemas for objects. So where would class and static methods go? They would go into the module :). For example,
# rectange.py - home of all things rectangular!

# This counts all the rectangles that have been created so far. This would have been a class variable
from enum import Enum
from typing import Optional
from dataclasses import dataclass
class DIRECTION(Enum):
LEFT = 'L'
RIGHT = 'R'
@opethe1st
opethe1st / summation.py
Last active March 15, 2019 13:04
How to express summation
"""
How do we express the concept of summing values of a function over a set of values?
∑f(n) with n ∈ A
"""
array = range(29)
f = lambda x: x**2
# imperative
total = 0
import string
from typing import List
graph = {
'1': ['2', '4'],
'2': ['1', '3', '5'],
'3': ['2', '6'],
'4': ['1', '5', '7'],
'5': ['2', '4', '6', '8'],
'6': ['3', '5', '9'],
@opethe1st
opethe1st / jinja_expression_statement.py
Created June 27, 2018 23:08
Describes using the expression statement
from jinja2 import FileSystemLoader
from jinja2 import Environment
from jinja2.ext import ExprStmtExtension
env = Environment(lstrip_blocks=True, loader=FileSystemLoader(searchpath='templates'), extensions=[ExprStmtExtension])
class Thing:
def __init__(self, a, b):
self.a = a
self.b = b
import re
import os
import sys
for directory in sys.path:
for directory, subDirectories, files in os.walk(directory):
for file in files:
if file.endswith('.py'):
filepath = os.path.join(directory, file).replace('/', '.')
solution = []
def possibleCombinations(N, items):
if N < 0:
return
if N == 0:
print solution
else:
for k in items:
@opethe1st
opethe1st / EnumerateSets.cpp
Created June 25, 2017 13:07
Enumerate all possible subsets of a set.
#include <iostream>
#include <vector>
using namespace std;
void enumerate(vector<int> arr){
for(int current_set = 0; current_set < (1<<(arr.size()-1)); current_set++){
for(int j=0; j<arr.size(); j++){ // j is the position in arr.
if (current_set & (1<<j)){ // if the bit is set in this set, then arr[j] is in the set
cout<<arr[j]<<',';
}
}
def flip(s):
"""changes all _ in s to + and all + in s to _"""
res = []
for l in s:
if l=='+':
res.append('-')
else:
res.append('+')
return "".join(res)
def flip(s):
res = []
for l in s:
if l=='+':
res.append('-')
else:
res.append('+')
return "".join(res)