当前位置: 首页 > 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 AS
  SELECT d.name AS dog_name
  FROM dogs d
  JOIN parents p ON d.name = p.child
  JOIN dogs pd ON p.parent = pd.name
  ORDER BY pd.height DESC;
Q2: Size of Dogs

根据体重确定狗的类型。

-- The size of each dog
CREATE TABLE size_of_dogs AS
  SELECT d.name AS name, s.size AS size
  FROM dogs d
  JOIN sizes s
  ON d.height > s.min AND d.height <= s.max;
Q3: Sentences

一堆查表连接。

-- [Optional] Filling out this helper table is recommended
CREATE TABLE siblings AS
  SELECT p1.child AS sibling1, p2.child AS sibling2
  FROM parents p1
  JOIN parents p2 ON p1.parent = p2.parent
  WHERE p1.child < p2.child;

-- Sentences about siblings that are the same size
CREATE TABLE sentences AS
  SELECT "The two siblings, " || s1.name || " and " || s2.name || ", have the same size: " || ss1.size AS sentence
  FROM siblings
  JOIN dogs s1 ON siblings.sibling1 = s1.name
  JOIN dogs s2 ON siblings.sibling2 = s2.name
  JOIN size_of_dogs ss1 ON ss1.name = s1.name
  JOIN size_of_dogs ss2 ON ss2.name = s2.name
  WHERE 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 AS
  SELECT fur, MAX(height) - MIN(height) AS height_range
  FROM dogs
  WHERE fur IN (
    SELECT d.fur
    FROM dogs d
    JOIN (
      SELECT fur, AVG(height) AS avg_height
      FROM dogs
      GROUP BY fur
    ) AS avg_heights ON d.fur = avg_heights.fur
    GROUP BY d.fur
    HAVING MIN(d.height) >= 0.7 * avg_heights.avg_height
       AND 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 AS
  SELECT a.course, COUNT(DISTINCT a.hall) AS shared
  FROM finals AS a, finals AS b
  WHERE 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.room
        ELSE b.room || ' and ' || a.room
    END || ' 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 close
  SELECT p.name || " closes at " || p.close AS status
  FROM pizzas p, meals m
  WHERE 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 > 6
  AND p.open <= m1.time AND p.close >= m1.time
  AND p.open <= m2.time AND p.close >= m2.time;


Fri Conclusion

阅读材料

复习与总结。

Homework 11: Finale

学评教?

Scheme Gallery

上面scheme画画的作品集。

http://www.dtcms.com/a/17221.html

相关文章:

  • 新一代高性能无线传输模块M-GATEWAY3
  • 玩转观察者模式
  • 【Linux】进程间关系与守护进程
  • MySQL 记录
  • c# textbox 设置不获取光标
  • 微信小程序 - 组件和样式
  • Django简介
  • 避雷,Ubuntu通过ollama本地化部署deepseek,open-webui前端显示
  • 链表(典型算法思想)—— OJ例题算法解析思路
  • android启动整体流程
  • Java面试——Tomcat
  • DeepSeek各版本说明与优缺点分析
  • ASP.NET Core 面试宝典【刷题系列】
  • 【大语言模型】在大语言模型中,user、assistant、system 三种角色的定位和功能有何不同。
  • HCIA项目实践--RIP的拓展配置
  • 通过命令行运行py文件与通过ide运行py文件,对文件中模块的引用方式的影响
  • 反射概率以及一些基本API的使用
  • BUU38 [RoarCTF 2019]Easy Java1
  • DeePseek结合PS!批量处理图片的方法教程
  • AnythingLLM打造私有知识库
  • 微软AutoGen高级功能——Serializing Components
  • 【原创】springboot+vue考试考场座位安排管理系统设计与实现
  • 【架构设计】微服务架构模型:常见模型的对比和分析
  • 更高效实用 vscode 的常用设置
  • 全排列(力扣46)
  • RFM模型-数据清洗
  • 基于mediapipe深度学习的手势数字识别系统python源码
  • 20250214实测飞凌的OK3588-C_Linux5.10.209+Qt5.15.10_用户资料_R1的USB2.0的速度为29.0 MB/s
  • 香港VPS服务器如何排查和修复 MySQL 数据库连接失败的问题
  • 从图像中提取的每行数字作为一张完整的图片,而不是每个数字单独成为一张图片