Skip to content

Instantly share code, notes, and snippets.

@shivamMg
shivamMg / bellman-ford.py
Last active March 1, 2026 15:14
Graph Theory
class GraphBellmanFord(Graph):
def bellman_ford(self, src: str):
dist = {}
for _, u in self.V.items():
dist[u] = float('Inf')
src = self.V[src]
dist[src] = 0
edges = []
for e in self.E.values():
@shivamMg
shivamMg / binary_exponentiation.py
Last active March 1, 2026 15:13
Popular algorithms
"""
Binary Exponentiation computes x^n in O(logn) time.
"""
def pow(x, n):
result = 1
power = x
while n > 0:
bit = n % 2
if bit == 1:
@shivamMg
shivamMg / git-bisect.sh
Last active September 4, 2018 09:26
git bisect
git clone github.com/go-openapi/spec && cd spec
# we need to know what commit was `PathLoader` added to the `spec` pkg
git bisect start --term-old not-defined --term-new defined
git bisect not-defined cb9ba12 # first commit
git bisect defined master
git bisect run isdefined.sh
@shivamMg
shivamMg / gist:b60885fea0cf657d82cbb6e820981d7b
Last active November 6, 2018 10:19
Grammars for parsing arithmetic expressions
Grammar with left-recursion (e.g. Expr's derivation contains Expr as first non-terminal).
Not suitable for Recursive Descent parsing.
Expr = Expr "+" Term | Expr "-" Term | Term
Term = Term "*" Factor | Term "/" Factor | Factor
Factor = "(" Expr ")" | "-" Factor | Number
@shivamMg
shivamMg / grammar.txt
Created November 18, 2018 09:39
EBNF for domain names (copied from https://www.ietf.org/rfc/rfc1035.txt)
<domain> ::= <subdomain> | " "
<subdomain> ::= <label> | <subdomain> "." <label>
<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
<let-dig-hyp> ::= <let-dig> | "-"
@shivamMg
shivamMg / curry.go
Created January 5, 2019 13:30
Currying
package main
type VarSum func(a ...int) interface{}
func curry(fn func(x, y, z int) int, args ...int) interface{} {
if len(args) >= 3 {
return fn(args[0], args[1], args[2])
}
return VarSum(func(more ...int) interface{} {
args = append(args, more...)
@shivamMg
shivamMg / gazebo9-ubuntu-xenial.yaml
Last active October 16, 2019 12:29
gazebo9 for ubuntu xenial
gazebo:
ubuntu:
xenial: [gazebo9]
gazebo_ros:
ubuntu:
xenial: [ros-kinetic-gazebo9-ros]
@shivamMg
shivamMg / command
Created November 1, 2019 18:23
webots arch linux coredump
[shivam@smg ~]$ webots
Fontconfig warning: FcPattern object weight does not accept value [40 210)
/snap/webots/12/usr/share/webots/webots: line 69: 89715 Segmentation fault (core dumped) "$webots_home/bin/webots-bin" "$@"
@shivamMg
shivamMg / timer.go
Last active November 9, 2019 06:08
Time a function in Go
package main
import (
"fmt"
"time"
)
func main() {
work()
}
@shivamMg
shivamMg / greet.c
Last active January 27, 2020 19:18
Simple example for building C Shared Libraries with Go
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libhello.h"
int main(int argc, char **argv) {
char *name, *greeting;
if (argc < 2) {
fprintf(stderr, "missing name\n");