Homework for 2B

Review

Topics Covered: creating, updating, iterating over arrays (lists); same for hashes (dictionaries); symbols; basic methods; options hash for methods


Exercises

E1: Reverse

Write a program that prints out the array ["apples", "bananas", "carrots", 4] in reverse. Don't just flip the array in the code.

$ ruby reverse.rb
4
carrots
bananas
apples

E2: Optimization

Reduce tweet_analyzer.rb as much as you can. Don't sacrifice clarity, however. How clear and concise can you make it? Is a different algorithm better?

E3: Hashes and lists, oh my!

While we've primarily seen examples of hashes mapping strings to integers or vice versa, the values can actually be anything (like array, or even other hashes). Create a hash (in ingredients.rb) that maps a dish name (i.e. "Hash browns") to a list of necessary ingredients (i.e. "Potatoes", "Butter", "...is there anything else?"). Add at least 3 dishes. Each dish should have at least 3 ingredients.

Hash on Hash on Hash

Create a new file recipes.rb. This time, map each dish name to another hash. That hash should have keys :description (which maps to a String), :ingredients (which maps to an array of Strings, like above, and :steps (which maps to an array of Strings that, you guessed it, tell you how to make the dish).

If you finish early, cook one of those dishes for your TA.

In code, formatting is important. If you're making a nested object, use a clear structure:

Notation

When describing complex objects, it's nice to have a way to describe them. If I were to describe a hash that maps a symbol to a string, for example, I would write Hash<Symbol, String>. For nested structures: Hash<Symbol, Array<String>> (a hash that maps a symbol to an array of strings). Write down the signatures for:

  1. The hash in ingredients.rb
  2. A hash that maps hashes to an array of integers
  3. An array of hashes that map arrays of strings to symbols
  4. The hash in the yellow box above (can it be done? explain why).

E4: Build a List

In d2, create a program that asks the user for items. Add each item to a list, and print out the whole list after each item.

$ ruby list_builder.rb
Welcome to list builder!
What can I add?
yummy things
Added! Your list is:
["yummy things"]
What can I add?
icky things
Added! Your list is:
["yummy things", "icky things"]

To keep a program running forever, put your code in a while true loop. To exit such a program, press Ctrl-c in the shell.

E5: An Extensive Listbuilder

Create an extensive list builder, that allows you to add items with a command like add x, remove items with remove x, and quit with quit

$ ruby ext_list_builder.rb
Welcome to list builder++!
What can I do for you?
add a
Added! Your list is:
["a"]
What can I do for you?
add b
Added! Your list is:
["a", "b"]
remove a
Removed! Your list is:
["b"]
quit
Bye!

This is the pinnacle—it'll require knowledge from all the exercises up until this point! Since the program will be long, you may want to sketch out a solution before you start coding—remember what we said about planning. How do you keep track of whether you're done? What if the user makes a typo, tries a different command, or tries to remove something not in the list? Can they they see the list of available commands somehow?
After you come up with a solution, try to stress test, and check for edge cases (not typing anything, typing many words, etc.). Then ask one of the TA's to stress test for you and find any bugs!

E6: A List Counter

Extend your program to use a Hash instead of an Array, and keep track of how many of each item the user has added.

$ ruby container.rb
Welcome to container builder!
What can I do for you?
add a
Added! Your container has:
{"a"=>1}
What can I do for you?
add b
Added! Your container has:
{"a"=>1, "b"=>1}
What can I do for you?
add a
Added! Your container has:
{"a"=>2, "b"=>1}
remove a
Removed! Your container has:
{"a"=>1, "b"=>1}
quit
Bye!

E7: Google Map

Translate the following code to use map.

# map.rb

result = []
engines = ["Google", "Bing", "Ask Jeeves"]
engines.each do |e|
  if e == "Google"
    result.push("OK")
  elsif e == "Bing"
    result.push("Bad!")
  else
    result.push("What is that?")
  end
end

result
# => ["OK", "Bad!", "What is that?"]

E8 (Bonus!): Rubeque

rubeque.com is a great site for simple Ruby puzzles. Try some out. To get you started:


Projects

A Coder's Selfie

The final push! You'll be briefly presenting your website to the class tomorrow, so give it your all. This is what The Bru came up with, in less than 100 lines of HTML and 100 lines of CSS.