Skip to content

Instantly share code, notes, and snippets.

@trueskawka
Last active July 6, 2016 17:26
Show Gist options
  • Save trueskawka/39d00f67173df34475c61cf6ec749140 to your computer and use it in GitHub Desktop.
Save trueskawka/39d00f67173df34475c61cf6ec749140 to your computer and use it in GitHub Desktop.
Ragnarson 2016 - Day 1
tab = []
10.times { tab << rand(36) + 1 }
#max
max = tab[0]
tab.each do |x|
max = x if x > max
end
puts "tab: #{tab}"
puts "max: #{max}"
#min
min = tab[0]
tab.each do |x|
min = x if x < min
end
puts "tab: #{tab}"
puts "min: #{min}"
#random
random = tab[rand(tab.size - 1)]
puts "tab: #{tab}"
puts "random: #{random}"
#sum and average
sum = tab[0]
tab.each do |x|
sum += x
end
avg = sum / tab.size
puts "tab: #{tab}"
puts "sum: #{sum}"
puts "avg: #{avg}"
#factorial
a = rand(10) + 1
puts "a: #{a}"
fact = a - 1
while fact > 1 do
a *= fact
fact -= 1
end
puts "factorial: #{a}"
#bubble sort
puts "table: #{tab}"
n = tab.size
swapped = true
while swapped do
swapped = false
for i in 0..(n - 2)
if tab[i] > tab[i + 1]
tab[i], tab[i + 1] = tab[i + 1], tab[i]
swapped = true
end
end
end
puts "sorted: #{tab}"
#primes
tab2 = (2..100).to_a
tab2.each do |x|
tab2.select! { |i| i % x != 0 }
end
puts "primes: #{tab2}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment