Skip to content

Instantly share code, notes, and snippets.

View gingeleski's full-sized avatar
📈

Randy Gingeleski gingeleski

📈
View GitHub Profile
@gingeleski
gingeleski / stock_picker.rb
Last active August 29, 2015 14:12
Takes an array of stock prices, assuming each entry represents successive daily averages, and determines what optimal buy and sell dates would've been.
# 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
@gingeleski
gingeleski / caesar_cipher.rb
Last active August 29, 2015 14:12
Takes an input string and an integer shift factor, performs a Caesar cipher.
# 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
@gingeleski
gingeleski / substrings.rb
Created January 4, 2015 07:09
Takes a string and dictionary, determines whether any dictionary entries are present as substrings.
# 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
@gingeleski
gingeleski / multiples_3_5.rb
Last active August 29, 2015 14:12
Finds the sum of all multiples of 3 or 5 below 1000.
# 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
@gingeleski
gingeleski / even_fibonacci_sum.rb
Created January 6, 2015 06:30
Finds the sum of all even-valued terms in the Fibonacci sequence that do not exceed 4 million.
# 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
@gingeleski
gingeleski / largest_prime_1024.rb
Created January 6, 2015 07:09
Finds the largest prime factor of a given integer, up to 1024.
# 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
@gingeleski
gingeleski / palindrome_number.rb
Created January 7, 2015 04:34
Determines whether a given integer is a palindrome.
# 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
@gingeleski
gingeleski / unique_date.rb
Created January 22, 2015 21:26
Finds the next date when all digits in the date sequence will be unique.
# 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
@gingeleski
gingeleski / bubble_sort.rb
Created January 26, 2015 00:25
Simple bubble sort for numbers.
# 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
@gingeleski
gingeleski / fibonacci.rb
Created February 8, 2015 23:21
Fibonacci sequence with and without recursion.
# 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