avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

9021_TUT_1_25T1

  2025-03-25        
字数统计: 952字   |   阅读时长: 5min

Introduction How Labs work

We can see the structure of the lab is like this:

1
2
3
%%run_and_test python3 -c "from exercise_1_1 import f1; print(f1(0, 0))"

'\n'

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
2
def f1(m, n):
return ('|' + '_' * n + '|') * m

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
2
def f2(n):
return (str(n)*n + "\n")*n

And… in case you want to use a for loop:

1
2
3
4
5
def f2(n):
result = ""
for i in range(n):
result += str(n) * n + "\n"
return result

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def f3(L):
# You can use [] list here, but set is more effective
num_remove = set()
# Check ED of Eric's reply
i = len(L)-2
flag = 0
while i >= 0:
if L[i] < L[-1] and flag == 0:
num_remove.add(L[i])
L.pop(i)
# reset the length in case it have nothing
i = len(L) - 2
elif L[i] >= L[-1]:
i -= 1
flag = 1
elif L[i] in num_remove:
L.pop(i)
else:
i -= 1
print(L)
You can full screen, change speed, and change quality of the video.

Standard Solution

I have to say Eric is a genius:

1
2
3
4
def f3(L):
while len(L) > 1 and L[-2] < L[-1]:
L.remove(L[-2])
print(L)
You can full screen, change speed, and change quality of the video.

Exercise 4

The fourth function, f4, works with dictionaries:

1
2
3
4
5
6
7
8
def f4(D, n):
if n not in D:
return []
ans = [n]
while n in D and D[n] > n:
ans.append(D[n])
n = D[n]
return ans
  1. 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.
  2. 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.
  3. For example, if D = {1: 2, 2: 3, 3: 5} and n = 1, the function will return [1, 2, 3, 5].

Standard Solution

1
2
3
4
5
6
def f4(D, n):
L = [] if n not in D else [n]
while n in D and n < D[n]:
L.append(D[n])
n = D[n]
return L

Exercise 5

The fifth function, f5, reads from a file and processes each line:

1
2
3
4
5
6
def f5(filename):
with open(filename) as f:
for i in f:
name, number = i.split(',')
number = int(number) * 1000
print(f"{number} people named {name}")
  1. This function reads a text file, where each line consists of a name and a count separated by a comma.
  2. The function multiplies the count by 1000 and prints the result in the format: “X people named Y”.
  3. For example, if the file contains a line “John,5”, it will print:
1
5000 people named John

Standard Solution

1
2
3
4
5
def f5(filename):
with open(filename) as file:
for line in file:
name, count = line.split(',')
print(int(count) * 1_000, 'people named', name)

Exercise 6

The final function, f6, also reads from a text file, but with a different format:

1
2
3
4
5
6
7
def f6(filename):
with open(filename) as f:
for i in f:
# Check if the line is not empty
if not i.isspace():
number, charactor = i.split()
print(int(number) * charactor)
  1. This function reads lines that contain an integer followed by a symbol (e.g., “5 *”).
  2. It multiplies the symbol by the integer and prints the result.
  3. For example, if the file contains the line “3 # “, the function will print:
1
### 
  • “9021”
  • Tutorial
  • Lab

扫一扫,分享到微信

微信分享二维码
9021_TUT_3_25T1
9021_TUT_3_25T1
目录
  1. 1. Introduction How Labs work
  2. 2. Exercise 1
    1. 2.0.1. My solution
  • 3. Exercise 2
    1. 3.0.1. My solution
  • 4. Exercise 3
    1. 4.0.1. My solution
    2. 4.0.2. Standard Solution
  • 5. Exercise 4
    1. 5.0.1. Standard Solution
  • 6. Exercise 5
    1. 6.0.1. Standard Solution
  • 7. Exercise 6

  • 150 篇 | 131.7k
    次 | 人
    这里自动载入天数这里自动载入时分秒
    2022-2025 loong loong | 新南威尔士龙龙号