Skip to content

Instantly share code, notes, and snippets.

// Implements addition using bit operations (an adder)
// This was an answer to a playful Koan test for .Net Core.
// I resolved not to use google and work it out from basic
// boolean math principles. Could probably be simpler, but was
// a fun exercise.
public void AdditionWithoutPlusOrMinusOperator()
{
var adder = new Func<int,int,int>((a,b)=>{
int c = 0; // carry
from math import log2, pow
class Tree():
def __init__(this,value=None,left=None,right=None):
this.value = value
this.left = left
this.right = right
tree = Tree('*',
Tree('-',
from math import cos, acos, sin, asin, sqrt, radians, degrees
def satLook(Le,le,ls):
rers = 6370.0/42242.0 # rEarth / rGsoSat
Ls = 0 # gso
eLat = radians(Le)
eLon = radians(le)
sLon = radians(ls)
sLat = radians(Ls)
gammaCos = sin(sLat)*sin(eLat)+cos(eLat)*cos(sLat)*cos(eLon-sLon)
from random import randint
import numpy as np
choices = np.array(['Keep','Change'])
choose = np.array([0,0])
win = np.array([0,0])
tries = 100000
for x in range(tries):
door_choices = [0,1,2]
winning_door = randint(0,2)
using System;
using System.Collections.Generic;
using static Extensions; // using statics
static class Extensions {
public static void HonkTwice<T>(this T vehicle) where T : Program.Wheeled {
vehicle.Honk();
vehicle.Honk();
}
class stack:
def __init__(this):
this.items = []
def push(this,i):
this.items.append(i)
def pop(this):
if len(this.items) > 0:
i = this.items[len(this.items)-1]
this.items.remove(i)
return i
import console
def getMove(n,lastmove):
hint = 1
if n <= 2*lastmove:
hint = n
elif n == 7:
hint = 2
elif n < 7:
hint = 1
class Mergesort:
def __init__(this):
this.aux = []
def merge(this,a,lo,mid,hi):
#print(f'merge(a,{lo},{mid},{hi})')
i = lo
j = mid+1
for k in range(lo,hi+1):
class MergesortBU:
def __init__(this):
this.aux = []
def merge(this,a,lo,mid,hi):
i = lo
j = mid+1
for k in range(lo,hi+1):
this.aux[k]=a[k]
for k in range(lo,hi+1):
# O(n)
def issorted(a):
for i in range(1,len(a)):
if a[i-1]>a[i]:
return False
return True
# Binary search for key
# in sorted array. O(n log n)
def rank(key,a):