当前位置: 首页 > news >正文

【CS61A 2024秋】Python入门课,全过程记录P7(Week13 Macros至完结)【完结撒花!】

文章目录

  • 关于
  • 新的问题
  • 更好的解决方案
  • Week13
    • Mon Macros
      • 阅读材料
      • Lab 11: Programs as Data, Macros
        • Q1: WWSD: Quasiquote
        • Q2: If Program
        • Q3: Exponential Powers
        • Q4: Repeat
    • Wed SQL
      • 阅读材料
      • Disc 11: Macros
        • Q1: Mystery Macro
        • Q2: Multiple Assignment
        • Q3: Switch
      • Optional Contest: Scheme Art
    • Fri Tables
      • 阅读材料
      • HW 10: SQL
        • Q1: By Parent Height
        • Q2: Size of Dogs
        • Q3: Sentences
        • Q4: Low Variance
  • Week14
    • Mon Aggregation
    • Wed No Lecture: Thanksgiving
    • Fri No Lecture: Thanksgiving
  • Week15
    • Mon Databases
      • 阅读材料
      • Lab 12: SQL
        • Q1: Room Sharing
        • Q2: Two Rooms
        • Q3: (OPTIONAL) Big Courses
        • Q4: (OPTIONAL) Seats Remaining
    • Wed Designing Functions
      • Discussion 12: SQL
        • Q1: Open Early
        • Q2: Study Session
        • Q3: Late Night Snack
        • Q4: Double Pizza
    • Fri Conclusion
      • 阅读材料
      • Homework 11: Finale
      • Scheme Gallery

关于

个人博客,里面偶尔更新,最近比较忙。发一些总结的帖子和思考。

江湖有缘相见🤝。如果读者想和我交个朋友可以加我好友(见主页or个人博客),共同学习。笔者是学生,课业还是比较繁重的,可能回复不及时。笔者也正在四处寻找一些可以兼职锻炼知识并且补贴一些生活的工作,如果读者需要一些详细的辅导,或者帮助完成一些简易的lab也可以找我,笔者还是学生,自以为才学有限,也没有高价的理由📖。

新的问题

在这里插入图片描述
可以发现,cs61a归档了,而这个网站是需要Berkeley账号的,还是没法登录。这时候笔者决定使用档案馆网站,去翻网页的归档了。虽然有点难受,但是还能用orz。

对了,textbook是可以直接访问的,在这里

更好的解决方案

我在GitHub上找到了cs61a 2024 fall的归档,这里给出连接link

Week13

Mon Macros

阅读材料

Quasiquotation是反引用,宏展开?

Lab 11: Programs as Data, Macros

Q1: WWSD: Quasiquote

问答题。略。

Q2: If Program

构造if程序。

(define (if-program condition if-true if-false)(list 'if condition if-true if-false)
)
Q3: Exponential Powers

难题啊,太晕了。反引号是给宏展开,列表内元素有逗号的后面的元素所在子列表会进行eval操作。

(define (pow-expr base exp)(cond ((= exp 0) 1)  ((even? exp) `(square ,(pow-expr base (/ exp 2))))  ; if exp is even, apply square(else `(* ,base ,(pow-expr base (- exp 1))))))  ; if exp is odd
Q4: Repeat

宏魔法。

(define-macro (repeat n expr)`(repeated-call ,n (lambda () ,expr))); Call zero-argument procedure f n times and return the final result.
(define (repeated-call n f)(if (= n 1)(f)(begin (f) (repeated-call (- n 1) f))))

Wed SQL

阅读材料

喜闻乐见SQL。
在这里插入图片描述
居然有ByteDance。
Structured Query Language (SQL)

Disc 11: Macros

Q1: Mystery Macro

神秘的宏。

(define-macro (mystery-macro expr old new)(mystery-helper expr old new))(define (mystery-helper e o n)(if (pair? e)(cons (mystery-helper (car e) o n) (mystery-helper (cdr e) o n))(if (eq? e o) n e)))

功能是把列表中的old替换成new。

Q2: Multiple Assignment

实现同时赋值。

(define-macro (assign sym1 sym2 expr1 expr2)`(begin(define ,sym1 ,expr1)(define ,sym2 ,expr2)))(assign x y (+ 1 1) 3)
(assign x y y x)
(expect x 3)
(expect y 2)

上面这样简单粗暴的写的。第一次x=2,y=3,第二次,x=3,y=3了。使用eval的话可能是因为优先级比较高。

(define-macro (assign sym1 sym2 expr1 expr2)`(begin(define ,sym1 ,expr1)(define ,sym2 ,(eval expr2))))(assign x y (+ 1 1) 3)
(assign x y y x)
(expect x 3)
(expect y 2)
Q3: Switch

宏版的Switch。宏真的抽象的黑魔法。这里map内要另外的反引号,引号map递归计算cases里面的元素。

(define-macro (switch expr cases)`(let ((val ,expr)),(cons'cond(map (lambda (case) (cons`(equal? val ,(car case))(cdr case)))cases))))

Optional Contest: Scheme Art

这个貌似是画图的,略了。

Fri Tables

阅读材料

讲解一些SQL知识。

HW 10: SQL

在这里插入图片描述
可以看到sqlite,sqlite是轻量级的数据库。据说大部分的手机APP都使用sqlite,包括微信。

Q1: By Parent Height

创建表格的方法第一次见,是通过UNION实现的。
现在是2025年了,写SQL语句对于AI来说轻而易举,只会CRUD可都得被淘汰。
首先找到有父母的狗,再对应上父母的体重,再根据父母体重排序。

-- All dogs with parents ordered by decreasing height of their parent
CREATE TABLE by_parent_height ASSELECT d.name AS dog_nameFROM dogs dJOIN parents p ON d.name = p.childJOIN dogs pd ON p.parent = pd.nameORDER BY pd.height DESC;
Q2: Size of Dogs

根据体重确定狗的类型。

-- The size of each dog
CREATE TABLE size_of_dogs ASSELECT d.name AS name, s.size AS sizeFROM dogs dJOIN sizes sON d.height > s.min AND d.height <= s.max;
Q3: Sentences

一堆查表连接。

-- [Optional] Filling out this helper table is recommended
CREATE TABLE siblings ASSELECT p1.child AS sibling1, p2.child AS sibling2FROM parents p1JOIN parents p2 ON p1.parent = p2.parentWHERE p1.child < p2.child;-- Sentences about siblings that are the same size
CREATE TABLE sentences ASSELECT "The two siblings, " || s1.name || " and " || s2.name || ", have the same size: " || ss1.size AS sentenceFROM siblingsJOIN dogs s1 ON siblings.sibling1 = s1.nameJOIN dogs s2 ON siblings.sibling2 = s2.nameJOIN size_of_dogs ss1 ON ss1.name = s1.nameJOIN size_of_dogs ss2 ON ss2.name = s2.nameWHERE ss1.size = ss2.size;
Q4: Low Variance

只有满足低方差类型的毛发种类的狗才输出。

-- Height range for each fur type where all of the heights differ by no more than 30% from the average height
CREATE TABLE low_variance ASSELECT fur, MAX(height) - MIN(height) AS height_rangeFROM dogsWHERE fur IN (SELECT d.furFROM dogs dJOIN (SELECT fur, AVG(height) AS avg_heightFROM dogsGROUP BY fur) AS avg_heights ON d.fur = avg_heights.furGROUP BY d.furHAVING MIN(d.height) >= 0.7 * avg_heights.avg_heightAND MAX(d.height) <= 1.3 * avg_heights.avg_height)GROUP BY fur;

Week14

Mon Aggregation

讲了一些关于SQL的语法。

Wed No Lecture: Thanksgiving

Fri No Lecture: Thanksgiving

Week15

Mon Databases

阅读材料

讲了一些其他的语法。

Lab 12: SQL

Q1: Room Sharing

找到每门课和别的课程共用的房间数。

CREATE TABLE sharing ASSELECT a.course, COUNT(DISTINCT a.hall) AS sharedFROM finals AS a, finals AS bWHERE a.course <> b.course and a.hall == b.hall GROUP BY a.course;
Q2: Two Rooms

case语句。

CREATE TABLE pairs AS
SELECT CASE WHEN a.room < b.room THEN a.room || ' and ' || b.roomELSE b.room || ' and ' || a.roomEND || ' together have ' || (a.seats + b.seats) || ' seats' AS rooms
FROM sizes AS a, sizes AS b
WHERE a.room < b.room AND a.seats + b.seats >= 1000
ORDER BY a.seats + b.seats DESC;
Q3: (OPTIONAL) Big Courses

连接+聚合。

CREATE TABLE big AS
SELECT f.course
FROM finals f
JOIN sizes s ON f.hall = s.room
GROUP BY f.course
HAVING SUM(s.seats) >= 1000;
Q4: (OPTIONAL) Seats Remaining

作个减法。

CREATE TABLE remaining AS
SELECT f.course,SUM(s.seats) - MAX(s.seats) AS remaining
FROM finals f
JOIN sizes s ON f.hall = s.room
GROUP BY f.course;

Wed Designing Functions

Discussion 12: SQL

Q1: Open Early

找到在13点前开门的店。

-- Pizza places that open before 1pm in alphabetical order
SELECT p.name as name 
FROM pizzas p
WHERE p.open < 13
ORDER BY name DESC
;
Q2: Study Session

都是晚上关门,直接和14减。

-- Pizza places and the duration of a study break that ends at 14 o'clock
SELECT name,MAX(14 - open, 0) AS duration
FROM pizzas
ORDER BY duration DESC;
Q3: Late Night Snack

比较ez。

-- Pizza places that are open for late-night-snack time and when they closeSELECT p.name || " closes at " || p.close AS statusFROM pizzas p, meals mWHERE m.time <= p.close and m.meal = 'snack';
Q4: Double Pizza

多表。

-- Two meals at the same place
SELECT m1.meal AS first, m2.meal AS second, p.name
FROM meals m1, meals m2, pizzas p
WHERE m2.time - m1.time > 6AND p.open <= m1.time AND p.close >= m1.timeAND p.open <= m2.time AND p.close >= m2.time;

Fri Conclusion

阅读材料

复习与总结。

Homework 11: Finale

学评教?

Scheme Gallery

上面scheme画画的作品集。


http://www.mrgr.cn/news/90409.html

相关文章:

  • [笔记.AI]Deepseek-R1 各参数版本的蒸馏模型对比
  • Spring Boot 配置 Mybatis 读写分离
  • jenkins备份还原配置文件
  • 基于STM32的智能鱼缸水质净化系统设计
  • C++引用深度详解
  • Flutter_学习记录_基本组件的使用记录_2
  • 数据结构与算法-单链表
  • UnrealEngine开发无人机飞行模拟软件的手柄如何选择
  • 伺服使能的含义解析
  • ubuntu20.04+ROS+Gazebo+px4+QGC+MAVROS
  • Packer 手动修复安装腾讯云插件
  • unity 安装Entities
  • 深入理解Java对接DeepSeek
  • 【Python深入浅出㉗】Python3正则表达式:开启高效字符串处理大门
  • 数据结构与算法之排序算法-快速排序(分治)
  • Linux内核实时机制x - 实时性之中断响应优化
  • 1.【线性代数】——方程组的几何解释
  • DeepSeek投喂数据(训练AI)
  • 如何评估云原生GenAI应用开发中的安全风险(下)
  • 解锁设计模式:代理模式的多面解析与实战