;; Racket (check-expect (colsum '((1))) '(1)) (check-expect (colsum '((1 2))) '(1 2)) (check-expect (colsum '((1 2) (3 4))) '(4 6)) (check-expect (colsum '((1 2) (3 4) (5 6))) '(9 12)) (check-expect (colsum '((1 2 3) (4 5 6))) '(5 7 9)) (define size 17) (check-expect (colsum (build-list size (lambda (j) (list j (+ j size) (+ j size size))))) '(136 425 714)) # JS const tree1 = new Tree(1,null,null); const tree2 = new Tree(2,null,null); const tree3 = new Tree(3, tree1, tree2); const tree4 = new Tree(6, new Tree(2,null,null), new Tree(4,null,null)) test("one node",()=>assert.deepStrictEqual(tree1.map((x)=>(x + 1)), new Tree(2,null,null))); test("three nodes",()=>assert.deepStrictEqual(tree3.map((x)=>(x * 2)), tree4)); test("immutable",() => { let tree5 = tree4.map(x=>x - 100); assert.deepStrictEqual(tree4, new Tree(6, new Tree(2,null,null), new Tree(4,null,null))); }); test("list", ()=>{ const size=17; let list1=null; let list2=null; for (let i=0; isize-x-1),list2); assert.deepStrictEqual(list2.map((x)=>size-x-1),list1); }); // Python def test_single(): d = IterDict(["hello"], ["world"]) assert d['hello'] == 'world' def test_multiple(): d = IterDict(range(10), [f'{x}' for x in range(10)]) assert (d[3],d[7])==('3','7') def test_range(): d = IterDict(range(100),range(0,200,2)) assert (d[0],d[10],d[99]) == (0,20,198) def test_generator(): d =IterDict((f'{x}' for x in range(100)), (x for x in range(200) if x % 2 == 0)) assert (d['0'],d['10'], d['99']) == (0,20,198) def test_infinite(): def naturals(): n=0 while True: yield n n+=1 def alternate(): flag=False while True: yield flag flag = not flag d = IterDict(naturals(), alternate()) assert (d[0],d[1001],d[1234]) == (False, True, False) # Octave %!shared X,CB %! X=[1,0,1;0,1,0;1,0,1]; %! CB=gallery("circul",[1,0,1,0,1,0,1,0]); %% Single X %!assert (howmany(X) == 1) %% Disjoint X's %!assert (howmany(blkdiag(X,X,X,X)) == 4) %% 2 Overlapping X's %!assert (howmany(gallery("circul",[1,0,1,0])) == 2) %% Checkerboard image %!assert (howmany(CB) == 18) %!assert (howmany(blkdiag(CB,CB,CB,CB,CB,CB))==108) %% Missing a pixel in the second X %!assert (howmany([1,0,1,0;0,1,0,1;1,0,1,0;0,1,0,0]) == 1) %% Non-square image %!assert (howmany([1,0,1;0,1,0;1,0,1;0,1,0;1,0,1]) == 2) %% Non square, disjoint X's %!assert (howmany([X,X,X;X,X,X])==6) %% filled gaps %!assert (howmany([1,1,1,1;0,1,1,0;1,1,1,1]) == 0)