Skip to content

Instantly share code, notes, and snippets.

@cmacaulay
Last active November 29, 2016 03:43
Show Gist options
  • Save cmacaulay/c1d421c780c52b454640714da65529ba to your computer and use it in GitHub Desktop.
Save cmacaulay/c1d421c780c52b454640714da65529ba to your computer and use it in GitHub Desktop.
Challenges // Working with Strings and Integers

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? Yes
  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? Yes because it includes a decimal on the calculator.
  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?

@cmacaulay
Copy link
Author

screen shot 2016-11-28 at 8 42 53 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment