Skip to content

Instantly share code, notes, and snippets.

@addie
addie / nearest_point_on_line_segment_to_point.go
Created August 19, 2019 14:29
Returns the point on a line segment nearest to a given point
// NearestPointOnLineSegmentToPoint returns the point on a segment closest to a given point.
// |------------- o ------|
// P1 | P2
// .P3
// The equation of a line defined through two points P1 (x1,y1) and P2 (x2,y2) is P = P1 + u (P2 - P1)
// The point P3 (x3,y3) is closest to the line at the tangent to the line which passes through P3,
// that is, the dot product of the tangent and line is 0, thus (P3 - P) dot (P2 - P1) = 0
// Substituting the equation of the line gives [P3 - P1 - u(P2 - P1)] dot (P2 - P1) = 0
// Solving this gives the value of u:
@addie
addie / stdc++.h
Created November 30, 2018 03:23 — forked from velicast/stdc++.h
Linux GCC 4.8.0 /bits/stdc++.h header definition.
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
@addie
addie / even_odd_split.py
Last active January 26, 2016 02:42
Splits a linked list of positive integers into two lists, one of even and one of odd integers. T:O(n) S:O(1)
class Node:
def __init__(self, value=None, next=None):
self.value = value
self.next = next
def print_list(head):
while head:
print(head.value, end='->')
head = head.next
@addie
addie / print_selected_paths.py
Last active January 24, 2016 16:17
Recursively prints every path from root to leaf in a binary search tree that meets a given condition
# coding: utf-8
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def __str__(self):
return str(self.value)
@addie
addie / print_closest.py
Last active January 24, 2016 16:17
Prints the k closest nodes to target t in a binary search tree
# coding: utf-8
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def __str__(self):
return str(self.value)
"""Zombit infection
================
Dr. Boolean continues to perform diabolical studies on your fellow rabbit kin, and not all of it is taking place in the lab. Reports say the mad doctor has his eye on infecting a rabbit in a local village with a virus that transforms rabbits into zombits (zombie-rabbits)!
Professor Boolean is confident in the virus's ability to spread, and he will only infect a single rabbit. Unfortunately, you and your fellow resistance agents have no idea which rabbit will be targeted. You've been asked to predict how the infection would spread if uncontained, so you decide to create a simulation experiment. In this simulation, the rabbit that Dr. Boolean will initially infect will be called "Patient Z".
So far, the lab experts have discovered that all rabbits contain a property they call "Resistance", which is capable of fighting against the infection. The virus has a particular "Strength" which Dr. Boolean needs to make at least as large as the rabbits' Resistance for it to infect t
"""
Name that rabbit
"You forgot to give Professor Boolean's favorite rabbit specimen a name? You know how picky the professor is! Only particular names will do! Fix this immediately, before you're... eliminated!"
Luckily, your minion friend has already come up with a list of possible names, and we all know that the professor has always had a thing for names with lots of letters near the 'tail end' of the alphabet, so to speak. You realize that if you assign the value 1 to the letter A, 2 to B, and so on up to 26 for Z, and add up the values for all of the letters, the names with the highest total values will be the professor's favorites. For example, the name Annie has value 1 + 14 + 14 + 9 + 5 = 43, while the name Earz, though shorter, has value 5 + 1 + 18 + 26 = 50.
If two names have the same value, Professor Boolean prefers the lexicographically larger name. For example, if the names were AL (value 13) and CJ (value 13), he prefers CJ.
Write a function answer(names) which takes a list of names and retu
// ==UserScript==
// @name HackerRank Copy/Paste
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Allow copy/paste on HackerRank
// @author Rinoc Johnson
// @include https://www.hackerrank.com/tests/*
// @grant none
// ==/UserScript==
/* jshint -W097 */
@addie
addie / trie.py
Created December 17, 2015 07:33
This is my naive implementation of a Trie data structure using an array to store the children
#!/usr/bin/env python3
class TrieNode:
""" Node storing character and array of children
nodes and is_terminal boolean """
def __init__(self, char=None, children=None, is_terminal=False):
self.char = char
self.children = [0 for x in range(128)]
self.is_terminal = is_terminal
@addie
addie / N_Queens.py
Created December 6, 2015 08:10
Solution to the N-Queens problem
# Three pretty print functions
def print_row(length):
for i in range(length):
print('+-', end="")
print('+')
def print_cell(array, length):
for i in range(length):
if i == array[0]:
print('|*', end="")