在算法设计中，我们先后追求以下两个层面的目标。

\n- \n
**找到问题解法**：算法需要在规定的输入范围内可靠地求得问题的正确解。 \n**寻求最优解法**：同一个问题可能存在多种解法，我们希望找到尽可能高效的算法。 \n

也就是说，在能够解决问题的前提下，算法效率已成为衡量算法优劣的主要评价指标，它包括以下两个维度。

\n- \n
**时间效率**：算法运行时间的长短。 \n**空间效率**：算法占用内存空间的大小。 \n

简而言之，**我们的目标是设计“既快又省”的数据结构与算法**。而有效地评估算法效率至关重要，因为只有这样，我们才能将各种算法进行对比，进而指导算法设计与优化过程。

效率评估方法主要分为两种：实际测试、理论估算。

\n假设我们现在有算法 `A`

和算法 `B`

，它们都能解决同一问题，现在需要对比这两个算法的效率。最直接的方法是找一台计算机，运行这两个算法，并监控记录它们的运行时间和内存占用情况。这种评估方式能够反映真实情况，但也存在较大的局限性。

一方面，**难以排除测试环境的干扰因素**。硬件配置会影响算法的性能表现。比如一个算法的并行度较高，那么它就更适合在多核 CPU 上运行，一个算法的内存操作密集，那么它在高性能内存上的表现就会更好。也就是说，算法在不同的机器上的测试结果可能是不一致的。这意味着我们需要在各种机器上进行测试，统计平均效率，而这是不现实的。

另一方面，**展开完整测试非常耗费资源**。随着输入数据量的变化，算法会表现出不同的效率。例如，在输入数据量较小时，算法 `A`

的运行时间比算法 `B`

短；而在输入数据量较大时，测试结果可能恰恰相反。因此，为了得到有说服力的结论，我们需要测试各种规模的输入数据，而这需要耗费大量的计算资源。

由于实际测试具有较大的局限性，因此我们可以考虑仅通过一些计算来评估算法的效率。这种估算方法被称为渐近复杂度分析（asymptotic complexity analysis），简称复杂度分析。

\n复杂度分析能够体现算法运行所需的时间和空间资源与输入数据大小之间的关系。**它描述了随着输入数据大小的增加，算法执行所需时间和空间的增长趋势**。这个定义有些拗口，我们可以将其分为三个重点来理解。

- \n
- “时间和空间资源”分别对应时间复杂度（time complexity）和空间复杂度（space complexity）。 \n
- “随着输入数据大小的增加”意味着复杂度反映了算法运行效率与输入数据体量之间的关系。 \n
- “时间和空间的增长趋势”表示复杂度分析关注的不是运行时间或占用空间的具体值，而是时间或空间增长的“快慢”。 \n

**复杂度分析克服了实际测试方法的弊端**，体现在以下几个方面。

- \n
- 它无需实际运行代码，更加绿色节能。 \n
- 它独立于测试环境，分析结果适用于所有运行平台。 \n
- 它可以体现不同数据量下的算法效率，尤其是在大数据量下的算法性能。 \n

\n

\n提示

\n如果你仍对复杂度的概念感到困惑，无须担心，我们会在后续章节中详细介绍。

\n复杂度分析为我们提供了一把评估算法效率的“标尺”，使我们可以衡量执行某个算法所需的时间和空间资源，对比不同算法之间的效率。

\n复杂度是个数学概念，对于初学者可能比较抽象，学习难度相对较高。从这个角度看，复杂度分析可能不太适合作为最先介绍的内容。然而，当我们讨论某个数据结构或算法的特点时，难以避免要分析其运行速度和空间使用情况。

\n综上所述，建议你在深入学习数据结构与算法之前，**先对复杂度分析建立初步的了解，以便能够完成简单算法的复杂度分析**。

在算法中，重复执行某个任务是很常见的，它与复杂度分析息息相关。因此，在介绍时间复杂度和空间复杂度之前，我们先来了解如何在程序中实现重复执行任务，即两种基本的程序控制结构：迭代、递归。

\n迭代（iteration）是一种重复执行某个任务的控制结构。在迭代中，程序会在满足一定的条件下重复执行某段代码，直到这个条件不再满足。

\n`for`

循环是最常见的迭代形式之一，**适合在预先知道迭代次数时使用**。

以下函数基于 `for`

循环实现了求和 $1+2+\cdots +n$1+2+⋯+n，求和结果使用变量 `res`

记录。需要注意的是，Python 中 `range(a, b)`

对应的区间是“左闭右开”的，对应的遍历范围为 $a,a+1,\dots ,b-1$a,a+1,…,b−1：

`def for_loop(n: int) -> int:\n \"\"\"for 循环\"\"\"\n res = 0\n # 循环求和 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n res += i\n return res`

\n图 2-1 是该求和函数的流程框图。

\n\n此求和函数的操作数量与输入数据大小 $n$n 成正比，或者说成“线性关系”。实际上，**时间复杂度描述的就是这个“线性关系”**。相关内容将会在下一节中详细介绍。

与 `for`

循环类似，`while`

循环也是一种实现迭代的方法。在 `while`

循环中，程序每轮都会先检查条件，如果条件为真，则继续执行，否则就结束循环。

下面我们用 `while`

循环来实现求和 $1+2+\cdots +n$1+2+⋯+n:

`def while_loop(n: int) -> int:\n \"\"\"while 循环\"\"\"\n res = 0\n i = 1 # 初始化条件变量\n # 循环求和 1, 2, ..., n-1, n\n while i <= n:\n res += i\n i += 1 # 更新条件变量\n return res`

\n** while 循环比 for 循环的自由度更高**。在

`while`

循环中，我们可以自由地设计条件变量的初始化和更新步骤。例如在以下代码中，条件变量 i 每轮进行两次更新，这种情况就不太方便用 `for`

循环实现：

`def while_loop_ii(n: int) -> int:\n \"\"\"while 循环（两次更新）\"\"\"\n res = 0\n i = 1 # 初始化条件变量\n # 循环求和 1, 4, 10, ...\n while i <= n:\n res += i\n # 更新条件变量\n i += 1\n i *= 2\n return res`

\n总的来说，** for 循环的代码更加紧凑，while 循环更加灵活**，两者都可以实现迭代结构。选择使用哪一个应该根据特定问题的需求来决定。

我们可以在一个循环结构内嵌套另一个循环结构，下面以 `for`

循环为例：

`def nested_for_loop(n: int) -> str:\n \"\"\"双层 for 循环\"\"\"\n res = \"\"\n # 循环 i = 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n # 循环 j = 1, 2, ..., n-1, n\n for j in range(1, n + 1):\n res += f\"({i}, {j}), \"\n return res`

\n图 2-2 是该嵌套循环的流程框图。

\n\n在这种情况下，函数的操作数量与 ${n}^{2}$n2 成正比，或者说算法运行时间和输入数据大小 $n$n 成“平方关系”。

\n我们可以继续添加嵌套循环，每一次嵌套都是一次“升维”，将会使时间复杂度提高至“立方关系”“四次方关系”，以此类推。

\n递归（recursion）是一种算法策略，通过函数调用自身来解决问题。它主要包含两个阶段。

\n- \n
**递**：程序不断深入地调用自身，通常传入更小或更简化的参数，直到达到“终止条件”。 \n**归**：触发“终止条件”后，程序从最深层的递归函数开始逐层返回，汇聚每一层的结果。 \n

而从实现的角度看，递归代码主要包含三个要素。

\n- \n
**终止条件**：用于决定什么时候由“递”转“归”。 \n**递归调用**：对应“递”，函数调用自身，通常输入更小或更简化的参数。 \n**返回结果**：对应“归”，将当前递归层级的结果返回至上一层。 \n

观察以下代码，我们只需调用函数 `recur(n)`

，就可以完成 $1+2+\cdots +n$1+2+⋯+n 的计算：

`def recur(n: int) -> int:\n \"\"\"递归\"\"\"\n # 终止条件\n if n == 1:\n return 1\n # 递：递归调用\n res = recur(n - 1)\n # 归：返回结果\n return n + res`

\n图 2-3 展示了该函数的递归过程。

\n\n虽然从计算角度看，迭代与递归可以得到相同的结果，**但它们代表了两种完全不同的思考和解决问题的范式**。

- \n
**迭代**：“自下而上”地解决问题。从最基础的步骤开始，然后不断重复或累加这些步骤，直到任务完成。 \n**递归**：“自上而下”地解决问题。将原问题分解为更小的子问题，这些子问题和原问题具有相同的形式。接下来将子问题继续分解为更小的子问题，直到基本情况时停止（基本情况的解是已知的）。 \n

以上述求和函数为例，设问题 $f\left(n\right)=1+2+\cdots +n$f(n)=1+2+⋯+n。

\n- \n
**迭代**：在循环中模拟求和过程，从 $1$1 遍历到 $n$n，每轮执行求和操作，即可求得 $f\left(n\right)$f(n)。 \n**递归**：将问题分解为子问题 $f\left(n\right)=n+f(n-1)$f(n)=n+f(n−1)，不断（递归地）分解下去，直至基本情况 $f\left(1\right)=1$f(1)=1 时终止。 \n

递归函数每次调用自身时，系统都会为新开启的函数分配内存，以存储局部变量、调用地址和其他信息等。这将导致两方面的结果。

\n- \n
- 函数的上下文数据都存储在称为“栈帧空间”的内存区域中，直至函数返回后才会被释放。因此，
**递归通常比迭代更加耗费内存空间**。 \n - 递归调用函数会产生额外的开销。
**因此递归通常比循环的时间效率更低**。 \n

如图 2-4 所示，在触发终止条件前，同时存在 $n$n 个未返回的递归函数，**递归深度为 $n$n** 。

在实际中，编程语言允许的递归深度通常是有限的，过深的递归可能导致栈溢出错误。

\n有趣的是，**如果函数在返回前的最后一步才进行递归调用**，则该函数可以被编译器或解释器优化，使其在空间效率上与迭代相当。这种情况被称为尾递归（tail recursion）。

- \n
**普通递归**：当函数返回到上一层级的函数后，需要继续执行代码，因此系统需要保存上一层调用的上下文。 \n**尾递归**：递归调用是函数返回前的最后一个操作，这意味着函数返回到上一层级后，无须继续执行其他操作，因此系统无须保存上一层函数的上下文。 \n

以计算 $1+2+\cdots +n$1+2+⋯+n 为例，我们可以将结果变量 `res`

设为函数参数，从而实现尾递归：

`def tail_recur(n, res):\n \"\"\"尾递归\"\"\"\n # 终止条件\n if n == 0:\n return res\n # 尾递归调用\n return tail_recur(n - 1, res + n)`

\n尾递归的执行过程如图 2-5 所示。对比普通递归和尾递归，两者的求和操作的执行点是不同的。

\n- \n
**普通递归**：求和操作是在“归”的过程中执行的，每层返回后都要再执行一次求和操作。 \n**尾递归**：求和操作是在“递”的过程中执行的，“归”的过程只需层层返回。 \n

\n

\n提示

\n请注意，许多编译器或解释器并不支持尾递归优化。例如，Python 默认不支持尾递归优化，因此即使函数是尾递归形式，仍然可能会遇到栈溢出问题。

\n当处理与“分治”相关的算法问题时，递归往往比迭代的思路更加直观、代码更加易读。以“斐波那契数列”为例。

\n\n

\n相关信息

\n给定一个斐波那契数列 $0,1,1,2,3,5,8,13,\dots $0,1,1,2,3,5,8,13,…，求该数列的第 $n$n 个数字。

\n设斐波那契数列的第 $n$n 个数字为 $f\left(n\right)$f(n)，易得两个结论。

\n- \n
- 数列的前两个数字为 $f\left(1\right)=0$f(1)=0 和 $f\left(2\right)=1$f(2)=1。 \n
- 数列中的每个数字是前两个数字的和，即 $f\left(n\right)=f(n-1)+f(n-2)$f(n)=f(n−1)+f(n−2)。 \n

按照递推关系进行递归调用，将前两个数字作为终止条件，便可写出递归代码。调用 `fib(n)`

即可得到斐波那契数列的第 $n$n 个数字：

`def fib(n: int) -> int:\n \"\"\"斐波那契数列：递归\"\"\"\n # 终止条件 f(1) = 0, f(2) = 1\n if n == 1 or n == 2:\n return n - 1\n # 递归调用 f(n) = f(n-1) + f(n-2)\n res = fib(n - 1) + fib(n - 2)\n # 返回结果 f(n)\n return res`

\n观察以上代码，我们在函数内递归调用了两个函数，**这意味着从一个调用产生了两个调用分支**。如图 2-6 所示，这样不断递归调用下去，最终将产生一棵层数为 $n$n 的递归树（recursion tree）。

从本质上看，递归体现了“将问题分解为更小子问题”的思维范式，这种分治策略至关重要。

\n- \n
- 从算法角度看，搜索、排序、回溯、分治、动态规划等许多重要算法策略直接或间接地应用了这种思维方式。 \n
- 从数据结构角度看，递归天然适合处理链表、树和图的相关问题，因为它们非常适合用分治思想进行分析。 \n

总结以上内容，如表 2-1 所示，迭代和递归在实现、性能和适用性上有所不同。

\n\n

\n表 2-1 迭代与递归特点对比

\n| | 迭代 | 递归 |

\n| :

CUH500CMD is assessed by coursework and exam.

\nFor the coursework, you need to submit a portfolio of work you’ve completed each week in the labs (and in your own time outside the labs).

\nEach week, there will be up to 4 types of task:

\n| Non-assessed Standard | Assessed Standard |

\n|

`knitr::opts_chunk$set(echo = TRUE)`

\n- \n
- \n
Save the .Rmd/.ipnyb file in your working directory - the same

\n

\ndirectory where you will download the data files into. \n - \n
Read the question and create the code necessary within the code

\n

\nchunk section immediately below each question. \n - \n
Type your answer to the questions in the text block provided

\n

\nimmediately after the response prompt. \n - \n
Once you've finished answering all questions, knit this file and

\n

\nsubmit the knitted file*as HTML*on Canvas.

\n`* Make sure to start submission of the exam at least 10 minutes before the end of the exam time. It is your responsibility to keep track of your time and submit before the time limit. \n\n* If you are unable to knit your file as HTML for whatever reason, you may upload your Rmd/ipynb/PDF/Word file instead. However, you will be penalized 15%.\n\n* If you are unable to upload your exam file for whatever reason, you may IMMEDIATELY attach the file to the exam page as a comment via Grades-> Midterm Exam 2 - Open Book Section - Part 2 -> Comment box. \n\n* Note that you will be penalized 15% (or more) if the submission is made within 5 minutes after the exam time has expired and a higher penalty if more than 5 minutes. Furthermore, you will receive zero points if the submission is made after 15 minutes of the exam time expiring. We will not allow later submissions or re-taking of the exam.\n\n* If you upload your file after the exam closes, let the instructors know via a private Piazza post. Please DON'T attach the exam file via a private Piazza post to the instructors since you could compromise the exam process. Any submission received via Piazza will not be considered.\n\n*#Commented out code will be graded for partial credit and the submitted file must be HTML`

\n

**Ready? Let's begin.**

In this exam, you will be considering various attributes to predict

\n**employee turnover** (whether an employee will leave or stay) based on

\nvarious factors:

- \n
- \n

\n**Commute Mode:**The mode of transportation the employee uses to

\ncommute to work.(\"Public Transport,\" \"Car,\" \"Bike,\" \"Walk\")

\n**(Qualitative**

\nvariable) \n - \n

\n**Workplace Flexibility:**The level of flexibility the employee has

\nin their work location. (\"Remote,\" \"Hybrid,\" \"On-Site\")

\n**(Qualitative variable)**\n - \n

\n**Team Dynamics:**The level of collaboration within the employee's

\nteam. (\"High Collaboration,\" \"Low Collaboration\")**(Qualitative**

\nvariable) \n - \n

\n**Office Location:**The geographical location of the office where

\nthe employee works. (\"City Center,\" \"Suburb,\" \"Rural Area\")

\n**(Qualitative variable)**\n - \n

\n**Health Benefits:**The type of health benefits provided to the

\nemployee by the company. (\"Full Coverage,\" \"Partial Coverage,\"

\n\"None\")**(Qualitative variable)**\n - \n

\n**Satisfaction Score:**A rating of the employee’s overall

\nsatisfaction with their job.(1-10)**(Quantitative variable)**\n - \n

\n**Monthly Working Hours:**The total number of hours worked by the

\nemployee in a typical month. (120-250)**(Quantitative variable)**\n - \n

\n**Years With Company:**The number of years the employee has worked

\nat the company. (0.5-35 years)**(Quantitative variable)**\n - \n

\n**Number of Trainings Attended:**The number of professional

\ndevelopment or training sessions the employee has participated in.

\n(0-10)**(Quantitative variable)**\n - \n

\n**Salary Increase Percentage:**The percentage increase in the

\nemployee's salary over the past year.(0%-20%)**(Quantitative**

\nvariable) \n - \n

\n**Turnover:**Indicates whether the employee left the company. \"1\"

\nfor Turnover (employee left), \"0\" for No Turnover (employee stayed)

\n**(Response variable)**. \n

`#This seed has been set to 100 to ensure results are reproducible. DO NOT CHANGE THIS SEED\nset.seed(100)\n\n#Read the csv file\nemployee_turnover = read.csv(\"employee_turnover_prediction.csv\", header=TRUE, na.strings = \"\")\n\n#Remove any potential trailing white space from column names\nnames(employee_turnover) <- trimws(names(employee_turnover), which = \"right\")\n\nemployee_turnover$Commute_Mode=as.factor(employee_turnover$Commute_Mode)\nemployee_turnover$Workplace_Flexibility=as.factor(employee_turnover$Workplace_Flexibility)\nemployee_turnover$Team_Dynamics=as.factor(employee_turnover$Team_Dynamics)\nemployee_turnover$Office_Location=as.factor(employee_turnover$Office_Location)\nemployee_turnover$Health_Benefits=as.factor(employee_turnover$Health_Benefits)\nemployee_turnover$Turnover=factor(employee_turnover$Turnover, levels = c(0, 1))\n\n#Dividing the dataset into training and testing datasets\ntestRows = sample(nrow(employee_turnover),0.2*nrow(employee_turnover))\ntestData = employee_turnover[testRows, ]\ntrainData = employee_turnover[-testRows, ]\nrow.names(trainData) <- NULL\nhead(trainData) #display train data`

\n**Use the FULL dataset \"employee_turnover\" for Question 1**

**1a) (2 points) What is the median \"Monthly_Working_Hours\" for\nemployees across different workplaces?**

* Note***: Answer must be grouped by \"Workplace_Flexibility\".**

`#Code\n# 计算每种工作灵活性下的月工作小时中位数\nmedian_working_hours <- aggregate(Monthly_Working_Hours ~ Workplace_Flexibility, \n data = employee_turnover, \n FUN = median)\nprint(median_working_hours)`

\n**1b) (2 points) What is the proportion of employees who stayed with the\ncompany (i.e., did not leave) for each type of \"Health_Benefits\"?**

* Note***: As an example, the proportion of employees who stayed with

\nthe company for Full Coverage equals the number of employees with full

\ncoverage who stayed divided by the number of employees with full

\ncoverage.**

`#Code\n# 计算各健康福利下员工留存比例\nstay_proportion <- aggregate(Turnover ~ Health_Benefits, \n data = employee_turnover, \n FUN = function(x) mean(x == 0))\nprint(stay_proportion)`

\n**1c) (2 points) Print the rows with the highest\n\"Salary_Increase_Percentage\". Identify the qualitative variable\nresponses that are the same between the rows with the highest\n\"Salary_Increase_Percentage\"?**

`#Code\n# 找出 Salary_Increase_Percentage 最高的行\nhighest_salary_increase <- employee_turnover[employee_turnover$Salary_Increase_Percentage == max(employee_turnover$Salary_Increase_Percentage), ]\nprint(highest_salary_increase)`

\n**Answer to question 1c**:

**1d) (5 points) Create boxplots and interpret each plot for the\nfollowing predictors against the response variable (Turnover).**

**i) Monthly_Working_Hours**

**ii) Years_With_Company**

**In general, using boxplots, can we make statements about statistical\nsignificance of the differences between the group means? How can we\ninfer if the group means are statistically significantly different from\neach other?**

`#Code\n# 月工作小时数和公司年限的箱线图\nboxplot(Monthly_Working_Hours ~ Turnover, data = employee_turnover, main = \"Monthly Working Hours vs Turnover\")\nboxplot(Years_With_Company ~ Turnover, data = employee_turnover, main = \"Years with Company vs Turnover\")`

\n**Answer to question 1d**:

**2a) (6 points) In this question, you will fit a reduced model:**

**i) Using the dataset “trainData”, create a logistic regression model\n(call it \" model1\") using \"Turnover\" as the response variable, and\nOffice_Location, Health_Benefits, Monthly_Working_Hours as the predictor\nvariables. (2 points)**

`# 创建逻辑回归模型\nmodel1 <- glm(Turnover ~ Office_Location + Health_Benefits + Monthly_Working_Hours, \n data = trainData, family = binomial)\nsummary(model1)`

\n**ii) (2 points) Using \"model1\", interpret the coefficients of the\nfollowing predictors below with respect to BOTH the log-odds of turnover\nand the odds of turnover.**

**1) Monthly working hours 2) Health_BenefitsPartial Coverage.**

**Answer to Question 2a(ii):**

**iii) Is the model with all the predictors better at predicting the\nresponse than a model with just an intercept term? Explain your\nreasoning. (2 points)**

* Note***: You can use only the summary output for model1 to answer

\nthis question.**

**Answer to Question 2a(iii):**

**2b) (4 points) In this question, you will fit the full model:**

**i) (2 points) Using the \"trainData\" dataset, create a logistic\nregression model using Turnover as response variable and all variables\nin \"trainData\" as predictors (call it model2) and display the summary of\nmodel2.**

`# 创建包含所有变量的完整模型 model2\nmodel2 <- glm(Turnover ~ ., data = trainData, family = binomial)\nsummary(model2)`

\n**ii)(2 points) Compare the full logistic regression model (model2) from\nQuestion (2bi) against the reduced model (model1) from Question (2ai).\nWhat can you conclude from the results of this comparison using a\nsignificance level of alpha=0.01?**

`# 比较 model2 和 model1\nanova(model1, model2, test = \"Chisq\")`

\n**Response to question (2b(ii))**:

**2c) (2 points) Perform a test for overall regression of the logistic\nregression \"model2\", using a significance level of alpha=0.05. Does the\noverall regression have explanatory power? Provide interpretation of the\ntest.**

`# 使用显著性水平alpha=0.05进行整体回归检验\n# 提取模型2的残差偏差和自由度\nmodel2_deviance <- model2$deviance\nmodel2_df <- model2$df.residual\n\n# 使用无解释变量的模型的残差偏差和自由度\nnull_deviance <- model2$null.deviance\nnull_df <- model2$df.null\n\n# 计算卡方统计量\nchi_square_stat <- null_deviance - model2_deviance\nchi_square_df <- null_df - model2_df\n\n# p值计算\np_value <- pchisq(chi_square_stat, chi_square_df, lower.tail = FALSE)\nprint(paste(\"Chi-square Statistic:\", chi_square_stat))\nprint(paste(\"Degrees of Freedom:\", chi_square_df))\nprint(paste(\"P-value:\", p_value))\n\n# 判断是否具有解释力\nif (p_value < 0.05) {\n print(\"模型具有解释力\")\n} else {\n print(\"模型不具有解释力\")\n}`

\n**Response to question (2c)**:

`# 查看 model2 的回归结果摘要\nsummary(model2)`

\n**2d)(5 points) Using \"model2\", apply hypothesis testing for a\nregression coefficient at the 0.01 significance level.**

**i) (1 point) Is the coefficient of “Number_of_Trainings_Attended”\nstatistically significant?**

**ii) (1 point) State the Null and alternative hypotheses of the test.**

**iii) (1.5 points) Describe the approach we would use to determine the\nstatistical significance of the regression coefficient.**

**iv) (1.5 points) What is the sampling distribution that the test\nstatistic follows?**

**Response to question 2d(i):**

**Response to question 2d(ii):**

**Response to question 2d(iii):**

**Response to question 2d(iv):**

**3a) (4 points) Using the dataset \"trainData\", fit the following\nclassification models below using all the predictors in \"trainData\" and\n\"Turnover\" as the response variable.**

**i) Decision Tree Model (call it model3).**

**ii) Random Forest Model (call it model4).**

**Use metric = “Accuracy”, trControl = trainControl(method=“cv”,\nnumber=3) for both models. Display the summary of both models and state\nthe average accuracy for both resampled models.**

`# Decision Tree\n# 安装 randomForest 包\ninstall.packages(\"randomForest\")\n\n# 加载必要的包\nlibrary(caret)\nlibrary(randomForest)\n\n# 决策树模型\ncontrol <- trainControl(method = \"cv\", number = 3)\nmodel3 <- train(Turnover ~ ., data = trainData, method = \"rpart\", trControl = control, metric = \"Accuracy\")\nprint(model3)\n\n# 随机森林模型\nmodel4 <- train(Turnover ~ ., data = trainData, method = \"rf\", trControl = control, metric = \"Accuracy\")\nprint(model4)`

\n`# Random Forest`

\n**3b) (2 points) Which model performed better when comparing the average\naccuracy of the resampled decision tree and random forest models?\nExplain the difference between the decision tree model and the random\nforest model.**

**Answer to Question 3b**

**Use the \"testData\" for all questions in this question.**

**4a)(4 points) Using testData, predict the probability of an employee\nleaving, i.e. being a turnover, and output the AVERAGE of these\nprobabilities for each of the models below:**

**i) model1 (question 2a) ii) model2 (question 2b) iii) model3 (question\n3a) and iv) model4 (question 3a)**

`# 使用测试数据预测员工流失概率\npred_prob1 <- predict(model1, newdata = testData, type = \"response\")\npred_prob2 <- predict(model2, newdata = testData, type = \"response\")\npred_prob3 <- predict(model3, newdata = testData)\npred_prob4 <- predict(model4, newdata = testData)\n\n# 计算平均概率\nmean_probs <- data.frame(\n Model1 = mean(pred_prob1),\n Model2 = mean(pred_prob2),\n Model3 = mean(pred_prob3),\n Model4 = mean(pred_prob4)\n)\nprint(mean_probs)`

\n**4b) (4 points) Using the probabilities from Q4a and a threshold of 0.5\n(inclusive of 0.5), obtain the classifications of an employee being a\nturnover for all four models. Note: every row in the testData prediction\nmust be classified. Print the last ten classification rows for all the\nmodel classifications as well as the actual response for Turnover of\nthose rows.**

`# 根据预测概率和0.5的阈值进行分类\nclassifications1 <- ifelse(pred_prob1 >= 0.5, 1, 0)\nclassifications2 <- ifelse(pred_prob2 >= 0.5, 1, 0)\nclassifications3 <- ifelse(pred_prob3 >= 0.5, 1, 0)\nclassifications4 <- ifelse(pred_prob4 >= 0.5, 1, 0)\n\n# 获取最后十行的分类结果和实际值\nlast_ten_results <- data.frame(\n Actual = tail(testData$Turnover, 10),\n Model1_Pred = tail(classifications1, 10),\n Model2_Pred = tail(classifications2, 10),\n Model3_Pred = tail(classifications3, 10),\n Model4_Pred = tail(classifications4, 10)\n)\n\nprint(last_ten_results)`

\n**4c) (6 points) In this question, you will compare the prediction\naccuracy of the four models.\ni) (4 points) Using the classifications from Q4b, create a confusion\nmatrix and output the classification evaluation metrics (i.e. Accuracy,\nSensitivity, and Specificity) for all four models. Note: every row in\nthe testData classification must be used (do not use only the last ten\nclassification rows).**\"

**ii) (2 points) Which metric measures the rate of true negatives? Which\nmodel shows the highest value for this metric?**

`# 将实际结果和预测结果都转换为因子，并确保级别一致\nactual <- factor(actual, levels = c(0, 1))\npred_class1 <- factor(pred_class1, levels = c(0, 1))\npred_class2 <- factor(pred_class2, levels = c(0, 1))\npred_class3 <- factor(pred_class3, levels = c(0, 1))\npred_class4 <- factor(pred_class4, levels = c(0, 1))\n\n# 计算混淆矩阵和指标\nlibrary(caret)\n\n# 计算混淆矩阵和指标并输出\nconf_matrix1 <- confusionMatrix(pred_class1, actual)\nconf_matrix2 <- confusionMatrix(pred_class2, actual)\nconf_matrix3 <- confusionMatrix(pred_class3, actual)\nconf_matrix4 <- confusionMatrix(pred_class4, actual)\n\n# 输出每个模型的准确率、敏感性和特异性\ncat(\"Model 1:\\n\", conf_matrix1$overall[\"Accuracy\"], \"\\n\", conf_matrix1$byClass[c(\"Sensitivity\", \"Specificity\")], \"\\n\")\ncat(\"Model 2:\\n\", conf_matrix2$overall[\"Accuracy\"], \"\\n\", conf_matrix2$byClass[c(\"Sensitivity\", \"Specificity\")], \"\\n\")\ncat(\"Model 3:\\n\", conf_matrix3$overall[\"Accuracy\"], \"\\n\", conf_matrix3$byClass[c(\"Sensitivity\", \"Specificity\")], \"\\n\")\ncat(\"Model 4:\\n\", conf_matrix4$overall[\"Accuracy\"], \"\\n\", conf_matrix4$byClass[c(\"Sensitivity\", \"Specificity\")], \"\\n\")`

\n**Response to question 4c(ii) :**

**Use the \"quine_dataset\" for Question 5.**

This data frame contains the following columns:

\n`Eth`

: ethnic background: Aboriginal or Not, (`\"A\"`

or `\"N\"`

).

`Sex`

: sex: factor with levels (`\"F\"`

or `\"M\"`

).

`Age`

: age group: Primary (`\"F0\"`

), or forms `\"F1,\"`

`\"F2\"`

or `\"F3\"`

.

`- **F0**: Primary school age (youngest children)\n\n- **F1**: First-year high school (Form 1)\n\n- **F2**: Second-year high school (Form 2)\n\n- **F3**: Third-year high school (Form 3)\n`

\n`Lrn`

: learner status: factor with levels Average (\"AL\") or Slow learner

\n(\"SL\")

`Days`

: days absent from school in the year.

\n`# Read data\nquine_dataset = read.csv(\"quine_dataset.csv\", header=TRUE, na.strings = \"\")\n\n# Remove any potential trailing white space from column names\nnames(quine_dataset) <- trimws(names(quine_dataset), which = \"right\")\n\n# Show first few rows\nhead(quine_dataset)`

\n**5a) (4 points)**

**i)(2 points) Plot a histogram of the count of \"Days\" from the\n\"quine_dataset\"**

`# Check the distribution of the response, Days\n# 绘制 Days 的直方图\nhist(quine_dataset$Days, main = \"Distribution of Days Absent\", xlab = \"Days Absent\")`

\n**ii)(2 points) Create boxplots of the response variable “Days” against\nthe predicting variable “Sex”. Explain the relationship between the\nresponse variable and predicting variable based on the boxplot. Using\nthe boxplot only, do you observe any overlap or potential outliers?**

`#Code\n# 绘制 Days 与 Sex 的箱线图\nboxplot(Days ~ Sex, data = quine_dataset, main = \"Days Absent by Sex\")`

\n**Response to Q5a(ii)**

**5b) (4 points)\ni) Fit a poisson regression model using all the predictors from the\n“quine_dataset” and “Days” as the response variable. Call\nit pois_model1 and display the model summary**

**ii) Interpret the coefficient of “AgeF2” in pois_model1 with respect\nto the log expected \"Days\".**

**iii) Interpret the coefficient of “EthN” in pois_model1 with respect\nto the rate ratio of \"Days\".**

**iv) Why can't we use a standard regression model with the log\ntransformation of the response variable instead of creating a Poisson\nregression model?**

`# 拟合泊松回归模型\npois_model1 <- glm(Days ~ ., data = quine_dataset, family = poisson)\nsummary(pois_model1)`

\n**Response to question 5b(ii):**

**Response to question 5b(iii):**

**Response to question 5b(iv):**

**5c)(4 points)**

**i) Calculate the estimated dispersion parameter for \"pois_model1\"\nusing both the deviance and Pearson residuals. Is this an overdispersed\nmodel using a threshold of 2.0? Justify your answer.**

**ii) Create a proposed model (call it \"pois_model2\") that handles\noverdispersion using the quine_dataset.**

**iii) Explain the concept of overdispersion in Poisson regression and\ndiscuss the potential causes of overdispersion.**

**iv) Describe how overdispersion can affect the reliability of\nstatistical inference in Poisson regression models.**

`# 计算德维恩斯和皮尔森残差离散参数\ndeviance_dispersion <- summary(pois_model1)$deviance / summary(pois_model1)$df.residual\npearson_dispersion <- sum(residuals(pois_model1, type = \"pearson\")^2) / summary(pois_model1)$df.residual\nprint(deviance_dispersion)\nprint(pearson_dispersion)`

\n**Response to Q5c(i)**

**Response to Q5c(ii)**

`# 如果发现存在过度离散，可以使用负二项回归模型来处理\nlibrary(MASS)\npois_model2 <- glm.nb(Days ~ ., data = quine_dataset)\nsummary(pois_model2)`

\n**Response to Q5c(iii)**

**Response to Q5c(iv)**

**End of exam**

\n

\n\n",
"image": "https://bornforthis.cn/gzh.jpg",
"date_published": "2024-11-03T17:41:56.000Z",
"authors": [
{
"name": "AI悦创"
}
],
"tags": []
},
{
"title": "考试",
"url": "https://bornforthis.cn/1v1/57-Ren-Xinxing-gatech-edu-USA/Quiz-Exam.html",
"id": "https://bornforthis.cn/1v1/57-Ren-Xinxing-gatech-edu-USA/Quiz-Exam.html",
"summary": "单选 Question 1 In multiple linear regression, if the constant variance assumption does not hold, we apply a Box-Cox transformation to the predicting variables. A. True B. False✅ ...",
"content_html": "AI悦创·编程一对一

\nAI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\nIn multiple linear regression, if the constant variance assumption does not hold, we apply a Box-Cox transformation to the predicting variables.

\nA. True

\nB. False✅

\nIn logistic regression, we interpret the regression coefficients with respect to the odds of success.

\nA. True✅

\nB. False

\nIn Support Vector Machines (SVM), the optimization problem involves maximizing the margin between the boundary and the farthest data points of each class, which are called support vectors.

\nA. True

\nB. False✅

\nIn Poisson regression, the test for overall regression significance uses the F-test.

\nA. True

\nB. False✅

\nIn a goodness of fit test for Poisson regression, if the p-value is small, we reject the null hypothesis and conclude that the model is a good fit.

\nA. True

\nB. False✅

\nSupport vector machine, or SVM, is robust to outliers but not to small changes in the data.

\nA. True

\nB. False✅

\n", "image": "https://bornforthis.cn/gzh.jpg", "date_published": "2024-11-03T10:23:15.000Z", "authors": [ { "name": "AI悦创" } ], "tags": [] }, { "title": "Greedy Algorithms", "url": "https://bornforthis.cn/1v1/77-xiaoxianyue/Greedy-Algorithms.html", "id": "https://bornforthis.cn/1v1/77-xiaoxianyue/Greedy-Algorithms.html", "summary": "1. Scheduling 1.1 Scheduling problem We can only perform one activity after another has finished What is the maximum number of activities that can be completed? Note that activi...", "content_html": "| i | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |

\n|

- \n
- In the coursework report, you should answer them well according to your understanding. The problems are: \n

- \n
- What is the Gaussian Mixture Model (GMM) or Mixture of Gaussian(MoG) and how to solve GMM via Expectation Maximization(EM)? Further, why can the GMM or MoG be solved well by the EM algorithm? Please explain the reason. \n
- What about the difference between linear regression and logistic regression? If we want to solve real-world related tasks, how do we choose these two methods? \n
- There are many feature descriptors to represent the image, for example, HOG, SIFT, GIST, BOW, and so on. Please choose one feature descriptor to explain its definition and function. It is encouraged to analyze/explain the reason or advantage why you select the feature descriptor. If an example could be used to introduce it, it would be better. \n

C:::

\n\n

\nAI悦创·编程一对一

\nAI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\nDespite advances over the past decade, including the advent of 5-HT₃ receptor antagonists, combination therapy, and multimodal strategies, **post-operative nausea and vomiting** (PONV) remains a serious and frequent adverse event associated with surgery and anaesthesia. PONV can be very distressing for patients, can lead to medical complications and impose economic burdens. A meta-analysis of several studies gives rates of 37% for nausea and 20% for vomiting in patients undergoing general anaesthesia. However, indiscriminate prophylaxis is not recommended (the 'prevent-or-cure' dilemma).

There are considerable variations in the reported incidence of PONV, which can be attributed to a number of factors. Risk factors for PONV can be divided into patient risk factors, procedural risk factors, anaesthetic risk factors, and post-operative risk factors. Main and well understood risk factors are gender, history of motion sickness/PONV, smoking status, and duration of anaesthesia.

\nA data set (courtesy of Dr. Jelena Velickovic, MD anesthesiologist, Belgrade University), is given in `ponv0.odc`

or `ponv0.csv`

. The **SinclairScore** (propensity to PONV) is to be predicted by variables **Gender** (0-male; 1-female), **Anaesthesiaduration** (duration of anaesthesia in minutes), **Smoking** (smoking status 0-nonsmoker; 1-smoker), and **PONVhist** (history of PONV 0-no; 1-yes) via Bayesian linear regression. Use normal noninformative priors on the beta coefficients and a gamma prior on the precision parameter (reciprocal of variance).

- \n
- (a) Find the 95% CS for parameter
**beta2**, the coefficient for**Anaesthesiaduration**? Does the credible set contain 0? If not, what does it mean? \n - (b) Find the 95% CS for an individual prediction of
**SinclairScore**if**Gender = 1**,**Anaesthesiaduration = 55**,**Smoking=0**, and**PONVhist=1**. \n - (c) Find the Bayesian ${R}^{2}$R2. \n

`import pandas as pd\nimport numpy as np\nimport scipy.stats as stats\n\n# 加载数据\nponv_data = pd.read_csv('PONV.csv')\n\n# 筛选相关列并重命名列\nponv_data_filtered = ponv_data[['SinclairScore', 'Gender', 'Anaesthesiaduration', 'Smoking', 'PONV0to24']].copy()\nponv_data_filtered.rename(columns={'PONV0to24': 'PONVhist'}, inplace=True)\n\n# 标准化 Anaesthesiaduration 列\nponv_data_filtered['Anaesthesiaduration_std'] = (\n ponv_data_filtered['Anaesthesiaduration'] - ponv_data_filtered['Anaesthesiaduration'].mean()\n) / ponv_data_filtered['Anaesthesiaduration'].std()\n\n# 提取特征和目标变量\nX = ponv_data_filtered[['Gender', 'Anaesthesiaduration_std', 'Smoking', 'PONVhist']].values\ny = ponv_data_filtered['SinclairScore'].values\n\n# 为特征矩阵添加截距列\nX = np.hstack([np.ones((X.shape[0], 1)), X])\n\n# 贝叶斯线性回归 (解析解)\nn, p = X.shape\nalpha = 1e-6 # 非信息先验的超参数\nbeta_prior_mean = np.zeros(p)\nprior_cov = (1 / alpha) * np.eye(p)\n\n# 计算后验分布的均值和协方差\nXTX_inv = np.linalg.inv(X.T @ X + alpha * np.eye(p))\nbeta_post_mean = XTX_inv @ X.T @ y\nbeta_post_cov = XTX_inv\n\n# Part 1(a): beta2 (麻醉时长系数) 的 95% 置信区间\nbeta2_mean = beta_post_mean[2]\nbeta2_std = np.sqrt(beta_post_cov[2, 2])\nbeta2_credible_interval = stats.norm.interval(0.95, loc=beta2_mean, scale=beta2_std)\n\n# Part 2(b): 使用指定条件预测 SinclairScore 的置信区间\nnew_x = np.array([1, 1, (55 - ponv_data_filtered['Anaesthesiaduration'].mean()) / ponv_data_filtered['Anaesthesiaduration'].std(), 0, 1])\npredicted_mean = new_x @ beta_post_mean\npredicted_var = new_x @ beta_post_cov @ new_x.T + (1 / alpha)\nprediction_interval = stats.norm.interval(0.95, loc=predicted_mean, scale=np.sqrt(predicted_var))\n\n# Part 3(c): 贝叶斯 R^2\ny_pred = X @ beta_post_mean\nss_total = np.sum((y - y.mean()) ** 2)\nss_residual = np.sum((y - y_pred) ** 2)\nbayesian_R2 = 1 - (ss_residual / ss_total)\n\n# 输出结果\nprint(\"beta2的95%置信区间:\", beta2_credible_interval)\nprint(\"指定条件下SinclairScore的预测区间:\", prediction_interval)\nprint(\"贝叶斯 R^2:\", bayesian_R2)`

\nSome colors are more attractive to insects than others. Wilson and Shade (1967) conducted an experiment aimed at determining the best color for attracting cereal leaf beetles (*Oulema melanopus*). Six boards in each of four selected colors (lemon yellow, white, green, and blue) were placed in a field of oats during summer time. The following table (modified from Wilson and Shade, 1967) gives data on the number of cereal leaf beetles trapped:

| Board color | Insects trapped |

\n|

你好，我是悦创。

\n**Prim算法**（Prim’s Algorithm），这是一个常用的**最小生成树**算法。

在图论中，一个**生成树**是包含了图中所有顶点并连接在一起的子图。**最小生成树**（Minimum Spanning Tree，MST）是一棵生成树，且它的边的权值总和最小。简单来说，最小生成树是找到连接所有节点的最低成本路径。

举个例子：**假设我们有一些城市，城市之间有道路连接，每条道路的建设成本不同。我们希望以最小的建设成本连接所有城市，这时就可以用最小生成树算法来找到最低成本的连接方案。**

**Prim算法**是一种贪心算法，用来求解加权无向连通图的最小生成树。Prim算法每次从已连接的节点中选择一条最短的边来扩展树，直到所有节点都被包括在内。

- \n
**开始**时，选择一个**起点**，并把它加入生成树中。 \n**逐步扩展**：每次从当前生成树中找出一个连接到树外节点的**最小权重边**，将该边和该节点加入生成树。 \n**重复**这个过程，直到所有的节点都加入到生成树中。 \n

假设我们有一个图 $G=(V,E)$G=(V,E)，其中 $V$V 是节点集合，$E$E 是边集合，边的权重用 $w$w 表示。

\n- \n
**初始化**：从任意一个节点开始，记为节点 ${v}_{0}$v0。 \n**选择边**：找到一个**连接生成树**和**未连接节点**的最小权重边，将该边和对应的节点加入生成树。 \n**重复选择**：重复上一步，直到所有节点都在生成树中。 \n

假设我们有以下加权无向连通图：

\n`![](./05-Prims-algorithm.assets/image-20241028092328533.png)`

**节点**：A、B、C、D

**边及权重**：

- \n
- A-B：权重 1 \n
- A-C：权重 3 \n
- A-D：权重 4 \n
- B-C：权重 2 \n
- B-D：权重 5 \n
- C-D：权重 6 \n

**步骤 1：初始化**

- \n
**起始节点**：选择节点 A，加入生成树。 \n**已加入的节点**：{A} \n**候选边**：A-B(1)、A-C(3)、A-D(4) \n

**步骤 2：选择最小权重边**

- \n
**从候选边中选择权重最小的边**：A-B，权重 1 \n**加入生成树**：边 A-B，节点 B \n**已加入的节点**：{A, B} \n**更新候选边**：\n- \n
- A-C(3) \n
- A-D(4) \n
- B-C(2) \n
- B-D(5) \n

\n

**步骤 3：继续选择最小权重边**

- \n
**选择最小的边**：B-C，权重 2 \n**加入生成树**：边 B-C，节点 C \n**已加入的节点**：{A, B, C} \n**更新候选边**：\n- \n
- A-D(4) \n
- B-D(5) \n
- C-D(6) \n

\n

**步骤 4：继续选择最小权重边**

- \n
**选择最小的边**：A-D，权重 4 \n**加入生成树**：边 A-D，节点 D \n**已加入的节点**：{A, B, C, D} \n**所有节点已加入生成树，算法结束**\n

**包含的边**：

- \n
- A-B，权重 1 \n
- B-C，权重 2 \n
- A-D，权重 4 \n

**总权重**：1 + 2 + 4 = **7**

SQL SELECT practice in MySQL

\nThis assignment gives you an opportunity to hone your skills as an SQL programmer. Please download the self-contained exported database schema and import the schema into a database named *musiclastnamefirstinitial,* where *lastname* is your last name and *firstinitial* is the first letter of your first name. Please submit one hwk5musicdblastnamefirstinitial.sql file to canvas that contains the queries that you wrote for the assignment.

The database schema represents the data associated with a music library. The database tracks artists, albums, songs, genres of songs, moods of songs, users, recording labels and users following artists. An artist represents a musician. A musician can release albums as well as perform on songs on an album.

\nThe database limits each song to one mood as well as one genre.

\nAn artist creates zero to many albums and an album is associated with 1 artist. An album has 0 to many songs. A song is associated with 1 album. A song can be associated with 1 to many artists and an artist is associated with 1 to many songs. As you can see, this many to many relationship is represented via a separate table, the artist_performs_song table.

\nA song is associated with 1 genre and a genre is associated with 0 to many songs. A song is associated with 1 mood and a mood is associated with 0 to many songs. An artist is associated with 1 recording label and a recording label is associated with 0 to many artists.

\nA user can follow 0 to many artists. An artist can be followed by 0 to many users. As you can see this many to many relationship is represented via the table user_follows_artist. When a user follows an artist, they follow the albums and the songs produced by that artist. Here is the logical database design for the database:

\n\n**Within your solution file please provide the question number in comments before the solution.**

**[IMPORTANT]**

Write SQL queries that answer the following questions. Make sure you compose a query that does not use literal values determined by perusing the tuples. The SQL queries that you compose must work against **any instance** of this database and not just the instance provided to you. Also, remember some queries may generate an empty result.

- \n
- \n
(5 points) For each artist, return the artist name and their recording label name.

\n \n - \n
(5 points) Determine the number of artists associated with each record label. Rename the count signed_artists. Sort the results in descending order using count signed_artists. Make sure all labels are in the result.

\n \n - \n
(5 points) Determine the number of users who have followed artists. Rename the count num_followers.

\n \n - \n
(5 points) Make a separate table from the song table – where the records are for the songs with the Rock genre. Name the new table rock_songs. Remember, a table can only be created once. If you attempt to create the same table multiple times it will generate an error.

\n \n - \n
(5 points) For each song return the song id, song title, album name, recording label and the artist’s name who produced the album.

\n \n - \n
(5 points) For each song, create an aggregated field that contains a list of the artists who performed the song. The result set should contain the song title, the album name, the recording label (found from the artist that produced the album the song is on) and the grouped list of artists. Order the results in ascending order by the song name.

\n \n - \n
(5 points) Which song has the most number of artists who appear on it? Return the song name.

\n \n - \n
(5 points) How many users follow each of the different artists? The result should contain the artist’s name and the count of the number of followers. Rename the count to num_followers. All artists must appear in the result. Order the results in descending order by num_followers.

\n \n - \n
(5 points) For each recording label, determine the number of albums they have released. Rename the number of albums to num_albums. Sort the results in descending order using the num_albums field.

\n \n - \n
(5 points) For each genre in the genre table, determine the number of songs associated with that genre. The result should contain the genre name and the count. Rename the count to

\n`num_songs`

. Order the results by`num_songs`

in descending order. Make sure all genres appear in the result. If a genre is not associated with any songs, then the count for the number of songs should be 0. \n - \n
(5 points) For each song on an album, return the song title, the album name, the producing artist, the genre, the mood, and the number of followers for the producing artist.

\n \n - \n
(5 points) Return the artist name(s), which are followed by all users.

\n \n - \n
(5 points) Return the user id, user name and user email for members who follow all artists in the database.

\n \n - \n
(5 points) Return all albums which are associated with record labels containing the keyword

\n`Music`

. The results should contain all fields from the albums table. \n - \n
(10 points) Find users that follow the same artist. Each returned tuple should contain the user name for the 2 users as well as the artist that is followed by the two users. Order the results in ascending order by the artist’s name. Make sure you do not match a member with themselves. Also, only report the same member pair once.

\n \n - \n
(5 points) For each artist, determine the number of albums they have released. The result should contain the artist’s name and the count of albums. Rename the count of albums to

\n`num_albums`

. Order the results in descending order by`num_albums`

. \n - \n
(5 Points) Return the maximum number of songs associated with any one of the artists. An artist can be associated with a song by producing it or by performing on it. The result should be the maximum song count for the artist. Rename the maximum value to

\n`most_songs`

. \n - \n
(5 Points) Return the name of the artist who performed the most number of songs. The result should contain the artist name and the count of songs, renamed to

\n`num_songs`

. \n

\n

\n\n",
"date_published": "2024-10-24T12:03:26.000Z",
"authors": [
{
"name": "AI悦创"
}
],
"tags": []
},
{
"title": "MIDTERM EXAM",
"url": "https://bornforthis.cn/1v1/57-Ren-Xinxing-gatech-edu-USA/MIDTERM-EXAM.html",
"id": "https://bornforthis.cn/1v1/57-Ren-Xinxing-gatech-edu-USA/MIDTERM-EXAM.html",
"summary": "ISyE6420 Fall 2024 Released October 18, 6:00 PM – due October 20, 6:00 PM. This exam is not proctored and not time limited except the due date. Late submissions will not be acce...",
"content_html": "AI悦创·编程一对一

\nAI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\nISyE6420

\nFall 2024

\nReleased October 18, 6:00 PM – due October 20, 6:00 PM. This exam is not proctored and not time limited except the due date. Late submissions will not be accepted.

\nUse of all course materials, including the class Github site, the textbook, and any personal notes are allowed. Internet search using exam-related keywords is not allowed during the exam period, nor is any communication with other students relating to the exam. Public Ed Discussion posts about the exam questions are not permitted. If you need any clarification on the questions, please use the private posting function so that your post is only visible to the instructors.

\nDiscussing the exam questions with anyone outside of the course staff and/or posting any portion of the exam to non-GT sites are considered serious violations of the Georgia Tech honor code. AI tools, like ChatGPT/Copilot and other similar ones, are also not allowed.

\nPlease read and sign (or e-sign) the following honor pledge and submit a copy along with your answers.

\nI pledge on my honor that I have completed the exam on my own and I have not used any unauthorized materials or taken anyone’s help for completing this exam.

\n**Fall 2024 Midterm Exam Problem 1.** Consider the following unnormalized posterior:

p\left(\theta {\mid}y\right)\propto {\theta}_{1}{e}^{-{\theta}_{1}-({\theta}_{2}-1{)}^{2}(1+{\theta}_{1})}p(θ∣y)∝θ1e−θ1−(θ2−1)2(1+θ1)

\nwhere ${\theta}_{1}\in (0,{\infty})$θ1∈(0,∞) and ${\theta}_{2}\in {R}$θ2∈R. Plot a two-dimensional image of this distribution for $\theta \in [0,5]\times [-2,3]$θ∈[0,5]×[−2,3]. Generate an MCMC sample of size 10,000 using the independent chain Metropolis-Hastings algorithm with 1,000 additional burn-in iterations for a total of 11,000. Use a uniform distribution in $[0,5]\times [-2,3]$[0,5]×[−2,3] as the proposal. This algorithm needs to be manually coded (without using a PPL) in Python, R, etc. The two-dimensional image can be created in Python using the Matplotlib function `contourf`

or in R using the function `image`

.

- \n
- \n
Report the acceptance rate of the algorithm.

\n \n - \n
Plot the sampled points over the two-dimensional image of the distribution.

\n \n - \n
Plot the marginal densities of the two parameters.

\n \n - \n
Obtain the 90% HPD credible intervals for each of the two parameters.

\n \n

**Fall 2024 Midterm Exam Problem 2.** Consider the same unnormalized posterior density in problem 1. Then,

- \n
- \n
Find the full conditional distributions of ${\theta}_{1}$θ1 and ${\theta}_{2}$θ2, and use Gibbs sampling to sample from the posterior. The Gibbs sampling algorithm needs to be manually coded (without using a PPL) in Python, R, etc. As in problem 1, obtain 10,000 samples with 1,000 additional burn-in iterations.

\n \n - \n
Plot the marginal posterior densities of the two parameters and provide their mean and 95% credible intervals.

\n \n - \n
Create trace plots for the two parameters. For the trace plots, the X-axis should be the iteration count, and the Y-axis should be the observed value of the chain at each iteration.

\n \n

`import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.stats import gamma, norm\n\n# 设置随机种子\nnp.random.seed(0)\n\n# 初始化参数\nnum_iter = 11000\nburn_in = 1000\n\ntheta1 = np.zeros(num_iter)\ntheta2 = np.zeros(num_iter)\n\n# 初始值\ntheta1[0] = 1.0\ntheta2[0] = 1.0\n\n# Gibbs 采样过程\nfor t in range(1, num_iter):\n # 更新 theta1\n rate = 1 + (theta2[t-1] - 1)**2\n theta1[t] = gamma.rvs(a=2, scale=1/rate)\n \n # 更新 theta2\n var = 1 / (1 + theta1[t])\n theta2[t] = norm.rvs(loc=1, scale=np.sqrt(var))\n\n# 舍弃燃烧期\ntheta1_samples = theta1[burn_in:]\ntheta2_samples = theta2[burn_in:]\n\n# 计算均值和 95% 可信区间\ntheta1_mean = np.mean(theta1_samples)\ntheta1_ci = np.percentile(theta1_samples, [2.5, 97.5])\n\ntheta2_mean = np.mean(theta2_samples)\ntheta2_ci = np.percentile(theta2_samples, [2.5, 97.5])\n\nprint(\"θ₁ 的后验均值：\", theta1_mean)\nprint(\"θ₁ 的 95% 可信区间：\", theta1_ci)\nprint(\"θ₂ 的后验均值：\", theta2_mean)\nprint(\"θ₂ 的 95% 可信区间：\", theta2_ci)\n\n# 绘制边际后验密度\nplt.figure(figsize=(12, 5))\n\nplt.subplot(1, 2, 1)\nplt.hist(theta1_samples, bins=50, density=True, color='skyblue', alpha=0.7)\nplt.title('θ₁ 的边际后验密度')\nplt.xlabel('θ₁')\nplt.ylabel('密度')\n\nplt.subplot(1, 2, 2)\nplt.hist(theta2_samples, bins=50, density=True, color='salmon', alpha=0.7)\nplt.title('θ₂ 的边际后验密度')\nplt.xlabel('θ₂')\nplt.ylabel('密度')\n\nplt.tight_layout()\nplt.show()\n\n# 绘制追踪图\nplt.figure(figsize=(12, 5))\n\nplt.subplot(2, 1, 1)\nplt.plot(theta1, color='skyblue')\nplt.title('θ₁ 的追踪图')\nplt.xlabel('迭代次数')\nplt.ylabel('θ₁')\n\nplt.subplot(2, 1, 2)\nplt.plot(theta2, color='salmon')\nplt.title('θ₂ 的追踪图')\nplt.xlabel('迭代次数')\nplt.ylabel('θ₂')\n\nplt.tight_layout()\nplt.show()`

\n**Fall 2024 Midterm Exam Problem 3.** Suppose two candidates (A and B) are standing in an election representing their political parties. From the past elections and surveys, it is known that the supporters for each political party are roughly equal with a margin of error of ±2%. A new survey of 1,000 people has been conducted just before the election and found that 515 people favor candidate A and 485 people favor candidate B.

- \n
- \n
Assume a beta distribution for the prior probability of support for each candidate. Obtain approximate estimate of the parameters of the beta distribution by equating ±2% to 95% confidence intervals.

\n \n - \n
Find the posterior distribution of the probability that the candidate B will win. Compute the posterior mean.

\n \n - \n
Now suppose that we also have information about the gender among the people surveyed. The data is shown below.

\n \n

| | Male | Female |

\n|

`Starter files: code.zip`

\nhttps://course.khoury.northeastern.edu/cs3500/hw_redseven_03_assignment.html

\n**Note: Homeworks 5 through 8 will begin a new project, and you will be working with a partner for them. Start thinking about who you’d like to partner with. You will sign up with your partner on the handin server, and you will not be able to submit subsequent assignments until you are part of a team on the handin server. If you do not know (or remember) how to request teams, follow these instructions. Please request teams no later than the start of homework 5, which is *Oct 20* — if you have not requested a teammate by then, we will randomly assign you one. You only need to request a partner for Assignment 5; we will copy the teams to the remaining assignments.**

The benefits of the model-view-controller architecture shine when we need to add new features, by isolating relevant parts of our design and changing them independently. In this assignment we will see those benefits pay off by supporting other forms of Solo Red. The goal of this assignment is to give you a chance to critically examine your earlier design choices, and either leverage or revise them to enable adding variations of this game with minimal change and duplication of code.

\n**With one exception (main), all** **new** **classes and interfaces for this homework should be in the** `cs3500.solored.model.hw04`

**package. All classes written in previous assignments, even if improved upon, should remain in their respective packages.**

There will be two submissions for this assignment:

\n- \n
- Your
*actual implementation*and full test suite \n - A
*self-evaluation*, due*one day plus one hour later*than the actual implementation, where we will ask you to reflect on your implementation and testing choices. \n

The same late-day policy as on the previous homework applies: each of these submissions independently may use up to one late day, and you are still able to submit your self-evaluation on time even if you submit your implementation late.

\nYou are expected to use your code from the previous assignment as the starting point for this assignment. **However, please ensure all of your new code is in the cs3500.solored.model.hw04 package. Additionally, your code from the previous assignment should remain in the cs3500.solored.model.hw02, cs3500.solored.view.hw02 and cs3500.solored.controller packages.**

To pass the public tests specific to this assignment, you need a controller that can handle a single input: the quit command. So just the input \"q\". The view’s render method and toString method should also be fully working. That is the bare minimum you need to start this assignment.

\nYou will also need a view that uses only the observations of `RedGameModel`

to create the toString and render the game state to an `Appendable`

.

Of course, for the hidden tests, you will need a fully working controller, view, and models.

\nTraditionally, the multi=player game Red7 has advanced rules that give more meaning to playing to a canvas or playing cards to a palette. You will implement a lighter variant of the advance rules with the following changes

\n- \n
- The player now draws a single card from the deck after they play to the palette. Naturally, if the deck is empty, no drawing occurs. \n
- The player can draw an additional card after playing to a palette if they play a card to the canvas and the number on the card played to the canvas is strictly greater than the number of cards in the now currently winning palette. If they fulfill this condition, they draw two cards from the deck instead of one after they play to the palette. After they draw, the number of cards they can draw resets to one. \n

Consider the state below with a non-empty deck and a max hand size of 5.

\n`Canvas: R\n> P1: B2 B7\nP2: R1 B5\nP3: O1 V7 R2\nP4: O4 I1\nHand: B1 O7 O3 R3 R4`

\nIf they play B1 to the canvas, the game state changes to the following

\n`Canvas: B\nP1: B2 B7\nP2: R1 B5\n> P3: O1 V7 R2\nP4: O4 I1\nHand: O7 O3 R3 R4`

\nSince the number of the card played to the canvas is 1 and the winning palette has 3 cards, the player does not get to draw an additional card at the end of the turn. Therefore, the player will only be able to draw a single card after playing to palette. If they then play O7 to palette P2 and draw for their hand, we get the following state. Notice the hand gets one new card from drawing.

\n`Canvas: B\nP1: B2 B7\n> P2: R1 B5 O7\nP3: O1 V7 R2\nP4: O4 I1\nHand: O3 R3 R4 I7`

\nSuppose instead of playing B1 to the canvas, they played O7 to the canvas at the start. Then the game state becomes as follows.

\n`Canvas: O\n> P1: B2 B7\nP2: R1 B5\nP3: O1 V7 R2\nP4: O4 I1\nHand: B1 O3 R3 R4`

\nSince the 7 of O7 is greater than the number of cards in the winning palette P1, the player will get to draw an additional card at the end of their turn. Say they play B1 to P4, then the state becomes as follows.

\n`Canvas: O\nP1: B2 B7\nP2: R1 B5\nP3: O1 V7 R2\n> P4: O4 I1 B1\nHand: O3 R3 R4 I7 R5`

\nAgain, noticed they drew two cards for their hand thanks to the canvas play!

\nNote that if they never changed the canvas and just played a card to a palette, then if the player is not losing, they only get to draw a single card.

\nEverything else about the game stays unchanged: the rules of the colors remain the same and drawing can only occur if the game has started and still going after playing to a palette.

\n- \n
- Design a class implementing the Advanced variant of Solo Red as the class
`AdvancedSoloRedGameModel`

. This new class should clearly implement`RedGameModel`

, and clearly shares some commonalities with the existing`SoloRedGameModel`

. In your implementation, strive to avoid as much code-duplication as possible among the two models, while making sure that both fully work properly. If done correctly, none of your code from before should break or be affected. You may need to refactor your earlier code, though, to make it more flexible and enable better code reuse for these new classes. \n - Design a
*factory*class, named`RedGameCreator`

, as described below. \n - Implement a
`main`

method to allow you to choose different game variants from the command line, when running your program. (This is described below.) \n - If you had to change any part of your design from prior assignments, document those changes in a README file. (This must be a plain-text file.) \n
- Test everything thoroughly: make sure the new models work properly, and that the controller can control them as well as it could the original model. You do not need to test your
`main`

method. \n

You must complete these requirements while respecting the following constraints:

\n- \n
- You are not allowed to change the interface of the model (
`RedGameModel`

) at all from before. \n - You are not allowed to change the controller interface (
`RedGameController`

) at all from before. \n - As before, you are not allowed to add any additional public methods in your classes, with the exception of any expected constructors. \n
- You must create separate model implementations, without eliminating
`SoloRedGameModel`

from before. That is, models that represent all variations of the game must co-exist. \n

In this assignment it is important not only to have a correctly working model, but also a design that uses interfaces and classes appropriately. Make sure you minimize replication of code. You *may* refactor your earlier designs to do this. You may also have to change earlier implementations to remove bugs. This is OK, but *must* be properly documented and justified.

- \n
- You should always plan out things in detail before you code. But this assignment is
**designed**to be significantly more difficult if you jump into the code prematurely and try to figure out things as you code! \n - For each variant, go through each operation of the model interface, and think about how that operation works for this variant of the game. Planning out every operation before coding will save you a lot of time! \n
- Recall that your view needs to be able to work with any model implementation, without knowing which one! \n
- You may be tempted to discover all possible abstractions beforehand. Make sure you minimize code repetition, but not over-abstract so much that some methods lose meaning. Even private helper methods should have a specific purpose! While it is good to anticipate abstraction, there is nothing wrong with thinking about abstraction after implementing all the code. It’s the end result that counts! \n

When testing your new models, you may find that a lot of your old model tests should also pass under the new models. This means you will end up creating a lot of duplicate tests. Make sure to abstract your test code appropriately (Hint: Test classes are just Java classes. So how do we deduplicate code between classes?). Make a test file for each variant in the `cs3500.solored`

package in your test folder and make sure to deduplicate code as necessary.

A really good test class for your new models will be reusing a bunch of the old model tests and then’ the only new code in your model test classes will be any factory methods you need to make and tests that only apply to that particular model

\n`RedGameCreator`

classDesign a class with the above name. The class should define a `public enum GameType`

with two possible values: `BASIC`

and `ADVANCED`

. It should offer one static method `createGame(GameType)`

that returns an instance of (an appropriate subclass of) `RedGameModel`

, depending on the `GameType`

given. ] (Note: What class/pattern am I asking you to design/follow?)

`main()`

methodAdd the following class to your project. Notice what package the class is in!

\n`package cs3500.solored;\n\npublic final class SoloRed {\n public static void main(String[] args) {\n // FILL IN HERE\n }\n}`

\nThis `main()`

method will be the entry point for your program. Your program needs to take inputs as command-line arguments (available in your program through the argument `args`

above). Review the documentation for command-line arguments in a Java program.

- \n
- The first command-line argument must be a string, specifically one of
`basic`

, or`advanced`

. This argument will decide which game variant (and hence which model) you should use. \n - You may optionally pass one or two more arguments
`P`

and`H`

, both of which should be parsed as integers, where P specifies the number of palettes, and H specifies the max hand size. If unspecified, you should use 4 palettes and a hand size of 7 as the defaults. \n

The following are some examples of valid command lines, and their meanings:

\n- \n
`basic`

produces a basic game of Solo Red with default number of palettes and max hand size \n`basic 4 7`

achieves the same thing, but explicitly sets the number of palettes and the max hand size. \n`advanced 8`

produces an Advanced Solo Red game with 8 palettes and the default max hand size. \n`advanced 7 8`

produces an Advanced Solo Red game with 7 palettes and a max hand size of 8. \n

All games created must start with all 35 possible cards and must be shuffled.

\nYou may add additional methods to your `RedGameCreator`

class, but you must maintain the methods specified above for my tests to compile against your code.

\n\nThis command-line specification also does not allow for customizing the deck of cards to be dealt. It is an interesting challenge to think how you might design such configuration options.

\n

This is *not* an exhaustive list; other command lines are possible.

When you specify command-line arguments, they are always treated as strings, even if they are not within quotes. However, quotes are necessary if you want to pass a string that contains spaces in it.

\nThese arguments will appear in the `String[] args`

parameter to your `main`

method; you can use them however you need to, to configure your models. For this assignment, your main method should throw an IllegalArgumentException if the game type specified is not a valid game type. However, you do *not* need to explicitly handle invalid command lines (e.g. by producing an informative error message) for the number arguments. However, your code should not *crash* in that case (e.g. by specifying -1 as the number of rows, and causing an `IndexOutOfBounds`

exception).

**16 Points**

A regular expression is said to be simpler than another if it involves less operations than the other. For example, `a`

(0 operations) is simpler than `(a*)*`

(2 operations) and `(a ∪ b)c`

(2 operations) is simpler than `ac ∪ bc`

(3 operations). Rewrite each of the following regular expressions as a simpler regular expression describing the same language. (For full credit, simplify as much as possible). Show your work.

**Question 1:** ${{\varnothing}}^{\ast}\cup {a}^{\ast}\cup {a}^{\ast}{b}^{\ast}$∅∗∪a∗∪a∗b∗

**Question 2**：$(\epsilon \cup {a}^{\ast})b$(ε∪a∗)b

**Question 3**：$({a}^{\ast}\cup ba{)}^{\ast}$(a∗∪ba)∗

**Question 4**：$\left(a\right(b{b}^{\ast}\left)\right)\cup a$(a(bb∗))∪a

**Question 5**：$(aa{)}^{\ast}\cup a(aa{)}^{\ast}$(aa)∗∪a(aa)∗

**Question 6**：$(a{b}^{\ast}{)}^{\ast}\cup (b{a}^{\ast}{)}^{\ast}$(ab∗)∗∪(ba∗)∗

**Question 7**：$(a\cup b{)}^{\ast}a(a\cup b{)}^{\ast}$(a∪b)∗a(a∪b)∗

**Q8 Proving that languages aren't regular**

Use the Pumping Lemma to show that the following languages over Σ = {a, b} are not regular. In each case, carefully describe the string that will be pumped and explain why pumping it leads to a contradiction.

\n**Question 1** $\{{a}^{n}{b}^{2n}\mid n\ge 0\}${anb2n∣n≥0}

**Question 2** $\{{a}^{i}{b}^{j}\mid i\ge 2j\text{and}i,j\ge 0\}${aibj∣i≥2j and i,j≥0}

**问题1：**

**证明：**

假设语言 ${L}_{1}=\{{a}^{n}{b}^{2n}\mid n\ge 0\}$L1={anb2n∣n≥0} 是正则的。根据抽档引理，存在一个抽档长度 $p$p，使得对于任何 $s\in {L}_{1}$s∈L1，且 ${\mid}s{\mid}\ge p$∣s∣≥p，都可分解为 $s=xyz$s=xyz，满足：

\n- \n
- ${\mid}y{\mid}0$∣y∣>0， \n
- ${\mid}xy{\mid}\le p$∣xy∣≤p， \n
- 对于所有 $i\ge 0$i≥0，$x{y}^{i}z\in {L}_{1}$xyiz∈L1。 \n

取 $s={a}^{p}{b}^{2p}$s=apb2p。由于 ${\mid}xy{\mid}\le p$∣xy∣≤p，$xy$xy 只能包含 $a$a，且 $y{\ne}\epsilon $y=ε，所以设 $y={a}^{k}$y=ak，其中 $k\ge 1$k≥1。

\n考虑 $i=0$i=0，得到 ${s}^{{\prime}}=x{y}^{0}z={a}^{p-k}{b}^{2p}$s′=xy0z=ap−kb2p。

\n此时，${s}^{{\prime}}$s′ 中 $a$a 的数量为 $p-k$p−k，$b$b 的数量为 $2p$2p。显然 $2(p-k){\ne}2p$2(p−k)=2p，因此 ${s}^{{\prime}}{\notin}{L}_{1}$s′∈/L1，与条件3矛盾。

\n**结论：**因此，${L}_{1}$L1 不是正则语言。

\n", "image": "https://bornforthis.cn/gzh.jpg", "date_published": "2024-10-15T10:51:59.000Z", "authors": [ { "name": "AI悦创" } ], "tags": [] }, { "title": "CPP 考试记录", "url": "https://bornforthis.cn/1v1/54-BananaYuShu/Other/sb1.html", "id": "https://bornforthis.cn/1v1/54-BananaYuShu/Other/sb1.html", "summary": "Suppose we have created two user-defined types, using the following code. Select the correct variable initialisation from the following options. A. B. C. The following code look...", "content_html": "Suppose we have created two user-defined types, using the following code.

\n`enum Colour\n{\n red, green, blue\n};\n\nenum class Activity\n{\n run, walk, cycle, swim\n};`

\nSelect the correct variable initialisation from the following options.

\nA.

\n`Colour signal = Colour::red;\nActivity activity = run;`

\nB.

\n`Colour signal = red;\nActivity activity = Activity::run;`

\nC.

\n`Colour signal = red;\nActivity activity = run;`

\nThe following code looks like it should output the powers of three less than 100,000. Alas it doesn't. Click on the location of the problem.

\n`#include <iostream>\n\nint main() {\n int i = 1;\n while (i < 100000) {\n std::cout << i << std::endl;\n i *= 3;\n }\n return 0;\n}`

\n问题出在第 5 行的变量声明 `int i = 1;`

。

由于 `int`

类型可能无法存储超过 32,767（在某些系统上）的值，当 `i`

乘以 3 后，可能会发生整数溢出，导致程序不能正确输出小于 100,000 的所有 3 的幂。

**解决方法：**

将变量 `i`

的类型从 `int`

改为 `long`

或 `long long`

，以确保它能存储更大的值。

`#include <iostream>\n\nint main() {\n long i = 1; // 将 int 改为 long\n while (i < 100000) {\n std::cout << i << std::endl;\n i *= 3;\n }\n return 0;\n}`

\n这段代码首先将变量 `i`

初始化为 1。然后进入一个 `while`

循环，循环的条件是 `i`

小于等于 100,000。

在每次循环中，代码会执行以下操作：

\n- \n
- 输出当前
`i`

的值到控制台。 \n - 将
`i`

的值乘以 3。 \n

具体来说，这段代码会输出以下数字序列：

\n- \n
- 1 \n
- 3 \n
- 9 \n
- 27 \n
- 81 \n
- 243 \n
- 729 \n
- 2187 \n
- 6561 \n
- 19683 \n
- 59049 \n

这些数字是 3 的幂次（即 3 的 0 次方到 10 次方），并且都小于等于 100,000。因为当 `i`

乘以 3 后超过 100,000 时，循环条件不再满足，循环结束。

**因此，这段代码的作用是：**

输出所有小于等于 100,000 的 3 的幂次。

\n上述代码的作用是**输出所有小于等于 100,000 的 3 的幂次**。具体原因如下：

- \n
**初始化**：变量`i`

被初始化为 1。 \n**循环条件**：`while (i <= 100000)`

，表示当`i`

小于等于 100,000 时继续循环。 \n**循环体**：\n- \n
`std::cout << i << std::endl;`

输出当前`i`

的值。 \n`i *= 3;`

将`i`

的值乘以 3，准备下一个循环的值。 \n

\n

**为什么会输出 3 的幂次？**

因为在每次循环中，`i`

都被乘以 3，这意味着 `i`

的值依次是 1、3、9、27、81......这些数正是 3 的连续幂次（3^0, 3^1, 3^2, ...）。循环会持续进行，直到 `i`

的值超过 100,000。

因此，代码实际上是在计算并输出 3 的幂次，只要这些值不超过 100,000，就会被输出。

\n", "image": "https://bornforthis.cn/gzh.jpg", "date_published": "2024-10-12T18:57:04.000Z", "authors": [ { "name": "AI悦创" } ], "tags": [] }, { "title": "开篇词｜AI+前端：开启未来发展的新篇章", "url": "https://bornforthis.cn/column/AI-practice-class-for-front-end-engineers/01.html", "id": "https://bornforthis.cn/column/AI-practice-class-for-front-end-engineers/01.html", "summary": "你好，我是悦创，欢迎来到前端工程师的 AI 实战课。 我算是国内探索 AI+ 前端领域比较早的一拨人。在各种大模型产品受到广泛关注之前，就已经开始尝试 AI 出码、UI 千人千面等诸多方式，帮助前端工程师提效，也给产品创新带来更多可能。 我曾有两段工作经历和 AI 紧密相关，现在回想起来感觉自己还挺幸运的。 一个是在腾讯游戏的数据平台，主要做 Web ...", "content_html": "\n你好，我是悦创，欢迎来到前端工程师的 AI 实战课。

\n我算是国内探索 AI+ 前端领域比较早的一拨人。在各种大模型产品受到广泛关注之前，就已经开始尝试 AI 出码、UI 千人千面等诸多方式，帮助前端工程师提效，也给产品创新带来更多可能。

\n我曾有两段工作经历和 AI 紧密相关，现在回想起来感觉自己还挺幸运的。

\n一个是在腾讯游戏的数据平台，主要做 Web 端游戏视频的智能剪辑功能研发。这让我深入了解了如何利用 AI 技术优化剪辑效果，提升视频内容的质量，给用户带来更好的观看体验。这也是我开始接触并思考端侧模型的开始。

\n另一个是淘宝的营销前台，主要做手机淘宝的导购营销，在这个过程中，有两个实践是我印象最深刻的。第一个是在工程链路上结合 AI 来实现自动出码，提升研发效率。第二个就是在用户体验层面探索 AI 驱动的人群精细化 UI 运营，提升业务转化。这使我进一步深化了自己对“AI+ 前端”的认识。

\n后来，随着 ChatGPT 和 Copilot 等代表性产品的出现和热度上升，我们再一次体会到了 AI（尤其是大模型）的强大。

\n这些经历，都让我更加看好 AI + 前端这个领域。就像 NodeJS 让前端经历了一次很大的跨越，我认为前端同样会因为 AI 的推动，再一次出现巨大的创新和发展。

\n我觉得前端结合 AI，是一种必然趋势。

\n随着智能技术的飞速发展，AI 已经在各个领域展现出巨大的潜力和影响力。前端作为用户与网站或应用交互的直接界面，引入 AI 技术可以极大地提升用户体验和应用的智能化水平。这是技术进步带来的必然趋势，也是**前端开发领域适应未来发展的重要方向。**

有需求就会有竞争。在现代社会，用户对于个性化、智能化的服务需求日益增长。前端开发融入 AI 技术，可以更好地满足这些需求，提升产品的市场竞争力。同时，随着越来越多的企业开始关注并投入 AI 技术的研发和应用，掌握 AI 技能的前端开发者也将更具市场竞争力。

\n结合我的实践经验，直观的感受就是能够**提升开发效率**。AI 技术可以帮助前端同学自动完成一些重复性高、模式化的工作，如代码生成、错误检测等。这不仅可以减轻我们的工作负担，还可以显著提升开发效率，使得开发者有更多的时间和精力专注于更具创新性和挑战性的任务。

在实际开发中，掌握 AI 技术还能让我们在**解决复杂问题时更加游刃有余**。AI 能够帮助前端工程师优化性能，例如通过智能预测与数据缓存技术，减少用户的等待时间，提升应用的响应速度。

更重要的是，AI 的引入能帮助前端处理更复杂的交互逻辑。在这个过程里，我们也能不断**提升自己的技术能力，更深层次地参与产品整体设计与开发**。比如借助 AI，我们就能实现个性化推荐、智能搜索、动态内容生成等功能，给用户带来更加优质的服务体验，这是未来互联网产品做大做强的必然方向。

总结来说就是，掌握 AI 技术，不仅能让前端工程师在求职市场上更具竞争力，更能让我们成长为全能型技术人才，推动产品创新和技术升级，满足用户日益增长的智能化需求。因此，**对于前端工程师，学习 AI 不仅是紧跟技术潮流的必然选择，更是加速个人成长、提升竞争力的有效途径。**

那么我们如何走进前端 +AI 这个领域，掌握其中的关键技术呢？我和很多前端小伙伴讨论这个问题的时候，发现最集中的困难就是这么两类。

\n一类是缺少相关理论基础，过不了 AI 原理关，即使想学习，也会迷失在庞杂的 AI 资料里无所适从。另一类就是动手实践关。对于怎么应用 AI 赋能日常的研发和业务，也可能没什么头绪。因为没有实践场景，即使初步掌握了一些 AI 技术也无法落地，长此以往就会打击我们的学习积极性。

\n针对这些困扰，我把自己的过往经验做了抽象和总结，希望通过这门课程来帮助更多前端同学了解 AI，并进一步深入到 AI+ 前端这个新方向。

\n这门课的知识导图如下所示：

\n\n先来看第一章，我会为你梳理一下后续课程会用到的前端知识（React 和 NodeJS），组件化及组件可解释性。这部分内容既是对我们熟悉的领域做个快速回顾，也是我们后续实践的前置基础。

\n第二章我们来打通原理关。我会站在前端工程师的视角，带你掌握必要的视觉 AI 技术基础。为了让理论学习不那么枯燥，我们还会实践使用 JS 版本的视觉模型，通过动手完成各种 AI 基础实例巩固所学，还会学习如何部署模型运行环境，并通过实验掌握数据整理、模型训练、验证和预测的技能。

\n有了前面的基础，第三章我们来实现一个 **AI 布局助手**，体验从 AI 模型的训练到自动出码的全过程。完成这个项目后，你将掌握数据集收集、增广、标签制作，模型训练与指标观察，组件识别及分析等技术，相信这些也会激发你对 AI+ 前端创新实践的更多灵感。

\n

\n\n",
"date_published": "2024-10-12T08:08:17.000Z",
"authors": [],
"tags": []
},
{
"title": "Homework 5",
"url": "https://bornforthis.cn/1v1/93-LiquidLeon/Homework5.html",
"id": "https://bornforthis.cn/1v1/93-LiquidLeon/Homework5.html",
"summary": "Instructions: This homework is to be submitted on GradeScope as a single pdf (not in parts) by 11:59 pm on the due date. You may either type your solutions in a word processor a...",
"content_html": "AI悦创·编程一对一

\nAI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\n**Instructions:** This homework is to be submitted on GradeScope as a *single* pdf (not in parts) by 11:59 pm on the due date. You may either type your solutions in a word processor and print to a pdf, or write them by hand and submit a scanned copy. Do write and submit your answers as if they were a professional report. There will be point deductions if the submission isn’t neat (is disordered, difficult to read, scanned upside down, etc...).

Begin by reviewing your class notes, the slides, and the textbook. Then do the exercises below. **Show your work.** An unjustified answer may receive little or no credit.

**Schedule:** You can do all problems right now, except for problems 7, 9, 10 that require Tuesday’s class.

**Read:** 1.3 (for Tuesday) and 1.4 (for Friday)

Consider the following unnormalized posterior:

\np(\theta \mid y)\propto {e}^{-\frac{1}{2}({\theta}_{1}^{2}{\theta}_{2}^{2}+{\theta}_{2}^{2}+{\theta}_{1}^{2}-2{\theta}_{1}{\theta}_{2}-4{\theta}_{1}-4{\theta}_{2})}p(θ∣y)∝e−21(θ12θ22+θ22+θ12−2θ1θ2−4θ1−4θ2)

\nwhere $\theta \in {{R}}^{2}$θ∈R2. Plot a two-dimensional image of this distribution for $\theta \in [-5,10{]}^{2}$θ∈[−5,10]2. Generate an MCMC sample of size 10,000 using the Metropolis algorithm with 1,000 additional burn-in iterations for a total of 11,000. This needs to be manually coded (without using a PPL) in Python, R, etc. The two-dimensional image can be created in Python using the Matplotlib function `contourf`

or in R using the function `image`

.

- \n
- \n
Choose the scale of the proposal distribution (bivariate normal distribution) so that the acceptance rate is around 0.40. Report the chosen scale and the actual acceptance rate.

\n \n - \n
Plot the sampled points over the two-dimensional image of the distribution.

\n \n - \n
Plot the marginal densities of the two parameters.

\n \n - \n
Obtain the 95% equi-tailed credible intervals for each of the two parameters.

\n \n

Consider the Bayesian model:

\ny\mid {\theta}_{1},{\theta}_{2}\sim N({\theta}_{1}+{\theta}_{2},1)\phantom{\rule{0ex}{0ex}}{\theta}_{i}{\sim}^{iid}N(0,{\nu}^{2}),\phantom{\rule{\"1em\"}{0ex}}i=1,2\phantom{\rule{0ex}{0ex}}{\nu}^{2}\sim \text{Inv-Gamma}(10,10)y∣θ1,θ2∼N(θ1+θ2,1)θi∼iidN(0,ν2),i=1,2ν2∼Inv-Gamma(10,10)

\nSuppose $y=1.2$y=1.2 is observed. Then,

\n(a) Find the full conditional distributions of ${\theta}_{1}$θ1, ${\theta}_{2}$θ2, and ${\nu}^{2}$ν2 and use Gibbs sampling to sample from the posterior.

\n(b) Plot the marginal posterior densities of the three parameters and provide their mean and 95% credible intervals.

\n(c) Create trace plots for all three parameters. For the trace plots, the X-axis should be the iteration count, and the Y-axis should be the observed value of the chain at each iteration.

\n\nAI悦创·编程一对一

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\nIn simple linear regression, a violation of the mean zero assumption will not lead to difficulties estimating ${\beta}_{0}$β0.

\nA. True

\nB. False✅

\nMultiple linear regression captures the causation of a predicting variable to the response variable, conditional of other predicting variables in the model.

\nA. True

\nB. False✅

\nIn Linear Regression, Ordinary Least Squares (OLS) can be considered a closed-form estimation approach that minimizes the sum of squared errors.

\nA. True✅

\nB. False

\nIn multiple linear regression with an intercept, the sampling distribution of ${\hat{\sigma}}^{2}$σ^2 follows a T-distribution with $n-p$n−p degree of freedom, where $n$n is the number of observations and $p$p is the number of model predictors.

\n**Note**: The number of model predictors ($p$p) does not include the intercept.

A. True

\nB. False✅

\nR-squared is the measure used to evaluate goodness of fit of a simple linear regression model.

\nA. True✅

\nB. False

\nIf the constant variance assumption does not hold in simple linear regression, we typically apply a transformation to the predicting variables.

\nA. True

\nB. False✅

\nThe F-test in ANOVA for equal means compares the between-group variability to the within-group variability.

\nA. True✅

\nB. False

\nIn linear regression, goodness of fit describes how accurately a model fits the observed data by minimizing its residuals.

\nA. True✅

\nB. False

\nIn ANOVA, the degrees of freedom for the pooled variance estimator is calculated as $n-1$n−1, where n is the total number of observations.

\nA. True

\nB. False✅

\nIn ANOVA, if one confidence interval of a pair of means in the pairwise comparison does not include zero, we conclude that the two means of the pair are plausibly equal.

\nA. True

\nB. False✅

\nIn a linear regression model without an intercept, we will include 5 dummy variables in the model if we have only 1 predicting variable in the model and that predicting variable is a qualitative variable with 5 categories.

\nA. True✅

\nB. False

\nThe Mean Squared Prediction Error and Precision Error are measures for computing prediction accuracy in a multiple linear regression model fitted using Ordinary Least Squares (OLS).

\nA. True

\nB. False✅

\nNote: Use the following information for questions 13-18

\nA linear regression model was fitted to estimate the response variable Roll using the predicting variable Unem. There are in total 29 observations.

\nHere is the model summary, with some values missing:

\n`Coefficients:\n\n Estimate Std. Error t value Pr(>|t|)\n(Intercept) 3957.0 4000.1 0.989 0.3313\nUNEM 1133.8 513.1 X 0.0358 *`

\n*Nobody goes there anymore. It's too crowded.*

Yogi Berra

\nYou've probably seen movies with crowds of people. While they might sometimes use real people, nowadays crowds are often computer-generated. This gives the film-makers more control over the size and behaviour of the crowd, and probably saves them money as well.

\nIn this assignment, we'll explore how to make a crowd scene using turtle graphics.

\n**Answer the questions below in order**: later questions depend upon earlier ones. Use *exactly* the function name and parameters as given in the question, and put all your functions into a single Python file named `a2.py`

. Please include your name and email at the top of `a2.py`

formatted like this:

`#\n# Full Name:\n# SFU ID #:\n# SFU Email:\n#`

\nWhen you're done, submit your finished `a2.py`

file on Canvas.

**Don't** use Google Colab for this assignment. The turtle graphics in Colab are different to set up and don't have the same functionality as the standard Python turtle graphics.

Please use IDLE or VS Code to write code with turtle graphics.

\nWrite a function called `polygon(n, size)`

that uses turtle graphics to draw an $n$n-sided regular polygon with each side of length `size`

.

Use this algorithm:

\n- \n
- Set the turning $angle$angle to be $\frac{360}{n}$n360 \n
- Do the following $n$n times:\n
- \n
- draw a line of length
`size`

\n - turn left $angle$angle degrees \n

\n - draw a line of length

In addition, please add error checking to your function:

\n- \n
- If $n3$n<3, print a helpful error message and return without drawing anything. \n
- If $size=0$size<=0, print a helpful error message and return without drawing

\nanything. \n

Here is a pentagon (a 5-sided polygon):

\n\n`import turtle\n\ndef polygon(n, size):\n # 错误检查：n 必须大于等于 3\n if n < 3:\n print(\"错误：n 必须大于等于 3 才能形成多边形。\")\n return\n # 错误检查：size 必须是正数\n if size <= 0:\n print(\"错误：size 必须是正数。\")\n return\n \n angle = 360 / n # 计算每个内角的度数\n for _ in range(n):\n turtle.forward(size) # 向前绘制一条边\n turtle.left(angle) # 左转指定的角度\n\n# 示例：绘制一个五边形\npolygon(5, 100)\nturtle.done()`

\nWrite a function called `jump_to(x, y)`

that moves the turtle to position (`x`

, `y`

) **without** drawing a line. After the turtle has jumped to (`x`

, `y`

) its pen should be down and ready to draw.

`turtle.goto(x, y)`

will move the turtle to (`x`

, `y`

), but if the pen is down it will draw a line.

Also, write a function called `jump_to_test()`

that uses `jump_to`

and `polygon`

to draw this picture:

`import turtle\n\ndef jump_to(x, y):\n # 提起画笔，不绘制线条\n turtle.penup()\n # 移动到指定位置\n turtle.goto(x, y)\n # 放下画笔，准备绘制\n turtle.pendown()\n\ndef jump_to_test():\n # 清屏并重置海龟状态\n turtle.reset()\n # 定义四个位置坐标\n positions = [(-100, -100), (100, -100), (-100, 100), (100, 100)]\n # 在每个位置绘制一个五边形\n for x, y in positions:\n jump_to(x, y)\n polygon(5, 50) # 使用之前定义的 polygon 函数，绘制边长为50的五边形\n # 完成绘制\n turtle.done()\n\ndef polygon(n, size):\n # 错误检查：n 必须大于等于 3\n if n < 3:\n print(\"错误：n 必须大于等于 3 才能形成多边形。\")\n return\n # 错误检查：size 必须是正数\n if size <= 0:\n print(\"错误：size 必须是正数。\")\n return\n \n angle = 360 / n # 计算每个内角的度数\n for _ in range(n):\n turtle.forward(size) # 向前绘制一条边\n turtle.left(angle) # 左转指定的角度\n\n# 运行测试函数\njump_to_test()`

\nWrite a function called `circle(radius)`

that draws a circle whose radius is `radius`

. To draw a circle in turtle graphics, just draw a polygon with lots of sides, say 50 - 100.

**Important** Don't call the `turtle.circle(r)`

to draw this circle. Use just your `polygon`

function.

To calculate the size (length) of the polygon edges, use this formula inside your `circle`

function:

size=2r\mathrm{sin}\frac{\pi}{n}size=2rsinnπ

\nHere, $r$r is the radius passed to `circle`

, and $n$n is the number sides of the polygon.

The `sin`

function, and `pi`

, are in Python's `math`

module.

Also, write a function called `circle_test()`

that uses your `circle`

function to draw 10 concentric circles like this:

`import turtle\nimport math\n\ndef circle(radius):\n n = 100 # 多边形的边数，边数越多，圆越光滑\n # 错误检查：半径必须是正数\n if radius <= 0:\n print(\"错误：半径必须是正数。\")\n return\n # 计算边长\n size = 2 * radius * math.sin(math.pi / n)\n # 移动到圆的起始位置，确保圆心在 (0, 0)\n turtle.penup()\n turtle.goto(0, -radius)\n turtle.pendown()\n turtle.setheading(0) # 设置初始方向为东\n # 绘制多边形近似的圆\n polygon(n, size)\n # 返回到圆心位置\n turtle.penup()\n turtle.goto(0, 0)\n turtle.pendown()\n\ndef circle_test():\n turtle.reset() # 重置绘图窗口\n for i in range(1, 11):\n circle(i * 10) # 绘制半径从10到100的同心圆\n turtle.done() # 保持窗口打开\n\ndef polygon(n, size):\n # 错误检查：n 必须大于等于 3\n if n < 3:\n print(\"错误：n 必须大于等于 3 才能形成多边形。\")\n return\n # 错误检查：size 必须是正数\n if size <= 0:\n print(\"错误：size 必须是正数。\")\n return\n \n angle = 360 / n # 计算每个角的度数\n for _ in range(n):\n turtle.forward(size) # 向前绘制一条边\n turtle.left(angle) # 左转指定的角度\n\n# 运行测试函数\ncircle_test()`

\nWrite a function called `eye(radius)`

that draws one (cartoon) eye using two circles: a big circle, and a filled-in smaller circle inside the big one (representing the pupil).

Use the `turtle.begin_fill()`

and `turtle.end_fill()`

functions to fill-in the smaller circle.

Here's an example:

\n\nAlso, write a function called `eye_test()`

that draws a pair of eyes of size 50 like this:

`import turtle\nimport math\n\ndef polygon(n, size):\n if n < 3:\n print(\"错误：n 必须大于等于 3 才能形成多边形。\")\n return\n if size <= 0:\n print(\"错误：size 必须是正数。\")\n return\n angle = 360 / n\n for _ in range(n):\n turtle.forward(size)\n turtle.left(angle)\n\ndef circle(radius):\n n = 100\n if radius <= 0:\n print(\"错误：半径必须是正数。\")\n return\n size = 2 * radius * math.sin(math.pi / n)\n turtle.penup()\n turtle.forward(radius)\n turtle.left(90)\n turtle.pendown()\n polygon(n, size)\n turtle.penup()\n turtle.right(90)\n turtle.backward(radius)\n turtle.pendown()\n\ndef eye(radius):\n # 绘制外圈（眼睛）\n circle(radius)\n \n # 绘制内圈（瞳孔），将其位置向下移动\n pupil_radius = radius / 2 # 瞳孔半径为眼睛半径的一半\n # 移动到瞳孔的起始位置（眼睛中心向下移动）\n turtle.penup()\n turtle.right(90)\n turtle.forward(radius / 2) # 向下移动半个半径\n turtle.left(90)\n turtle.pendown()\n turtle.fillcolor('black') # 设置填充颜色为黑色\n turtle.begin_fill()\n circle(pupil_radius)\n turtle.end_fill()\n # 返回到眼睛中心位置\n turtle.penup()\n turtle.left(90)\n turtle.backward(radius / 2)\n turtle.right(90)\n turtle.pendown()\n\ndef eye_test():\n turtle.reset()\n turtle.speed(0)\n # 绘制左眼\n turtle.penup()\n turtle.goto(-60, 0)\n turtle.pendown()\n eye(50)\n # 绘制右眼\n turtle.penup()\n turtle.goto(60, 0)\n turtle.pendown()\n eye(50)\n turtle.hideturtle() # 隐藏海龟\n turtle.done()\n\n# 运行测试函数\neye_test()`

\nWrite a function called `nose(size)`

that draws a (cartoon) nose that consists of, at least, two different shapes. The exact style and look of the nose is up to you. It could be as simple as an upside-down 7.

Make it so that the bigger `size`

is, the bigger the nose.

`import turtle\nimport math\n\n\ndef polygon(n, size):\n turtle.speed(0)\n if n < 3:\n print(\"错误：n 必须大于等于 3 才能形成多边形。\")\n return\n if size <= 0:\n print(\"错误：size 必须是正数。\")\n return\n angle = 360 / n\n for _ in range(n):\n turtle.forward(size)\n turtle.left(angle)\n\n\ndef circle(radius):\n turtle.speed(0)\n n = 100\n if radius <= 0:\n print(\"错误：半径必须是正数。\")\n return\n size = 2 * radius * math.sin(math.pi / n)\n turtle.penup()\n turtle.forward(radius)\n turtle.left(90)\n turtle.pendown()\n polygon(n, size)\n turtle.penup()\n turtle.right(90)\n turtle.backward(radius)\n turtle.pendown()\n\n\ndef eye(radius):\n turtle.speed(0)\n # 绘制外圈（眼睛）\n circle(radius)\n pupil_radius = radius / 2\n turtle.penup()\n turtle.right(90)\n turtle.forward(radius / 2)\n turtle.left(90)\n turtle.pendown()\n turtle.fillcolor('black')\n turtle.begin_fill()\n circle(pupil_radius)\n turtle.end_fill()\n turtle.penup()\n turtle.left(90)\n turtle.backward(radius / 2)\n turtle.right(90)\n turtle.pendown()\n\n\n\n\ndef eye_test():\n turtle.reset()\n turtle.speed(0)\n # 绘制左眼\n turtle.penup()\n turtle.goto(-60, 0)\n turtle.pendown()\n eye(50)\n # 绘制右眼\n turtle.penup()\n turtle.goto(60, 0)\n turtle.pendown()\n eye(50)\n # turtle.hideturtle() # 隐藏海龟\n # turtle.done()\n\ndef nose(size):\n turtle.speed(0)\n turtle.pensize(3)\n turtle.penup()\n turtle.goto(0, -100)\n turtle.pendown()\n turtle.left(15)\n turtle.forward(size / 2)\n turtle.left(125)\n turtle.forward(size)\n turtle.done()\n\n# 运行测试函数\neye_test()\nnose(60)`

\nWrite a function called `mouth(size, style)`

that draws a (cartoon) mouth as follows:

- \n
- If
`style == 'happy'`

, the mouth is drawn smiling. \n - If
`style == 'sad'`

, the mouth is drawn frowning. \n - If
`style == 'surprised'`

, the mouth is drawn as a circle. \n - For any other value of
`style`

, the mouth is drawn as a neutral expression,

\ne.g. a straight line. \n

Make it so that the bigger `size`

is, the bigger the mouth.

Here are examples of the four mouths:

\n\n`import turtle\nimport math\n\n\ndef polygon(n, size):\n turtle.speed(0)\n if n < 3:\n print(\"错误：n 必须大于等于 3 才能形成多边形。\")\n return\n if size <= 0:\n print(\"错误：size 必须是正数。\")\n return\n angle = 360 / n\n for _ in range(n):\n turtle.forward(size)\n turtle.left(angle)\n\n\ndef circle(radius):\n turtle.speed(0)\n n = 100\n if radius <= 0:\n print(\"错误：半径必须是正数。\")\n return\n size = 2 * radius * math.sin(math.pi / n)\n turtle.penup()\n turtle.forward(radius)\n turtle.left(90)\n turtle.pendown()\n polygon(n, size)\n turtle.penup()\n turtle.right(90)\n turtle.backward(radius)\n turtle.pendown()\n\n\ndef eye(radius):\n turtle.speed(0)\n # 绘制外圈（眼睛）\n circle(radius)\n pupil_radius = radius / 2\n turtle.penup()\n turtle.right(90)\n turtle.forward(radius / 2)\n turtle.left(90)\n turtle.pendown()\n turtle.fillcolor('black')\n turtle.begin_fill()\n circle(pupil_radius)\n turtle.end_fill()\n turtle.penup()\n turtle.left(90)\n turtle.backward(radius / 2)\n turtle.right(90)\n turtle.pendown()\n\n\n\n\ndef eye_test():\n turtle.reset()\n turtle.speed(0)\n # 绘制左眼\n turtle.penup()\n turtle.goto(-60, 0)\n turtle.pendown()\n eye(50)\n # 绘制右眼\n turtle.penup()\n turtle.goto(60, 0)\n turtle.pendown()\n eye(50)\n # turtle.hideturtle() # 隐藏海龟\n # turtle.done()\n\ndef nose(size):\n # turtle.reset()\n turtle.speed(0)\n turtle.pensize(3)\n turtle.penup()\n turtle.goto(0, -100)\n turtle.pendown()\n turtle.left(15)\n turtle.forward(size / 2)\n turtle.left(125)\n turtle.forward(size)\n\ndef jump_to(x, y):\n # 提起画笔，不绘制线条\n turtle.penup()\n # 移动到指定位置\n turtle.goto(x, y)\n # 放下画笔，准备绘制\n turtle.pendown()\n\ndef mouth(size, style):\n turtle.speed(0)\n turtle.pensize(3)\n turtle.hideturtle()\n if style == 'happy':\n jump_to(0, -130)\n turtle.setheading(0)\n turtle.left(25)\n turtle.forward(size)\n \n jump_to(0, -130)\n turtle.setheading(180)\n turtle.right(25)\n turtle.forward(size)\n \n turtle.done()\n elif style == 'sad':\n jump_to(0, -120)\n turtle.setheading(0)\n turtle.right(20)\n turtle.forward(size)\n \n jump_to(0, -120)\n turtle.setheading(180)\n turtle.left(20)\n turtle.forward(size)\n turtle.done()\n elif style == 'surprised':\n jump_to(0, -140)\n circle(30)\n turtle.done()\n else:\n jump_to(0, -125)\n turtle.setheading(0)\n turtle.forward(size)\n jump_to(0, -125)\n turtle.setheading(180)\n turtle.forward(size)\n turtle.done()\n \n \n\n# 运行测试函数\neye_test()\nnose(60)\n# mouth(60, 'happy')\n# mouth(60, 'sad')\nmouth(60, 'surprisssed')`

\nWrite a function called `head(size)`

that draws a (cartoon) head that consists of, at least, two eyes, a random mouth, a nose, and a head (e.g. a circle) all around it. Use the functions you wrote above to draw the eyes, mouth, nose, and

head.

\nChoose the mouth style at random to be one of the 4 mouth shapes. Remember to put `import random`

at the top of your file to get access to the `random`

module.

The size of the head should be controllable using `size`

, e.g. a small value of `size`

should draw a small head, and a big value should draw a big head. Make sure all the eyes, nose, and mouth fit into it snugly and are proportional to the size.

For instance, a head with a surprised face could look like this:

\n\n\nWrite a function called `stick_figure(size)`

that draws a stick figure with a head and body. It should have, at least, a head (using your `head`

function), a torso (maybe just a line), arms, and legs. They can be simple stick figures if you like, or more elaborate.

**Include some randomness in the body beyond just the mouth randomness**. For example, you could chose the color at random, or make the arms point in random directions, etc.

Write a function called `crowd(n, min_size, max_size)`

that draws `n`

stick figures (using your `stick_figure`

function) at random locations on the screen.

The size of each stick figure should be chosen at random in the range `min_size`

to `max_size`

.

For example, here is a result of `crowd(100, 5, 20)`

:

AI悦创·编程一对一

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\n**Due dates:**

- \n
**Implementation: Tuesday 10/08 at 8:59pm**\n**Self-evaluation: Wednesday 10/09 at 11:59pm**\n

Starter files: code.zip

\nThe goal of this assignment is to practice writing a controller. While the model in a program represents the game state, the controller “runs” the program, effectively facilitating it through a sequence of operations. This assignment mimics the style of the previous assignment, in that you are provided an interface that you must implement and adequately test. The class you implement will act as the controller and work with the model that you created in the previous assignment. This controller will “run” a game of Solo Red, asking for input and outputting the game state. Since the game will still be text-based and the input/output will be limited to the console and keyboard, the notion of a “view” will be minimal in this assignment.

\nThere will be two submissions for this assignment:

\n- \n
- Your
*actual implementation*of the controller and full test suite \n - A
*self-evaluation*, due*one day plus three hours later*than the actual implementation, where we will ask you to reflect on your implementation and testing choices. \n

The same late-day policy as on the previous homework applies: each of these three submissions independently may use up to one late day, and you are still able to submit your self-evaluation on time even if you submit your implementation late.

\nYou are expected to use your code from the previous assignment as the starting point for this assignment. **However, please ensure all of your new code is in the cs3500.solored.controller package. Additionally, your code from the previous assignment should remain in the cs3500.solored.model.hw02 and cs3500.solored.view.hw02 packages.**

In HW2, the method `getCanvas`

was documented as throwing an exception when the game is over. In the controller, this is a *hindrance*. You must edit the interface to remove that exception and then remove the relevant code from the implementation. Otherwise the client can never see the final state of the game.

In the previous assignment, you implemented a `SoloRedGameTextView`

class, such that its `toString`

method provided the desired output. This was not ideal design, but it was convenient at the time. For this assignment, you will refine that view to be slightly more flexible. Enhance your class such that it now implements the following interface, which should replace the one given in the previous assignment:

`public interface RedGameView {\n /**\n * Renders a model in some manner (e.g. as text, or as graphics, etc.).\n * @throws IOException if the rendering fails for some reason\n */\n void render() throws IOException;\n}`

\nThis view interface is tiny, but it abstracts away the idea that views are intrinsically `String`

-based.

You should then add a second constructor to your `SoloRedGameTextView`

class, that takes in both a model and an `Appendable`

(see below), and implement `render`

such that it appends the current textual output to that `Appendable`

. *You should preserve* your `toString`

method, since it is useful, but you should *also* implement this `render`

method — it should be very short!

The interface for the Solo Red controller must support the following functionality (as an interface `RedGameController`

that you should place in the `cs3500.solored.controller`

package):

- \n
- A method
`void <C extends Card> playGame(RedGameModel<C> model, List<C> deck, boolean shuffle, int numPalettes, int handSize)`

. This method should play a new game of Solo Red using the provided model, using the`startGame`

method on the model. It should throw an`IllegalArgumentException`

if the provided model is`null`

. It should throw an`IllegalStateException`

**only**if the controller is unable to successfully receive input or transmit output. The nature of input/output will be an implementation detail (see below). It should also throw an`IllegalArgumentException`

if the game cannot be started. \n

Before you implement your controller (see The Controller implementation below), you should first work through examples for it.

\nA suggestion about workload management: You should spend a *reasonable* amount of effort trying to test your controller. If you find yourself getting stuck, switch gears and start working on the implementation, and maybe new testing ideas will occur to you as you work through that implementation. For your own learning, keep notes of which ideas occurred to you before implementing anything, vs which ideas only occurred as a result of trying to implement the controller: is there a pattern of “things you only noticed later” that you might try to deliberately look for sooner, on future projects?

Design a class `SoloRedTextController`

that implements the `RedGameController`

interface above (also in the `cs3500.solored.controller`

package). You will need to:

- \n
- \n
Think about which additional fields and types it needs to implement the promised functionality.

\n \n - \n
Design a constructor

\n`SoloRedTextController(Readable rd, Appendable ap) throws IllegalArgumentException`

. Recall from Lecture 8: Controllers and Mock Objects that`Readable`

and`Appendable`

are two existing interfaces in Java that abstract input and output respectively. The constructor should throw the`IllegalArgumentException`

if and only if either of its arguments are`null`

. Your controller should accept and store these objects for doing input and output. Any input coming from the user will be received via the`Readable`

object, and any output sent to the user should be written to the`Appendable`

object by way of a`RedGameView`

.*Hint:*Look at the`Readable`

and`Appendable`

interfaces to see how to read from and write to them. Ultimately you must figure out a way to transmit a`String`

to an`Appendable`

and read suitable data from a`Readable`

object. The`Scanner`

class will likely be useful, as will the lecture notes on controllers. \n - \n

\n`playGame`

method should play a game. It should “run” the game in the following sequence until the game is over.**Note:**Each*transmission*described below should end with a newline.a.

\n*Transmit*game state to the`Appendable`

object exactly as the view of the model provides it.b.

\n*Transmit*`\"Number of cards in deck: N\"`

, replacing N with the actual number of cards left in the deck.c. If the game is ongoing (i.e. there is more user input and the user hasn’t quit yet), obtain the next user input from the

\n`Readable`

object.**Do not prompt them with a message.**A user input consists of a “move” specified by a move command followed by a sequence of values (separated by any type of whitespace):- \n
- \n

\n`palette`

followed by two natural numbers.\n

\nA natural number is a whole number greater than or equal to zero.

\nThe first number is the index of the palette you want to paint to; and the second number is the index of the card in the hand you want to play. For example, an input of

\n`palette 1 3`

should cause the controller to call the`playToPalette`

method on your model with appropriate inputs to try and play the third card to the first palette.**If the game is not over, the controller should draw until the hand is full or the deck is empty**(see note below). \n - \n

\n`canvas`

followed by one natural number. The number represents the index of the card in the hand you want to play. For example,`canvas 2`

should cause your controller to call the`playToCanvas`

method on your model to play the second card from the hand. \n

d. If the game is over, the method should do the following

\n- \n
*transmit*the message`\"Game lost.\"`

if the player lost or the message`\"Game won.\"`

if the player won. \n*transmit*the final game state one last time \n*transmit*the message`\"Number of cards in deck: N\"`

as before \n

The method should then end.

\n \n - \n

**Key points:**

- \n
**User input numbering:**To make the inputs more user-friendly, all indices in the user provided input begin from 1. This will affect the inputs that your controller passes along to your model. \n**Quitting:**If at*any*point, the next value is either the letter`'q'`

or the letter`'Q'`

, the controller should*transmit*the following in order: the message`\"Game quit!\"`

, the message`\"State of game when quit:\"`

, the current game state, and the message`\"Number of cards in deck: N\"`

with*N*replaced by the remaining number of cards left in the deck. The method should then end. For example: \n

`Game quit!\nState of game when quit:\nCanvas: R\nP1: R1\n> P2: O2 R7\nP3: V2\nHand: V1 I2 O6 R3\nNumber of cards in deck: 4`

\n- \n
- \n

\n**Bad inputs:**If any individual value is unexpected (i.e. something other than`'q'`

,`'Q'`

or a number) it should wait for the user to re-enter that value again. For example, if the user is trying to play to a palette , and has entered the palette index correctly, but entered the card index incorrectly, the controller should continue attempting to read a value for the card index before continuing. For another example, if the user is trying to play to a palette and has entered the palette index incorrectly, the controller should continue attempting to read a value for the palette index before moving on to reading for the card index. The controller should behave similarly for the other commands. Once all the numbers are successfully read, if the model indicates the move is invalid, the controller should*transmit*a message to the`Appendable`

object saying`\"Invalid move. Try again. X\"`

where*X*is any informative message about why the move was invalid (all on one line), and resume waiting for valid input.*Hint:*You should probably design a helper method to retry reading inputs until you get a number or a`'q'`

/`'Q'`

. Using that helper consistently will make it much easier to implement the desired retrying behavior described here. That helper probably should*not*be responsible for determining if a number is a valid coordinate — that’s the model’s job — but that helper does need to return either the user’s number*or*their desired to quit the game. Think carefully about the signature of this method before you start implementing it...If an input is not a valid command for the game (i.e. something other than

\n`'q'`

,`'Q'`

,`'palette'`

, or`'canvas'`

), the controller should*transmit*a message to the`Appendable`

object saying`\"Invalid command. Try again. X\"`

where*X*is some helpful message (all in one line). Then the controller should try reading the next value for a command. \n - \n

\n**Error handling:**The`playGame`

method should throw an`IllegalArgumentException`

if a`null`

model is passed to it. If the`Appendable`

object is unable to transmit output or the`Readable`

object is unable to provide inputs (for whatever reason), the`playGame`

method should throw an`IllegalStateException`

to its caller. The`playGame`

method**must not**throw any other exceptions, nor should it propagate*any*exceptions thrown by the model. \n - \n

\n**Write sufficient tests**to be confident that your code is correct.**Note:**once the model has been tested thoroughly (which you hopefully did in the previous assignment), all that remains to be tested is whether the controller works correctly in all cases. Lecture 8: Controllers and Mock Objects will prove to be helpful. \n

Be sure to properly document your code with Javadoc as appropriate. Method implementations that inherit Javadoc need not provide their own unless their contract differs from the inherited documentation. Finally, **no method in your implementation should exceed 50 lines. This hampers clarity of your code.**

**If you had to change your implementation from the previous assignment, please document your changes in a README file (a plain text file) that explains what you changed and why. This doesn’t have to be long; a simple bullet-point list will suffice. But having this documentation will make your TAs’ grading job a lot easier!**

You will need to add additional tests to assess whether your controller works *regardless of whether your model works*. (Again, if you’ve sufficiently tested your model in the previous assignment, then you can rely on your model working here.) You will likely need to use the techniques in Lecture 8: Controllers and Mock Objects.

You should create your primary test class in the `cs3500.solored`

package. This test is outside your controller package, and so can only test the public-facing behaviors of your controller. If you want to test internal implementation details as well, you should create one more test class in the `cs3500.solored.controller`

package, so that you can check protected and package-private implementation details if needed.

Be mindful of which test cases you place in which test class! *Technically,* you could run all the tests from a single class. But using multiple classes like this helps convey to the reader of your code some of your thought processes behind each test: the reader should understand the examples first, then look at the tests of public behavior, and finally look at implementation-specific fiddly details.

**Note:** When you submit your full implementation, you will see automated tests that *I* wrote and run against *your* code. I gave some of my test methods mnemonic names, so that you can try to deduce what my tests are checking for. Just because *I* have a test for a given scenario, though, does not mean that you shouldn’t write your own test case to confirm your understanding!

- \n
- For your implementation: submit a properly-structured zip containing at minimum\n
- \n
- The model interface (
`RedGameModel.java`

) \n - Your implementation of the model (
`SoloRedGameModel.java`

) \n - The view interface (
`RedGameView.java`

) \n - Your implementation of the view (
`SoloRedGameTextView.java`

) \n - The controller interface (
`RedGameController.java`

) \n - Your implementation of the controller (
`SoloRedTextController.java`

) \n - Any additional classes necessary to compile your code \n
- All your tests for all your implementations in one or more JUnit test classes. You should include at least all your tests from the previous assignment, and add to them... \n
- A brief README file (a plain text file) explaining what changes from the previous assignment you made, and why. \n

\n - The model interface (

As with the previous assignment, please submit a zip containing *only* the `src/`

and `test/`

directories with *no surrounding directories*, so that the autograder recognizes your package structure. **Please do not include your output/, out, or .idea/ directories — they’re not useful and we will deduct points if you do!**

For this assignment, you will be graded on

\n- \n
- Whether your interfaces specify all necessary operations with appropriate method signatures, \n
- Whether your code implements the specifications (functional correctness), \n
- the clarity of your code, including length of your methods as established in this assignment and documentation \n
- How well your code follows the design principles discussed so far and the relevant principles in the Design Principles Masterlist, \n
- The comprehensiveness of your test coverage, and \n
- How well you follow the style guide. \n

Please submit your homework by the above deadline. Then be sure to complete your self evaluation by the second deadline.

\nAI悦创·编程一对一

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\n- \n
- Submit your responses in a single knitted file, following the Homework Structure which requires model outputs, plots, and code for all questions to be submitted in a single
**HTML file**by**Sept 29 at 11:59 p.m. Eastern****. [Time converter]** \n - The solutions will be made available on
**Oct 1 at 1:00 a.m. Eastern**. You will need to assess**three**of your peers to complete the Data Analysis by**Oct 6 at 11:59 p.m. Eastern****. Students who do not submit the graded assessments before the deadline will receive a zero on their own Data Analysis.** \n **Before you begin, please review the following document:**DONT_CHEAT.docx. Cheating is not helping you learn and be successful. It does the opposite. While you may collaborate with other students, please submit your own responses to the data analysis questions. Please refrain from consulting prior homework solutions or other materials that provide answers to the data analysis questions. Any case identified as potential plagiarism will result in a zero grade for the assignment and it will be reported to the OMSA program. \n

**To maintain the integrity of this course:**

**1. Do not plagiarize (even if it is a particular question). This is an automatic zero for the entire HW and your lowest HW grade will not be dropped.**

**2. Do not use any AI tools such as chatGPT or CoPiliot.**

**3. Non-HTML submissions are not accepted. Submitting the wrong file type is an automatic zero.**

**Data Analysis (60 points)**

For this assignment, you are provided with **templates in R and python languages**:

Fall2024-HW2-Starter-Template-Python.ipynbDownload Fall2024-HW2-Starter-Template-Python.ipynb

\nFall2024-HW2-Starter-Template-R.RmdDownload Fall2024-HW2-Starter-Template-R.Rmd

\nFall2024-HW2-Starter-Template-R.ipynbDownload Fall2024-HW2-Starter-Template-R.ipynb

\nIn homework 2, we again visit the sales dataset used in homework 1.

\nlarge_sales_dataset.csvDownload large_sales_dataset.csv

\nAs a reminder, the dataset contains the following columns:

\n- \n
- Customer_ID: Unique identifier for each customer. \n
- Product_Category: Category of purchased product (e.g. ‘Books’, ‘Electronics’, ‘Toys’, ‘Clothing’, etc.). \n
- Purchase_Amount: Total amount, in dollars, spent on each purchase. \n
- Purchase_Date: Date of transaction. \n
- Customer_Age: Age of customer at time of purchase. \n
- Customer_Gender: Gender of the customer (‘Male’ or ‘Female’). \n
- Store_Location: Location of store where purchase occurred. \n
- Satisfaction_Score (response variable) : Numeric response variable indicating customer’s satisfaction level from 0 (least satisfied) to 10 (most satisfied). \n

**set.seed(17)** # Seed set to ensure reproducible results. DO NOT CHANGE SEED.

1a) Create a dataframe, “sales_data”, from “large_sales_dataset.csv”. The following columns should be treated as categorical variables: “Prodcuct_Category”, “Customer_Gender”, “Store_Location”. Column “Purchase_Date” should be in an appropriate date format. Sort “sales_data” by the Satisfaction_Score (from low to high), print the first 10 rows of the dataframe, then print minimum, maximum, 10th percentile, 25th percentile, 50th percentile, 75th percentile, 90th percentile, average, and median Satisfaction_Score. Note: the sorting should be permanently (not temporarily) stored unless otherwise stated. (3 points)

\n1b) Suppose management is concerned that specific customers with frequent purchases may skew sales results. How many customers have made more than 5 purchases? List the customer IDs of customers that have made more than 5 purchases. Tip: do not list IDs of customers that have made exactly 5 purchases, only list those greater than 5 purchases (2 points)

\n1c) If the customers with more than 5 purchases have made more than 5 purchases at a single Store_Location, management would like to exclude these customers from future analyses. For each customer identified in 1b, list the Store_Location where they made purchases. Have the customers identified in question 1b made more than 5 purchases at a single Store_Location? (1 point)

\n2a) Create a well-labeled stacked bar chart showing the proportion of purchases from each Product_Category in each Store_Location. Note: Store_Location should be on the x-axis, proportion of purchases should be on the y-axis, and the stacked bar chart should be colored by Product_Category which should also be the legend. The proportion should be based on number of purchases, not on the purchase amount. The proportion is a value between 0 and 1 and is not equal to the number of purchases (3 points)

\n2b) What Product_Category accounts for the lowest cumulative purchase amount (use values in “Purchase_Amount”)? (1 point)

\n2c) Which Store_Location has the highest proportion of furniture purchases? The proportion should be based on number of purchases, not on purchase amount. (1 point)

\n2d) Which Store_Location has the lowest total purchases (use amounts in “Purchase_Amount”)? (1 point)

\n2e) Unfortunately, let’s imagine that management has decided to close the Denver store location and also stop selling furniture across all store locations. Create a new dataframe called “filtered_sales_data”, where rows that contain “Denver” in “Store_Location” or “Furniture” in “Product_Category” are removed. The resulting dataframe must have 812 observation rows and 8 variables. Sort filtered_sales_data by Customer_ID in descending order and print rows 150 to 160 of the resulting dataframe. Note: the sorting should be permanently (not temporarily) stored unless otherwise stated. (3 points)

\n3a) Using the dataframe “filtered_sales_data” from Question 2e), split filtered_sales_data into a 20% test set and a 80% training set. Store the test set into “test_sales_data” and the training set into “train_sales_data”. Sort the train and test sets by Purchase_Date in ascending order, then remove the row names and print the first 5 rows of both the test_sales_data and train_sales_data. We will be using these going forward. Note: the sorting should be permanently (not temporarily) stored unless otherwise stated. Note: The resulting test dataframe must have 162 observation rows and 8 variables while the training dataframe must have 650 observation rows and 8 variables. The seed was already set earlier in the notebook so do not change the seed and there is no need to re-set the seed. Tip: Use sample.int(). Use the ceiling function in the size argument. (3 points)

\n3b) Using “train_sales_data”, create an Hexbin Plot of the response, “Satisfaction_Score” against “Purchase_Amount”. Calculate then output the R-squared and comment on the direction (positive or negative) and strength of the correlation (<=0.3: weak; >0.3 and <0.7: moderate; >=0.7: strong). (2 points)

\n3c)Using “train_sales_data”, create a boxplot of the response, Satisfaction_Score, vs. Purchase_Month. You will have to calculate Purchase_Month by using Purchase_Date. Ensure Purchase_Month is a categorical variable and ranges from 01 - 12. From observation of the boxplot, what month has the highest median satisfaction score? Using just the resulting boxplot, is the satisfaction score for any of the month significantly different from the satisfaction scores of the other months? Note: we will now have 9 variables (since we added Purchase_Month) in the resulting train and test dataframes (3 points)

\n4a) Using train_sales_data, create a multiple linear regression model and call it “model1” with Satisfaction_Score as the response then Product_Category, Purchase_Amount, Customer_Age, Customer_Gender, Store_Location, and Purchase_Month as the predictors. Print the model summary. Is the model of any use in predicting Satisfaction_Score? using α = 0.05, provide the following elements of the test of overall regression of the model: null hypothesis H0, alternative hypothesis Ha, F-statistic or p-value, and conclusion (3 points)

\n4b) Using α = 0.05, which of the estimated coefficients are statistically significant in model1 (2 points)

\n4c) What is the interpretation of the intercept coefficient with respect to the satisfaction score? In other words, what does the intercept represent for this particular model (2 points)

\n4d) What is the interpretation of the Purchase_Amount coefficient? (2 points)

\n4e) Create scatter plots of the standardized residuals of model1 vs. the quantitative predictors (Purchase_Amount, Customer_Age). Using these plots, does the linearity assumption hold for these predictors? (2 points)

\n4f) Create a scatterplot of the standardized residuals vs. the fitted values of model1. Does the constant variance assumption hold? Does the assumption of uncorrelated errors hold? (2 points)

\n4g) Create a histogram and normal QQ plot of the standardized residuals. A 95% pointwise confidence envelope must be used for the QQ plot. Does the assumption of normality hold? (2 points)

\n5a) Calculate the VIF of each predictor in model1. Using a VIF threshold of **max(10,1/(1−R2))** what conclusion do you make regarding multicolinearity? (2 points)

5b) Create a plot of Cook’s distances for model1. Using a threshold of 4/n, print the calculated threshold. Are there any outliers present? How many outliers do we have? Output the highest 5 outlier rows from train_sales_data (since we used train_sales_data to create model1). (5 points)

\n5c) Create a new dataframe from train_sales_data and call it train_sales_data_cook. This dataframe should exclude the outliers identified in Question (5b) above. Sort the dataframe by Purchase_Amount in descending order and print the first 5 rows. Note: the sorting should be permanently (not temporarily) stored unless otherwise stated. (2 points)

\n6a) Calculate the 95% confidence interval for the regression coefficient of Customer_Age in model1. What can you conclude from the 95% confidence interval for the Customer_Age coefficient? (1 point)

\n6b) Using train_sales_data, create a new model and call it “model_no_age” with the same response and predictors as “model1”, but without the predictor Customer_Age. Print the model summary. (1 point)

\n6c) Perform a partial F-test between model_no_age and model1. What is the F-value and the corresponding p-value of the test? What is your interpretation of the test (use an alpha level of 0.05 when evaluating the p-value)? (3 points)

\n6d) Using the dataset created in Question (5c) after our outlier analysis i.e. train_sales_data_cook, create a new model and call it “model_outlier”, with Satisfaction_Score as the response and Product_Category, Purchase_Amount, Customer_Gender, Store_Location and Purchase_Month as the predictors (note that we are excluding Customer_Age compared to model1). Print the model summary. (1 point)

\n7a) Use model1, model_no_age, and model_outlier to predict the Satisfaction_Score for each row in test_sales_data. Calculate and output the mean squared prediction error (MSPE) for each model’s predictions. You will have to add Purchase_Month to test_sales_data as you did to train_sales_data. Which model has the lower MSPE? (2 points)

\n7b) Print the Adjusted R-squared for model1, model_no_age, and model_outlier? Comment on what you observed considering what you also observed from the mean squared prediction error (MSPE) from Question (7a) (1 point)

\n7c) Suppose there is a purchase with the following data: Product_Category: Electronics; Purchase_Amount: 574.99; Customer_Gender: Female; Customer_Age: 39; Store_Location: San Francisco; Purchase_Month: 06. Use model1 to predict the Satisfaction_Score of the purchase with a 95% prediction interval. What is the interpretation of this interval? (2 points)

\nWrite (with explanations) separate R codes where model.matrix() and as.factor() is used in pre-processing and another where only as.factor() is used in pre-processing on the R built-in “mtcars” dataset (since it has both quantitative and qualitative prediction variables). Build two multiple linear regression models using those pre-processing techniques with `mpg`

(miles per gallon) as the response variable, `cyl`

(number of cylinders, a qualitative variable), and `hp`

(horsepower, a quantitative variable) as predictors. Compare and explain any similarities and differences between the model summary outputs for these 2 models. Tip: For those using python, you load the mtcars dataset from R into a pandas DataFrame in Python, allowing you to work with it using familiar Python tools. (2 points)

AI悦创·编程一对一

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\nThis project is inspired by a genre of video game, where players choose the actions that their characters take in a turn-based battle. Some examples include the battle systems in Super Mario RPG, Pokémon, Final Fantasy and table-top games like Dungeons and Dragons.

\n\nSuper Mario RPG Battle

\nThe characters will have various stats, such as health or hp, that will decrease when the opponent's characters successfully attack.

\n\nPokémon Battle, https://essentialsdocs.fandom.com/wiki/Battles

\nDuring the player's turn, they pick actions for their characters. The player needs to consider their strategy and some actions will be more effective than others in different situations.

\n\nFinal Fantasy Battle System, https://finalfantasy.fandom.com/wiki/Battle_system

\nSome games have special action types that use magic or some other special resource. Many games have an item system, where the player can choose to use an item that they have obtained that will have some special effects.

\n\nThis project will ask you to complete the following tasks:

\n- \n
- In task 1 you will be producing a csv file with information about the action history of a game. \n
- In task 2 you will be loading in a saved game state from a file. \n
- In task 3 you will be checking whether an action list is valid, given the state of the game. \n
- In task 4 you will be doing some image manipulation to create stat bars for the game. \n
- In task 5 you will be creating a player agent to play against other agent in a tournament \n

Your player has a team of five characters, up to two of which can be actively fighting for your side at any time. Each character has its own attack and defend moves, so it is important to select (or swap during battle) to form a suitable pair to face your opponent's pair.

\nFor each turn, you will decide on an action for each active character. This might be to attack using one of your character's attack moves, defend with a defensive move, use an item, or swap to make a different character from your team active. Your opponent will also choose actions for their own active characters.

\nYour characters have some stats that begin the match as full and will be affected by the actions you and your opponent take. One of these is health, which is individual for each character and can be lowered by things like your opponent's attacks or a move backfiring. The other is electricity, which is used up by choosing actions that have an electricity cost and is shared across your team.

\nIn this game, the player will select up to one action for each active character they currently have (active at the start of the turn). An action can be an attack, a defend move, using an item, or swapping an active character out for one of their other characters.

\nWe will represent each action as a tuple of varying length (depending on the type of action) and the first two elements of the tuple are the name of the character performing the action and then the action type.

\nFor the specific action types:

\n- \n
- An attack action has the name of the character performing the action, the action type (
`'attack'`

), the attack move name, the damage it can inflict, the number of opponent active characters it can target, the electricity cost, and a list of the opponent active characters targeted. \n - A defend action has the name of the character performing the action, the action type (
`'defend'`

), the defend move name, the amount of protection provided, and the electricity cost. \n - An item action has the name of the character using the item, the action type (
`'item'`

), and the item name. \n - A swap action has the name of the character being swapped out, the action type (
`'swap'`

), and the name of the character being swapped in. \n

**汇总出来的元组：**

`attack = (角色名称, 动作类型, 攻击名称「技能名称」, 可以造成的伤害, 攻击数量, 电力成本, 攻击的目标)\ndefence = (角色名称, 动作类型, 防御动作的名称, 防御数量, 电力成本)\n赋予使用工具 = ('Python Pal', 'item', 'Debugging Tool')`

\n`action1 = ('Network Ninja', 'attack', 'DDoS', 3, 1, 1, ['Python Pal'])`

\nThe above action is an attack action, where Network Ninja is performing DDoS attack on Python Pal. This action can inflict 3 damage to a single opponent active character and will cost 1 electricity.

\n`action2 = ('HTML Hero', 'defend', 'Firewall', 2, 0)`

\nIn action 2, we see HTML Hero using a defend action called Firewall. This can provide 2 protection and is free (0 electricity!).

\n`action3 = ('Python Pal', 'item', 'Debugging Tool')`

\nHere in action 3, Python Pal is using an item called Debugging Tool.

\n`action4 = ('HTML Hero', 'swap', 'Binary Bot')`

\nAction 4 is a swap action - The active character, HTML Hero, is swapping places with Binary Bot, so Binary Bot will become an active character and HTML Hero becomes inactive.

\nFor this task you will be calculating some statistics about the game based on the given history of all actions played so far.

\nThe `history`

is a list, with each element being one of the players actions for that turn. There are two players who alternate turns. Player 0 first actions are at `history[0]`

, then player 1's actions are at `history[1]`

, then player 0's next actions are at `history[2]`

, etc.

The player's actions for that turn are formatted as a list of actions in the order performed. See the previous slide for how we represent actions in this game.

\nFor this part of the task, write a function called `calculate_comprpg_stats(history, filename)`

that takes the history and a string `filename`

and calculates some statistics on the actions played in the game.

Your function should calculate the number of attacks, defends, items, swaps, attempted damage, attempted protection, electricity used, and turns taken for each player based on game history and save this in csv format to the specified file name.

\nYou may assume that the actions are formatted correctly and are the correct length for their action type.

\n`>>> history = [[('Haskell Heroine', 'defend', 'Recursion Rebuff', 2, 0), ('Python Pal', 'item', 'Screen Repair Kit')], [('Linux Legend', 'attack', 'Root Reckoning', 4, 2, 5, ['Haskell Heroine', 'Python Pal']), ('Binary Bot', 'item', 'Debugging Tool')], [('Haskell Heroine', 'attack', 'Lambda Lunge', 3, 1, 0, ['Linux Legend']), ('Python Pal', 'item', 'RAM Boost')], [('Linux Legend', 'attack', 'Kernel Kick', 4, 1, 3, ['Python Pal']), ('Binary Bot', 'swap', 'Network Ninja')]]\n>>> calculate_comprpg_stats(history, \"example_game1_stats.csv\")`

\nShould produce the file:

\n`player,attacks,defends,items,swaps,attempted damage,attempted protection,electricity used,turns taken\n0,1,1,2,0,3,2,0,2\n1,2,0,1,1,8,0,8,2`

\n`>>> history = [[('C Charmer', 'item', 'RAM Boost'), ('Binary Bot', 'swap', 'Linux Legend')], [('Hardware Hacker', 'swap', 'HTML Hero')], [('C Charmer', 'attack', 'Segfault Slam', 2, 2, 2, ['HTML Hero', 'Python Pal']), ('Linux Legend', 'attack', 'Bash Bonk', 2, 1, 0, ['HTML Hero'])], [('HTML Hero', 'attack', 'Entity Eruption', 2, 2, 2, ['C Charmer', 'Linux Legend']), ('Python Pal', 'attack', 'Syntax Strike', 4, 1, 3, ['Linux Legend'])], [('C Charmer', 'defend', 'Buffer Overflow Block', 7, 5), ('Linux Legend', 'defend', 'Firewall', 2, 0)], [('HTML Hero', 'swap', 'Haskell Heroine')], [('C Charmer', 'swap', 'Binary Bot')], [('Haskell Heroine', 'attack', 'Lambda Lunge', 3, 1, 0, ['Binary Bot']), ('Python Pal', 'swap', 'C Charmer')], [('Binary Bot', 'attack', 'Keyboard Bash', 2, 1, 0, ['C Charmer']), ('Linux Legend', 'attack', 'Root Reckoning', 4, 2, 5, ['C Charmer', 'Haskell Heroine'])], [('Haskell Heroine', 'attack', 'Monadic Might', 5, 1, 2, ['Linux Legend']), ('C Charmer', 'defend', 'Compiler Crash', 3, 0)]]\n>>> calculate_comprpg_stats(history, \"example_game2_stats.csv\")`

\nShould produce the file:

\n`player,attacks,defends,items,swaps,attempted damage,attempted protection,electricity used,turns taken\n0,4,2,1,2,10,9,12,5\n1,4,1,0,3,14,3,7,5`

\nAI悦创·编程一对一

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\nThree devices are monitored until failure. The observed lifetimes are 1.1, 2.2, and 0.4 years. If the lifetimes are modeled as exponential distribution with rate $\lambda $λ,

\n{T}_{i}\sim \text{Exp}\left(\lambda \right),\phantom{\rule{\"1em\"}{0ex}}f(t\mid \lambda )=\lambda {e}^{-\lambda t},\phantom{\rule{\"1em\"}{0ex}}t0,\lambda 0Ti∼Exp(λ),f(t∣λ)=λe−λt,t>0,λ>0

\nAssume an exponential prior on $\lambda $λ:

\n$\lambda \sim \text{Exp}\left(2\right),\phantom{\rule{\"1em\"}{0ex}}\pi \left(\lambda \right)=2{e}^{-2\lambda},\phantom{\rule{\"1em\"}{0ex}}\lambda 0$λ∼Exp(2),π(λ)=2e−2λ,λ>0

\n(a) Find the posterior distribution of $\lambda $λ.

\n(b) Find the Bayes estimator for $\lambda $λ.

\n(c) Find the MAP estimator for $\lambda $λ.

\n(d) Numerically find both the equi-tailed and highest posterior density credible sets for $\lambda $λ, at the 95% credibility level.

\n(e) Find the posterior probability of hypothesis ${H}_{0}:\lambda \le 1{/}2$H0:λ≤1/2.

\nLet

\n{y}_{i}\mid {\theta}_{i}{\sim}^{ind{.}}\text{Poisson}\left({\theta}_{i}\right)yi∣θi∼ind.Poisson(θi)

\n{\theta}_{i}{\sim}^{iid}\text{Gamma}(2,b)θi∼iidGamma(2,b)

\nfor $i=1,\dots ,n$i=1,…,n, where $b$b is unknown. Find the empirical Bayes estimator of ${\theta}_{i},i=1,\dots ,n$θi,i=1,…,n. (Note: If $X\sim \text{Gamma}(a,b)$X∼Gamma(a,b), then its pdf is

\np\left(x\right)=\frac{{b}^{a}}{{\Gamma}\left(a\right)}{x}^{a-1}{e}^{-bx}\text{for}x\ge 0,a,b0.p(x)=Γ(a)baxa−1e−bx for x≥0,a,b>0.

\n要找到 ${\theta}_{i}$θi 的经验贝叶斯估计，需要以下步骤：

\n**1. 计算后验分布：**

给定似然函数和先验分布：

\n- \n
- \n

\n**似然函数：**P({y}_{i}\mid {\theta}_{i})=\frac{{\theta}_{i}^{{y}_{i}}{e}^{-{\theta}_{i}}}{{y}_{i}!}P(yi∣θi)=yi!θiyie−θi

\n \n - \n

\n**先验分布：**p\left({\theta}_{i}\right)=\frac{{b}^{2}}{{\Gamma}\left(2\right)}{\theta}_{i}^{2-1}{e}^{-b{\theta}_{i}}={b}^{2}{\theta}_{i}{e}^{-b{\theta}_{i}}p(θi)=Γ(2)b2θi2−1e−bθi=b2θie−bθi

\n \n

因此，后验分布为：

\np({\theta}_{i}\mid {y}_{i})\propto P({y}_{i}\mid {\theta}_{i})p\left({\theta}_{i}\right)={\theta}_{i}^{{y}_{i}}{e}^{-{\theta}_{i}}\cdot {\theta}_{i}{e}^{-b{\theta}_{i}}={\theta}_{i}^{{y}_{i}+1}{e}^{-(b+1){\theta}_{i}}p(θi∣yi)∝P(yi∣θi)p(θi)=θiyie−θi⋅θie−bθi=θiyi+1e−(b+1)θi

\n这表明后验分布是一个新的 Gamma 分布：

\n{\theta}_{i}\mid {y}_{i}\sim \text{Gamma}({y}_{i}+2,b+1)θi∣yi∼Gamma(yi+2,b+1)

\n**2. 计算后验均值：**

后验均值（即贝叶斯估计）为：

\nE[{\theta}_{i}\mid {y}_{i}]=\frac{{y}_{i}+2}{b+1}E[θi∣yi]=b+1yi+2

\n**3. 估计超参数 ( b )：**

为了应用经验贝叶斯方法，我们需要估计未知的超参数 ( b )。首先，计算边缘似然函数：

\nP\left({y}_{i}\right)={\int}_{0}^{{\infty}}P({y}_{i}\mid {\theta}_{i})p\left({\theta}_{i}\right)d{\theta}_{i}=\frac{{b}^{2}({y}_{i}+1)!}{(b+1{)}^{{y}_{i}+2}{y}_{i}!}P(yi)=∫0∞P(yi∣θi)p(θi)dθi=(b+1)yi+2yi!b2(yi+1)!

\n因此，样本的对数似然函数为：

\n\mathrm{log}L\left(b\right)=2n\mathrm{log}b-(S+2n)\mathrm{log}(b+1)+\sum _{i=1}^{n}\mathrm{log}({y}_{i}+1)logL(b)=2nlogb−(S+2n)log(b+1)+i=1∑nlog(yi+1)

\n其中 $S={\sum}_{i=1}^{n}{y}_{i}$S=∑i=1nyi。

\n对 ( b ) 求导并令导数为零，得到：

\n\frac{d}{db}\mathrm{log}L\left(b\right)=\frac{2n}{b}-\frac{S+2n}{b+1}=0dbdlogL(b)=b2n−b+1S+2n=0

\n解方程得到 ( b ) 的估计值：

\n\hat{b}=\frac{2n}{S}b^=S2n

\n**4. 计算经验贝叶斯估计：**

将 $\hat{b}$b^ 代入后验均值，得到经验贝叶斯估计：

\n{\hat{\theta}}_{i}=E[{\theta}_{i}\mid {y}_{i},\hat{b}]=\frac{{y}_{i}+2}{\hat{b}+1}=({y}_{i}+2)\frac{S}{2n+S}θ^i=E[θi∣yi,b^]=b^+1yi+2=(yi+2)2n+SS

\n**最终答案：**

经验贝叶斯估计为：

\n{\hat{\theta}}_{i}=({y}_{i}+2)\cdot \frac{\sum _{j=1}^{n}{y}_{j}}{2n+\sum _{j=1}^{n}{y}_{j}}θ^i=(yi+2)⋅2n+∑j=1nyj∑j=1nyj

\nSuppose $y\mid \beta \sim \text{Gamma}(\alpha ,\beta )$y∣β∼Gamma(α,β), where $\alpha $α is known.

\n(a) Find the Jeffreys prior for $\beta $β.

\n(b) Using the Jeffreys prior from Part 1, derive the posterior distribution $p(\beta \mid {y}_{1},\dots ,{y}_{n})$p(β∣y1,…,yn) for $n$n i.i.d. observations ${y}_{1},\dots ,{y}_{n}$y1,…,yn.

\n**(a) 求 Jeffreys 先验分布：**

给定条件 $y\mid \beta \sim \text{Gamma}(\alpha ,\beta )$y∣β∼Gamma(α,β)，其中 $\alpha $α 已知。

\n首先，写出似然函数：$f(y\mid \beta )=\frac{{\beta}^{\alpha}}{{\Gamma}\left(\alpha \right)}{y}^{\alpha -1}{e}^{-\beta y}$f(y∣β)=Γ(α)βαyα−1e−βy

\n计算对数似然函数：$\mathrm{ln}f(y\mid \beta )=\alpha \mathrm{ln}\beta -\mathrm{ln}{\Gamma}\left(\alpha \right)+(\alpha -1)\mathrm{ln}y-\beta y$lnf(y∣β)=αlnβ−lnΓ(α)+(α−1)lny−βy

\n计算关于 $\beta $β 的一阶导数：$\frac{{\partial}}{{\partial}\beta}\mathrm{ln}f(y\mid \beta )=\frac{\alpha}{\beta}-y$∂β∂lnf(y∣β)=βα−y

\n计算关于 $\beta $β 的二阶导数：$\frac{{{\partial}}^{2}}{{\partial}{\beta}^{2}}\mathrm{ln}f(y\mid \beta )=-\frac{\alpha}{{\beta}^{2}}$∂β2∂2lnf(y∣β)=−β2α

\nFisher 信息量为二阶导数的负期望值：$I\left(\beta \right)=-E[\frac{{{\partial}}^{2}}{{\partial}{\beta}^{2}}\mathrm{ln}f(y\mid \beta \left)\right]=\frac{\alpha}{{\beta}^{2}}$I(β)=−E[∂β2∂2lnf(y∣β)]=β2α

\n因此，Jeffreys 先验分布为：$\pi \left(\beta \right)\propto \sqrt{I\left(\beta \right)}=\sqrt{\frac{\alpha}{{\beta}^{2}}}\propto \frac{1}{\beta}$π(β)∝I(β)=β2α∝β1

\n**答案：** Jeffreys 先验分布为 $\pi \left(\beta \right)\propto {\scriptscriptstyle \frac{1}{\beta}}$π(β)∝β1。

**Instructions:**

- \n
- Print your First and Last name and NetID on your answer sheets \n
- Submit all your answers including Python scripts and report in a single Jupyter Lab file (.ipynb) or along with a single PDF to Brightspace by due date. No other file formats will be graded. No late submission will be accepted. \n
- Total 5 problems. Total points: 100 \n

Consider the following computer output.

\n`The regression equation is Y = 254 + 2.77 x1 - 3.58 x2\n\nPredictor Coef SE Coef T\nConstant 253.810 4.781 ?\nx1 2.7738 0.1846 15.02\nx2 -3.5753 0.1526 ?\n\nS = 5.05756 R-Sq = ? R-Sq (adj) = 98.4%\n\nAnalysis of Variance\nSource DF SS MS F\nRegression 2 22784 11392 ?\nResidual error ? ? ? ?\nTotal 14 23091`

\n(a) Fill in the missing quantities.

\n(b) What conclusions can you draw about the significance of regression?

\n(c) What conclusions can you draw about the contributions of the individual regressors to the model?

\n*Note: check the critical value in the F-distribution or t-distribution table.*

`The regression equation is Y = 254 + 2.77 x1 - 3.58 x2\n\nPredictor Coef SE Coef T\nConstant 253.810 4.781 53.08\nx1 2.7738 0.1846 15.02\nx2 -3.5753 0.1526 -23.44\n\nS = 5.05756 R-Sq = 98.7% R-Sq (adj) = 98.4%\n\nAnalysis of Variance\nSource DF SS MS F\nRegression 2 22784 11392 445.3\nResidual error 12 307 25.5833 —\nTotal 14 23091`

\nA study was performed on wear of a bearing and its relationship to ${x}_{1}=$x1= oil viscosity and ${x}_{2}=$x2= load. The data can be found in attached file **bearingdata.csv**.

`bearingdata.csv:\ny,x1,x2\n293,1.6,851\n230,15.5,816\n172,22,1058\n91,43,1201\n113,33,1357\n125,40,1115\n,,`

\n**(a)** Fit a multiple linear regression model in the form of $y={\beta}_{0}+{\beta}_{1}{x}_{1}+{\beta}_{2}{x}_{2}+\epsilon $y=β0+β1x1+β2x2+ε. Write out the estimated model.

**(b)** Estimate ${\sigma}^{2}$σ2 and compute the *t*-statistics for each regression coefficient. Using $\alpha =0.05$α=0.05, what conclusions can you draw?

**(c)** Test for significance of overall regression using $\alpha =0.05$α=0.05. What is the *P*-value for this test? What are your conclusions?

**(d)** Use the model to predict wear when ${x}_{1}=25$x1=25 and ${x}_{2}=1000$x2=1000.

**(e)** Use the extra sum of squares method to investigate the usefulness of adding ${x}_{2}=$x2= load to a model that already contains ${x}_{1}=$x1= oil viscosity. Use $\alpha =0.05$α=0.05.

**(f)** Refit the model with an interaction term. Test for significance of regression using $\alpha =0.05$α=0.05.

**(g)** Use the extra sum of squares method to determine whether the interaction term contributes significantly to the model. Use $\alpha =0.05$α=0.05.

We have used a sample of 30 observations to fit a regression model. The full model has 9 regressors, the variance estimate is ${\hat{\sigma}}^{2}=MSE=100$σ^2=MSE=100, and ${R}^{2}=0.92$R2=0.92.

\n**(a)** Calculate the *F*-statistic for testing significance of regression. Using $\alpha =0.05$α=0.05, what would you conclude?

**(b)** Suppose that we fit another model using only four of the original regressors and that the error sum of squares for this new model is 2200. Find the estimate of ${\sigma}^{2}$σ2 for this new reduced model. Would you conclude that the reduced model is superior to the old one? Why?

**(c)** Find the value of ${C}_{p}$Cp for the reduced model in part (b). Would you conclude that the reduced model is better than the old model?

Use the Carseats data set (attached Carseats.csv) to answer the following questions.

\n**(a)** Fit a multiple regression model to predict Sales using Price, Urban, and US.

**(b)** Provide an interpretation of each coefficient in the model.

**(c)** Write out the model in equation form, show the qualitative variables properly.

**(d)** For which of the predictors can you reject the null hypothesis ${H}_{0}:{\beta}_{j}=0$H0:βj=0?

**(e)** On the basis of your response to the previous question, fit a smaller model that only uses the predictors which is statistically significant. Compare it to the model in (a), which one is a better model?

**(f)** Using the model from (e), obtain 95% confidence intervals for the coefficient(s).

Perform the following Python code to generate simulated data, and answer the following questions:

\nAI悦创·编程一对一

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\n**Due dates:**

- \n
**Implementation: Thu, Sep. 26 at 8:59pm**\n**Self-evaluation: Fri, Sep. 27 at 11:59pm**\n

Starter files: code.zip

\n**Note:** The description may make assignments seem longer than they are. Distilling the description to make a list of all the tasks, classes, and methods you are actually supposed to implement will go a long way in having a good plan to tackle it. Read the description several times to confirm this list before acting on it!

The primary goal of this assignment is to practice implementing an interface based on given specifications, and by choosing an appropriate data representation that helps in providing the functionality promised by the interface.

\n**Please ensure all of your *source* code is in the cs3500.solored.model.hw02 and cs3500.solored.view.hw02 packages.** Note that the model package refers to

`hw02`

, and the view does not. (See Package Management below.)There will be two submissions for this assignment:

\n- \n
- Your
*actual implementation*and full test suite \n - A
*self-evaluation*, due*one day plus three hours later*than the actual implementation, where we will ask you to reflect on your implementation and testing choices. \n

**The Handins assignment will be delayed by for a little while** This is to encourage you to focus on design and planning testing of your code *before* implementing a representation. It is tempting to get lost in the Handins tests because that is a quantitative measure of progress, but a wrong-headed design can lead to many lost hours of frustration. Please plan ahead before writing your code.

**A reminder about late days:** Each submission is a *distinct* submission, and each one will independently use up your late days if you submit after the deadline. (Submitting late for the implementation does not mean you automatically get a “free” late day for the assignment as well – submitting both parts late will use *two* late days.) The 27-hour window for the self-evaluation is deliberate, so that even if you submit your assignment late, you can still submit your self-evaluation on time.

Read below for more details of each submission.

\nIn the next three assignments, you will implement a solitaire variation of the party card matching game called “Red7.” The goal of the game is to play cards in such a way that you are constantly winning according to an ever changing ruleset.

\n**A video showing the solitaire game of Solo Red is coming shortly!** In the meantime, here is an image of the game being played.

Note that our version will have some differences to the real version of the game. **Furthermore, a future assignment might involve adding more features to this game.** Keep the above in mind when thinking about your representation in this assignment. However, read this assignment *carefully and from beginning to the end* for what features you are required to implement in this assignment.

Our game uses a special set of playing cards for the game. Each card consists of a color and a number. The valid colors are as follows

\n- \n
- Red (R) \n
- Orange (O) \n
- Blue (B) \n
- Indigo (I) \n
- Violet (V) \n

The numbers are the integers 1 to 7, inclusive. For example, Red 3, Blue 1, and Violet 7, are all possible cards. We will present cards as two character strings, a letter for the color and the number itself. So Red 3 is printed as R3, Blue as B1 and so on.

\nThe game consists of 4 structures of cards:

\n- \n
- the
*deck*: the cards a player will draw from \n - the
`n`

*palettes*where`n`

is decided on game start: face-up cards that the player will interact with \n - the
*canvas*: the face-up card that details the current rule for a palette to win \n - a
*hand*: the cards a player can use to change the canvas or update a palette \n

Before we explain the rules of the game, we need to explain the concept of a palette \"winning\". Palettes can either be winning or losing, depending on the current rule for winning. The current rule is dictated by the canvas, specifically the *color* of the card on the top of the canvas. In other words, colors have rules.

Each color represents a different rule. For example, define the following palettes P and Q

\n- \n
- P has the cards Red 4, Blue 5, Red 6 \n
- Q has the cards Orange 6, Violet 7, Blue 3, Indigo 3 \n

The rules for the colors are defined below and we use the above palettes as a way to explain who is winning under that color’s rule.

\n- \n
- \n
Red: The palette with the highest card wins. Let C, D be cards. C is higher than D if the number of C is greater than the number of D OR if the numbers are equal, C’s color is closer to Red than D’s color. We use the rainbow ordering when it comes to declaring closeness. So this means R > O > B > I > V.

\nFor example, Blue 7 is higher than Red 1 because 7 is greater than 1. With our example palettes, that means Q is winning. Q has a Violet 7 as it’s highest card and P has a Red 6 as it’s highest card. Since 7 > 6, Violet 7 is higher.

\n \n - \n
Orange: The palette with the most of a single number wins. With our example palettes, Q is winning because it has two cards with the number 3. P has one card with the same number.

\n \n - \n
Blue: The palette with the most cards of different colors wins. With our example palettes, Q is winning because it has 4 cards with different colors. P only has 2 cards with different colors.

\n \n - \n
Indigo: The palette with the cards that form the longest run wins. A run is an unbroken increasing sequence of consecutive numbers. For instance 1-2-3 is a run as is the single number 4. However, 4-6 is not a run because it is missing the number 5 and 2-2 is not a run because it is not increasing. With our current example, P is winning because it has a run of length 3 made of Red 4, Green 5, and Red 6. Q only has a run of length 2 made of the colors Orange 6 and Violet 7.

\n \n - \n
Violet: The palette with the most cards below the value of 4 wins. With our example palettes, Q wins. Q has 2 cards with a number below 4: Blue 3 and Indigo 3. P has none.

\n \n

Note that it is possible that with the color rule alone, there can be a tie. For example, define the following pair of palettes

\n- \n
- P1 has the cards Red 1, Blue 5, Red 6 \n
- Q1 has the cards Violet 4, Blue 2, Orange 6 \n

Under Violet’s rule, P1 and Q1 are tied: both have a single card below 4. P1 has a Red 1 and Q1 has a Blue 2.

\nIf there is a tie under the current rule, focus on only the tied palettes. For each palette, look at the cards that help the palette win under the current rule. Choose the highest card amongst only those cards according to the Red rule. The palette with the highest card wins. Since all cards are unique, this ruling will always break a tie.

\nReturning to the example, P1 has a Red 1 and Q1 has a Blue 2. By the rules of breaking ties, Q1 wins because blue 2 is a higher card than Red 1.

\nTo play, we need a deck of unique cards, enough to fill the palettes with one card and give the player a full hand. The game can be played with two or more palettes and the maximum hand size must be positive (e.g. greater than 0)

\nFirst, the palettes are created, from first to last. Each palette is created by putting the current top card of the deck face-up on the palette.

\nNext, the canvas is created by assuming it is Red. Note it does *not* use a card from the deck but a special card separate from the deck that is Red. (In the packaging, it is an actual separate card but you will improvise in your implementation.)

Finally, draw cards from the deck to the hand until the hand is full.

\nWith the game setup, the game starts. The player must perform one of the following actions

\n- \n
- Play one card from their hand to a palette that is losing. \n
- Play one card from their hand to the canvas to change the rule and then play one card from their hand to a palette that is losing. \n

After the player plays a card from their hand to the current palette, the player checks if the current palette is winning under the current rule. If so,

\n- \n
- The player draws from the deck until either their hand is full or the deck is empty. \n

There used to be a statement about shifting palettes here. That is not possible in this version of the game.

\nConsider the game state displayed below for a 4-palette, 7-card hand game. At this point, P3 is winning under the current rule.

\n`Canvas: R\nP1: B2\nP2: R1 B5\n> P3: O1 V7\nP4: O4\nHand: R2 B4 V6 B7 I1 O3 R3`

\nThe player needs to make a palette win, so they play the B7 card to P1. Since B7 is now the highest card on P1 and it is higher than the B5. So now palette 1 is winning. The player then draws a card from the deck. The visualization below reflects the state of the game. Also notice that every card to the right of where B7 was has slid down!

\n`Canvas: R\n> P1: B2 B7\nP2: R1 B5\nP3: O1 V7\nP4: O4\nHand: R2 B4 V6 I1 O3 R3 R4`

\nNotice the player’s hand. There is nothing in their hand higher than a B7. In this scenario, the player decides to play the R2 to P3. Notice this means the palette P3, which is now losing, continues to lose. This ends the game, resulting in the final state below. Notice no card has been drawn because the player does not have the chance to do so.

\n`Canvas: R\n> P1: B2 B7\nP2: R1 B5\nP3: O1 V7 R2\nP4: O4\nHand: B4 V6 I1 O3 R3 R4`

\nAs seen in the above section, if the player could only play cards to a palette, then it is pretty simple to lose. However, that is where the second possible action occurs. Suppose the game is in the state below

\n`Canvas: R\nP1: B2 B7\n> P2: R1 R7\nP3: O1 V7\nP4: O4\nHand: R2 B4 V6 I1 O3 R3 R4`

\nThe player then decides to play I1 to the canvas, changing the rule from Red to Indigo and giving us the state below. Notice a couple of changes.

\n- \n
- The canvas has changed to I to reflect the indigo color \n
- The winning palette has not changed because P2 is still winning under this rule. It has the a run of 1 card and the B7, part of the best run, is the highest card amongst all palettes with a run of 1 card. \n
- The hand has NOT been replenished because the player does not draw yet. \n
- Also notice that every card to the right of where I1 was has slid down in the hand! \n

`Canvas: I\nP1: B2 B7\n> P2: R1 R7\nP3: O1 V7\nP4: O4\nHand: R2 B4 V6 O3 R3 R4`

\nNow the player can play a new card to a losing palette and thus make it win. They will play R3 to P4. This will give P4 a run of 2 cards, making it the palette with the longest run. The game continues and the player draws enough cards to fill their hand if possible.

\n`Canvas: I\nP1: B2 B7\nP2: R1 B5\nP3: O1 V7\n> P4: O4 R3\nHand: R2 B4 V6 O3 R4 B1 I5`

\nNow the player can choose from one of the two actions as the game has continued.

\nIf you look at the first state again, this is not the only way the player could have proceeded. They could have instead played V6 to the Canvas to change the rule first. That results in a new state depicted below. Notice that the winning palette *changed* under the current rule to P1.

`Canvas: V\n> P1: B2 R7\nP2: R1 B5\nP3: O1 V7\nP4: O4\nHand: R2 B4 I1 O3 R3 B3 R4`

\nNow the player must make another palette the winner by playing a card to a losing palette. Since P2 is not winning, P2 is a viable candidate. The player plays I1 to P2, which means P2 is winning because there are 2 cards below 4 there. Again, notice that every card to the right of where I1 was has slid down in the hand!

\n`Canvas: V\nP1: B2 R7\n> P2: R1 B5 I1\nP3: O1 V7\nP4: O4\nHand: R2 B4 O3 R3 B3 R4`

\nThe move changed the winning palette again and so the game continues. Note the player must now draw cards to fill their hand.

\nIf the deck runs out of cards during a draw, then the hand is as full as it is going to get. Consider the following state

\n`Canvas: R\n> P1: B2 B7\nP2: R1 B5\nP3: O1 V7\nP4: O4\nHand: R2 B4 V6 O3 R3 B3 R4 B1`

\nSay the player plays V6 to the canvas and then plays R2 to P3. We get the following state.

\n`Canvas: V\nP1: B2 B7\nP2: R1 B5\n> P3: O1 V7 R2\nP4: O4\nHand: B4 O3 R3 B3 R4 B1`

\nNow the player must draw cards to fill the hand. If the deck has no cards left, the state does not change but the game continues. If the deck has exactly one card left, then the player draws the last card, resulting in the following state. Note that the game continues because the hand is not yet empty and the player was able to successfully change the winning palette.

\n`Canvas: V\nP1: B2 B7\nP2: R1 B5\n> P3: O1 V7 R2\nP4: O4\nHand: B4 O3 R3 B3 R4 B1 O2`

\nThe game continues at this pace until the game ends. The game ends under one of two conditions

\n- \n
- The player played to a losing palette, but that palette did not win. In this case, the player has
*lost*. \n - The player has no cards in hand and the deck is empty. In this case, the player has
*won*. \n

Below are some examples of game states, annotated with whether the game is over in that state. These examples are not exhaustive, but provide some idea of when the game is over and when it is not. Assume for each of these, the deck is empty.

\n`State A: Game is over\n\nCanvas: R\nP1: R1\nP2: R2\n> P3: R3\nHand:`

\n`State B: Game is NOT over\n\nCanvas: V\nP1: R1 B7\n> P2: R2 B2\nP3: V1\nHand: B1`

\n`State C: Game is NOT over\n \nCanvas: R\nP1: R1\nP2: R2\n> P3: R3\nHand: B2`

\nNotice that it can be difficult to tell from looking at the state if the game has ended in a loss because that condition only occurs after the player plays a card to the palette.

\nBelow are the same three examples, but captioned as to whether the game has been won, can be won, or can only result in a loss.

\n`State A: Game is won\n\nCanvas: R\nP1: R1\nP2: R2\n> P3: R3\nHand:`

\n`State B: Game can be won\n\nCanvas: V\nP1: R1 B7\n> P2: R2 B2\nP3: V1\nHand: B3`

\n`State C: Game can only lose\n \nCanvas: R\nP1: R1\nP2: R2\n> P3: R3\nHand: B2`

\nIn this assignment you will design the model for this game. The model will maintain the state of the game and update itself when a client specifies moves. You are *not* required to make the game playable by a user at this point: only you-the-programmer can manipulate the model right now, and there is no mechanism yet for you-the-player to actually specify moves and play the game.

Finally, **no method in your implementation should exceed 50 lines. This hampers clarity of your code.**

Start by modeling a card in the game. You are free to name the class and its methods whatever you want, but it must implement the Card interface. You *cannot modify* the Card interface, but you may create a new interface that extends the Card interface to define new public methods your model can rely on. Then your Card implementation can implement the new interface. This is how we can extend the functionality of code without changing existing code!

Your card implementation should behave like a proper “good citizen of Java”, and implement its own `toString`

, `equals`

and `hashCode`

methods. (See below for some hints.) The `toString`

method should render the card as described above in the Cards section: e.g. Red 4 is printed as `\"R4\"`

, etc.

**Note:** If you realize you do not need to implement an `equals`

method, state in the Java documentation of your Card concrete class why it is not necessary. We want to know the reason for this design decision.

In order to play the game, the client would expect the following operations: start a new game, make a move, get the current state of the game, get the current score and know when the game has ended. These operations have been specified and documented in the provided `RedGameModel`

interface. You are **not** allowed to change the interface in any way!

A short explanation of most of the interface follows (the explanation here supplements the documentation provided in the interface):

\n- \n
- The
`RedGameModel`

interface itself takes in a type parameter representing the type of your piece. Your implementation will state what concrete type that is. \n `getAllCards()`

should return a`List`

containing all the concrete`Card`

s the game can be played with. This is exposed so others can create decks to play or test your game (see`startGame(List<C>, boolean, int, int)`

for more information). \n`startGame(List<C> deck, boolean shuffle, int numPalettes, int handSize)`

follows the description above, and lets the caller specify the cards to play with, the number of palettes, and the maximum hand size. It also specifies whether the model should shuffle the deck prior to setting up the game. \n`playToPalette`

and`playToCanvas`

implement the player moves described above. Whenever present, any indices in the parameters are assumed to be zero-based. \n`drawForHand`

is what fills the player’s hand and allows the player to play cards to the canvas again. \n`numPalettes()`

is useful for determining the setup of the game. \n`numOfCardsInDeck()`

,`getCanvas()`

,`getPalette(int)`

,`winningPaletteIndex()`

, and`getHand()`

allow the client to peek into the state of the game. \n`isGameOver()`

returns`true`

if the game is over, and`false`

otherwise. \n`isGameWon()`

returns`true`

if the player won the game and`false`

otherwise. \n

Implement the `RedGameModel`

interface in a class called `SoloRedGameModel`

:

- \n
- \n
When defining the class and implementing the interface, fill the type parameter in the

\n`extends`

clause with the name of your concrete card class. This will replace the type parameter in your class with your card type. Do*not*make your class fully generic. This will break the Handins tests. \n - \n
Design a suitable representation of this game. Think carefully about what fields and types you will need, and how possible values of the fields correspond to game states. Remember that a data representation makes it easy to implement behaviors.

\n \n - \n
Instantiating the game: Your class should define at least two constructors:

\n- \n
- One with zero arguments which initializes your game into a state that’s ready for someone to call one of the
`startGame`

methods and begin playing. Note the game has*not*started yet. \n - One with a single argument, a
`Random`

object that you use to perform the shuffling. If the object is null, this constructor must throw an`IllegalArgumentException`

. This will be useful for testing randomness related properties. \n

You may define whatever other constructors you wish on top of those; consider carefully all the methods you are expected to implement, and design your code to avoid as much duplication as possible. Keep in mind that a client should not be able to start a game without calling either

\n`startGame`

method! \n - One with zero arguments which initializes your game into a state that’s ready for someone to call one of the
- \n
Encapsulation: Your

\n`SoloRedGameModel`

class should not have*any*public fields, nor any public methods other than constructors and the public methods required by the`RedGameModel`

interface. \n

Be sure to properly document your code with Javadoc as appropriate. Method implementations that inherit Javadoc need not provide their own **unless** they implement something different or in addition to what is specified in the inherited documentation.

Our game should have some way of showing us the game board during game play. You have been provided with an empty `RedGameView`

interface that represents a view — we will add meaningful methods to this interface later. In this assignment, you should implement a class called `SoloRedGameTextView`

in the `cs3500.solored.view.hw02`

package.

`public interface RedGameView {\n\n}\n\npublic class SoloRedGameTextView implements RedGameView{\n private final RedGameModel<?> model;\n // ... any other fields you need\n\n public SoloRedGameTextView(RedGameModel<?> model) {\n this.model = model;\n }\n\n // your implementation goes here\n}`

\n- \n
- \n
Notice the wildcard generic. That is intentional since your view does not need to know the model is filled with Card objects.

\n \n - \n
Your class should at least have a constructor with exactly one argument of type

\n`RedGameModel`

— this model provides all the information the view needs in order to be rendered. \n - \n
The

\n`toString()`

method of this class returns a`String`

that may be used to display the state of the game. Here is an example rendering of a recently-started 4 palette, 6 hand size game; your`toString()`

method should reproduce this:

\n`Canvas: R\nP1: B2\nP2: R1 B5\n> P3: O1 V7\nP4: V4\nHand: R2 B4 V6 I1 O3 R3 V3`

Every line should end with a newline character,

\n**except**the final line — in this example, the first character of output is the`'C'`

in Canvas on the first line and the final character is the last`'3'`

on the last line.If the hand is ever empty, there will be no cards printed after the colon in \"Hand:\".

\n \n - \n
When writing toString, you may need to observe a card in the model. The wildcard type argument means you don’t know the dynamic type, but you do know it is still an

\n*Object*. Can you still call*toString*on such an object? If you are unsure, look up the Object java documentation. \n

You will need to add tests to assess whether your model implementation implements the behavior specified by both the interfaces, this assignment description, and possibly interesting interactions that are not entirely specified by the interface (e.g., that `createListOfPieces`

returns pieces in a particular order and no more than what is needed to make a board).

To do that, you should create *two test classes*. One of them should go in the `cs3500.solored`

package of the test directory, and it should test properties of the *public* model interface. To test implementation-specific details (i.e. protected or package-private details), you should create one last test class that you would place in the `cs3500.solored.model.hw02`

package itself, so that you can check protected and package-private implementation details if needed. Note if you do not have anything with protected or default access modifiers, then this test file is unnecessary.

**Note:** When writing tests with the public interface, you *cannot* assume you have access to your Card class. After all, those are implementation-specific. Instead, use the methods on the model’s interface. Is there a method you can use to get a set of cards that the game allows you to use? Can you then rearrange those cards and pass them into the `startGame`

method to start the game?

Be mindful of which test cases you place in which test class! *Technically,* you could run all the tests from a single class. But using multiple classes like this helps convey to the reader of your code some of your thought processes behind each test: the reader should understand the examples first, then look at the tests of public behavior, and finally look at implementation-specific fiddly details.

**Note:** When you submit your full implementation, you will see automated tests that *I* wrote and run against *your* code. I gave some of my test methods long but specific names, so that you can try to deduce what my tests are checking for. Just because *I* have a test for a given scenario, though, does not mean that you shouldn’t write your own test case to confirm your understanding!

To make sure that your packages are in the correct layout, you should tell IntelliJ to do the following. Do this early, before you’ve written much code, to ensure that your files wind up in the right locations automatically, instead of having to fix it afterward:

\nWhen you create a new project, you should see something like this:

\n\nNotice that the `src`

directory is marked blue, which means IntelliJ believes that this directory contains the source files of your project. *If it isn’t marked blue,* you need to tell IntelliJ that it should be: right-click on the `src`

folder and select *Mark Directory As -> Sources root*. To create a new package, right-click on the `src`

directory, select *New -> Package*. In the dialog box that pops up, enter the new package name

- \n
- To create new files within a particular package, right-click on the package folder and select
*New -> Java Class*. If you want to create a new file in the*default*package, then select the`src`

directory itself. \n - To create a test directory, right-click on the
*project*itself, and select*New -> Directory*. In the dialog box that pops up, enter “test” as the name. Right-click on the directory, select*Mark Directory As -> Test Sources root*. Henceforth, you should add any test classes in this folder. See the tutorial video for a demo of this. \n - The
`src/`

and`test/`

directories can parallel each other in structure. However, keeping your sources and tests separated is always a good idea, so you don’t inadvertently release your tests as part of your source! \n

- \n
- Read through the assignment and consider what objects exist. Can you use separate classes to keep your design cohesive while giving each object a single purpose? This will help decrease the complexity of the implementation as there are a lot of parts working together. \n
- I suggest getting key observations working and then finishing the view. Being able to see the model makes debugging the operations much easier. \n
- Before implementing the rules for colors, make an example and break down how you would do this by hand. Create a list of steps and use that to guide your implementation. \n
- Do the same for finding a winning palette, but also start with the
*simplest*version of the problem. Break down the steps you would go through to find the winning palette under to the canvas alone. Then consider what breaking ties looks like. A plan goes a long way when designing large code. \n

- \n
- \n
For your implementation: submit a properly-structured zip containing

\n- \n
- The model interface (
`RedGameModel.java`

) \n - Implementation of the model interface (
`SoloRedGameModel.java`

) \n - The view interface (
`RedGameView.java`

) \n - Implementation of the view (
`SoloRedGameTextView.java`

) \n - Any additional classes you saw fit to write \n
- All your tests in one or more JUnit test classes \n

Note the src folder cannot be empty this time as our tests need your source code.

\n \n - The model interface (

**Again, please ensure all of your project’s** **sources** **are in the** `cs3500.solored.model.hw02`

**and** `cs3500.solored.view.hw02`

**packages, accordingly. Please ensure that your project’s** **test cases** **are in the packages explained above. The autograder will give you an automatic 0 if it cannot compile your code!**

**下面代码 25.5分**

AI悦创·编程一对一

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\n你好，我是悦创。

\nJava 的 Builder 模式（建造者模式）是一种设计模式，通常用于构建复杂对象。它通过一步步地构建一个对象来避免构造函数参数过多的问题，同时提高代码的可读性和灵活性。我们可以通过 Builder 模式在构建对象时指定我们需要的字段，而不必每次都传递所有的参数。

\n- \n
**构造函数参数过多**：如果类有多个可选参数，直接使用构造函数可能会显得混乱。 \n**对象不可变**：通过 Builder 模式，创建对象的过程在构造完成之后就不会再被修改。 \n**提高可读性**：可以按需设置不同的参数，而不用处理多个构造函数的重载。 \n

- \n
**定义类**：包含我们要创建的对象的属性。 \n**创建 Builder 类**：Builder 类和主类（我们要创建的对象的类）会有相同的属性，但 Builder 类将逐步构建对象。 \n**设置方法**：Builder 类包含用于设置属性的方法，每个方法都会返回 Builder 本身以实现链式调用。 \n：最后提供一个`build`

方法`build`

方法来创建对象。 \n

假设我们有一个 `Person`

类，它有多个属性：`name`

、`age`

、`address`

、`phoneNumber`

。有些属性是可选的，我们可以使用 Builder 模式来创建对象。

`// 1. 定义 Person 类\npublic class Person {\n private String name;\n private int age;\n private String address;\n private String phoneNumber;\n\n // 私有构造函数，只能通过 Builder 创建\n private Person(PersonBuilder builder) {\n this.name = builder.name;\n this.age = builder.age;\n this.address = builder.address;\n this.phoneNumber = builder.phoneNumber;\n }\n\n // 2. 创建 Builder 类\n public static class PersonBuilder {\n // 与 Person 类中的属性相同\n private String name;\n private int age;\n private String address;\n private String phoneNumber;\n\n // 3. 为每个属性创建 setter 方法，返回 Builder 对象\n public PersonBuilder setName(String name) {\n this.name = name;\n return this;\n }\n\n public PersonBuilder setAge(int age) {\n this.age = age;\n return this;\n }\n\n public PersonBuilder setAddress(String address) {\n this.address = address;\n return this;\n }\n\n public PersonBuilder setPhoneNumber(String phoneNumber) {\n this.phoneNumber = phoneNumber;\n return this;\n }\n\n // 4. build 方法返回最终的 Person 对象\n public Person build() {\n return new Person(this);\n }\n }\n\n @Override\n public String toString() {\n return \"Person{name='\" + name + \"', age=\" + age +\n \", address='\" + address + \"', phoneNumber='\" + phoneNumber + \"'}\";\n }\n\n public static void main(String[] args) {\n // 5. 使用 Builder 创建对象\n Person person = new Person.PersonBuilder()\n .setName(\"Alice\")\n .setAge(30)\n .setAddress(\"123 Main St\")\n .setPhoneNumber(\"555-1234\")\n .build();\n\n System.out.println(person); // 输出：Person{name='Alice', age=30, address='123 Main St', phoneNumber='555-1234'}\n }\n}`

\n- \n
**Person 类**：包含四个属性：`name`

、`age`

、`address`

、`phoneNumber`

。构造函数是私有的，只有通过`PersonBuilder`

才能创建`Person`

对象。 \n**PersonBuilder 类**：内部静态类，包含与`Person`

类相同的属性，并提供链式调用的 setter 方法。 \n**链式调用**：每个`set`

方法都返回`this`

（即`PersonBuilder`

实例本身），以便可以连续调用多个`set`

方法。 \n：一旦所有所需属性都已设置，`build`

方法`build`

方法将返回一个新的`Person`

对象。 \n**使用 Builder 创建对象**：在`main`

方法中，通过链式调用设置所需的属性，最后通过`build`

方法生成对象。 \n

- \n
**简化代码**：避免了多个构造函数的重载，让代码更清晰。 \n**灵活性**：可以按需设置不同的属性，避免在创建对象时传入不必要的参数。 \n**不可变性**：对象一旦构建，就不会再被修改，增加了安全性。 \n

这样，你就可以很容易地通过 Builder 模式创建对象，即使这个对象有很多可选的参数。

\n- \n
- 传统情况下，我们通过构造函数来创建对象，可能会有多个构造函数来处理不同的情况。如果
`Person`

类的属性较多，构造函数的参数会变得非常复杂。 \n - 使用 Builder 模式，我们可以更加灵活地构建对象，只设置需要的属性，而不必为各种组合创建不同的构造函数。 \n

| **特性** | **没有 Builder 模式** | **使用 Builder 模式** |

\n|

This assignment is a set of practice problems to help you get familiar with Python and its environment.

\nPlease copy the following files to your computer, and for each question write your answers in the corresponding file: q1.py, q2.py,

\nq3.py, and q4.py. To get all the files at once, download a1_starting_files.zip and extract them to your computer.

When you are ready to submit your answers, compress q1.py, q2.py, q3.py, and q4.py into a.zip file named `a1.zip`

, and submit just `a1.zip`

on Canvas.

\n\nIf you are unsure how to zip/unzip files, check here:Windows, Mac

\nIf you run into problems, please talk to a TA, or post a question to the Tech Support discussion in the 120 Canvas course.

\n

**Hint** f-strings and string arithmetic are need in some questions, and are explained at the end of this assignment.

Put your answer to this question in q1.py.

\nWrite a program that asks the user for their name and age, and then prints a message saying how old they'll be in 25 years. Use **f-strings** to print the output.

For example:

\n`What's your name? Elon\n\nHow old are you? 51\n\nHi Elon! In 25 years it will be 2049 and you'll be 76 years old.`

\nYou don't need to do any error checking: assume a sensible name and integer age are entered.

\nPut your answer to this question in q2.py.

\nWrite a program that converts feet $f$f to meters $m$m using the formula $m=0.3048\cdot f$m=0.3048⋅f. Ask the user to enter a measurement in feet, and then print it in meters:

\n`How many feet? 21\n\n21.0 is about 6.4 meters`

\nUse f-strings to format your output. It should work for any number the user enters. Make your output the same as shown. The value for both the feet and meters should be printed to **exactly** 1 decimal place.

You can assume the user enters a valid number.

\n**答案：**

`# q2.py\n\n# Ask the user to input the number of feet\nfeet = float(input(\"How many feet? \"))\n\n# Convert feet to meters using the formula\nmeters = 0.3048 * feet\n\n# Print the result formatted to 1 decimal place using f-strings\nprint(f\"{feet:.1f} is about {meters:.1f} meters\")`

\nPut your answer to this question in q3.py.

\nWrite a program that asks the user to enter a message and a box character, and then prints the message in a box made from that character. Print it in exactly the style shown in the examples.

\nFor example:

\n`What do you want your sign to say? Code Carefully\nWhat character do you want for the box? !\n\n!!!!!!!!!!!!!!!!!!\n! Code Carefully !\n!!!!!!!!!!!!!!!!!!`

\nOr:

\n`What do you want your sign to say? SFU\nWhat character do you want for the box? %\n\n%%%%%%%\n% SFU %\n%%%%%%%`

\nMake sure your box has *exactly* the same format as shown. Note that there is one space before the message, and one space after the message.

Assume the user enters a single character for the box character.

\n**答案：**

Put your answer to this question in q4.py.

\nYou are the programmer for the *CyberCone*, a company started by unemployed CyberTruck engineers. *CyberCone* sells *cybercones*, which are robot-assembled ice cream cones with any cone height and any number of scoops of ice cream. Part of the appeal is that the scoops are perfect spheres, and the cone is a perfect cone.

For example, a 3-scoop cone with would look like this:

\n\nThe total height is the height of the cone plus the height of the scoops and is given by this formula:

\n\begin{array}{}\end{array}\backslash n\backslash \backslash begin\{align*\}\backslash n\backslash \backslash text\{total-height\}\; =\; \backslash \backslash text\{cone-height\}\; +\; (\backslash \backslash text\{num-scoops\}\; \backslash \backslash times\; 2\; \backslash \backslash times\; \backslash \backslash text\{scoop-radius\})\backslash n\backslash \backslash end\{align*\}\backslash ntotal-height=cone-height+(num-scoops×2×scoop-radius)

\nIt's assumed that each scoop has the same radius.

\nThe purchase cost, in dollars, of the cone is given by this formula:

\n\begin{array}{}\end{array}\backslash n\backslash \backslash begin\{align*\}\backslash n\backslash \backslash text\{cost\}\; =\; (\backslash \backslash text\{num-scoops\}\; \backslash \backslash times\; \backslash \backslash text\{scoop-volume\}\; \backslash \backslash times\; 0.75)\; +\; (\backslash \backslash text\{cone-volume\}\; \backslash \backslash times\; 0.25)\backslash n\backslash \backslash end\{align*\}\backslash ncost=(num-scoops×scoop-volume×0.75)+(cone-volume×0.25)

\n$\text{scoop-volume}$scoop-volume is the volume of one scoop (all scoops have the same volume), and $\text{cone-volume}$cone-volume is the volume of the cone.

\nYour task is to write a program that asks the customer the following questions, and then prints the total height of the cone and the purchase cost:

\n- \n
**How many scoops do they want?**The choices are 1 or more. \n**What is the radius of a scoop?**The scoops are perfect spheres, and the radius is in centimeters. All scoops for this cone have the same radius. \n**What is the height of the cone?**The height is in centimeters and greater than 0. \n

After getting this information the program then prints the total height and the purchase cost of the cone.

\nHere is a sample run:

\n`+`

\n正六边形的内角和可以通过计算其内角数来得到。

\n对于任何多边形，内角和的公式是：

\n\text{\u5185\u89d2\u548c}=(n-2)\times 18{0}^{\circ}内角和=(n−2)×180∘

\n其中，$n$n 是多边形的边数。正六边形有 6 条边，所以代入公式：

\n\text{\u5185\u89d2\u548c}=(6-2)\times 18{0}^{\circ}=4\times 18{0}^{\circ}=72{0}^{\circ}内角和=(6−2)×180∘=4×180∘=720∘

\n因此，正六边形的内角和是 **720 度**。

如果要计算每个内角，由于正六边形是正多边形，每个内角相等，所以每个内角为：

\n\text{\u6bcf\u4e2a\u5185\u89d2}=\frac{72{0}^{\circ}}{6}=12{0}^{\circ}每个内角=6720∘=120∘

\n所以正六边形的每个内角都是 **120 度**。

要求等腰三角形的底边长度，我们还需要知道一些额外的信息，例如三角形的顶角（夹角）或底边上的高。如果你能提供这些信息，我们就可以使用三角函数或几何方法来求解底边长度。

\n例如：

\n- \n
- \n

\n**如果已知顶角：**- \n
- 假设顶角的大小为 $\theta $θ，则可以使用三角函数来求解底边长度。通过将三角形分成两个直角三角形，底边的一半可以通过以下公式计算：$\frac{\text{\u5e95\u8fb9}}{2}=50\times \mathrm{sin}\left(\frac{\theta}{2}\right)$2底边=50×sin(2θ) \n

底边长度为：$\text{\u5e95\u8fb9}=2\times 50\times \mathrm{sin}\left(\frac{\theta}{2}\right)$底边=2×50×sin(2θ)

\n \n - \n

\n**如果已知高：**- \n
- 如果已知底边上的高为 ( h )，则可以使用勾股定理计算底边长度：$\frac{\text{\u5e95\u8fb9}}{2}=\sqrt{5{0}^{2}-{h}^{2}}$2底边=502−h2

\n底边长度为：$\text{\u5e95\u8fb9}=2\times \sqrt{5{0}^{2}-{h}^{2}}$底边=2×502−h2 \n

\n - 如果已知底边上的高为 ( h )，则可以使用勾股定理计算底边长度：$\frac{\text{\u5e95\u8fb9}}{2}=\sqrt{5{0}^{2}-{h}^{2}}$2底边=502−h2
- \n
已知等腰三角形的等腰长度为 50，且顶角为 120 度，我们可以使用三角函数来求底边的长度。

\n可以将等腰三角形分为两个直角三角形，每个直角三角形的角为 $\frac{12{0}^{\circ}}{2}=6{0}^{\circ}$2120∘=60∘，并且腰的一半构成这个直角三角形的对边，而底边的一半构成邻边。

\n通过正弦公式可以求得底边的一半：$\frac{\text{\u5e95\u8fb9}}{2}=50\times \mathrm{sin}\left(6{0}^{\circ}\right)$2底边=50×sin(60∘)

\n接下来，计算底边长度：$\text{\u5e95\u8fb9}=2\times 50\times \mathrm{sin}\left(6{0}^{\circ}\right)$底边=2×50×sin(60∘)

\n我们知道 $\mathrm{sin}\left(6{0}^{\circ}\right)=\frac{\sqrt{3}}{2}$sin(60∘)=23，所以：$\text{\u5e95\u8fb9}=2\times 50\times \frac{\sqrt{3}}{2}=50\sqrt{3}$底边=2×50×23=503

\n计算得出底边长度：$\text{\u5e95\u8fb9}\approx 50\times 1.732=86.6$底边≈50×1.732=86.6

\n因此，底边的长度约为

\n**86.6**。 \n

要计算等腰梯形的底边长度，必须提供一些额外的信息，例如上底、腰长、高或顶角等。不同条件下有不同的计算方法。下面列举几种常见的情况：

\n假设等腰梯形的上底长度为 $a$a、腰长为 $l$l、高为 $h$h，我们可以使用勾股定理来计算底边长度。

\n**计算步骤：**

- \n
- 由于等腰梯形是对称的，可以将梯形分为一个上底为 $a$a、下底为 $b$b 的矩形和两个直角三角形。 \n
- 每个直角三角形的底边的一半（对应于梯形的底边和上底之间的水平距离）为：$\frac{b-a}{2}$2b−a \n
- 使用勾股定理可以得出：${l}^{2}={h}^{2}+{\left(\frac{b-a}{2}\right)}^{2}$l2=h2+(2b−a)2 \n
- 解这个方程求底边 ( b )：$b=a+2\times \sqrt{{l}^{2}-{h}^{2}}$b=a+2×l2−h2 \n

假设已知等腰梯形的上底长度为 $a$a、腰长为 $l$l、顶角为 $\theta $θ，可以使用三角函数来计算底边长度。

\n**计算步骤：**

- \n
- 顶角 $\theta $θ 是梯形上底与腰之间的夹角。我们可以通过三角函数计算底边与上底之间的水平距离：$\frac{b-a}{2}=l\times \mathrm{cos}\left(\theta \right)$2b−a=l×cos(θ) \n
- 底边长度为：$b=a+2\times (l\times \mathrm{cos}(\theta \left)\right)$b=a+2×(l×cos(θ)) \n

根据已知条件：

\n- \n
- 上底 $a=50$a=50 \n
- 两个腰长 $l=50$l=50 \n
- 顶角为 $12{0}^{\circ}$120∘ \n

我们可以使用三角函数来求解底边长度。

\n**解题思路：**

由于是等腰梯形，可以将梯形分为一个上底为 $50$50、下底为 $b$b 的矩形和两个直角三角形。每个直角三角形的顶角为 $12{0}^{\circ}$120∘，剩余的角为 $6{0}^{\circ}$60∘（因为等腰梯形对称，顶角一分为二，每个角 $6{0}^{\circ}$60∘）。可以使用余弦定理来求解底边长度。

\n首先我们知道梯形中，两腰垂直投影到下底上的长度为：$\frac{b-a}{2}=l\times \mathrm{cos}\left(6{0}^{\circ}\right)$2b−a=l×cos(60∘)

\n因为 $\mathrm{cos}\left(6{0}^{\circ}\right)=0.5$cos(60∘)=0.5，所以：$\frac{b-50}{2}=50\times 0.5=25$2b−50=50×0.5=25

\n解这个方程：$b-50=50$b−50=50

\n因此：$b=100$b=100

\n**结果：**

等腰梯形的下底边长为 **100**。

欢迎关注我公众号：AI悦创，有更多更好玩的等你发现！

\n\n

\n\n",
"image": "https://bornforthis.cn/gzh.jpg",
"date_published": "2024-09-23T18:43:35.000Z",
"authors": [
{
"name": "AI悦创"
}
],
"tags": []
},
{
"title": "Simon Fraser University（Python）",
"url": "https://bornforthis.cn/1v1/92-lztc1751/HW1.html",
"id": "https://bornforthis.cn/1v1/92-lztc1751/HW1.html",
"content_html": "\n",
"date_published": "2024-09-23T12:29:29.000Z",
"authors": [
{
"name": "AI悦创"
}
],
"tags": []
},
{
"title": "Linear Search",
"url": "https://bornforthis.cn/column/data-structure/Python-DSA/03-linear-search.html",
"id": "https://bornforthis.cn/column/data-structure/Python-DSA/03-linear-search.html",
"summary": "1. 介绍 你好，我是悦创。 Python 中的线性查找（Linear Search）是一种简单的查找算法，用于在列表或数组中查找特定元素。 线性查找的基本思想是从列表的第一个元素开始，逐一比较每个元素，直到找到目标元素或遍历完整个列表为止。 2. 线性查找的基本步骤 从第一个元素开始，将其与目标值进行比较。 如果找到目标值，则返回该元素所在的索引。 ...",
"content_html": "AI悦创·编程一对一

\nAI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Linux、Web 全栈」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\n你好，我是悦创。

\nPython 中的线性查找（Linear Search）是一种简单的查找算法，用于在列表或数组中查找特定元素。

\n线性查找的基本思想是从列表的第一个元素开始，逐一比较每个元素，直到找到目标元素或遍历完整个列表为止。

\n- \n
**从第一个元素开始**，将其与目标值进行比较。 \n**如果找到目标值**，则返回该元素所在的索引。 \n**如果没有找到目标值**，继续比较列表中的下一个元素。 \n- 如果遍历完整个列表仍然没有找到目标值，表示目标值不存在于列表中。（一般返回
`-1`

）。 \n

`def linear_search(arr, target):\n \"\"\"\n 在线性查找中，逐个元素与目标值比较，直到找到目标或遍历结束。\n\n 参数:\n arr -- 需要查找的列表\n target -- 需要查找的目标值\n\n 返回:\n 如果找到目标值，返回其索引；如果未找到，返回-1。\n \"\"\"\n # 遍历列表中的每个元素\n for index in range(len(arr)):\n # 比较当前元素是否与目标值相等\n if arr[index] == target:\n return index # 找到目标值，返回其索引\n return -1 # 如果未找到，返回-1表示目标值不存在\n\n# 示例使用\nnumbers = [10, 23, 45, 70, 11, 15]\ntarget = 70\n\n# 调用线性查找函数\nresult = linear_search(numbers, target)\n\n# 打印结果\nif result != -1:\n print(f\"找到目标值 {target}，它的索引是 {result}\")\nelse:\n print(f\"未找到目标值 {target}\")`

\n`def linear_search(arr, target):\n positions = []\n # 遍历列表中的每个元素\n for index in range(len(arr)):\n # 比较当前元素是否与目标值相等\n if arr[index] == target:\n positions.append(index)\n if positions:\n return -1\n return positions\n\n\n# 示例使用\nnumbers = [10, 23, 45, 70, 11, 15]\ntarget = 70\n\n# 调用线性查找函数\nresult = linear_search(numbers, target)\n\n# 打印结果\nif result != -1:\n print(f\"找到目标值 {target}，它的索引是 {result}\")\nelse:\n print(f\"未找到目标值 {target}\")`

\n**这三道题的创新点：**

- \n
**题目1**：模拟了寻找宝藏的过程，结合冒险和线性查找算法。 \n**题目2**：将迷宫的路径搜索与线性查找结合，强调算法在实际问题中的应用。 \n**题目3**：引入了足迹追踪的情景，探索首次和最后一次出现的情况，通过线性查找解决时间上的定位问题。 \n

AI悦创·编程一对一

\nC++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

\n方法一：QQ

\n方法二：微信：Jiabcdefh

\nLet ${y}_{i}\mid \theta {\sim}^{iid}\text{Uniform}(-\theta ,\theta )$yi∣θ∼iidUniform(−θ,θ), for $i=1,\dots ,n$i=1,…,n. Assume the prior distribution for $\theta $θ to be $\text{Pareto}(a,b)$Pareto(a,b), where

\np\left(\theta \right)=\frac{b{a}^{b}}{{\theta}^{b+1}}\phantom{\rule{\"1em\"}{0ex}}\text{for}\theta \ge a\text{and}0\text{otherwise}{.}p(θ)=θb+1babfor θ≥a and 0 otherwise.

\nFind the posterior distribution of $\theta $θ