Skip to content

Commit 4b1a0aa

Browse files
longsizhuogithub-actions[bot]
authored andcommitted
chore(docs): sync doc metadata [skip ci]
1 parent a96b8fe commit 4b1a0aa

File tree

2 files changed

+67
-37
lines changed

2 files changed

+67
-37
lines changed

app/docs/CommunityShare/Leetcode/9021_TUT_3_25T1.md

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
title: 9021_TUT_3_25T1.md
33
date: 2025/3/07
44
tags:
5-
- '9021'
5+
- "9021"
66
- lab
77
abbrlink: 974decd3
8+
docId: s0cadbu09dgu54q0zxttkx7z
89
---
910

1011
# Exercise 1
1112

1213
### Problem Description:
14+
1315
You are given two integers, `m` and `n`, where:
1416

1517
- `m` represents the number of repeated units (patterns).
1618
- `n` represents the number of elements (underscores) within each unit.
1719

1820
The goal is to generate a string where:
1921

20-
- Each unit contains `n` underscores (_), separated by `|`.
22+
- Each unit contains `n` underscores (\_), separated by `|`.
2123
- These units are then concatenated together, separated by `*`.
2224

2325
### My Solution:
@@ -57,7 +59,6 @@ In summary, my solution focuses on modularity by breaking down the problem into
5759

5860
# Exercise 2
5961

60-
6162
### Problem Description:
6263

6364
The goal is to generate a pattern based on the digits of a given number n. Specifically:
@@ -82,19 +83,17 @@ def f2(n):
8283
#### My Thought Process:
8384

8485
1. Loop through each digit:
85-
Convert the number n to a string to iterate over each digit individually.
86+
Convert the number n to a string to iterate over each digit individually.
8687

8788
2. Check if the digit is even or odd:
88-
Convert each digit back to an integer and use the modulus operator (% 2) to check if the digit is even or odd.
89+
Convert each digit back to an integer and use the modulus operator (% 2) to check if the digit is even or odd.
8990

9091
3. Append the corresponding square:
9192
- If the digit is even, append a white square (⬜) to the result string.
9293
- If the digit is odd, append a black square (⬛).
9394

9495
4. Print the final string:
95-
After processing all the digits, print the final string containing black and white squares.
96-
97-
96+
After processing all the digits, print the final string containing black and white squares.
9897

9998
### Standard Solution:
10099

@@ -104,6 +103,7 @@ def f2(n):
104103
```
105104

106105
Dr.Martin's solution is:
106+
107107
1. More compact and Pythonic.
108108
2. Uses a dictionary and list comprehension for brevity and efficiency.
109109
3. The Unicode characters for the squares are referenced directly using their escape sequences (`\u2b1c` for white, `\u2b1b` for black).
@@ -115,9 +115,10 @@ Dr.Martin's solution is:
115115
In this task, the number `n` is treated as a number expressed in different bases (ranging from 2 to 10), and we aim to convert it into its corresponding base 10 value for each of these bases, where the conversion is valid.
116116

117117
For n = 2143:
118-
- `2143` in base `5` is equivalent to `298` in base `10`.
119-
- `2143` in base `6` is equivalent to `495` in base `10`.
120-
- And so on.
118+
119+
- `2143` in base `5` is equivalent to `298` in base `10`.
120+
- `2143` in base `6` is equivalent to `495` in base `10`.
121+
- And so on.
121122

122123
The goal is to iterate over different base systems from 2 to 10, treat the input number `n` as if it is expressed in each base, and then convert it to base 10.
123124

@@ -133,7 +134,9 @@ def f3(n: int):
133134
except ValueError:
134135
pass
135136
```
137+
136138
#### My Thought Process:
139+
137140
1. Iterating over Bases (2 to 10):
138141
- We loop through the base values i ranging from 2 to 10.
139142

@@ -146,16 +149,16 @@ def f3(n: int):
146149
- The `try-except` block ensures that invalid bases are skipped, allowing us to handle cases where the digits in `n` are not valid for a particular base.
147150

148151
### Standard Solution:
149-
152+
150153
```python
151154
def f3(n):
152155
n_as_string = str(n)
153156
min_base = max(2, max({int(d) for d in n_as_string}) + 1)
154157
for b in range(min_base, 11):
155158
print(f'{n} is {int(n_as_string, b)} in base {b}')
156159
```
157-
It skips the bases where the digits in n are not valid, and it uses a set comprehension to extract the unique digits from n_as_string. The maximum digit is then used to determine the minimum base to start iterating from.
158160

