Skip to content

Instantly share code, notes, and snippets.

/*
* Binary search is an efficient, O(log n) search on a sorted array
* which works by eliminating half of the search area for each comparison
* between the current middle element and the search target.
*/
public class BinarySearch {
/**
* Performs a binary search
*
<?php
if (isset($_GET['source'])) {
highlight_file($_SERVER['SCRIPT_FILENAME']);
exit;
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
/* Radix sort works on a positive array of numbers,
* sorting them for each place value in the longest
* number in that array. At each place value, starting from the
* right, all numbers in the array are placed into one of 10
* "buckets" which correspond to each number in the decimal
* system. After all numbers are sorted for a value place,
* the buckets are emptied back into the original array,
* and the process is repeated for each remaining value place.
*/
/* comb sort works like a bubble sort, but adds a gap to each pass
* to help neutralize the inefficiencies of small values at the end
* of the unsorted list, called "turtles". the iteration ends when
* no elements were swapped during the last comb through the list.
* efficiency: O(n^2)
*/
function combSort(arr) {
// save array length in a variable
<?php
/* quicksort sorts an array by partitioning the array
* into elements greater than a pivot point and elements
* less than or equal to that pivot point. the pivot point
* is now in its final, sorted location by definition.
* then, recursively perform the same operation on each half
* of the array.
* efficiency: O(n log n)
*/
/* Shell sort is basically an insertion sort
* with variable gap (interval) between numbers
* that it sorts in a given pass. By beginning
* with large gaps between indexes, rough sorts
* are performed, making the final insertion sort
* (gap === 1) more efficient than usual.
* Efficiency: O(n^1.5)
*/
function shellSort(arr, gap) {
<?php
if (isset($_GET['source'])) {
highlight_file($_SERVER['SCRIPT_FILENAME']);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
@ggorlen
ggorlen / potions.rb
Created March 20, 2018 15:05
solution to an excercise from Computer Science Programming Basics with Ruby by Frieder, Frider and Grossman
# https://stackoverflow.com/questions/26440997/optimizing-a-potion-brewing-program-using-ingredients-and-formulaic-conversions
#
# "The three witches in Hamlet can brew any potion provided they have the right ingredients.
# Suppose that five ingredients are necessary in making a health potion => eye of newt (eon),
# toe of frog (tof), wool of bat (wob), adder's fork (af), and tooth of wolf (tow). Four
# reactions can occur between these ingredients:
#
# 4 eon + 2 wob = 3 af + 4 tow
# 3 tow + 1 tof = 2 eon
# 1 wob + 2 af = 1 tof
"""
Script to download "shelf" lists from the SFPL Bibliocommons website
http://stackoverflow.com/questions/189555/how-to-use-python-to-login-to-a-webpage-and-retrieve-cookies-for-later-usage
http://stackoverflow.com/questions/14630288/unicodeencodeerror-charmap-codec-cant-encode-character-maps-to-undefined
"""
import datetime
import sys
from bs4 import BeautifulSoup
from requests import session
@ggorlen
ggorlen / transcribe.py
Created March 24, 2019 23:08
Transcribe speech using the Google Speech Recognition API
"""
Transcribe speech using the Google Speech Recognition API
"""
import math
import speech_recognition
import sys
if len(sys.argv) < 2: