Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adamsjr8576/36f34e3de1539193c54e1add37701e7d to your computer and use it in GitHub Desktop.
Save adamsjr8576/36f34e3de1539193c54e1add37701e7d 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 drop method takes an array - collection or list can be strings, integers - and it returns all of the values except for the specified first amounts - x. I found the term "enumerable collection" come up a good bit. So, within the drop method you fill in the variable x and running the method takes the established array and skips the value of x elements in the array - listing the remaining elements. Would be best used if you have a collection of something and you want the result to only contain a certain amount of those items. You could use it in conjunction with a sorting function - so you say you have a list of foods that you want listed based on their temperature. You could then assing each element as a variable to a certain temperature. Then place those into the array sorted by their temperature say from cold to hot. You could then use the drop method to pull the top 3 hottest foods. The below would skip a, b, c, d and list the rest of the elements in the array.

Example: array - @alphabet = [a, b, c, d, e, f, g, h, i, j, k] drop method - @alhabet.drop(4)

  • What did you Google to help you with this task, and how did you pick your results?

At first I just tried googling what was in the questions above - ruby array drop method - and got a lot of different answers and things that did not seem to help much. Here was my progression definition ruby array drop method - definition drop method ruby array - "drop method" in ruby -

Using the quotations and rewording in that way I got a lot better examples and explanations of what the drop method is and how it works. I went down this list and opened up the first 5 options and scanned through those. I found that the first one I opened to be the most helpful in explaining what it does and how to do it - http://www.rubycuts.com/developer-resources/ruby-enumerable-module/drop-method/

  • 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 seems to be most commonly used to convert a string into an array of the elements that are found within the string. Basically, it takes the content of the string and seperates it out into individual elements based on the criteria your provide it. The criteria aspect of this seems to be fairly extensive and can get pretty complicated. The delimiter is used to define how the split method will tell how and when to seperate the characters within the string. Wether that be something like every time there is a space then seperate, or every time there is a comma then seperate, or just seperate every character, or every time there is a comme seperate and eliminate the space after the comma, etc. So, it seems that based on the delimiter you can use this method to sort and seperate the content in a lot of different ways.

example: str = "Monday Tuesday Wednesday Thursday Friday" puts str.split(" ")

The information between the "" is considered the delimiter and determines how the info in the string is seperated and then listed. If my list was seperated by commas - "Monday,Tuesday,Wednesday,Thursday,Friday" then my delimiter would need to be ","

If my delimiter in the above example was just "" then it would seperate by every single letter and character including the spaces. So, using the delimiter correctly is essential to the functionality of this method.

  • What did you Google to help you with this task, and how did you pick your results?

I used a similar googling method that ended up working for the first - "split method" ruby string - this yielding quite a few examples and I went with the first 5-7 and scanned through those to find which would be the most useful and to get multiple sources of information. I found this one to be the most useful - https://www.thoughtco.com/using-the-split-method-2907756. rubyguides.com was also helpful. I then used repl.it to try and putting some code in under Ruby and tinker around with delimiter to understand more how that worked and how it effected the outcome. It took a little bit to start to figure that out as well as what a delimiter was, which I definitely still don't fully understand.

  • 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 takes a certain amount (specific by the user) of elemements form the original array and creates a new array with those selected elements. It does not change the elements of the original array and can be used to further categorize your array by creating a new one.

example: var weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']; console.log(weekdays.slice(1, 3));

There is an array containing the 5 weekdays M-F. The slice method is then used to pull certain elements from that array and put them into a new one or create an ouput of the new ones. This example just puts the output of the selected elements as text. The paranthesis after slice are used to determine what elements it selects from the specified array. The first one being denoted by 0 and then so on for however many there are. So, in this example slice(1,3) includes 1 and takes all of the ones afterwards until it reaches the third and does not bring that one in, so it only outputs Tuesday and Wednesday.

  • What did you Google to help you with this task, and how did you pick your results?

Honestly, the provided webpage was more than enough to get a grasp on how this works and apply it. Though, I did the same google search method as the ones before "slice method" javascript array and got some good resources that way. I used the JS demo editor on the develper Mozilla webpage to mess around with 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: 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:

User color: "blue"

Resource: "food"

forms of construction: "road"

  1. Integer and/or float data:

number of cards: 4

number of roads: 5

dice roll: 10

  1. Boolean data:

Was an 11 rolled? Yes

Can I build a settlement? No

Do I have the longest road? Yes

  1. Array data:

list of resources: ["food", "brick", "stone", "sheepies", "wood"]

list of construction types: ["road", "settlement", "city"]

dice roll options: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

  1. Hash or Object data:

construction (key) resources needed (value): { "road": "brick and wood", "settlement": "brick, wood, food, sheep", "city": "rock and food" }

resources you get (key) on certain dice rolls (value): { 10: "wood", 8: "stone and brick", 4: "sheepies", 6: "food, sheepies, and rock" }

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.

  • Scenario: Writing thank you notes - Collection: Thank you notes - For Each: Thank you note - Do This: 1.write thank you note 2.put thank you note in envelope 3.seal enveloped 4.adress letter 5.put stamp on letter 6.mail letter - Then: repeat with next thank you note

  • Scenario: checking email - Collection: emails - For Each: email - Do This: 1.open unread email 2.read 3.file or delete - Then: repeat with next email

  • Scenario: making moscow mules - Collection: moscow mules - For Each: moscow mule - Do This: 1.get copper mug 2.pour 1 shot vodka in mug 2.pour 8oz ginger beer in mug 3.squeeze half lime in mug 4.put in ice 5.stir 6.serve - Then: repeat with next moscow mule

  • these are all example of iterations because they contain specific steps that are repeated consecutively in a certain order every time the task is performed across multiple iterations of that task.

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

  • Converting Farhenheit to Celsius: This would be an iteration because for every Farhenheit temperature the same exact mathematical equation must be performed to convert it to celsius. So, it would take first temperature perform the equation and output celsius then do the same thing to the next temperature and so on until all temperatures have been converted.

  • Calculating someones age: If you had data containing someones birth year as well as the current year then you could calculate their age (approximately). You would subtract birth year from current year givne you their age and then you would repeat for each year. This contains the same calculation repeated for each year across all of the years in the data structure.

  • Reformatting a sentence to be grammatically correct. Say you had a bunch of single lines of text that you wanted to form into a correct sentence structure - needing capitalization and punctuation. Scenario: formatting sentences - Collection: sentences - For Each: sentence - Do This: 1.Capitalize first character 2. add a period - Then: repeat for next sentence. This would be an example of iteration because you are repeating the same set of tasks for each individual data across a whole data set.

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 the use of paranthesis instead of curly brackets
.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 not using a hyphen between sans and serif - should be 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 the use if min instead of max
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 the use b instead of a - should be a == 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 mispelled initialize - forgot the second i after the n

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