Add solutions

This commit is contained in:
Roman Pushkin 2021-08-14 14:21:37 -07:00
parent 7696e6e0b4
commit c507dcfe90
4 changed files with 232 additions and 6 deletions

View file

@ -74,7 +74,7 @@ There are few basic math operators that you can use in Ruby:
* `/` to divide one number by another. For example: `120 / 12`
* `*` to multiply. For example: `2 * 5`
X> ## Exercise
X> ## Exercise
X> Without looking to solution below, try to write a program that calculates the number of milliseconds in one day. How would you approach this problem? How many hours in one day? How many minutes in one hour? How many seconds in one minute? How many milliseconds in one second?
Here is the answer to exercise above:

View file

@ -75,7 +75,7 @@ Copy file to directory:
$ cp file1.txt my_directory
```
X> ## Exercise
X> ## Exercise 1
X> Open up your terminal. List all files (`ls -lah`). Create directory with the name `my_directory`. List all files again, make sure directory exists now. Pick any file from current directory and copy this file to directory you just created. Use file manager to ensure you did the right job.
Copy file one level up:
@ -144,7 +144,7 @@ As you can see, there are many ways of finding files in _current directory_. Cur
$ find / -name '*.log'
X> ## Exercise
X> ## Exercise 2
X> Try to find all log files in your root directory.
Far Manager has built-in dialog (which is quite friendly) for searching files. You can search files by using Alt+F7 combination. By default search file mask is `*.*` (all files). You can also specify a substring you want to be present in these files.
@ -173,7 +173,7 @@ $ cat > file.txt
Then type in your data, for example "`foo bar`", and hit Ctrl+D combination on your keyboard (if it doesn't work, try to Ctrl+D twice). "Ctrl+D" stands for "end of input" in POSIX systems, and your terminal should understand it.
X> ## Exercise
X> ## Exercise 3
X> Create a text file and put your name to this file. Use `ls -lah` command to make sure file has been already created. Use `cat file.txt` to see the contents of the file.
E> ## Be Careful

View file

@ -59,7 +59,7 @@ And so on up to 12th fret (E F F# G G# A A# B C C# D D# E).
Few notes about the listing above. As you may have already noticed, there are comments on some lines. Comments start with pound sign `#` (or "hash"). You can leave as many comments as you want, including comments on the new line. Comments will not affect behavior of your program.
X> ## Exercise
X> ## Exercise 1
X> Finish the program above if you understand music theory. If you don't, create a program to learn 10 foreign words. Try to add some comments to your program so you can understand it better.
Another note is about encoding. Since our program has only characters from A to Z, there is no need to specify any encoding. However, if you want to use Chinese, Russian, etc. characters, you have two options:
@ -73,5 +73,5 @@ It doesn't mean that Windows operating system sucks, not at all. The only reason
So we highly recommend switching to a free operating system like Linux Mint Cinnamon or MacOS.
X> ## Exercise
X> ## Exercise 2
X> If you're running Windows, _make an attempt_ to download and install [VirtualBox](https://www.virtualbox.org/) (free) or VMWare Workstation (requires a license). With this software you'll be able to run Linux inside of your Windows box. You will need to install [Linux Mint Cinnamon 64-bit](https://linuxmint.com/download.php). Try to open up the terminal, install Midnight Commander, and write your first program in Linux. Do not expect immediate success, it might take a couple of tries the first time. If it doesn't work, feel free to come back to this topic later. Important step here is to identify if your computer hardware supports virtualization. If not, you'll probably need to install Linux _instead_ of Windows (recommended) or upgrade your computer.

226
manuscript/solutions.md Normal file
View file

@ -0,0 +1,226 @@
007
===
No need for a solution
009
===
Solution provided as a part of the chapter, see `009.txt`
010
===
Step 1. Run REPL, for example, by typing `irb`
Step 2. Just type `60 * 60 * 24 * 1000` to see the result.
012
===
No need for a solution
014
===
No need for a solution
015
===
### Exercise 1
1) `ls - lah`
2) `mkdir my_directory`
3) `ls -lah`
4) `echo blabla > example.txt` (creating a sample file)
5) `cp example.txt my_directory`
6) `ls -lah my_directory`
### Exercise 2
`find ~ -type f -name '*.log'`
### Exercise 3
1) `touch file.txt` - create empty file or "touch" (change date for) existing, optional step
2) `ls -lah`
3) `echo Walt > file.txt`
4) `cat file.txt`
016
===
No need for a solution
017
===
### Exercise 1
```ruby
puts "Say a note on a 0 fret?" # The right answer is E
gets
puts "Say a note on a 1st fret?" # The right answer is F
gets
puts "Say a note on a 2nd fret?" # The right answer is F#
gets
puts "Say a note on a 3rd fret?" # G
gets
puts "Say a note on a 4th fret?" # G#
gets
puts "Say a note on a 5th fret?" # A
gets
puts "Say a note on a 6th fret?" # A#
gets
puts "Say a note on a 7th fret?" # B
gets
puts "Say a note on a 8th fret?" # C
gets
puts "Say a note on a 9th fret?" # C#
gets
puts "Say a note on a 10th fret?" # D
gets
puts "Say a note on a 11th fret?" # D#
gets
puts "Say a note on a 12th fret?" # E
gets
```
### Exercise 2
No need for a solution
020
===
Skip for now
024
===
### Exercise 1
No need for a solution
### Exercise 2
```ruby
puts "Enter your annual salary (for example, type 50 for $50,000 USD): "
annual_salary = gets.to_i * 1000
daily_salary = annual_salary / 365
puts "Your daily salary is ~$#{daily_salary}"
```
Sample output:
```
$ ruby app.rb
Enter your annual salary (for example, type 50 for $50,000 USD):
200
Your daily salary is ~$547
```
025
===
No need for a solution
027
===
### Exercise 1
```ruby
price = 500_000
30.times do |n|
puts "Year #{n}, left to pay: #{price - n * 16_666}"
end
```
Output:
```
Year 0, left to pay: 500000
Year 1, left to pay: 483334
Year 2, left to pay: 466668
Year 3, left to pay: 450002
Year 4, left to pay: 433336
Year 5, left to pay: 416670
Year 6, left to pay: 400004
Year 7, left to pay: 383338
Year 8, left to pay: 366672
Year 9, left to pay: 350006
Year 10, left to pay: 333340
Year 11, left to pay: 316674
Year 12, left to pay: 300008
Year 13, left to pay: 283342
Year 14, left to pay: 266676
Year 15, left to pay: 250010
Year 16, left to pay: 233344
Year 17, left to pay: 216678
Year 18, left to pay: 200012
Year 19, left to pay: 183346
Year 20, left to pay: 166680
Year 21, left to pay: 150014
Year 22, left to pay: 133348
Year 23, left to pay: 116682
Year 24, left to pay: 100016
Year 25, left to pay: 83350
Year 26, left to pay: 66684
Year 27, left to pay: 50018
Year 28, left to pay: 33352
Year 29, left to pay: 16686
```
### Exercise 2
```ruby
price = 500_000
interest = 0.04
annual_payment = 16_666
30.times do |n|
remaining = price - n * annual_payment
interest_payment = remaining * interest
total = annual_payment + interest_payment
puts "Year #{n}, left to pay: #{remaining}. You are paying #{annual_payment} plus #{interest_payment} of interest (total is #{total})"
end
```
Output:
```
Year 0, left to pay: 500000. You are paying 16666 plus 20000.0 of interest (total is 36666.0)
Year 1, left to pay: 483334. You are paying 16666 plus 19333.36 of interest (total is 35999.36)
Year 2, left to pay: 466668. You are paying 16666 plus 18666.72 of interest (total is 35332.72)
Year 3, left to pay: 450002. You are paying 16666 plus 18000.08 of interest (total is 34666.08)
Year 4, left to pay: 433336. You are paying 16666 plus 17333.44 of interest (total is 33999.44)
Year 5, left to pay: 416670. You are paying 16666 plus 16666.8 of interest (total is 33332.8)
Year 6, left to pay: 400004. You are paying 16666 plus 16000.16 of interest (total is 32666.16)
Year 7, left to pay: 383338. You are paying 16666 plus 15333.52 of interest (total is 31999.52)
Year 8, left to pay: 366672. You are paying 16666 plus 14666.880000000001 of interest (total is 31332.88)
Year 9, left to pay: 350006. You are paying 16666 plus 14000.24 of interest (total is 30666.239999999998)
Year 10, left to pay: 333340. You are paying 16666 plus 13333.6 of interest (total is 29999.6)
Year 11, left to pay: 316674. You are paying 16666 plus 12666.960000000001 of interest (total is 29332.96)
Year 12, left to pay: 300008. You are paying 16666 plus 12000.32 of interest (total is 28666.32)
Year 13, left to pay: 283342. You are paying 16666 plus 11333.68 of interest (total is 27999.68)
Year 14, left to pay: 266676. You are paying 16666 plus 10667.04 of interest (total is 27333.04)
Year 15, left to pay: 250010. You are paying 16666 plus 10000.4 of interest (total is 26666.4)
Year 16, left to pay: 233344. You are paying 16666 plus 9333.76 of interest (total is 25999.760000000002)
Year 17, left to pay: 216678. You are paying 16666 plus 8667.12 of interest (total is 25333.120000000003)
Year 18, left to pay: 200012. You are paying 16666 plus 8000.4800000000005 of interest (total is 24666.48)
Year 19, left to pay: 183346. You are paying 16666 plus 7333.84 of interest (total is 23999.84)
Year 20, left to pay: 166680. You are paying 16666 plus 6667.2 of interest (total is 23333.2)
Year 21, left to pay: 150014. You are paying 16666 plus 6000.56 of interest (total is 22666.56)
Year 22, left to pay: 133348. You are paying 16666 plus 5333.92 of interest (total is 21999.92)
Year 23, left to pay: 116682. You are paying 16666 plus 4667.28 of interest (total is 21333.28)
Year 24, left to pay: 100016. You are paying 16666 plus 4000.64 of interest (total is 20666.64)
Year 25, left to pay: 83350. You are paying 16666 plus 3334.0 of interest (total is 20000.0)
Year 26, left to pay: 66684. You are paying 16666 plus 2667.36 of interest (total is 19333.36)
Year 27, left to pay: 50018. You are paying 16666 plus 2000.72 of interest (total is 18666.72)
Year 28, left to pay: 33352. You are paying 16666 plus 1334.08 of interest (total is 18000.08)
Year 29, left to pay: 16686. You are paying 16666 plus 667.44 of interest (total is 17333.44)
```
028
===
Skip for now