Skip to content

Commit 60445dd

Browse files
committedMar 23, 2025·
docs(notes): update data representation content and fix MathJax script
1 parent 8366d02 commit 60445dd

File tree

2 files changed

+107
-86
lines changed

2 files changed

+107
-86
lines changed
 

‎notes/2025-03-23-data-representation-en.md

+34-34
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ Below is a comprehensive tutorial that breaks down the key topics in the “Data
1818
**Concepts:**
1919

2020
- **Base-2 System:** Uses only two digits: 0 and 1.
21-
- **Place Value:** Each digit represents a power of 2. For a binary number \( b_n b_{n-1} \dots b_1 b_0 \), the value is
21+
- **Place Value:** Each digit represents a power of 2. For a binary number \\( b_n b_{n-1} \dots b_1 b_0 \\), the value is
2222
\[
2323
\sum_{i=0}^{n} b_i \times 2^i
2424
\]
25-
where \( b_i \) is either 0 or 1.
25+
where \\( b_i \\) is either 0 or 1.
2626

2727
**Example:**
2828

29-
Convert binary \( 1011_2 \) to decimal:
30-
- \( 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^0 = 8 + 0 + 2 + 1 = 11_{10} \)
29+
Convert binary \\( 1011_2 \\) to decimal:
30+
- \\( 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^0 = 8 + 0 + 2 + 1 = 11_{10} \\)
3131

3232
**Practice Exercise:**
3333

34-
- Convert the binary number \( 110010_2 \) to decimal.
34+
- Convert the binary number \\( 110010_2 \\) to decimal.
3535

3636
---
3737

@@ -40,7 +40,7 @@ Convert binary \( 1011_2 \) to decimal:
4040
**Concepts:**
4141

4242
- **Base-16 System:** Uses sixteen symbols: 0–9 and A–F (where A=10, B=11, …, F=15).
43-
- **Place Value:** Each digit represents a power of 16. For a hexadecimal number \( h_n h_{n-1} \dots h_1 h_0 \), the value is
43+
- **Place Value:** Each digit represents a power of 16. For a hexadecimal number \\( h_n h_{n-1} \dots h_1 h_0 \\), the value is
4444
\[
4545
\sum_{i=0}^{n} h_i \times 16^i
4646
\]
@@ -52,16 +52,16 @@ Convert binary \( 1011_2 \) to decimal:
5252

5353
**Example:**
5454

55-
Convert binary \( 1011011101_2 \) to hexadecimal:
56-
- Group into 4-bit groups: \( 10 \, 1101 \, 1101 \) (pad left with zeros if needed → \( 0010 \, 1101 \, 1101 \))
57-
- \( 0010_2 = 2_{16} \)
58-
- \( 1101_2 = D_{16} \)
59-
- \( 1101_2 = D_{16} \)
60-
- Final hexadecimal: \( 2DD_{16} \)
55+
Convert binary \\( 1011011101_2 \\) to hexadecimal:
56+
- Group into 4-bit groups: \\( 10 \, 1101 \, 1101 \\) (pad left with zeros if needed → \\( 0010 \, 1101 \, 1101 \\))
57+
- \\( 0010_2 = 2_{16} \\)
58+
- \\( 1101_2 = D_{16} \\)
59+
- \\( 1101_2 = D_{16} \\)
60+
- Final hexadecimal: \\( 2DD_{16} \\)
6161

6262
**Practice Exercise:**
6363

64-
- Convert the binary number \( 11101010_2 \) into hexadecimal.
64+
- Convert the binary number \\( 11101010_2 \\) into hexadecimal.
6565

6666
---
6767

@@ -93,18 +93,18 @@ where the bias for single precision is 127.
9393

9494
Suppose you have a 32-bit binary string representing a floating-point number:
9595
- **Sign Bit:** 0 (positive)
96-
- **Exponent Bits:** e.g., \( 10000010_2 \) → Decimal 130. Subtract bias: \( 130 - 127 = 3 \).
97-
- **Mantissa Bits:** Suppose they represent a fractional part like \( .101000... \).
96+
- **Exponent Bits:** e.g., \\( 10000010_2 \\) → Decimal 130. Subtract bias: \\( 130 - 127 = 3 \\).
97+
- **Mantissa Bits:** Suppose they represent a fractional part like \\( .101000... \\).
9898

9999
Then the number would be:
100100
\[
101101
+1.101000 \times 2^3
102102
\]
103-
Convert \( 1.101000 \) from binary to decimal and then multiply by \( 2^3 \) to get the final value.
103+
Convert \\( 1.101000 \\) from binary to decimal and then multiply by \\( 2^3 \\) to get the final value.
104104

105105
**Practice Exercise:**
106106

107-
- Given the following breakdown for a 32-bit floating-point number: sign = 0, exponent = \( 10000001_2 \) (decimal 129), and mantissa = \( 01000000000000000000000 \), compute the decimal value.
107+
- Given the following breakdown for a 32-bit floating-point number: sign = 0, exponent = \\( 10000001_2 \\) (decimal 129), and mantissa = \\( 01000000000000000000000 \\), compute the decimal value.
108108

109109
---
110110

@@ -113,9 +113,9 @@ Convert \( 1.101000 \) from binary to decimal and then multiply by \( 2^3 \) to
113113
### 2.1 Basic Boolean Operations
114114

115115
**Key Operations:**
116-
- **AND (·):** \( A \land B \) is true only if both \( A \) and \( B \) are true.
117-
- **OR (+):** \( A \lor B \) is true if at least one of \( A \) or \( B \) is true.
118-
- **NOT (’ or \(\neg\)):** \( \neg A \) inverts the truth value of \( A \).
116+
- **AND (·):** \\( A \land B \\) is true only if both \\( A \\) and \\( B \\) are true.
117+
- **OR (+):** \\( A \lor B \\) is true if at least one of \\( A \\) or \\( B \\) is true.
118+
- **NOT (’ or \\(\neg\\)):** \\( \neg A \\) inverts the truth value of \\( A \\).
119119

120120
**Truth Tables:**
121121

@@ -146,7 +146,7 @@ Convert \( 1.101000 \) from binary to decimal and then multiply by \( 2^3 \) to
146146

147147
**Practice Exercise:**
148148

149-
- Given the Boolean expression \( \neg(A \land B) \), use a truth table to show it is equivalent to \( \neg A \lor \neg B \) (De Morgan’s Law).
149+
- Given the Boolean expression \\( \neg(A \land B) \\), use a truth table to show it is equivalent to \\( \neg A \lor \neg B \\) (De Morgan’s Law).
150150

151151
---
152152

@@ -155,24 +155,24 @@ Convert \( 1.101000 \) from binary to decimal and then multiply by \( 2^3 \) to
155155
**Important Laws:**
156156

157157
- **Commutative Law:**
158-
- \( A \lor B = B \lor A \)
159-
- \( A \land B = B \land A \)
158+
- \\( A \lor B = B \lor A \\)
159+
- \\( A \land B = B \land A \\)
160160

161161
- **Associative Law:**
162-
- \( (A \lor B) \lor C = A \lor (B \lor C) \)
163-
- \( (A \land B) \land C = A \land (B \land C) \)
162+
- \\( (A \lor B) \lor C = A \lor (B \lor C) \\)
163+
- \\( (A \land B) \land C = A \land (B \land C) \\)
164164

165165
- **Distributive Law:**
166-
- \( A \land (B \lor C) = (A \land B) \lor (A \land C) \)
167-
- \( A \lor (B \land C) = (A \lor B) \land (A \lor C) \)
166+
- \\( A \land (B \lor C) = (A \land B) \lor (A \land C) \\)
167+
- \\( A \lor (B \land C) = (A \lor B) \land (A \lor C) \\)
168168

169169
- **De Morgan’s Laws:**
170-
- \( \neg (A \land B) = \neg A \lor \neg B \)
171-
- \( \neg (A \lor B) = \neg A \land \neg B \)
170+
- \\( \neg (A \land B) = \neg A \lor \neg B \\)
171+
- \\( \neg (A \lor B) = \neg A \land \neg B \\)
172172

173173
**Practice Exercise:**
174174

