Introduction How Labs work
We can see the structure of the lab is like this:
1 | %run_and_test python3 -c "from exercise_1_1 import f1; print(f1(0, 0))" |
Which means run_and_test
? It is a Magic Command, run python file which is exercise_1_1.py
and test the output of f1(0, 0)
.
And at the top of this Jupyter notebook, we can see the following code:
1 | load_ext run_and_test |
This is a magic command that loads the run_and_test
extension, which allows us to run and test Python code in the notebook.
And if you run %pycat exercise_1_1_template.py
, you will get a new file exercise_1_1.py
which is the template of the exercise.
You can write your code inside the exercise_1_1.py
file and run the test code in the notebook to check if your code is correct. Or directly write your code after %%writefile exercise_1_1.py
command block.
Exercise 1
Finding the pattern, we can see that the first parameter m means how many structures there are, and the second parameter n means how many underscores there are.
So we can assume the structure is like:
1 | |____| |
And if we have m = 3
and n = 4
, the output would be:
1 | |____||____||____| |
My solution
In Python, we can use multiplication to repeat a string. So we can use the following code to solve this problem:
1 | def f1(m, n): |
Exercise 2
Same way to find the pattern, n rows and n columns, and each row contains the digit n repeated n times.
And you can find, the input n
is a digit, which means it is an integer. While we use “*” with the integer, it is the mathematical multiplication.
So we need to convert the integer to a string, and then multiply it with the integer, and add a newline at the end of each row.
My solution
1 | def f2(n): |
And… in case you want to use a for loop:
1 | def f2(n): |
Exercise 3
This problem is a bit complex to understand, so let’s break it down step by step. Based on Eric’s reply in Ed, we can see that the problem requires us to traverse from the rightmost side, removing all elements that are ≤ the last element (x
) until we encounter a number that is ≥ the last element (y
). It is important to note that if there are any elements to the left of y
that were previously removed, they should also be removed.
This Exercise needs us use only 1 loop.
My solution
1 | def f3(L): |
You can full screen, change speed, and change quality of the video.
Standard Solution
I have to say Eric is a genius:
1 | def f3(L): |
You can full screen, change speed, and change quality of the video.
Exercise 4
The fourth function, f4, works with dictionaries:
1 | def f4(D, n): |
- This function takes a dictionary D and an integer n. It generates a strictly increasing sequence of integers based on the relationships defined in the dictionary.
- Starting from n, it appends D[n], D[D[n]], and so on to the list, as long as each subsequent value is greater than the previous one.
- For example, if D = {1: 2, 2: 3, 3: 5} and n = 1, the function will return [1, 2, 3, 5].
Standard Solution
1 | def f4(D, n): |
Exercise 5
The fifth function, f5, reads from a file and processes each line:
1 | def f5(filename): |
- This function reads a text file, where each line consists of a name and a count separated by a comma.
- The function multiplies the count by 1000 and prints the result in the format: “X people named Y”.
- For example, if the file contains a line “John,5”, it will print:
1 | 5000 people named John |
Standard Solution
1 | def f5(filename): |
Exercise 6
The final function, f6, also reads from a text file, but with a different format:
1 | def f6(filename): |
- This function reads lines that contain an integer followed by a symbol (e.g., “5 *”).
- It multiplies the symbol by the integer and prints the result.
- For example, if the file contains the line “3 # “, the function will print:
1 | ### |