Skip to content

Instantly share code, notes, and snippets.

View kalyco's full-sized avatar

Kayla Comalli kalyco

  • 6 River Systems
  • Boston, MA
View GitHub Profile
@kalyco
kalyco / randSelect.py
Created October 26, 2020 14:27
Randomized selection to get k-th rank of a value
import random
def randSelect (array, rank):
print("Looking for value with rank %s in the array:" % rank)
print(array)
i = random.randint(0, len(array)-1)
array, pos = inPlacePartition(array, i)
direction = getDirection(rank, pos)
print("Selected %s as the pivot; its rank is %s; Thus, we recurse on %s" % (array[0], pos, direction))
#include <functional>
#include <iostream>
void print(const int &i)
{
std::cout << i << '\n';
}
int main()
{
@kalyco
kalyco / insertion.js
Created June 23, 2019 21:18
Insertion Sort w JS
var insert = function(array, rightIndex, value) {
var i = 0;
for (var j = 1; array[j] < array[i]; j++) {
var tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
break;
}
for (i = rightIndex; value < array[i] && i >= 0; i--) {
@kalyco
kalyco / binarysearch.js
Created June 12, 2019 09:05
Binary Search
/* Returns either the index of the location in the array,
or -1 if the array did not contain the targetValue */
var doSearch = function(array, targetValue) {
var min = 0;
var max = array.length - 1;
var guess;
var guessCount = 0;
while (max >= min) {
guessCount ++;
guess = Math.floor((min + max)/2);
@kalyco
kalyco / gist:55b8fa3b8ea98b5f013621c682ffa5a2
Created April 24, 2019 18:42
BBox to YOLO Annotation File Converter
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 9 14:55:43 2015
This script is to convert the txt annotation files to appropriate format needed by YOLO
@author: Guanghan Ning
Email: gnxr9@mail.missouri.edu
"""
import os
from os import walk, getcwd
@kalyco
kalyco / InsertionSort.cpp
Created August 5, 2018 18:37
An Insertion Sort
void insertionSort(Array * S, int n) {
if (n <= 1) return; // 1. Base case: Return if last (null) mem point
insertionSort(S, n-1); // 2. recursively go through each element in array, starting with last
int last = S[n-1]; // 3. set current last element
int j = n-2; // 4. Run through each previous element
while (j >= 0 && S[j] > last) { // 5. While that element is bigger than the last element
S[j+1] = S[j]; // 6. Move its location forward in the array.
j--; // 7. Go on to the next element in array.
}
S[j+1] = last; // 8. set last element to the final point in the array.
@kalyco
kalyco / SimpleSplay.cpp
Created August 1, 2018 16:34
Super simple splay execution -- ref stjepang/splay.cpp
// Simple Splay
#include<iostream>
using namespace std;
template<class T>
struct Node {
T value;
Node *l, *r, *p;
#ifndef SPLAY_TREE_H
#define SPLAY_TREE_H
#include <utility>
#include <iostream>
#include "Array.h"
#include "bst.h"
#include <cassert>
using namespace std;
@kalyco
kalyco / rescursive_exponent.cpp
Created July 10, 2018 21:02
Write a recursive algorithm to perform x^n
// If exp is even, split in half twice and call each recursively
// otherwise, split in half twice and call each recursively AND multiply x
// Questions:
// What if exponent is 1? -- Then it will return x*1*1
// What if exponent is 3+? -- If even, split. If odd, multiply next split value to x
int power(int base, int exponent) {
if (exponent == 0) return 1;
else if (exponent%2 == 0) // if even
@kalyco
kalyco / linked_list_cycle
Created July 10, 2018 20:27
Write an algorithm to detect a cycle in a linked list
// Assume a linked list object with mHead, and mTail LLNodes, and mSize for size of list
// If there is a cycle, the linked list would never break out of a while loop.
// So if it exceeds the length of the list, we know there's a cycle somewhere
bool isCycle() {
if(mHead == NULL) return false;
node * n = mHead;
int lSize = mSize;
while(true) {