Complete the following exercises to solidify your understandings of strings and integers.
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:
- What code can you write to output the string
"FirstLast"
? - What code can you write to output the string
"LastFirst"
? - What code can you write to output the string
"First Last"
? - What code can you write to output the string
"Last First Last First"
?
Then repeat 1-4 using only the "string interpolation" technique.
Start with this string:
name_1 = "Megan Smith"
name_2 = "Todd Park"
- Can you come up with two ways to output just the fragment
"Megan"
fromname_1
? - Would either of your techniques from A would work to output
"Todd"
fromname_2
? Why or why not? - Write code that can output the initials of
name_2
.
Start with these numbers:
a = 12
b = 65
c = 31
d = 98
- Write code to find the average of these four numbers.
- Find the average yourself using paper or a calculator. Is your answer different than you found in A? Why?
- 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?
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.
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.
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?