Skip to content

Instantly share code, notes, and snippets.

View cgopalan's full-sized avatar

Chandrakant Gopalan cgopalan

View GitHub Profile
@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 / 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"})
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.
import csv
import json
input_file = 'PROD_CUSTOMERS_GOOD.csv'
output_file = 'wowcher_customer_creation_formatted.json'
with open(input_file) as infile:
infile.readline()
with open(output_file, 'w') as outfile:
@cgopalan
cgopalan / search_all_org_repos.py
Created February 25, 2015 21:34
Searches all repos in an organization for a text
"""
Search for a text in all repos for an organization.
Currently via the web UI you can only search per repo.
This will only return number of matches for the text and not
the filenames (as of now).
You need an OAuth Token for this to work.
Run it as:
@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')