Created
August 17, 2013 01:44
-
-
Save mattdvhope/6254850 to your computer and use it in GitHub Desktop.
Exercises 9, 10, 11 on Assessment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# EXERCISE 9 | |
# My plain English answer: | |
# 1. Create a method for sorting the numbers in order from 1 to 10,000 (w/o the missing number). | |
# 2. Create a code block for comparing the value each successive number with its previous number. | |
# If their difference is greater than 1, then I'll subtract the previous number from the subsequent number. Then I'll add 1 to that previous number: that sum equals/is the missing number. | |
# Output the missing number. | |
# CODE... | |
def find_missing(no_order) | |
ordered_missing = no_order.sort | |
ordered_include = [*(1..10000)] | |
# sum_missing | |
sum_m = 0 | |
ordered_missing.each { |num| sum_m += num } | |
# sum_include | |
sum_i = 0 | |
ordered_include.each { |num| sum_i += num } | |
missing_number = sum_i - sum_m | |
end | |
# EXERCISE 10 | |
# My pseudo code solution for solving this problem... | |
# 1. I need to make a long list of grocery items--several of which will be repeated. | |
# 2. I need to tabulate each item (get the number of each individual item). | |
# 3. I need to write a left column of each grocery item and a right column of the number of each corresponding item on the list. | |
# Write out in Ruby code your solution for solving this problem. | |
def groceries(item) | |
puts "Grocery List".center(30) | |
incremented = Hash.new(0) | |
item.each do |item_type| | |
incremented[item_type] += 1 | |
end | |
incremented.each do |item_incrd, num| | |
puts item_incrd.ljust(20) + num.to_s.rjust(10) | |
end | |
end | |
items_list = ["pasta box", "coffee packet", "cereal box", "oatmeal box", "milk carton", "coffee packet", "egg carton", "pasta box", "loaf of bread", "coffee packet", "milk carton", "oatmeal box", "pasta box", "coffee packet", "cereal box", "loaf of bread", "egg carton", "coffee packet", "pasta box", "oatmeal box", "cereal box", "loaf of bread", "coffee packet",] | |
groceries(items_list) | |
# EXERCISE 11 | |
class House | |
def initialize(state) | |
@state = state | |
end | |
def update_temperature!(appliance) # causes the increase/decrease in temp | |
current_temp = @state | |
while true | |
if appliance == 'heater' | |
current_temp = current_temp + 1 | |
elsif appliance == 'air' | |
current_temp = current_temp - 2 | |
end | |
p "After running for some time, the temperature is now #{current_temp}." | |
p "Do you want the #{appliance} on or off now (type \'on\' or \'off\')?" | |
while true | |
switch = gets.chomp.downcase | |
if (switch=='on' || switch=='off') | |
break | |
else | |
p "Type \'on\' or \'off\'." | |
end | |
end | |
if switch=='off' | |
break | |
else | |
true | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment