Skip to content

Commit

Permalink
Added exercise 1 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbois committed Jun 18, 2024
1 parent d5ca083 commit c39ca20
Show file tree
Hide file tree
Showing 122 changed files with 10,663 additions and 207 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 1.1: Command line exercises\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this exercise, you will play around with the command line on your machine and get more familiar with it.\n",
"\n",
"**a)** Let's play around with some options for the `ls` command. First `cd` into a directory that has some interesting files in it (like `~git/bootcamp/command_line_tutorial`). Try the following if you are using `bash`.\n",
"\n",
" ls -F\n",
" ls -G # Might not be as cool with Git Bash on Windows\n",
" ls -l\n",
" ls -lh\n",
" ls -lS\n",
" ls -FGLh\n",
" \n",
"You should be able to infer what these different options do, but you can ask the course staff as well.\n",
"\n",
"Normally, files that begin with a dot (`.`) are omitted when listing things. They are also generally omitted when you use your OS's GUI-based file handling system (like Finder on Macs). To see them, use `ls -a`. So, `cd` into your home directory (you remember how to do that, right?), and then do\n",
"\n",
" ls -a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**b)** The nuclear option to delete *everything* in a directory is `rm -rf`. The `r` means to delete recursively, and the `f` means to \"force\" deletion. I was going to give you an exercise that uses the nuclear option, but I'm not going to do that. So, just forget I said anything. For this part of the problem, I want you to discuss with someone else in the class *when* the nuclear option might be used, and what needs to be in place before exercising it."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**c)** Try doing this if you are using macOS or Linux:\n",
"\n",
" ls /\n",
" \n",
"What is `/`? Try `cd`-ing there and seeing what's in there. **Do not delete anything!**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"This problem more or less consisted of messing around with the command line."
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 1.2: Making an rc file\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Having a `.bashrc` or `.zshrc` file allows you to configure your shell how you like.\n",
"\n",
"**a)** If you are using Linux or macOS, open a terminal and type\n",
"\n",
" echo $SHELL\n",
" \n",
"This will tell you if you are using a Bash shell or Zsh, which will tell you which kind of rc file to set up in the next part of the exercise. If you are using Windows, you will create a `.bashrc` file.\n",
"\n",
"**b)** Create a `.bashrc` or `.zshrc` file in your home directory. If you already have one, open it up for editing using Jupyter's text editor.\n",
"\n",
"**c)** It is often useful to `alias` functions to other functions. For example, I am always worried I will accidentally delete things by accident. I therefore have the following line in my `.zshrc` file.\n",
"\n",
" alias rm=\"rm -i\"\n",
" \n",
"You should create aliases for commands like `ls` based on the flags you like to *always* use. Do the same for `rm` and `mv` (I use the `-i` flag with these). To figure out what flags are available, you can look at the `man` pages. Asking Google will usually give you the information you need on flags.\n",
"\n",
"If you like, you can use my `.bashrc` file, available in `~/git/bootcamp/misc/jb_bashrc`, or my `.zshrc` file, available in `~/git/bootcamp/misc/jb_zshrc`.\n",
"\n",
"**d)** Depending on your operating system, if you are using Bash, your `~/.bashrc` file may or may not be properly loaded upon opening a new bash shell. You may, e.g. for new macOS versions, need to explicitly source your `.bashrc` file in your `~/.bash_profile` file. Therefore, you should add the following to the bottom of your `~/.bash_profile` file.\n",
"\n",
"```bash\n",
"if [ -f $HOME/.bashrc ]; then\n",
" . $HOME/.bashrc\n",
"fi\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"Again, this was mostly you messing around with the command line. The contents of my .bashrc file are shown below.\n",
"\n",
"```bash\n",
"# Give me a nice prompt that tells me my pwd\n",
"export PS1=\"\\[\\e[1;32m\\]\\u\\[\\e[0m\\]@\\e[1;36m\\]\\h\\[\\e[0m\\] [\\w]\\n% \"\n",
"\n",
"\n",
"# Keep me out of trouble!\n",
"alias rm=\"rm -i\"\n",
"alias mv=\"mv -i\"\n",
"alias cp=\"cp -i\"\n",
"\n",
"\n",
"# customize list output\n",
"alias ls=\"ls -FGh\"\n",
"export LSCOLORS=\"gxfxcxdxCxegedabagacad\"\n",
"```\n",
"\n",
"And my .zshrc file is:\n",
"\n",
"```zsh\n",
"# This is a nice prompt; gives green check mark if last command executed\n",
"# without a problem and gives a red questionmark with an exit code\n",
"# if it didn't, along with pwd.\n",
"PROMPT='%(?.%F{green}√.%F{red}?%?)%f [%B%F{240}%10~%f%b] \n",
"%# '\n",
"\n",
"# Aliases for save moving, removing, and copying of files\n",
"alias rm=\"rm -i\"\n",
"alias mv=\"mv -i\"\n",
"alias cp=\"cp -i\"\n",
"\n",
"# Nicely formatted listing\n",
"alias ls=\"ls -FGh\"\n",
"\n",
"# Nice set of colors\n",
"export LSCOLORS=\"gxfxcxdxCxegedabagacad\"\n",
"```"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 1.3: Time and type conversions\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the techniques you have learned in the first day of bootcamp, generate a time stamp (like 13:29:45 for nearly half past one in the afternoon) for the time that is 63,252 seconds after midnight. Start with this statement:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"seconds_past_midnight = 63252"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After that statement, the only numeric keys you should need or want to push are `0`, `2` or `3`, and `6`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br/>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To get the number of hours, we floor divide by 3600. To get the number of minutes, we take the modulus of division by 3600, and then divide that by 60. Finally, the seconds are what is left over when we divide by 60."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"hours = seconds_past_midnight // 60**2\n",
"minutes = (seconds_past_midnight % 60**2) // 60\n",
"seconds = seconds_past_midnight % 60"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have these, we concatenate a string together."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"17:34:12\n"
]
}
],
"source": [
"time_str = str(hours) + ':' + str(minutes) + ':' + str(seconds)\n",
"\n",
"print(time_str)"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"There are much elegant ways of doing these two operations, including using string methods. For most applications using time stamps, you would use the [datetime module](https://docs.python.org/3/library/datetime.html) of the standard library."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Computing environment"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"tags": [
"hide-input"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python implementation: CPython\n",
"Python version : 3.11.3\n",
"IPython version : 8.12.0\n",
"\n",
"jupyterlab: 3.6.3\n",
"\n"
]
}
],
"source": [
"%load_ext watermark\n",
"%watermark -v -p jupyterlab"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit c39ca20

Please sign in to comment.