Skip to content

Instantly share code, notes, and snippets.

View srishanbhattarai's full-sized avatar
💭
Make software fast again

Srishan srishanbhattarai

💭
Make software fast again
View GitHub Profile
"vim:fileencoding=utf-8:ft=conf:foldmethod=marker
" General {{{
" These are general settings that work on both Vim/Neovim. They mostly relate
" to saner defaults like `hidden` buffers, `unnamed` clipboard etc. They've
" been heavily commented to make sense to the future reader (most likely me!).
set hidden "hidden buffers are allowed. This allows me to move around files without saving them.
set clipboard=unnamed "unnamed clipboard unifies the system clipboard and Vim's clipboard.
set ignorecase "ignorecases allows for case insensitive pattern matching during search
# start window and pane numbering from 1
set -g base-index 1
setw -g pane-base-index 1
# Vi bindings
set -g status-keys vi
# renumber windows on close
set -g renumber-windows on
@srishanbhattarai
srishanbhattarai / bounded_knapsack.cpp
Last active May 3, 2021 15:44
0/1 Bounded Knapsack (several variations, along with solution reconstruction and space optimization)
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <cstdio>
@srishanbhattarai
srishanbhattarai / rev_sublist.cpp
Created April 24, 2021 20:06
Reversing a sublist within a linked list
/*
There are three sections of the list:
1. The part before the sub list that gets reversed
2. The actual sub list that gets reversed
3. The part after the sub list that gets reversed
When reversing the entire list, (1) and (3) are empty, and (2) is the entire list.
In the following code:
• You need to traverse the list until you get to the p'th node and then only reverse 'q - p + 1' times (length of the window)
○ When reversing the entire list, p = 0 and q = length of list - 1 so you don't need to do this.
@srishanbhattarai
srishanbhattarai / ospf_dijkstra.cpp
Created April 7, 2021 18:45
OSPF Link State (LS) Algorithm (using Dijkstra's shortest path)
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <queue>
#include <cstdio>
#include <unordered_set>
#include <limits>
#include <functional>
@srishanbhattarai
srishanbhattarai / struct_alignment.c
Last active January 2, 2021 04:16
Example of alignment of structures, and padding fields
#include <stdio.h>
struct Foo {
char a;
int b;
char c;
};
// This little program shows how memory alignment can affect layout in memory, and how compilers do extra
// work to insert padding into your structures.
@srishanbhattarai
srishanbhattarai / linkedlist_contingous_alloc.c
Last active October 3, 2020 08:40
An example of a small custom memory allocator that contiguously allocates space for a linkedlist to prevent excessive jumps. Please don't use this code. There's several ways this can be improved.
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
// 16 byte struct
typedef struct Node {
int64_t value;
struct Node* next;
} Node;
@srishanbhattarai
srishanbhattarai / bin_tree_search.cpp
Created July 2, 2020 22:23
Binary tree traversals example.
#include <iostream>
#include <functional>
/**
* Easy way to distinguish between the 3 traversal methods is to check the position of the root.
* If the root comes first -> pre-order
* If the root comes last -> post-order
* If the root comes in the middle -> in-order.
*/
@srishanbhattarai
srishanbhattarai / game.cpp
Last active April 28, 2020 02:42
Tic Tac Toe AI using AB pruning
/**
* Tic Tac Toe using Minmax search with Alpha Beta Pruning.
*
* Author: Srishan Bhattarai
*
* Compile: g++ -g --std=c++11 ttt.cc -o ttt
*
* Follow the "get_ai_move" function to see the algorithm in action, rest of
* the code is to get user input, render board into terminal, check for goal
* states etc.
@srishanbhattarai
srishanbhattarai / markov.rs
Created March 13, 2020 08:20
Basic Markov chain in Rust (clones stuff around so not the most performant, gets job done)
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
struct MarkovChain {
order: usize,
chain: HashMap<Vec<String>, HashMap<String, usize>>,
freqs: HashMap<Vec<String>, usize>,