├── .gitignore
├── README.md
└── jumpstart-files
├── checkpoints.md
├── code-eval
├── code-eval-instructions.md
├── code_eval_code.png
├── code_eval_page.png
└── sample-code-eval-file.rb
├── deprecated
├── part1
│ ├── advanced-conditionals.md
│ ├── arrays.md
│ ├── booleans.md
│ ├── break-next.md
│ ├── byebug-in-action.gif
│ ├── byebug.png
│ ├── debugging.md
│ ├── functions.md
│ ├── implicit-returns.md
│ ├── irb-downcase.png
│ ├── irb-factors.png
│ ├── irb-map.png
│ ├── irb-multiline.png
│ ├── irb-push.png
│ ├── irb-underscore.png
│ ├── irb.md
│ ├── iteration.md
│ ├── loops.md
│ ├── nil.md
│ ├── numbers.md
│ ├── output.md
│ ├── part1.md
│ └── strings.md
├── part2
│ ├── advanced-arrays.md
│ ├── advanced-enumerables.md
│ ├── boolean-enumerable-methods.md
│ ├── function-decomposition.md
│ ├── hash-defaults.md
│ ├── hash-maps.md
│ ├── inline-if-and-unless.md
│ ├── part2.md
│ └── ranges.md
├── part3
│ ├── advanced-iteration.md
│ ├── enumerables++.md
│ ├── more-array-methods.md
│ ├── more-string-methods.md
│ ├── mutation.md
│ ├── nested-iteration.md
│ ├── nested-iteration.png
│ ├── part3.md
│ └── pass-by-reference.md
├── part_3_solution.rb
└── terminal
│ └── terminal.md
├── part1
├── README.md
├── assets
│ ├── repl
│ │ ├── demo_capitalize.png
│ │ ├── demo_list.png
│ │ ├── demo_math.png
│ │ ├── demo_truncated_list.png
│ │ ├── irb1.png
│ │ ├── irb2.png
│ │ ├── irb3.png
│ │ ├── irb4.png
│ │ ├── pry1.png
│ │ ├── pry2.png
│ │ └── pry3.png
│ ├── scripts
│ │ ├── arguments.png
│ │ ├── first_method1.png
│ │ ├── first_method2.png
│ │ └── first_script.png
│ └── terminal
│ │ ├── cd.png
│ │ ├── clear1.png
│ │ ├── clear2.png
│ │ ├── example_terminal_command.png
│ │ ├── ls.png
│ │ ├── man-page.png
│ │ ├── mkdir.png
│ │ ├── pwd.png
│ │ ├── ruby1.png
│ │ └── ruby2.png
├── repl_exercises.md
├── repl_readings.md
├── return_exercises.md
├── return_readings.md
├── scripts_and_methods_exercises.md
├── scripts_and_methods_readings.md
├── terminal_exercises.md
├── terminal_readings.md
├── variables_and_scope_exercises.md
└── variables_and_scope_readings.md
├── part2
├── README.md
├── boolean_logic_exercises.md
├── boolean_logic_readings.md
├── conditionals_exercises.md
├── conditionals_readings.md
├── loops_exercises.md
├── loops_readings.md
├── numbers_exercises.md
├── numbers_readings.md
├── strings_exercises.md
├── strings_readings.md
├── type_conversion_exercises.md
└── type_conversion_readings.md
├── part3
├── README.md
├── arrays_exercises.md
├── arrays_readings.md
├── enumerables_exercises.md
├── enumerables_readings.md
├── hashes_exercises.md
├── hashes_readings.md
├── iterators_exercises.md
├── iterators_readings.md
├── ranges_exercises.md
└── ranges_readings.md
├── part4
├── README.md
├── code_style_exercises.md
├── code_style_readings.md
├── debugging_exercises.md
├── debugging_readings.md
├── write-out-answer-1.jpg
├── write-out-answer-1.md
└── write-out-answer-2.md
├── practice_assessments
├── practice_assessment_1.rb
├── practice_assessment_2.rb
├── practice_assessment_3.rb
├── practice_assessment_4.rb
└── practice_assessment_5.rb
├── pre-curriculum
├── c9_setup.md
├── debugger-setup-cloud9.gif
├── debugger_setup.md
├── hello-world-cloud9.gif
├── intro.md
├── remove_rvm.md
├── setup-cloud9.gif
└── setup_rbenv.md
├── problem_sets
├── problem_set_1.rb
├── problem_set_2.rb
├── problem_set_3.rb
├── problem_set_4.rb
├── problem_set_5.rb
├── problem_set_6.rb
└── problem_set_7.rb
├── solutions
├── README.md
├── part1
│ ├── return_solutions.md
│ └── scripts_and_methods_solutions.md
├── part2
│ ├── boolean_logic_solutions.md
│ ├── conditionals_solutions.md
│ ├── loops_solutions.md
│ ├── number_solutions.md
│ ├── string_solutions.md
│ └── type_conversion_solutions.md
├── part3
│ ├── array_solutions.md
│ ├── enumerable_solutions.md
│ ├── hash_solutions.md
│ ├── iterator_solutions.md
│ └── range_solutions.md
├── practice_assessment_1_solution.rb
├── practice_assessment_2_solution.rb
├── practice_assessment_3_solution.rb
├── practice_assessment_4_solution.rb
├── practice_assessment_5_solution.rb
├── problem_set_1_solution.rb
├── problem_set_2_solution.rb
├── problem_set_3_solution.rb
├── problem_set_4_solution.rb
├── problem_set_5_solution.rb
├── problem_set_6_solution.rb
└── problem_set_7_solution.rb
├── terminal-commands.txt
└── video_link.png
/.gitignore:
--------------------------------------------------------------------------------
1 | .byebug_history
2 | .DS_Store
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # This curriculum is deprecated. Please use [Prep Step 1][prep1] and [Prep Step 2][prep2] instead!
2 |
3 | [prep1]: https://app-academy.gitbooks.io/prep-step-1/content/
4 | [prep2]: https://app-academy.gitbooks.io/prep-step-2/content/
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | # App Academy JumpStart
14 |
15 | Welcome to JumpStart!
16 |
17 | # Pre-Curriculum
18 |
19 | Before the course starts, you must:
20 |
21 | 0. Complete the ruby [codecademy track][codecademy-ruby] (chapters 1 - 6).
22 | 0. [Set up ruby on Cloud9][ruby-setup]. ([Or on your Mac][setup-rbenv])
23 | 0. [Install a debugger][debugger-setup].
24 | 0. Read our [Intro Doc][intro-doc].
25 |
26 | [codecademy-ruby]: https://www.codecademy.com/learn/ruby
27 | [ruby-setup]: ./jumpstart-files/pre-curriculum/c9_setup.md
28 | [debugger-setup]: ./jumpstart-files/pre-curriculum/debugger_setup.md
29 | [intro-doc]: ./jumpstart-files/pre-curriculum/intro.md
30 | [setup-rbenv]: ./jumpstart-files/pre-curriculum/setup_rbenv.md
31 |
32 | # Curriculum
33 |
34 | Start by going through parts 1-4. **Do not do the problem sets** until after you're done with part 4.
35 |
36 | ### Readings (week 1)
37 | ##### [Part 1 - Coding Basics](./jumpstart-files/part1/README.md)
38 | ##### [Part 2 - Ruby Basics](./jumpstart-files/part2/README.md)
39 | ##### [Part 3 - Collections](./jumpstart-files/part3/README.md)
40 | ##### [Part 4 - Code Style & Debugging](./jumpstart-files/part4/README.md)
41 |
42 |
43 | ### Problem Sets (week 2)
44 |
45 | **Read the [solutions][solutions]** after you finish each problem, but **not beforehand!** Try to finish all the problem sets by the end of week 2.
46 |
47 | ##### [Problem Set 1](./jumpstart-files/problem_sets/problem_set_1.rb)
48 | ##### [Problem Set 2](./jumpstart-files/problem_sets/problem_set_2.rb)
49 | ##### [Problem Set 3](./jumpstart-files/problem_sets/problem_set_3.rb)
50 | ##### [Problem Set 4](./jumpstart-files/problem_sets/problem_set_4.rb)
51 | ##### [Problem Set 5](./jumpstart-files/problem_sets/problem_set_5.rb)
52 | ##### [Problem Set 6](./jumpstart-files/problem_sets/problem_set_6.rb)
53 |
54 | ### Practice Assessments
55 |
56 | Take the practice assessments at home in a timed setting. **Read the [solutions][solutions]**.
57 |
58 | ##### [Practice Assessment 1](./jumpstart-files/practice_assessments/practice_assessment_1.rb)
59 | ##### [Practice Assessment 2](./jumpstart-files/practice_assessments/practice_assessment_2.rb)
60 | ##### [Practice Assessment 3](./jumpstart-files/practice_assessments/practice_assessment_3.rb)
61 | ##### [Practice Assessment 4](./jumpstart-files/practice_assessments/practice_assessment_4.rb)
62 | ##### [Practice Assessment 5](./jumpstart-files/practice_assessments/practice_assessment_5.rb)
63 |
64 |
65 | ### CodeEval
66 |
67 | If you're finished with the problem sets and practice assessments, follow the CodeEval instructions below. Don't worry if you don't get here.
68 |
69 | ##### [Code Eval Instructions](./jumpstart-files/code-eval/code-eval-instructions.md)
70 |
71 | [solutions]: ./jumpstart-files/solutions
72 |
--------------------------------------------------------------------------------
/jumpstart-files/checkpoints.md:
--------------------------------------------------------------------------------
1 | ### Part 1
2 | [Returns](./part1/return_readings.md)
3 | [Variables & Scope](./part1/variables_and_scope_exercises.md)
4 |
--------------------------------------------------------------------------------
/jumpstart-files/code-eval/code-eval-instructions.md:
--------------------------------------------------------------------------------
1 | # CodeEval
2 |
3 | Once you're finished, jump onto [CodeEval](http://codeeval.com) and work through some problems in Ruby. Find the problems that look like they're just beyond your comfort zone.
4 |
5 | ## Instructions
6 |
7 | The test cases in CodeEval can be a little strange. Don't worry about the "path to a filename" code they want you to write.
8 |
9 | Instead: For each problem you do, make a new ruby file yourself and add the test cases for the problem.
10 |
11 | Here's an [example file](./sample-code-eval-file.rb) you can reference. In the example file, notice how we represent the sample input from CodeEval by `puts`ing whether it `==` the expected output. For example:
12 |
13 | 
14 |
15 | turns into
16 |
17 | 
18 |
19 | Do the same with your challenges.
20 |
21 | Create new files like the [example file](./code-eval-instructions.md) as you pick challenges to do. Translate the sample inputs and outputs like we did above.
22 |
23 | Feel free to work through the example file first.
24 |
25 |
--------------------------------------------------------------------------------
/jumpstart-files/code-eval/code_eval_code.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/code-eval/code_eval_code.png
--------------------------------------------------------------------------------
/jumpstart-files/code-eval/code_eval_page.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/code-eval/code_eval_page.png
--------------------------------------------------------------------------------
/jumpstart-files/code-eval/sample-code-eval-file.rb:
--------------------------------------------------------------------------------
1 | #Lowest Unique Number [https://www.codeeval.com/open_challenges/103/]
2 |
3 | # There is a game where each player picks a number from 1 to 9, writes it on a paper and gives to a guide. A player wins if his number is the lowest unique. We may have 10-20 players in our game.
4 |
5 | # INPUT SAMPLE:
6 |
7 | # Your program should accept as its first argument an array.
8 |
9 | # Example Input: [3, 3, 9, 1, 6, 5, 8, 1, 5, 3]
10 |
11 | # OUTPUT SAMPLE:
12 |
13 | # Print a winner's position or 0 in case there is no winner. In the example input above the lowest unique number is 6. So player 5 wins.
14 |
15 | # Output: 5
16 |
17 | def lowest_unique_number(arr)
18 | end
19 |
20 | puts lowest_unique_number([3, 3, 9, 1, 6, 5, 8, 1, 5, 3]) == 5
21 | puts lowest_unique_number([9, 2, 9, 9, 1, 8, 8, 8, 2, 1, 1]) == 0
22 |
23 | #Word to Digits [https://www.codeeval.com/open_challenges/104/]
24 |
25 | # Having a string representation of a set of numbers you need to print this numbers.
26 |
27 | # All numbers are separated by semicolon. There are up to 20 numbers in one line. The numbers are "zero" to "nine"
28 |
29 | # INPUT SAMPLE:
30 |
31 | # Your program should accept as its first argument a string.
32 |
33 | # Example Input: "zero;two;five;seven;eight;four"
34 |
35 | # OUTPUT SAMPLE:
36 |
37 | # Print numbers in the following way:
38 |
39 | # 025784
40 |
41 | def word_to_digit(string)
42 | end
43 |
44 | puts word_to_digit("zero;two;five;seven;eight;four") == 025784
45 | puts word_to_digit("three;seven;eight;nine;two") == 37892
46 |
47 | # Shortest Repetition [https://www.codeeval.com/open_challenges/107/]
48 |
49 | # Write a program to determine the shortest repetition in a string.
50 | # A string is said to have period p if it can be formed by concatenating one or more repetitions of another string of length p. For example, the string "xyzxyzxyzxyz" has period 3, since it is formed by 4 repetitions of the string "xyz". It also has periods 6 (two repetitions of "xyzxyz") and 12 (one repetition of "xyzxyzxyzxyz").
51 |
52 | # INPUT SAMPLE:
53 |
54 | # Your program should accept as its argument a string of up to 80 non-blank characters. E.g.
55 |
56 | # "abcabcabcabc"
57 |
58 | # OUTPUT SAMPLE:
59 |
60 | # Print out the smallest period of the input string. E.g.
61 |
62 | # 3
63 |
64 |
65 | def shortest_repetition(str)
66 | end
67 |
68 | puts shortest_repetition("abcabcabcabc") == 3
69 | puts shortest_repetition("bcbcbcbcbcbcbcbcbcbcbcbcbcbc") == 2
70 | puts shortest_repetition("dddddddddddddddddddd") == 1
71 | puts shortest_repetition("adcdefg") == 7
72 |
73 | # Morse Code [https://www.codeeval.com/open_challenges/116/]
74 |
75 | # You have received a text encoded with Morse code and want to decode it.
76 |
77 | # INPUT SAMPLE:
78 |
79 | # Your program should accept as its argument a string. Input example is the following:
80 |
81 | # example_input_1 = ".- ...- ..--- .-- .... .. . -.-. -..- ....- ....."
82 |
83 | # Each letter is separated by space char, each word is separated by 2 space chars.
84 |
85 | # OUTPUT SAMPLE:
86 |
87 | # Print out decoded words. E.g.
88 |
89 | # example_output_1 = "AV2WHIECX 45"
90 |
91 | def morse_code()
92 |
93 | end
94 |
95 | puts morse_code(".- ...- ..--- .-- .... .. . -.-. -..- ....- .....") == "AV2WHIECX 45"
96 | puts morse_code("-... .... ...--") == "BH3"
97 |
98 |
99 | #Hidden Digits [https://www.codeeval.com/open_challenges/122/]
100 |
101 | # CHALLENGE DESCRIPTION:
102 |
103 | # In this challenge you're given a random string containing hidden and visible digits. The digits are hidden behind lower case latin letters as follows: 0 is behind 'a', 1 is behind ' b ' etc., 9 is behind 'j'. Any other symbol in the string means nothing and has to be ignored. So the challenge is to find all visible and hidden digits in the string and print them out in order of their appearance.
104 |
105 | # INPUT SAMPLE:
106 |
107 | # Your program should accept as its argument a string. You may assume that there will be no white spaces inside the string. E.g.
108 |
109 | # "abcdefghik"
110 |
111 | # OUTPUT SAMPLE:
112 |
113 | # For each test case print out all visible and hidden digits in order of their appearance. Print out "NONE" in case there is no digits in the string. E.g.
114 |
115 | # 012345678
116 |
117 | def hidden_digits(str)
118 | end
119 |
120 | puts hidden_digits("abcdefghik") == 012345678
121 | puts hidden_digits("Xa,}A#5N}{xOBwYBHIlH,#W") == 05
122 | puts hidden_digits("(ABW>'yy^'M{X-K}q,") == "NONE"
123 | puts hidden_digits("6240488") == 6240488
124 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/arrays.md:
--------------------------------------------------------------------------------
1 | # Array Refresher
2 | Here's a quick refresher on a few Array methods:
3 | ```ruby
4 | ["Anakin", "Luke", "Leia"].length #=> 3
5 | [].size #=> 0
6 | [0, 1, 2].count #=> 3
7 | ["ice", "ice", "baby"].count("ice") #=> 2
8 | droids = ["C-3PO", "R2-D2", "MSE-6"] #=> ["C-3PO", "R2-D2", "MSE-6"]
9 | droids[0] #=> "C-3PO"
10 | droids[2] #=> "MSE-6"
11 | droids[-1] #=> "MSE-6"
12 | droids[-3] = "BB-8" #=> "BB-8"
13 | droids #=> ["BB-8", "R2-D2", "MSE-6"]
14 | droids.first #=> "BB-8"
15 | droids.last #=> "MSE-6"
16 | droids.include?("R2-D2") #=> true
17 | droids.include?("Chewie") #=> false
18 | ```
19 |
20 | Here's how we can use `push` (`<<`), `pop`, `unshift`, and `shift`
21 | ```ruby
22 | numerals = ["IV", "V"] #=> ["IV", "V"]
23 | numerals.push("VI") #=> ["IV", "V", "VI"]
24 | numerals << "I" #=> ["IV", "V", "VI", "I"]
25 | one = numerals.pop #=> "I"
26 | numerals #=> ["IV", "V", "VI"]
27 | numerals.unshift(one) #=> ["I", "IV", "V", "VI"]
28 | one = numerals.shift #=> "I"
29 | numerals #=> ["IV", "V", "VI"]
30 | ["H", "d", "r"].join("o") #=> "Hodor"
31 | ```
32 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/booleans.md:
--------------------------------------------------------------------------------
1 | # Booleans
2 |
3 | Here are a few code examples illustrating the key concepts for booleans:
4 |
5 | This first example isn't practical--it just prints a bunch of boolean values--but it lets us review comparators:
6 | ```ruby
7 | def print_some_comparator_results
8 | puts 1 == 1 # true
9 | puts 1 < 1 # false
10 | puts 1 < 2 # true
11 | puts 1 >= 2 # false
12 | puts 2 >= 2 # true
13 | puts 2 <= 3 # true
14 | end
15 | ```
16 |
17 | Here's a more practical function that uses a comparator:
18 | ```ruby
19 | # This function returns true if the input is odd.
20 | def odd_integer?(integer)
21 | return integer % 2 == 1
22 | end
23 | ```
24 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/break-next.md:
--------------------------------------------------------------------------------
1 | # Break and Next
2 |
3 |
4 | `break` and `next` give us some flexibility when using looping constructs. Both `break` and `next` skip over the rest of the code within an iteration of a loop. `next` will then continue running the loop, starting with the next element in the collection, while `break` stops the loop completely.
5 |
6 | Here's a code example that uses both `break` and `next`.
7 | ```ruby
8 | # This function takes an array of numbers that are all greater than or equal to
9 | # zero. It then returns the smallest number.
10 | def smallest_nonnegative_number(numbers)
11 | current_smallest = numbers.first
12 |
13 | numbers.each do |number|
14 | if number > current_smallest # If the number is bigger than the current
15 | next # smallest number, skip to the next number
16 | end
17 |
18 | current_smallest = number # Update the current_smallest
19 |
20 | if current_smallest == 0 # 0 is the smallest possible non-negative
21 | break # number, so if we find a 0, break out of the
22 | end # loop.
23 | end
24 |
25 | current_smallest
26 | end
27 |
28 | some_positive_numbers = [7, 4, 0, 5, 1] # => [7, 4, 0, 5, 1]
29 | smallest_nonnegative_number(some_positive_numbers) # => 0
30 | ```
31 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/byebug-in-action.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/deprecated/part1/byebug-in-action.gif
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/byebug.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/deprecated/part1/byebug.png
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/debugging.md:
--------------------------------------------------------------------------------
1 | # Debugging
2 |
3 |
4 | You might notice that it can be hard to catch the errors you make. These errors, like misspellings, or being off by one, might feel "silly" or "stupid", but professional software engineers make the same mistakes. The key is catching them quickly.
5 |
6 | The most effective way to catch your errors is to use the debugger. You had a small taste of this in the pre-curriculum.
7 |
8 | The way to put a debugger in your code: stick `byebug` in the middle of a function and run your code. Make sure you `require 'byebug'` at the top of your file. For example:
9 |
10 | 
11 |
12 | In your interviews, we'll be asking you to play the role of debugger (knowing what every variable is at every point in time in a function), so it's important that you know how to use a debugger well. Debuggers are arguably the most useful tool you can have while learning to program.
13 |
14 | For your final assessment, it will be very helpful to know how to use the debugger to fix your errors quickly.
15 |
16 | The three most important commands in `byebug`:
17 |
18 | - `n` (or `next`): Goes to the next line
19 | - `c` (or `continue`): Goes to the next place byebug lives in the code
20 | - Printing out variable names: `elephants`, `grandma_age`, etc. You type out the variable name and press enter.
21 |
22 | Take a look at how you can use byebug while you're running your code.
23 |
24 | 
25 |
26 | You can find out more about byebug in the "Byebug Commands" section [here][byebug]. Feel free to play around with new commands.
27 |
28 | [byebug]: https://github.com/deivid-rodriguez/byebug
29 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/functions.md:
--------------------------------------------------------------------------------
1 | # Functions
2 |
3 | **Note**: When you're writing a function, indent with two spaces, not four. Python uses four spaces, but ruby uses two spaces.
4 |
5 | Also, when you're writing a function, never use single letters for variable names. Always write out the full name of the variable.
6 |
7 | For example, instead of:
8 |
9 | ```ruby
10 | def add_two_numbers(a, b)
11 | return a + b
12 | end
13 |
14 | ```
15 |
16 | You should be writing:
17 |
18 | ```ruby
19 | def add_two_numbers(first_number, second_number)
20 | return first_number + second_number
21 | end
22 |
23 | ```
24 |
25 | More example functions:
26 |
27 | ```ruby
28 | def print_out_yo_dawg # this is a function
29 | puts "yo dawg"
30 | end
31 |
32 | def multiply(first_number, second_number) # this is another function
33 | return first_number * second_number
34 | end
35 |
36 | def squared_product(first_number, second_number) # this function calls another function (multiply) inside
37 | squared_product = multiply(first_number, second_number) * multiply(first_number, second_number)
38 |
39 | return squared_product
40 | end
41 |
42 | ```
43 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/implicit-returns.md:
--------------------------------------------------------------------------------
1 | # Implicit and Explicit Returns
2 |
3 | The code example below makes use of both implicit and explicit returns to determine whether a number is a power of 2 (`2`, `4`, `8`, `16`, ... `2**n`).
4 |
5 | For all powers of two, we should be able to divide them in half over and over and eventually get one. This function takes a number and keeps dividing it in half until it reaches one. If the function gets to an odd number before reaching one, it knows right away that it is not a power of two, so it uses an explicit return to stop the function and return false.
6 |
7 | ```ruby
8 | # This function takes an integer input and returns whether it's a power of two.
9 | def silly_power_of_two?(number)
10 |
11 | while number > 1
12 | if number % 2 == 1 # return false if the number is not evenly
13 | return false # divisible by two.
14 | end
15 |
16 | number /= 2 # divide the number by two.
17 | end
18 |
19 | number == 1 # implicitly return the result of the comparator
20 | end
21 | ```
22 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/irb-downcase.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/deprecated/part1/irb-downcase.png
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/irb-factors.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/deprecated/part1/irb-factors.png
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/irb-map.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/deprecated/part1/irb-map.png
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/irb-multiline.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/deprecated/part1/irb-multiline.png
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/irb-push.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/deprecated/part1/irb-push.png
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/irb-underscore.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/deprecated/part1/irb-underscore.png
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/irb.md:
--------------------------------------------------------------------------------
1 | # IRB
2 |
3 | ## Introduction
4 |
5 | The Interactive Ruby Shell, more commonly known as IRB, is one of Ruby's most popular features, especially with new developers.
6 | You can bash out a one-liner, try a method you've just learned about, or even build a small algorithm or two without going the whole way to writing a complete program. It’s a great way of experimenting with Ruby and checking that things work.
7 |
8 | To get it up and running, all you need to do is type the following at a command line prompt, or in our case, into the Cloud9 terminal:
9 |
10 | 
11 |
12 | This launches IRB and brings you into a view like this:
13 |
14 | 
15 |
16 | When you are finished with IRB, type exit to quit:
17 |
18 | 
19 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/iteration.md:
--------------------------------------------------------------------------------
1 | # Iteration
2 |
3 | Here's a basic example of iteration using `each`:
4 | ```ruby
5 | # This function prints out several delicious shrimp dishes.
6 | # Here is a sample output:
7 | #
8 | # Shrimp kabobs
9 | # Shrimp creole
10 | # Shrimp gumbo
11 | # Shrimp soup
12 | # Shrimp stew
13 | # Shrimp burger
14 | # Shrimp sandwich
15 | # That - that's about it.
16 | #
17 | def fruit_of_the_sea
18 | dishes = ["kabobs", "creole", "gumbo", "soup", "stew", "burger", "sandwich"]
19 |
20 | dishes.each do |dish|
21 | puts "Shrimp " + dish
22 | end
23 |
24 | puts "That - that's about it."
25 | end
26 | ```
27 |
28 | Here's a more interesting example using `each_with_index`:
29 | ```ruby
30 | # This function prints out even-indexed elements of an array of famous Stevens.
31 | # It returns the whole array. Here is a sample output:
32 | #
33 | # Steven Spielberg
34 | # Steven McQueen
35 | # Steven Tyler
36 | #
37 | def even_stevens
38 | surnames = ["Spielberg", "Seagal", "McQueen", "Fernandez", "Tyler", "Gerrard"]
39 |
40 | surnames.each_with_index do |surname, index|
41 | if index % 2 == 0
42 | puts "Steven " + surname
43 | end
44 | end
45 | end
46 | ```
47 |
48 | This is a more practical `each_with_index` example:
49 | ```ruby
50 | # This function takes a force and a distance as input and returns a torque.
51 | def torque(force, distance)
52 | force * distance
53 | end
54 |
55 | torque(5, -2) # => -10
56 |
57 | # This function takes an array of forces and the index of the pivot point. It
58 | # returns the sum of torques around that pivot point.
59 | def net_torque(forces, pivot_index)
60 | net_torque = 0
61 |
62 | forces.each_with_index do |force, index|
63 | distance = index - pivot_index
64 | net_torque += torque(force, distance)
65 | end
66 |
67 | net_torque
68 | end
69 | loaded_beam = [4, -1, 0, 1, 3]
70 | net_torque(loaded_beam, 1) # => 7
71 | net_torque(loaded_beam, 2) # => 0
72 | net_torque(loaded_beam, 3) # => -7
73 | ```
74 |
75 | This example uses `chars` to turn a string into an array of characters, so it can iterate over each character:
76 | ```ruby
77 | # This function takes a team_name and prints a cheer for that team. Here is a
78 | # sample output for the input "ducks":
79 | #
80 | # Give me a D!
81 | # Give me a U!
82 | # Give me a C!
83 | # Give me a K!
84 | # Give me a S!
85 | # Go ducks!
86 | #
87 | def cheer_squad(team_name)
88 | team_name.chars.each do |letter|
89 | puts "Give me a " + letter.upcase + "!"
90 | end
91 |
92 | puts "Go " + team_name + "!"
93 | end
94 | ```
95 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/loops.md:
--------------------------------------------------------------------------------
1 | # Loops
2 |
3 | Here are a few code examples illustrating the key concepts for loops:
4 |
5 | This function employs a while-loop, which we use to do similar work repeatedly:
6 | ```ruby
7 | # This function prints "T-Minus, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Liftoff!",
8 | # each on a new line.
9 | def countdown
10 | counter = 10
11 |
12 | puts "T-Minus"
13 |
14 | while counter > 0
15 | puts counter
16 | counter -= 1
17 | end
18 |
19 | puts "Liftoff!"
20 | end
21 | ```
22 |
23 | Here's a more complicated function that combines a while-loop with if-elsif-else branching:
24 | ```ruby
25 | # This function prints:
26 | # Ooga-Chaka Ooga-Ooga
27 | # Ooga-Chaka Ooga-Ooga
28 | # Ooga-Chaka Ooga-Ooga
29 | # Ooga-Chaka Ooga-Ooga
30 | # I can't stop this feeling...
31 | def blue_swede_intro
32 | beat = 0
33 | bar = 0
34 |
35 | while bar < 4
36 | if beat % 2 == 0
37 | print "Ooga-" #print is like puts, but it doesn't make a newline afterward
38 | elsif beat == 1
39 | print "Chaka "
40 | else
41 | puts "Ooga"
42 | end
43 |
44 | if beat == 3
45 | beat = 0
46 | bar += 1
47 | else
48 | beat += 1
49 | end
50 | end
51 |
52 | puts "I can't stop this feeling..."
53 | end
54 | ```
55 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/nil.md:
--------------------------------------------------------------------------------
1 | # Nil
2 |
3 | Here's an example of code that implicitly returns `nil`, because the last thing to get evaluated in the function (`puts`), returns `nil`:
4 | ```ruby
5 | # This function takes a catchphrase as an argument, prints it, and returns nil.
6 | def print_catchphrase(catchphrase)
7 | puts catchphrase
8 | end
9 |
10 | return_value = print_catchphrase("Bazinga!") #=> nil
11 | ```
12 |
13 | Here are two functions that try to decrease a quantity by five. `if`-`elsif` branches will return the value of the branch that gets evaluated. If no branch is evaluated (none of the conditions were true), `nil` is returned:
14 | ```ruby
15 | # This function takes an integer and if the integer is greater than or equal to
16 | # five, it returns five less than the integer. Otherwise, it returns nil.
17 | def take_five(initial_quantity)
18 | if initial_quantity >= 5
19 | initial_quantity - 5
20 | end
21 | end
22 |
23 | take_five(9) #=> 4
24 | take_five(2) #=> nil
25 | ```
26 | In the example above, if the initial_quantity was less than five, the function returned nil because the `if` condition evaluated to false.
27 |
28 | In this second example, the method adds an `elsif` branch so it can return the initial_quantity again if it was less than five.
29 | ```ruby
30 | # This function takes an integer argument. If the integer is greater than or
31 | # equal to five, it returns five less than the integer. Otherwise, if the
32 | # integer is greater than zero, it returns the integer itself. Integer inputs
33 | # that are not greater than zero cause the function to return nil.
34 | def try_to_take_five(initial_quantity)
35 | if initial_quantity >= 5
36 | initial_quantity - 5
37 | elsif initial_quantity > 0
38 | initial_quantity
39 | end
40 | end
41 |
42 | try_to_take_five(9) #=> 4
43 | try_to_take_five(2) #=> 2
44 | try_to_take_five(0) #=> nil
45 | ```
46 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/numbers.md:
--------------------------------------------------------------------------------
1 | # Numbers
2 |
3 | ## Operators
4 |
5 | Ruby can do all the usual arithmetic a calculator would do:
6 |
7 | ```ruby
8 | $ def print_some_arithmetic_results
9 | puts 40 + 2 # Addition: will print 42
10 | puts 49 - 7 # Subtraction: will print 42
11 | puts 2 * 3 # Multiplication: will print 6
12 | puts 6 / 2 # Division: will print 3
13 | puts 2**3 # Exponentiation: will print 8
14 | end
15 | ```
16 |
17 | Notice all of the numbers above are integers (they don't have decimal points).
18 |
19 | Computers store integers and floating-point numbers (like `2.5` or `3.333...`) in different ways.
20 |
21 | ## Integer Division
22 |
23 | When we do math in Ruby, our answer will be an integer if all of the inputs were integers. To make this work, ruby uses integer division, ignoring any fractional components (remainders) in the answer:
24 | ```ruby
25 | def print_some_integer_division_results
26 | puts 6 / 2 # will print 3
27 | puts 7 / 2 # will print 3, ignoring the remainder of 1
28 | puts 8 / 2 # will print 4
29 | end
30 | ```
31 |
32 | If we are interested in the fractional component of division, we can use the modulo operator:
33 | ```ruby
34 | def print_some_modulo_results
35 | puts 6 % 2 # will print 0, 6 divides evenly by 2
36 | puts 7 % 2 # will print 1, there is a remainder of 1 when 7 is divided by 2
37 | puts 8 % 2 # will print 0, there is no remainder when 8 is divided by 2
38 | end
39 | ```
40 |
41 | To force a floating-point answer, we can make one of our inputs a float:
42 | ```ruby
43 | def print_some_more_division_results
44 | puts 7.0 / 2 # will print 3.5
45 | puts 7 / 2.0 # will print 3.5
46 | end
47 | ```
48 |
49 | ## Shorthand
50 |
51 | Ruby has a nice shorthand for combining arithmetic with variable assignment:
52 |
53 | Long Expression | Shorthand
54 | ---------------------------------|-----------
55 | `number = number + 1` | `number += 1`
56 | `number = number - 2` | `number -= 2`
57 | `number = number * other_number` | `number *= other_number`
58 | `number = number / 3` | `number /= 3`
59 | **Note**: There are many valid shorthand operators, but these are the most common.
60 |
61 | Here are two functions that make use of the shorthand:
62 |
63 | ```ruby
64 | def number_doubler(number)
65 | number *= 2 # shorthand for: number = number * 2
66 | return number
67 | end
68 |
69 | def next_smallest_integer(integer)
70 | integer -= 1 # shorthand for: integer = integer - 1
71 | return integer
72 | end
73 | ```
74 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/output.md:
--------------------------------------------------------------------------------
1 | # Output
2 |
3 | `puts` and `p` are both methods that take an argument and print it. To make our
4 | code more readable, we usually omit the parentheses.
5 |
6 | ```ruby
7 | # This function prints four phrases, collecting the return value for each. These
8 | # values are returned as an array.
9 | def collect_p_and_puts_return_values
10 | first_puts = puts "This is shorthand"
11 | second_puts = puts("for this.")
12 |
13 | first_p = p "The same is true"
14 | second_p = p("for p.")
15 |
16 | return [first_puts, second_puts, first_p, second_p]
17 | end
18 |
19 | collect_p_and_puts_return_values # => [nil, nil, "The same is true", "for p."]
20 | ```
21 |
22 |
23 | `p` is also shorthand for calling `puts` with `#inspect`.
24 |
25 | For example:
26 |
27 | ```ruby
28 |
29 | my_array = [1, 2, 3]
30 |
31 | puts my_array
32 | # 1
33 | # 2
34 | # 3
35 | # => nil
36 |
37 | p my_array
38 | # [1, 2, 3]
39 | # => [1, 2, 3]
40 |
41 | ```
42 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part1/strings.md:
--------------------------------------------------------------------------------
1 | # String Refresher
2 |
3 | Here's a quick refresher on a few String methods:
4 | ```ruby
5 | "Hey you!".length #=> 8
6 | "answer to life the universe and everything".length #=> 42
7 | "Hey!".upcase #=> "HEY!"
8 | "Hey!".downcase #=> "hey!"
9 | "bar" + "it" + "one" #=> "baritone"
10 | "#{5**2} or #{4 + 2} to #{8 / 2}" #=> "25 or 6 to 4"
11 | "you"[2] #=> "u"
12 | movie_title = "Apocalypse Now" #=> "Apocalypse Now"
13 | movie_title[11] = "C" #=> "C"
14 | movie_title #=> "Apocalypse Cow"
15 | "/Users/dak/clients/empire/projects/death_star.tar".split("/")
16 | #=> ["", "Users", "dak", "clients", "empire", "projects", "death_star.tar"]
17 | ```
18 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part2/advanced-arrays.md:
--------------------------------------------------------------------------------
1 | # Advanced Arrays
2 |
3 | Here's a quick example to refresh advanced array topics:
4 | ```ruby
5 | # This function returns a copy of an array with the
6 | # first and last elements removed.
7 | def except_first_and_last(array)
8 | array[1...-1]
9 | end
10 |
11 | eighty = Array.new(4, 20) #=> [20, 20, 20, 20]
12 | eighty[0] -= 5 #=> 15
13 | eighty[-1] += 5 #=> 25
14 | eighty #=> [15, 20, 20, 25]
15 | forty = except_first_and_last(eighty) #=> [20, 20]
16 | ```
17 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part2/advanced-enumerables.md:
--------------------------------------------------------------------------------
1 | # Advanced Enumerable Methods
2 |
3 | Ruby's `times` is a nice way to write a loop that will stop after a number of iterations.
4 | ```ruby
5 | # This method takes a quantity of pushups to perform and prints
6 | # the platoon's responses. Here's a sample output for input of 2:
7 | #
8 | # Platoon (in unison): Sir, yes sir!
9 | # Platoon: 1!
10 | # Platoon: 2!
11 | # Hup!
12 | # Hup!
13 | # Hup!
14 | #
15 | def drop_and_give_me(quantity)
16 | puts "Platoon (in unison): Sir, yes sir!"
17 |
18 | quantity.times do |index|
19 | puts "Platoon: #{index + 1}!"
20 | end
21 |
22 | 3.times do
23 | puts "Hup!"
24 | end
25 | end
26 | ```
27 | What is the return value of the method in the example above? Why?
28 |
29 | `Enumerable#map` evaluates the block for each successive item in the collection, storing the results in an array.
30 |
31 | ```ruby
32 | # This function takes a percentage,
33 | # returning the corresponding letter grade
34 | def letter_grade(percentage)
35 | letters = ("A".."D").to_a
36 |
37 | letters.each.with_index do |letter, index|
38 | return letter if percentage >= 90 - 10 * index
39 | end
40 |
41 | "F"
42 | end
43 |
44 | # This function takes an array of grades as percentages,
45 | # returning a new array of their corresponding letter grades.
46 | def grade_class(class_percentages)
47 | class_percentages.map do |student_percentage|
48 | letter_grade(student_percentage)
49 | end
50 | end
51 |
52 | grade_class([ 92.5, 88, 50, 64, 0, 70]) #=> ["A", "B", "F", "D", "F", "C"]
53 | ```
54 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part2/boolean-enumerable-methods.md:
--------------------------------------------------------------------------------
1 | # Boolean Enumerable Methods
2 |
3 | `Enumerable#any?` will return true if the block evaluates to true for any element in the collection. As soon as an element makes the block return true, `#any?` will return true without checking the remaining elements.
4 | ```ruby
5 | # This function takes an array of numbers, returning true if any
6 | # of them is evenly divisible by two.
7 | def any_even?(numbers)
8 | numbers.any? do |number|
9 | number % 2 == 0
10 | end
11 | end
12 |
13 | any_even?([1, 2, 3]) # => true
14 | any_even?([1, 3, 5]) # => false
15 | ```
16 |
17 | `Enumerable#all?` will return true if the block evaluates to true for every element in the collection. As soon as an element makes the block return false, `#all?` will return false without checking the remaining elements.
18 | ```ruby
19 | # This function takes an array of numbers, returning true if all
20 | # of them are zero or greater.
21 | def all_positive?(numbers)
22 | numbers.all? do |number|
23 | number >= 0
24 | end
25 | end
26 |
27 | all_positive?([0, -1, 1]) # => false
28 | all_positive?([1, 0, 11]) # => true
29 | ```
30 |
31 | `Enumerable#none?` will return true if the block evaluates to false for every element in the collection. As soon as an element makes the block return true, `#none?` will return false without checking the remaining elements.
32 | ```ruby
33 | # This function takes an array of numbers, returning true if none
34 | # of them is less than zero.
35 | def all_positive?(numbers)
36 | numbers.none? do |number|
37 | number < 0
38 | end
39 | end
40 |
41 | all_positive?([0, -1, 1]) # => false
42 | all_positive?([1, 0, 11]) # => true
43 | ```
44 |
45 | **Note** These methods give us a lot of flexibility to craft very readable conditions. If your condition is confusing, think about whether using a different boolean method would make it clearer.
46 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part2/function-decomposition.md:
--------------------------------------------------------------------------------
1 | # Function Decomposition
2 |
3 | In the example below, `neighbor_sums` decomposes into `neighbors_in_array` and `sum_array`.
4 |
5 | ```ruby
6 | # This function takes an array and returns a new array where each element is
7 | # replaced by the sum of its neighbors. Notice the functions it calls.
8 | def neighbor_sums(array)
9 | array.map.with_index do |number, index|
10 | neighbors = neighbors_in_array(array, index)
11 | sum_array(neighbors)
12 | end
13 | end
14 |
15 | # This function returns an array of the values of the elements directly next
16 | # to a given index.
17 | def neighbors_in_array(array, index)
18 | neighbors = []
19 |
20 | neighbors << array[index - 1] unless index == 0
21 | neighbors << array[index + 1] unless index == array.length - 1
22 |
23 | neighbors
24 | end
25 |
26 | # This function returns the sum of an array.
27 | def sum_array(array)
28 | sum = 0
29 |
30 | array.each do |number|
31 | sum += number
32 | end
33 |
34 | sum
35 | end
36 |
37 |
38 | neighbor_sums([1, 5, 3, 2, 8, 0, 6]) # => [5, 4, 7, 11, 2, 14, 0]
39 | ```
40 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part2/hash-defaults.md:
--------------------------------------------------------------------------------
1 | # Hash Defaults
2 |
3 | Here's an example showing how we can set a hash's default value to something more useful than `nil`. This example shows a very useful pattern.
4 | ```ruby
5 | # This function takes an array of letter grades,
6 | # returning a hash with each grade as the key and the number off
7 | # occurrences of the grade in the array as the value.
8 | def grade_counts(class_letter_grades)
9 | grade_counts = Hash.new(0)
10 |
11 | class_letter_grades.each do |letter_grade|
12 | grade_counts[letter_grade] += 1
13 | end
14 |
15 | grade_counts
16 | end
17 |
18 | math_grades = ["A", "B", "A", "A", "C", "F"] #=> ["A", "B", "A", "A", "C", "F"]
19 | math_counts = grade_counts(math_grades) #=> {"A"=>3, "B"=>1, "C"=>1, "F"=>1}
20 | math_counts["A"] #=> 3
21 | math_counts["D"] #=> 0
22 | math_counts["platypus"] #=> 0
23 | ```
24 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part2/hash-maps.md:
--------------------------------------------------------------------------------
1 | # Hash Maps
2 |
3 |
4 | Here's an example of how we can put build hashes and access their data.
5 | ```ruby
6 | pantry = {
7 | "eggs" => 12,
8 | "tomatoes" => 3,
9 | "peppers" => 5
10 | } #=> {"eggs"=>12, "tomatoes"=>3, "peppers"=>5}
11 |
12 | pantry["onions"] = 2 #=> {"onions" => 2}
13 | pantry["eggs"] #=> 12
14 | pantry["kielbasa"] #=> nil
15 | pantry.keys #=> ["eggs", "tomatoes", "peppers", "onions"]
16 | pantry.values #=> [12, 3, 5, 2]
17 | ```
18 |
19 | Here's a function that iterates over a hash with `each`:
20 | ```ruby
21 | # This function takes an inventory as a hash,
22 | # in the form { item => quantity }. It then prints an inventory.
23 | # Here's sample output for the input { "eggs" => 12, "tea" => 1 }:
24 | # eggs: 12
25 | # tea: 1
26 | def print_inventory(inventory)
27 | inventory.each do |item, quantity|
28 | puts "#{item}: #{quantity}"
29 | end
30 | end
31 | ```
32 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part2/inline-if-and-unless.md:
--------------------------------------------------------------------------------
1 | ### In-line `if` and `unless`
2 |
3 | * With great power comes great responsibility. You're ready.
4 | * You don't have to do this:
5 | ```ruby
6 | def even?(number)
7 | if number % 2 == 0
8 | return true
9 | end
10 | end
11 | ```
12 | * You can do this instead:
13 | ```ruby
14 | def even?(number)
15 | return true if number % 2 == 0
16 | end
17 | ```
18 | OR
19 | ```ruby
20 | def even?(number)
21 | return true unless number % 2 == 1
22 | end
23 | ```
24 | (In reality, you should just return `number % 2 == 0`)
25 | * Only do this for **one line** conditionals. Otherwise, use the long form.
26 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part2/ranges.md:
--------------------------------------------------------------------------------
1 | # Ranges
2 |
3 | Here's an example that iterates over a range:
4 | ```ruby
5 | # This function prints "5, 6, 7, 8" each on a newline, returning the range (5..8)
6 | def dance_instructor
7 | (5..8).each do |count|
8 | puts count
9 | end
10 | end
11 | ```
12 |
13 | This example employs a range of characters. Note the method **returns** the string instead of printing it:
14 | ```ruby
15 | # This function takes a letter grade as input, returning a reasonable response to the grade.
16 | def report_card_feedback(grade)
17 | passing_grades = ("A".."D").to_a
18 | failing_grade = "F"
19 |
20 | if passing_grades.include?(grade.upcase)
21 | "That'll do, I guess"
22 | elsif grade.upcase == failing_grade
23 | "Try harder"
24 | else
25 | "Nice try. #{grade} isn't a real grade!"
26 | end
27 | end
28 | report_card_feedback("c") # => "That'll do, I guess"
29 | report_card_feedback("F") # => "Try harder"
30 | report_card_feedback("Awesome") # => "Nice try. Awesome isn't a real grade!"
31 | ```
32 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part3/advanced-iteration.md:
--------------------------------------------------------------------------------
1 | # Advanced Iteration
2 |
3 | ```ruby
4 | # This function takes a player's shooting record as an array of "miss"ed and
5 | # "made" shots. It prints the appropriate commentary for each shot using the
6 | # following rules:
7 | # second shot made in a row: print "He's heating up"
8 | # third shot made in a row: print "He's on fire"
9 | # fourth through seventh: print "Boom-shaka-laka!"
10 | # The streak is reset if eight shots in a row are made.
11 |
12 | def nba_jam(shots)
13 | current_streak = 0
14 |
15 | shots.each do |shot|
16 | if shot == "made"
17 | current_streak += 1
18 | else
19 | current_streak = 0
20 | end
21 |
22 | if current_streak == 2
23 | puts "He's heating up"
24 | elsif current_streak == 3
25 | puts "He's on fire"
26 | elsif current_streak > 3 && current_streak < 8
27 | puts "Boom-shaka-laka!"
28 | elsif current_streak > 8
29 | current_streak = 0
30 | end
31 | end
32 |
33 | end
34 |
35 | shaqs_shooting_record = ['made', 'made', 'miss', 'made', 'made', 'made', 'made']
36 | nba_jam(shaqs_shooting_record)
37 | # He's heating up
38 | # He's heating up
39 | # He's on fire
40 | # Boom-shaka-laka!
41 | ```
42 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part3/enumerables++.md:
--------------------------------------------------------------------------------
1 | # Enumerables++
2 |
3 | Here's an example that uses `count` with a block to get the number of elements that meet a criteria:
4 | ```ruby
5 | # This function takes an array of integers and returns the number of them
6 | # that exceed 85.
7 | def number_of_high_scoring_students(class_percentages)
8 | class_percentages.count do |percentage|
9 | percentage > 85
10 | end
11 | end
12 | number_of_high_scoring_students([55, 100, 35, 75, 90]) # => 2
13 | ```
14 |
15 | Here's an example that uses `select` and a block to filter an array:
16 | ```ruby
17 | # This function takes a paragraph of text as a string and returns an array of
18 | # all the four letter words.
19 | def four_letter_words(paragraph)
20 | paragraph.split.select do |word|
21 | word.length == 4
22 | end
23 | end
24 | marc_antony = "Friends, Romans, countrymen, lend me your ears"
25 | four_letter_words(marc_antony) # => ["lend", "your", "ears"]
26 | ```
27 |
28 | Here's an example using `reject`, returning the items cause the block to return false.
29 | ```ruby
30 | # This function takes a hash in the form { item => quantity }, returning
31 | # an array of the items with a quantity of three or less.
32 | def shopping_list(pantry)
33 | pantry.reject { |item, quantity| quantity > 3 }.keys
34 | end
35 | my_pantry = {"eggs" => 12, "pies" => 1, "rice" => 3}
36 | shopping_list(my_pantry) # => ["pies", "rice"]
37 | ```
38 |
39 | This example shows `reduce`, which passes the return value from the previous iteration into the next iteration as the variable `sum`
40 | ```ruby
41 | # This function takes an array of numbers and returns the mean (average).
42 | # Note: the average is obtained using integer division.
43 | def average_percentage(class_percentages)
44 | scores_sum = class_percentages.reduce do |sum, percentage|
45 | sum + percentage # The return value of this block
46 | end # becomes the sum for the next iteration.
47 |
48 | scores_sum / class_percentages.length
49 | end
50 | average_percentage([55, 100, 35, 75, 90]) # => 71
51 | ```
52 |
53 | This function uses `max_by` to find the item which causes the block to return the highest value.
54 | ```ruby
55 | # This function takes an array of numbers and returns the number that would
56 | # be farthest from +5 on a number line.
57 | def farthest_from_five(numbers)
58 | numbers.max_by do |number|
59 | (number - 5).abs
60 | end
61 | end
62 | farthest_from_five([-4, -6, 0, 6, 12]) # => -6
63 | ```
64 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part3/more-array-methods.md:
--------------------------------------------------------------------------------
1 | # More Array Methods
2 |
3 | Here's a quick demo of some more array methods:
4 | ```ruby
5 | [1, 2, 3].reverse #=> [3, 2, 1]
6 | [1, 2, 3].min #=> 1
7 | [1, 2, 3].max #=> 3
8 | [9, 4, 7].sort #=> [4, 7, 9]
9 | [1, 1, 2].uniq #=> [1, 2]
10 | ["e", "a", "r"].shuffle #~> ["r", "a", "e"]
11 | ```
12 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part3/more-string-methods.md:
--------------------------------------------------------------------------------
1 | # More String Methods
2 |
3 |
4 | ```ruby
5 | # This function takes a string and encrypts it by replacing every instance of
6 | # "President" with "Eagle".
7 | def secret_service_encrypt(plaintext)
8 | plaintext.gsub("President", "Eagle")
9 | end
10 | secret_service_encrypt("The President is in the Presidential suite")
11 | #=> "The Eagle is in the Eagleial suite"
12 | ```ruby
13 |
14 | The `g` in `gsub` is for global, and it replaces all the matches in the string, while `sub` only replaces the first match.
15 | ```
16 | "better".sub("e", "i") #=> "bitter"
17 | "AAA".gsub("A", "").empty? #=> true
18 | ```
19 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part3/mutation.md:
--------------------------------------------------------------------------------
1 | * Safe vs. unsafe methods. The bang, `!`, at the end of the method name denotes an unsafe method.
2 | * The bang generally means the method mutates the input.
3 | * Pay attention to whether you need to re-assign your variable, or mutate it in your function.
4 | * Some Ruby objects are **immutable**, which means they cannot mutate. You must create a new and different object and re-assign your variable if you want to change an immutable object.
5 | * Things that are immutable:
6 | * Booleans (`true`/`false`)
7 | * `nil`
8 | * Numbers
9 | * Arrays and hashes are **mutable**. `[1,2,3,[4,5,6],7]` is an array with some immutable elements, and one mutable element. This could make a difference if you are iterating over elements of an array and changing them as you go.
10 | * Here's a rule of thumb for mutability—if you can pass it into a function and change the object, then it's mutable. Otherwise it's not.
11 | * Think about it: can you imagine something immutable, like `true` or `0` changing because you called some function?
12 |
13 | ```ruby
14 | variable = false
15 | crazy_function_that_does_crazy_things!(variable)
16 | puts variable == false # no function can change its value!
17 | ```
18 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part3/nested-iteration.md:
--------------------------------------------------------------------------------
1 | # Nested Iteration
2 |
3 | Here's an example of nested iteration in code:
4 |
5 | 
6 |
7 | There's one "until" nested inside of another "until".
8 |
9 | ```ruby
10 | # This function takes an integer number of minutes and an integer number of
11 | # seconds as input and proceeds to count down, printing the remaining number
12 | # of minutes and seconds in the form m:ss, until the time reaches 0:00.
13 | # Here's sample output for the egg_timer(2, 1):
14 | #
15 | # 2:01
16 | # 2:00
17 | # 1:59
18 | # ...
19 | # 0:01
20 | # 0:00
21 | # Time's up!
22 | def egg_timer(minutes, seconds)
23 |
24 | while minutes >= 0
25 |
26 | while seconds >= 0
27 | puts "#{minutes}:#{'%02d' % seconds}"
28 | # sleep 1 # pauses the function for 1 second.
29 |
30 | seconds -= 1
31 | end
32 |
33 | seconds = 59
34 | minutes -= 1
35 | end
36 |
37 | puts "Time's up!"
38 | end
39 | ```
40 |
41 | There's one `while` nested inside of another `while`.
42 |
43 | ```ruby
44 | def color_mixer(colors)
45 | mixes = []
46 |
47 | colors.each do |first_color|
48 | colors.each do |second_color|
49 | mixes << "#{first_color}-#{second_color}" unless first_color == second_color
50 | end
51 | end
52 |
53 | mixes
54 | end
55 | color_mixer(["red", "blue", "green"]) # => [ "red-blue", "red-green",
56 | # "blue-red", "blue-green",
57 | # "green-red", "green-blue"]
58 | ```
59 | There's one `each` nested inside of another `each`.
60 |
61 | ```ruby
62 | adjectives = ["bawdy", "puny", "rank", "saucy", "yeasty", "surly"]
63 | hyphenated = ["beetle-headed", "elf-skinned", "onion-eyed", "swag-bellied"]
64 | nouns = ["lout", "scut", "strumpet", "foot-licker", "bugbear"]
65 |
66 | # This function prints every possible insult using three
67 | # arrays of words as input. Using the three arrays above,
68 | # we can generate Shakespearean insults:
69 | # insult_generator(adjectives, hyphenated, nouns)
70 | # You bawdy, beetle-headed, lout!
71 | # You bawdy, beetle-headed, scut!
72 | # You bawdy, beetle-headed, strumpet!
73 | # ...
74 | # You surly, swag-bellied, foot-licker!
75 | # You surly, swag-bellied, bugbear!
76 | #
77 | def insult_generator(first_adjectives, second_adjectives, nouns)
78 | first_adjectives.each do |first_word|
79 | second_adjectives.each do |second_word|
80 | nouns.each do |third_word|
81 | puts "You #{first_word}, #{second_word}, #{third_word}!"
82 | end
83 | end
84 | end
85 | end
86 |
87 | insult_generator(adjectives, hyphenated, nouns)
88 | ```
89 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part3/nested-iteration.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/deprecated/part3/nested-iteration.png
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part3/pass-by-reference.md:
--------------------------------------------------------------------------------
1 | # Pass-By-Reference
2 |
3 |
4 | Notice how the variable nyse always refers to the same string here, even after the function reassigns work_addres to the white house:
5 | ```ruby
6 | nyse = "11 Wall Street"
7 |
8 | # This function takes a previous work address and prints a change of address
9 | # form for a newly elected president. The function returns nil.
10 | # Here's a sample output:
11 | # I no longer work here: 11 Wall Street
12 | # Now, I work here: 1600 Pennsylvania Avenue
13 | #
14 | def get_elected(work_address)
15 | puts "I no longer work here: #{work_address}"
16 |
17 | work_address = "1600 Pennsylvania Avenue"
18 |
19 | puts "Now, I work here: #{work_address}"
20 | end
21 |
22 | get_elected(nyse) #=> nil
23 |
24 | nyse #=> "11 Wall Street"
25 | # **Note** hasn't changed.
26 | ```
27 |
--------------------------------------------------------------------------------
/jumpstart-files/deprecated/part_3_solution.rb:
--------------------------------------------------------------------------------
1 | ### Answers for some of Part 3 Exercises
2 |
3 | def longest_run_digit(number)
4 | number_as_string = number.to_s
5 | result = number_as_string[0]
6 | longest_run = 1
7 | this_run = 1
8 | (1...number_as_string.length).each do |index|
9 | if number_as_string[index] == number_as_string[index - 1]
10 | this_run += 1
11 | else
12 | if this_run > longest_run
13 | longest_run = this_run
14 | result = number_as_string[index - 1]
15 | end
16 | this_run = 1
17 | end
18 | end
19 | if this_run > longest_run
20 | longest_run = this_run
21 | result = number_as_string[-1]
22 | end
23 | result.to_i
24 | end
25 |
26 | def all_words_pair(string)
27 | words = string.split(/ /)
28 | result = []
29 | for index1 in 0...words.length - 1
30 | for index2 in (index1 + 1)...words.length
31 | result << [words[index1], words[index2]]
32 | end
33 | end
34 | result
35 | end
36 |
37 | def any_make_yahtzee?(words)
38 | 0.upto(words.length - 1) do |index1|
39 | 0.upto(words.length - 1) do |index2|
40 | if words[index1] + words[index2] == "yahtzee"
41 | return true
42 | end
43 | end
44 | end
45 | false
46 | end
47 |
48 | def longest_word(words)
49 | words.max_by(&:length)
50 | end
51 |
52 | def longest_word(words)
53 | words.max_by{|x| x.length}
54 | end
55 |
56 | def is_prime?(number)
57 | number > 1 && (2..number**0.5).to_a.count{|n| number % n == 0} == 0
58 | end
59 |
60 | def list_primes(n)
61 | (2..n).select{|number| is_prime?(number)}
62 | end
63 |
--------------------------------------------------------------------------------
/jumpstart-files/part1/README.md:
--------------------------------------------------------------------------------
1 | # Coding Basics
2 |
3 | In this section we'll talk about how ruby interacts with your computer.
4 |
5 | ## Instructions
6 |
7 | **Do every exercise.** Feel free to skim the readings and then use them as references during the exercises to fill in the gaps in your knowledge.
8 |
9 | You will not learn by simply reading about code. **You must write it too.** Do the exercises :)
10 |
11 | **Call over a TA** when you hit a "mandatory TA check point".
12 |
13 | ## Sections
14 |
15 | * Terminal [Readings](./terminal_readings.md)
16 | * Terminal [Exercises](./terminal_exercises.md)
17 | * REPL [Readings](./repl_readings.md)
18 | * REPL [Exercises](./repl_exercises.md)
19 | * Scripts & Methods [Readings](./scripts_and_methods_readings.md)
20 | * Scripts & Methods [Exercises](./scripts_and_methods_exercises.md)
21 | * Return Value [Readings](./return_readings.md)
22 | * Return Value [Exercises](./return_exercises.md)
23 | * Variables & Scope [Readings](./variables_and_scope_readings.md)
24 | * Variables & Scope [Exercises](./variables_and_scope_exercises.md)
25 |
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/repl/demo_capitalize.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/repl/demo_capitalize.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/repl/demo_list.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/repl/demo_list.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/repl/demo_math.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/repl/demo_math.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/repl/demo_truncated_list.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/repl/demo_truncated_list.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/repl/irb1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/repl/irb1.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/repl/irb2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/repl/irb2.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/repl/irb3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/repl/irb3.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/repl/irb4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/repl/irb4.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/repl/pry1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/repl/pry1.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/repl/pry2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/repl/pry2.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/repl/pry3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/repl/pry3.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/scripts/arguments.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/scripts/arguments.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/scripts/first_method1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/scripts/first_method1.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/scripts/first_method2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/scripts/first_method2.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/scripts/first_script.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/scripts/first_script.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/terminal/cd.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/terminal/cd.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/terminal/clear1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/terminal/clear1.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/terminal/clear2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/terminal/clear2.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/terminal/example_terminal_command.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/terminal/example_terminal_command.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/terminal/ls.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/terminal/ls.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/terminal/man-page.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/terminal/man-page.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/terminal/mkdir.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/terminal/mkdir.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/terminal/pwd.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/terminal/pwd.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/terminal/ruby1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/terminal/ruby1.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/assets/terminal/ruby2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part1/assets/terminal/ruby2.png
--------------------------------------------------------------------------------
/jumpstart-files/part1/repl_exercises.md:
--------------------------------------------------------------------------------
1 | # REPL Exercises
2 |
3 | 0. Install `pry` via the instructions in the reading
4 |
5 | 0. Use pry to figure out what 732 * 114 is
6 | * Create a variable, x, and then assign it to the last return value by using the underscore, `_`
7 | * Then, divide x by 4
8 |
9 | 
10 |
11 | 0. Capitalize a string by using ruby's `#capitalize` method
12 |
13 | 
14 |
15 | 0. Type `Integer.instance_methods`, notice that this returns an `Array` of method names that are available for `Integers`. This output is *really* long. We could use the down arrow to scroll down to the bottom, but that would take forever...
16 | * Press `Q` to truncate the list and get a new pry prompt
17 |
18 | 
19 | 
20 |
21 | 0. Re-run `Integer.instance_methods`
22 | * Look through the list for methods you haven't seen before. Test them out!
23 | * Again, use `Q` to truncate the output
24 | * Repeat the process with `String.instance_methods`
25 |
--------------------------------------------------------------------------------
/jumpstart-files/part1/repl_readings.md:
--------------------------------------------------------------------------------
1 | ## REPLs
2 |
3 | ## Introduction
4 |
5 | The Interactive Ruby Shell, more commonly known as IRB, is one of the most important tools for a new developer. IRB allows you to quickly test small bits of code without having to create new scripts. IRB is a type of *repl*.
6 |
7 | To get it up and running, all you need to do is type the following into a terminal prompt:
8 |
9 | `irb`
10 |
11 | 
12 |
13 | This launches IRB. Inside, you can perform basic ruby methods, like adding two numbers:
14 |
15 | 
16 |
17 | You can also create variables:
18 |
19 | 
20 |
21 | You can leave irb by typing `exit`
22 |
23 | 
24 |
25 | ## Pry
26 |
27 | Pry is a slightly nicer repl; it's color-coded and formats output nicely for us. Install pry by running:
28 |
29 | `gem install pry`
30 |
31 | 
32 |
33 | Start pry by just typing:
34 |
35 | `pry`
36 |
37 | (You leave in the same manner, `exit`)
38 |
39 | 
40 |
41 | Oh the colors! :)
42 |
43 | 
44 |
--------------------------------------------------------------------------------
/jumpstart-files/part1/return_exercises.md:
--------------------------------------------------------------------------------
1 | # Return Value Exercises
2 |
3 | 0. Create a new file called `return_exercises.rb`
4 |
5 | 0. Write a method called `#times_three_and_plus_five` that accepts an integer, n, as an argument. Your method should multiply n by 3, and then add 5. For example, `times_three_and_plus_five(4)` should return 17. Do this using an **explicit return**.
6 |
7 | 0. Change your `#times_three_and_plus_five` method to instead use an **implicit** return.
8 |
9 | 0. Open pry and play around with the `#even?` and `#odd?` methods for integers. We'll use these in the next problem.
10 |
11 | ```ruby
12 | 5.odd? # ==> true
13 | 5.even? # ==> false
14 | ```
15 |
16 | 0. Write a method called `#even_or_odd`, that accepts an integer as an argument. Your method should return the string "even" if the integer is even, and return "odd" if the integer is odd. Follow the pattern used in the `#is_billy?` method from the reading. Also, use the `#even?` and `#odd?` methods we explored in the previous step.
17 |
18 | 0. Read the [solutions](../solutions/part1/return_solutions.md)
19 |
--------------------------------------------------------------------------------
/jumpstart-files/part1/return_readings.md:
--------------------------------------------------------------------------------
1 | # Return Values
2 |
3 | ## Introduction
4 |
5 | **All methods have a return value.** Meaning, if I set a variable equal to the result of invoking some method, then that variable will have some value.
6 |
7 | ```ruby
8 | def multiply(a, b)
9 | return a * b
10 | end
11 |
12 | product = multiply(3, 5)
13 |
14 | puts product # ==> 15
15 | ```
16 |
17 | Above, `product` is a variable that stores the *return value* of the multiply method.
18 |
19 | Until now, we have been building methods that `puts` useful information to the terminal. For the majority of this course, we will write methods that are supposed to return a particular value. We can then `puts` the return value to see it.
20 |
21 | ---
22 | ### Native Methods
23 |
24 | In addition to the methods we create using `def ... end`, all of ruby's built in methods have return values. Some that you may have seen already include:
25 |
26 | ```ruby
27 | "string".capitalize # ==> "String"
28 | 5.even? # ==> false
29 | puts "hello" # ==> nil
30 | ```
31 |
32 | That's right! Even the `#puts` method has a return value, and it's the `nil` object (we'll talk more about nil later). Don't believe me? Open `pry`
33 | and type:
34 |
35 | ```ruby
36 | x = puts "Hello, World"
37 | puts x
38 | ```
39 |
40 | ---
41 |
42 | ## Implicit vs Explicit Returns
43 |
44 | Ruby has two types of returns: *implicit* and *explicit*.
45 |
46 | ### Explicit Returns
47 |
48 | Methods that have *explicit* returns use the `return` statement. If you don't see the word `return` somewhere in the method, than the method has no explicit returns.
49 |
50 | Explicit returns are used to *return early* from a method. This means that once the method hits the `return` statement, it will stop executing!
51 |
52 | ```ruby
53 | def say_hi_not_bye
54 | return "hello!"
55 | puts "will you see me? byyeee"
56 | end
57 | ```
58 |
59 | In the method above, we will never invoke the `puts` method. You should also know that a method can only ever have **one return value**.
60 |
61 | ```ruby
62 | def add_and_multiply(a, b)
63 | return a + b
64 | return a * b
65 | end
66 | ```
67 |
68 | The above method **doesn't make sense**. We will only ever hit the first return statement.
69 |
70 | ### Implicit Returns
71 |
72 | Since we already noted that **all methods must have a return value**, what happens if we don't write the word `return` in our method?
73 |
74 | ```ruby
75 | def multiply(a, b)
76 | a * b
77 | end
78 |
79 | product = multiply(3, 5)
80 |
81 | puts product # ==> ?
82 | ```
83 |
84 | As it turns out, **this works!** If no `return` statement is made, then ruby will default to using the last line evaluated as the return value. You will commonly see methods that use *both* implicit and explicit returns.
85 |
86 | ```ruby
87 | def is_billy?(name)
88 | if name == "billy"
89 | return true
90 | end
91 | false
92 | end
93 |
94 | puts is_billy?("avi") # ==> false
95 | puts is_billy?("billy") # ==> true
96 | ```
97 |
98 | We used a conditional (an `if` statement) in the above code snippet, and we'll get to those soon! But for now, try to focus on the the return value from the method `#is_billy?`. Here, we have both an implicit return *and* an explicit return. If the person in question is in fact billy, then we use an *explicit* return to return true! Otherwise, we use an *implicit* return to return false.
99 |
100 | ---
101 |
102 | ### Common `puts` Mistake
103 |
104 | A common mistake among beginner rubyists is to use the `#puts` method as the return value of their custom method. Here's an example:
105 |
106 | ```ruby
107 | def multiply(a, b)
108 | product = a * b
109 | puts product
110 | end
111 |
112 | answer = multiply(2, 3)
113 | puts answer
114 | ```
115 |
116 | Compare the script above to the following:
117 |
118 | ```ruby
119 | def multiply(a, b)
120 | product = a * b
121 | return product
122 | end
123 |
124 | answer = multiply(2, 3)
125 | puts answer
126 | ```
127 |
--------------------------------------------------------------------------------
/jumpstart-files/part1/scripts_and_methods_exercises.md:
--------------------------------------------------------------------------------
1 | # Scripts and Methods Exercises
2 |
3 | 0. Create a new file, `methods_exercises.rb`. Define the following methods in this script:
4 |
5 | 0. Write a method `#print_thrice`, that accepts a string as an argument. It should `puts` that string 3 times.
6 |
7 | ```ruby
8 | def print_thrice(string)
9 | # Your code here
10 | end
11 | ```
12 |
13 | 0. Write a method, `#print_thrice_thrice`, that also accepts a string as an argument. It should call `print_thrice` with that string three times (for 9 total prints).
14 |
15 | 0. Write a method, `#add_multiply_subtract`, that accepts two integers as arguments. Your method should do the following:
16 | * add the two numbers
17 | * multiply the two numbers
18 | * subtract the sum from the product
19 | * puts the result
20 |
21 | You should create 3 variables (sum, product, difference) to help keep your code neat and clean!
22 |
23 |
24 | 0. Test your methods by invoking them in your script and running your script from the terminal.
25 |
26 | 0. Read the [solutions](../solutions/part1/scripts_and_methods_solutions.md)
27 |
--------------------------------------------------------------------------------
/jumpstart-files/part1/scripts_and_methods_readings.md:
--------------------------------------------------------------------------------
1 | # Scripts and Methods
2 |
3 | ## Introduction
4 |
5 | Let's start this section with a few definitions:
6 |
7 | * **script** --> A file that contains code. We run ruby scripts from the terminal by typing `ruby script_name.rb`.
8 | * **method** --> A function or set of commands that are bundled together. We can use scripts to define methods and execute them.
9 |
10 | You've already used several methods. `#capitalize` and even `+` (integer addition) are ruby methods! We haven't written any scripts yet. Let's change that!
11 |
12 | ## Our first script
13 |
14 | * Create a new file called `my_script.rb`
15 | * Enter the following code into that file:
16 |
17 | ```ruby
18 | puts "hello, world!"
19 | ```
20 |
21 | * Using the terminal, navigate to the directory containing `my_script.rb`
22 | * Tell your computer to run the script by typing: `ruby my_script.rb`
23 |
24 | 
25 |
26 | Notice that we used the `puts` method to tell the computer to print the string to the terminal. Nothing is printed automatically from scripts (like it is in `pry`/`irb`). Instead, we have to tell the computer manually whenever we want it to print something.
27 |
28 | ## Our First Method
29 |
30 | Let's change our `my_script.rb` file to instead look like this:
31 |
32 | ```ruby
33 | def say_hello
34 | puts "hello"
35 | end
36 | ```
37 |
38 | In this file, we're now defining a method called `#say_hello` (the '#' in documentation is used to tell the reader that we're talking about a method). `#say_hello` is responsible for printing 'hello' to the screen.
39 |
40 | Let's run our script.
41 |
42 | 
43 |
44 | Notice that nothing happens. Well, in our script, we *defined* a method, but we never *invoked* the method. Let's amend our script to also invoke `#say_hello`.
45 |
46 | ```ruby
47 | def say_hello
48 | puts "hello"
49 | end
50 |
51 | say_hello
52 | ```
53 | Let's run our script again.
54 |
55 | 
56 |
57 | ## Arguments
58 |
59 | Arguments are pieces of information that we pass to methods. Let's re-write our script to instead define a method called `#say_hello_to`. This method should *accept one argument*, the name of the person to say hello to!
60 |
61 |
62 | ```ruby
63 | def say_hello_to(name)
64 | puts "hello, " + name
65 | end
66 |
67 | say_hello_to("Avi")
68 | say_hello_to("Rosalie")
69 | say_hello_to("Ksenija")
70 | ```
71 |
72 | Let's run our new script.
73 |
74 | 
75 |
76 | **95% of JumpStart will be focused on defining methods**
77 |
78 | From this point on, you will be expected to create scripts, define methods, and run them.
79 |
--------------------------------------------------------------------------------
/jumpstart-files/part1/terminal_exercises.md:
--------------------------------------------------------------------------------
1 | # Terminal Exercises
2 |
3 | 0. Practice using `cd`, `pwd`, and `ls` to explore your computer's directory structure. Enter `cd ..` until you go all the way down to the root directory, `/`. Look at the various directories that make up a Linux and OSX computer system. Don't worry, you're not going to break anything.
4 |
5 | 0. If you're using your computer's native terminal, navigate to the directory of your desktop. This is usually `~/Desktop`. If you're using Cloud9, navigate to the directory of your workspace. This is usually `~/workspace`. Use the `mkdir` command to make a new folder. See it appear!? (Feel free to delete it afterwards).
6 |
7 | 0. Look at the 'man pages' for one of the commands in the reading and try out some of the different options. `ls` has a ton, for example.
8 |
9 | 0. Run the last three commands you did, without typing them out. Use the up arrow and see what happens.
10 |
--------------------------------------------------------------------------------
/jumpstart-files/part1/variables_and_scope_exercises.md:
--------------------------------------------------------------------------------
1 | # Variables & Scope Exercises
2 |
3 | 0. For the following script, draw the variables / pointers and how they interact with objects in memory. Refer to the video in the reading for help!
4 |
5 | ```ruby
6 | a = 5
7 | b = a
8 | c = b * a
9 | a = 6
10 |
11 | puts a
12 | puts b
13 | puts c
14 | ```
15 |
16 | 0. Draw the pointers for the following script. Use an asterisk to represent variables of the same name, but different scope. (ex `a*`)
17 |
18 | ```ruby
19 | def multiply(a, b)
20 | c = a * b
21 | return c
22 | end
23 |
24 | a = 5
25 | c = a
26 | b = multiply(a, c)
27 |
28 | puts b
29 | ```
30 |
--------------------------------------------------------------------------------
/jumpstart-files/part1/variables_and_scope_readings.md:
--------------------------------------------------------------------------------
1 | # Variables and Scope
2 |
3 | ## Introduction
4 |
5 | We've already used variables a number of times so far, but let's talk about them in greater detail!
6 |
7 | ## Variables
8 |
9 | Similar to variables in algebra, we can create variables in ruby that will provide a name to certain objects so that we can access them later. As an example:
10 |
11 | ```ruby
12 | x = 5
13 | ```
14 |
15 | This code stores the number 5 into a variable called `x`. Later on, we can use this variable to retrieve the value:
16 |
17 | ```ruby
18 | x = 5
19 | puts x
20 | ```
21 |
22 | In ruby, the equal sign, `=`, is known as the **assignment operator**. Ruby first evaluates everything to the right of the `=`, and then stores the result into the variable on the left-hand side.
23 |
24 | ---
25 |
26 | ### What can a variable store?
27 |
28 | **Any object** in Ruby can be stored in a variable. Some types of objects we're already familiar with:
29 |
30 | ```ruby
31 | a = 5
32 | b = "string"
33 | ```
34 |
35 | And some objects we'll learn about later, but we can still store them in variables!
36 |
37 | ```ruby
38 | arr = [1, 2, 3]
39 | t = true
40 | n = nil
41 | ```
42 |
43 | ---
44 |
45 | ### Variable Re-assignment
46 |
47 | You can also re-assign a variable so that it refers to a different value:
48 |
49 | ```ruby
50 | # Assignment
51 | my_favorite_author = "Franz Kafka"
52 | puts my_favorite_author
53 |
54 | # Re-assignment
55 | my_favorite_author = "Aldous Huxley"
56 | puts my_favorite_author
57 | ```
58 |
59 | This means that the value assigned to a variable can change over time. Let's look at another example:
60 |
61 | ```ruby
62 | a = 5
63 | b = a
64 | c = b
65 |
66 | a = 7
67 |
68 | puts a # ==> 7
69 | puts b # ==> 5
70 | puts c # ==> 5
71 | ```
72 |
73 | Run this example in the pry and confirm the output!
74 |
75 | Let's watch an example of how variable re-assignment works.
76 |
77 | [](https://vimeo.com/181828680)
78 |
79 | ---
80 |
81 | ## Variable Naming
82 |
83 | Variable names in ruby can be made with any combination of:
84 | * lowercase letters: a - z
85 | * uppercase letters: A - Z
86 | * numbers: 0 - 9
87 | * underscores: _
88 |
89 | The following are valid variable names:
90 | * `x`
91 | * `x3`
92 | * `a88HDK83H`
93 | * `_`
94 | * `__5_`
95 |
96 | ### Rules
97 |
98 | A ruby variable name must obey the following rules:
99 | * Cannot start with a number (ex: `3x`)
100 | * No special characters (@ # $ % ^ & | , ' " ...)
101 | * Cannot be one of ruby's keywords (below)
102 |
103 | ### Rubyisms
104 |
105 | Ruby developers follow a particular set of guidelines called Rubyisms. (We'll talk more about these later). For now, know that you should adhere to the following guidelines when naming variables:
106 |
107 | * all lower case letters
108 | * use **snake case**
109 |
110 | #### Snake Case
111 |
112 | Snake case is the pattern of separating words with underscores. Some variable names that might use snake case include:
113 |
114 | ```ruby
115 | my_favorite_number = 5
116 | your_favorite_number = 8
117 | ```
118 |
119 | ### Keywords
120 |
121 | The following collection of words are called **keywords** or **reserved words**. We can't use them as variable names. Here is a list of all the **keywords**:
122 |
123 | ```ruby
124 | __FILE__ and def end in or self unless
125 | __LINE__ begin defined? ensure module redo super until
126 | BEGIN break do false next rescue then when
127 | END case else for nil retry true while
128 | alias class elsif if not return undef yield
129 | ```
130 |
131 | ---
132 |
133 | ## Scope
134 |
135 | The word "scope" is used to describe what context a variable is defined in. For example:
136 |
137 | ```ruby
138 | def set_a
139 | a = 10
140 | end
141 |
142 | a = 7
143 | set_a
144 |
145 | puts a
146 | ```
147 |
148 | What will the value of `a` be when we `puts` `a`? Go find out! Here is a very important concept in ruby:
149 |
150 | **ruby methods have their own scope**
151 |
152 | This means that `a` inside of the `#set_a` method exists completely independently from the `a` outside the method. They don't even know one another exists! Let's show this method scoping another way..
153 |
154 | ```ruby
155 | def set_b
156 | b = 10
157 | end
158 |
159 | set_b
160 |
161 | puts b
162 | ```
163 |
164 | The above code will throw an error. This is because the variable `b` doesn't exist in the scope where we try to call `puts` `b`.
165 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/README.md:
--------------------------------------------------------------------------------
1 | # Ruby Basics
2 |
3 | In this section we'll talk about some of the fundamental objects in ruby:
4 | * `Fixnums` and `Floats`
5 | * `Strings`
6 | * Booleans and `nil`
7 |
8 | We'll also explore comparators, conditionals, and loops.
9 |
10 | ## Instructions
11 |
12 | **Do every exercise.** Feel free to skim the readings and then use them as references during the exercises to fill in the gaps in your knowledge.
13 |
14 | You will not learn by simply reading about code. **You must write it too.** Do the exercises :)
15 |
16 | **Call over a TA** when you hit a "mandatory check point".
17 |
18 | ## Sections
19 |
20 | * Numbers [Readings](./numbers_readings.md)
21 | * Numbers [Exercises](./numbers_exercises.md)
22 | * Strings [Readings](./strings_readings.md)
23 | * Strings [Exercises](./strings_exercises.md)
24 | * Type Conversion [Readings](./type_conversion_readings.md)
25 | * Type Conversion [Exercises](./type_conversion_exercises.md)
26 | * Boolean Logic [Readings](./boolean_logic_readings.md)
27 | * Boolean Logic [Exercises](./boolean_logic_exercises.md)
28 | * Conditionals [Readings](./conditionals_readings.md)
29 | * Conditionals [Exercises](./conditionals_exercises.md)
30 | * Loops [Readings](./loops_readings.md)
31 | * Loops [Exercises](./loops_exercises.md)
32 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/boolean_logic_exercises.md:
--------------------------------------------------------------------------------
1 | # Boolean Logic Exercises
2 |
3 | 0. Define a method, `odd_integer?`, that accepts an integer, `n`, as an argument. Your method should return `true` or `false` based on whether `n` is odd or even. Do not use the built in `#odd?` method. Instead, think about how you could use the modulo operator, `%`, to determine whether an integer is odd or even.
4 |
5 | ```ruby
6 | odd_integer?(5) # ==> true
7 | odd_integer?(6) # ==> false
8 | ```
9 |
10 | 0. Define a method, `before_tarantula?`, that accepts a string as an argument. The method should return `true` if the argument comes before the word "tarantula" alphabetically.
11 |
12 | ```ruby
13 | before_tarantula?("baboon") # ==> true
14 | before_tarantula?("tarantula") # ==> false
15 | before_tarantula?("yak") # ==> false
16 | ```
17 |
18 | 0. Here is an example of a truth table:
19 |
20 | A | B | !A | A && B | A || B
21 | -------|-------|-------|----------|-------------|
22 | `true` |`true` |`false`|`true` | `true`
23 | `true` |`false`|`false`|`false` | `true`
24 | `false`|`true` |`true` |`false` | `true`
25 | `false`|`false`|`true` |`false` | `false`
26 |
27 | Fill out the following truth table:
28 |
29 | A | B | !A && B | !(A || B) | (A && B) || !B
30 | -------|-------|------------|-------------|--------------------|
31 | `true` |`true` | | |
32 | `true` |`false`| | |
33 | `false`|`true` | | |
34 | `false`|`false`| | |
35 |
36 | 0. Define a method, `#same_type?` that accepts any two objects as arguments. Your method should return true if both objects are the same type (`String`, `Fixnum`, etc.). Otherwise, return false.
37 |
38 | ```ruby
39 | same_type?(5, "abc") # ==> false
40 | same_type?(5, 10) # ==> true
41 | same_type?(5.0, 10) # ==> false
42 | same_type?(true, false) # ==> false
43 | same_type?(nil, nil) # ==> true
44 | ```
45 |
46 | To aid you in solving this problem, you can use the `#class` method, which tells you which class a ruby object belongs to.
47 |
48 | ```ruby
49 | 5.class # ==> Fixnum
50 | "abc".class # ==> String
51 | ```
52 |
53 | 0. Read the [solutions](../solutions/part2/boolean_logic_solutions.md)
54 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/boolean_logic_readings.md:
--------------------------------------------------------------------------------
1 | # Boolean Logic
2 |
3 | ## Introduction
4 |
5 | We're going to introduce several concepts in this reading. Three types of ruby objects:
6 | * `true`
7 | * `false`
8 | * `nil`
9 |
10 | We'll also talk about **comparators**, such as "does a equal b?", or "is c greater than d?".
11 |
12 | Then, we'll introduce **logical operators** such as `and`, `or`, and `not`.
13 |
14 | Finally, we'll discuss the concepts of **truthiness** and **falseness**.
15 |
16 | ## Booleans
17 |
18 | Booleans are `true` and `false`. That's it. They are their own objects, with their own methods! For example:
19 |
20 | ```ruby
21 | true.to_s # ==> "true"
22 | ```
23 |
24 | Boolean objects are the *return value* of comparator methods. One that we've already seen is the `==` method. Again, the *return value* of the `==` method is either `true` or `false`.
25 |
26 | ```ruby
27 | 1 == "1" # ==> false
28 | "pizza" == "pizza" # ==> true
29 | false == false # ==> true
30 | true == "true" # ==> false
31 | ```
32 |
33 | **Test these in pry!**
34 |
35 | Since `==` is a *method* with a *return value* you can capture that return value in a variable and use it elsewhere.
36 |
37 | ```ruby
38 | result = (1 == 1)
39 | puts result # ==> true
40 | ```
41 |
42 | ## `nil`
43 |
44 | `nil` is another object in ruby. It represents nothingness. `nil` also has it's own methods!
45 |
46 | ```ruby
47 | nil.to_i # ==> 0
48 | ```
49 |
50 | `nil` is often the return value of a method **if you ask for something that doesn't exist.** For example, *what is the 4th character in a 3-character string?*
51 |
52 | ```ruby
53 | "abc"[3] # ==> nil
54 | # remember, indices are 0-based, so "abc"[3] is
55 | # asking for the 4th character
56 | ```
57 |
58 | **Test this in pry!**
59 |
60 | ### Boolean Methods
61 |
62 | Rubyists customarily name "boolean methods" with a question mark at the end. A "boolean method" is just a method whose return value is either `true` or `false`. Some examples of built-in boolean methods include:
63 |
64 | ```ruby
65 | 5.even? # ==> false
66 | 5.odd? # ==> true
67 | "abc".start_with?("ab") # ==> true
68 | "abc".is_a?(String) # ==> true
69 | ```
70 |
71 | ---
72 |
73 | ## Comparators
74 |
75 | Ruby has several comparator methods that allow us to compare two objects. Remember, the return value of a comparator method is either `true` or `false`!
76 |
77 | Method| Example | Result
78 | ------|----------------|---------
79 | `==` | `nil == false` | `false`
80 | `<` | `3 < 4` | `true`
81 | `>` | `"B" > "A"` | `true`
82 | `<=` | `"C" <= "B"` | `false`
83 | `>=` | `9 >= 9` | `true`
84 |
85 | **Test these in pry!** Note: you can compare strings by alphabetical order using `<`, `>`, `<=`, or `>=`. Also note that any two objects can be compared using `==`, but only some objects can be compared using the `<`, `>`, `<=`, or `>=`. For example:
86 |
87 | ```ruby
88 | "string" >= 5
89 | ```
90 |
91 | This comparison will throw an error. It doesn't make sense! How do you compare a string to an integer?
92 |
93 | ---
94 | ## Logical Operators
95 |
96 | Logical operators are like the `+`, `-`, `*`, and `/` of the boolean world! We can operate on booleans in 3 primary manners:
97 |
98 | ### And: `&&`
99 |
100 | The **and** operator (`&&`) combines two boolean values. If *both* of them are true, it evaluates to true. If either or both of them are false, it evaluates to false:
101 |
102 | ```ruby
103 | puts true && true # ==> true
104 | puts true && false # ==> false
105 | puts false && true # ==> false
106 | puts false && false # ==> false
107 | ```
108 |
109 | **Test these in pry!**
110 |
111 | ### Or: `||`
112 |
113 | The **or** operator (`||`) evaluates to true if *one or both* of the boolean values is true. It only evaluates to false if both of the booleans are false:
114 | ```ruby
115 | puts true || true # ==> true
116 | puts true || false # ==> true
117 | puts false || true # ==> true
118 | puts false || false # ==> false
119 | ```
120 |
121 | **Test these in pry!**
122 |
123 | ### Not: `!`
124 |
125 | The **not** operator (`!`) for boolean values works like a negative sign does for numbers. It returns the opposite of the value directly to its right:
126 |
127 | ```ruby
128 | puts !true # ==> false
129 | puts !false # ==> true
130 | ```
131 |
132 | **Test these in pry!**
133 |
134 | ---
135 | ## Truthiness and Falseness
136 |
137 | In ruby, we can say that all objects are *truthy* except for two:
138 | * `false`
139 | * `nil`
140 |
141 | We can determine the truthiness of an object by using a "double-bang"
142 |
143 | ```ruby
144 | !!true # ==> true
145 | !!false # ==> false
146 | !!nil # ==> false
147 | !!"string" # ==> true
148 | !!5 # ==> true
149 | ```
150 |
151 | **Test these in pry!**
152 |
153 | The *truthiness* of an object will come into play when using logical operators as well:
154 |
155 | ```ruby
156 | true && nil # ==> nil
157 | true && "abc" # ==> 'abc'
158 | 5 && "abc" # ==> 'abc'
159 |
160 | true || nil # ==> true
161 | nil || false # ==> false
162 | "abc" || nil # ==> "abc"
163 | 5 || "abc" # ==> 5
164 | ```
165 |
166 | **Try these in pry!**
167 | Note that the `&&` operator returns the **last** truthy value if both values are truthy, and the **first** falsey value if either element is falsey. The `||` operator will return *the first truthy value* if either value is true, or the first falsey value if both values are false.
168 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/conditionals_exercises.md:
--------------------------------------------------------------------------------
1 | # Conditionals Exercises
2 |
3 | 0. Write a method `#swap_p`, that accepts a string an argument. If the string argument starts with a "p", then your method should replace it with an underscore, "\_". Otherwise, just return the original string. Assume the string only has lowercase letters.
4 |
5 | ```ruby
6 | swap_p("billabong") # ==> "billabong"
7 | swap_p("porcupine") # ==> "_orcupine"
8 | ```
9 |
10 | 0. Write a method, `#pos_or_neg?` that accepts an integer, `n`, as an argument. Your method should return `"positive"` if n > 0, `"negative"`, if n < 0, and `"your number is rather passive"` otherwise.
11 |
12 | ```ruby
13 | pos_or_neg?(2) # ==> "positive"
14 | pos_or_neg?(-23) # ==> "negative"
15 | pos_or_neg?(0) # ==> "your number is rather passive"
16 | ```
17 |
18 | 0. Read the [solutions](../solutions/part2/conditionals_solutions.md)
19 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/conditionals_readings.md:
--------------------------------------------------------------------------------
1 | # Conditionals
2 |
3 | ## Introduction
4 |
5 | Conditionals are how we tell computers "if this.. do that! otherwise.. do this!" There a are a couple key words to learn:
6 | * `if`
7 | * `else`
8 | * `elsif`
9 | * `unless`
10 |
11 | Let's learn how to use these tools to create powerful code! **This is where things get fun :)**
12 |
13 |
14 | ## `if`
15 |
16 | The syntax for using `if` blocks in ruby looks like this:
17 |
18 | ```ruby
19 | if _______ #this thing is true
20 | # do something!
21 | end
22 | ```
23 |
24 | For example, we could say:
25 |
26 | ```ruby
27 | if 1 == 1
28 | puts "yup! 1 equals 1"
29 | end
30 | ```
31 |
32 | `if` statements become even more powerful when we write methods that can do different things.
33 |
34 | ```ruby
35 | def exclaim_even_or_odd(n)
36 | if n.odd?
37 | return "this is odd!"
38 | end
39 | "this is even"
40 | end
41 | ```
42 |
43 | ## `else`
44 |
45 | The `else` statement is an *optional* add-on to the `if` statement. Let's say we wanted to change our method above to `puts` rather than `return`...
46 |
47 | ```ruby
48 | def exclaim_even_or_odd(n)
49 | if n.odd?
50 | puts "this is odd!"
51 | end
52 | puts "this is even"
53 | end
54 | ```
55 |
56 | This won't work as we intended, because "this is even" will always be printed! We could use an `if .. else` statement to accomplish this..
57 |
58 | ```ruby
59 | def exclaim_even_or_odd(n)
60 | if n.odd?
61 | puts "this is odd!"
62 | else
63 | puts "this is even"
64 | end
65 | end
66 | ```
67 |
68 | ## `elsif`
69 |
70 | In ruby, we have the ability to chain an infinite number of `if` statements together using `elsif`. For example..
71 |
72 | ```ruby
73 | def do_i_know_you?(name)
74 | if name == "Pierre"
75 | return "I know this guy!"
76 |
77 | elsif name == "Kofi"
78 | return "We are friends!"
79 |
80 | elsif name == "Fatima"
81 | return "We go wayyy back"
82 |
83 | else
84 | return "Sorry, I don't know you!"
85 | end
86 | end
87 | ```
88 |
89 | ## `unless`
90 |
91 | Finally, ruby gives us one move conditional to use: `unless`. `unless` is identical to `if !`..
92 |
93 | ```ruby
94 | unless x #===>>> if !x
95 | if x #===>>> unless !x
96 | ```
97 |
98 | We can make `if !` statements read more like English if we refactor them to use `unless`. For example:
99 |
100 | ```ruby
101 | if !(x == 5)
102 | # do something...
103 | end
104 |
105 | unless x == 5
106 | # do something
107 | end
108 | ```
109 |
110 | Both conditional statements above are perfectly valid! But read them both aloud and think about which seems more intuitive to work with.
111 |
112 | ## One-liners
113 |
114 | In ruby , we can write *short* conditionals on one line using `if` and `unless`.
115 |
116 | ```ruby
117 | if x == 10
118 | puts "yay 10!"
119 | end
120 | ```
121 |
122 | Can be refactored into...
123 |
124 | ```ruby
125 | puts "yay 10!" if x == 10
126 | ```
127 |
128 | We can make one-liner `unless` statements too:
129 |
130 | ```ruby
131 | puts "yay not 10!" unless x == 10
132 | ```
133 |
134 | This is **very** common practice in ruby. Generally, if you can make the conditional fit on one line, put it on one line. Note that you **cannot** use one-lines when writing `if .. else` statements.
135 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/loops_exercises.md:
--------------------------------------------------------------------------------
1 | # Loops Exercises
2 |
3 | 0. Write a `while` loop that prints out all the numbers from 1 to 25 inclusive.
4 |
5 | 0. Write an `until` loop that prints out all of the positive numbers that are less than 20 and are not divisible by 6.
6 |
7 | 0. Write a method, `#sum_up_to(number)` that, given a number greater than 1, sums all the numbers from 1 to that number, inclusive. It should use a `while` loop internally.
8 |
9 | 0. Write a method, `print_factors(number)` that prints all of the factors of a number, including 1 and the number itself. You'll need to use a while loop, a conditional statement, and the modulo operator!
10 |
11 | 0. Read the [solutions](../solutions/part2/loops_solutions.md)
12 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/loops_readings.md:
--------------------------------------------------------------------------------
1 | # Loops
2 |
3 | ## Introduction
4 |
5 | Complete the [CodeAcademy Loops][Loops] section if you haven't yet.
6 |
7 |
8 | A common saying in computer science suggests that humans are good at performing tasks that are hard to describe but easy to accomplish, such as: "make a sandwich".
9 |
10 | Computers, on the other hand, are good at performing tasks that are easy to describe, but hard to accomplish, such as: "write 'oo-la-la' 1000 times". That task might take you a while.. but introduce 5 lines of code:
11 |
12 | ```ruby
13 | i = 1
14 | while i <= 1000
15 | puts "oo-la-la"
16 | i += 1
17 | end
18 | ```
19 |
20 | And you're done! Copy and paste the above loop into pry. Remember to use the `clear` command to clear your terminal afterwards!
21 |
22 |
23 | [Loops]: https://www.codecademy.com/courses/ruby-beginner-en-XYcN1/0/1?curriculum_id=5059f8619189a5000201fbcb
24 | ---
25 |
26 | ## The `while` loop
27 |
28 | The `while` loop tells your computer to do something over and over again, but only *while* some condition is true. In the example above, we tell the computer to do something *while* `i <= 1000`. Meaning, as soon as `i` reaches 1001, we stop.
29 |
30 | This might beg the question, what if `i` never reaches 1001? Or to phase the question differently, what if our condition never becomes true?
31 |
32 | ```ruby
33 | while 1 == 1
34 | # ...
35 | end
36 | ```
37 |
38 | The above loop will run **forever**, until the end of time. This means we have to design our loops carefully!
39 |
40 | **All while loops should perform some operation that trends towards a falsey condition.** In our first example, we increment `i` by one with each iteration.
41 |
42 | ## The `until` loop
43 |
44 | The `until` loop is to the `while` loop, what `unless` is to `if`. In other words, `until` is the same as `while !(..)`. We can re-write the first example using an `until` loop like so:
45 |
46 | ```ruby
47 | i = 1
48 | until !(i <= 1000)
49 | puts "oo-la-la"
50 | i += 1
51 | end
52 | ```
53 |
54 | or...
55 |
56 | ```ruby
57 | i = 1
58 | until i > 1000
59 | puts "oo-la-la"
60 | i += 1
61 | end
62 | ```
63 |
64 | All three approaches are perfectly valid! You should use whichever makes more lexical sense -- whichever reads more like natural English.
65 |
66 | ## `break` and `next`
67 |
68 | `break` and `next` are two keywords that we can use inside of any ruby loop. So far, we only know `while` and `until`, but we'll learn more later and `break` and `next` will apply to them as well!
69 |
70 | `break` allows us to exit a loop early, similar to how `return` exits a method early.
71 | `next` will tell our loop to jump to the next iteration.
72 |
73 |
74 | We could re-write our first `while` loop like so:
75 |
76 | ```ruby
77 | i = 1
78 | while true
79 | break if i > 1000
80 | puts "oo-la-la"
81 | i += 1
82 | end
83 | ```
84 |
85 | The `next` command is good for skipping over irrelevant iterations. Let's say we have the following problem statement:
86 |
87 | *Define a method, sum_odds(n), that accepts an integer, n, as an argument. Sum all the odd integers between 0 and n.*
88 |
89 | ```ruby
90 | def sum_odds(n)
91 | i = 0
92 | sum = 0
93 |
94 | while i <= n
95 | sum += i if i.odd?
96 | i += 1
97 | end
98 |
99 | sum
100 | end
101 | ```
102 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/numbers_exercises.md:
--------------------------------------------------------------------------------
1 | # Number Exercises
2 |
3 | 0. Write a method, `#division_plus_remainder`, that accepts two integers, `big_int` and `small_int`, as arguments. Your method should find the number of times `small_int` completely divides into `big_int`, and then add the remainder that's left over. For example, `divisor_plus_remainder(7, 2)` should equal `4`.
4 |
5 | 0. Write a method `#divide_two_places`, that accepts two floats, `big_float` and `small_float`, as arguments. Your method should divide `big_float` / `small_float` and round to two decimal places.
6 |
7 | 0. Test your order of operations knowledge! You don't need to write any code here! Use `pry` to test your answer. Solve this expression:
8 |
9 | ```
10 | 5 + 3 * 3 ** 2 + ( 9 - 2 ** 2 ) ** 2
11 | ```
12 |
13 | 0. Read the [solutions](../solutions/part2/number_solutions.md)
14 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/numbers_readings.md:
--------------------------------------------------------------------------------
1 | # Numbers
2 |
3 | ## Introduction
4 |
5 | Ruby has several different types of objects for representing numbers. We're going to focus on two:
6 | * `Fixnums`
7 | * `Floats`
8 |
9 | `Fixnums` represent integers: 1, 3, 10, 90332... `Floats` represent numbers with decimal points: 1.3, 10.0, 9.00000001...
10 |
11 | ---
12 |
13 | ## Operations
14 |
15 | Ruby can perform all the standard mathamatical operations:
16 |
17 | ```ruby
18 | puts 40 + 2 # Addition: will print 42
19 | puts 49 - 7 # Subtraction: will print 42
20 | puts 2 * 3 # Multiplication: will print 6
21 | puts 6 / 2 # Division: will print 3
22 | puts 2 ** 3 # Exponentiation: will print 8
23 | ```
24 |
25 | ### Integer Division
26 |
27 | When we divide two integers in ruby, our answer also *has* to be an integer. For example:
28 |
29 | ```ruby
30 | puts 6 / 2 # will print 3
31 | puts 7 / 2 # will print 3, ignoring the remainder of 1
32 | puts 11 / 3 # will print 3, ignoring the remainder of 2
33 | ```
34 |
35 | **Try this in pry!**
36 |
37 | Ruby only calculates the number of whole times that a divisor can divide another number. Aka.. it rounds down.
38 |
39 | ### The Modulo Operator
40 |
41 | If we are interested in the remainder, we can use the modulo operator:
42 |
43 | ```ruby
44 | puts 6 % 2 # will print 0, 6 is divided evenly by 2
45 | puts 7 % 2 # will print 1, there is a remainder of 1 when 7 is divided by 2
46 | puts 11 % 3 # will print 2, there is a remainder of 2 when 11 is divided by 3
47 | ```
48 |
49 | Again, **try this yourself in pry!**
50 |
51 | To force a floating-point answer, we can make one of our operands a `Float`:
52 |
53 | ```ruby
54 | puts 7.0 / 2 # will print 3.5
55 | puts 7 / 2.0 # will print 3.5
56 | ```
57 |
58 | ### Shorthand
59 |
60 | Ruby has a nice shorthand for combining arithmetic with variable assignment:
61 |
62 | Long Expression | Shorthand
63 | ----------------|-----------
64 | `a = a + b` | `a += b`
65 | `a = a - c` | `a -= c`
66 | `a = a * d` | `a *= d`
67 | `a = a / e` | `a /= e`
68 |
69 | ```ruby
70 | a = 5
71 | a -= 2
72 | puts a # ==> 3
73 | ```
74 |
75 | ---
76 |
77 | ## Order of Operations
78 |
79 | So far, we have seen expressions with one operator. What happens if we chain more than one together? Let's try this in `pry`:
80 |
81 | ```ruby
82 | puts 4 - 2 * 5
83 | ```
84 |
85 | Just like in mathematics, ruby follows a pre-determined order when deciding which operations to carry out first. Remember this one from algebra? The mnemonic is:
86 |
87 | `PEMDAS` --> "Please Excuse My Dear Aunt Sally"
88 |
89 | Letter | Operation
90 | -------|-----------
91 | P | Parentheses
92 | E | Exponentiation
93 | M | Multiply
94 | D | Divide
95 | A | Addition
96 | S | Subtraction
97 |
98 | The table above specifies the order in which mathematical operations are carried out. Parentheses have the highest precedence. addition/subtraction, the lowest.
99 |
100 | ---
101 |
102 | ## Other Useful Methods
103 |
104 | Class | Method | Definition
105 | ---------|-------------|-----------
106 | `Float` | `#round(n)` | rounds a float to `n` decimal places
107 | `Fixnum` | `#odd?` | returns true / false if a number is odd
108 | `Fixnum` | `#even?` | returns true / false if a number is even
109 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/strings_exercises.md:
--------------------------------------------------------------------------------
1 | # String Exercises
2 |
3 | 0. Visit the official documentation for using [Ruby Strings](http://ruby-doc.org/core-2.2.0/String.html). Get *very* familiar with this page -- you'll be visiting it frequently! Use the examples and explanations on this page to help you do the next step.
4 |
5 | 0. Open `pry`. Create some strings and try **each** of the following methods:
6 | * `#length`
7 | * `#upcase`
8 | * `#downcase`
9 | * `#delete`
10 | * `#count`
11 | * `#gsub`
12 | * `#start_with?`
13 | * `#reverse`
14 |
15 | 0. Write a method `#capitalize` that accepts a string as an argument and capitalizes the first letter of the string. Note: Do not use the built in method `String#capitalize`
16 |
17 | ```ruby
18 | capitalize("yahoo!") == "Yahoo!"
19 | ```
20 |
21 | 0. Write a method, `#shout_then_whisper` that takes in two strings, and shouts the first part, then whispers the second part. The output should look like this:
22 |
23 | ```ruby
24 | shout_then_whisper("Hello", "McDouglas") == "HELLO!! ... mcdouglas"
25 | ```
26 |
27 | 0. Write a method, `#how_long?` that accepts a string as an argument. Your method should return a new string in the format:
28 |
29 | ```ruby
30 | how_long?("I am a sentence :)") == "Your string is 18 characters long"
31 | ```
32 |
33 | 0. Write a method, `#hyphenify` that accepts a string as an argument. Your method should remove all the spaces and replace them with hyphens.
34 |
35 | ```ruby
36 | hyphenify("I am a sentence!") == "I-am-a-sentence!"
37 | ```
38 |
39 | 0. Read the [solutions](../solutions/part2/string_solutions.md)
40 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/strings_readings.md:
--------------------------------------------------------------------------------
1 | # Strings
2 |
3 | ## Introduction
4 |
5 | We've already seen strings several times! Let's cover them in more detail.
6 |
7 | ## What is a string?
8 |
9 | A `String` is an object that represents a sequence of characters in ruby. Strings can contain anything: letters, numbers, special characters... Here are some examples of strings:
10 | * `"Hello, World!"`
11 | * `"123456"`
12 | * `"!@#$%^&"`
13 |
14 | ## How to make a string
15 |
16 | We can make a string by enclosing a sequence of characters in either single ('') or double ("") quotes. These operate identically, **with one important difference**, interpolation. We'll talk about this more later. It's convenient to have two options for making strings for the following reason:
17 |
18 | Let's say we want to make a string that looks like this:
19 |
20 | ```
21 | phrase = 'This isn't ice cream!'
22 | ```
23 |
24 | Or maybe..
25 |
26 | ```
27 | other_phrase = "Billy says "I love ruby" a lot."
28 | ```
29 |
30 | Neither of the above code snippets are valid because we're trying to use quotation marks inside the string and ruby is getting confused! Let's use one type of quotation to enclose a string that contains the other type:
31 |
32 | ```ruby
33 | phrase = "This isn't ice cream!"
34 | other_phrase = 'Billy says "I love ruby" a lot.'
35 | ```
36 |
37 | Much better :)
38 |
39 | ---
40 |
41 | ## Concatenation & interpolation
42 |
43 | Let's say I have two strings and I want to combine them into one:
44 |
45 | ```ruby
46 | greeting = "Why, hello there "
47 | name = "Grace"
48 | ```
49 |
50 | I could do so using string `concatenation`:
51 |
52 | ```ruby
53 | greeting = "Why, hello there "
54 | name = "Grace"
55 |
56 | puts greeting + name
57 | ```
58 |
59 | **Test this in pry!**
60 |
61 | This works well when the two strings are combined one after another. But what if I want to insert one string somewhere in the middle of another? This is where we'll want to use `interpolation`. An important note:
62 |
63 | **interpolation only works with double quoted strings!**
64 |
65 | ```ruby
66 | name = "Yi"
67 |
68 | puts "Why, hello there #{name}! Have a nice day!"
69 | ```
70 |
71 | **Test this in pry!**
72 |
73 | Note that you must use the following symbols: `#{..}` whatever ruby code is contained inside the curly braces is `interpolated` into the string. Here's another example:
74 |
75 | ```ruby
76 | puts "5 + 7 is #{5 + 7}"
77 | ```
78 |
79 | ---
80 |
81 | ## Useful Methods
82 |
83 | ### Indexing & Slicing
84 |
85 | Watch the video!
86 |
87 | [](https://vimeo.com/181974255)
88 |
89 | Argument | `#slice` example | `#[]` example | Result
90 | -----------|------------------------|------------------|---------
91 | Integer | `"string".slice(1)` | `"string"[1]` | `"t"`
92 | \- Integer | `"string".slice(-2)` | `"string"[-2]` | `"n"`
93 | 2 Integers | `"string".slice(2, 3)` | `"string"[2, 3]` | `"rin"`
94 | Range | `"string".slice(2..3)` | `"string"[2..3]` | `"ri"`
95 |
96 | **Try this in pry!**
97 |
98 | ### Other Useful Methods
99 |
100 | Method | Definition
101 | ---------------|-----------
102 | `#length` | Returns the number of characters in a string (including spaces!)
103 | `#upcase` | Converts all letters to uppercase
104 | `#downcase` | Converts all letters to lowercase
105 | `#delete` | Returns a copy of the string with the characters deleted
106 | `#count` | Counts the number of times a particular substring occurs
107 | `#gsub` | Replaces all occurrences of one substring with another
108 | `#start_with?`| Returns true/false if the string starts with a particular sub-string
109 | `#reverse` | Reverses a string
110 |
111 | #### [String Documentation](http://ruby-doc.org/core-2.2.0/String.html)
112 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/type_conversion_exercises.md:
--------------------------------------------------------------------------------
1 | # Type Conversion Exercises
2 |
3 | 0. Define a method, `#two_digit_sum`, that accepts a two-digit integer as an argument. Your method should return the sum of the two digits.
4 |
5 | ```ruby
6 | two_digit_sum(34) == 7 # ==> 3 + 4 == 7
7 | ```
8 |
9 | 0. Define a method, `#orders_of_magnitude`, that accepts an integer as an argument. Your method should return the number of digits in the integer.
10 |
11 | ```ruby
12 | orders_of_magnitude(99) == 2
13 | orders_of_magnitude(1034) == 4
14 | ```
15 |
16 | 0. Define a method, `#accurate_division`, that accepts two **integers** as arguments. Your method should return the quotient of the two integers (rounding to 3 decimal places).
17 |
18 | ```ruby
19 | accurate_division(3, 4) == 0.75
20 | accurate_division(1, 3) == 0.333
21 | accurate_division(7, 6) == 1.167
22 | ```
23 |
24 | 0. Define a method, `#two_digit_format`, that accepts an integer, `n`, and returns a string version of that integer. If the integer has a single digit, you should append a 0. You can assume that n will always be two or fewer digits. Hint: try creating a string using `'0' + n.to_s`, then use `#slice` to get the last two digits!
25 |
26 | ```ruby
27 | two_digit_format(8) == "08"
28 | two_digit_format(12) == "12"
29 | ```
30 |
31 | 0. Define a method, `#time_string` that accepts 3 arguments: hours, minutes, and seconds (all integers). Convert these integers into one contiguous time string that has the following format:
32 |
33 | ```ruby
34 | "hh:mm:ss"
35 | ```
36 |
37 | Use the method, `#two_digit_format`, to help you do this!
38 |
39 | ```ruby
40 | time_string(12, 1, 3) == "12:01:03"
41 | time_string(1, 10, 6) == "01:10:06"
42 | ```
43 |
44 | 0. Read the [solutions](../solutions/part2/type_conversion_solutions.md)
45 |
--------------------------------------------------------------------------------
/jumpstart-files/part2/type_conversion_readings.md:
--------------------------------------------------------------------------------
1 | # Type Conversion
2 |
3 | ## Introduction
4 |
5 | We can represent the number 2 in ruby in a variety of different formats:
6 | * `2`
7 | * `2.0`
8 | * `"2"`
9 |
10 | Though it may seem strange to represent 2 as a `String`, we often find use cases for this, such as:
11 |
12 | Define a method, `#time_string` that accepts 3 arguments: hours, minutes, and seconds (all integers). Convert these integers into one contiguous time string that has the following format:
13 |
14 | ```ruby
15 | "hh:mm:ss"
16 | ```
17 |
18 | Well.. if we use simple interpolation:
19 |
20 | ```ruby
21 | "#{hours}:#{minutes}:#{seconds}"
22 | ```
23 |
24 | We might end up with something like this:
25 |
26 | ```ruby
27 | "12:1:3"
28 | ```
29 |
30 | And this doesn't have the appropriate format! We need something like:
31 |
32 | ```ruby
33 | "12:01:03"
34 | ```
35 |
36 | To aid us in this challenge, we can use type conversion. Don't worry about solving the above problem now, we'll do it in the exercises!
37 |
38 | ---
39 |
40 | ## `to_?`
41 |
42 | All three objects: `Fixnums`, `Floats`, and `Strings` have methods that allow us to convert between the types.
43 |
44 | For example, if I want to convert from a `String` to a `Fixnum`, I could use the `#to_i` method.
45 |
46 | ```ruby
47 | int = "5".to_i
48 | puts int # ==> 5
49 | ```
50 |
51 | We can actually convert to/from any and all classes.
52 |
53 | ```ruby
54 | 5.to_s # ==> "5"
55 | 5.to_f # ==> 5.0
56 |
57 | 5.3.to_i # ==> 5
58 | 5.3.to_s # ==> "5.3"
59 |
60 | "5.6".to_i # ==> 5
61 | "5.6".to_f # ==> 5.6
62 | ```
63 |
64 | **Test these in pry!**
65 |
--------------------------------------------------------------------------------
/jumpstart-files/part3/README.md:
--------------------------------------------------------------------------------
1 | # Ruby Objects
2 |
3 | In this section we'll talk about the different types of collections in ruby:
4 | * Arrays
5 | * Ranges
6 | * Hashes
7 |
8 | We'll also talk about some more looping constructs:
9 | * Enumerables
10 | * Iterators
11 |
12 | ## Instructions
13 |
14 | **Do every exercise.** Feel free to skim the readings and then use them as references during the exercises to fill in the gaps in your knowledge.
15 |
16 | You will not learn by simply reading about code. **You must write it too.** Do the exercises :)
17 |
18 | **Call over a TA** when you hit a "mandatory check point".
19 |
20 | ## Sections
21 |
22 | * Arrays [Readings](./arrays_readings.md)
23 | * Arrays [Exercises](./arrays_exercises.md)
24 | * Ranges [Readings](./ranges_readings.md)
25 | * Ranges [Exercises](./ranges_exercises.md)
26 | * Hashes [Readings](./hashes_readings.md)
27 | * Hashes [Exercises](./hashes_exercises.md)
28 | * Enumerables [Readings](./enumerables_readings.md)
29 | * Enumerables [Exercises](./enumerables_exercises.md)
30 | * Iterators [Readings](./iterators_readings.md)
31 | * Iterators [Exercises](./iterators_exercises.md)
32 |
--------------------------------------------------------------------------------
/jumpstart-files/part3/arrays_exercises.md:
--------------------------------------------------------------------------------
1 | # Array Exercises
2 |
3 | 0. Write a method, `#first_n_evens(n)` that returns an array of the first n even numbers, starting from 0.
4 |
5 | ```ruby
6 | first_n_evens(1) # ==> [0]
7 | first_n_evens(3) # ==> [0, 2, 4]
8 | ```
9 |
10 | 0. Write a method, `#reverse(array)` that reverses an array in-place; don't create any new arrays, and don't use `array.reverse`!
11 |
12 | ```ruby
13 | reverse([1]) # ==> [1]
14 | reverse([1, 2]) # ==> [2, 1]
15 | reverse([4, 5, 6, 7]) # ==> [7, 6, 5, 4]
16 | ```
17 |
18 | 0. Write a method, `#rotate(array, shift)` that, given an array and an integer (shift), rotates the array in-place by the shift amount.
19 | * Hint: some combination of `#shift`/`#push` or `#unshift`/`#pop` should come in handy here.
20 |
21 | ```ruby
22 | rotate([1, 2, 3, 4], 1) # ==> [4, 1, 2, 3]
23 | rotate([1, 2], 2) # ==> [1, 2]
24 | rotate([4, 5, 6, 7], 3) # ==> [5, 6, 7, 4]
25 | ```
26 |
27 | 0. Write a method, `#all_uniqs(array1, array2)` that, given two arrays, produces a new array of only elements unique to `array1` and elements unique to `array2`. Don't worry about the order of the elements in the output array.
28 |
29 | ```ruby
30 | all_uniqs([1, 2, 3], [1, 2, 4]) # ==> [3, 4]
31 | all_uniqs([1, 2, 7], [1, 3, 8]) # ==> [2, 7, 3, 8]
32 | ```
33 |
34 | 0. Read the [solutions](../solutions/part3/array_solutions.md)
35 |
--------------------------------------------------------------------------------
/jumpstart-files/part3/enumerables_exercises.md:
--------------------------------------------------------------------------------
1 | # Enumerable Exercises
2 |
3 | 0. Write a method, `#words_with_a`, that accepts an array of strings as an argument. It should return the number of strings that have an 'a' in them.
4 |
5 | ```ruby
6 | words_with_a(["enumerables", "are", "cake!"]) == 3
7 | words_with_a(["this", "is", "a", "string"]) == 1
8 | words_with_a(["ruby"]) == 0
9 | ```
10 |
11 | 0. Write a method, `#odds`, that accepts an array of integers as an argument. It should return an array containing only the odd numbers.
12 |
13 | ```ruby
14 | odds([1,2,3,4,5,6]) == [1,3,5]
15 | odds([2,4,6]) == []
16 | ```
17 |
18 | 0. Write a method, `#triple`, that accepts an array of integers as an argument. It should return a new array with every element tripled.
19 |
20 | ```ruby
21 | triple([1,2,3,4,5,6]) == [3,6,9,12,15,18]
22 | ```
23 |
24 | 0. Write a method, `#odd_value_and_position`, that accepts an array of integers as an argument. It should return a new array of all the elements whose value and position (index) in the original array are odd.
25 |
26 | ```ruby
27 | odd_value_and_position([0,1,2,3,4,5]) == [1,3,5]
28 | odd_value_and_position([1,2,3,4,5]) == []
29 | ```
30 |
31 | 0. Read the [solutions](../solutions/part3/enumerable_solutions.md)
32 |
--------------------------------------------------------------------------------
/jumpstart-files/part3/enumerables_readings.md:
--------------------------------------------------------------------------------
1 | # Enumerables
2 |
3 | ## Introduction
4 |
5 | The `Enumerable` module contains a set of methods that are 'mixed in' to various other classes. You don't have to understand how this works, but you need to know where and how to use `Enumerable` methods.
6 |
7 | The `Enumerable` methods are *extremely* useful methods that can be used with ruby collections. All of the following can use `Enumerable` methods:
8 | * Arrays
9 | * Ranges
10 | * Hashes
11 |
12 | Generally, if the object has an `#each` method, then it probably has access to the `Enumerable` module.
13 |
14 | For example, let's say you have an array of integers and you want to double each value. There's a handy `Enumerable` method we can use, called `#map`:
15 |
16 | ```ruby
17 | [1, 2, 3].map { |el| el * 2 } # ==> [2, 4, 6]
18 | ```
19 |
20 | You should take a minute to review the [documentation][documentation] for the `Enumerable` module. Glance over the list of methods in the grey box on the left-hand side of the page. Try to guess what these methods might do!
21 |
22 | ### Code Blocks
23 |
24 | Code blocks used with `Array` and `Range` `Enumerable` methods use a single argument, but code blocks used with `Hash` `Enumerable` methods use two arguments: one for keys and one for values.
25 |
26 | ```ruby
27 | [1, 2, 3].select { |el| ... } # ==> single argument in code block
28 | (0...100).select { |el| ... } # ==> single argument in code block
29 | {"a" => "b"}.select { |key, val| ... } # ==> two arguments in code block
30 | ```
31 |
32 | ---
33 |
34 | ## Useful Methods
35 |
36 | #### `#map`
37 | Returns an array with each element in the original collection is "mapped" by the given code block
38 |
39 | ```ruby
40 | squares = (1..5).map { |el| el ** 2 } # ==> [1, 4, 9, 16, 25]
41 | ```
42 |
43 | #### `#select`
44 | Returns a new collection but only with the elements whose code block evaluated to `true`
45 |
46 | ```ruby
47 | dictionary = {
48 | "apple" => "fruit",
49 | "ant" => "a bug",
50 | "banana" => "yellow fruit",
51 | "cat" => "fluffy animal"
52 | }
53 |
54 | a_words = dictionary.select do |key, val|
55 | key.start_with?("a")
56 | end
57 |
58 | p a_words # ==> { "apple" => "fruit", "ant" => "a bug"}
59 | ```
60 |
61 | #### `#reject`
62 | Returns a new collection but omits the the elements whose code block evaluated to `true`
63 |
64 | ```ruby
65 | no_tens = (1..100).reject do |el|
66 | el % 10 == 0
67 | end
68 | ```
69 |
70 | #### `#any?`
71 | Returns `true` / `false` based on whether *any* of the elements in the collection force the code block to evaluate `true`
72 |
73 | ```ruby
74 | words = ["pan", "pot", "spatula", "knife", "fork"]
75 | words.any? { |kitchen_item| kitchen_item == "knife" } # ==> true
76 |
77 | # At least one of the words is "knife"
78 | ```
79 |
80 | #### `#all?`
81 | Returns `true` / `false` based on whether *all* of the elements in the collection force the code block to evaluate `true`
82 |
83 | ```ruby
84 | objects = [2, 4, 9, "5", 6]
85 | objects.all? { |obj| obj.is_a?(Integer) }
86 | # ==> false
87 |
88 | # Not all of the objects are Integers
89 | ```
90 |
91 | #### `#none?`
92 | Returns `true` / `false` based on whether *none* of the elements in the collection force the code block to evaluate `true`
93 |
94 | ```ruby
95 | numbers = [12, 14, 16, 23]
96 | objects.none? { |n| n < 10 } # ==> true
97 |
98 | # None of the numbers are less than 10
99 | ```
100 |
101 | #### `#count`
102 | Counts the number of elements that evaluate `true` for a particular code block
103 |
104 | ```ruby
105 | [1, 4, 5, 4, 3, 5, 6, 7].count { |n| n.even? } # ==> 3
106 |
107 | # There are 3 even numbers in the array
108 | ```
109 |
110 |
111 | #### `#with_index`
112 | Some enumerable methods are chainable with the `#with_index` method. Let's say we wanted to map an array to a new value where `n` \~\~> `n * index(n)`
113 |
114 | We could accomplish this using the `#with_index` method.
115 |
116 | ```ruby
117 | result = [1, 5, 6, 8].map.with_index do |el, idx|
118 | el * idx
119 | end
120 |
121 | result # ==> [0, 5, 12, 24]
122 | ```
123 |
124 |
125 | **Try these in pry!**
126 |
127 | [documentation]: http://ruby-doc.org/core-2.3.1/Enumerable.html
128 |
--------------------------------------------------------------------------------
/jumpstart-files/part3/hashes_exercises.md:
--------------------------------------------------------------------------------
1 | # Hash Exercises
2 |
3 | 0. Write a method, `#word_lengths(string)` that, given a sentence, returns a hash of each of the words and their lengths.
4 |
5 | ```ruby
6 | word_lengths("hello my good good pal") =
7 | { 'hello' => 5, 'my' => 2, 'good' => 4, 'pal' => 3 }
8 | ```
9 |
10 | 0. Write a method, `#uniq(array)` that returns an array with only the unique values in the array. (Hint: use a hash!) Don't use the `Array#uniq` method here.
11 |
12 | ```ruby
13 | uniq([1, 1, 2, 3]) # ==> [2, 3]
14 | uniq([1, 3, 4, 3, 2, 5, 4]) # ==> [1, 2, 5]
15 | ```
16 |
17 | 0. Write a method, `#greatest_key_by_val(hash)` that takes in a hash with any keys but only integers for values, and returns the key with the largest value.
18 |
19 | ```ruby
20 | greatest_key_by_val({ "a" => 5, "b" => 6, "c" => 3 }) # ==> "b"
21 | ```
22 |
23 | 0. Read the [solutions](../solutions/part3/hash_solutions.md)
24 |
--------------------------------------------------------------------------------
/jumpstart-files/part3/hashes_readings.md:
--------------------------------------------------------------------------------
1 | # Hashes
2 |
3 | ## Introduction
4 |
5 | Complete the [CodeAcademy Hashes][hashes] section if you haven't yet.
6 |
7 | Hashes are used for storing key-value pairs. Like arrays, they are an extremely common and useful data structure. Get familiar with them!
8 |
9 | A common example of a `Hash` object is a dictionary, where the words are the `keys` in the `Hash`, and the `values` are the definitions.
10 |
11 | ```ruby
12 | dictionary = {
13 | "apple" => "a delicious fruit",
14 | "cat" => "a cute animal",
15 | "dog" => "a best friend",
16 | "human" => "not as cute as cats.."
17 | }
18 |
19 | dictionary["dog"] # ==> "a best friend"
20 | ```
21 |
22 | [hashes]: https://www.codecademy.com/en/courses/ruby-beginner-en-F3loB/1/1?curriculum_id=5059f8619189a5000201fbcb
23 |
24 | ---
25 | ## Basic Structure
26 |
27 | Hashes are defined using curly braces:
28 |
29 | ```ruby
30 | hash = {}
31 | ```
32 |
33 | Inside of the curly braces, we can add key-value pairs. The syntax must follow:
34 |
35 | ```
36 | key => value
37 | ```
38 |
39 | The symbol uses in the above expression, `=>`, is called a *hash rocket*. It is used to separate key-value pairs. Multiple sets of key-value pairs must be separated by a comma.
40 |
41 | ```ruby
42 | hash = {
43 | "key1" => "value1",
44 | "key2" => "value2",
45 | }
46 | ```
47 |
48 | So far, we've only used strings as our keys and values, but we can actually use *any ruby object* as either a key or value -- even other hashes! The following is a perfectly valid hash:
49 |
50 | ```ruby
51 | valid_hash = {
52 | 5 => "five",
53 | [] => "array",
54 | "hash" => {"a" => "b"}
55 | }
56 | ```
57 |
58 | ---
59 | ## Bracket Methods
60 |
61 | Just like with strings and arrays, ruby hashes have bracket methods. These methods will be the primary mechanism for setting and retrieving values from hashes. For example:
62 |
63 | ```ruby
64 | hash = {}
65 | hash[5] = "five"
66 |
67 | p hash # ==> {5 => "five"}
68 | p hash[5] # ==> "five"
69 | ```
70 |
71 | **Test this in pry!**
72 |
73 | Asking for a key that doesn't exist in the hash will give you nil.
74 |
75 | ```ruby
76 | hash = {1: "one"}
77 | p hash[5] # ==> nil
78 | ```
79 |
80 | ### Uniqueness
81 |
82 | Note that `keys` in hashes **must be uniq**. If you try to add a new key-value pair where the key already exists, then the old key-value pair will be overwritten.
83 |
84 | ```ruby
85 | hash = {3 => "three"}
86 | hash[3] = "velociraptor"
87 |
88 | p hash[3] # ==> "velociraptor"
89 | ```
90 |
91 | ---
92 | ## `#each`
93 |
94 | Hashes in ruby have an `#each` method, just like arrays! This allows us to iterate over the entire collection and do something for each key-value pair.
95 |
96 | ```ruby
97 | dictionary = {
98 | "apple" => "a delicious fruit",
99 | "cat" => "a cute animal",
100 | "dog" => "a best friend",
101 | "human" => "not as cute as cats.."
102 | }
103 |
104 | dictionary.each do |key, value|
105 | puts "The definition of #{key} is: '#{value}'"
106 | end
107 |
108 | ```
109 |
110 | **Note:** the code block passed to the each method is designed to accept *two arguments*, one for the `key` and one for the `value`.
111 |
112 | **Test this in pry!**
113 |
114 | ---
115 | ## Other Useful Methods
116 |
117 | Method | Definition
118 | --------------|-----------
119 | `#keys` | Returns an array of all the keys
120 | `#values` | Returns an array of all the values
121 | `#length` | Returns the number of key-value pairs
122 | `#include?` | Returns `true` / `false` if the hash has a `key` matching the argument
123 | `#delete` | Deletes the specified key (and value) from the hash
124 |
125 | **Test these in pry!**
126 |
--------------------------------------------------------------------------------
/jumpstart-files/part3/iterators_exercises.md:
--------------------------------------------------------------------------------
1 | # Iterator Exercises
2 |
3 | 0. Define a method, `#print_10`, that prints the numbers 1 - 10. Do this two different ways:
4 | * Using `Range#each`
5 | * Using `#upto`
6 |
7 | 0. Define a method, `#string_squared(string)`, that prints the string the number of times that it has characters.
8 |
9 | ```ruby
10 | string_squared('abc') # ==> prints "abc" 3 times
11 | string_squared('hello') # ==> prints "hello" 5 times
12 | ```
13 |
14 | 0. Define a method, `#collect_thirds(array)`, that iterates through an array and collects every third element. Do this in two different ways:
15 | * `array.select.with_index`
16 | * `#step`
17 |
18 | ```ruby
19 | collect_thirds([3, 4]) # ==> []
20 | collect_thirds([3, 4, 5, 7, 6, 4, 2]) # ==> [5, 4]
21 | collect_thirds([3, 4, 5, 7, 6, 4, 2, 1, 1]) # ==> [5, 4, 1]
22 | ```
23 |
24 | 0. Read the [solutions](../solutions/part3/iterator_solutions.md)
25 |
--------------------------------------------------------------------------------
/jumpstart-files/part3/iterators_readings.md:
--------------------------------------------------------------------------------
1 | # Iterators
2 |
3 | ## Introduction
4 |
5 | Iterators are loops of a fixed length. Using while loops, we do something while some condition is met. With iterators, we'll just do something N times. These methods can be useful when we know beforehand the number of necessary iterations.
6 |
7 | ---
8 | ## `#times`
9 |
10 | Let's tell the computer to do something 7 times:
11 |
12 | ```ruby
13 | 7.times do
14 | puts "Yee-haw!"
15 | end
16 | ```
17 |
18 | We can also use code blocks to determine which iteration we're on.
19 |
20 | ```ruby
21 | 7.times do |n|
22 | puts n
23 | end
24 | ```
25 |
26 | **Test this in pry!**
27 |
28 | ---
29 | ### `#upto` and `#downto`
30 |
31 | Let's count from 10 up to 20.
32 |
33 | ```ruby
34 | 10.upto(20) do |n|
35 | puts n
36 | end
37 | ```
38 |
39 | Now, let's count down from 10 to 1.
40 |
41 | ```ruby
42 | 10.downto(1) do |n|
43 | puts n
44 | end
45 | ```
46 |
47 | **Test these in pry!**
48 |
49 | ---
50 | ### `#step`
51 |
52 | Lastly, let's count from 0 to 100, but in increments of 5.
53 |
54 | ```ruby
55 | 0.step(100, 5) do |n|
56 | puts n
57 | end
58 | ```
59 |
60 | **Test this in pry!**
61 |
--------------------------------------------------------------------------------
/jumpstart-files/part3/ranges_exercises.md:
--------------------------------------------------------------------------------
1 | # Range Exercises
2 |
3 | 0. Define a method, `#alphabet_slice(range)`, that accepts a range of integers as an argument. Use the range to slice a string of characters representing the alphabet. Remember, the `String#slice` method can accept a range as an argument!
4 |
5 | ```ruby
6 | alphabet_slice((0..3)) # ==> "abcd"
7 | alphabet_slice((0...3)) # ==> "abc"
8 | alphabet_slice((0..25)) # ==> "abcdefghijklmnopqrstuvwxyz"
9 | ```
10 |
11 | 0. Define a method, `#alphabet_without(array)`, that accepts an array of letters as an argument. Your method should return an array of all the characters in the alphabet, but exclude the characters in the input array.
12 |
13 | ```ruby
14 | alphabet_without(["a"]) # ==> b - z in array
15 | alphabet_without(["a", "z"]) # ==> b - y in array
16 | ```
17 |
18 | 0. Define a method, `#evens_below(integer)`, that accepts an integer as an argument. Your method should return an array of all the even numbers (starting at 0) that are less than (but not equal to) the integer argument.
19 |
20 | ```ruby
21 | evens_below(6) # ==> [0, 2, 4]
22 | evens_below(9) # ==> [0, 2, 4, 6, 8]
23 | ```
24 |
25 | 0. Define a method, `#to_range(array)` that, given an array of integers, constructs a range covering the span of integers in the array argument.
26 |
27 | ```ruby
28 | to_range([1, 8, 11, 5, 6]) # ==> (1..11)
29 | to_range([6, 15, 2, 12]) # ==> (2..15)
30 | ```
31 |
32 | 0. Read the [solutions](../solutions/part3/range_solutions.md)
33 |
--------------------------------------------------------------------------------
/jumpstart-files/part3/ranges_readings.md:
--------------------------------------------------------------------------------
1 | # Ranges
2 |
3 | ## Introduction
4 |
5 | Ranges can be used to represent a set of integers or characters. For example, lets say you wanted to create an alphabet. You'll need a collection of all the characters from "a" -- "z". Well, you could use an array..
6 |
7 | ```ruby
8 | alphabet = ["a", "b", "c", ... "z"]
9 | ```
10 |
11 | but this process is quite annoying. We don't want to have to type out 26 different characters. Behold, the alphabet range:
12 |
13 | ```ruby
14 | alphabet = ("a".."z")
15 | ```
16 |
17 | And that's it! Ruby knows how to fill in the middle characters for us. All we have to do is specify the upper and lower bounds of the range. Want to know the 16th letter in the alphabet?
18 |
19 | ```ruby
20 | alphabet = ("a".."z").to_a
21 | alphabet[15] # ==> "p"
22 | ```
23 |
24 | ---
25 |
26 | ## Creating Ranges
27 |
28 | Ranges are made by wrapping a lower and upper bound in parentheses and including 2 or 3 dots in between. The number of dots specify whether the range is *inclusive* or *exclusive*.
29 |
30 | ### Inclusive Ranges
31 |
32 | Inclusive ranges are defined using two dots: `(? .. ?)`. Below are some examples of *inclusive* ranges and the values they represent:
33 |
34 | ```ruby
35 | abc = ("a".."c") # ==> a - c
36 | digits = (0..9) # ==> 0 - 9
37 | ```
38 |
39 | Inclusive ranges *include* the upper bound. In the above examples, both "c" and 9 are included in their respective ranges.
40 |
41 | ### Exclusive Ranges
42 |
43 | Exclusive ranges are defined using 3 dots: `(? ... ?)`.
44 |
45 | ```ruby
46 | letters = ("d"..."q") # ==> d - p
47 | digits = (1...5) # ==> 1 - 4
48 | ```
49 |
50 | Exclusive ranges *exclude* the upper bound.
51 |
52 | ## `#to_a`
53 |
54 | We cannot directly index a range:
55 |
56 | ```ruby
57 | alphabet = ("a".."z")
58 | alphabet[15] # throws error!
59 | ```
60 |
61 | Instead, we can convert a range into an array using the `to_a` method. Doing so allows us to index the collection and ask for specific values.
62 |
63 | ```ruby
64 | alphabet = ("a".."z").to_a
65 | alphabet[10] # ==> "k"
66 | ```
67 |
68 | ## `#each`
69 |
70 | Just like arrays, ranges have an `#each` method. This method allows us to execute a code block -- perform some action -- for each element in the range. If we wanted to print every letter of the alphabet...
71 |
72 | ```ruby
73 | ("a".."z").each do |letter|
74 | puts letter
75 | end
76 | ```
77 |
78 | Note the we **don't have to convert the range** to an array in order to use the `#each` method.
79 |
--------------------------------------------------------------------------------
/jumpstart-files/part4/README.md:
--------------------------------------------------------------------------------
1 | # Rubyisms & Debugging
2 |
3 | In this section we'll talk about Rubyisms and what it means to write "good code". We'll also go over using the `byebug` gem and give you some debugging tips.
4 |
5 | ## Instructions
6 |
7 | **Do every exercise.** Feel free to skim the readings and then use them as references during the exercises to fill in the gaps in your knowledge.
8 |
9 | You will not learn by simply reading about code. **You must write it too.** Do the exercises :)
10 |
11 | **Call over a TA** when you hit a "mandatory check point".
12 |
13 | ## Sections
14 |
15 | * Code Style [Readings](./code_style_readings.md)
16 | * Code Style [Exercises](./code_style_exercises.md)
17 | * Debugging [Readings](./debugging_readings.md)
18 | * Debugging [Exercises](./debugging_exercises.md)
19 |
--------------------------------------------------------------------------------
/jumpstart-files/part4/code_style_exercises.md:
--------------------------------------------------------------------------------
1 | # Code Style Exercises
2 |
3 | 0. The methods below contain valid Ruby code. However, the styling could use some work. See how many style problems you can fix (including variable/method naming).
4 |
5 | ```ruby
6 | def hello_world
7 | puts "Hello, world!" end
8 | ```
9 |
10 | ```ruby
11 | def is_prime num
12 | n = 2
13 | while n < num
14 | if (num%n==0)
15 | return (false)
16 | end
17 | n = n + 1
18 | end
19 | return true
20 | end
21 | ```
22 |
--------------------------------------------------------------------------------
/jumpstart-files/part4/code_style_readings.md:
--------------------------------------------------------------------------------
1 | # Code Styles
2 |
3 | ## Introduction
4 |
5 | In this section we'll try to answer the question of "how should I organize my code?" and "which way of doing things is better?".
6 |
7 | For starters, read this article: [Principles of Good Programming][article]
8 |
9 | ## Rubyisms
10 |
11 | Watch the video!
12 |
13 | [](https://vimeo.com/181705461)
14 |
15 | [article]: https://www.artima.com/weblogs/viewpost.jsp?thread=331531
16 |
--------------------------------------------------------------------------------
/jumpstart-files/part4/debugging_exercises.md:
--------------------------------------------------------------------------------
1 | # Debugging Exercises
2 |
3 | Imagine you wrote the following code:
4 |
5 | ```ruby
6 | def is_palindrome?(word)
7 | index = 0
8 |
9 | while index < word.length / 2
10 | start_letter = word[index]
11 | end_letter = word[word.length - 1 - index]
12 |
13 | if start_letter != end_letter
14 | return false
15 | end
16 |
17 | index += 1
18 | end
19 |
20 | true
21 | end
22 |
23 | sample_word = "detartrated"
24 |
25 | ```
26 |
27 | Watch [this video][write-out-video] at 2x and compare it to the code. We recommend putting this window on the left half of the screen and the youtube video on the right half. Follow along on a piece of paper. (The "t" in the `end_letter` row should have been crossed out in the video.)
28 |
29 |
30 | [write-out-video]: https://youtu.be/MpkXN5V-DqI
31 |
32 | ## Exercises
33 |
34 | ### Back and forth
35 |
36 | - Given the following code, write out what all the variables are, like in the video above, as you "run" the code in your head. Write out what the code returns. Do not run the code on your computer.
37 |
38 | ```ruby
39 | def includes_a_word_forwards_and_backwards?(sentence)
40 | words = sentence.split
41 |
42 | first_word_index = 0
43 |
44 | while first_word_index < words.length
45 | second_word_index = first_word_index + 1
46 |
47 | while second_word_index < words.length
48 | first_word = words[first_word_index]
49 | second_word = words[second_word_index]
50 |
51 | return true if first_word == second_word.reverse
52 |
53 | second_word_index += 1
54 | end
55 |
56 | first_word_index += 1
57 | end
58 |
59 | false
60 | end
61 |
62 | sentence = "i trot to eat a tort"
63 | p includes_a_word_forwards_and_backwards?(sentence)
64 |
65 |
66 | ```
67 |
68 | Then compare what you wrote to [the answer.](./write-out-answer-1.md). You can now run the code on your computer.
69 |
70 | ### Buggin' out
71 |
72 | There is a bug in the following code. Figure out where it is by writing out all the variables as you "run" the code in your head. Do not run the code on your computer.
73 |
74 | ```ruby
75 |
76 | def pair_greater_than_n(numbers, n)
77 | first_index = 0
78 |
79 | while first_index < numbers.length
80 | second_index = first_index + 1
81 | first_number = numbers[first_index]
82 |
83 | while second_index <= numbers.length
84 | second_number = numbers[second_index]
85 |
86 | return true if first_number + second_number > n
87 |
88 | second_index += 1
89 | end
90 |
91 | first_index += 1
92 | end
93 |
94 | false
95 | end
96 |
97 |
98 | sample_numbers = [1, 4, 5]
99 | p pair_greater_than_n(sample_numbers, 6)
100 |
101 | ```
102 |
103 |
104 | Then check [the answer](./write-out-answer-2.md). You can now run the code on your computer.
105 |
--------------------------------------------------------------------------------
/jumpstart-files/part4/debugging_readings.md:
--------------------------------------------------------------------------------
1 | # Debugging
2 |
3 | ## Introduction
4 |
5 | **"Be" the debugger**. You should be able to look at the code you wrote and walk through that code from beginning to end, knowing exactly what every variable is at every point in time. (Following Rubyisms and the single responsibility principle will make this easier.)
6 |
7 | ## Debugging with `byebug`
8 |
9 | Watch the video!
10 |
11 | [](https://vimeo.com/181236028)
12 |
13 | ### Useful `byebug` Commands
14 |
15 | * `c` / `continue`
16 | * `n` / `next`
17 | * `s` / `step`
18 | * `up`
19 | * `display`
20 | * `exit`
21 |
--------------------------------------------------------------------------------
/jumpstart-files/part4/write-out-answer-1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/part4/write-out-answer-1.jpg
--------------------------------------------------------------------------------
/jumpstart-files/part4/write-out-answer-1.md:
--------------------------------------------------------------------------------
1 | The code should return `true`.
2 |
3 | Your paper should look something like this:
4 |
5 | 
6 |
--------------------------------------------------------------------------------
/jumpstart-files/part4/write-out-answer-2.md:
--------------------------------------------------------------------------------
1 | When `second_index` is 3, `second_number` is going to be `nil`.
2 |
3 | You won't be able to add `first_number` (1) to `nil`, which will throw an error.
4 |
--------------------------------------------------------------------------------
/jumpstart-files/practice_assessments/practice_assessment_3.rb:
--------------------------------------------------------------------------------
1 | # ------------------------------------------------------------------------------
2 | # Instructions
3 | # ------------------------------------------------------------------------------
4 | # This file is in the same format as your assessments.
5 | #
6 | # Make sure everything puts out as 'true' when you run the file.
7 | #
8 | # Give yourself an hour to complete this assessment.
9 | # When time is up, make sure you don't have any unexpected `end`s or infinite loops
10 | # that would keep your code from running.
11 | #
12 | # Rename the file to be your firstname and lastname.
13 | #
14 | # Do not use the internet. If you forget a particular ruby method, write it yourself.
15 | #
16 | # Test your code with a few cases in addition to the ones we give
17 | # you. When we grade you, we use different test cases to make sure your logic
18 | # is sound.
19 | #
20 | # Look at the test cases below the problem before you approach it.
21 | # Use the debugger when code doesn't run how you expect.
22 | # ------------------------------------------------------------------------------
23 | #
24 | #
25 | # In All Strings
26 | # ------------------------------------------------------------------------------
27 | # Check if a short_string is a substring of ALL of the long_strings
28 |
29 | def in_all_strings?(long_strings, short_string)
30 | #
31 | # your code goes here
32 | #
33 | end
34 |
35 | puts "---------In All Strings-------"
36 | puts in_all_strings?(["thisisaverylongstring", "thisisanotherverylongstring"], "sisa") == true
37 | puts in_all_strings?(["thisisaverylongstring", "thisisanotherverylongstring"], "isan") == false
38 | puts in_all_strings?(["gandalf", "aragorn", "sauron"], "sam") == false
39 | puts in_all_strings?(["axe", "ajax", "axl rose"], "ax") == true
40 |
41 | # Biodiversity
42 | # ------------------------------------------------------------------------------
43 | # Given an array of specimens, return the biodiversity index, which is defined
44 | # by the following formula: number_of_species^2 times the smallest_population_size
45 | # divided by the largest_population_size.
46 |
47 | # In code, biodiversity = number_of_species**2 * smallest_population_size / largest_population_size
48 | #
49 | # ------------------------------------------------------------------------------
50 |
51 | def biodiversity_index(specimens)
52 | #
53 | # your code goes here
54 | #
55 | end
56 |
57 | puts "------Biodiversity------"
58 | puts biodiversity_index(["cat"]) == 1
59 | puts biodiversity_index(["cat", "cat", "cat"]) == 1
60 | puts biodiversity_index(["cat", "cat", "dog"]) == 2
61 | puts biodiversity_index(["cat", "fly", "dog"]) == 9
62 | puts biodiversity_index(["cat", "fly", "dog", "dog", "cat", "cat"]) == 3
63 |
64 | # For F's Sake
65 | # ------------------------------------------------------------------------------
66 | # Given a string, return the word that has the letter "f" closest to
67 | # the end of it. If there's a tie, return the earlier word. Ignore punctuation.
68 | #
69 | # If there's no f, return an empty string.
70 | # ------------------------------------------------------------------------------
71 |
72 |
73 | def for_fs_sake(string)
74 | #
75 | # your code goes here
76 | #
77 | end
78 |
79 | puts "------For F's Sake------"
80 | puts for_fs_sake("puff daddy") == "puff"
81 | puts for_fs_sake("I got a lot of problems with you people! And now you're gonna hear about it!") == "of"
82 | puts for_fs_sake("fat friars fly fish") == "fat"
83 | puts for_fs_sake("the French call him David Plouffe") == "Plouffe"
84 | puts for_fs_sake("pikachu! i choose you!") == ""
85 |
86 |
87 |
88 | # Censor
89 | # ------------------------------------------------------------------------------
90 | # Write a function censor(sentence, curse_words) that censors the words given.
91 | # Replace the vowels in the curse word with "*".
92 |
93 | def censor(sentence, curse_words)
94 | #
95 | # your code goes here
96 | #
97 | end
98 |
99 | puts "------Censor------"
100 | puts censor("Darn you Harold you son of a gun", ["darn", "gun"]) == "D*rn you Harold you son of a g*n"
101 | puts censor("Schnikeys I don't give a diddly squat", ["schnikeys", "diddly", "squat"]) == "Schn*k*ys I don't give a d*ddly sq**t"
102 |
--------------------------------------------------------------------------------
/jumpstart-files/practice_assessments/practice_assessment_5.rb:
--------------------------------------------------------------------------------
1 | # Write a functinon that takes a string and
2 | # returns a hash in which each key is a character in the string
3 | # pointing to an array indicating the index that the character
4 | # first occurs and last occurs.
5 | #
6 | # If the character occurs only once, the array should hold a single index
7 | #
8 | # Example:
9 | #
10 | # str = "banana"
11 | # return {"b" => [0], "a" => [1, 5], "n" => [2, 4]}
12 | # "b" occurs once at index 0
13 | # "a" occurs three times, but the first is at index 1 and the last is at index 5
14 | # "n" occurs three times, but the first is at index 2 and the last is at index 4
15 |
16 | def first_last_indices(str)
17 | end
18 |
19 |
20 | puts "-------First Last Indices-------"
21 | puts first_last_indices("cat") == {"c" => [0], "a" => [1], "t" => [2]}
22 | puts first_last_indices("dude") == {"d" => [0, 2], "u" => [1], "e" => [3]}
23 | puts first_last_indices("banana") == {"b" => [0], "a" => [1, 5], "n" => [2, 4]}
24 | puts first_last_indices("racecar") == {"r" => [0, 6], "a" => [1, 5], "c" => [2, 4], "e" => [3]}
25 |
26 |
27 | ##################
28 |
29 |
30 | # Anagrams are two words with the exact same letters.
31 | # Write a function that take two strings and returns true if they are anagrams
32 | # and false if they are not.
33 |
34 | def anagrams?(str1, str2)
35 | end
36 |
37 | puts "-------Anagrams-------"
38 | puts anagrams?("alert", "alter") == true
39 | puts anagrams?("desert", "rested") == true
40 | puts anagrams?("banana", "fofanna") == false
41 | puts anagrams?("meat master", "team stream") == true
42 |
43 |
44 | # An abundant number is a number that is less than the sum of its divisors,
45 | # not including itself.
46 |
47 | # Ex. 12's divisors are 1, 2, 3, 4, 6, which sum to 16
48 | # 16 > 12 so 12 is an abundant number.
49 |
50 | # Write a function that takes a number and return true if the number is abundant
51 | # otherwise, return false.
52 |
53 |
54 | ##################
55 |
56 |
57 | def abundant?(num)
58 | end
59 |
60 | puts "-------Abundant-------"
61 | puts abundant?(12) == true
62 | puts abundant?(24) == true
63 | puts abundant?(9) == false
64 | puts abundant?(10001) == false
65 | puts abundant?(20000) == true
66 |
67 |
68 | ##################
69 |
70 |
71 | # Save the Prisoner!
72 | #
73 | # A jail has n prisoners, and each prisoner has a unique ID number, ranging
74 | # from 1 to n. There are m sweets that must be distributed to the prisoners.
75 | # The jailer decides the fairest way to do this is by sitting the prisoners
76 | # down in a circle (ordered by ascending ID number), and then, starting with
77 | # some random prisoner, distribute one candy at a time to each sequentially
78 | # numbered prisoner until all candies are distributed. For example, if the
79 | # jailer picks prisoner ID=2, then his distribution order would be
80 | # (2,3,4,5,...,n-1,n,1,2,3,4,...) until all m sweets are distributed.
81 | #
82 | # But wait — there's a catch — the very last sweet is poisoned! Can you find and
83 | # print the ID number of the last prisoner to receive a sweet so they can be warned?
84 | #
85 | # n => Number of Prisoners
86 | # m => Number of Sweets
87 | # ID => Starting ID
88 | # save_the_prisoner(N,M,ID)
89 |
90 |
91 | def save_the_prisoner(n, m, id)
92 | end
93 |
94 | puts "-------Save The Prisoners-------"
95 | puts save_the_prisoner(5,2,1) == 2
96 | puts save_the_prisoner(99, 99, 1) == 99
97 | puts save_the_prisoner(49, 98, 2) == 1
98 | puts save_the_prisoner(94431605, 679262176, 5284458) == 23525398
99 |
100 |
101 | ##################
102 |
103 |
104 | # Write a method, #build_power_plants, that will take in two arguments. The
105 | # first of which is the number of cities that need to be powered. The cities
106 | # are arranged in a line and are equidistant from eachother.
107 | # The second argument will be the range of each powerplant (the number of cities
108 | # in each direction a plant can reach and power). So, a range of 1 means that each
109 | # plant can only reach the cities on either side of it. A range of 0 means that the
110 | # plants can only power the city they are built in.
111 | #
112 | # Your function should dictate which cities' powerplants need to be built such
113 | # that all of the cities have power, and the least number of cities have powerplants
114 | # as possible.
115 | #
116 | # Output an array of the cities, with 0 representing a city without a powerplant
117 | # and 1 representing a city with one.
118 | #
119 | # For example:
120 | # build_power_plants(3, 2) means that there are three cities, and each powerplant
121 | # will have a range of two cities. Therefore your function should output the array
122 | # [ 0, 1, 0 ]
123 |
124 |
125 | def build_power_plants(number_of_cities, plant_range)
126 | end
127 |
128 | puts "-------Power Plants-------"
129 | puts build_power_plants(0,1) == []
130 | puts build_power_plants(1,1) == [1]
131 | puts build_power_plants(1,4) == [1]
132 | puts build_power_plants(2,1) == [1, 0] || build_power_plants(2,1) == [0, 1]
133 | puts build_power_plants(2,0) == [1, 1]
134 | puts build_power_plants(3,0) == [1, 1, 1]
135 | puts build_power_plants(3,1) == [0, 1, 0]
136 | puts build_power_plants(3,2) == [1, 0, 0] ||
137 | build_power_plants(3,2) == [0, 1, 0] ||
138 | build_power_plants(3,2) == [0, 0, 1]
139 | puts build_power_plants(5,1) == [0, 1, 0, 1, 0] ||
140 | build_power_plants(5,1) == [1, 0, 0, 1, 0] ||
141 | build_power_plants(5,1) == [0, 1, 0, 0, 1]
142 | puts build_power_plants(5,2) == [0, 0, 1, 0, 0]
143 | puts build_power_plants(10, 2) == [0, 0, 1, 0, 0, 0, 0, 1, 0, 0]
144 | puts build_power_plants(8, 3).count(1) == 2
145 |
--------------------------------------------------------------------------------
/jumpstart-files/pre-curriculum/c9_setup.md:
--------------------------------------------------------------------------------
1 | # Ruby Setup
2 |
3 | ## Make a Cloud9 account
4 |
5 | Start by making a [Cloud9 account][c9]. Cloud9 will ask for your credit card info, but they won't charge you unless you specifically ask to upgrade (we'll vouch for them!) If you'd prefer not to disclose your credit card info, feel free to use [CodeAnywhere][ca] instead.
6 |
7 | Once you have an account on Cloud9, follow the following steps:
8 |
9 | 
10 |
11 | 0. Create a new workspace; call it 'jumpstart_workspace'
12 | 0. Choose 'custom' when asked to chose a template
13 |
14 | ## Test that it works for you
15 |
16 | Test your environment by following these steps:
17 |
18 | 
19 |
20 | 0. Create a new file called `hello_world.rb`; put it in the 'jumpstart_workspace' folder
21 | 0. write the line `puts "Hello World!"` in `hello_world.rb`
22 | 0. Open a new terminal and run `ruby hello_world.rb` - You should see "Hello World!"
23 |
24 | [c9]: https://c9.io
25 | [ca]: https://codeanywhere.com
26 |
--------------------------------------------------------------------------------
/jumpstart-files/pre-curriculum/debugger-setup-cloud9.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/pre-curriculum/debugger-setup-cloud9.gif
--------------------------------------------------------------------------------
/jumpstart-files/pre-curriculum/debugger_setup.md:
--------------------------------------------------------------------------------
1 | # Debugger setup
2 |
3 | We're going to be using the debugger a lot in your course. We'd like you to install it ahead of time. Here's how.
4 |
5 | ## Instructions
6 |
7 | 1. Open your Cloud9 workspace.
8 | 2. In your console, run `gem install byebug`.
9 | 3. Make a new file and `require 'byebug'` at the top.
10 | 4. Test out that you can drop the word `byebug` into a sample function and that you can print out the argument. Make sure you return something from the function.
11 | 5. Make sure you've saved your file.
12 |
13 | Note: If you are using Terminal on a Mac instead of Cloud9, you need to set up [ruby version control](./setup_rbenv.md) before installing any gems.
14 |
15 | If you're confused, see below:
16 |
17 | 
18 |
19 |
20 |
--------------------------------------------------------------------------------
/jumpstart-files/pre-curriculum/hello-world-cloud9.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/pre-curriculum/hello-world-cloud9.gif
--------------------------------------------------------------------------------
/jumpstart-files/pre-curriculum/intro.md:
--------------------------------------------------------------------------------
1 | # Intro to JumpStart
2 |
3 | ## Course Structure
4 |
5 | Here's what the average day will look like:
6 |
7 | When | What
8 | ---------- |:---------------------:
9 | 6:30pm | Attendance
10 | 6:30 - 7:00 | Code demo & Q/A
11 | 7:00 - 7:30 | Warm-up
12 | 7:30 - 9:30 | Independent work
13 | 9:30 + | Homework problem
14 |
15 | ### Attendance
16 | We will take attendance every day; your record will be considered when making admission decisions. You **must** be present for attendance on assessment days.
17 |
18 | ### Code Demo
19 | During the coding demo, we will demonstrate solving a challenging coding problem that was assigned as homework the night before. Here, we will focus more on problem-solving architectures and design techniques.
20 |
21 | ### Warm-up
22 | You will have a different warm-up partner every day. Pairs will be assigned via email at the beginning of the day. After meeting your pair, the two of you will solve two sets of problems and answer the discussion questions together.
23 |
24 | ### Independent Work
25 | The majority of your time in JumpStart will be spent doing independent work. The independence will allow you to go through the curriculum at your own pace, but will also require some intrinsic motivation.
26 |
27 | You will be grouped into pods with other students at similar levels, and each pod will have a TA mentor. You are **strongly encouraged** to discuss problems with your fellow pod members and to ask your TA mentor for help.
28 |
29 | ### Homework
30 | You will have one homework problem every night. If your time is limited during JumpStart, feel free do the homework problem during independent work time. Don't spend more than 20 - 30 minutes on it (unless you want to!). We'll do the problem together in class (the next day) during the code demo.
31 |
32 | ---
33 |
34 | ## Assessments
35 |
36 | You will have an assessment on the second Monday and Thursday during JumpStart (W2D1 and W2D4). If you receive a passing score on either, your application will be forwarded to our admissions team for a final review. You do not need to attend week 2 if you pass assessment 1.
37 |
38 | ## How to approach JumpStart
39 |
40 | Everyone who clears our bar gets in. This means you should be helping your fellow JumpStarters. **You are not competing with each other.** There is no limit to the number of accepted students.
41 |
42 | You're all at different levels and will work at different paces. **Don't worry if someone moves faster than you** or solves problems faster than you. What's important is that you get the most out of this investment of your time.
43 |
44 | Remember that passing this assessment can speed up your application process, but failing it **will not hurt your chances of getting into a/A.** Jumpstart is a small, small piece of your journey to becoming a developer. It's outcome pails in comparison to your intrinsic motivation.
45 |
--------------------------------------------------------------------------------
/jumpstart-files/pre-curriculum/remove_rvm.md:
--------------------------------------------------------------------------------
1 | ## Removing RVM
2 |
3 | 0. Open terminal
4 | 0. Copy, paste, and run the following, one line at a time:
5 | - `sudo rvm implode`
6 | - If it says `sudo: rvm: command not found`, that's fine -- you're done here!
7 | - `sudo rm -rf ~/.rvm`
8 |
--------------------------------------------------------------------------------
/jumpstart-files/pre-curriculum/setup-cloud9.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/pre-curriculum/setup-cloud9.gif
--------------------------------------------------------------------------------
/jumpstart-files/pre-curriculum/setup_rbenv.md:
--------------------------------------------------------------------------------
1 | ##Installing rbenv on Mac
2 |
3 | ### rbenv:
4 |
5 | If you have previously installed rvm, you must [remove it!][remove-rvm]
6 |
7 | 0. Open the terminal
8 | 0. [Install Homebrew](http://brew.sh)
9 | 0. Copy, paste, and run the following:
10 | - `brew install rbenv`
11 | - `brew install ruby-build`
12 | 0. Copy, paste, and run the following:
13 | - `echo 'export PATH=$PATH:$HOME/.rbenv/bin' >> ~/.bash_profile`
14 | - `echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile`
15 | 0. Close the terminal, and re-open
16 | 0. Copy, paste, and run:
17 | - `rbenv install 2.1.2`
18 | - `rbenv global 2.1.2`
19 |
20 | Try running a ruby program to test!
21 |
22 |
23 | ### Atom:
24 |
25 | In addition to ruby, you'll want a native text editor. We recommend using [atom][atom].
26 |
27 | [remove-rvm]: ./remove_rvm.md
28 | [atom]: https://atom.io/
29 |
--------------------------------------------------------------------------------
/jumpstart-files/problem_sets/problem_set_1.rb:
--------------------------------------------------------------------------------
1 | # Using comments, write out an approach in pseudocode for each of these first.
2 |
3 | # Write a function that given an array, returns another array with each of the numbers multiplied by two. Don't change the original array, make sure you construct a copy!
4 | def array_times_two(array)
5 | end
6 |
7 | # Tests — these should all spit out true!
8 | puts "\nArray times two:\n" + "*" * 15 + "\n"
9 | puts array_times_two([1, 2, 3]) == [2, 4, 6]
10 | puts array_times_two([0, -1, -2]) == [0, -2, -4]
11 | dont_change_this = [3, 4, 5]
12 | array_times_two(dont_change_this)
13 | puts dont_change_this == [3, 4, 5]
14 |
15 | # *********************************************
16 |
17 | # Write a function that given an array, now CHANGES each of the numbers to be twice as big. This should mutate the original array!
18 | def array_times_two!(array)
19 | end
20 |
21 | # Tests
22 | puts "\nArray times two!:\n" + "*" * 15 + "\n"
23 | puts array_times_two!([1, 2, 3]) == [2, 4, 6]
24 | change_this = [6, 7, 8]
25 | array_times_two!(change_this)
26 | puts change_this == [12, 14, 16]
27 |
28 | # *********************************************
29 |
30 | # Write a function that given an array, returns another array of only the unique elements. I.e., return a version without duplicates.
31 | def uniq(array)
32 | end
33 |
34 | # Tests
35 | puts "\nUniq:\n" + "*" * 15 + "\n"
36 | puts uniq([5, 5, 5, 5]) == [5]
37 | puts uniq([1]) == [1]
38 | puts uniq([1, 2, 1, 3, 3]) == [1, 2, 3]
39 |
40 | # *********************************************
41 |
42 | # A Slippery Number is a number is that has 3 as a factor or has 5 as a factor, but does *not* have both as factors. For example, 6 is a Slippery Number, but 30 is not. Write a function that given an N, returns an array of the first N Slippery numbers.
43 |
44 | # You'll want to write a helper function that helps you determine which numbers are Slippery.
45 |
46 | def slippery_numbers(n)
47 | end
48 |
49 | def is_slippery?(number)
50 | end
51 |
52 | # Tests
53 | puts "\nSlippery numbers:\n" + "*" * 15 + "\n"
54 | puts slippery_numbers(1) == [3]
55 | puts slippery_numbers(2) == [3, 5]
56 | puts slippery_numbers(7) == [3, 5, 6, 9, 10, 12, 18]
57 |
58 | # *********************************************
59 |
60 | # Write a function that finds whether any two elements in the array sum to 0. If it does, return true; else, return false.
61 | def two_sum_to_zero?(array)
62 | end
63 |
64 | # Tests
65 | puts "\nTwo sum to zero?:\n" + "*" * 15 + "\n"
66 | puts two_sum_to_zero?([4, 2, 6]) == false
67 | puts two_sum_to_zero?([-2, 5, 12, -3, 2]) == true
68 | puts two_sum_to_zero?([0, 5]) == false
69 |
70 | # *********************************************
71 |
72 | # A magic number is a number whose digits, when added together, sum to 7. For example, the number 34 would be a magic number, because 3 + 4 = 7. Write a function that finds the first N many magic numbers.
73 |
74 | # You'll want to write a helper function that checks whether a given number is a magic number.
75 | # Reminder: you can convert an integer to a string using #to_s. You can convert a string back to an integer using #to_i.
76 |
77 | def magic_numbers(count)
78 | end
79 |
80 | def is_magic_number?(number)
81 | end
82 |
83 | # Tests
84 | puts "\nMagic numbers:\n" + "*" * 15 + "\n"
85 | puts magic_numbers(1) == [7]
86 | puts magic_numbers(3) == [7, 16, 25]
87 | puts magic_numbers(20) == [7, 16, 25, 34, 43, 52, 61, 70, 106, 115, 124, 133, 142, 151, 160, 205, 214, 223, 232, 241]
88 |
--------------------------------------------------------------------------------
/jumpstart-files/problem_sets/problem_set_3.rb:
--------------------------------------------------------------------------------
1 | # ***********************************
2 | # Write a method that takes an array of numbers as input and uses #select to return an array only of even numbers
3 |
4 | def get_evens(array)
5 | end
6 |
7 | puts "\nGet evens:\n" + "*" * 15 + "\n"
8 | puts get_evens([1, 2, 3, 4, 5, 6, 7]) == [2, 4, 6]
9 | puts get_evens([2, 4, 6, 8, 10, 12, 14]) == [2, 4, 6, 8, 10, 12, 14]
10 | puts get_evens([1, 3, 5, 7, 9, 11]) == []
11 |
12 | # ***********************************
13 | # Write a method that takes an array of numbers as input and uses #reject to return an array of number that aren't odd
14 |
15 | def reject_odds(array)
16 | end
17 |
18 | puts "\nReject odds:\n" + "*" * 15 + "\n"
19 | puts reject_odds([1, 2, 3, 4, 5]) == [2, 4]
20 | puts reject_odds([2, 4, 6, 8, 10]) == [2, 4, 6, 8, 10]
21 | #puts reject_odds([1.1, 3.5, 2.9]) == [1.1, 3.5, 2.9]
22 |
23 | # ************************************
24 | # Write a method that uses #reduce to sum up the numbers in an array
25 |
26 | def array_sum(array)
27 | end
28 |
29 | puts "\nArray sum:\n" + "*" * 15 + "\n"
30 | puts array_sum([]) == 0
31 | puts array_sum([1, 2, 3]) == 6
32 | puts array_sum([5, 5, 5, 5, 5]) == 25
33 |
34 | # ************************************
35 | # Write a method that takes an array of integers and returns an array of those values doubled.
36 | # This method should *not* modify the original array
37 |
38 | def calculate_doubles(array)
39 | end
40 |
41 | puts "\nCalculate doubles:\n" + "*" * 15 + "\n"
42 | array = [1, 2, 3, 4, 5]
43 | doubled_array = calculate_doubles(array)
44 | puts array != doubled_array
45 | puts doubled_array == [2, 4, 6, 8, 10]
46 |
47 | # ************************************
48 | # Write a method that takes an array of integers and returns an array of those values doubled.
49 | # This method *should* modify the original array
50 |
51 | def calculate_doubles!(array)
52 | end
53 |
54 | puts "\nCalculate doubles!:\n" + "*" * 15 + "\n"
55 | array = [1, 2, 3, 4, 5]
56 | calculate_doubles!(array)
57 | puts array == [2, 4, 6, 8, 10]
58 |
59 | # ************************************
60 | # Write a method that takes an array of numbers as input and returns the sum of each element multiplied by its index.
61 | # For example, [2, 9, 7] would return 23, since (2 * 0) + (9 * 1) + (7 * 2) = 0 + 9 + 14 = 23
62 |
63 | def array_sum_with_index(array)
64 | end
65 |
66 | puts "\nArray sum with index:\n" + "*" * 15 + "\n"
67 | puts array_sum_with_index([0, 1, 2, 3]) == 14
68 |
69 | # ************************************
70 | # Write a method that uses sort to implement the max function. Your method should take an array as input and return
71 | # the element with the maximum value, calling sort once.
72 |
73 | def my_max(array)
74 | end
75 |
76 | puts "\nMy max:\n" + "*" * 15 + "\n"
77 | puts my_max([5, 9, 2, 4, 7]) == 9
78 | puts my_max([1, 4, 9, 19, 3]) == 19
79 |
80 | # ************************************
81 | # Write a method that uses sort to implement the min function. Your method should take an array as input and return
82 | # the element with the minimum value, calling sort once.
83 |
84 | def my_min(array)
85 | end
86 |
87 | puts "\nMy min:\n" + "*" * 15 + "\n"
88 | puts my_min([5, 9, 2, 4, 7]) == 2
89 | puts my_min([0, -2, -5, -5, 1]) == -5
90 |
91 | # ************************************
92 | # Write a method that returns the third greatest element in an array
93 |
94 | def third_greatest(array)
95 | end
96 |
97 | puts "\nThird greatest:\n" + "*" * 15 + "\n"
98 | puts third_greatest([5, 9, 3, 7, 7, 2, 10]) == 7
99 |
100 | # ************************************
101 | # Write a method that takes a string of words separated by spaces and returns the longest word. If there is more
102 | # than one word of that length, it should return the first instance of that word.
103 |
104 | def longest_word(string)
105 | end
106 |
107 | puts "\nLongest word:\n" + "*" * 15 + "\n"
108 | puts longest_word("we the people in order to form a more perfect union establish justice ensure domestic tranquility") == "tranquility"
109 | puts longest_word("one two three four five six seven") == "three"
110 |
111 | # ************************************
112 | # Write a method that takes a string and counts the number of vowels in the string. For our purposes, "y" counts as a vowel.
113 | # You might want to use the Array#include? method for this. The usage is:
114 | # [1, 2, 3, 4, 5].include?(2) => true
115 | # [1, 2, 3, 4, 5].include?("k") => false
116 | # Assume all letters will be lower case
117 |
118 | def count_vowels(string)
119 | end
120 |
121 | puts "\nCount vowels:\n" + "*" * 15 + "\n"
122 | puts count_vowels("cat dog elephant monkey") == 8
123 | puts count_vowels("abcdefghijklmnopqrstuvwxyz") == 6
124 |
125 | # ************************************
126 | # Write a method that takes a string of words separated by spaces and returns a string the same as the original, but
127 | # with five-letter words replaced with "#####"
128 |
129 | def redact_five_letter_words(string)
130 | end
131 |
132 | puts "\nRedact five letter words:\n" + "*" * 15 + "\n"
133 | puts redact_five_letter_words("one two three four five six seven eight") == "one two ##### four five six ##### #####"
134 | puts redact_five_letter_words("we the people in order to form a more perfect union establish justice ensure domestic tranquility") == "we the people in ##### to form a more perfect ##### establish justice ensure domestic tranquility"
135 |
136 | # ************************************
137 | # Write a method that takes a string of words separated by spaces and returns the most common vowel. If more than one vowel has that count,
138 | # return the one that occurs earliest in the alphabet. For our purposes, count "y" as a vowel.
139 | # Assume all letters are lower case.
140 |
141 | def most_common_vowel(string)
142 | end
143 |
144 | puts "\nMost common vowel:\n" + "*" * 15 + "\n"
145 | puts most_common_vowel("we the people in order to form a more perfect union establish justice ensure domestic tranquility") == "e"
146 | puts most_common_vowel("cat dog octopus spider") == "o"
147 |
--------------------------------------------------------------------------------
/jumpstart-files/problem_sets/problem_set_4.rb:
--------------------------------------------------------------------------------
1 |
2 | # Write a method that takes an array of words and returns only the words that are
3 | # anagrams of a given string.
4 |
5 | def anagrams(string, array)
6 | end
7 |
8 | #Tests
9 | puts "\nAnagrams:\n" + "*" * 15 + "\n"
10 | puts anagrams("cat", ["cat"]) == ["cat"]
11 | words = ["cat", "act", "bat", "tac"]
12 | puts anagrams("tca", words) == ["cat", "act", "tac"]
13 | words = ["aaa", "aa", "a"]
14 | puts anagrams("aa", words) == ["aa"]
15 |
16 | # ************************************
17 | # Write a boolean function that returns true if the vowels in a given word appear in order
18 |
19 | def ordered_vowel_word?(word)
20 | end
21 |
22 | #Tests
23 | puts "\nOrdered Vowel Word:\n" + "*" * 15 + "\n"
24 | puts ordered_vowel_word?("car") == true
25 | puts ordered_vowel_word?("academy") == true
26 | puts ordered_vowel_word?("programmer") == false
27 | puts ordered_vowel_word?("grapefruit") == false
28 |
29 | # ************************************
30 | # Write a function that takes an array of words and returns the words whose vowels appear in order
31 |
32 | def ordered_vowels(words)
33 | end
34 |
35 | puts "\nOrdered Vowels:\n" + "*" * 15 + "\n"
36 | puts ordered_vowels(["are"]) == ["are"]
37 | puts ordered_vowels(["era", "are", "ear"]) == ["are"]
38 | puts ordered_vowels(["hey", "wassup", "hello"]) == ["hey", "wassup", "hello"]
39 | puts ordered_vowels(["firefox", "chrome", "safari", "netscape", "aeiou"]) == ["safari", "aeiou"]
40 |
41 | # ************************************
42 | # Write a function that takes two years and returns all the years within that range with no repeated digits.
43 | # Hint: helper method?
44 | # no_repeat_years(2010,2015) -> [2013,2014,2015]
45 |
46 | def no_repeat_years(first_year, last_year)
47 | end
48 |
49 | puts "\nNo Repeat Years:\n" + "*" * 15 + "\n"
50 | puts no_repeat_years(1990, 2000) == []
51 | puts no_repeat_years(1900,1902) == [1902]
52 | puts no_repeat_years(2016, 2020) == [2016, 2017, 2018, 2019]
53 |
54 |
55 | # ************************************
56 | # Write a method that takes a string of lower case words (no punctuation) and returns the letter that occurs most frequently.
57 | # Use a hash within your method to keep track of the letter counts. You will need to account for spaces. There are a few ways you can do this.
58 |
59 | def most_frequent_letter(string)
60 | end
61 |
62 | puts "\nMost Frequent Letter\n" + "*" * 15 + "\n"
63 | puts most_frequent_letter("aaaaa") == "a"
64 | puts most_frequent_letter("aaaaabbbbbb") == "b"
65 | puts most_frequent_letter("we the people in order to form a more perfect union") == "e"
66 |
67 |
68 | # ************************************
69 | # Write a method that takes a string of lower case words (no punctuation) and returns an array of letters that occur more
70 | # than once. We'll need to account for spaces, too. Again, there are a few ways you can do this.
71 |
72 | def non_unique_letters(string)
73 | end
74 |
75 | puts "\nNon-Unique Letters\n" + "*" * 15 + "\n"
76 | puts non_unique_letters("abbbcdddde") == ["b", "d"]
77 | puts non_unique_letters("abcde") == []
78 | puts non_unique_letters("aabbccddee") == ["a", "b", "c", "d", "e"]
79 | puts non_unique_letters("aaa bbb c d eee") == ["a", "b", "e"]
80 |
81 | # ************************************
82 | # Write a method that takes a string of lower case words and returns an array of letters that do not occur in the string.
83 | # This problem requires a different approach from the one we used in most_frequent_letter and we will need to show some care
84 | # in how we set up the hash or process it to get the result. Do you see why?
85 | # We'll need to account for spaces, too. Again, there are a few ways you can do this.
86 |
87 | def missing_letters(string)
88 | end
89 |
90 | puts "\nMissing Letters\n" + "*" * 15 + "\n"
91 | puts missing_letters("abcdefghijklmnopqrstuvwxyz") == []
92 | puts missing_letters("abcdefghiklmnopqrstuvwxyz") == ["j"]
93 | puts missing_letters("abcdefghiklmnopstuvwxyz") == ["j", "q", "r"]
94 |
95 | #write a function primes that an argument n and returns the first n primes
96 |
97 | def primes(n)
98 | end
99 |
100 | puts "\nPrimes:\n" + "*" * 15 + "\n"
101 | puts primes(0) == []
102 | puts primes(1) == [2]
103 | puts primes(2) == [2,3]
104 | puts primes(6) == [2,3,5,7,11,13]
105 |
106 | #write a boolean function zero_sum? that takes an array of integers and returns
107 | #true if 2 elements in the array sum to zero.
108 |
109 | # NOTE: For this question, do the "write out the variables" exercise from part 4 after
110 | # you've written a first draft.
111 |
112 | def zero_sum?(array)
113 | end
114 |
115 | puts "\nZero Sum:\n" + "*" * 15 + "\n"
116 | puts zero_sum?([1, -1]) == true
117 | puts zero_sum?([1,1,0,2,1]) == false
118 | puts zero_sum?([1,1,0,2,1,0]) == true
119 | puts zero_sum?([2,3,4,-3,1]) == true
120 |
121 | #write a function largest_sum_pair that takes an array of intergers and returns the
122 | #2 unique indices whose elements sum to the largest number. Formatted [earlier_index, later_index]
123 |
124 | # NOTE: For this question, do the "write out the variables" exercise from part 4 after
125 | # you've written a first draft.
126 |
127 | def largest_sum_pair(array)
128 | end
129 |
130 | puts "\nLargest Sum Pair:\n" + "*" * 15 + "\n"
131 | puts largest_sum_pair([1,2,3,4,5]) == [3,4]
132 | puts largest_sum_pair([3,2,1,0,1,2,3]) == [0,6]
133 | puts largest_sum_pair([-2,-1,-1,-2,-3]) == [1,2]
134 |
--------------------------------------------------------------------------------
/jumpstart-files/problem_sets/problem_set_5.rb:
--------------------------------------------------------------------------------
1 | # chunk an array into nested arrays of length n
2 | def chunk(array, n)
3 |
4 | end
5 |
6 | puts "---------chunk-------"
7 | puts chunk([1, 8, 9, 4, "hey", "there"], 2) == [[1, 8], [9, 4], ["hey", "there"]]
8 | puts chunk([10, 9, 8, 7, 6, 5, 4], 3) == [[10, 9, 8], [7, 6, 5], [4]]
9 |
10 | # Translate into pig-latin! First consonants go to the end of a word. End with "ay"
11 | def pig_latin(sentence)
12 | end
13 |
14 | puts "---------pig latin-------"
15 | puts pig_latin("i speak pig latin") == "iay eakspay igpay atinlay"
16 | puts pig_latin("throw me an aardvark") == "owthray emay anay aardvarkay"
17 |
18 | # Remove the nth letter of the string
19 | def remove_nth_letter(string, n)
20 |
21 | end
22 |
23 | puts "---------remove nth letter-------"
24 | puts remove_nth_letter("helloworld", 5) == "helloorld"
25 | puts remove_nth_letter("helloworld", -3) == "hellowold"
26 |
27 | # Boolean function: check if short_string is a substring of long_string
28 | def substring?(long_string, short_string)
29 |
30 | end
31 |
32 | puts "---------substring-------"
33 | puts substring?("thisisaverylongstring", "sisa") == true
34 | puts substring?("thisisaverylongstring", "ting") == false
35 | puts substring?("whatifikeptontypingforever", "ik") == true
36 |
37 | # count the number of times that two adjacent numbers in an array add up to n.
38 | # You cannot reuse a number. So count_adjacent_sums([1, 5, 1], 6) => 1
39 |
40 | def count_adjacent_sums(array, n)
41 |
42 | end
43 |
44 | puts "---------count adjacent sums-------"
45 | puts count_adjacent_sums([7, 2, 4, 6, 8, 10], 7) == 0
46 | puts count_adjacent_sums([6, 7, 11, 2, 5, 10, 3], 13) == 3
47 | puts count_adjacent_sums([1, 9, 1, 8, 2, 10], 10) == 2
48 |
49 | # update the older inventory with the newer inventory. Add any new items to the
50 | # hash and replace the values for items that already exist.
51 |
52 | def inventory_hash(older, newer)
53 |
54 | end
55 |
56 | puts "---------inventory hash-------"
57 | march = {rubies: 10, emeralds: 14, diamonds: 2}
58 | april = {emeralds: 27, moonstones: 5}
59 | puts inventory_hash(march, april) == {rubies: 10, emeralds: 27, diamonds: 2, moonstones: 5}
60 |
61 | # Now, alphabetical order matters in your inventory. Insert new inventory items into
62 | # your array in the appropriate place
63 | def inventory_array(older, newer)
64 |
65 | end
66 |
67 | puts "---------inventory array-------"
68 | march_array = [['diamonds', 2], ['emeralds', 14], ['rubies', 10]]
69 | april_array = [['emeralds', 27], ['moonstones', 5]]
70 | puts inventory_array(march_array, april_array) == [['diamonds', 2], ['emeralds', 27], ['moonstones', 5], ['rubies', 10]]
71 |
--------------------------------------------------------------------------------
/jumpstart-files/problem_sets/problem_set_6.rb:
--------------------------------------------------------------------------------
1 |
2 | #write a function that takes a number n and returns the highest prime factor of
3 | #that number. Hint: Helper function.
4 |
5 | def highest_prime_factor(n)
6 |
7 | end
8 |
9 | puts "\nHighest prime factor:\n" + "*" * 15 + "\n"
10 | puts highest_prime_factor(4) == 2
11 | puts highest_prime_factor(6) == 3
12 | puts highest_prime_factor(28) == 7
13 | puts highest_prime_factor(69842) == 743
14 |
15 | #write a boolean function zero_sum? that takes an array of integers and returns
16 | #true if 2 elements in the array sum to zero.
17 |
18 | def zero_sum?(array)
19 |
20 | end
21 |
22 | puts "\nZero Sum:\n" + "*" * 15 + "\n"
23 | puts zero_sum?([1, -1]) == true
24 | puts zero_sum?([1,1,0,2,1]) == false
25 | puts zero_sum?([1,1,0,2,1,0]) == true
26 | puts zero_sum?([2,3,4,-3,1]) == true
27 |
28 |
29 |
30 | #write a function missing_letters that take in a phrase and returns an array of
31 | #letters in alphabetical order that are not contained in that phrase
32 |
33 | def missing_letters(phrase)
34 |
35 | end
36 |
37 | puts "\nMissing Letters\n" + "*" * 15 + "\n"
38 | puts missing_letters("abcdefghijklmnopqrstuvwxyz") == []
39 | puts missing_letters("abcdefghiklmnopqrstuvwxyz") == ["j"]
40 | puts missing_letters("abcdefghiklmnopstuvwxyz") == ["j", "q", "r"]
41 | puts missing_letters("we the people of the united states in order to form a more perfect union") == "bgjkqvxyz".split("")
42 |
43 |
44 | #Write a function count_odds that takes an array of numbers and returns the count
45 | #of odd numbers in that array
46 | def count_odds(numbers_array)
47 |
48 | end
49 |
50 | puts "\nCount Odds\n" + "*" * 15 + "\n"
51 |
52 | puts count_odds([]) == 0
53 | puts count_odds([2]) == 0
54 | puts count_odds([1,2]) == 1
55 | puts count_odds([1,2,3,4,5,6]) == 3
56 | puts count_odds([123,42,23,71,0]) == 3
57 |
58 | #Write a function delete_first_letter that takes a phrase and a letter to hide and removes
59 | #the first instances of that letter
60 | def delete_first_letter(phrase, letter)
61 |
62 | end
63 |
64 | puts "\nHide Letter\n" + "*" * 15 + "\n"
65 | puts delete_first_letter("abc", "d") == "abc"
66 | puts delete_first_letter("abc", "b") == "ac"
67 | puts delete_first_letter("hello, my name is Tevy", "e") == "hllo, my name is Tevy"
68 |
--------------------------------------------------------------------------------
/jumpstart-files/problem_sets/problem_set_7.rb:
--------------------------------------------------------------------------------
1 | # Sign Tweakers
2 | # ------------------------------------------------------------------------------
3 | # Given a perfectly respectable business sign, determine if pranksters George
4 | # and Harold can make a vandalized sign out of it using the same or fewer letters.
5 | # Ignore capitalization and punctuation.
6 |
7 | def can_tweak_sign?(normal_sign, vandalized_sign)
8 | #
9 | # your code goes here
10 | #
11 | end
12 |
13 | puts "-------Sign Tweakers-------"
14 | puts can_tweak_sign?("Come in and see our pretty armchairs", "Come and see our hairy armpits") == true
15 | puts can_tweak_sign?("Student and teacher art fair starts Wed.", "Teacher farts stain underwear") == true
16 | puts can_tweak_sign?("Choose the bread of life or you are toast", "Teacher farts stain underwear") == false
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | # Repeated Number Ranges
25 | # ------------------------------------------------------------------------------
26 | # Given a list of numbers, give the start and end indices each time a number shows
27 | # up multiple times in a row.
28 | #
29 |
30 | def repeated_number_ranges(numbers)
31 | #
32 | # your code goes here
33 | #
34 | end
35 |
36 | puts "-------Repeated Number Ranges-------"
37 | puts repeated_number_ranges([1, 1, 2]) == [[0, 1]]
38 | puts repeated_number_ranges([1, 2, 3, 3, 4]) == [[2, 3]]
39 | puts repeated_number_ranges([1, 2, 3, 3, 4, 4]) == [[2, 3], [4, 5]]
40 | puts repeated_number_ranges([1, 1, 1, 2, 3, 3, 4]) == [[0, 2], [4, 5]]
41 | puts repeated_number_ranges([8, 7, 7, 14, 12, 12, 12, 12, 21]) == [[1, 2], [4, 7]]
42 |
43 |
44 |
45 |
46 |
47 |
48 | # Time Sums
49 | # ------------------------------------------------------------------------------
50 | # Return an array of all the minutes of the day whose digits sum to N.
51 | #
52 | # Use military time, so 1:00 PM is really 13:00 PM.
53 | # ------------------------------------------------------------------------------
54 |
55 | def time_sums(n)
56 | #
57 | # your code goes here
58 | #
59 | end
60 |
61 |
62 | puts "\nTens Time\n" + "*" * 15 + "\n"
63 | puts time_sums(0) == ["00:00"]
64 | puts time_sums(1) == ["00:01", "00:10", "01:00", "10:00"]
65 | puts time_sums(23) == ["09:59", "18:59", "19:49", "19:58"]
66 | puts time_sums(24) == ["19:59"]
67 |
68 |
69 |
70 |
71 |
72 |
73 | # Fall and Winter Birthdays
74 | # ------------------------------------------------------------------------------
75 | # Given a list of students and what month their birthday is, return all the pairs
76 | # of students whose birthdays both fall in the second half of the year. Months
77 | # are numbers, and assume that July (month 7) and later is the second half of
78 | # the year.
79 | #
80 | # Only count pairs once, and work from the beginning of the list to the end.
81 | #
82 |
83 | def fall_and_winter_birthdays(students_with_birthdays)
84 | #
85 | # your code goes here
86 | #
87 | end
88 |
89 | students_with_birthdays_1 = [
90 | ["Asher", 6],
91 | ["Bertie", 11],
92 | ["Dottie", 8],
93 | ["Warren", 9]
94 | ]
95 |
96 | second_half_birthday_pairs_1 = [
97 | ["Bertie", "Dottie"],
98 | ["Bertie", "Warren"],
99 | ["Dottie", "Warren"]
100 | ]
101 |
102 | students_with_birthdays_2 = [
103 | ["Asher", 6],
104 | ["Bertie", 11],
105 | ["Dottie", 8],
106 | ["Warren", 9],
107 | ["Charlie", 7],
108 | ["Nassim", 4],
109 | ["Ajit", 10],
110 | ]
111 |
112 | second_half_birthday_pairs_2 = [
113 | ["Bertie", "Dottie"],
114 | ["Bertie", "Warren"],
115 | ["Bertie", "Charlie"],
116 | ["Bertie", "Ajit"],
117 | ["Dottie", "Warren"],
118 | ["Dottie", "Charlie"],
119 | ["Dottie", "Ajit"],
120 | ["Warren", "Charlie"],
121 | ["Warren", "Ajit"],
122 | ["Charlie", "Ajit"],
123 | ]
124 |
125 |
126 | puts "\nFall and Winter Birthdays\n" + "*" * 15 + "\n"
127 | puts fall_and_winter_birthdays(students_with_birthdays_1) == second_half_birthday_pairs_1
128 | puts fall_and_winter_birthdays(students_with_birthdays_2) == second_half_birthday_pairs_2
129 |
130 |
131 |
132 |
133 |
134 |
135 | # Care Bear Summary
136 | # ------------------------------------------------------------------------------
137 | # You have a calendar of hugs that care bears made (given as a list of
138 | # names by care bears). Some of them have hugged multiple
139 | # times in a row.
140 | #
141 | # Return a hash where the keys are the care bears and the values are an array
142 | # of all of the start and end days of their hugging streaks.
143 | #
144 | # Days are the index of the calendar array.
145 |
146 | def care_bear_summary(calendar_of_hugs)
147 | #
148 | # your code goes here
149 | #
150 | end
151 |
152 | puts "\nCare Bear Summary\n" + "*" * 15 + "\n"
153 |
154 | hug_calendar_1 = [
155 | "Birthday Bear",
156 | "Bedtime Bear",
157 | "Birthday Bear",
158 | "Birthday Bear",
159 | "Bedtime Bear"
160 | ]
161 |
162 | care_bear_counts_1 = {
163 | "Birthday Bear" => [[2, 3]]
164 | }
165 |
166 | hug_calendar_2 = [
167 | "Birthday Bear",
168 | "Birthday Bear",
169 | "Cheer Bear",
170 | "Bedtime Bear",
171 | "Bedtime Bear",
172 | "Birthday Bear",
173 | "Birthday Bear",
174 | "Birthday Bear",
175 | "Bedtime Bear",
176 | "Friend Bear"
177 | ]
178 |
179 | care_bear_counts_2 = {
180 | "Birthday Bear" => [[0, 1], [5, 7]],
181 | "Bedtime Bear" => [[3, 4]]
182 | }
183 |
184 | puts care_bear_summary(hug_calendar_1) == care_bear_counts_1
185 | puts care_bear_summary(hug_calendar_2) == care_bear_counts_2
186 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/README.md:
--------------------------------------------------------------------------------
1 | ## Solutions
2 |
3 | You should only be reading these solutions if:
4 | * You have already finished the problem
5 | * You are very very stuck and can't wait until class starts
6 |
7 | ## Video Solutions
8 | You may find these [video solutions][video solutions] helpful. Here, we demo solutions to problem sets 1 & 2. **Note:** the solutions to part 1 exercises are deprecated.
9 |
10 | [video solutions]: https://www.youtube.com/playlist?list=PL7ZJSWk1ZKlur3YGIgKScyzjdyf2opR4a
11 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part1/return_solutions.md:
--------------------------------------------------------------------------------
1 | # Return Value Exercise Solutions
2 |
3 |
4 | \#2. Write a method called `#times_three_and_plus_five` that accepts an integer, n, as an argument. Your method should multiply n by 3, and then add 5. For example, `times_three_and_plus_five(4)` should return 17. Do this using an **explicit return**.
5 |
6 | ```ruby
7 | def times_three_and_plus_five(n)
8 | return (n * 3) + 5
9 | end
10 | ```
11 |
12 | \#3. Change your `#times_three_and_plus_five` method to instead use an **implicit** return.
13 |
14 | ```ruby
15 | def times_three_and_plus_five(n)
16 | (n * 3) + 5
17 | end
18 | ```
19 |
20 | \#5. Write a method called `#even_or_odd`, that accepts an integer as an argument. Your method should return the string "even" if the integer is even, and return "odd" if the integer is odd. Follow the pattern used in the `#is_billy?` method from the reading. Also, use the `#even?` and `#odd?` methods we explored in the previous step.
21 |
22 | ```ruby
23 | def even_or_odd(n)
24 | if n.even?
25 | return "even"
26 | end
27 | "odd"
28 | end
29 | ```
30 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part1/scripts_and_methods_solutions.md:
--------------------------------------------------------------------------------
1 | # Scripts and Methods Solutions
2 |
3 | \#2. Write a method `#print_thrice`, that accepts a string as an argument. It should `puts` that string 3 times.
4 |
5 | ```ruby
6 | def print_thrice(string)
7 | puts(string)
8 | puts(string)
9 | puts(string)
10 | end
11 | ```
12 |
13 | \#3. Write a method, `#print_thrice_thrice`, that also accepts a string as an argument. It should call `print_thrice` with that string three times (for 9 total prints).
14 |
15 | ```ruby
16 | def print_thrice_thrice(string)
17 | print_thrice(string)
18 | print_thrice(string)
19 | print_thrice(string)
20 | end
21 | ```
22 |
23 | \#4. Write a method, `#add_multiply_subtract`, that accepts two integers as arguments. Your method should do the following:
24 | * add the two numbers
25 | * multiply the two numbers
26 | * subtract the sum from the product
27 | * puts the result
28 |
29 | You should create 3 variables (sum, product, difference) to help keep your code neat and clean!
30 |
31 | ```ruby
32 | def add_multiply_subtract(m, n)
33 | sum = m + n
34 | product = m * n
35 | difference = product - sum
36 | puts difference
37 | end
38 | ```
39 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part2/boolean_logic_solutions.md:
--------------------------------------------------------------------------------
1 | # Boolean Logic Readings
2 |
3 | 0. Define a method, `odd_integer?`, that accepts an integer, `n`, as an argument. Your method should return `true` or `false` based on whether `n` is odd or even. Do not use the built in `#odd?` method. Instead, think about how you could use the modulo operator, `%`, to determine whether an integer is odd or even.
4 |
5 | ```ruby
6 | def odd_integer?(n)
7 | n % 2 == 1
8 | end
9 | ```
10 |
11 | 0. Define a method, `before_tarantula?`, that accepts a string as an argument. The method should return `true` if the argument comes before the word "tarantula" alphabetically.
12 |
13 | ```ruby
14 | def before_tarantula?(string)
15 | string < "tarantula"
16 | end
17 | ```
18 |
19 | 0. Fill out the following truth table:
20 |
21 | A | B | !A && B | !(A || B) | (A && B) || !B
22 | -------|-------|------------|---------------------|--------------------|
23 | `true` |`true` | `false` | `false` | `true`
24 | `true` |`false`| `false` | `false` | `true`
25 | `false`|`true` | `true` | `false` | `false`
26 | `false`|`false`| `false` | `true` | `true`
27 |
28 |
29 | 0. Define a method, `#same_type?` that accepts any two objects as arguments. Your method should return true if both objects are the same type (`String`, `Fixnum`, etc.). Otherwise, return false.
30 |
31 | ```ruby
32 | def same_type?(object1, object2)
33 | object1.class == object2.class
34 | end
35 | ```
36 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part2/conditionals_solutions.md:
--------------------------------------------------------------------------------
1 | # Conditionals Solutions
2 |
3 | 0. Write a method `#swap_p`, that accepts a string an argument. If the string argument starts with a "p", then your method should replace it with an underscore, "\_". Otherwise, just return the original string. Assume the string only has lowercase letters.
4 |
5 | ```ruby
6 | def swap_p(string)
7 | if string[0] == "p"
8 | return "_" + string[1..-1]
9 | end
10 |
11 | string
12 | end
13 | ```
14 |
15 | 0. Write a method, `#pos_or_neg?` that accepts an integer, `n`, as an argument. Your method should return `"positive"` if n > 0, `"negative"`, if n < 0, and `"your number is rather passive"` otherwise.
16 |
17 | ```ruby
18 | def pos_or_neg?(n)
19 | if n > 0
20 | return "positive"
21 | elsif n < 0
22 | return "negative"
23 | end
24 |
25 | "your number is rather passive"
26 | end
27 | ```
28 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part2/loops_solutions.md:
--------------------------------------------------------------------------------
1 | # Loops Solutions
2 |
3 | 0. Write a `while` loop that prints out all the numbers from 1 to 25 inclusive.
4 |
5 | ```ruby
6 | n = 1
7 |
8 | while n <= 25
9 | puts n
10 | n += 1
11 | end
12 | ```
13 |
14 | 0. Write an `until` loop that prints out all of the numbers that are less than 20 and are not divisible by 6.
15 |
16 | ```ruby
17 | n = 1
18 |
19 | until n == 20
20 | puts n unless n % 6 == 0
21 | n += 1
22 | end
23 | ```
24 |
25 | 0. Write a method, `#sum_up_to(number)` that, given a number, sums all the numbers from 1 to that number, inclusive. It should use a `while` loop internally.
26 |
27 | ```ruby
28 | def sum_up_to(number)
29 | n = 1
30 | sum = 0
31 |
32 | while n <= number
33 | sum += n
34 | n += 1
35 | end
36 |
37 | sum
38 | end
39 | ```
40 |
41 | 0. Write a method, `print_factors(number)` that prints all of the factors of a number, including 1 and the number itself. You'll need to use a while loop, a conditional statement, and the modulo operator!
42 |
43 | ```ruby
44 | def print_factors(number)
45 | possible_factor = 1
46 |
47 | while possible_factor <= number
48 | puts possible_factor if number % possible_factor == 0
49 | possible_factor += 1
50 | end
51 | end
52 | ```
53 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part2/number_solutions.md:
--------------------------------------------------------------------------------
1 | # Number Solutions
2 |
3 | 0. Write a method, `#division_plus_remainder`, that accepts two integers, `big_int` and `small_int`, as arguments. Your method should find the number of times `small_int` completely divides into `big_int`, and then add the remainder that's left over. For example, `divisor_plus_remainder(7, 2)` should equal `4`.
4 |
5 | ```ruby
6 | def division_plus_remainder(big_int, small_int)
7 | quotient = big_int / small_int
8 | remainder = big_int % small_int
9 | quotient + remainder
10 | end
11 | ```
12 |
13 | 0. Write a method `#divide_two_places`, that accepts two floats, `big_float` and `small_float`, as arguments. Your method should divide `big_float` / `small_float` and round to two decimal places.
14 |
15 | ```ruby
16 | def divide_two_places(big_float, small_float)
17 | quotient = big_float / small_float
18 | quotient.round(2)
19 | end
20 | ```
21 |
22 | 0. Test your order of operations knowledge! You don't need to write any code here! Use `pry` to test your answer. Solve this expression:
23 |
24 | ```
25 | 5 + 3 * 3 ** 2 + ( 9 - 2 ** 2 ) ** 2
26 | ```
27 |
28 | **Answer: 57**
29 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part2/string_solutions.md:
--------------------------------------------------------------------------------
1 | # String Solutions
2 |
3 | \#3. Write a method `#capitalize` that accepts a string as an argument and capitalizes the first letter of the string. Note: Do not use the built in method `String#capitalize`
4 |
5 | ```ruby
6 | def capitalize(string)
7 | first_letter = string[0]
8 | end_of_string = string[1..-1]
9 |
10 | first_letter.upcase + end_of_string
11 | end
12 | ```
13 |
14 | \#4. Write a method, `#shout_then_whisper` that takes in two strings, and shouts the first part, then whispers the second part. The output should look like this:
15 |
16 | ```ruby
17 | shout_then_whisper("Hello", "McDouglas") == "HELLO!! ... mcdouglas"
18 | ```
19 |
20 | ```ruby
21 | def shout_then_whisper(string1, string2)
22 | shout = string1.upcase
23 | whisper = string2.downcase
24 |
25 | "#{shout}!! ... #{whisper}"
26 | end
27 | ```
28 |
29 | \#5. Write a method, `#how_long?` that accepts a string as an argument. Your method should return a new string in the format:
30 |
31 | ```ruby
32 | how_long?("I am a sentence :)") == "Your string is 18 characters long"
33 | ```
34 |
35 | ```ruby
36 | def how_long?(string)
37 | character_count = string.length
38 | "Your string is #{character_count} characters long"
39 | end
40 | ```
41 |
42 | \#6. Write a method, `#hyphenify` that accepts a string as an argument. Your method should remove all the spaces and replace them with hyphens.
43 |
44 | ```ruby
45 | def hyphenify(string)
46 | string.gsub(" ", "-")
47 | end
48 | ```
49 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part2/type_conversion_solutions.md:
--------------------------------------------------------------------------------
1 | # Type Conversion Solutions
2 |
3 | 0. Define a method, `#two_digit_sum`, that accepts a two-digit integer as an argument. Your method should return the sum of the two digits.
4 |
5 | ```ruby
6 | def two_digit_sum(int)
7 | first_digit = int.to_s[0]
8 | second_digit = int.to_s[1]
9 |
10 | first_digit.to_i + second_digit.to_i
11 | end
12 | ```
13 |
14 | 0. Define a method, `#orders_of_magnitude`, that accepts an integer as an argument. Your method should return the number of digits in the integer.
15 |
16 | ```ruby
17 | def orders_of_magnitude(int)
18 | int.to_s.length
19 | end
20 | ```
21 |
22 | 0. Define a method, `#accurate_division`, that accepts two **integers** as arguments. Your method should return the quotient of the two integers (rounding to 3 decimal places).
23 |
24 | ```ruby
25 | def accurate_division(int1, int2)
26 | quotient = int1.to_f / int2
27 | quotient.round(3)
28 | end
29 | ```
30 |
31 | 0. Define a method, `#two_digit_format`, that accepts an integer, `n`, and returns a string version of that integer. If the integer has a single digit, you should append a 0. You can **assume that n will always be two or fewer digits.** Hint: try creating a string using `'0' + n.to_s`, then use `#slice` to get the last two digits!
32 |
33 | ```ruby
34 | def two_digit_format(n)
35 | string_number = '0' + n.to_s
36 | string_number[-2, 2]
37 | end
38 | ```
39 |
40 | 0. Define a method, `#time_string` that accepts 3 arguments: hours, minutes, and seconds (all integers). Convert these integers into one contiguous time string that has the following format:
41 |
42 | ```ruby
43 | "hh:mm:ss"
44 | ```
45 |
46 | Use the method, `#two_digit_format`, to help you do this!
47 |
48 | ```ruby
49 | def time_string(hours, minutes, seconds)
50 | hour_string = two_digit_format(hours)
51 | minute_string = two_digit_format(minutes)
52 | second_string = two_digit_format(seconds)
53 |
54 | "#{hour_string}:#{minute_string}:#{second_string}"
55 | end
56 | ```
57 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part3/array_solutions.md:
--------------------------------------------------------------------------------
1 | # Array Solutions
2 |
3 | 0. Write a method, `#first_n_evens(n)` that returns an array of the first n even numbers, starting from 0.
4 |
5 | ```ruby
6 | def first_n_evens(n)
7 | result = []
8 | even_num = 0
9 |
10 | while result.length < n
11 | result << even_num
12 | even_num += 2
13 | end
14 |
15 | return result
16 | end
17 | ```
18 |
19 | 0. Write a method, `#reverse(array)` that reverses an array in-place; don't create any new arrays, and don't use `array.reverse`!
20 |
21 | ```ruby
22 | def reverse(array)
23 | left_idx = 0
24 | right_idx = array.length - 1
25 |
26 | until left_idx >= right_idx
27 | array[left_idx], array[right_idx] = array[right_idx], array[left_idx]
28 |
29 | left_idx += 1
30 | right_idx -= 1
31 | end
32 |
33 | array
34 | end
35 | ```
36 |
37 | 0. Write a method, `#rotate(array, shift)` that, given an array and an integer (shift), rotates the array in-place by the shift amount.
38 | * Hint: some combination of `#shift`/`#push` or `#unshift`/`#pop` should come in handy here.
39 |
40 | ```ruby
41 | def rotate(array, shift)
42 | shift_count = 0
43 |
44 | while shift_count < shift
45 | array.unshift(array.pop)
46 | shift_count += 1
47 | end
48 |
49 | array
50 | end
51 | ```
52 |
53 | 0. Write a method, `#all_uniqs(array1, array2)` that, given two arrays, produces a new array of only elements unique to `array1` and elements unique to `array2`. Don't worry about the order of the elements in the output array.
54 |
55 | ```ruby
56 | def all_uniqs(array1, array2)
57 | uniques = []
58 |
59 | array1.each do |el|
60 | uniques << el unless array2.include?(el)
61 | end
62 |
63 | array2.each do |el|
64 | uniques << el unless array1.include?(el)
65 | end
66 |
67 | uniques
68 | end
69 | ```
70 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part3/enumerable_solutions.md:
--------------------------------------------------------------------------------
1 | # Enumerable Solutions
2 |
3 | 0. Write a method, `#words_with_a(array)`, that accepts an array of strings as an argument. It should return the number of strings that have an 'a' in them.
4 |
5 | ```ruby
6 | def words_with_a(array)
7 | array.count { |string| string.include?("a") }
8 | end
9 | ```
10 |
11 | 0. Write a method, `#odds(array)`, that accepts an array of integers as an argument. It should return an array containing only the odd numbers.
12 |
13 | ```ruby
14 | def odds(array)
15 | array.select { |n| n.odd? }
16 | end
17 | ```
18 |
19 | 0. Write a method, `#triple(array)`, that accepts an array of integers as an argument. It should return a new array with every element tripled.
20 |
21 | ```ruby
22 | def tripple(array)
23 | array.map { |el| el * 3 }
24 | end
25 | ```
26 |
27 | 0. Write a method, `#odd_value_and_position(array)`, that accepts an array of integers as an argument. It should return a new array of all the elements whose value and position (index) in the original array are odd.
28 |
29 | ```ruby
30 | def odd_value_and_position(array)
31 | array.select.with_index { |el, idx| el.odd? && idx.odd? }
32 | end
33 | ```
34 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part3/hash_solutions.md:
--------------------------------------------------------------------------------
1 | # Hash Solutions
2 |
3 | 0. Write a method, `#word_lengths(string)` that, given a sentence, returns a hash of each of the words and their lengths.
4 |
5 | ```ruby
6 | def word_lengths(string)
7 | result = {}
8 | words = string.split
9 |
10 | words.each do |word|
11 | result[word] = word.length
12 | end
13 |
14 | result
15 | end
16 | ```
17 |
18 | 0. Write a method, `#uniq(array)` that returns an array with only the unique values in the array. (Hint: use a hash!) Don't use the `Array#uniq` method here.
19 |
20 | ```ruby
21 | def uniq(array)
22 | counter = Hash.new(0)
23 |
24 | array.each do |el|
25 | counter[el] += 1
26 | end
27 |
28 | uniques = []
29 | counter.each do |key, val|
30 | uniques << key if val == 1
31 | end
32 |
33 | uniques
34 | end
35 | ```
36 |
37 | 0. Write a method, `#greatest_key_by_val(hash)` that takes in a hash with any keys but only integers for values, and returns the key with the largest value.
38 |
39 | ```ruby
40 | def greatest_key_by_val(hash)
41 | current_best_val = nil
42 | current_best_key = nil
43 |
44 | hash.each do |key, val|
45 | if current_best_val.nil? || val > current_best_val
46 | current_best_val = val
47 | current_best_key = key
48 | end
49 | end
50 |
51 | current_best_key
52 | end
53 | ```
54 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part3/iterator_solutions.md:
--------------------------------------------------------------------------------
1 | # Iterator Solutions
2 |
3 | 0. Define a method, `#print_10`, that prints the numbers 1 - 10. Do this two different ways:
4 | * Using `Range#each`
5 | * Using `#upto`
6 |
7 | ```ruby
8 | def print_10_range
9 | (1..10).each { |n| puts n }
10 | end
11 |
12 | def print_10_upto
13 | 1.upto(10) { |n| puts n }
14 | end
15 | ```
16 |
17 | 0. Define a method, `#string_squared(string)`, that prints the string the number of times that it has characters.
18 |
19 | ```ruby
20 | def string_squared(string)
21 | string.length.times do
22 | puts string
23 | end
24 | end
25 | ```
26 |
27 | 0. Define a method, `#collect_thirds(array)`, that iterates through an array and collects every third element. Do this in two different ways:
28 | * `array.select.with_index`
29 | * `#step`
30 |
31 | ```ruby
32 | def collect_thirds_select(array)
33 | array.select.with_index do |_, idx|
34 | (idx + 1) % 3 == 0
35 | end
36 | end
37 |
38 | def collect_thirds_step(array)
39 | thirds = []
40 | last_idx = array.length - 1
41 |
42 | 2.step(last_idx, 3) do |idx|
43 | thirds << array[idx]
44 | end
45 |
46 | thirds
47 | end
48 | ```
49 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/part3/range_solutions.md:
--------------------------------------------------------------------------------
1 | # Range Exercises
2 |
3 | 0. Define a method, `#alphabet_slice(range)`, that accepts a range of integers as an argument. Use the range to slice a string of characters representing the alphabet. Remember, the `String#slice` method can accept a range as an argument!
4 |
5 | ```ruby
6 | def alphabet_slice(range)
7 | alphabet = ("a".."z").to_a.join
8 | alphabet[range]
9 | end
10 | ```
11 |
12 | 0. Define a method, `#alphabet_without(array)`, that accepts an array of letters as an argument. Your method should return an array of all the characters in the alphabet, but exclude the characters in the input array.
13 |
14 | ```ruby
15 | def alphabet_without(array)
16 | alphabet = ("a".."z").to_a
17 | array.each { |letter| alphabet.delete(letter) }
18 | alphabet
19 | end
20 | ```
21 |
22 | 0. Define a method, `#evens_below(integer)`, that accepts an integer as an argument. Your method should return an array of all the even numbers (starting at 0) that are less than (but not equal to) the integer argument.
23 |
24 | ```ruby
25 | def evens_below(integer)
26 | evens = []
27 | (0...integer).each do |n|
28 | evens << n if n.even?
29 | end
30 |
31 | evens
32 | end
33 | ```
34 |
35 | 0. Define a method, `#to_range(array)` that, given an array of integers, constructs a range covering the span of integers in the array argument.
36 |
37 | ```ruby
38 | def to_range(array)
39 | min = array.min
40 | max = array.max
41 | (min..max)
42 | end
43 | ```
44 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/practice_assessment_3_solution.rb:
--------------------------------------------------------------------------------
1 |
2 | # In All Strings
3 | # ------------------------------------------------------------------------------
4 | # Check if a short_string is a substring of ALL of the long_strings
5 |
6 | def in_all_strings?(long_strings, short_string)
7 | long_strings.all? do |long_string|
8 | long_string.include?(short_string)
9 | end
10 | end
11 |
12 | puts "---------In All Strings-------"
13 | puts in_all_strings?(["thisisaverylongstring", "thisisanotherverylongstring"], "sisa") == true
14 | puts in_all_strings?(["thisisaverylongstring", "thisisanotherverylongstring"], "isan") == false
15 | puts in_all_strings?(["gandalf", "aragorn", "sauron"], "sam") == false
16 | puts in_all_strings?(["axe", "ajax", "axl rose"], "ax") == true
17 |
18 | # Biodiversity
19 | # ------------------------------------------------------------------------------
20 | # Given an array of specimens, return the biodiversity index, which is defined
21 | # by the following formula: number_of_species^2 times the smallest_population_size
22 | # divided by the largest_population_size.
23 |
24 | # In code, biodiversity = number_of_species**2 * smallest_population_size / largest_population_size
25 | #
26 | # ------------------------------------------------------------------------------
27 |
28 | def biodiversity_index(specimens)
29 | population_size = Hash.new(0)
30 | specimens.each do |specimen|
31 | population_size[specimen] += 1
32 | end
33 |
34 | number_of_species = specimens.uniq.length
35 | smallest_population_size = population_size.values.min
36 | largest_population_size = population_size.values.max
37 |
38 | number_of_species ** 2 * smallest_population_size / largest_population_size
39 | end
40 |
41 |
42 | puts "------Biodiversity------"
43 | puts biodiversity_index(["cat"]) == 1
44 | puts biodiversity_index(["cat", "cat", "cat"]) == 1
45 | puts biodiversity_index(["cat", "cat", "dog"]) == 2
46 | puts biodiversity_index(["cat", "fly", "dog"]) == 9
47 | puts biodiversity_index(["cat", "fly", "dog", "dog", "cat", "cat"]) == 3
48 |
49 | # For F's Sake
50 | # ------------------------------------------------------------------------------
51 | # Given a string, return the word that has the letter "f" closest to
52 | # the end of it. If there's a tie, return the earlier word. Ignore punctuation.
53 | #
54 | # If there's no f, return an empty string.
55 | # ------------------------------------------------------------------------------
56 |
57 |
58 | def for_fs_sake(string)
59 | words = string.split
60 | min_f_distance = nil
61 | min_f_word = ""
62 |
63 | words.each do |word|
64 | current_f_distance = find_f_distance(word)
65 | if current_f_distance && (min_f_distance.nil? || current_f_distance < min_f_distance)
66 | min_f_distance = current_f_distance
67 | min_f_word = word
68 | end
69 | end
70 |
71 | min_f_word
72 | end
73 |
74 | def find_f_distance(word)
75 | index_at_end = word.length - 1
76 | index = index_at_end
77 | punctuation = ["!", ".", "?", "'", ","]
78 | punctuation_count = 0
79 |
80 | until index < 0
81 | letter = word[index]
82 | punctuation_count += 1 if punctuation.include?(letter)
83 |
84 | break if letter == "f" || letter == "F"
85 | index -= 1
86 | end
87 |
88 | if index >= 0
89 | index_at_end - index - punctuation_count
90 | else
91 | nil
92 | end
93 | end
94 |
95 | puts "------For F's Sake------"
96 | puts for_fs_sake("puff daddy") == "puff"
97 | puts for_fs_sake("I got a lot of problems with you people! And now you're gonna hear about it!") == "of"
98 | puts for_fs_sake("fat friars fly fish") == "fat"
99 | puts for_fs_sake("the French call him David Plouffe") == "Plouffe"
100 | puts for_fs_sake("pikachu! i choose you!") == ""
101 |
102 |
103 | # Censor
104 | # ------------------------------------------------------------------------------
105 | # Write a function censor(sentence, curse_words) that censors the words given.
106 | # Replace the vowels in the curse word with "*".
107 |
108 | def censor(sentence, curse_words)
109 | words = sentence.split(" ")
110 |
111 | asterisked_words = words.map do |word|
112 | if curse_words.include?(word.downcase)
113 | asterisked_word(word)
114 | else
115 | word
116 | end
117 | end
118 |
119 | asterisked_words.join(" ")
120 | end
121 |
122 | def asterisked_word(word)
123 | asterisked_array = word.split("")
124 | asterisked_array.map! do |letter|
125 | if ["a", "e", "i", "o", "u"].include?(letter)
126 | "*"
127 | else
128 | letter
129 | end
130 | end
131 |
132 | asterisked_array.join("")
133 | end
134 |
135 | puts "------Censor------"
136 | puts censor("Darn you Harold you son of a gun", ["darn", "gun"]) == "D*rn you Harold you son of a g*n"
137 | puts censor("Schnikeys I don't give a diddly squat", ["schnikeys", "diddly", "squat"]) == "Schn*k*ys I don't give a d*ddly sq**t"
138 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/practice_assessment_5_solution.rb:
--------------------------------------------------------------------------------
1 | # Write a functinon that takes a string and
2 | # returns a hash in which each key is a character in the string
3 | # pointing to an array indicating the index that the character
4 | # first occurs and last occurs.
5 | #
6 | # If the character occurs only once, the array should hold a single index
7 | #
8 | # Example:
9 | #
10 | # str = "banana"
11 | # return {"b" => [0], "a" => [1, 5], "n" => [2, 4]}
12 | # "b" occurs once at index 0
13 | # "a" occurs three times, but the first is at index 1 and the last is at index 5
14 | # "n" occurs three times, but the first is at index 2 and the last is at index 4
15 |
16 | def first_last_indices(str)
17 | answer = Hash.new
18 | str.each_char do |c|
19 | first_index = first_occurence(str, c)
20 | last_index = last_occurence(str, c)
21 | answer[c] = [first_index]
22 | answer[c] << last_index if last_index != first_index
23 | end
24 | answer
25 | end
26 |
27 | def first_occurence(str, c)
28 | str.index(c)
29 | end
30 |
31 | def last_occurence(str, c)
32 | str.length - str.reverse.index(c) - 1
33 | end
34 |
35 |
36 | ##################
37 |
38 |
39 | # Anagrams are two words with the exact same letters.
40 | # Write a method that take two strings and returns true if they are anagrams
41 | # and false if they are not.
42 |
43 | def anagrams?(str1, str2)
44 | characters = str1.split("")
45 | characters.all? do |c|
46 | str1.count(c) == str2.count(c)
47 | end
48 | end
49 |
50 |
51 | ##################
52 |
53 |
54 | # An abundant number is a number that is less than the sum of its divisors,
55 | # not including itself.
56 |
57 | # Ex. 12's divisors are 1, 2, 3, 4, 6, which sum to 16
58 | # 16 > 12 so 12 is an abundant number.
59 |
60 | # Write a method that takes a number and returns true if the number is abundant
61 | # otherwise, return false.
62 |
63 |
64 | def abundant?(num)
65 | sum(divisors(num)) > num
66 | end
67 |
68 | def divisors(num)
69 | (1...num).select { |n| num % n == 0 }
70 | end
71 |
72 | def sum(array)
73 | sum = 0
74 | array.each { |num| sum += num }
75 | sum
76 | end
77 |
78 |
79 | ##################
80 |
81 |
82 | # Save the Prisoner!
83 | #
84 | # A jail has n prisoners, and each prisoner has a unique ID number, ranging
85 | # from 1 to n. There are m sweets that must be distributed to the prisoners.
86 | # The jailer decides the fairest way to do this is by sitting the prisoners
87 | # down in a circle (ordered by ascending ID number), and then, starting with
88 | # some random prisoner, distribute one candy at a time to each sequentially
89 | # numbered prisoner until all candies are distributed. For example, if the
90 | # jailer picks prisoner ID=2, then his distribution order would be
91 | # (2,3,4,5,...,n-1,n,1,2,3,4,...) until all m sweets are distributed.
92 | #
93 | # But wait — there's a catch — the very last sweet is poisoned! Can you find and
94 | # print the ID number of the last prisoner to receive a sweet so they can be warned?
95 | #
96 | # n => Number of Prisoners
97 | # m => Number of Sweets
98 | # ID => Starting ID
99 | # save_the_prisoner(N,M,ID)
100 |
101 |
102 | def save_the_prisoner(n, m, id)
103 | prisoners = n
104 | sweets = m
105 |
106 | prisonerToWarn = (id + sweets - 1) % prisoners
107 | prisonerToWarn == 0 ? prisoners : prisonerToWarn
108 | end
109 |
110 |
111 | ##################
112 |
113 | # Write a method, #build_power_plants, that will take in two arguments. The
114 | # first of which is the number of cities that need to be powered. The cities
115 | # are arranged in a line and are equidistant from eachother.
116 | # The second argument will be the range of each powerplant (the number of cities
117 | # in each direction a plant can reach and power). So, a range of 1 means that each
118 | # plant can only reach the cities on either side of it. A range of 0 means that the
119 | # plants can only power the city they are built in.
120 | #
121 | # Your method should dictate which cities' powerplants need to be built such
122 | # that all of the cities have power, and the least number of cities have powerplants
123 | # as possible.
124 | #
125 | # Output an array of the cities, with 0 representing a city without a powerplant
126 | # and 1 representing a city with one.
127 | #
128 | # For example:
129 | # build_power_plants(3, 2) means that there are three cities, and each powerplant
130 | # will have a range of two cities. Therefore your method should output the array
131 | # { 0, 1, 0 ]
132 |
133 |
134 | def build_power_plants(number_of_cities, plant_range)
135 | return Array.new(number_of_cities, 1) if plant_range < 1 || number_of_cities < 2
136 | cities = Array.new(number_of_cities, 0)
137 |
138 | curr_idx = plant_range
139 | until curr_idx > cities.length
140 | cities[curr_idx] = 1
141 | if (curr_idx + plant_range * 2 + 1) > (number_of_cities - 1) && cities[(-1 - plant_range)..-1].all? {|el| el == 0}
142 | curr_idx = number_of_cities - 1
143 | else
144 | curr_idx += plant_range * 2 + 1
145 | end
146 | end
147 |
148 | cities
149 | end
150 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/problem_set_1_solution.rb:
--------------------------------------------------------------------------------
1 | def array_times_two(array)
2 | times_two_array = []
3 | array.each do |element|
4 | times_two_array << element * 2
5 | end
6 | times_two_array
7 | end
8 |
9 | def array_times_two!(array)
10 | array.each_with_index do |element, index|
11 | array[index] = array[index] * 2
12 | end
13 | array
14 | end
15 |
16 | def uniq(array)
17 | uniq_array = []
18 | array.each do |element|
19 | uniq_array << element unless uniq_array.include?(element)
20 | end
21 | uniq_array
22 | end
23 |
24 | def slippery_numbers(n)
25 | slippery_array = []
26 | number = 1
27 | until slippery_array.length == n
28 | slippery_array << number if is_slippery?(number)
29 | number += 1
30 | end
31 | slippery_array
32 | end
33 |
34 | def is_slippery?(number)
35 | (number % 3 == 0 || number % 5 == 0) && number % 15 != 0
36 | end
37 |
38 | def two_sum_to_zero?(array)
39 | array.each_with_index do |element, index1|
40 | array.each_with_index do |element2, index2|
41 | return true if element + element2 == 0 && index1 != index2
42 | end
43 | end
44 | false
45 | end
46 |
47 | def is_magic_number?(number)
48 | string_digits = number.to_s.chars
49 |
50 | sum = 0
51 | string_digits.each do |digit_character|
52 | sum += digit_character.to_i
53 | end
54 | sum == 7
55 | end
56 |
57 | def magic_numbers(count)
58 | magic_numbers_array = []
59 | number = 1
60 | until magic_numbers_array.size == count
61 | magic_numbers_array << number if is_magic_number?(number)
62 | number += 1
63 | end
64 | magic_numbers_array
65 | end
66 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/problem_set_5_solution.rb:
--------------------------------------------------------------------------------
1 | # chunk an array into nested arrays of length n
2 | def chunk(array, n)
3 | chunk = []
4 | results = []
5 | array.each do |element|
6 | if chunk.length == n
7 | results << chunk
8 | chunk = []
9 | end
10 | chunk << element
11 | end
12 |
13 | results << chunk
14 | end
15 |
16 | puts "---------chunk-------"
17 | puts chunk([1, 8, 9, 4, "hey", "there"], 2) == [[1, 8], [9, 4], ["hey", "there"]]
18 | puts chunk([10, 9, 8, 7, 6, 5, 4], 3) == [[10, 9, 8], [7, 6, 5], [4]]
19 |
20 | # Translate into pig-latin! First consonants go to the end of a word. End with "ay"
21 | def pig_latin(sentence)
22 | sentence.split.map { |word| latinify(word) }.join(" ")
23 | end
24 |
25 | def latinify(word)
26 | vowels = "aeiou".chars
27 | until vowels.include?(word[0].downcase)
28 | word = word[1..-1] + word[0]
29 | end
30 | word + "ay"
31 | end
32 |
33 | puts "---------pig latin-------"
34 | puts pig_latin("i speak pig latin") == "iay eakspay igpay atinlay"
35 | puts pig_latin("throw me an aardvark") == "owthray emay anay aardvarkay"
36 |
37 | # Remove the nth letter of the string
38 | def remove_nth_letter(string, n)
39 | string[0...n] + string[n + 1..-1]
40 | end
41 |
42 | puts "---------remove nth letter-------"
43 | puts remove_nth_letter("helloworld", 5) == "helloorld"
44 | puts remove_nth_letter("helloworld", -3) == "hellowold"
45 |
46 | # Boolean function: check if shortstr is a substring of longstr
47 | def substring?(long_string, short_string)
48 | length = short_string.length
49 | letters = long_string.chars
50 | letters.each_with_index do |letter, index|
51 | return true if letters[index...index + length].join == short_string
52 | end
53 | false
54 | end
55 |
56 | puts "---------substring-------"
57 | puts substring?("thisisaverylongstring", "sisa") == true
58 | puts substring?("thisisaverylongstring", "ting") == false
59 | puts substring?("whatifikeptontypingforever", "ik") == true
60 |
61 | # count the number of times that two adjacent numbers in an array add up to n.
62 | # You cannot reuse a number. So count_adjacent_sums([1, 5, 1], 6) => 1
63 |
64 | def count_adjacent_sums(array, n)
65 | count = 0
66 | counted_numbers = []
67 |
68 | (1...array.length).each do |index|
69 | number = array[index]
70 | number_before = array[index - 1]
71 | sum = number + number_before
72 | havent_counted_these_numbers_yet = !counted_numbers.include?(number) && !counted_numbers.include?(number_before)
73 |
74 | if sum == n && havent_counted_these_numbers_yet
75 | counted_numbers.push(number)
76 | counted_numbers.push(number_before)
77 | count += 1
78 | end
79 | end
80 |
81 | count
82 | end
83 |
84 | puts "---------count adjacent sums-------"
85 | puts count_adjacent_sums([7, 2, 4, 6, 8, 10], 7) == 0
86 | puts count_adjacent_sums([6, 7, 11, 2, 5, 10, 3], 13) == 3
87 | puts count_adjacent_sums([1, 9, 1, 8, 2, 10], 10) == 2
88 |
89 | # update the older inventory with the newer inventory. Add any new items to the
90 | # hash and replace the values for items that already exist.
91 |
92 | def inventory_hash(older, newer)
93 | newer.each do |key, value|
94 | older[key] = value
95 | end
96 | older
97 | end
98 |
99 | puts "---------inventory hash-------"
100 | march = {rubies: 10, emeralds: 14, diamonds: 2}
101 | april = {emeralds: 27, moonstones: 5}
102 | puts inventory_hash(march, april) == {rubies: 10, emeralds: 27, diamonds: 2, moonstones: 5}
103 |
104 | # Now, alphabetical order matters in your inventory. Insert new inventory items into
105 | # your array in the appropriate place
106 | def inventory_array(older, newer)
107 | newer.each do |item|
108 | name, quantity = item
109 | older.reject! { |olditem| olditem[0] == name }
110 | older << item
111 | end
112 |
113 | older.sort { |item_a, item_b| item_a[0] <=> item_b[0] }
114 | end
115 |
116 | puts "---------inventory array-------"
117 | march_array = [['diamonds', 2], ['emeralds', 14], ['rubies', 10]]
118 | april_array = [['emeralds', 27], ['moonstones', 5]]
119 | puts inventory_array(march_array, april_array) == [['diamonds', 2], ['emeralds', 27], ['moonstones', 5], ['rubies', 10]]
120 |
--------------------------------------------------------------------------------
/jumpstart-files/solutions/problem_set_6_solution.rb:
--------------------------------------------------------------------------------
1 |
2 | #write a function that takes a number n and returns the highest prime factor of
3 | #that number. Hint: Helper function.
4 |
5 | def is_prime?(number)
6 | return false if number < 2
7 | (2...number).to_a.none?{ |integer| number % integer == 0 }
8 | end
9 |
10 | def highest_prime_factor(n)
11 | number = n - 1
12 | while number > 0
13 | return number if n % number == 0 && is_prime?(number)
14 | number -= 1
15 | end
16 | end
17 |
18 | puts "\nHighest prime factor:\n" + "*" * 15 + "\n"
19 | puts highest_prime_factor(4) == 2
20 | puts highest_prime_factor(6) == 3
21 | puts highest_prime_factor(28) == 7
22 | puts highest_prime_factor(69842) == 743
23 |
24 | #write a boolean function zero_sum? that takes an array of integers and returns
25 | #true if 2 elements in the array sum to zero.
26 |
27 | def zero_sum?(array)
28 | (0...array.length).each do |index1|
29 | (index1 + 1...array.length).each do |index2|
30 | return true if array[index1] + array[index2] == 0
31 | end
32 | end
33 | false
34 | end
35 |
36 | puts "\nZero Sum:\n" + "*" * 15 + "\n"
37 | puts zero_sum?([1, -1]) == true
38 | puts zero_sum?([1,1,0,2,1]) == false
39 | puts zero_sum?([1,1,0,2,1,0]) == true
40 | puts zero_sum?([2,3,4,-3,1]) == true
41 |
42 |
43 |
44 | #write a function missing_letters that take in a phrase and returns an array of
45 | #letters in alphabetical order that are not contained in that phrase
46 |
47 | def missing_letters(phrase)
48 | counts = Hash.new(0)
49 | phrase.chars.each do |character|
50 | counts[character] += 1
51 | end
52 | missing = []
53 | ('a'..'z').each do |letter|
54 | missing << letter if counts[letter] == 0
55 | end
56 | missing
57 | end
58 |
59 | puts "\nMissing Letters\n" + "*" * 15 + "\n"
60 | puts missing_letters("abcdefghijklmnopqrstuvwxyz") == []
61 | puts missing_letters("abcdefghiklmnopqrstuvwxyz") == ["j"]
62 | puts missing_letters("abcdefghiklmnopstuvwxyz") == ["j", "q", "r"]
63 | puts missing_letters("we the people of the united states in order to form a more perfect union") == "bgjkqvxyz".split("")
64 |
65 |
66 | #Write a function count_odds that takes an array of numbers and returns the count
67 | #of odd numbers in that array
68 | def count_odds(numbers)
69 | numbers.count { |number| number % 2 == 1 }
70 | end
71 |
72 | puts "\nCount Odds\n" + "*" * 15 + "\n"
73 |
74 | puts count_odds([]) == 0
75 | puts count_odds([2]) == 0
76 | puts count_odds([1,2]) == 1
77 | puts count_odds([1,2,3,4,5,6]) == 3
78 | puts count_odds([123,42,23,71,0]) == 3
79 |
80 | #Write a function delete_first_letter that takes a phrase and a letter to hide and removes
81 | #the first instances of that letter
82 | def delete_first_letter(phrase, letter)
83 | (0...phrase.length).each do |index|
84 | if phrase[index] == letter
85 | phrase[index] = ""
86 | break
87 | end
88 | end
89 | phrase
90 | end
91 |
92 | puts "\nHide Letter\n" + "*" * 15 + "\n"
93 | puts delete_first_letter("abc", "d") == "abc"
94 | puts delete_first_letter("abc", "b") == "ac"
95 | puts delete_first_letter("hello, my name is Tevy", "e") == "hllo, my name is Tevy"
96 |
--------------------------------------------------------------------------------
/jumpstart-files/terminal-commands.txt:
--------------------------------------------------------------------------------
1 |
2 |
3 | $ pwd (Print Working Directory) -> Tells you where you are.
4 | $ cd (Change Directory)
5 | $ cd folder_name -> navigate into that folder
6 | $ cd -> navigate to home directory
7 | $ cd .. -> navigate one folder up.
8 | $ ls -> list all files and folders in the current directory
9 | $ open . -> open navigator window to the current directory
10 | $ mkdir -> create a new folder in the current directory
11 | $ touch filename -> create a new file in the current directory
12 | $ atom . -> open atom in the current directory (if this isn't working open atom click the atom toolbar and install shell commands)
13 | $ ruby scriptname.rb -> run your ruby script in the terminal (ctrl + c to force quit infinite loop)
14 | $ irb -> open irb in terminal (exit to quit irb)
15 |
--------------------------------------------------------------------------------
/jumpstart-files/video_link.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/appacademy/jump-start/832e90c93dce53c7f4f9bf32ddf9499f66288a6c/jumpstart-files/video_link.png
--------------------------------------------------------------------------------