JumpStart Live Day 5: Optional Problems

You KNOW I’m gonna do those optional problems!

And since my last post got pretty long, I decided to do a separate entry for optional stuff.

So, here we go!

Exercise 1

Create an array of people that are invited to a party. Allow the user to ask if a certain person is invited to the party. If the person is on the invitation list respond INVITED otherwise respond NOT INVITED.

Here’s my code:

exercise1

What my code does:

  • Create an array of people invited to the party
  • Prompts user to enter a name
  • Collects user input
  • Based on user input, prints INVITED or NOT INVITED

Checking whether a name is on the list is fairly straightforward.

First, I assign user input to variable name.

Then, using an if-else statement, I check whether name is included in the invitees array.  An .include? method (with name entered as the argument) does the job quite nicely!

If invitees.include?(name) evaluates as true, then the program prints INVITED.  Otherwise, it prints NOT INVITED.

Sample run:

exercise1_output1
Claire is invited to the party!

Since “Claire” is one of the elements inside the invitees array, the program prints INVITED.

Another sample run:

exercise1_output2
Kathleen is not invited to the party.

“Kathleen” is NOT an element inside the invitees array, so the program prints NOT INVITED.

Exercise 2

Have the user enter in a sentence. Then, using an array, store the frequency of each letter of the alphabet from the sentence. Print out the frequency of each letter. Do not count uppercase and lowercase letters differently.

Here’s my code:

exercise2_code
Printing frequency of letters inside user input

What my code does:

  • Collect user input
  • Checks the frequency of each letter of the alphabet
  • Stores each letter’s frequency inside an array
  • Prints the frequency of each letter that actually appears inside the sentence.

After prompting the user to enter a sentence, I assign the string to variable sentence.

Since we’re not supposed to count uppercase and lowercase letters differently, I convert all letters to lowercase with .downcase.

Then, I pass "a".."z" as a range for an .each loop.  Each letter is represented by the letter variable.

Here’s how I actually calculate the frequency of letters:  By entering letter as an argument for the .count method…which I called on sentence.

The resulting value goes into my frequency_of_letters array.

After that, I use the .each_with_index method with a range of "a".."z".  Each letter is represented by letter, and i represents the current letter’s index number.

Then, in curly braces, I tell the program to print a) the current letter, and b) its corresponding frequency inside frequency_of_numbers .

However, if the current letter appears 0 times in the sentence, then it won’t be printed (courtesy of a quick unless statement).

Sample run:

Exercise2_output
Every letter of the alphabet appears at least once in this sentence!

Another sample run:

exercise2_output2
Fewer letters appear in this sentence.

 

Exercise 3

Create an array of size 8. Fill the array randomly with 0’s and 1’s. Print out the array so that it appears as a binary number. Calculate and print out the decimal value of that binary number.

Here’s my code:

exercise3
Creating a random binary number, and printing out its decimal value.

First, I create a new array for 8 elements with Array.new(8).  The following block uses rand() — and tells Ruby to generate a random number between 0 and 1 for each position.

The resulting array is assigned to variable binary.

Here’s how I printed the list as a single binary number: Call the .join method on binary…and print the result on a single line!

Now, to calculate the random binary number’s decimal value:

  1. Initialize a new variable decimal with the value 0 (for the upcoming loop)
  2. Tell an .each_with_index method to cycle through each element in binary
  3. Use .reverse to iterate through each element — from last to first position
  4. Assign current number in binary to num, and its index number to i
  5. Tell Ruby to calculate 2 to the power of the number’s current index…
  6. …And add the result to the current value inside decimal!
  7. ONLY  perform the above calculations if num equals 1

Finally, I print the result by using puts on decimal.

Sample run:

exercise3_output
Prints a random binary number and its decimal value

Summary

These optional exercises gave more opportunities to store, print and calculate information with arrays!

References

Leave a comment