Skip to content

Instantly share code, notes, and snippets.

@addie
addie / girlFraction
Last active December 17, 2015 14:59
Fraction of the population that are girls if every couple keeps having children until they have a boy
# http://www.thebigquestions.com/2010/12/21/are-you-smarter-than-google/
# There’s a certain country where everybody wants to have a son.
# Therefore each couple keeps having children until they have a boy;
# then they stop. What fraction of the population is female? (30.7%)
# Probability Configuration
# 1/2 B
# 1/4 GB
# 1/8 GGB
@addie
addie / array_mem
Created April 16, 2014 21:22
Playing with c
/*Write a program Array, that accepts arguments from the command line (argc, *argv[ ])as follows: (20%)
➢ Array Type Size InitValue
o The above program will create an array on the heap of type (Type: float, int, chars, etc) having a size (Size).
o After creating the array on the heap, the user should initialize the array with the entry values having initial value of InitValue to InitValue + Size
o Print out the total number of array elements in a block type format (i.e. 8 values per line)
o Deallocates the memory from the heap
Demonstrate that the following Use Cases:
Array float 32 1
Plugboard = Hash[*('A'..'Z').to_a.shuffle.first(20)]
Plugboard.merge!(Plugboard.invert)
Plugboard.default_proc = proc { |hash, key| key }
def build_a_rotor
Hash[('A'..'Z').zip(('A'..'Z').to_a.shuffle)]
end
Rotor_1, Rotor_2, Rotor_3 = build_a_rotor, build_a_rotor, build_a_rotor
@addie
addie / algorithms.rb
Last active August 29, 2015 14:27
Collection of Algorithms in Ruby
def insertion_sort(array)
array.each do |i|
value = array[i]
j = i - 1
while j >= 0 and array[j] > value
array[j+1] = array[j]
j -= 1
end
array[j+1] = value
end
@addie
addie / linkedlist.py
Created October 20, 2015 19:31
Python linked list
class Node(object):
def __init__(self, data=None):
self.data = data
self.next = None
def __repr__(self):
return str(self.data)
class SinglyLinkedList(object):
def __init__(self, iterable=[]):
@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="")
@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
// ==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 */
"""
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
"""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