Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save qfarenwald/0210d4a005b4879356f70f3378990070 to your computer and use it in GitHub Desktop.
Save qfarenwald/0210d4a005b4879356f70f3378990070 to your computer and use it in GitHub Desktop.
Mod 0 Session 2 Practice Tasks

Session 2 Practice Tasks

The assignments listed here should take you approximately 2 hours.

To start this assignment, click the button in the upper right-hand corner that says Fork. This is now your copy of the document. Click the Edit button when you're ready to start adding your answers. To save your work, click the green button in the bottom right-hand corner. You can always come back and re-edit your gist.

1. Documentation and Googling (75 min)

Documentation of a langauge, framework, or tool is the information that describes its functionality. For this part of the practice tasks, you're going to practice digging into documentation and other reference material.

NOTE: The linked documentation for each question below is a good starting place, but you should also be practicing your Googling skills and sifting through the results to find relevant and helpful sites.

  • In your own words, what does the Ruby array drop method do? As you're explaining, be sure to provide an example. Your answer: This method removes specified elements from the front of an array and returns a new array with the remaning array items. Ex:

  • a = ["Paul", "Vin", "Dwayne", "Michelle"]

  • a.drop(1)

  • #=> ["Vin", "Dwayne", "Michelle"]

  • What did you Google to help you with this task, and how did you pick your results? I understood the basic concept from the link provided. But then I was unsure how to simply remove the fist item, beacause doesn't counting start at 0 in coding... would the answer be a.drop(0)? So I used google to find more examples of using the drop method to check. An example of a google phrase I used is 'remove the first item in an array using drop method in ruby'. I used stack overflow and paid attention to the upvotes. It seems that the number in the drop function corrosponds to places in the array, so 1 removes the first element (even if its in the 0 place), 2 removes the first 2 elements.

  • In your own words, what does the Ruby array push method do? As you're explaining, be sure to provide an example. Your answer: This method adds more elements to the end of an array. Ex:

  • a = ["Paul", "Vin", "Dwayne", "Michelle"]

  • a.push("Tyrese", "Jordana")

  • #=> ["Paul", "Vin", "Dwayne", "Michelle", "Tyrese", "Jordana"]

  • What did you Google to help you with this task, and how did you pick your results? I understood the basic concept from the link provided and I did not need to google anything.

  • In your own words, what does the Ruby string split method do? As you're explaining, be sure to provide an example. Your answer: This method can break up a string in many different ways in to what are called substrings and returns an array of these substrings. Regular expressions can be used as your delimiter to make the split method a lot more flexible. Ex:

  • "Too Fast, Too Furious".split #=> ["Too", "Fast,", "Too", "Furious"]

  • " Too Fast, Too Furious".split(' ') #=> ["Too", "Fast,", "Too", "Furious"]

  • "Fast".split(//) #=> ["F", "a", "s", "t"]

  • "Fast".split(//, 2) #=> ["F", "ast"]

  • What did you Google to help you with this task, and how did you pick your results? I understand the very basic concept of this method, but when the link above got into the examples, I became very confused by some of the new terminolgy and symbols. I first Googled 'Ruby string [split] method explained'. The first link had a date of March 2019, so I took a look. Here I learned that Regexp meant 'regular expression'. Next I Googled 'What is regular expression in ruby?'. I even tried Googling some of the exact Regexp, like '.split(/ /) in ruby' to try and understand them better. I had to include 'Ruby' in the search because it seems JS also has some of these methods.

  • In your own words, what does the JavaScript array slice method do? As you're explaining, be sure to provide an example. Your answer: This method returns only a portion of the array that is indicated in the method, removing all other elements. Counting of elements in the array starts at 0. Whatever location in the array that is listed as the starting point in the slice is included in the final output. Whatever location in the array that is listed as the ending point in the slice will not appear in the final output but the slice will include the element just before as the ending element in the slice. These removed elements will not be deleted forever but will remain and if the original method is called upon, all original elements will return. Ex:

  • var actors = ['Paul', 'Vin', 'Dwayne', 'Michelle', 'Tyrese', 'Jordana']

  • console.log(actors.slice(2));

  • // expected output: Array ["Dwayne", "Michelle", "Tyrese", "Jordana"]

  • console.log(actors.slice(1, 4));

  • // expected output: Array ["Vin", "Dwayne", "Michelle"]

  • What did you Google to help you with this task, and how did you pick your results? I did understand the basic concept from the initial link, however, in the initial link, a mix of '' and "" were used and this confused me. I Googled 'write a variable in javascript'. I located the reccomended MDN link. Other links I found, like stack overflow use "" around the varialbe strings. Is this simply a typo or perhaps either '' or "" can be used? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values has the same, '' is used in the const and "" is used in the ouput array.

  • In your own words, what does the JavaScript object values method do? As you're explaining, be sure to provide an example. Your answer: This method returns the values in the key-value pairs into an array in the same order in which they are listed. Ex:

  • const object1 = {

  • a: 'Eclipse'

  • b: 'GTO'

  • c: 'Supra'

  • };

  • console.log(Object.values(object1));

  • // expected output: Array ["Eclipse", "GTO", "Supra"]

  • What did you Google to help you with this task, and how did you pick your results? I did have to google 'enumerable' to find the definition in hopes to understand the first link better. The definition is '...able to be counted by one-to-one correspondence with the set of all positive integers.' which makes sense since an object has key-value pairs.

2. Data Types (15 min)

Imagine that you're taking your favorite board game and turning it into a computer-based game.

  • Name of board game: Liars Dice

  • Use the space below to categorize game data into each of the following data types. You should have a minimum of two pieces of data for each category.

  1. String data:
  • Required game pieces: "1 cup", "6 dice", "flat surface"
  1. Integer and/or float data:
  • Number options available on dice: 1, 2, 3, 4, 5, 6
  1. Boolean data:
  • Did the numbers I rolled on the dice equal a full house?: True or False
  1. Array data:
  • List of hands able to roll: ["1 high", "2 of a kind", "3 of a kind", "4 of a kind", "5 of a kind", "6 of a kind", "2 pair", "full house", "low straight", "high straight"]
  1. Hash or Object data:
  • dice (key) and number rolled (value)
  • {"Dice 1": 2, "Dice 2": 4, "Dice 3": 4, "Dice 4": 4, "Dice 5": 2, "Dice 6": 1}

3. Iteration (30 min)

  • Create a list below of three real-life situations where iteration is used. For each situation, explain why it would be an example of iteration.

  • The scenario is stretching your muscles before playing a sport. It is an example of interation because each body part needs to be stretched untill the entire body is ready to play the desired sport. The collection is your body parts. For each muscle you must stretch, hold for 15 secs and release then move on to the next body part untill all are stretched.

  • The scenario is brushing your shedding dog. It is an example of iteration because each area of the dog needs to be brushed to remove all the extra hair. The collection is parts of the dog. For each part you must brush and remove hair from brush then move on to the next body part untill the entire dog has been brushed.

  • The scenario is eating a chocolate bar. It is an example of iteration because each square needs to be eaten untill the entire bar is gone. The collection is all the chocolate bar squares that make up the bar. For each square, you must break it off, put it in your mouth, chew, and swallow then repeat with the next square of chocolate untill the bar is all gone.

  • Create a list below of three programming situations where iteration would be used. For each situation, explain why it would be an example of iteration.

  • The scenario is counting the amount of money removed from an ATM. It is an example of iteration because it will record the amount of money removed at each transaction and subtract that from the total money in the machine, if the machine money gets below a specified amount, it will alert the teller to refill the ATM. The collection is money. For each amount withdrawn, the program will record that amount, subtract it from the total money in the ATM, and test that amount to see if it is more or less than the minimum. If it is less than the minimum and alert will be sent to the teller. If it is more than the minium, no alert will be sent. The program will perform this iteration with each amount of money withdrawn.

  • The scenario is formatting a phone number in a form to look like this xxx - xxx - xxxx. It is an example of iteration because each form will need to have the phone number formatted. The collection is the phone numbers. For each phone number, delete all symbols that are not integers, check that there are 7 integers, insert ' - ' after the first 3 integers, insert ' - ' after the next 3 integers, check to make sure there are 4 integers left then move on to the next phone number untill all there are no more phone numbers.

  • The scenario is checking if bike parts are in stock and if not, order them. It is an example of iteration because each bike part will need to be checked to see if more need to be ordered. The collection is bike parts. For each bike part, the current quantity will be checked. If the current quanity is 0 that part will be ordered. If the current quantity is above 0 the part will not be ordered. Then the system will move on the next part and run the true or false (boolean) untill all parts are checked.

4. Modify your Bash Profile (10 min)

  • Watch this video and follow each step to modify your own bash profile. As mentioned in the video, you will need this snippet below:
# get current branch in git repo
function parse_git_branch() {
  BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
  if [ ! "${BRANCH}" == "" ]
  then
    STAT=`parse_git_dirty`
    echo "[${BRANCH}${STAT}]"
  else
    echo ""
  fi
}

# get current status of git repo
function parse_git_dirty {
  status=`git status 2>&1 | tee`
  dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
  untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
  ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
  newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
  renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
  deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
  bits=''
  if [ "${renamed}" == "0" ]; then
    bits=">${bits}"
  fi
  if [ "${ahead}" == "0" ]; then
    bits="*${bits}"
  fi
  if [ "${newfile}" == "0" ]; then
    bits="+${bits}"
  fi
  if [ "${untracked}" == "0" ]; then
    bits="?${bits}"
  fi
  if [ "${deleted}" == "0" ]; then
    bits="x${bits}"
  fi
  if [ "${dirty}" == "0" ]; then
    bits="!${bits}"
  fi
  if [ ! "${bits}" == "" ]; then
    echo " ${bits}"
  else
    echo ""
  fi
}

export PS1="\u\w\`parse_git_branch\`$ "

5. Questions/Comments/Confusions

If you have any questions, comments, or confusions from the any of the readings that you would an instructor to address, list them below:

  1. For the Ruby string [split] method, I understand the basic concept, but I am trying to understand the specific examples provided that use Regular Expressions. A more specific question, in my example does 'Too' need a space before it to create a substring in the first two examples?? My thought is no, it is just to show that all leading white space and runs of white space are ignored?

  2. For Ruby array [slice] method, in the initial link, a mix of '' and "" were used and this confused me. Obviously that output is an array which surrounds the elements with "". Other examples I found, like on stack overflow use "" around the varialbe strings. Is this simply a typo or perhaps either '' or "" can be used in a variable string? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values has the same, '' is used in the const and "" is used in the ouput array.

@katiescruggs
Copy link

Nice work, @qfarenwald! For your questions:

  1. Your Ruby split method looks good. You don't need an extra space. The part in the parentheses shows which thing will be used to separate the string. I feel like this is hard to explain through text. Feel free to visit https://repl.it/ and try out the difference between "Too Fast, Too Furious".split() and "Too Fast, Too Furious".split(" ").
  2. Good eye for detail. Yes ' or " can be used for strings in JavaScript and Ruby. Some languages are pickier. In the link you posted in your question, I think it is trying to show that the output is in the format of how a console would print it out by using double quotes.

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