Skip to content

Instantly share code, notes, and snippets.

@phillipoertel
Last active April 26, 2016 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipoertel/a29e8d38f0bba8fe71c80b0bc6996443 to your computer and use it in GitHub Desktop.
Save phillipoertel/a29e8d38f0bba8fe71c80b0bc6996443 to your computer and use it in GitHub Desktop.
test.rb
def hr
puts
puts '-' * 80
puts
end
# Number comparsion
def number_comparison(a, b)
difference = a - b
puts "#{a} is #{difference} larger than #{b}"
end
number_comparison(5, 1)
number_comparison(21, 5)
hr
# Array sum
def array_sum(array)
sum = 0
array.each { |nr| sum = sum + nr}
puts sum
# also: array.inject(:+)
end
array_sum([1, 2])
array_sum([5, 1, 0])
hr
# Array with steps
# 5 Min
#
def array_with_steps(step_size, array_length)
out = []
last = 0
array_length.times do
out << (last + step_size)
last = out.last
end
out
end
p array_with_steps(1, 3)
p array_with_steps(1, 5)
p array_with_steps(3, 4)
hr
# also
def array_with_steps(step_size, array_length)
Array.new(array_length) { |i| (i + 1) * step_size }
end
p array_with_steps(1, 3)
p array_with_steps(1, 5)
p array_with_steps(3, 4)
hr
# Ordinal ordinal(1) outputs the string "1st"
# 5
def ordinal(nr)
suffix = if (nr == 1 || nr == 21)
"st"
elsif (nr == 2 || nr == 22)
"nd"
elsif (nr == 3 || nr == 23)
"rd"
else
"th"
end
"#{nr}#{suffix}"
end
(1..25).each { |nr| puts ordinal(nr) }
hr
def expenses(e1, e2)
sum_felix = e1.inject(:+)
sum_stefan = e2.inject(:+)
if (sum_stefan > sum_felix)
puts "Felix owes Stefan #{(sum_stefan - sum_felix) / 2} Euros"
else
puts "Stefan owes Felix #{(sum_felix - sum_stefan) / 2} Euros"
end
end
felix = [210, 22, 45]
stefan = [120, 50, 87]
expenses(felix, stefan)
hr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment