Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save polsieira/ca7f9703ecee04d253c66ef3f5a4758e to your computer and use it in GitHub Desktop.

Select an option

Save polsieira/ca7f9703ecee04d253c66ef3f5a4758e 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:
    The Ruby array drops the first n elements of a specified array and creates a whole new array to put the remaining elements. For example:
array = ["one","fish","two","fish","red","fish","blue","fish"] 
array.drop(4) 
#=> ["red","fish","blue","fish"]
  • What did you Google to help you with this task, and how did you pick your results?
    I googled "ruby array drop method" and chose the Ruby documentation because it is probably one of the most reliable sources for information on Ruby. When opening the page I used cmd + F to find all instances of the word 'drop' and scrolled down to its definition.

  • In your own words, what does the Ruby array push method do? As you're explaining, be sure to provide an example. Your answer:
    The Ruby array method push will add items to the end of a specified array. For example:

array = [1, 2, 3, 4, 5]
array.push(6).push(7)
#=> [1, 2, 3, 4, 5, 6, 7]
  • What did you Google to help you with this task, and how did you pick your results?
    I googled "ruby array push method" and chose the Ruby documentation because, again, it is one of the most reliable sources for information on Ruby. When opening the page I used cmd + F to find all instances of the word 'push' and scrolled down to its definition.

  • In your own words, what does the Ruby string split method do? As you're explaining, be sure to provide an example. Your answer:
    split divides strings into specified substrings determined by a specified delimiter or can split strings into individual characters. The resulting substrings or characters are put into a new array. For example:

str = "my string"
str.split
#=> ["my", "string"]

OR

str = "colorado"
str.split
#=> ["c","o","l","o","r","a","d","o"]
  • What did you Google to help you with this task, and how did you pick your results?
    I googled "Ruby split string method" and (you guessed it) went straight to the Ruby documentation. But for fun I checked out what StackOverflow had to say and found a question with 77 up votes and a confirmed answer with 97 upvotes. Although they didn't give a direct definition like the Ruby documentation, they provided a good example of pulling elements out of an array by using split. I did notice this isn't exactly what I was looking for because I wanted the string method and not the array but here is the example anyways:
array = [1, 2, 3, 4, 5]
array.split(,).first
#=> 1

OR

array = [1, 2, 3, 4, 5]
array.split(,).last
#=> 5
  • In your own words, what does the JavaScript array slice method do? As you're explaining, be sure to provide an example. Your answer:
    The slice method will take the specified elements (from a until b) and put them into a new area, leaving the original array untouched. For example:
array = ["first", "second",....., "tenth"]
array.split(1, 3)
#=> ["second", "third"]

As you can see the from element isn't sliced out but the until element is.

  • What did you Google to help you with this task, and how did you pick your results?
    I googled "JavaScript array slice element" and picked Medium as a resource because it has been edited in 2019 and gave a good explanation although I believe splice isn't an element that changes very much. Medium is a source I've used in the past but to be sure I reference w3schools and Mozilla to see if they all had similar exlanations.

  • In your own words, what does the JavaScript object values method do? As you're explaining, be sure to provide an example. Your answer:
    JavaScript object values method returns an array of all the objects enumerable property values in the order the values are listed in the object. For example:

const Pol = {
  height: 74,
  weight: 180,
  eyeColor: "brown",
  hairColor: "brown",
  age: 24
};

console.log(Object.values(Pol));

> Array [74, 180, "brown", "brown", 24]
  • What did you Google to help you with this task, and how did you pick your results?
    I googled "JavaScript object values method" to find the results. Knowing that Mozilla is a reliable source, I went straight to their site. They have a executable example in the webpage which I found useful.

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: Settlers of Catan

  • 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: color of player ("blue"), name of resource ("wheat")
  2. Integer and/or float data: resulting number from dice roll (int), number of roads (int), number of settlements (int), number of resources you get to pick up on a turn (int)
  3. Boolean data: is a player willing to trade?, do I have enough resource cards to trade in for a settlement?, do I have enough resource cards for a development card?, do I have enough points to win?
  4. Array data: list of remaining types of land to put settlements on, list of types of ports a player has settlements on, list of remaining development cards
  5. Hash or Object data: resource (key) and number of cards you have of the resource (value), type of land (key) and number of settlements you have on the type of land (value)

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.
  • Playing flip cup! Once your turn is up you drink the cup then begin an iterative process of setting up the cup on the edge of the table and tapping it lightly to try to flip it and have it land face down on the table. If you fail, set up the cup again and repeat the tap until the task has been completed.
  • Mopping the kitchen. Not as fun as flip cup but the iterative process still exist. First you wet the mop, then strain it, then mop an area and repeat. You will do this as many iterations as it takes for the kitchen to be clean. In steps it'll look something like:
Step 1: Wet mop
Step 2: Strain mop
Step 3: Mop floor until mop is dirty
Repeat Step 1: Wet mop
.
.
.
  • Grocery shopping could be an iterative process! Say you get into the grocery store and go to aisle one. You scan through the aisle put necessary items in the cart and repeat the same process for aisle 2 until you've goen through all of the aisles. You are repeating the same taks through every aisle making it an iterative process.

  • 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.

  • Adding a the same string to an array of strings. For example having an array of names and want to add the names to the greeting of an email to personalize the email before sending them. For each name, insert the name into the greeting "Welcome (insert name here)!". Below is a simplified example of what the code would look like:

for (i = 0, i < names.length, i ++) {
  console.log("Welcome " + names[i] + "!");
}
  • Say your are trying to break a 4 digit pin... You could use programming to iterate through all possible combinations of numbers. Not saying that it wouldn't take forever but for every iteration you could start at 0000 and work your way up to 9999 using iteration and increment the right digit by one and rolling over the next digit at 0009 (0009 -> 0010). The process would take the last pin and add one to the number every iteration through to test 9999 possible combinations.
  • If you are looking at residuals and simply want ot know how far data is from a mean you could iterate through all the residuals and make them positive. This might not be the most efficient way to do this but say you have an array of 1000 numbers, 500 of which are negative. You take the first number and see if its greater than 0. If it is you go onto the next number. If it isn't you multiply it by -1 and move then move onto the next number until all 1000 numbers in the array have been iterated through and the entire array is positive.

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:

@katiescruggs
Copy link

Nice work, @posi7790! Most of your iteration examples look great, but as fun as flip cup is, your example seems more like a while loop. While a condition is true (the cup has not landed properly), keep going. Iteration examples will have a collection that they are looping through.

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