Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save trishalanglois/1249b87108444fef046a5fda9df28934 to your computer and use it in GitHub Desktop.

Select an option

Save trishalanglois/1249b87108444fef046a5fda9df28934 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 (60 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 drop method asks for a value (n), and will then "drop" those first n values . For example, if you have the array a = [11, 12, 13, 14], then a a.drop(2), the return will be [13,14].

  • What did you Google to help you with this task, and how did you pick your results? I Googled "array drop function ruby". From there, I looked through the first 5 results and chose to look at two of them to get a general overview. The first, apidock.com, was literally just copied and pasted from the ruby documentation that was provided for this practice. The second source was stack overflow and more original. It only had 7 upticks from users, but it provided another original definition and some other options for arrays like pop, take, slice and drop_while and take_while.

  • 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 is, put simply, a way to separate different parts of our array into smaller pieces, whether that is based on characters or words within that array. An example would be : " hi i'm trisha".split(' ') would be ["hi", "i'm", "trisha"], splitting the array between each whitespace or space.

  • What did you Google to help you with this task, and how did you pick your results? Googling this concept was much more time consuming for me than the array drop method. The original link to ruby documentation didn't make much sense to me, so I went straight to Google in this case. I opened about 4 different sites and skimmed them until I had a general idea, defining other concepts like regex and delimiter along the way. I spent the most time with the most basic, introductory definitions and examples, which also included some code that was commented out, explaining what the ruby code was doing. From there, I went back to some of the other sites and went a little deeper until I got the basic gist of the capabilities of split.

  • In your own words, what does the JavaScript array slice method do? As you're explaining, be sure to provide an example. Your answer: Slicing returns a defined set of an array, starting and ending at a defined point. When defining which part of the array will be returned, be sure to keep in mind that the first value is 0. For example:

var groceries = ['apples', 'granola', 'celery', 'chicken', 'honey'];

console.log(groceries.slice(1, 4)); //expected output: Array ("granola", "celery", "chicken", "honey")

The end is not always defined, so another way to write this would be:

console.log(groceries.slice(1)); //expected output: Array ("granola", "celery", "chicken", "honey")

  • What did you Google to help you with this task, and how did you pick your results? Right away, I noticed the term "shallow copy" in the mozilla documentation. It seemed like their definition of slices was contigent on my knowing what a shallow copy was, so I started with Googling that. Simply typing in shallow copy javascript wasn't giving me what I wanted, so I changed it to "shalow copy" javascript. In the small description that came up in the Google search, one of the sites previewed a simple definition. I clicked on the link, then used command + f to find "shallow copy" on that page. After reading the definition, I still didn't quite see the connection to slicing so decided to stop spending any more time on finding an intro-level definition of "shallow copy" and went back to slicing. I modified my Google search to slicing javascript OR slicing array javascript, and that gave me the results I was looking for. Turns out, I was able to kind of figure out the definition of shallow copy through the context of these other sites, so in the future, I don't think that I'll assume I need to look up the definition of anything I don't know. Fluency before understanding, right? I quickly found a definition on freecodecamp.com that was a good intro to slicing arrays with the beginning and end. I returned back to the Mozilla and found that I understood this definition after seeing a couple examples in other places.

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: Monopoly Deal

  • 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: Property Names, Action Cards
  2. Integer and/or float data: Rent Costs, Number of cards in hand
  3. Boolean data: Is there a Monopoly?, Do I have a Deal Breaker Card?, Do I have enough money to pay rent?
  4. Array data: list of properties played, list of money cards, list of properties opponent has played
  5. Hash or Object data: Rent card and cost I owe, Property color and amount of cards for monopoly

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.

  • Making juice. Your collection will be fruits and vegetables. For each fruit or vegetable, you will do the following: wash it, cut into smaller pieces, push it through the juicer. Repeat with all of your fruit and vegetables. This is an example of iteration because you are repeating the same process with all of your fruits and vegetables.

  • Mailing thank you cards. Your collection will be stationary, envelopes and stamps. Write a message in the thank you card, place in the envelope, seal the envelope, address the envelope, and stamp it, then repeat with the next card. This is an example of iteration because you are completing the same steps with each thank you card and envelope.

  • Doing sun salutations in yoga. Your collection includes your different poses, and for each sun salutation, do the following: mountain pose, standing forward fold, halfway lift, standing forward fold, high plank, low plank, upward dog, downward dog, lunge. Then, repeat all steps. This is iteration because it is following a real-life pattern over and over. And over and over. And over and over.

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

  • Formatting the city and state of a location. The collection would be names of cities and states, and for each name, you will 1. capitalize the first letter of the city. 2. lowercase the rest of the letters. 3. place a comma. 4. capitalize the two-letter state abbreviation. Then, repeat with the next city and state. This is an example of iteration because all of the information will go through these steps and be formatted in the exact same way.

  • Creating a countdown to a certain date. The collection would be the list of dates in a specific format MMDDYYYY. For each date: 1. subtract the current year from the inputted YYYY value. 2. subtract the current month from the MM value. 3. subtract the current date from the DD value. Repeat with the next date. One issue that I would need to troubleshoot would be negative integers and how to determine values when "borrowing" from a month or year amount, especially considering our calendar is not a base-10 system. Seems like it could be a fun project! This is an example of iteration because the same mathematical steps are being done to all of our incoming data and returning another value that will all be in the same format.

  • Calculating 10% off sale prices. The collection would be the list of original prices. For each price, 1. Multiply by .9. 2. Format to a float to the hundredth place. 3. Format with a $ in front of the float. Then, repeat with the next price. This is an example of iteration because we are completing the same calculation for each price so they are all 10% off and formatted as the new price.

4. Identifying Mistakes (15 min)

The following code examples each contain a mistake. Describe the problem for each.

Original Mistakes Problem
students.each do |student|
  puts "Welcome, #{student}"
end
students.each do |student|
  puts "Welcome, #(student)"
end
The problem is {student} vs. (student)
.main-content {
  font-size: 12px;
  border: 3px solid black;
  font-family: sans-serif;
}
.main-content {
  font-size: 12px;
  border: 3px solid black;
  font-family: sans serif;
}
The problem is sans-serif vs. sans serif
log(2, (1022 * ((score - min(score) over ()) / ((max(score) over ()) - (min(score) over ()))) + 2)::numeric) log(2, (1022 * ((score - min(score) over ()) / ((min(score) over ()) - (min(score) over ()))) + 2)::numeric) The problem is max vs. min
arr.product(arr).reject { |a,b| a == b }.any? { |a,b| a + b == n } arr.product(arr).reject { |a,b| b == b }.any? { |a,b| a + b == n } The problem is a == b vs. b == b
class Cat
  attr_reader :color, :name
  def initialize(data)
    @name = data[:name]
    @color = data[:color]
  end
end
class Cat
  attr_reader :color, :name
  def intialize(data)
    @name = data[:name]
    @color = data[:color]
  end
end
The problem is initialize vs. intialize

5. 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:

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