161+
It skips the bases where the digits in n are not valid, and it uses a set comprehension to extract the unique digits from n_as_string. The maximum digit is then used to determine the minimum base to start iterating from.
159162

160163
# Exercise 4
161164

@@ -194,23 +197,25 @@ def f4(n: int, base: int):
194197
- After collecting all digits, we reverse the list and return it as a string.
195198

196199
2. Main Function `f4(n, base)`:
197-
We initialize an empty dictionary `D`.
198-
For each number `i` from `0` to `n`, we convert `i` to the given base using `convert_to_base()`.
199-
The converted base digits are then mapped to integers and stored in a tuple as the value for each key `i` in the dictionary.
200+
We initialize an empty dictionary `D`.
201+
For each number `i` from `0` to `n`, we convert `i` to the given base using `convert_to_base()`.
202+
The converted base digits are then mapped to integers and stored in a tuple as the value for each key `i` in the dictionary.
200203

201204
### Explanation of Why `map()` is not Pythonic:
205+
202206
In the function f4, the use of `map(int, convert_to_base(i, base))` applies the `int` function
203207
to each element of the result from `convert_to_base`, effectively converting each character to an integer.
204208

205209
However, it's worth noting that the `map()` function, which originates from functional programming,
206210
has largely been superseded by more Pythonic constructs such as list comprehensions.
207211

208212
These comprehensions are generally considered superior for several reasons:
209-
- They are more elegant and concise.
210-
- They tend to be shorter in terms of syntax, making the code easier to read.
211-
- They are easier to understand for most people, especially those who are more familiar with Python's
212-
standard syntax rather than functional programming constructs.
213-
- In many cases, they are also more efficient in terms of performance.
213+
214+
- They are more elegant and concise.
215+
- They tend to be shorter in terms of syntax, making the code easier to read.
216+
- They are easier to understand for most people, especially those who are more familiar with Python's
217+
standard syntax rather than functional programming constructs.
218+
- In many cases, they are also more efficient in terms of performance.
214219

215220
For example, instead of using `map(int, ...)`, the same functionality could be achieved with a
216221
list comprehension, like so:
@@ -240,9 +245,11 @@ whereas the standard solution is more concise and directly integrates the conver
240245
# Exercise 5
241246

242247
At the first, try to run this:
248+
243249
```python
244250
print(0.1 + 0.2)
245251
```
252+
246253
What happened? The result is not 0.3, but 0.30000000000000004. Why?
247254

248255
### Problem Description:
@@ -252,6 +259,7 @@ The approach we are using in this exercise is designed to expose the limitations
252259
This problem can seem complex, and it's designed to highlight the subtleties of floating-point arithmetic. Let's walk through the logic using the test cases to figure out what the function does.
253260

254261
### Key Concepts:
262+
255263
- **Floating-point numbers**: Computers store floating-point numbers using a binary format, which often introduces precision errors.
256264
- **Precision**: We’re working with two types of precision in this function—simple precision (same as the length of the fractional part) and double precision (twice that length).
257265

@@ -261,22 +269,22 @@ This problem can seem complex, and it's designed to highlight the subtleties of
261269
def f5(integral_part, fractional_part):
262270
# Calculate the number of digits in the fractional part
263271
precision = len(str(fractional_part))
264-
272+
265273
# Concatenate the integral and fractional parts as strings, then convert to a float
266274
a_float = float(str(integral_part) + '.' + str(fractional_part))
267-
275+
268276
# Format the float with precision equal to the number of digits in the fractional part (simple precision)
269277
simple_precision = f'{a_float:.{precision}f}'
270-
278+
271279
# Append a number of zeros equal to the fractional part length to the simple precision value (extended precision)
272280
extended_simple_precision = simple_precision + '0' * precision
273-
281+
274282
# Format the float with precision equal to twice the number of fractional digits (double precision)
275283
double_precision = f'{a_float:.{precision * 2}f}'
276-
284+
277285
# Print the first part of the output
278286
print('With', precision * 2, 'digits after the decimal point, ', end='')
279-
287+
280288
# Compare if extended precision and double precision values are the same
281289
if extended_simple_precision == double_precision:
282290
# If they are the same, it means the float is precise and has trailing zeros
@@ -307,6 +315,7 @@ We are required to sort the list L by using a multi-key sorting mechanism, where
307315
- Finally, if required, the sorting proceeds up to the last field in `fields`.
308316

309317
### Example of Fields Explanation:
318+
310319
If `fields = [2, 1]`, it means:
311320

312321
1. First, sort based on the second element of each sublist.
@@ -320,39 +329,43 @@ def f6(L, fields):
320329
```
321330

322331
1. Sorting with sorted():
323-
The `sorted()` function is used to sort the list `L`.
324-
The key parameter defines how the sorting will be performed.
332+
The `sorted()` function is used to sort the list `L`.
333+
The key parameter defines how the sorting will be performed.
325334

326335
2. Lambda Function:
327-
The lambda function defines how the sublists will be sorted. It generates a list of values for each sublist based on the positions specified in `fields`.
328-
For example, if `fields = [2, 1]`, the lambda function will extract the second and first elements from each sublist in that order, and sorting will be done based on this new list.
336+
The lambda function defines how the sublists will be sorted. It generates a list of values for each sublist based on the positions specified in `fields`.
337+
For example, if `fields = [2, 1]`, the lambda function will extract the second and first elements from each sublist in that order, and sorting will be done based on this new list.
329338

330339
3. Key Structure:
331-
The key is a list of elements from each sublist, indexed by the positions specified in fields. We use `x[i - 1]` because fields is `1-based`, and list indexing in Python is `0-based`.
340+
The key is a list of elements from each sublist, indexed by the positions specified in fields. We use `x[i - 1]` because fields is `1-based`, and list indexing in Python is `0-based`.
332341

333342
4. What is Lambda Function?
334343

335344
For example:
336345

337346
We have:
347+
338348
```python
339349
f = lambda x: x * x
340350
```
341351

342352
This is equivalent to:
353+
343354
```python
344355
def f(x):
345356
return x * x
346357
```
347358

348359
And lambda function in a sorted function is used to define a custom sorting key.
360+
349361
```python
350362
L = [(1,2), (3,1), (5,0)]
351363

352364
SortedL = sorted(L, key=lambda x: x[1])
353365

354366
print(SortedL)
355367
```
368+
356369
The result is: `[(5, 0), (3, 1), (1, 2)]`, it sorts the list based on the second element of each tuple.
357370

358371
### Standard Solution:

generated/doc-contributors.json

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"repo": "InvolutionHell/involutionhell",
3-
"generatedAt": "2025-11-26T03:06:32.553Z",
3+
"generatedAt": "2025-11-27T03:05:27.753Z",
44
"docsDir": "app/docs",
5-
"totalDocs": 124,
5+
"totalDocs": 125,
66
"results": [
77
{
88
"docId": "ue27z7z95yzw3lhhfj7nit1c",
@@ -2123,13 +2123,13 @@
21232123
"docId": "chb8ee5s38v8gh751n9e5znj",
21242124
"path": "app/docs/CommunityShare/Leetcode/1828统计一个圆中点的数目_translated.md",
21252125
"contributorStats": {
2126-
"114939201": 1
2126+
"114939201": 2
21272127
},
21282128
"contributors": [
21292129
{
21302130
"githubId": "114939201",
2131-
"contributions": 1,
2132-
"lastContributedAt": "2025-11-26T03:00:05.000Z",
2131+
"contributions": 2,
2132+
"lastContributedAt": "2025-11-26T03:06:33.000Z",
21332133
"login": "longsizhuo",
21342134
"avatarUrl": "https://avatars.githubusercontent.com/u/114939201?v=4",
21352135
"htmlUrl": "https://github.com/longsizhuo"
@@ -2272,6 +2272,23 @@
22722272
}
22732273
]
22742274
},
2275+
{
2276+
"docId": "s0cadbu09dgu54q0zxttkx7z",
2277+
"path": "app/docs/CommunityShare/Leetcode/9021_TUT_3_25T1.md",
2278+
"contributorStats": {
2279+
"114939201": 1
2280+
},
2281+
"contributors": [
2282+
{
2283+
"githubId": "114939201",
2284+
"contributions": 1,
2285+
"lastContributedAt": "2025-11-27T03:00:16.000Z",
2286+
"login": "longsizhuo",
2287+
"avatarUrl": "https://avatars.githubusercontent.com/u/114939201?v=4",
2288+
"htmlUrl": "https://github.com/longsizhuo"
2289+
}
2290+
]
2291+
},
22752292
{
22762293
"docId": "d5evrnoglwjvmyginjq84bl0",
22772294
"path": "app/docs/CommunityShare/Leetcode/93复原Ip地址.md",

0 commit comments

Comments
 (0)