Skip to content

Instantly share code, notes, and snippets.

View rvprasad's full-sized avatar

Venkatesh-Prasad Ranganath rvprasad

View GitHub Profile
@rvprasad
rvprasad / howManyLongsCanWeLoad.scala
Last active March 7, 2019 21:14
A script to identify how many longs can we store at a time with given heap size on JVM
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
object M {
def main(args: Array[String]) {
val t = new ArrayBuffer[Long]()
while (true) {
t += Random.nextLong
if (t.length % 10000 == 0) {
System.gc()
@rvprasad
rvprasad / parallelStreamAndAsSequence.kts
Last active February 21, 2019 05:02
Java parallel stream is significantly faster than the same stream converted into a Kotlin sequence
import kotlin.streams.asSequence
import kotlin.system.measureTimeMillis
val k = 500000000L
println(measureTimeMillis {
java.util.stream.LongStream.range(1, k)
.parallel()
.asSequence()
.map { it.toString().length.toLong() }
@rvprasad
rvprasad / permutations.kt
Last active February 19, 2019 07:52
Permutation Generator (Algorithm from Cameron's "Combinatorics: Topics, Techniques, Algorithms")
class PermutationGenerator(numOfValues:Int) : Iterator<List<Int>> {
private val maxPos = numOfValues - 1
private val values = (0..maxPos).shuffled()
private var currPerm = (0..maxPos).toMutableList()
@Synchronized override fun hasNext() = currPerm.isNotEmpty()
@Synchronized override fun next(): List<Int> {
val perm = currPerm.toList()
val j = (maxPos - 1 downTo 0).find { currPerm[it] < currPerm[it + 1] }
@rvprasad
rvprasad / io-measure.cpp
Last active February 14, 2019 01:10
Measure throughput of default buffered I/O in different programming languages
#include <chrono>
#include <fstream>
#include <iostream>
using namespace std;
int k = 256;
int numOfNums = k * 1024 * 1024;
int reps = 6;
void writeUsingFile(string fileName) {
@rvprasad
rvprasad / iperf-client-worst-case.yml
Last active February 5, 2019 00:02
Measure network throughput in worst case scenario -- many-to-one
- hosts: worker_raspi
remote_user: life
tasks:
- name: iperf
shell: iperf -t 20 -c 192.168.2.10
@rvprasad
rvprasad / iperf-client-best-case.yml
Last active February 5, 2019 00:01
Measure network throughput in best case scenario -- one-to-one
- hosts: worker_raspi
remote_user: life
strategy: free
tasks:
- name: iperf1
shell: iperf -t 20 -c 192.168.2.10
when: ansible_host == '192.168.2.13'
- name: iperf2
shell: iperf -t 20 -c 192.168.2.11
when: ansible_host == '192.168.2.14'
@rvprasad
rvprasad / overlapping_intervals.py
Created November 6, 2018 03:50
Calculate the maximum number of overlapping intervals
# Given a list of intervals, calculate the maximum number of overlapping intervals.
# Each interval is inclusive at both ends.
def solve(intervals): # list of pairs
tmp1 = set(y for x in intervals for y in x)
tmp2 = max(map(lambda x: sum(1 if y[0] <= x <= y[1] else 0 for y in intervals), tmp1))
print "%s %d" % (intervals, tmp2)
solve([(1,5), (2,4), (3, 6)])
solve([(1,5), (2,3), (4, 6), (7,9)])
@rvprasad
rvprasad / collectTagsOccurringWithGivenTag.py
Last active May 24, 2018 01:49
Python script to extract tags that co-occur with a given tag on question-type posts in Posts.xml file from Stack Overflow data dump.
#
# Copyright (c) 2017, Venkatesh-Prasad Ranganath
#
# BSD 3-clause License
#
# Author: Venkatesh-Prasad Ranganath
#
import argparse
import datetime
import itertools
@rvprasad
rvprasad / anonymizeNames.py
Last active May 5, 2018 20:41
Anonymizes the names (in names.txt) without disturbing the order and frequency of alphabet and number positions
from random import randrange, sample, shuffle
alphas = list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
nums = list('1234567890')
shuffle(alphas)
shuffle(nums)
def scramble(s, seen):
while True:
ret = ''.join([(nums[int(x)] if x in nums else
@rvprasad
rvprasad / dfsSolver.groovy
Last active April 16, 2018 03:14
Identifies a DFS traversal order of nodes of a given directed graph using z3 solver
/*
* Copyright (c) 2018, Venkatesh-Prasad Ranganath
*
* Licensed under BSD 3-clause License
*
* Author: Venkatesh-Prasad Ranganath
*/
import groovy.transform.Field