Skip to content

Latest commit

 

History

History
80 lines (54 loc) · 2.24 KB

working_with_strings_and_integers.markdown

File metadata and controls

80 lines (54 loc) · 2.24 KB

Working with Strings and Integers

Complete the following exercises to solidify your understandings of strings and integers.

Just Strings

1. First & Last

If you have the strings "First" and "Last" in the following variables:

f = "First"
l = "Last"

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

  1. What code can you write to output the string "FirstLast"?
  2. What code can you write to output the string "LastFirst"?
  3. What code can you write to output the string "First Last"?
  4. What code can you write to output the string "Last First Last First"?

Then repeat 1-4 using only the "string interpolation" technique.

2. Names

Start with this string:

name_1 = "Megan Smith"
name_2 = "Todd Park"
  1. Can you come up with two ways to output just the fragment "Megan" from name_1?
  2. Would either of your techniques from A would work to output "Todd" from name_2? Why or why not?
  3. Write code that can output the initials of name_2.

Just Integers

Start with these numbers:

a = 12
b = 65
c = 31
d = 98
  1. Write code to find the average of these four numbers.
  2. Find the average yourself using paper or a calculator. Is your answer different than you found in A? Why?
  3. 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?

Strings & Integers

People

Say we have these people:

a = "Ezra"
b = "Ada"
c = "Yukihiro"
d = "Grace"

Write code to output both the total characters in all the names together and the average length of the names.

Happy Birthday

In our family we like to say "Happy" once for every year of your age when we say "Happy birthday!". So when you turn four we'd say "Happy happy happy happy birthday!" Note the capitalization.

Say you have an age variable that holds the person's age. Write code to output the appropriate greeting.

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?