Skip to content

Commit 0da5ed1

Browse files
committed
feat: Added cs61a
1 parent ac8fa39 commit 0da5ed1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+4919
-5
lines changed

constants/nav.ts

+47-3
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,78 @@ export const sidebar: DefaultTheme.Sidebar = [
1111
text: 'Computer Science & Software Engineering',
1212
items: [
1313
{
14-
text: 'CS106B',
14+
text: 'CS61A: Structure and Interpretation of Computer Programs',
15+
link: '/pages/coding/cs61a/info',
16+
collapsed: true,
17+
items: [
18+
{
19+
text: 'Python',
20+
collapsed: true,
21+
items: await getNavItems(`${noteRootDir}/coding/cs61a/src/python`),
22+
},
23+
{
24+
text: 'Programming Ideas',
25+
collapsed: true,
26+
items: await getNavItems(`${noteRootDir}/coding/cs61a/src/programming`),
27+
},
28+
{
29+
text: 'Object Oriented Programming',
30+
collapsed: true,
31+
items: await getNavItems(`${noteRootDir}/coding/cs61a/src/oop`),
32+
},
33+
{
34+
text: 'Data Structures',
35+
collapsed: true,
36+
items: await getNavItems(`${noteRootDir}/coding/cs61a/src/ds`),
37+
},
38+
{
39+
text: 'Scheme',
40+
collapsed: true,
41+
items: await getNavItems(`${noteRootDir}/coding/cs61a/src/scheme`),
42+
},
43+
{
44+
text: 'Labs and HW Reflection',
45+
collapsed: true,
46+
items: await getNavItems(`${noteRootDir}/coding/cs61a/src/labs`),
47+
},
48+
]
49+
},
50+
{
51+
text: 'CS106B: Programming Abstractions in C++',
1552
link: '/pages/coding/cs106b/info',
1653
collapsed: true,
1754
items: await getNavItems(`${noteRootDir}/coding/cs106b/src/`),
1855
},
1956
{
20-
text: 'CS61B',
57+
text: 'CS61B: Data Structures and Algorightm',
2158
link: '/pages/coding/cs61b/info',
2259
collapsed: true,
2360
items: [
2461
{
2562
text: 'Java',
63+
collapsed: true,
2664
items: await getNavItems(`${noteRootDir}/coding/cs61b/src/java/`),
2765
},
2866
{
2967
text: 'OOP',
68+
collapsed: true,
3069
items: await getNavItems(`${noteRootDir}/coding/cs61b/src/oop/`),
3170
},
3271
{
3372
text: 'Data Structures',
73+
collapsed: true,
3474
items: await getNavItems(`${noteRootDir}/coding/cs61b/src/ds/`),
3575
},
3676
]
3777
},
3878
{
39-
text: 'CS61C',
79+
text: 'CS61C: Machine Structure in C and RISC-V',
4080
link: '/pages/coding/cs61c/info',
4181
collapsed: true,
4282
items: [
4383
{
4484
text: 'C',
85+
collapsed: true,
4586
items: [
4687
{ text: 'Intro to C', link: '/pages/coding/cs61c/src/c/Intro to C' },
4788
{ text: 'Generics', link: '/pages/coding/cs61c/src/c/Generics' },
@@ -51,6 +92,7 @@ export const sidebar: DefaultTheme.Sidebar = [
5192
},
5293
{
5394
text: 'RISC-V',
95+
collapsed: true,
5496
items: [
5597
{ text: 'Intro to RISC-V', link: '/pages/coding/cs61c/src/riscv/Intro to RISC-V' },
5698
{ text: 'RISC-V Instructions', link: '/pages/coding/cs61c/src/riscv/RISC-V Instructions' },
@@ -61,6 +103,7 @@ export const sidebar: DefaultTheme.Sidebar = [
61103
},
62104
{
63105
text: 'CPU',
106+
collapsed: true,
64107
items: [
65108
{ text: 'Design Hierarchy', link: '/pages/coding/cs61c/src/cpu/Design Hierarchy' },
66109
{ text: 'Combinational Logic', link: '/pages/coding/cs61c/src/cpu/Combinational Logic' },
@@ -75,6 +118,7 @@ export const sidebar: DefaultTheme.Sidebar = [
75118
},
76119
{
77120
text: 'Performance',
121+
collapsed: true,
78122
items: [
79123
{ text: 'Caches', link: '/pages/coding/cs61c/src/performance/Caches' },
80124
{ text: 'Data-Level Parallelism (DLP)', link: '/pages/coding/cs61c/src/performance/Data-Level Parallelism (DLP)' },
221 KB
Loading

pages/coding/cs61a/info.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CS61A: Structure and Interpretation of Computer Programs
2+
===
3+
Ahh... This is where this all begins.
4+
5+
It was late night at 2024-01-16 (Yes I literally just `stat` my files), when I took the first step into this course, and all my journey of programming begins.
6+
7+
Look back on these notes, I'm surprised to remember most of the contents apart from the code are summarized with my own opinions and reflections over the materials given by Prof. DeNero, which is quite praiseworthy due to the fact that during these days I prefer reading over the text, and just yanking and pasting them all over into my notes.
8+
9+
I rarely approach these notes today. Partly because they're already known, and partly because I'm busy and lazy. There are some new stuff I've rediscovered when I'm filtering through them, like the keyword `nonlocal` and function decorator `@`. I might need to come back to these later on when I get my hands on some backend projects...
10+
11+
I stopped, as the new semester approached, shortly after I finished the project 3 "Scheme". (This is why I always joke about '学期' (semester) to be '学间期' (somewhere in the middle of learning) since I have no control over my learning during these times.) It is a pity that I didn't leave any design info or docs other than the code itself, which could have been a good material to reflect on. I had also done it way too fast, for the fear that I might not be able to finish it. The price is, I didn't remember a single byte from it.
12+
13+
It is fair enough to say, "You've came across so much burdens during the semester, so forgetfulness is just normal", but it did remind me of the importance of **writing design docs and reflections**. (Though you didn't write it again in CS61C Project 1, 2.)
14+
15+
The missing parts are SQLs. Initially I thought it was not a big deal. This didn't strike me until I suddenly become NIMO's WebDev, and need to take care of all of those frontend, backend and stuff. I will come back to them later.
16+
17+
***
18+
:::info Timespan
19+
2024-01 - 2024-02
20+
:::
21+
***
22+
23+
Oh, by the way, in the meantime I took another one called SysAdmin Decal, for learning Linux usage and management, git, apt, and fun. Unfortunately I didn't leave text materials behind. Great course though, I'll recommend all those who want to at least know about Linux to take a look at this course.
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
tags:
3+
- CS
4+
- Python
5+
- CS61A
6+
---
7+
Linked List Class
8+
===
9+
```python
10+
class Link:
11+
"""A linked list.
12+
13+
>>> s = Link(1)
14+
>>> s.first
15+
1
16+
>>> s.rest is Link.empty
17+
True
18+
>>> s = Link(2, Link(3, Link(4)))
19+
>>> s.first = 5
20+
>>> s.rest.first = 6
21+
>>> s.rest.rest = Link.empty
22+
>>> s # Displays the contents of repr(s)
23+
Link(5, Link(6))
24+
>>> s.rest = Link(7, Link(Link(8, Link(9))))
25+
>>> s
26+
Link(5, Link(7, Link(Link(8, Link(9)))))
27+
>>> print(s) # Prints str(s)
28+
<5 7 <8 9>>
29+
"""
30+
empty = ()
31+
32+
def __init__(self, first, rest=empty):
33+
assert rest is Link.empty or isinstance(rest, Link)
34+
self.first = first
35+
self.rest = rest
36+
37+
def __repr__(self):
38+
if self.rest is not Link.empty:
39+
rest_repr = ', ' + repr(self.rest)
40+
else:
41+
rest_repr = ''
42+
return 'Link(' + repr(self.first) + rest_repr + ')'
43+
44+
def __str__(self):
45+
string = '<'
46+
while self.rest is not Link.empty:
47+
string += str(self.first) + ' '
48+
self = self.rest
49+
return string + str(self.first) + '>'
50+
```
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
tags:
3+
- CS
4+
- Python
5+
- CS61A
6+
---
7+
Linked Lists
8+
===
9+
A linked list is either empty or a first value and the rest of the linked list.
10+
```mermaid
11+
%%{init: {"flowchart": {"defaultRenderer": "elk"}} }%%
12+
graph LR
13+
subgraph 1
14+
direction LR
15+
A[Data] -.-o B((Rest))
16+
end
17+
B --> 2
18+
subgraph 2
19+
direction LR
20+
C[Data] -.-o D((Rest))
21+
end
22+
D --> 3
23+
subgraph 3
24+
direction LR
25+
E[Data] -.-o F((null))
26+
end
27+
```
28+
## Linked List Class
29+
We use object system to implement linked lists in Python
30+
```python
31+
class Link:
32+
empty = ()
33+
34+
def __init__(self, first, rest=empty):
35+
assert rest is Link.empty or isinstance(rest, Link)
36+
self.first = first
37+
self.rest = rest
38+
def __getitem__(self, i):
39+
if i == 0:
40+
return self.first
41+
else:
42+
return self.rest[i-1]
43+
def __len__(self):
44+
return 1 + len(self.rest)
45+
46+
def __repr__(self):
47+
if self.rest is self.empty:
48+
rest = ''
49+
else:
50+
rest = ', ' + repr(self.rest)
51+
return f'Link({self.first}{rest})'
52+
53+
54+
s = Link(3, Link(4, Link(5)))
55+
>>> def join_link(s, separator):
56+
    if s is Link.empty:
57+
    return ""
58+
    elif s.rest is Link.empty:
59+
    return str(s.first)
60+
    else:
61+
    return str(s.first) + separator + join_link(s.rest, separator)
62+
>>> join_link(s, ", ")
63+
'3, 4, 5'
64+
```
65+
66+
- For `__len__`, the base case is reached when self.rest evaluates to the empty tuple, `Link.empty`, which has a length of 0.
67+
## Processing
68+
```python
69+
def range_link(begin, end):
70+
"""Return a Link containing consecutive integers from start to end.
71+
72+
>>> range_lilnk(3, 6)
73+
Link(3, Link(4, Link(5)))
74+
"""
75+
if start >= end:
76+
return List.empty
77+
else:
78+
return Link(start, range_link(start+1, end))
79+
80+
81+
def map_link(f, s):
82+
"""Return a Link contains f(x) for each x in Link s.
83+
84+
>>> map_link(square, range_link(3, 6))
85+
Link(9, Link(16, Link(25)))
86+
"""
87+
if s is Link.empty:
88+
return s
89+
else:
90+
return Link(f(s.first), map_link(f, s.rest))
91+
92+
93+
def filter_link(f, s):
94+
"""Return a Link that contains only the elements of x of Links s for which f(x) is a true value.
95+
96+
>>> filter_link(odd, range_link(3, 6))
97+
Link(3, Link(5))
98+
"""
99+
if s is Link.empty:
100+
return s
101+
filtered_rest = filter_link(f, s.rest)
102+
if f(s.first):
103+
return Link(s.first, filtered_rest)
104+
else:
105+
return filtered_rest
106+
```
107+
108+
## Mutating
109+
Linked list object instance can be mutated through mutating `List.first` and `List.rest`.
110+
111+
The rest of a linked list can contain the linked list as a sub-list.
112+
Do not mutate directly the `List`, which might leads to recursive definition.
113+
```python
114+
s = Link(1, Link(2, Link(3)))
115+
s.first = 5
116+
t = s.rest
117+
t.rest = s # It is mutating s because t.rest is s.rest.rest
118+
```
119+
```mermaid
120+
%%{init: {"flowchart": {"defaultRenderer": "elk"}} }%%
121+
graph LR
122+
subgraph linked_1
123+
direction LR
124+
A[5] -.-o B((Rest))
125+
end
126+
B --> linked_2
127+
subgraph linked_2
128+
direction LR
129+
C[2] -.-o D((Rest))
130+
end
131+
D --> linked_1
132+
t --> linked_2
133+
s --> linked_1
134+
```
135+
To copy a linked list, use `Link(List.first, List.rest)`

0 commit comments

Comments
 (0)