175-
- Simplify the expression \( A \lor (\neg A \land B) \) using Boolean algebra laws.
175+
- Simplify the expression \\( A \lor (\neg A \land B) \\) using Boolean algebra laws.
176176

177177
---
178178

@@ -184,17 +184,17 @@ Convert \( 1.101000 \) from binary to decimal and then multiply by \( 2^3 \) to
184184

185185
- **Addition:**
186186
- Follows similar rules to decimal addition but with base 2.
187-
- **Example:** \( 1011_2 + 1101_2 \)
187+
- **Example:** \\( 1011_2 + 1101_2 \\)
188188
- Align and add bit by bit, carrying over when the sum exceeds 1.
189189

190190
- **Subtraction:**
191191
- Can be performed by borrowing, or by using the two's complement method.
192192
- **Two's Complement:** To represent negative numbers, invert the bits and add 1.
193-
- **Example:** To subtract \( 1101_2 \) from \( 1011_2 \), first find the two's complement of \( 1101_2 \) then add.
193+
- **Example:** To subtract \\( 1101_2 \\) from \\( 1011_2 \\), first find the two's complement of \\( 1101_2 \\) then add.
194194

195195
**Practice Exercise:**
196196

197-
- Perform the binary subtraction \( 10100_2 - 01101_2 \) using two’s complement.
197+
- Perform the binary subtraction \\( 10100_2 - 01101_2 \\) using two’s complement.
198198

199199
---
200200

@@ -207,7 +207,7 @@ Convert \( 1.101000 \) from binary to decimal and then multiply by \( 2^3 \) to
207207

208208
**Practice Exercise:**
209209

210-
- Add \( 2A3_{16} \) and \( 1F7_{16} \).
210+
- Add \\( 2A3_{16} \\) and \\( 1F7_{16} \\).
211211

212212
---
213213

‎scripts/content/fix_mathjax.py

+73-52
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,104 @@
33
import markdown
44
import argparse
55

6-
def fix_mathjax_in_markdown(directory, max_files=None):
6+
def fix_mathjax_in_file(filepath):
77
"""
8-
Finds all markdown files in a directory and replaces instances of
9-
\( and \) with \\( and \\) respectively, skipping code blocks.
8+
Replaces instances of \( and \) with \\( and \\) respectively in a markdown file,
9+
skipping code blocks.
1010
1111
Args:
12-
directory (str): The directory to search for markdown files.
13-
max_files (int, optional): Maximum number of files to process. Defaults to None (unlimited).
12+
filepath (str): The path to the markdown file to process.
1413
"""
15-
files_processed = 0
16-
for root, _, files in os.walk(directory):
17-
for filename in files:
18-
if filename.endswith(".md"):
19-
filepath = os.path.join(root, filename)
20-
try:
21-
with open(filepath, 'r', encoding='utf-8') as f:
22-
content = f.read()
14+
try:
15+
with open(filepath, 'r', encoding='utf-8') as f:
16+
content = f.read()
2317

24-
# Parse markdown to identify code blocks
25-
md = markdown.Markdown(extensions=['fenced_code'])
26-
html_content = md.convert(content)
18+
# Parse markdown to identify code blocks
19+
md = markdown.Markdown(extensions=['fenced_code'])
20+
html_content = md.convert(content)
2721

28-
# Identify code blocks using regex
29-
code_blocks = list(re.finditer(r'<pre><code.*?>.*?</code></pre>', html_content, re.DOTALL))
22+
# Identify code blocks using regex
23+
code_blocks = list(re.finditer(r'<pre><code.*?>.*?</code></pre>', html_content, re.DOTALL))
3024

31-
# Extract code block content and their positions
32-
code_block_data = []
33-
for match in code_blocks:
34-
code_block_data.append({
35-
'start': match.start(),
36-
'end': match.end(),
37-
'content': match.group(0)
38-
})
25+
# Extract code block content and their positions
26+
code_block_data = []
27+
for match in code_blocks:
28+
code_block_data.append({
29+
'start': match.start(),
30+
'end': match.end(),
31+
'content': match.group(0)
32+
})
3933

40-
# Function to replace MathJax delimiters outside code blocks
41-
def replace_mathjax(text):
42-
temp_text = text
43-
for cb in code_block_data:
44-
temp_text = temp_text.replace(cb['content'], 'CODE_BLOCK_PLACEHOLDER')
34+
# Function to replace MathJax delimiters outside code blocks
35+
def replace_mathjax(text):
36+
temp_text = text
37+
for cb in code_block_data:
38+
temp_text = temp_text.replace(cb['content'], 'CODE_BLOCK_PLACEHOLDER')
4539

46-
temp_text = re.sub(r'\\\(', r'\\\\(', temp_text)
47-
temp_text = re.sub(r'\\\)', r'\\\\)', temp_text)
40+
temp_text = re.sub(r'\\\(', r'\\\\(', temp_text)
41+
temp_text = re.sub(r'\\\)', r'\\\\)', temp_text)
4842

49-
for cb in code_block_data:
50-
temp_text = temp_text.replace('CODE_BLOCK_PLACEHOLDER', cb['content'])
51-
return temp_text
43+
for cb in code_block_data:
44+
temp_text = temp_text.replace('CODE_BLOCK_PLACEHOLDER', cb['content'])
45+
return temp_text
5246

53-
updated_content = replace_mathjax(content)
47+
updated_content = replace_mathjax(content)
5448

55-
with open(filepath, 'w', encoding='utf-8') as f:
56-
f.write(updated_content)
49+
with open(filepath, 'w', encoding='utf-8') as f:
50+
f.write(updated_content)
5751

58-
print(f"Fixed MathJax delimiters in: {filepath}")
59-
files_processed += 1
52+
print(f"Fixed MathJax delimiters in: {filepath}")
53+
return True
54+
55+
except Exception as e:
56+
print(f"Error processing {filepath}: {e}")
57+
return False
6058

61-
if max_files and files_processed >= max_files:
62-
print(f"Maximum files processed ({max_files}). Exiting directory.")
63-
return
59+
def fix_mathjax_in_markdown(directory, max_files=None):
60+
"""
61+
Finds all markdown files in a directory and replaces instances of
62+
\( and \) with \\( and \\) respectively, skipping code blocks.
6463
65-
except Exception as e:
66-
print(f"Error processing {filepath}: {e}")
64+
Args:
65+
directory (str): The directory to search for markdown files.
66+
max_files (int, optional): Maximum number of files to process. Defaults to None (unlimited).
67+
"""
68+
files_processed = 0
69+
for root, _, files in os.walk(directory):
70+
for filename in files:
71+
if filename.endswith(".md"):
72+
filepath = os.path.join(root, filename)
73+
success = fix_mathjax_in_file(filepath)
74+
if success:
75+
files_processed += 1
6776

77+
if max_files and files_processed >= max_files:
78+
print(f"Maximum files processed ({max_files}). Exiting directory.")
79+
return
6880

6981
def main():
7082
"""
71-
Main function to specify directories to process.
83+
Main function to process either a single file or directories.
7284
"""
7385
parser = argparse.ArgumentParser(description="Fix MathJax delimiters in Markdown files.")
7486
parser.add_argument("--maxfiles", type=int, help="Maximum number of files to process.")
87+
parser.add_argument("--file", type=str, help="Path to a specific markdown file to process.")
7588
args = parser.parse_args()
7689

77-
directories = ["_posts", "original", "notes"] # Add "." to process the current directory as well
78-
for directory in directories:
79-
if os.path.exists(directory):
80-
fix_mathjax_in_markdown(directory, max_files=args.maxfiles)
90+
if args.file:
91+
# Process a single file
92+
if os.path.exists(args.file) and args.file.endswith(".md"):
93+
fix_mathjax_in_file(args.file)
8194
else:
82-
print(f"Directory not found: {directory}")
95+
print(f"Invalid file path or not a markdown file: {args.file}")
96+
else:
97+
# Process directories
98+
directories = ["_posts", "original", "notes"]
99+
for directory in directories:
100+
if os.path.exists(directory):
101+
fix_mathjax_in_markdown(directory, max_files=args.maxfiles)
102+
else:
103+
print(f"Directory not found: {directory}")
83104

84105
if __name__ == "__main__":
85106
main()

0 commit comments

Comments
 (0)
Please sign in to comment.