recitation3 notes

Exforiin15do printi end prints12345 c

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: onths[0, 2] // start index = 0, number of elements to extract = 2 // Returns [“Jan”, “Feb”] e. months.include? “Nov” //Returns whether “Nov” is present in months 7. Concatenation a. Let q1, q2 be two arrays. You can concatenate both arrays into one array called q3 using: q3 = q1 + q2 b. yrs = [2010, 2011, 2012] 7 yrs << 2013 // yrs is now [2010, 2011, 2012, 2013] 8. Set Operations a. Intersection (&) creates a new array, merging the common elements of two arrays but removing uncommon elements and duplicates Example: year = semester1 & semester2 b. Difference (-) creates a new array, removing elements that appear in both arrays Example: year = semester1 – semester2 c. Union (|) joins two arrays together, removing duplicates Example: year = semester1 | semester2 9. Extracting Unique Elements a. shopping_list = [“apples”, “oranges”, “apples”, “bananas”, “oranges”] shopping_list.uniq! //Returns [“apples”, “oranges”, “bananas”] 10. Deletions a. months = [“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Dec” ] b. months.delete(“Jan”) //Deletes element “Jan” from array c. months.delete_at(2) //Deletes element at index 2 from array 11. Iterations a. Array has an each method that allows you to iterate over every element in the array b. Ex: months.each { |e| print e }/prints out every element inside months / 12. Sorting an array of numbers a. num_list = [2, 5, 1, 7, 23, 99, 14, 27] b. num_list.sort! //sorts num_list to [1, 2, 5, 7, 14, 23, 27, 99] Topic 9: Conditionals 1. If Statements a. Example: if x == 256 puts “x equals 256” end b. The operator && means “and” Example: if a == 10 && b == 27... Alternatively, you can do: if a == 10 and b == 27... c. The operator || is used to represent “or” Example: if a == 10 || b ==27 Alternatively: if a == 10 or b == 27... 2. The Ternary Operator: 8 a. Example: label = length == 1? "argument" : "arguments" b. This expression assigns a string value to label based on the value of length. If the...
View Full Document

This document was uploaded on 03/18/2014 for the course EECS 6.170 at MIT.

Ask a homework question - tutors are online