Skip to content

Instantly share code, notes, and snippets.

Venkatesh-Prasad Ranganath rvprasad

Block or report user

Report or block rvprasad

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@rvprasad
rvprasad / variance.kt
Last active Apr 21, 2019
Understanding Contravariance and Covariance via Kotlin
View variance.kt
class Producer<out T: Any>(val e:T) {
fun read(): T = e
}
class Consumer<in T: Any>() {
private lateinit var e: T
fun write(v: T): Unit { e = v }
}
fun main() {
@rvprasad
rvprasad / howManyLongsCanWeLoad.scala
Last active Mar 7, 2019
A script to identify how many longs can we store at a time with given heap size on JVM
View howManyLongsCanWeLoad.scala
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 Feb 21, 2019
Java parallel stream is significantly faster than the same stream converted into a Kotlin sequence
View parallelStreamAndAsSequence.kts
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 Feb 19, 2019
Permutation Generator (Algorithm from Cameron's "Combinatorics: Topics, Techniques, Algorithms")
View permutations.kt
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 Feb 14, 2019
Measure throughput of default buffered I/O in different programming languages
View io-measure.cpp
#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-best-case.yml
Last active Feb 5, 2019
Measure network throughput in best case scenario -- one-to-one
View iperf-client-best-case.yml
- 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 / iperf-client-worst-case.yml
Last active Feb 5, 2019
Measure network throughput in worst case scenario -- many-to-one
View iperf-client-worst-case.yml
- hosts: worker_raspi
remote_user: life
tasks:
- name: iperf
shell: iperf -t 20 -c 192.168.2.10
@rvprasad
rvprasad / overlapping_intervals.py
Created Nov 6, 2018
Calculate the maximum number of overlapping intervals
View overlapping_intervals.py
# 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 / extractCommentsWithPageNum.groovy
Last active Jul 20, 2018
Extracts comments associated with highlights in a PDF document along with page numbers
View extractCommentsWithPageNum.groovy
/*
* Copyright (c) 2018, Venkatesh-Prasad Ranganath
*
* Licensed under BSD 3-clause License
*
* Author: Venkatesh-Prasad Ranganath
*
* Adapted from https://stackoverflow.com/questions/33253757/java-apache-pdfbox-extract-highlighted-text
*/
@rvprasad
rvprasad / max_subseq_sum2.py
Created Jun 29, 2018
Find a sub-sequence of sequence of numbers that is maximal in terms of sum of its contents and does not contain consecutive numbers from the sequence.
View max_subseq_sum2.py
# Given a sequence of positive numbers, pick a sub-sequence such that
# - the sum of numbers in the sub-sequence is maximal and
# - the sub-sequence does not contain consecutive numbers from the sequence.
#
# Assuming the index of the list goes from 1 thru n,
# f(l, n>0) = max(f(l, n-2) + l(n), f(l, n-1))
# f(l, n<1) = 0
def answer(L):
if len(L) == 1:
You can’t perform that action at this time.