Skip to content

Instantly share code, notes, and snippets.

View balu's full-sized avatar

Balagopal Komarath balu

  • Gandhinagar, India
View GitHub Profile
@balu
balu / rustmodinterception.rs
Last active April 4, 2024 07:35
A demonstration of an imported module intercepting calls to local functions in importing module in rust.
mod a {
// pub fn foo() { println!("a::foo()"); } // Uncomment this line to see interception.
pub fn bar() { println!("a::bar()"); }
}
fn foo() { println!("foo()"); }
fn greet() { use a::*; foo(); bar(); }
@balu
balu / skip-link.md
Created July 10, 2023 15:03
HTML and CSS for skip links
@balu
balu / nav.js
Created July 10, 2023 08:23
JS for handling navigation menus acessibly
'use strict';
class DisclosureNav {
constructor(navNode) {
this.navNode = navNode;
var menus = navNode.querySelectorAll('a[role="button"]');
this.menus = [];
for (var i = 0; i < menus.length; ++i) {
this.menus.push(new DisclosureMenu(menus[i]));
}
@balu
balu / SubsetSum.py
Created August 24, 2022 16:02
An implementation for the canonical subset-sum DP in Python.
def subset_sum(elements, target):
"""
Determine if a subset of ELEMENTS sum exactly to TARGET.
We assume that ELEMENTS contain only non-negative numbers. The program can
be modified to accomodate negative numbers in ELEMENTS using a table that
has rows for -W to TARGET where -W is the smallest possible sum from ELEMENTS.
"""
if not elements:
return target == 0
@balu
balu / FordFulkerson.py
Created August 24, 2022 15:57
An implementation of the Ford-Fulkerson max-flow algorithm in Python.
# We represent the original network using a dictionary that maps edges into
# capacities. A residual graph maps edges into its weight and whether or not
# it is a forward edge.
#
# We assume that the source is 0 and sink is n-1 where n = #vertices.
def empty_graph(nvertices):
return [nvertices, dict()]
def nvertices(network):

Exercises

  1. Write a function maplist that takes a function and a list and returns a list of values obtained by applying the given function to each element in the list.
def square(x): return x*x
def cube(x): return x*x*x

assert maplist(square, [1, 2, 3, 4]) == [1, 4, 9, 16]
assert maplist(cube, [1, 2, 3, 4]) == [1, 8, 27, 64]

Exercises

  1. Write a function intersecting that takes as input a list of sets and a set and returns True iff the given set has at least one common element with each set in the list.
assert intersecting([{1, 2}, {3, 4}], {2, 3})
assert not intersecting([{1, 2}, {3, 4}], {1, 2})
  1. Write a function that takes a verb and outputs the past tense. Your function should also handle some irregular verbs.

Introduction

Use the random library and its functions when you want to generate random values for simulations.

For example, to simulate a dice throw.

from random import choice
choice([1, 2, 3, 4, 5, 6])
@balu
balu / classes.md
Last active February 15, 2021 07:49

Introduction

If your program needs to define a new kind of values, use a class. Follow the below steps to define a class.

  1. Come up with a good name for the type.
  2. Determine the components and their types.
  3. Write a __init__ function that initializes the components of the type appropriately from the arguments. This may involve complex logic.

To write functions that operate on your new type, follow the same instructions as for tuples. Instead of unpacking, you may use . to access the components.

Introduction

Use the following guidelines to manipulate lists.

  1. The output list is initially empty or some fixed sequence.
  2. Use for x in xs: to process each element of the input sequence xs in turn.
  3. Process the element x appropriately.
  4. Append the element corresponding to x into the output list.
  5. Once you've finished processing all elements, return the output list.