Skip to content

Instantly share code, notes, and snippets.

View wsgac's full-sized avatar

Wojciech S. Gac wsgac

  • Keepit
  • Warszawa, Poland
View GitHub Profile
@wsgac
wsgac / grid.lisp
Created July 1, 2019 13:04
Adaptation of the grid approach to 2D arrays from PicoLisp to Common Lisp
(defun pair-to-alphanumeric (a b)
(loop
for n = a then (truncate n 26)
until (zerop n)
collect (mod n 26) into chars
finally
(setf chars (nreverse chars))
(when (cdr chars)
(decf (car chars)))
(unless chars
@wsgac
wsgac / row-col-to-alnum.l
Created July 1, 2019 14:53
PicoLisp function to generate alphanumeric strings along the pattern (1 1) -> "a1"
(de num->alnum (Col Row)
(let N Nil
(until (=0 Col)
(push 'N (if (=0 (/ Col 27)) (% Col 27) (inc (% Col 27))))
(setq Col (/ Col 27)))
(pack (mapcar '((C) (char (+ 96 C))) N) Row)))
(test (num->alnum 1 1) "a1")
(test (num->alnum 26 26) "z26")
(test (num->alnum 27 27) "aa27")
@wsgac
wsgac / hourglass.c
Last active July 3, 2019 09:38
Hourglass sum maximizer - based on a HackerRank problem
// The premise is that you define an 'hourglass' as the following shape in an array:
// ###
// #
// ###
// Given a two-dimensional array, find the highest sum of elements "masked" by moving
// the hourglass-shaped mask over the array
int hourglassSum(int arr_rows, int arr_columns, int** arr) {
// Naive, inefficient approach
/*
@wsgac
wsgac / vector.c
Last active July 5, 2019 12:35
Simple vector implementation in C
#include <stdlib.h>
#include "vector.h"
vector make_vector() {
vector vec = {
(int*)malloc(BUFSIZE*sizeof(int)),
BUFSIZE,
0
};
return vec;
@wsgac
wsgac / stack.c
Created July 5, 2019 12:42
Simple stack implementation in C
#include<stdlib.h>
#include "stack.h"
stack make_stack() {
stack s = {
(int*)malloc(BUFSIZE*sizeof(int)),
BUFSIZE,
0
};
return s;
@wsgac
wsgac / array-of-functions.cpp
Created July 8, 2019 15:15
Demonstration of function pointer array in C++
#include <iostream>
using namespace std;
int (*functions[4])(int) = {
[](int x) -> int {
return x + 10;
},
[](int x) -> int {
return x - 10;
@wsgac
wsgac / dynamic-adder.cpp
Created July 9, 2019 09:51
Dynamic function creation example in C++
#include <iostream>
#include <functional>
using namespace std;
// Return a custom, parametrized adder function (lambda)
auto make_adder(int n) {
return [n](int x) {
return x + n;
};
@wsgac
wsgac / circumscribe.py
Created July 18, 2019 14:55
Circumscribe a square over arbitrary polygon (Python)
import math
# Assume 'points' is a list of tuples representing 2D points
# Returns a tuple of top-left corner, and side length
def circumscribe_square(points):
# Initialize extremes
x_min = math.inf
x_max = -math.inf
y_min = math.inf
@wsgac
wsgac / abc.l
Created August 1, 2019 12:15
PicoLisp solution for the ABC problem on Kattis
# Kattis - ABC
# https://open.kattis.com/problems/abc
(de getInput ()
(let (Numbers (sort (list (read) (read) (read)))
Seq (chop (read))
Alist (mapcar '((K V) (cons K V)) '("A" "B" "C") Numbers))
(list Alist Seq)))
(de processInput(Numbers Sequence)
@wsgac
wsgac / aboveaverage.l
Created August 1, 2019 13:48
PicoLisp solution for the Above Average problem on Kattis
# Kattis - Above Average
# https://open.kattis.com/problems/aboveaverage
(load "@lib/math.l")
(de getInput ()
(mapcar '((Row) (mapcar '((El) (read))
(range 1 (read))))
(range 1 (read))))