This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 52-Card Perfect Shuffles - Numberphile | |
# https://www.youtube.com/watch?v=Y2lXsxmBx7E | |
module Magic | |
class Sys | |
class << self | |
def ask | |
print "I choose: " | |
gets.chomp | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to Win a Guessing Game - Numberphile | |
# Based on https://www.youtube.com/watch?v=ud_frfkt1t0 | |
class WinGuessing | |
def initialize(total_iterations, max_random_num) | |
@total_times = total_iterations | |
@max_random_num = max_random_num | |
@max_length = @max_random_num.to_s.length | |
@total_result = 0 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def findIndex(values, target) | |
values.each_with_index do |value, i| | |
return i if value == target | |
end | |
end | |
findIndex([4, 8, 15, 16, 23, 42], 15) | |
# => 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedList | |
def initialize | |
@head = @tail = nil | |
end | |
def add(value) | |
node = Node.new(value) | |
@head = node if @head.nil? | |
@tail.next = node unless @tail.nil? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def removeAt(index) | |
prev = nil | |
node = @head | |
i = 0 | |
loop do | |
prev = node | |
node = node.next | |
i += 1 | |
break unless !node.nil? and i < index | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HashTable | |
def initialize(size) | |
@size = size | |
@buckets = Array.new(@size) | |
end | |
def add(value) | |
index = hash(value) | |
@buckets[index] = value | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedList | |
#... | |
end | |
class Node | |
#... | |
end | |
class HashTable | |
def initialize(size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def findIndex(values, target) | |
binarySearch(values, target, 0, values.size - 1) | |
end | |
def binarySearch(values, target, start, finish) | |
return -1 if start > finish | |
middle = ((start+finish)/2).floor | |
value = values[middle] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sort(values) | |
length = values.size - 2 | |
swapped = true | |
while swapped | |
swapped = false | |
0.upto(length) do |i| | |
if values[i] > values[i+1] | |
values[i], values[i+1] = values[i+1], values[i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sort(values) | |
length = values.size - 1 | |
1.upto(length) do |i| | |
temp = values[i] | |
j = i - 1 | |
while j >= 0 and values[j] > temp | |
values[j+1] = values[j] | |
j -= 1 |
OlderNewer