Skip to content

Instantly share code, notes, and snippets.

View cgopalan's full-sized avatar

Chandrakant Gopalan cgopalan

View GitHub Profile
@cgopalan
cgopalan / htmls_to_pdf.py
Created February 13, 2018 19:25
Convert list of html files to one pdf
import pdfkit
# Fill this list with the html pages you want to be converted
file_list = []
# Disable links since some point to local hard drive and dont work
pdfkit.from_file(file_list, "generatedpdf.pdf", options={"disable-external-links": None, "load-error-handling": "ignore"})
@cgopalan
cgopalan / selectionsort.py
Created June 13, 2021 14:42
selectionsort.py
def selection_sort(nums):
nums_length = len(nums)
if nums_length < 2:
return nums
for k in range(nums_length):
for i in range(k, nums_length):
if i == k:
smallest_idx, smallest = i, nums[i]
else:
@cgopalan
cgopalan / insertionsort.py
Created May 30, 2021 20:51
Insertion Sort
def insertion_sort(nums):
nums_length = len(nums)
if nums_length < 2:
return nums
for i in range(1, nums_length):
x = i-1
while nums[i] < nums[x] and x >= 0:
x -= 1
else:
nums.insert(x+1, nums[i])
@cgopalan
cgopalan / quicksort.py
Created April 30, 2021 23:19
Quicksort
from typing import List
def quicksort(ints: List[int]):
if len(ints) < 2 or len(set(ints)) == 1:
return ints
pivot = ints[0]
ints_less_than_pivot = [x for x in ints[1:] if x <= pivot]
ints_more_than_pivot = [x for x in ints[1:] if x > pivot]
return quicksort(ints_less_than_pivot) + [pivot] + quicksort(ints_more_than_pivot)
@cgopalan
cgopalan / tailrecursivesum.scala
Created September 8, 2011 17:52
Scala Tail Recursive Sum
object FirstClassFunctions {
def sum(f: Int => Int)(a: Int, b:Int): Int = {
def iter(a: Int, result: Int): Int =
if (a > b) result
else iter(a + 1, f(a) + result)
iter(a,0)
}
def main(args: Array[String]) {
println("Sum is: " + sum(x => x)(1,10))
import unittest
def flatten(arg_list, result_list):
""" Flatten an array of arbitrarily nested arrays of
integers into a flat array of integers. """
for x in arg_list:
if isinstance(x, list):
flatten(x, result_list)
else:
from itertools import groupby
from operator import itemgetter
myvals = [('hello', 'arrival'), ('hi', 'arrival'), ('test', 'no'), ('voo', 'yes'), ('cool', 'arrival')]
[(a,map(itemgetter(1), b))
for a,b in groupby( # 4. Group by key for which you have create list.
sorted([(x, # 3. Sort the keys like arrival, yes, etc.
reduce(lambda a,b: a+' '+b, map(itemgetter(0), y))) # 2. Concantenate the strs with same contiguous key.
for x,y in groupby(myvals, key=lambda e: e[1])]), # 1. Group by key for which you have to concat str.
@cgopalan
cgopalan / mergesort.py
Created March 24, 2012 22:17
Mergesort In Python
def mergesort(arr, n):
if n == 1: return arr
len_half = int(n/2)
return merge(mergesort(arr[:len_half], len_half),
mergesort(arr[len_half:], n - len_half), n)
def merge(arr1, arr2, n):
result = []
i,j = 0,0
for x in range(n):
@cgopalan
cgopalan / autocomplete.py
Created January 7, 2012 17:55
Autocomplete for python interpreter
import rlcompleter, readline
readline.parse_and_bind('tab: complete')
@cgopalan
cgopalan / CompoundOrGate.scala
Created October 6, 2011 21:57
OR Gate as combination of inverter and AND gate
object CompoundOrGate {
type Action = () => Unit
class Wire {
private var sigVal = false
private var actions: List[Action] = List()
def getSignal = sigVal
def setSignal(s: Boolean) =
if (s != sigVal) {
sigVal = s