Skip to content

Instantly share code, notes, and snippets.

View rgrig's full-sized avatar

Radu Grigore rgrig

View GitHub Profile
@rgrig
rgrig / a.py
Created December 7, 2020 15:39
import sys
M = 10
cache = [[None for k in range(2**b+1)] for b in range(M)]
def maxo(a, b):
if b is None:
return a
if a is None:
return b
@rgrig
rgrig / foo.hs
Last active November 24, 2020 14:41
import Data.Array.IArray (Array, array, (!), elems)
imap :: (Int -> a -> b) -> [a] -> [b]
imap = m 1
where
m _ _ [] = []
m i f (x : xs) = (f i x) : m (i + 1) f xs
solve :: [Int] -> Int
solve xs = n - cyclesCount

Questions: https://github.com/regehr/assert_quiz/blob/master/assert_quiz.md

  1. bad, should handle error sensibly
  2. bad, debug builds don't even alloc
  3. bad, should handle error sensibly
  4. ?, assert ok, but code around clearly bad: the hash_function has no idea what lenght is, and no modulo done after
  5. bad, should handle error sensibly
  6. kinda ok. You probably want a way to turn off expensive assertions, though. This way you can run prog with all asserts on small tests, and with cheap asserts on big tests (an with no asserts in release -- if you so fancy).
include <stdio.h>
int main() {
const int n = 200;
int data[n];
data[0]=0; data[1]=2;
int i, j;
i = j = 0;
int state = 0;
while (j < n) {
printf("%d\n",data[j]);
diff -r e5fa13aae1cc Python/bltinmodule.c
--- a/Python/bltinmodule.c Tue Dec 31 06:52:47 2013 -0500
+++ b/Python/bltinmodule.c Wed Jan 01 15:46:01 2014 +0000
@@ -1559,14 +1559,15 @@
equivalent to (x**y) % z, but may be more efficient (e.g. for ints).");
-
static PyObject *
builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
class Benchmark {
public final boolean run() {
if (++iteration == 1) prepare();
if (validateOutput) {
System.setOut(out); // this is where System.out is redefined
out.openLog();
}
try { iterate(); }
finally {
if (validateOutput) {
cache = dict() # cache[x] is a map from weights to paths s->x
s = 0 # source
def find(t):
if t in cache: return cache[t]
if t == s:
return dict([(0,[s])])
r = dict()
for (x, w) in pred(t):
for (ww, path) in find(x):
solution time [s] memory [bytes]
peter.hs .163 7479296
radu.hs .160 7495680
duncan.hs 176.97 7467008
tim.hs .160 7471104 Files out and out.ref differ
nicolas.lhs .460 19001344 Files out and out.ref differ
lennart.hs 2134.98 7467008 Files out and out.ref differ
This was run in /dev/shm (that is, not using the HDD) on a computer with two cores at 4788 bogomips each (Intel i3 @2.4GHz), and 4GB of RAM.
import qualified Data.IntMap as M
import Maybe
fillBin capacity ws@(w:ws') weight count
| newWeight > capacity = (ws, weight, count)
| otherwise = fillBin capacity ws' newWeight (succ count)
where newWeight = weight + w
go period capacity bins weights acc state seen
| bins == 0 = acc