2.3. Exercises#
Each exercise carries the difficulty level indication:
[*] Easy
[**] Moderate
[***] Advanced
The highest difficulty level is meant for students who wish to challenge themselves and/or have previous experience in programming.
Zip files
When you have a directory with contents, such as additional subdirectories and files, it is possible to zip it. The resulting zipped (compressed) file is not a directory anymore, but a file. Importantly, this zipped file takes up less storage space. Additionally, you can treat it as a file, i.e., you can download it in one go, which makes it convenient for sharing.
For some exercises in this book, we want to share more than one file or an entire directory with you. To make downloading easier, we zipped those. In practice, this means you will occassionally be downloading .zip
files.
However, to start treating them as directories, you will always need to unzip (extract) them. You can typically do this by double-clicking and, depending on your computer’s OS, using additional options for extraction. Of course, it’s also possible to unzip files from within the terminal with unzip file.zip
, as long as you have unzip installed (which can be done with this line: sudo apt-get install unzip
).
Data management
It’s good practice to think about managing your directories and folder, i.e., locations and names you give them. While it sounds like a small thing, it saves time in the long run (think in a few months’ or years’ time when you won’t remember where your data is, unless you saved it in a logical place with a logical name). Therefore, when solving exercises from this book, try to keep your data and exercise solutions organized.
([*] Your computer)
Open your BASH terminal. Using the outlined commands, answer the following questions:
In which directory are you located by default? Use
pwd
.What other directories and files can you see in there? Use
ls
.Select one non-empty directory (i.e., a directory that you know contains some files) and move there using
cd
. Which files can you see and which one is the largest in size? Usels -l
.Copy the largest file one directory up. Use
cp
and../
.
([*] Ecosystems)
In this exercise, the following commands will be useful: pwd
(print working directory), ls
(list contents of current directory), and cd
(change directory).
Download this zip file
and unzip it in your working directory. Open the terminal and use it to navigate to this new unzipped directory.
Explore the directory - how many sub-directories does it contain? What if you go into these sub-directories? Use pen and paper to draw a diagram of the directories structure. Hint: ls *
may come in handy.
When you draw a full scheme, use it to answer the following questions:
Which of ecosystem contains the most subtypes?
Which subtype is located in a wrong directory?
Advanced: use the internet search to find a command that counts the number of sub-directories in a directory and try it out.
([*] The Hogwarts library)
Download this zip file
and unzip it in your working directory. Open the terminal and use it to navigate to this new unzipped directory. What do you find inside? Use cd
, ls
, and less
to explore directories, files, and their contents.
One file contains the spell Expecto Patronum. Find this spell hidden in the spellbooks. You can use less
to try to find it. Alternatively, to search through the files more efficiently, use grep
.
Once you find the spellbook file with Expecto Patronum, copy it into a new directory called Powerful_Spells/
. You can create this new directory in the Library/
directory using the mkdir
command.
([*] Time traveler’s data backup)
Download this zip file
and unzip it. Open the terminal and use it to navigate to this new unzipped directory. Inside, you will find the time traveler’s data backup. It seems that some of their files have corrupted or scrambled file names!
Let’s help the time traveler organize their data.
Use the terminal to:
Correct the file names, e.g., rename
Pharaoh123.txt
toPharaohs_of_Egypt.txt
. Commandmv
could be useful here, as well asless
.Create 3 new directories -
Ancient_Egypt/
,Middle_Ages/
, andFuture/
- usingmkdir
.Move the six text files into corresponding directories. Once again, the
mv
command will come in handy.
([*] Physics lab report)
We got our hand on this physics lab report
. However, it seems like our physicist was in a hurry and made some mistakes in the report. Let’s use the terminal and Vim text editor to fix the report.
First, open your terminal, navigate to the directory with the lab report file and open the file in Vim. To open the file, use vim Physics_lab_report.txt
.
Fixing the typos
The first typo we see is “acceleratian” and we want to correct it to “acceleration”. There are two occurrences of this typo. Use arrows to move the cursor to the position of each typo. Altenatively, you can also move from word to word by pressing w
. When you’re at the position of a typo, press x
to delete the wrong character (“a”). Then press i
to enter the insert Vim mode, type the correct letter “o”, and press q
to exit interative mode.
There’s another typo in the word “directly” in the Hypothesis section. Go ahead and fix that one too.
Add missing results data
In a hurry, our physicist forgot to record their results. Navigate to the Results section of the lab report and add this sentence: “The velocity was recorded as 2 m/s”. Once again, you will need the insert mode.
Save changes and exit Vim
To save all your changes and exit Vim, use the :wq
command in Vim. Verify that all your changes got saved correctly by reading your file using less
.
([**] Organizing research data)
Download this zip file
containing some research data, and unzip it in your working directory. Using the terminal, explore the contents of the downloaded directory.
As you will notice, the data originate from a physics experiment where force and velocity were measured in several experiments. However, rather than organizing data in directories by experiment number, you now realize that it would be more useful to have force data together in one directory, and velocity data together in another directory. Let’s reorganize the data!
First, in your working directory (i.e., outside of Physics_data/
directory), create two empty directories named Velocity_reports/
and Force_reports/
. Then use wildcards *
to copy all files containing velocity data from Experiment_1/
, Experiment_2/
, Experiment_3/
, and Misc/
into Velocity_reports/
. Finally, do the same for the force reports, and verify that all files were copied in the correct new directories.
([**] Proteins)
Part I
Use your BASH terminal to do the following:
Navigate to your desired work directory (e.g., Documents).
Create a new directory called
Proteins/
.In
Proteins/
, create the following sub-directories named by species:Homo_sapiens/
,Escherichia_coli
, andSaccharomyces_cerevisiae/
.
Part II
Download this zip file
and unzip it in your working directory.Using the terminal, navigate to this new unzipped directory. What can you find inside? How many files are there?
Which file is the largest in size, and how can you verify this in the terminal?
Explore the contents of the files using
less
. Determine which file belongs to which of the three species in Part I. Use themv
command to move each file to the corresponding directory inProteins/
.
Part III
Using the terminal, navigate again to the Proteins/
directory and its sub-directories. Explore each of the protein sequence files you placed there using less
to answer the following questions:
Which organism’s protein is involved in metal resistance?
Which protein contains this sequence:
QQESQSLQLHFRVW
?How many times does the sequence
SSI
appear in each protein?
([**] Electron configurations)
A chemist is organizing data on electron configurations for various elements and they need our help with locating and organizing electron configuration data for specific elements. Specifically, it seems like the file for one element got misplaced.
Download this zip file
and unzip it in your working directory. Use the terminal to navigate to it and explore it.
Locate the misplaced file and move it to the correct directory.
One element has a wrong symbol listed inside its file. Find the error, open the corresponding file in Vim, and correct it. Then save and close the file.
One element has a faulty electron configuration. Open the corresponding file in Vim and correct it. Then save and close the file.
([**] Harry Potter and the Vim Replace)
Open your terminal and navigate to the working directory.
With one command, create and open a new file called
harry_potter.txt
in Vim.Copy the text below into your file. Note that there’s a copy button in the top right part of the text box.
So the boy . . . the boy must die?” asked Snape quite calmly.
“And Voldemort himself must do it, Severus. That is essential.”
Another long silence. Then Snape said, “I thought . . . all these years . . . that we were protecting him for her. For Lily.”
“We have protected him because it has been essential to teach him, to raise him, to let him try his strength,” said Dumbledore, his eyes still tight shut. “Meanwhile, the connection between them grows ever stronger, a parasitic growth: Sometimes I have thought he suspects it himself. If I know him, he will have arranged matters so that when he does set out to meet his death, it will truly mean the end of Voldemort.”
Dumbledore opened his eyes. Snape looked horrified. “You have kept him alive so that he can die at the right moment?”
“Don’t be shocked, Severus. How many men and women have you watched die?”
“Lately, only those whom I could not save,” said Snape. He stood up. “You have used me.”
“Meaning?”
“I have spied for you and lied for you, put myself in mortal danger for you. Everything was supposed to be to keep Lily Potter’s son safe. Now you tell me you have been raising him like a pig for slaughter —”
“But this is touching, Severus,” said Dumbledore seriously. “Have you grown to care for the boy, after all?”
“For HIM?” shouted Snape. “Expecto Patronum!" From the tip of his wand burst the silver doe: She landed on the office floor, bounded once across the office, and soared out of the window. Dumbledore watched her fly away, and as her silvery glow faded he turned back to Snape, and his eyes were full of tears.
“After all this time?”
“Always,” said Snape.
Use a single Vim command to replace all occurrences of
Voldemort
withHe-Who-Must-Not-Be-Named
.Use a Vim command to replace the first three occurrences of
Snape
withSeverus
.Save changes and exit the file.
([***] Quotes)
Open your terminal and navigate to the working directory.
With one command, create and open a new file called
fun_quotes.txt
in Vim.Copy the text below into your file. Note that there’s a copy button in the top right part of the text box.
1. Andy Weir: The Martian
Yes, of course, duct tape works in a near-vacuum. Duct tape works anywhere. Duct tape is magic and should be worshipped
2. Terry Pratchett: Hogfather
Real stupidity beats artificial intelligence every time
3. Bill Nye the Science Guy
If you’re not part of the solution, you’re part of the precipitate
4. Douglas Adams: Hitchhiker’s Guide to the Galaxy
The answer to the ultimate question of life, the universe, and everything is… 42
5. Interstellar
We used to look up at the sky and wonder at our place in the stars. Now we just look down and worry about our place in the dirt
6. Neil Gaiman and Terry Pratchett: Good Omens
God does not play dice with the universe; He plays an ineffable game of His own devising, which might be compared, from the perspective of any of the other players, to being involved in an obscure and complex version of poker in a pitch-dark room, with blank cards, for infinite stakes, with a dealer who won’t tell you the rules and who smiles all the time
7. Breaking Bad
You know, technically, chemistry is the study of matter. But I prefer to see it as the study of change
8. Bill Bryson: A Short History of Nearly Everything
There are three stages in scientific discovery. First, people deny that it is true, then they deny that it is important; finally they credit the wrong person
9. The Big Bang Theory
I'm not crazy, my mother had me tested
10. Neil deGrasse Tyson: Cosmos
There’s as many atoms in a single molecule of your DNA as there are stars in the typical galaxy; we are, each of us, a little universe
11. Yuval Noah Harari: Sapiens
You could never convince a monkey to give you a banana by promising him limitless bananas after death in monkey heaven
12. Randall Munroe
I can't remember where I heard this, but someone once said that defending a position by citing free speech is sort of the ultimate concession. You're saying that the most compelling thing you can say for your position is that it's not literally illegal to express
13. WALL-E
This is called farming! You kids are gonna grow all kinds of plants! Vegetable plants, pizza plants
14. Terry Pratchett: Thief of Time
Some humans would do anything to see if it was possible to do it. If you put a large switch in some cave somewhere, with a sign on it saying 'End-of-the-World Switch. PLEASE DO NOT TOUCH', the paint wouldn't even have time to dry
In Vim, create a function called
dots
that adds a dot to the end of a quote. Then, call this function the necessary number of times to add dots to all quotes in one go.Save changes and close your file.