Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ericwm76/a819dd8e297ff5ca28e7496a97bced1a to your computer and use it in GitHub Desktop.
Save ericwm76/a819dd8e297ff5ca28e7496a97bced1a 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: array.drop(n) drops all the elements in an array up to the nth element, and returns all the elements of the array after the nth element. For example, if I have an array arr = ['a', 'b', 'c', 'd', 'e', 'f'], arr.drop(3) will return ['d', 'e', 'f'], since 'c' is the third element of the array.

  • What did you Google to help you with this task, and how did you pick your results? I searched drop method ruby, and found the official Ruby documentation to be pretty clear and helpful.

  • In your own words, what does the Ruby array push method do? As you're explaining, be sure to provide an example. Your answer: array.push() will push whatever values are in the parentheses onto the end of the array. For example, if I have an array arr = ['a', 'b', 'c', 'd', 'e', 'f'], arr.push('g', 'h', 'i')) will return ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'].

  • What did you Google to help you with this task, and how did you pick your results? I had already learned what the push method was in Javascript through a free online course I took, and this is basically the same, so the Ruby documentation itself was enough to jog my memory and help me understand.

  • In your own words, what does the Ruby string split method do? As you're explaining, be sure to provide an example. Your answer: The split method allows you to take a string and split it up into an array of several smaller strings. The value you put in the parentheses of .split() will be what divides up the string. An example I found was words_str = 'Foo,Bar,Baz' words_arr = words_str.split(',') This would return an array of the words ['Foo', 'Bar', 'Baz'].

  • What did you Google to help you with this task, and how did you pick your results? Kept it simple: "split method ruby". I read through several different blog posts, StackOverflow posts, etc. I tried to stick with results that were two years old or less. Some of the posts got complex quickly, so I tried to stick to the ones that were simple.

  • 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 allows you to remove elements from an existing array to create a new array. Slice accepts two arguments: the index of the first element you want to remove, and the index of the last element you want to remove. It will remove all the elements in between those two and make a new array using those elements. For example, if I have an array var treeNuts = ["pistachios", "almonds", "walnuts", "cashews", "hazelnuts"] treeNuts.slice(1, 4) will return ["almonds", "walnuts", "cashews"], because it goes from the first argument in my slice, 1 (remembering that the first element in an array is always 0), and goes until the second argument, not inclusive. Since the second argument is 4, this slice will include elements 1 through 3. I can then define treeNuts.slice as its own variable.

  • What did you Google to help you with this task, and how did you pick your results? I remembered learning about slice through a free online intro to javascript course I had done, so I went back to that and reviewed different methods for modifying arrays. I also searched "slice method javascript" to verify what I learned in the coding course. I found a Medium blog post written in October 2018 that explained it pretty clearly.

  • In your own words, what does the JavaScript object values method do? As you're explaining, be sure to provide an example. Your answer: If I have an object, such as a variable, with multiple properties included, I can use object.values() to return the properties of the object as an array. Here's a clear example I found: const icecreamColors = { chocolate: 'brown', vanilla: 'white', strawberry: 'red', }

const colors = Object.values(icecreamColors); // colors will be equal to ["brown", "white", "red"]

  • What did you Google to help you with this task, and how did you pick your results? I searched "object values method javascript" and read several of the results that appeared. The first result that appeared was a site called geeksforgeeks.com, which was somewhat helpful. I found a few stack overflow posts about it, which were still helpful, but the most helpful was a Medium blog post, codeburst.io, that spelled it out pretty clearly. It was from May 2018, so fairly recent, and the content seemed of high quality, so trusted it.

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: Pandemic

  • 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: Names of characters, occupations of characters
  2. Integer and/or float data: Number of cubes on a particular country, number of outbreaks.
  3. Boolean data: Are there already three cubes on this space? If true, then the space outbreaks. If the number of outbreaks equals eight, then you lose the game.
  4. Array data: Infected cities in discard pile; cities containing research stations
  5. Hash or Object data: Each character could be an object, with properties such as name, occupation, etc. Each country could also be an object, with properties such as number of adjacent countries, starting color (blue, red, yellow, black), etc.

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.

  • Solving a Rubik's cube - I don't know exactly how to do it, but from what I understand, there are a couple of different algorithms that you use over and over again until it's solved. Which algorithm you use is conditional -it depends on the way the colors line up.

  • Farming - each year is a new iteration. Your decisions about which crops to plant in which fields are based on algorithms that incorporate factors of soil quality (ph, sand/clay/loam content, moisture, sun exposure), economic yields, anticipated weather, etc. Then, you pull it all up and do it over again the next year. The loop only closes when you retire, shut down the farm, or sell it.

  • Filing taxes - I just did this yesterday, and inputting each document (W-2s, 1099s, 1095-Cs, etc.) into the system felt like a loop of the same basic procedure, with slightly different conditions on each one.

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

  • Printing off a list of students whose GPA is above a 3.9 for Honor Roll purposes - the software would have to go through each and every student record, match the data against a certain criteria, and either accept or reject it, then do the whole process again.

  • It seems that any kind of search function operates on iterations. You set up your conditions/criteria, then the software runs through each data point to find if it matches the criteria, and returns only those that do. As I'm typing this, I'm finding myself blown away at how quickly that all happens when you search something.

  • Online shopping - a retail website iterates to make advertisement recommendations based on each individual customer's search history, demographics, etc. It runs through the same loop for every different customer.

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, @ericwm76! Good examples of iteration. The way search functions operate is usually more complicated than iterating over data points, but it is crazy how fast they work! With the Rubik's cube example, I'm not sure what the collection you're iterating over would be, but your other examples have good collections!

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