Skip to content

Instantly share code, notes, and snippets.

@vnprc
vnprc / spiral.py
Created December 28, 2014 00:24
This is a programming exercise to print a spiral array of numbers to the screen. The spiral starts with 0 in the middle (or upper left of the middle square) and iterates in concentric square spiral pattern in a clockwise direction.
"""
This is a programming exercise to print a spiral array of numbers to
the screen. The spiral starts with 0 in the middle (or upper left of
the middle square) and iterates in concentric square spiral pattern in
a clockwise direction.
"""
import sys
import math
@vnprc
vnprc / hanoi.py
Created December 28, 2014 00:34
This is a programming exercise to solve the towers of hanoi puzzle. The physical puzzle has three pegs in a row. On the first peg is a stack of disks of descending diameter. You are only allowed to put a smaller disk on top of a larger disk, never the reverse. The task is to move the entire pile from the far left peg, to the far right peg. This …
"""
This is a programming exercise to solve the towers of hanoi puzzle.
The physical puzzle has three pegs in a row. On the first peg is a
stack of disks of descending diameter. You are only allowed to put
a smaller disk on top of a larger disk, never the reverse. The task
is to move the entire pile from the far left peg, to the far right
peg.
This script accepts one input argument, n, for the number of disks.
"""
@vnprc
vnprc / permutations.py
Created December 28, 2014 00:44
Programming exercise to print all permutations of an input strings. Contains two implementations, one with duplicates (in the case of repeated characters), and one without.
"""
Programming exercise to print all the permutations of an input string.
"""
import sys
def get_permutations(string):
permutations = []
if len(string) == 1:
@vnprc
vnprc / calculate_change.py
Created December 29, 2014 04:52
Given a list of change denominations and a target amount of money, calculate the number of ways to make that amount of change.
"""
Given a list of change denominations and a target amount of money,
calculate the number of ways to make that amount of change.
"""
def calculate_variations(total, denomination):
"""
return all possible sequences of denomination whose sum is <= desired_sum
"""
list = []
@vnprc
vnprc / DigitCount.java
Last active August 29, 2015 14:13
Generate a list of random doubles. For each double, take the first digit after the decimal and count how many times that digit appears in the double. Sum the counts and print the results.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeSet;
/**
* This is a programming exercise to generate a series of random doubles between
* 0.0 and 1.0 and return an ordered list of the count of the first digit after
@vnprc
vnprc / hide_show_term.sh
Last active December 24, 2022 14:51
Hide, show, open terminal
#!/bin/bash
pids="$(pidof gnome-terminal-server)"
if [ -z "$pids" ]
then
gnome-terminal
else
focus_window_pid="$(xdotool getwindowfocus getwindowpid)"
if [[ $pids == *"$focus_window_pid"* ]]
@vnprc
vnprc / max_val_contig_subsequence.py
Created September 30, 2016 03:58
find a subsequence of a given sequence with the greatest sum
test = [5, -6, 7, 12, -3, 0, -11, -6]
def get_max_contiguous_sum(array):
results = []
previous_element = 0
for element in array:
previous_element = max(element, previous_element + element)
results.append(previous_element)
return max(results)
@vnprc
vnprc / Tree.java
Created October 13, 2016 02:56
Two search algorithms for a tree, three implementations
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
public class Tree {
@vnprc
vnprc / TopologicalSort.java
Last active October 20, 2016 09:56
An implementation of Kahn's topographical sort algorithm
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
public class TopologicalSort {
@vnprc
vnprc / FindRightNeighbor.java
Last active October 15, 2016 04:16
Given a binary tree, write a method to find the nearest neighbor to the right of a given node.
public class FindRightNeighbor {
Node root = null;
public void insert(Node node) {
if (root == null) {
root = node;
return;
}