Skip to content

Instantly share code, notes, and snippets.

View rahulrajaram's full-sized avatar

rahulrajaram rahulrajaram

View GitHub Profile
@rahulrajaram
rahulrajaram / .cpp
Last active August 13, 2017 05:53
Tail recursive function to find arithmetic sum in C++/C
/*
TL;DR: Compile the program as follows:
------
g++ add.cpp -O2 && ./a.out
RIGOROUS DISCUSSION:
--------------------
Suppose you want to compute the sum of the series:
@rahulrajaram
rahulrajaram / .md
Last active September 30, 2018 22:01
Ruby: Where is the method defined?

Ruby: Different ways to define methods in

The Ruby Object Model

One of the central aspects of  the Ruby programming language is the Ruby object model, as per which everything (but for constructs such as methods, and keywords) in Ruby -- ​​classes, instances, lambdas, procs, strings, numbers, decimals, hashmaps -- is an object. This renders a kind of uniformity to Ruby that few other languages offer.

At the very same time, the object model can also become confusing. In particular, the following two questions can sometimes become difficult to answer, but being able to readily answer which can set you apart as a Ruby programmer.

  1. What is self in a given context?
  2. If I define a method here, where will it go? That is, which object is it going to be defined on?
@rahulrajaram
rahulrajaram / .py
Last active October 24, 2017 22:10
Python print colored STDOUT
COLOR_MAP = {
'black': '30',
'red': '31',
'green': '32',
'yellow': '33',
'blue': '34',
'magenta': '35',
'cyan': '36',
'white': '37',
}
@rahulrajaram
rahulrajaram / .md
Last active April 2, 2023 15:47
Python: Write to a file from multiple threads

I recently came across the need to spawn multiple threads, each of which needs to write to the same file. Since the file will experience contention from multiple resources, we need to guarantee thread-safety.

NOTE: The following examples work with Python 3.x. To execute the following programs using Python 2.7, please replace threading.get_ident() with thread.get_ident(). As a result, you would need to import thread and not threading.

  1. (The following example will take a very long time). It will create 200 threads, each of which will wait until a global lock is available for acquisition.
# threading_lock.py
import threading
@rahulrajaram
rahulrajaram / .md
Last active October 23, 2017 02:30
Password generator using Python
import random
import string

# The pool of characters to pick the characters for your password from
password_char_set = string.ascii_letters + string.digits + string.punctuation

def random_character():
    return password_char_set[random.randint(0, len(password_char_set))]
@rahulrajaram
rahulrajaram / .rb
Created April 19, 2018 19:47
An example with procs, blocks and lambdas in Ruby
lambda_1 = -> { puts 'From lambda_1: Hello, World!' }
lambda_2 = -> { puts 'From lambda_2: Hello, World!' }
proc_1 = Proc.new { puts 'From proc_1: Hello, World!' }
proc_2 = Proc.new { puts 'From proc_2: Hello, World!' }
def proc_or_lambda_called(
proc_or_lamdba_1,
proc_or_lamdba_2
)
@rahulrajaram
rahulrajaram / .py
Created December 10, 2018 21:55
Save stack trace to file
import traceback
def print_stack_trace():
try:
raise Exception
except Exception:
with open('~/.my_important_stack_trace', 'w') as file:
file.write('{}'.format(repr(traceback.format_stack())))
@rahulrajaram
rahulrajaram / .c
Created May 8, 2019 07:01
Generic in C using macros
#include <string.h>
#include <stdio.h>
/*
* Macro function that initializes a struct object
* and returns it.
*
* For this to work, a `{type}Init` function must
* be defined.
*/
@rahulrajaram
rahulrajaram / .js
Created August 31, 2019 08:18
List all S3 Objects using JavaScript SDK
/*
* File invokes s3#listObjects recursively to retrieve all objects
*/
const AWS = require('aws-sdk')
const s3 = new AWS.S3();
let args = {
'Bucket': ADD_BUCKET_NAME_HERE,
'Prefix': ADD_PATH_PREFIX_HERE_OR_REMOVE_THIS_ATTRIBUTE
};
@rahulrajaram
rahulrajaram / .py
Created October 6, 2019 04:43
`tail` implementation in Python
"""
Script implements:
tail FILE_NAME NUMBER_OF_LINES_TO_TAIL
"""
import sys
def seek_eof(file):
file.seek(0, 2)