Home - nortonbino/sample_app GitHub Wiki

Exercises Cap 4

4.2.2

Ex 1) Assign variables city and state to your current city and state of residence. (If residing outside the U.S., substitute the analogous quantities.)

irb(main):005:0> city = "Guarapuava"
=> "Guarapuava"`

`irb(main):006:0> state = "Parana"
=> "Parana"

Ex 2) Using interpolation, print (using puts) a string consisting of the city and state separated by a comma and as space, as in “Los Angeles, CA”.

irb(main):007:0> puts city + ", " + state
Guarapuava, Parana

Ex 3) Repeat the previous exercise but with the city and state separated by a tab character.

irb(main):010:0> puts city + " " + state
Guarapuava Parana

Ex 4) What is the result if you replace double quotes with single quotes in the previous exercise?

irb(main):011:0> puts city + ' ' + state
Guarapuava Parana

4.2.3

Ex 1) What is the length of the string “racecar”?

irb(main):013:0> "racecar".length
=> 7

Ex 2) Confirm using the reverse method that the string in the previous exercise is the same when its letters are reversed.

irb(main):014:0> "racecar".reverse.length
=> 7

Ex 3) Assign the string “racecar” to the variable s. Confirm using the comparison operator == that s and s.reverse are equal.

irb(main):017:0> s = "racecar"
=> "racecar"
irb(main):018:0> s == s.reverse
=> true

Ex 4) What is the result of running the code shown in Listing 4.9? How does it change if you reassign the variable s to the string “onomatopoeia”? Hint: Use up-arrow to retrieve and edit previous commands

irb(main):019:0> puts "It's a palindrome!" if s == s.reverse
It's a palindrome!
irb(main):020:0> s = "onomatopoeia"
=> "onomatopoeia"
irb(main):021:0> puts "It's a palindrome!" if s == s.reverse
=> nil

4.2.4

Ex 1) By replacing FILL_IN with the appropriate comparison test shown in Listing 4.10, define a method for testing palindromes. Hint: Use the comparison shown in Listing 4.9.

irb(main):018:0> palindrome_tester s
It's not a palindrome.
=> nil
irb(main):019:0> 

Ex 2) By running your palindrome tester on “racecar” and “onomatopoeia”, confirm that the first is a palindrome and the second isn’t.

irb(main):023:0> s = "racecar"
=> "racecar"
irb(main):024:0> palindrome_tester s
It's a palindrome!

irb(main):026:0> s = "onomatopoeia"
=> "onomatopoeia"
irb(main):027:0> palindrome_tester s
It's not a palindrome.

Ex 3) By calling the nil? method on palindrome_tester("racecar"), confirm that its return value is nil (i.e., calling nil? on the result of the method should return true). This is because the code in Listing 4.10 prints its responses instead of returning them.

irb(main):028:0> palindrome_tester("racecar").nil?
It's not a palindrome.
=> true

4.3.1

Ex 1) Assign a to be to the result of splitting the string “A man, a plan, a canal, Panama” on comma-space.

irb(main):031:0> a = "A man, a plan, a canal, Panama".split
=> ["A", "man,", "a", "plan,", "a", "canal,", "Panama"]

Ex 2) Assign s to the string resulting from joining a on nothing.

irb(main):035:0> s = a.join("")
=> "Aman,aplan,acanal,Panama"

Ex 3) Split s on whitespace and rejoin on nothing. Use the palindrome test from Listing 4.10 to confirm that the resulting string s is not a palindrome by the current definition. Using the downcase method, show that s.downcase is a palindrome.

irb(main):035:0> s = a.join("")
=> "Aman,aplan,acanal,Panama"

4.3.2

Ex 1) Using the range 0..16, print out the first 17 powers of 2.

irb(main):036:0> (0..16).each do |number|
irb(main):037:1* puts number * number
irb(main):038:1> end
0
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
=> 0..16

Ex 2) Define a method called yeller that takes in an array of characters and returns a string with an ALLCAPS version of the input. Verify that yeller([’o’, ’l’, ’d’]) returns "OLD". Hint: Combine map, upcase, and join.

irb(main):039:0> def yeller(x)
irb(main):040:1> yel = x.map do |a|
irb(main):041:2* a.upcase
irb(main):042:2> end
irb(main):043:1> yel.join
irb(main):044:1> end
=> :yeller
irb(main):045:0> yeller(['o','l','d'])
=> "OLD"

Ex 3) Define a method called random_subdomain that returns a randomly generated string of eight letters.

irb(main):046:0> def random_subdomain
irb(main):047:1> (0...8).map { ('a'..'z').to_a[rand(26)] }.join
irb(main):048:1> end
=> :random_subdomain
irb(main):049:0> puts random_subdomain
azohnldo