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
# Takes an array of stock prices, assuming each entry | |
# represents average price on successive days, and | |
# determines what optimal buy and sell dates would've been. | |
def stock_picker(stock_prices_array) | |
hypothesis = [0,0,1] # [Hypothetical profit, buy time, sell time] | |
i = 0 | |
while i < stock_prices_array.length | |
if_buy = 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
# Takes an input string and an integer shift factor, performs | |
# a Caesar cipher. | |
def caesar_cipher(input,shift_factor) | |
message = input.scan(/./) | |
cipher = Array.new | |
for x in message | |
y = x[0].to_i | |
z = y + shift_factor |
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
# Takes string input, dictionary to determines whether any dictionary | |
# entries are present as substrings. | |
def substrings(input,dictionary) | |
input.downcase! | |
result = Hash.new | |
for x in dictionary | |
search = input.scan(x).length | |
result[x] = search unless search < 1 |
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
# Finds the sum of all multiples of 3 or 5 below 1000 | |
sum = 0 | |
i = 1 | |
while i <= 1000 | |
sum += i if i % 3 == 0 || i % 5 == 0 | |
i += 1 | |
end | |
sum |
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
# Considering the terms in the Fibonacci sequence whose values | |
# do not exceed 4 million, find the sum of the even-valued terms. | |
x = 1 | |
y = 2 | |
sum = 2 | |
while x < 4000000 && y < 4000000 | |
x += y | |
sum += x if x % 2 == 0 |
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
# Finds the largest prime factor of a given integer. | |
# Up to 1024 for sake of efficiency. | |
def find_factors(number) | |
factors = Array.new | |
for x in 1..number | |
factors << x if number % x == 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
# Determines whether a given integer is a palindrome | |
print "Integer: " | |
number = gets.chomp.to_s | |
if number.eql? number.reverse | |
print "\nThis number is a palindrome." | |
else | |
print "\nThis number is not a palindrome." | |
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
# Finds the next date when all digits in the date sequence will be unique. | |
require "date" | |
day_test = Date.today | |
while true | |
day_test = day_test.next | |
query = day_test.year.to_s |
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
# Simple bubble sort for numbers | |
def bubble_sort(array_to_sort) | |
swap = true # Track whether swap has occurred | |
while swap | |
(0... array_to_sort.length - 1).each do |x| | |
# If we detect disorder... | |
if array_to_sort[x] > array_to_sort[x + 1] | |
# Perform a simple 3-step swap |
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
# Fibonacci sequence with and without recursion | |
def fibs(num) # Not recursive | |
return num if num <= 1 | |
fib = 1 | |
fib_prev = 1 | |
count = 2 | |
while count < num |
OlderNewer