Skip to content

Instantly share code, notes, and snippets.

@JF-Lalonde
Last active May 2, 2017 03:32
Show Gist options
  • Save JF-Lalonde/d299414b3c9260bf75409dcff15c9b77 to your computer and use it in GitHub Desktop.
Save JF-Lalonde/d299414b3c9260bf75409dcff15c9b77 to your computer and use it in GitHub Desktop.
May 1st Monday night homework JF Lalonde

Working with String and Integers

Just Strings

1. First and Last

  1. Use only the "string concatenation" technique to complete the following:

    • What code can you write to output the string "FirstLast"?

      • puts "First" + "Last"
    • What code can you write to output the string "LastFirst"?

      • puts "Last" + "First"
    • What code can you write to output the string "First Last"?

      • puts "First" + " " + "Last"
    • What code can you write to output the string "Last First Last First"?

      • puts "Last " + "First " + "Last " + "First"
    • Then repeat 1-4 using only the "string interpolation" technique.

    • What code can you write to output the string "FirstLast"?

      • last = "Last"
      • puts "First#{last}"
    • What code can you write to output the string "LastFirst"?

      • first = "First"
      • puts "Last#{first}"
    • What code can you write to output the string "First Last"?

      • first = "First"
      • puts "Last #{first}"
    • What code can you write to output the string "Last First Last First"?

      • first = "First"
      • puts "Last #{first} Last #{first}"

2. Names

  • Can you come up with two ways to output just the fragment "Megan" from name_1?
    • name_1.delete(" Smith")
    • name_1.tr(" Smith","")
  • Would either of your techniques from A would work to output "Todd" from name_2? Why or why not?
    • Neither would work for name_2 because they specifically delete/trim the letters "Smith"
  • Write code that can output the initials of name_2.
    • name_2.tr("odd ark","")

Just Integers

  • Write code to find the average of these four numbers.
    • (a + b + c + d)/4.to_f
  • Find the average yourself using paper or a calculator. Is your answer different than you found in A? Why?
    • 51.5 It's the same answer as in A because I converted the answer to a Float.
  • Say you have the operation a + b * c / d. What result do you get out from Ruby? What other outputs can you get out by adding one or more pairs of parentheses to the equation?
    • 32 You could also get 24 if you put (a + b) in parentheses, or 20 if you do (a + (b * c)) / d

Strings and Integers

People

  • Write code to output both the total characters in all the names together and the average length of the names.
    • (a.length + b.length + c.length + d.length) = 20 total characters
    • (a.length + b.length + c.length + d.length)/4 = 5 average length of names

Happy Birthday

  • Say you have an age variable that holds the person's age. Write code to output the appropriate greeting.
    • ("Happy" * age.to_i) + "Birthday"

String Compression

  • There's a silly compression algorithm that outputs the first letter, the number of letters in the middle, and the last letter. So for the string "Kalamazoo" it'd output "K7o" or "Denver" would be "D4r". Can you write code to implement that?
    • string = "Something"
    • string[0] +string[1..-2].length + string[-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment