Skip to content

Instantly share code, notes, and snippets.

@dbernheisel
Last active August 31, 2015 19:13
Show Gist options
  • Select an option

  • Save dbernheisel/be8e4d934cb8629007f5 to your computer and use it in GitHub Desktop.

Select an option

Save dbernheisel/be8e4d934cb8629007f5 to your computer and use it in GitHub Desktop.
Iron Yard Homework, Week 1
#!/usr/bin/env ruby
# Need the ability to determine if input text is really just a number. This
# will return a simple true/false
def is_number?(f)
begin
Float(f)
return true
rescue
return false
end
end
# Because a lot of white text causes eyes to bleed, I want the ability to color
# text that I'm printing
class String
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
end
# Setup infinite loop that will end when the user enters nothing and hits return.
# This loop will also organize input into two separate arrays so we can mess
# with them later.
puts "Enter text or numbers, and I will statistic them. Press enter when you're finished."
numbers = []
strings = []
while true
print "Input: ".colorize(36)
s = gets.chomp!
if s == ""
puts "\e[A\e[K" #clear the last input line so I don't have a hanging input
break
end
is_number?(s) ? numbers.push(s.to_f) : strings.push(s)
# and why should I limit it to one 'mode' versus accepting both?
if numbers.length > 0 && strings.length > 0
puts "Don't mix strings and numbers. Please relaunch.".colorize(101)
exit
end
end
#puts "Numbers: #{numbers}"
#puts "Strings: #{strings}"
if numbers.length > 0
puts ""
puts "Number Statistics".colorize(31)
puts "==================================".colorize(31)
# Determine the sum of the numbers
sumnum = 0.0
numbers.each { |num| sumnum += num }
print "Sum of numbers: ".colorize(33)
puts "#{sumnum}"
# Determine the average of the numbers
averagenum = (sumnum / (numbers.length))
print "Average of numbers: ".colorize(33)
puts "#{averagenum}"
# Determine the standard deviation of the numbers
standarddev = 0.0
variances = []
numbers.each { |n| variances.push((n - averagenum)**2.0) }
variancessum = 0.0
variances.each { |n| variancessum += n }
#puts "Variances: #{variances}"
#puts "Variance Sum: #{variancessum}"
variance = variancessum / variances.length
standarddev = Math.sqrt(variance)
print "Population standard deviation: ".colorize(33)
puts "#{standarddev}"
# Determine the median number
numbers.sort!
print "Median number: ".colorize(33)
if numbers.length % 2 == 1
mediannum = numbers[(numbers.length)/2]
else
mediannum = (numbers[(numbers.length-1)/2] + numbers[(numbers.length+1)/2]) / 2
end
puts "#{mediannum}"
end
if strings.length > 0
puts ""
puts "String Statistics".colorize(31)
puts "==================================".colorize(31)
# Concatenate the strings into a long string.
bigassstring = ""
strings.each { |s| bigassstring.concat(s) }
print "Concatenated string: ".colorize(33)
puts "#{bigassstring}"
# Determine the sum length of all strings.
sumlength = 0
strings.each { |s| sumlength += s.length }
avglength = sumlength / strings.length.to_f
print "Average length of strings: ".colorize(33)
puts "#{avglength}"
# Determine how many times the character e is used in the strings.
ecount = 0
strings.each { |s| ecount += s.downcase.count('e') }
print "Number of times 'e' was used in all strings: ".colorize(33)
puts "#{ecount}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment