Please do every exercise. Prioritize doing the exercises (correctly) over doing the readings. We want you to struggle with problems and then fill in your knowledge.
Reference the linked material when you get stuck or need help.
It's OK to go "I don't understand the question". That's when you should read the linked material.
-
A computer will always do exactly what you tell it to, no more and no less.
-
There's no bullshitting in programming. Your program either works or it doesn't, and no amount of cleverness or importance will change that.
Readings link with examples (in case you get stuck or need help):
-
Practice using
cd
andls
to explore your computer's directory structure. Entercd ..
until you go all the way down to the root/
. Look at the various directories that make up a Linux and OSX computer system. You'll be fine as long as you don't enter therm
command. -
From your desktop or workspace, make a sample folder (
mkdir sample_folder_name
), andcd
into it.touch
andcat
are two additional commands that we use a fair amount. Look them up in google and try them out: e.g.,touch my_file_1
,echo "Hello world" > my_file_1
,cat my_file_1
. After you feel you have a basic understanding of what they do look up theman
pages and try out some different options. -
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. -
Run the last three commands you did, without typing them out. Use the up arrow and see what happens.
Readings link with examples (in case you get stuck or need help):
- Pop open IRB.
- Initialize an array and push an element into it. (See picture below.)
As we can see in this example, IRB executes our ruby commands as we type them in line by line. We've successfully initialized our array in a variable and pushed a value into it.
- Map over your array:
- Use underscore to access the last returned value. Save the results of your previous command like so:
- Manipulate a string. Type the following code into IRB and observe the results.
- Type multiple lines of code into IRB before it evaluates. Try typing the following multi-liner into IRB.
- Invoke a method in IRB. Copy and paste this method, which lists the factors of a number in descending order, into IRB, and hit enter.
def factors(num)
return -1 if !num.is_a?(Integer) || num < 1
(1..num).select { |el| num % el == 0 }.reverse
end
Now you can invoke the method by typing factors() into IRB and passing it any input you want! Try a couple of sample inputs and see what you get.
- Play around in IRB and test out methods that you are not familiar with. Did any of the results surprise you? IRB is one of the best tools for learning how Ruby works and testing newly written code, so you want to become familiar with it. Most importantly, have fun with it! A programmer's curiosity is the greatest asset he/she has in learning :)
Key concepts:
- The building blocks of all programs
def
andend
- Function arguments
- Watch your indentation!
- Functions can call other functions
Readings link with examples (in case you get stuck or need help):
Codecademy link (complete this first if you haven't yet):
- Write a function called
hello_world
that willputs
the string"Hello world!"
to the screen. - Write a function called
print_thrice(number)
that, given a number, willprint
the number three times. - Write a function called
print_thrice_thrice(number)
that, given a number, will callprint_thrice
with that number three times (for 9 total prints).
Key concepts:
- Mathematical operators
- Integer division (
/
) - Modulo (
%
) +=
,-=
,/=
etc.
Readings link with examples (in case you get stuck or need help):
Codecademy link (complete this first if you haven't yet):
- [Math operators] (https://www.codecademy.com/en/courses/ruby-beginner-en-d1Ylq/0/4?curriculum_id=5059f8619189a5000201fbcb)
- Write a function called
times_three_and_plus_five(number)
that takes a number, multiplies it by 3, and then adds 5. For example,times_three_and_plus_five(4)
should output 17. - Write a function called
even_or_odd(number)
that prints"even"
if the number is even, and prints"odd"
if the number is odd. - Write a function called
division_plus_remainder(big_number, small_number)
that finds the number of timessmall_number
completely divides intobig_number
, and then adds the remainder that's left over. For example,divisor_plus_remainder(7, 2)
should equal4
. - Write a function called
is_factor_of(big_number, small_number)
that returnstrue
ifsmall_number
is a factor ofbig_number
, andfalse
if it is not.
We recommend watching the video below at 2x speed and pausing at new concepts. DO NOT WATCH IT UNTIL AFTER YOU'VE DONE THE EXERCISES.
Key concepts:
- Booleans are
true
andfalse
. That's it. while
loops —while(condition)
, do stuff,end
- Conditionals and control flow
elsif
andelse
- Comparators that produce booleans:
==
,<
,>
,>=
,<=
Readings link with examples (in case you get stuck or need help):
Codecademy links (complete these first if you haven't yet):
- [While loop] (https://www.codecademy.com/courses/ruby-beginner-en-XYcN1/0/1?curriculum_id=5059f8619189a5000201fbcb)
- [Conditionals] (https://www.codecademy.com/courses/ruby-beginner-en-NFCZ7/0/2?curriculum_id=5059f8619189a5000201fbcb)
- [Comparators] (https://www.codecademy.com/en/courses/ruby-beginner-en-NFCZ7/1/1?curriculum_id=5059f8619189a5000201fbcb)
- Write a
while
loop that prints out all the numbers from 1 to 25 inclusive. - Write a function called
print_up_to(number)
that given a number, prints out all the numbers from 1 to that number, inclusive. It should use awhile
loop internally. - Write a function called
print_factors(number)
that prints all of the factors of a number, including 1 and the number itself.- Hint: use your
is_factor_of(big_number, small_number)
function.
- Hint: use your
We recommend watching the video below at 2x speed and pausing at new concepts. DO NOT WATCH IT UNTIL AFTER YOU'VE DONE THE EXERCISES.
Key concepts:
- Hitting
command + /
comments out the current line of code. On Windows, this isctrl + /
. - Hitting
command + /
on an empty line starts a comment. - Selecting multiple lines with
shift + up/down arrow
- Paste the following code into a file and comment out the offending lines using keyboard shortcuts (don't delete them).
def print_hello
adsiofjiodafsj
raiojtdaiocccpp
nothisisntgoodcode
infactitwontrun
3.times do
puts "hello"
end
end
- Using comments, write out in pseudocode an approach to the Cats N' Hats problem that you did day 1. You don't have to write real code off of it.
Key concepts:
return
jumps all the way out of the function
Readings link with examples (in case you get stuck or need help):
- Write a function
largest_factor(number)
that returns the largest factor of a number. Adapt yourprint_factors
method to use awhile
loop starting atnumber - 1
, and return immediately once you find a number that's a factor. - Write a function
is_prime?(number)
that returns a boolean depending on whether the number is prime. A reminder: a prime number only has two factors: 1 and itself.- Once you've written it, see if you can use an early
return
on numbers that you know for sure aren't prime, without having to check every possible factor.
- Once you've written it, see if you can use an early
Key concepts:
while
vs.until
if
vs.unless
- Logical operators (
&&
= and,||
= or,!
= not) - Parenthesization
- Truth tables
Readings link with examples (in case you get stuck or need help):
Codecademy links (complete these first if you haven't yet):
- [Until loop] (https://www.codecademy.com/en/courses/ruby-beginner-en-XYcN1/0/3?curriculum_id=5059f8619189a5000201fbcb)
- [Unless] (https://www.codecademy.com/en/courses/ruby-beginner-en-NFCZ7/3/3?curriculum_id=5059f8619189a5000201fbcb)
- [Logical Operators] (https://www.codecademy.com/en/courses/ruby-beginner-en-NFCZ7/2/1?curriculum_id=5059f8619189a5000201fbcb)
- Complete these truth tables. Write it out on pencil and paper in your notebook.
x | y | z | x && y || z | x && (y || z) |
---|---|---|---|---|
true | true | false | ||
true | false | false | ||
false | true | false | ||
false | false | false |
x | y | z | x || y && z | x || (y && z) |
---|---|---|---|---|
true | true | false | ||
true | false | false | ||
false | true | false | ||
false | false | false |
- Using pen and paper, see if you can figure out the value of these conditionals:
False || (True || False)
(True && False) || False
(True || False) && (False && True)
3 == 5 || 4 % 2 > 2
6 + 4 <= 10 && 5 != 6
(6 != 6 || (1 == 10 % 3)) && (3 + 5 < 6)
- Write an
until
loop that prints out all of the numbers that are less than 20 and are not divisible by 6.
Key concepts:
- String methods
#length
#upcase
#downcase
+
#{}
interpolationstring[index]
to look up the character at an indexstring[index] = letter
to assign a character at an index#split(character_to_split_on)
to split a string into an array of parts
Readings link with examples (in case you get stuck or need help):
Codecademy link (complete this first if you haven't yet):
- Crack open irb and make a sample string. Play around with each of the above methods on the string.
- Write a function
capitalize(string)
that takes in a string, capitalizes the first letter of the string, and returns the string. Note: Do not use the built in methodString#capitalize
- Write a function
shout_then_whisper(string1, string2)
that takes in two strings, and shouts the first part, then whispers the second part. The output should look like this:shout_then_whisper("Hello", "McDouglas") == "HELLO!! ... mcdouglas"
Key Concepts:
- Array methods
#length
or#size
or#count
array[index]
array[index] = val
#first
#last
#include?(value)
#push(value)
to add a value onto the end (also<<
)#pop
to pop off a value from the end#unshift(value)
to stick a value onto the beginning#shift
to shift off a value from the beginning#join(separator)
to join an array of strings, separated by a delimiter
Readings link with examples (in case you get stuck or need help):
Codecademy link (complete this first if you haven't yet):
- Write a function
first_n_evens(n)
that returns an array of the first N many even numbers, starting from 0. - Write a function
reverse(array)
that reverses an array in-place. - Write a function
rotate(array, shift)
that given an array, rotates it in-place by the shift amount. E.g.,rotate([1, 2, 3, 4, 5], 2)
should produce[4, 5, 1, 2, 3]
.rotate([5, 6, 7], -1)
should produce[6, 7, 5]
.- Hint: a combination of
#shift
,#unshift
,#pop
and#push
should come in handy here.
- Hint: a combination of
- Write a function
all_uniqs(array1, array2)
that given two arrays, produces a new array of only elements unique toarray1
and elements unique toarray2
. E.g.,all_uniqs([2, 5, 7], [1, 2, 7])
should return[1, 5]
Key concepts:
- Iteration and blocks (
each
) - Blocks —
do
|argument1, argument2|
, then code stuff, thenend
array.each_with_index
yields|element, index|
string.chars.each_with_index
gives you the same
Readings link with examples (in case you get stuck or need help):
Codecademy link (complete this first if you haven't yet):
- Write a function called
vowels(string)
that returns an array of every vowel in the string.- Using comments, write out an approach in pseudocode first.
- Use the
String#chars
method, which returns an array of characters. Then use aneach
over the chars.
- Write a function called
odd_elements(array)
that prints out every other element (those with odd indices). Useeach_with_index
. - Write a function called
zip_with_indices(array)
that given an array, converts each element into a tuple (array with two items) with its index.- E.g.,
zip_with_indices([3, 20, 8]) == [[3, 0], [20, 1], [8, 2]]
- E.g.,
- Everything returns something in Ruby, including control flow
- Specifically, it often returns
nil
- Chances are, if you're getting a
nil
, you're doing something wrong
Readings link with examples (in case you get stuck or need help):
Key concepts:
p
andputs
p
returns the thing you're printing,puts
returnsnil
Readings link with examples (in case you get stuck or need help):
- In irb, make a sample array.
puts
the array. Thenp
the array. See the difference?
Key concepts:
break
next
Readings link with examples (in case you get stuck or need help):
- Adapt your
odd_elems(array)
function to usenext
to skip over every even-indexed element. - Write a function
first_n_primes(n)
that returns the first N many prime numbers. You'll want to call (use) youris_prime?
function that you wrote earlier. Usebreak
in your function.- For example,
first_n_primes(6)
should return[1, 2, 3, 5, 7, 11]
. - Using comments, write out an approach in pseudocode first.
- For example,
Readings link with examples (in case you get stuck or need help):
- Debugging Reading
- Debugging Lecture
- Watch at up to 2-4x speed. Install the chrome extension
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.
The most effective way to catch your errors is to use the debugger. You had a small taste of this in the pre-curriculum.
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:
In interviews, we ask applicants 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.
For your final assessment, it will be very helpful to know how to use the debugger to fix your errors quickly.
The three most important commands in byebug
:
n
(ornext
): Goes to the next linec
(orcontinue
): Goes to the next place byebug lives in the code- Printing out variables:
multiples
,amount_to_multiple_by
, etc. You type out the variable name and press enter.
Take a look at how you can use byebug while you're running your code.
You can find out more about byebug in the "Byebug Commands" section here. Feel free to play around with new commands.
- Copy and paste the code below into a file.
def company_performance(quarterly_projections, quarterly_profits)
times_we_beat_projection = 0
quarterly_projections.each_with_index do |projection, quarter|
profit = quarterly_profits[quarter]
if profit > projection
times_we_beat_projection += 1
end
end
return times_we_beat_projection
end
apple_projections = [10000, 25000, 8000, 30000]
apple_profits = [5000, 26000, 3000, 50000]
p company_performance(apple_projections, apple_profits)
Run the file. With pen and paper, write down what projection
, profit
and quarter
are at every iteration of the loop.
Play around with n
and c
in byebug and see how you can jump to the next iteration of a loop. Remember to require 'byebug'
at the top if you haven't.
Move byebug inside and outside the if statement and rerun your code. How many times should you hit byebug if it's inside the if statement?
- Copy and paste the code below into a file.
def identify_sheep
sheep = ["Gary", "Adam", "Jacob", "Sam", "Toby", "Billy", "Mandy", "Guinness"]
index = 0
while index <= sheep.length
individual_sheep = sheep[index]
sheep_with_exclamation_point = individual_sheep + "!"
puts "This sheep is named #{sheep_with_exclamation_point}"
index += 1
end
end
identify_sheep
Run the file and see the error that pops up.
Find the top line in the list of error lines (the stacktrace) that looks familiar. Put a debugger (byebug
) on the line right above the line with the error. Print out all the variables that are on the line right below your debugger.
Try to figure out where the error might be coming from and fix it.
Key concepts:
- Be as expressive and specific as possible
- Break down operations into multiple named steps
- Make your code read like English!
- Refactor this tip-calculating function. Try to rename the variables and to make it more expressive, and take advantage of early returns.
def ct(x)
r = 0
if x < 5
r = 1
elsif x < 10
r = x * 0.18
else
r = x * 0.13
end
r
end
- Refactor this function that returns the Nth biggest prime number. Don't start by trying to understand what it's doing—first just start renaming the variables. Then, once you see what it's doing, make it use your own
is_prime?
function to pull out some of the complexity and make it more readable!- Feel free to pause the code using the debugger so that you know which variable is which.
def pr(x)
c = 0
n = 2
while true
i = 2
f = true
while i < n
if n % i == 0
f = false
break
end
i += 1
end
if f
c += 1
if c == x
return n
end
end
n += 1
end
end