又到 Python 作业时间,这次三道题有两道题难住了我。。。

2019-11-15 16:05:40 +08:00
 HHH01

又遇到难题了,人不在学校还没法跟同学们一块讨论,只能求助 V2EX 的各路大神了!

第一题是写一个 Function 把负面评价全部改成正面。。。还要求大小写对应还有如果提到等待时间,等待时间要减半。。。这个可咋整?

Exercise 3. You are a restaurant owner plagued by bad online reviews. You want to write a function to turn these bad reviews into good reviews. In a file called positivity.py, write a function positivize(review) that takes review text review as a string. Your function should return a “positivized” version of the review, where:

• Every instance of the word “bad” should be replaced by “good”

• Every instance of the word “horrible” should be replaced by “fantastic”

• Every instance of the word “dirty” should be replaced by “clean”

• Every instance of the word “disgusting” should be replaced by “sublime”

• Every instance of the word “expensive” should be replaced by “affordable”

• Every instance of the word “moldy” should be replaced by “flavourful”

• Every instance of the word “frozen” should be replaced by “farm-fresh”

• Every instance of the phrase “n minutes” should be replaced by “only n=2 minutes”

Your function should preserve the original review’s capitalization – i.e. “Dirty” should map to “Clean.” You can assume the original review is capitalized sensibly. Example: The string

The food was horrible!!! We waited 40 minutes for frozen vegetables and moldy bread. Disgusting!

should map to

The food was fantastic!!! We waited only 20 minutes for farm-fresh vegetables and flavourful bread. Sublime!

第二题是写一个排序的 Function,但是不能用 list.sort 和 sorted,不过这个有点思路,就是先把 list 中最小的排出来,然后摘出来,再把这个最小的从 list 里面删除,再重复操作。

Exercise 4. In a file called order.py, write a function sort(words) , which takes as an argument a list of strings words . sort should modify words so as to sort the elements in alphabetical order. Python provides some built-in functions to do this, such as list.sort and sorted – do not use these for this exercise. Your solution should only use “basic” components for its logic, such as list access, list assignments, variables, if-statements, loops. In particular, the only built-in function you may use for this exercise is len .

Example:

animals = [ ' cat ' , ' bat ' , ' zebra ' , ' fish ' , ' dog ' ]

sort(animals)

animals

[ ' bat ' , ' cat ' , ' dog ' , ' fish ' , ' zebra ' ]

3411 次点击
所在节点    Python
29 条回复
InkStone
2019-11-15 16:18:12 +08:00
我不是很明白你究竟卡在哪里了……
第一道题不就是替换一下字符串吗,N minutes 可以用正则匹配。第二道题你的思路肯定是可行的……不过最好还是学习一下正统的排序算法。
cigarzh
2019-11-15 16:27:12 +08:00
基础太差,建议复习
HHH01
2019-11-15 16:27:42 +08:00
@InkStone 啥是正则匹配?
HHH01
2019-11-15 16:32:35 +08:00
@InkStone 大小写的问题不是很明白,比如我一个词中某一个字母大写
@cigarzh 谢谢,刚开始学习,总共才上了 5 节课,当然这不是借口,确实基础差。
jmc891205
2019-11-15 17:15:12 +08:00
"You can assume the original review is capitalized sensibly."
cherbim
2019-11-15 17:25:01 +08:00
基础太差,建议留级
scukmh
2019-11-15 17:27:30 +08:00
基础太差,建议复读
rioshikelong121
2019-11-15 17:28:32 +08:00
基础太差,不如。。
InkStone
2019-11-15 17:29:15 +08:00
@ryanjmliao

正则表达式,专门用于文本匹配的东西。刚接触的时候可能觉得有些复杂,不过很实用,是必学必会的东西。
JerryCha
2019-11-15 17:29:46 +08:00
问题 1 逐行读取按空格 split,按情况匹配,善用 python 的 in 减少 if-else 的数量。
问题 2 读完之后自己实现,[]( https://runestone.academy/runestone/books/published/pythonds/SortSearch/sorting.html)
HHH01
2019-11-15 17:55:39 +08:00
@cherbim
@scukmh
@rioshikelong121
要有基础也不会来宝地问了,说了总共才上 5 节课,以前也没学过,如果说 V2EX 是不能问基础题目的,那我以后就另寻他处问吧,打扰。
HHH01
2019-11-15 18:00:49 +08:00
@JerryCha 就是 in 用了很多了,我是想全部只用一个 if,然后 or '' xxx" in review,但是一想情况太多了,比如 Good, good, 或者 GOOD,相当于每一个都有三遍。。。那我的用 7x3=21 个 or in
HHH01
2019-11-15 18:01:38 +08:00
@jmc891205 就是这个不懂
Yvette
2019-11-15 18:02:14 +08:00
@ryanjmliao 不是不欢迎问基础问题,而是你问的都是网上拿关键词一搜就能找到答案的问题。如果不知道怎么搜,可以说清楚你卡在了什么地方,不然你这样原题直接丢上来就问,太伸手党了
hakono
2019-11-15 18:03:05 +08:00
@ryanjmliao 直接把原始文本都处理成大写或小写不就好了? 原始文本留一份,改在原始文本里
amet
2019-11-15 18:08:57 +08:00
@ryanjmliao 正文中全小写,句首大写,表达强烈情感全部大写。你这么问 jmc891205 我还以为自己对 " capitalized sensibly." 的理解出了偏差,还去查了遍词典。
superrichman
2019-11-15 18:16:08 +08:00
帮忙做 assignment 要收费的 /手动狗头
JerryCha
2019-11-15 18:21:33 +08:00
@ryanjmliao
善用 lower、upper,善用 title,善用网络资料查找自带方法。https://www.runoob.com/python3/python3-string.html

在国外念书你应该明白,上课讲了多少根本不是你会与不会的理由,上课讲的浅不代表对学生的要求低。
HHH01
2019-11-15 18:26:58 +08:00
@Yvette 我都有说明吧,比如第一题,我卡住的地方是大小写,在第二题,我也把自己的思路说出来,我是希望在这里求问,能获得点播,我从来没有伸手要答案,我也没这个习惯,你可以看看我之前发的求教帖子,我没要求大家都把答案贴出来,我都是希望大家给思路,一起讨论。
如果觉得这个帖子对你没有价值或者不想回答,大可以不必要回复,但是没必要冷嘲热讽。如果探讨题目本身那我很感谢。
HHH01
2019-11-15 18:42:07 +08:00
@JerryCha 不瞒你说,因为签证问题,我还没去上过一节课,也还没见到过同学,所以是在家看课件自学,我只能说你说的很对,感谢提醒。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/619941

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX