Jesse Farber brings his award-winning creative talents and passion for detail to his full-stack software and web development, producing elegant solutions with innovative design.
This Ruby program checks a number with the Luhn algorithm, a method for determining valid and invalid numbers for credit cards and other identification purposes. It is meant to prevent users from accidental errors.
module Luhn
def self.is_valid?(number)
revnumber = number.to_s.reverse
split_revnumber = revnumber.split('').map { |digit| digit.to_i }
split_revnumber.length.times do |n|
if n.odd?
split_revnumber[n] *= 2
if split_revnumber[n] > 9
split_revnumber[n] -= 9
end
end
end
digit_sum = split_revnumber.inject(0){|sum,x| sum + x }
if digit_sum % 10 == 0
puts true
else
puts false
end
end
end
Luhn.is_valid?(4194560385008504)
This Ruby program provides two different methods to find the Fibonacci sequence value for a particular number: one recursive solution, and one iterative solution. It also uses Ruby's Benchmark module to show which method finds the value more efficiently.
class Fib_sequence
def recursive_fib(n)
if n < 2
n
else
recursive_fib(n - 1) + recursive_fib(n - 2)
end
end
def iterative_fib(num)
fib_numbers = [0, 1]
(1..num).each do |n|
if n < 2
fib_numbers[n] = n
else
fib_numbers[n] = fib_numbers[n - 1] + fib_numbers[n - 2]
n += 1
end
end
fib_numbers.last
end
end
f = Fib_sequence.new
num = 35
puts f.recursive_fib(num)
puts f.iterative_fib(num)
Benchmark.bm do |x|
x.report("recursive_fib") { f.recursive_fib(num) }
x.report("iterative_fib") { f.iterative_fib(num) }
end
This Ruby program provides methods for working with a linked list data structure, including building the list and printing the series of values, reversing the node sequence, and using the Floyd's Cycle Detection Algorithm (aka Tortoise and Hare) to check whether the list contains a loop.
class LinkedListNode
attr_accessor :value, :next_node
def initialize(value, next_node=nil)
@value = value
@next_node = next_node
end
end
def print_values(list_node)
if list_node
print "#{list_node.value} --> "
print_values(list_node.next_node)
else
print "nil\n"
return
end
end
def reverse_list(list, previous=nil)
while list
new_list = LinkedListNode.new(list.value, previous)
list = list.next_node
previous = new_list
end
list = new_list
end
def jump_two_spaces(h)
h = h.next_node.next_node
end
def jump_one_space(t)
t = t.next_node
end
def has_infinite_loop?(list)
tortoise = hare = list
while hare && hare.next_node
hare = hare.next_node.next_node
tortoise = tortoise.next_node
return true if hare == tortoise
end
return false
end
Jesse has developed proficiency and expertise in the following programming languages, and experience with the following tools.
Currently entertaining new opportunities. Please get in touch via email: