Skip to content

Instantly share code, notes, and snippets.

@taylorruizchiu
Created April 8, 2014 05:52
Show Gist options
  • Save taylorruizchiu/10095338 to your computer and use it in GitHub Desktop.
Save taylorruizchiu/10095338 to your computer and use it in GitHub Desktop.
Week 1, Day 1 HW
#TEMPERATURE CONVERTER
puts "Type 1 to convert from Celsius to Fahrenheit"
puts "Type 2 to convert from Fahrenheit to Celsius"
convertor = gets.chomp
if convertor == "1"
puts "Enter temperature in Celsius: "
temp1 = gets.chomp.to_i
temp1 = ((temp1 * 9) / 5) + 32
puts "The temperature in Fahrenheit is: #{temp1}"
elsif convertor == "2"
puts "Enter temperature in Fahrenheit: "
temp2 = gets.chomp.to_i
temp2 = ((temp2 - 32) * 5) / 9
puts "The temperature in Celsius is: #{temp2}"
end
###########################################################
#GUESSING GAME
i = rand(1..100)
puts "Guess a number between 1 and 100: "
tries = 0
guess = gets.chomp.to_i
until i == guess
if i > guess
puts "The number is higher than #{guess}. Guess again: "
elsif i < guess
puts "The number is lower than #{guess}. Guess again: "
else i == guess
puts "You guessed it in #{tries + 1} tries!"
end
guess = gets.chomp.to_i
tries += 1
end
puts "Congrats! You guessed it in #{tries + 1}!"
#########################################################
#SIMPLE ASCII ART
puts "How many rows do you want your pyramid to have? (up to 100) "
n = gets.chomp.to_i
rows = 0
stars = 1
spaces = (100 - stars) / 2
until n == rows
puts (" " * spaces) + ("*" * stars)
stars += 2
spaces -= 1
rows += 1
end
#########################################################
# REVERSE A STRING, IN PLACE, without using .reverse (use loops!)
puts "Enter a string: "
string = gets.chomp
loop = string.length
word = ''
while loop > 0
loop -= 1
word += string[loop]
end
puts word
#########################################################
# MULTIPLICATION TABLE
# 9 x 9 multiplication table, with even spacing
puts "A multiplication table: "
puts
for row_number in 1..9
line = "#{row_number}| "
for column_number in 1..9
line += "#{row_number * column_number}\t"
end
puts line
end
#this explains it! http://ruby.bastardsbook.com/chapters/loops/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment