csci-1100-hw-2
This commit is contained in:
BIN
content/en/posts/csci-1100/hw-2/HW2.zip
Normal file
BIN
content/en/posts/csci-1100/hw-2/HW2.zip
Normal file
Binary file not shown.
326
content/en/posts/csci-1100/hw-2/index.md
Normal file
326
content/en/posts/csci-1100/hw-2/index.md
Normal file
@@ -0,0 +1,326 @@
|
||||
---
|
||||
title: CSCI 1100 - Homework 2 - Strings and Functions
|
||||
subtitle:
|
||||
date: 2024-03-12T02:41:25-04:00
|
||||
slug: csci-1100-hw-2
|
||||
draft: false
|
||||
author:
|
||||
name: James
|
||||
link: https://www.jamesflare.com
|
||||
email:
|
||||
avatar: /site-logo.avif
|
||||
description: This homework assignment consists of three parts, focusing on sizing a gum ball machine, implementing a simple substitution cipher, and performing basic sentiment analysis on sentences using Python functions and string manipulation.
|
||||
keywords: ["Python","functions","string manipulation","sentiment analysis"]
|
||||
license:
|
||||
comment: true
|
||||
weight: 0
|
||||
tags:
|
||||
- CSCI 1100
|
||||
- Homework
|
||||
- RPI
|
||||
- Python
|
||||
- Programming
|
||||
categories:
|
||||
- CSCI
|
||||
- Programming
|
||||
hiddenFromHomePage: false
|
||||
hiddenFromSearch: false
|
||||
hiddenFromRss: false
|
||||
hiddenFromRelated: false
|
||||
summary: This homework assignment consists of three parts, focusing on sizing a gum ball machine, implementing a simple substitution cipher, and performing basic sentiment analysis on sentences using Python functions and string manipulation.
|
||||
resources:
|
||||
- name: featured-image
|
||||
src: featured-image.jpg
|
||||
- name: featured-image-preview
|
||||
src: featured-image-preview.jpg
|
||||
toc: true
|
||||
math: false
|
||||
lightgallery: false
|
||||
password:
|
||||
message:
|
||||
repost:
|
||||
enable: true
|
||||
url:
|
||||
|
||||
# See details front matter: https://fixit.lruihao.cn/documentation/content-management/introduction/#front-matter
|
||||
---
|
||||
|
||||
<!--more-->
|
||||
|
||||
## Overview
|
||||
|
||||
This homework, worth 100 points total toward your overall homework grade, is due Thursday, February 1, 2024 at 11:59:59 pm. Three parts should each be submitted separately. All parts should be submitted by the deadline or your program will be considered late.
|
||||
|
||||
**Note on grading**: Make sure you read the Submission Guidelines document. It applies to this and all future homework assignments and will be of increasing importance. In all parts of the homework, we will specify which functions you must provide. Make sure you write these functions, even if they are very simple. Otherwise, you will lose points. We will write more complex functions as the semester goes on. In addition, we will also look at program structure (see Lecture 4), and at the names of variables and functions in grading this homework.
|
||||
|
||||
You are not allowed to use any loops anywhere in this assignment.
|
||||
|
||||
## Fair Warning About Excess Collaboration
|
||||
|
||||
For all homework assignments this semester we will be using software that compares all submitted programs, looking for inappropriate similarities. This handles a wide variety of differences between programs, so that if you either (a) took someone else’s program, modified it (or not), and submitted it as your own, (b) wrote a single program with one or more colleagues and submitted modified versions separately as your own work, or (c) submitted (perhaps slightly modified) software submitted in a previous year as your software, this software will mark these submissions as very similar.
|
||||
|
||||
All of (a), (b), and (c) are beyond what is acceptable in this course — they are violations of the academic integrity policy. Furthermore, this type of copying will prevent you from learning how to solve problems and will hurt you in the long run. The more you write your own code, the more you learn.
|
||||
|
||||
Please read the Collaboration Policy document for acceptable levels of collaboration and how you can protect yourself. The document can be found on the Course Materials page on Submitty. Penalties for excess collaboration can be as high as:
|
||||
|
||||
- 0 on the homework, and
|
||||
- an additional overall 5% reduction on the semester grade.
|
||||
|
||||
Penalized students will also be prevented from dropping the course. More severe violations, such as stealing someone else’s code, will lead to an automatic F in the course. A student caught in a second academic integrity violation will receive an automatic F.
|
||||
|
||||
By submitting your homework you are asserting that you both (a) understand the academic integrity policy and (b) have not violated it.
|
||||
|
||||
Finally, please note that this policy is in place for the small percentage of problems that will arise in this course. Students who follow the strategies outlined above and use common sense in doing so will not have any trouble with academic integrity.
|
||||
|
||||
## Part 1: A Penny for a Gum Ball Mickey (40 pts)
|
||||
|
||||
We are going to conduct an experiment in selling gum balls, but we are going to make a few assumptions. Assume that you are selling gum balls from a vending machine. The machine is a cube, and the gum balls are spheres. You check the machine once a week. The goal is to size the machine so that it is completely full at the start of the week, you never run out of gum balls before you get back to check the machine, and you do not have too many gum balls left at the end of the week to go stale. We will make the assumption that all the gum balls line up so that the number of gum balls along any dimension of the cube is simply the side length divided by the diameter of the spheres. So if the side length is 9.0 and the gum balls have a radius of 0.5 exactly 9 gum balls would fit along each dimension, or 729 gum balls in total. This is known as a cubic lattice.
|
||||
|
||||
Do the following:
|
||||
|
||||
1. First write two functions, `find_volume_sphere(radius)` and `find_volume_cube(side)` that calculate the volume of a sphere given a radius, and that calculate the volume of a cube given a side, respectively.
|
||||
|
||||
2. Now ask the user for the radius of the gum balls and the weekly sales.
|
||||
|
||||
3. Calculate the total number of gum balls that need to fit in the machine as 1.25 times the weekly sales and use this to calculate the side length of the machine in terms of an integer number of gum balls. Hint: You know the total number of gum balls and in a cubic lattice, you can fit the same number along each dimension, so if you can fit N gum balls along each dimension, then the machine holds N³ gum balls. The math module function `ceil` will always round up and may be of use here. (We won’t be cutting gum balls to fit them in the machine.)
|
||||
|
||||
4. Calculate a few more values: how many gum balls will actually fit given the dimension you chose (remember that there must be an integer number of gum balls along each dimension of the cube); the volume of the cube; the volume of each gum ball, and the wasted space if we put in both the number of gum balls we need to hold and how many we can hold.
|
||||
|
||||
5. Print these values out using the `.2f` format for all floating point values.
|
||||
|
||||
Two examples of the program run (how it will look when you run it using Spyder IDE) are provided in files `hw2 part1 output 01.txt` and `hw2 part1 output 02.txt` (You will need to download file `hw02_files.zip` from the Course Materials section of Submitty and unzip it into your directory for HW 2).
|
||||
|
||||
We will test your code for the above values as well as a range of different values. Test your code well and when you are sure that it works, please submit it as a file named `hw2 part1.py` to Submitty for Part 1 of the homework.
|
||||
|
||||
## Part 2: Find the Hidden Message (40 pts)
|
||||
|
||||
Write a program to determine if a simple substitution code is reversible for a given string. The program should ask the user for a sentence using `input`. The program should then encrypt the string into a cipher, decode the cipher, and compare the result of the decode operation to the original sentence. If the decoded cipher matches the original, then the operation is reversible on the string. Otherwise, it is not reversible.
|
||||
|
||||
Along the way, the program should print out the cipher, the difference in length between the cipher and the original sentence, the decoded cipher, and a brief message saying whether the operation was reversible. Note that the difference in length should always be printed as a positive number.
|
||||
|
||||
Two examples of the program run (how it will look when you run it using Spyder IDE) are provided in files `hw2 part2 output 01.txt` and `hw2 part2 output 02.txt` (can be found inside the `hw02_files.zip` file).
|
||||
|
||||
The encryption rules are based on a set of string replacements, they should be applied in this order exactly:
|
||||
|
||||
| Original | Replacement | Note |
|
||||
|:--------:|:-----------:|:-----|
|
||||
| ' a' | '%4%' | Replace any 'a' after a space with '%4%' |
|
||||
| 'he' | '7!' | Replace all occurrences of string 'he' with '7!' |
|
||||
| 'e' | '9(*9(' | Replace any remaining 'e' with '9(*9(' |
|
||||
| 'y' | '*%' | Replace all occurrences of string 'y' with '*%' |
|
||||
| 'u' | '@@@' | Replace all occurrences of string 'u' with '@@@' |
|
||||
| 'an' | '-?' | Replace all occurrences of string 'an' with '-?' |
|
||||
| 'th' | '!@+3' | Replace all occurrences of string 'th' with '!@+3' |
|
||||
| 'o' | '7654' | Replace all occurrences of string 'o' with '7654' |
|
||||
| '9' | '2' | Replace all occurrences of string '9' with '2' |
|
||||
| 'ck' | '%4' | Replace all occurrences of string 'ck' with '%4' |
|
||||
|
||||
For example the cipher for methane is `m2(*2(!@+3-?2(*2(`. Here is how we get this:
|
||||
|
||||
```python
|
||||
>>> 'methane'.replace('e','9(*9(')
|
||||
'm9(*9(than9(*9('
|
||||
>>> 'm9(*9(than9(*9('.replace('an','-?')
|
||||
'm9(*9(th-?9(*9('
|
||||
>>> 'm9(*9(th-?9(*9('.replace('th','!@+3')
|
||||
'm9(*9(!@+3-?9(*9('
|
||||
>>> 'm9(*9(!@+3-?9(*9('.replace('9', '2')
|
||||
'm2(*2(!@+3-?2(*2('
|
||||
```
|
||||
|
||||
Decrypting will involve using the rules in reverse order.
|
||||
|
||||
Your program must use two functions:
|
||||
|
||||
- Write one function `encrypt(word)` that takes as an argument a string in plain English, and returns a ciphered version of it as a string.
|
||||
|
||||
- Write a second function `decrypt(word)` that does the reverse: takes a string in cipher and returns the plain English version of it.
|
||||
|
||||
Both functions will be very similar in structure, but they will use the string replacement rules in different order. You can now test whether your functions are correct by first encrypting a string, and then decrypting. The result should be identical to the original string (assuming the replacement rules are not ambiguous).
|
||||
|
||||
Use these functions to implement the above program. We will test your code for the above values as well as a range of different values.
|
||||
|
||||
Test your code well and when you are sure that it works, please submit it as a file named `hw2 part2.py` to Submitty for Part 2 of the homework.
|
||||
|
||||
## Part 3: How Do You Feel about Homework? (20 pts)
|
||||
|
||||
In this part of the homework, you will implement a very rough sentiment analysis tool. While the real tools use natural language processing, they all use word counts similar to the one we use here. Understanding the sentiment in messages is a crucial part of a lot of artificial intelligence tools.
|
||||
|
||||
Write a program that will ask the user for a string containing a sentence. The program will then compute the happiness and sadness level of the sentence using the two functions described below. If the happiness level is higher than sadness level, then the tone of the sentence is happy. If the sadness level is higher, then the tone of the sentence is sad. Otherwise, it is neutral. Find and print the tone of the sentence by first printing a sentiment line with a number of + equal to the number of happy words followed by the number of - equal to the number of sad words, followed by a simple statement of the analysis.
|
||||
|
||||
Two examples of the program run (how it will look when you run it using Spyder IDE 101) are provided in files `hw2 part3 output 01.txt` and `hw2 part3 output 02.txt` (Can be found inside the `hw02_files.zip` file).
|
||||
|
||||
To accomplish this you will write a function called `number_happy(sentence)` which returns the number of words in a given string called sentence that are happy. To do this, find the total count of the following 6 words: laugh happiness love excellent good smile. Here is an example run of this function:
|
||||
|
||||
```python
|
||||
>>> number_happy("I laughed and laughed at her excellent joke.")
|
||||
3
|
||||
```
|
||||
|
||||
This is because the count of happy words is 3 (laugh is repeated twice). Your code should work even if there are upper and lower case words and extra spaces in the beginning and end of the sentence.
|
||||
|
||||
```python
|
||||
>>> number_happy(" Happiness is the state of a student who started homework early. ")
|
||||
1
|
||||
```
|
||||
|
||||
Next, write a second function called `number_sad(sentence)` that works the same way but instead counts the number of the following 6 sad words in English: bad sad terrible horrible problem hate
|
||||
|
||||
```python
|
||||
>>> number_sad("Dr. Horrible's Sing-Along Blog is an excellent show.")
|
||||
1
|
||||
>>> number_sad("Alexander and the Terrible, Horrible, No Good, Very Bad Day")
|
||||
3
|
||||
```
|
||||
|
||||
Of courses, there are more than 6 words of each category. We will see how to feed them using a file and use lists to process them in future classes.
|
||||
|
||||
Test your code well and when you are sure that it works, please submit it as a file named `hw2 part3.py` to Submitty for Part 3 of the homework.
|
||||
|
||||
## Supporting Files
|
||||
|
||||
{{< link href="HW2.zip" content="HW2.zip" title="Download HW2.zip" download="HW2.zip" card=true >}}
|
||||
|
||||
***
|
||||
|
||||
## Solution
|
||||
|
||||
### hw2_part1.py
|
||||
|
||||
```python
|
||||
import math
|
||||
|
||||
#Functions
|
||||
|
||||
def find_volume_sphere(radius):
|
||||
"""Calculates the volume of a sphere with a given radius"""
|
||||
return (4/3) * math.pi * radius**3
|
||||
|
||||
def find_volume_cube(side):
|
||||
"""Calculates the volume of a cube with a given side length"""
|
||||
return side**3
|
||||
|
||||
#Input
|
||||
|
||||
radius = str(input("Enter the gum ball radius (in.) => ").strip())
|
||||
print(radius)
|
||||
weekly_sales = str(input("Enter the weekly sales => ").strip())
|
||||
print(weekly_sales, "\n")
|
||||
|
||||
#Calculations
|
||||
|
||||
target_sales = math.ceil(float(weekly_sales) * 1.25)
|
||||
|
||||
edge_gumballs = math.ceil(target_sales**(1/3))
|
||||
edge_length = edge_gumballs * float(radius)*2
|
||||
edge_gumballs_max = edge_length / (float(radius)*2 + 0.0000000000000001)
|
||||
#Aviod ZeroDivisionError by adding a small number to the radius
|
||||
|
||||
number_extra_gumballs = math.ceil(edge_gumballs_max**3 - target_sales)
|
||||
|
||||
volume_gumballs = find_volume_sphere(float(radius))
|
||||
volume_cube = find_volume_cube(edge_length)
|
||||
volume_wasted_target = volume_cube - volume_gumballs * target_sales
|
||||
volume_wasted_full = volume_cube - volume_gumballs * (edge_gumballs_max) ** 3
|
||||
|
||||
#Print
|
||||
|
||||
print("The machine needs to hold", str(edge_gumballs), "gum balls along each edge.")
|
||||
print("Total edge length is", "{:.2f}".format(edge_length), "inches.")
|
||||
print("Target sales were", str(target_sales) + ", but the machine will hold", str(int(number_extra_gumballs)), "extra gum balls.")
|
||||
print("Wasted space is", "{:.2f}".format(volume_wasted_target), "cubic inches with the target number of gum balls,")
|
||||
print("or", "{:.2f}".format(volume_wasted_full), "cubic inches if you fill up the machine.")
|
||||
```
|
||||
|
||||
### hw2_part2.py
|
||||
|
||||
```python
|
||||
user_input = input("Enter a string to encode ==> ").strip()
|
||||
print(user_input, "\n")
|
||||
|
||||
#Replacing
|
||||
def encrypt(word):
|
||||
word = word.replace(" a", "%4%")
|
||||
word = word.replace("he", "7!")
|
||||
word = word.replace("e", "9(*9(")
|
||||
word = word.replace("y", "*%$")
|
||||
word = word.replace("u", "@@@")
|
||||
word = word.replace("an", "-?")
|
||||
word = word.replace("th", "!@+3")
|
||||
word = word.replace("o", "7654")
|
||||
word = word.replace("9", "2")
|
||||
word = word.replace("ck", "%4")
|
||||
return word
|
||||
|
||||
#Calculation
|
||||
|
||||
length_difference = abs(len(user_input) - len(encrypt(user_input)))
|
||||
|
||||
#Decoding
|
||||
|
||||
def decrypt(word):
|
||||
word = word.replace("%4", "ck")
|
||||
word = word.replace("2", "9")
|
||||
word = word.replace("7654", "o")
|
||||
word = word.replace("!@+3", "th")
|
||||
word = word.replace("-?", "an")
|
||||
word = word.replace("@@@", "u")
|
||||
word = word.replace("*%$", "y")
|
||||
word = word.replace("9(*9(", "e")
|
||||
word = word.replace("7!", "he")
|
||||
word = word.replace("%4%", " a")
|
||||
return word
|
||||
|
||||
#Printing
|
||||
|
||||
print("Encrypted as ==>", encrypt(user_input))
|
||||
print("Difference in length ==>", str(length_difference))
|
||||
print("Deciphered as ==>", decrypt(encrypt(user_input)))
|
||||
|
||||
if user_input == decrypt(encrypt(user_input)):
|
||||
print("Operation is reversible on the string.")
|
||||
else:
|
||||
print("Operation is not reversible on the string.")
|
||||
```
|
||||
|
||||
### hw2_part3.py
|
||||
|
||||
```python
|
||||
def number_happy(sentence):
|
||||
happy_words = ["laugh", "happiness", "love", "excellent", "good", "smile"]
|
||||
sentence = sentence.lower()
|
||||
#sentence = sentence.strip()
|
||||
#sentence = sentence.split()
|
||||
count = 0
|
||||
for word in happy_words:
|
||||
count += sentence.count(word)
|
||||
return count
|
||||
|
||||
def number_sad(sentence):
|
||||
sad_words = ["bad", "sad", "terrible", "horrible", "problem", "hate"]
|
||||
sentence = sentence.lower()
|
||||
#sentence = sentence.strip()
|
||||
#sentence = sentence.split()
|
||||
count = 0
|
||||
for word in sad_words:
|
||||
count += sentence.count(word)
|
||||
return count
|
||||
|
||||
#Get user input
|
||||
|
||||
#sentence = "I laughed and laughed at her excellent joke."
|
||||
sentence = input("Enter a sentence => ").strip()
|
||||
|
||||
#Print
|
||||
|
||||
#print(number_happy(sentence))
|
||||
print(sentence)
|
||||
print("Sentiment: " + ("+" * number_happy(sentence)) + ("-" * number_sad(sentence)))
|
||||
|
||||
if number_happy(sentence) > number_sad(sentence):
|
||||
print("This is a happy sentence.")
|
||||
elif number_happy(sentence) == number_sad(sentence):
|
||||
print("This is a neutral sentence.")
|
||||
else:
|
||||
print("This is a sad sentence.")
|
||||
```
|
||||
BIN
content/zh-cn/posts/csci-1100/hw-2/HW2.zip
Normal file
BIN
content/zh-cn/posts/csci-1100/hw-2/HW2.zip
Normal file
Binary file not shown.
326
content/zh-cn/posts/csci-1100/hw-2/index.md
Normal file
326
content/zh-cn/posts/csci-1100/hw-2/index.md
Normal file
@@ -0,0 +1,326 @@
|
||||
---
|
||||
title: CSCI 1100 - 作业2 - 字符串和函数
|
||||
subtitle:
|
||||
date: 2024-03-12T02:41:25-04:00
|
||||
slug: csci-1100-hw-2
|
||||
draft: false
|
||||
author:
|
||||
name: James
|
||||
link: https://www.jamesflare.com
|
||||
email:
|
||||
avatar: /site-logo.avif
|
||||
description: 本次家庭作业共分为三部分,重点是使用 Python 函数和字符串操作来设计口香糖机的大小,实现一个简单的替换密码,以及对句子进行基本的情感分析。
|
||||
keywords: ["Python","函数","字符串操作","情感分析"]
|
||||
license:
|
||||
comment: true
|
||||
weight: 0
|
||||
tags:
|
||||
- CSCI 1100
|
||||
- 作业
|
||||
- RPI
|
||||
- Python
|
||||
- 编程
|
||||
categories:
|
||||
- CSCI
|
||||
- 编程语言
|
||||
hiddenFromHomePage: false
|
||||
hiddenFromSearch: false
|
||||
hiddenFromRss: false
|
||||
hiddenFromRelated: false
|
||||
summary: 本次家庭作业共分为三部分,重点是使用 Python 函数和字符串操作来设计口香糖机的大小,实现一个简单的替换密码,以及对句子进行基本的情感分析。
|
||||
resources:
|
||||
- name: featured-image
|
||||
src: featured-image.jpg
|
||||
- name: featured-image-preview
|
||||
src: featured-image-preview.jpg
|
||||
toc: true
|
||||
math: false
|
||||
lightgallery: false
|
||||
password:
|
||||
message:
|
||||
repost:
|
||||
enable: true
|
||||
url:
|
||||
|
||||
# See details front matter: https://fixit.lruihao.cn/documentation/content-management/introduction/#front-matter
|
||||
---
|
||||
|
||||
<!--more-->
|
||||
|
||||
## 概述
|
||||
|
||||
本次家庭作业总分100分,将计入你的总体家庭作业成绩,截止日期为2024年2月1日星期四晚上11:59:59。三个部分应分别提交。所有部分都必须在截止日期前提交,否则你的程序将被视为迟交。
|
||||
|
||||
**关于评分的注意事项**:请务必仔细阅读提交指南文档。它适用于本次和以后所有的家庭作业,并且将变得越来越重要。在家庭作业的所有部分中,我们会指定你必须提供哪些函数。即使这些函数非常简单,也请务必编写它们。否则,你将失去分数。随着学期的进展,我们将编写更复杂的函数。此外,在评分本次家庭作业时,我们还会检查程序结构(参见第4讲),以及变量和函数的命名。
|
||||
|
||||
在本次作业中的任何地方都不允许使用任何循环。
|
||||
|
||||
## 关于过度合作的严正警告
|
||||
|
||||
对于本学期的所有家庭作业,我们将使用软件来比较所有提交的程序,以发现不当的相似之处。该软件可以处理程序之间的各种差异,因此如果你 (a) 拿了别人的程序,修改了(或没有修改)并作为自己的程序提交,(b) 与一个或多个同学一起编写了一个程序,并分别提交修改后的版本作为自己的作品,或者 (c) 提交(可能经过轻微修改)以前某一年提交的软件作为你的软件,这个软件都会将这些提交标记为非常相似。
|
||||
|
||||
(a)、(b) 和 (c) 都超出了本课程可接受的范围——它们违反了学术诚信政策。此外,这种抄袭行为会阻碍你学习如何解决问题,从长远来看会对你不利。你编写自己的代码越多,学到的东西就越多。
|
||||
|
||||
请阅读合作政策文档,了解可接受的合作水平以及如何保护自己。该文档可以在 Submitty 的课程材料页面上找到。过度合作的处罚可高达:
|
||||
|
||||
- 家庭作业得0分,并且
|
||||
- 学期成绩再额外降低5%。
|
||||
|
||||
受到处罚的学生也将被禁止退出该课程。更严重的违规行为,如盗用他人代码,将导致该课程自动得F。第二次被发现学术诚信违规的学生将自动得到F。
|
||||
|
||||
通过提交你的家庭作业,你断言你 (a) 理解学术诚信政策,并且 (b) 没有违反它。
|
||||
|
||||
最后请注意,制定这项政策是为了应对本课程中可能出现的小部分问题。遵循上述策略并在执行过程中运用常识的学生不会在学术诚信方面遇到任何麻烦。
|
||||
|
||||
## 第1部分:一分钱一个口香糖米奇(40分)
|
||||
|
||||
我们将进行一个销售口香糖的实验,但需要做一些假设。假设你从自动售货机出售口香糖。售货机是立方体形状,口香糖是球形。你每周检查一次售货机。目标是调整售货机的大小,使其在每周开始时完全装满,在你回来检查之前不会卖完口香糖,并且在一周结束时不会留下太多变质的口香糖。我们假设所有的口香糖都整齐地排列,因此沿立方体任何一个维度的口香糖数量就等于该维度的边长除以口香糖直径。例如,如果边长是9.0,口香糖的半径正好是0.5,那么每个维度正好可以容纳9个口香糖,整个售货机总共可容纳729个口香糖。这被称为立方晶格。
|
||||
|
||||
请完成以下任务:
|
||||
|
||||
1. 首先编写两个函数,`find_volume_sphere(radius)` 和 `find_volume_cube(side)`,分别用于计算给定半径的球体体积和给定边长的立方体体积。
|
||||
|
||||
2. 然后询问用户口香糖的半径和每周的销售量。
|
||||
|
||||
3. 计算售货机需要容纳的口香糖总数为每周销售量的1.25倍,并利用这个数据计算以口香糖个数为单位的售货机边长。提示:你知道口香糖的总数,在立方晶格中,每个维度可以容纳相同数量的口香糖,所以如果每个维度可以容纳 N 个口香糖,那么整个售货机可以容纳 N³ 个口香糖。数学模块的 `ceil` 函数总是向上取整,可能会有用。(我们不会切割口香糖来使它们适合售货机。)
|
||||
|
||||
4. 再计算几个值:考虑到你选择的尺寸,实际可以容纳多少个口香糖(记住,沿立方体的每个维度必须容纳整数个口香糖);立方体的体积;每个口香糖的体积;如果我们放入需要容纳的口香糖数量和实际可以容纳的数量,浪费的空间大小。
|
||||
|
||||
5. 使用 `.2f` 格式打印这些值,所有浮点数保留2位小数。
|
||||
|
||||
在文件 `hw2 part1 output 01.txt` 和 `hw2 part1 output 02.txt`(需要从 Submitty 的课程材料部分下载 `hw02_files.zip` 文件,并将其解压缩到你的 HW 2 目录中)中提供了程序运行的两个示例(使用 Spyder IDE 运行时的样子)。
|
||||
|
||||
我们将测试你的代码是否符合上述值以及一系列不同的值。请充分测试你的代码,确保它能正常工作后,将其作为 `hw2 part1.py` 文件提交到 Submitty 作为家庭作业的第1部分。
|
||||
|
||||
## 第2部分:找到隐藏的信息(40分)
|
||||
|
||||
编写一个程序,判断对于给定的字符串,简单的替换密码是否可逆。该程序应使用 `input` 要求用户输入一个包含句子的字符串。然后,程序应将字符串加密成密文,再解密密文,并将解密结果与原始句子进行比较。如果解密后的密文与原始句子匹配,则该操作在该字符串上是可逆的。否则,它是不可逆的。
|
||||
|
||||
在此过程中,程序应打印出密文、密文与原始句子之间的长度差(始终打印为正数)、解密后的密文以及一条简短消息,说明该操作是否可逆。
|
||||
|
||||
在文件 `hw2 part2 output 01.txt` 和 `hw2 part2 output 02.txt`(可以在 `hw02_files.zip` 文件中找到)中提供了程序运行的两个示例(使用 Spyder IDE 运行时的样子)。
|
||||
|
||||
加密规则基于一组字符串替换,它们应该按照以下确切顺序应用:
|
||||
|
||||
| 原始 | 替换 | 注释 |
|
||||
|:----:|:----:|:-----|
|
||||
| ' a' | '%4%' | 将空格后的任何'a'替换为'%4%' |
|
||||
| 'he' | '7!' | 将所有出现的'he'替换为'7!' |
|
||||
| 'e' | '9(*9(' | 将任何剩余的'e'替换为'9(*9(' |
|
||||
| 'y' | '*%' | 将所有出现的'y'替换为'*%' |
|
||||
| 'u' | '@@@' | 将所有出现的'u'替换为'@@@' |
|
||||
| 'an' | '-?' | 将所有出现的'an'替换为'-?' |
|
||||
| 'th' | '!@+3' | 将所有出现的'th'替换为'!@+3' |
|
||||
| 'o' | '7654' | 将所有出现的'o'替换为'7654' |
|
||||
| '9' | '2' | 将所有出现的'9'替换为'2' |
|
||||
| 'ck' | '%4' | 将所有出现的'ck'替换为'%4' |
|
||||
|
||||
例如,"methane"的密文是 `m2(*2(!@+3-?2(*2(`。以下是加密过程:
|
||||
|
||||
```python
|
||||
>>> 'methane'.replace('e','9(*9(')
|
||||
'm9(*9(than9(*9('
|
||||
>>> 'm9(*9(than9(*9('.replace('an','-?')
|
||||
'm9(*9(th-?9(*9('
|
||||
>>> 'm9(*9(th-?9(*9('.replace('th','!@+3')
|
||||
'm9(*9(!@+3-?9(*9('
|
||||
>>> 'm9(*9(!@+3-?9(*9('.replace('9', '2')
|
||||
'm2(*2(!@+3-?2(*2('
|
||||
```
|
||||
|
||||
解密将按相反顺序使用这些规则。
|
||||
|
||||
你的程序必须使用两个函数:
|
||||
|
||||
- 编写一个函数 `encrypt(word)`,它接受一个普通英语字符串作为参数,并返回它的密文版本(也是字符串)。
|
||||
|
||||
- 编写第二个函数 `decrypt(word)`,它执行相反的操作:接受密文字符串并返回它的普通英语版本。
|
||||
|
||||
这两个函数在结构上会非常相似,但它们使用字符串替换规则的顺序不同。现在,你可以通过先加密一个字符串,然后再解密来测试你的函数是否正确。如果替换规则没有歧义,结果应该与原始字符串相同。
|
||||
|
||||
使用这些函数来实现上述程序。我们将测试你的代码是否符合上述值以及一系列不同的值。
|
||||
|
||||
请充分测试你的代码,确保它能正常工作后,将其作为 `hw2 part2.py` 文件提交到 Submitty 作为家庭作业的第2部分。
|
||||
|
||||
## 第3部分:你对家庭作业有何感想?(20分)
|
||||
|
||||
在本部分作业中,你将实现一个非常简单的情感分析工具。虽然真正的工具使用自然语言处理,但它们都使用类似于我们在这里使用的词频统计。理解消息中的情感是许多人工智能工具的关键部分。
|
||||
|
||||
编写一个程序,要求用户输入一个包含句子的字符串。然后,程序将使用下面描述的两个函数计算句子的快乐程度和悲伤程度。如果快乐程度高于悲伤程度,那么句子的语气是快乐的。如果悲伤程度更高,那么句子的语气是悲伤的。否则,它是中性的。首先打印一行情感分析结果,其中 + 号的数量等于快乐词的数量,- 号的数量等于悲伤词的数量,然后给出一个简单的分析结论,找出并打印句子的语气。
|
||||
|
||||
在文件 `hw2 part3 output 01.txt` 和 `hw2 part3 output 02.txt`(可以在 `hw02_files.zip` 文件中找到)中提供了程序运行的两个示例(使用 Spyder IDE 101 运行时的样子)。
|
||||
|
||||
为了实现这一点,你将编写一个名为 `number_happy(sentence)` 的函数,它返回给定字符串中称为句子的快乐词的数量。为此,统计以下6个词的出现次数:laugh、happiness、love、excellent、good、smile。下面是此函数的一个示例运行:
|
||||
|
||||
```python
|
||||
>>> number_happy("I laughed and laughed at her excellent joke.")
|
||||
3
|
||||
```
|
||||
|
||||
这是因为快乐词的数量是3(laugh 出现了两次)。即使句子中有大小写词和句子开头及结尾有额外的空格,你的代码也应该能正常工作。
|
||||
|
||||
```python
|
||||
>>> number_happy(" Happiness is the state of a student who started homework early. ")
|
||||
1
|
||||
```
|
||||
|
||||
接下来,编写第二个函数 `number_sad(sentence)`,它的工作方式相同,但统计句子中以下6个悲伤词的数量:bad、sad、terrible、horrible、problem、hate。
|
||||
|
||||
```python
|
||||
>>> number_sad("Dr. Horrible's Sing-Along Blog is an excellent show.")
|
||||
1
|
||||
>>> number_sad("Alexander and the Terrible, Horrible, No Good, Very Bad Day")
|
||||
3
|
||||
```
|
||||
|
||||
当然,每个类别的词语远不止6个。在未来的课程中,我们将学习如何使用文件来读取词语并用列表来处理它们。
|
||||
|
||||
请充分测试你的代码,确保它能正常工作后,将其作为 `hw2 part3.py` 文件提交到 Submitty 作为家庭作业的第3部分。
|
||||
|
||||
## 支持文件
|
||||
|
||||
{{< link href="HW2.zip" content="HW2.zip" title="下载 HW2.zip" download="HW2.zip" card=true >}}
|
||||
|
||||
***
|
||||
|
||||
## 参考答案
|
||||
|
||||
### hw2_part1.py
|
||||
|
||||
```python
|
||||
import math
|
||||
|
||||
#Functions
|
||||
|
||||
def find_volume_sphere(radius):
|
||||
"""Calculates the volume of a sphere with a given radius"""
|
||||
return (4/3) * math.pi * radius**3
|
||||
|
||||
def find_volume_cube(side):
|
||||
"""Calculates the volume of a cube with a given side length"""
|
||||
return side**3
|
||||
|
||||
#Input
|
||||
|
||||
radius = str(input("Enter the gum ball radius (in.) => ").strip())
|
||||
print(radius)
|
||||
weekly_sales = str(input("Enter the weekly sales => ").strip())
|
||||
print(weekly_sales, "\n")
|
||||
|
||||
#Calculations
|
||||
|
||||
target_sales = math.ceil(float(weekly_sales) * 1.25)
|
||||
|
||||
edge_gumballs = math.ceil(target_sales**(1/3))
|
||||
edge_length = edge_gumballs * float(radius)*2
|
||||
edge_gumballs_max = edge_length / (float(radius)*2 + 0.0000000000000001)
|
||||
#Aviod ZeroDivisionError by adding a small number to the radius
|
||||
|
||||
number_extra_gumballs = math.ceil(edge_gumballs_max**3 - target_sales)
|
||||
|
||||
volume_gumballs = find_volume_sphere(float(radius))
|
||||
volume_cube = find_volume_cube(edge_length)
|
||||
volume_wasted_target = volume_cube - volume_gumballs * target_sales
|
||||
volume_wasted_full = volume_cube - volume_gumballs * (edge_gumballs_max) ** 3
|
||||
|
||||
#Print
|
||||
|
||||
print("The machine needs to hold", str(edge_gumballs), "gum balls along each edge.")
|
||||
print("Total edge length is", "{:.2f}".format(edge_length), "inches.")
|
||||
print("Target sales were", str(target_sales) + ", but the machine will hold", str(int(number_extra_gumballs)), "extra gum balls.")
|
||||
print("Wasted space is", "{:.2f}".format(volume_wasted_target), "cubic inches with the target number of gum balls,")
|
||||
print("or", "{:.2f}".format(volume_wasted_full), "cubic inches if you fill up the machine.")
|
||||
```
|
||||
|
||||
### hw2_part2.py
|
||||
|
||||
```python
|
||||
user_input = input("Enter a string to encode ==> ").strip()
|
||||
print(user_input, "\n")
|
||||
|
||||
#Replacing
|
||||
def encrypt(word):
|
||||
word = word.replace(" a", "%4%")
|
||||
word = word.replace("he", "7!")
|
||||
word = word.replace("e", "9(*9(")
|
||||
word = word.replace("y", "*%$")
|
||||
word = word.replace("u", "@@@")
|
||||
word = word.replace("an", "-?")
|
||||
word = word.replace("th", "!@+3")
|
||||
word = word.replace("o", "7654")
|
||||
word = word.replace("9", "2")
|
||||
word = word.replace("ck", "%4")
|
||||
return word
|
||||
|
||||
#Calculation
|
||||
|
||||
length_difference = abs(len(user_input) - len(encrypt(user_input)))
|
||||
|
||||
#Decoding
|
||||
|
||||
def decrypt(word):
|
||||
word = word.replace("%4", "ck")
|
||||
word = word.replace("2", "9")
|
||||
word = word.replace("7654", "o")
|
||||
word = word.replace("!@+3", "th")
|
||||
word = word.replace("-?", "an")
|
||||
word = word.replace("@@@", "u")
|
||||
word = word.replace("*%$", "y")
|
||||
word = word.replace("9(*9(", "e")
|
||||
word = word.replace("7!", "he")
|
||||
word = word.replace("%4%", " a")
|
||||
return word
|
||||
|
||||
#Printing
|
||||
|
||||
print("Encrypted as ==>", encrypt(user_input))
|
||||
print("Difference in length ==>", str(length_difference))
|
||||
print("Deciphered as ==>", decrypt(encrypt(user_input)))
|
||||
|
||||
if user_input == decrypt(encrypt(user_input)):
|
||||
print("Operation is reversible on the string.")
|
||||
else:
|
||||
print("Operation is not reversible on the string.")
|
||||
```
|
||||
|
||||
### hw2_part3.py
|
||||
|
||||
```python
|
||||
def number_happy(sentence):
|
||||
happy_words = ["laugh", "happiness", "love", "excellent", "good", "smile"]
|
||||
sentence = sentence.lower()
|
||||
#sentence = sentence.strip()
|
||||
#sentence = sentence.split()
|
||||
count = 0
|
||||
for word in happy_words:
|
||||
count += sentence.count(word)
|
||||
return count
|
||||
|
||||
def number_sad(sentence):
|
||||
sad_words = ["bad", "sad", "terrible", "horrible", "problem", "hate"]
|
||||
sentence = sentence.lower()
|
||||
#sentence = sentence.strip()
|
||||
#sentence = sentence.split()
|
||||
count = 0
|
||||
for word in sad_words:
|
||||
count += sentence.count(word)
|
||||
return count
|
||||
|
||||
#Get user input
|
||||
|
||||
#sentence = "I laughed and laughed at her excellent joke."
|
||||
sentence = input("Enter a sentence => ").strip()
|
||||
|
||||
#Print
|
||||
|
||||
#print(number_happy(sentence))
|
||||
print(sentence)
|
||||
print("Sentiment: " + ("+" * number_happy(sentence)) + ("-" * number_sad(sentence)))
|
||||
|
||||
if number_happy(sentence) > number_sad(sentence):
|
||||
print("This is a happy sentence.")
|
||||
elif number_happy(sentence) == number_sad(sentence):
|
||||
print("This is a neutral sentence.")
|
||||
else:
|
||||
print("This is a sad sentence.")
|
||||
```
|
||||
Reference in New Issue
Block a user