又到 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 ' ]

3425 次点击
所在节点    Python
29 条回复
HHH01
2019-11-15 19:08:28 +08:00
@InkStone 还没学到,,,
HHH01
2019-11-15 19:10:19 +08:00
@InkStone 我想到的是用 index 定位到 minutes,然后再减去三位数,得到数值的定位,再取出数值,除以二
HHH01
2019-11-15 19:46:00 +08:00
这是我想到的第一题,但好像不完美,特别是,当时间为三位数比如 100 分钟,他总是显示不出来。。。
def positivize(review):
if "bad" in review or "Bad" in review or "BAD" in review:
review=review.replace("bad", "good")
review=review.replace("Bad", "Good")
review=review.replace("BAD", "GOOD")

if "horrible" in review or "Horrible" in review or "HORRIBLE" in review:
review=review.replace("horrible", "fantastic")
review=review.replace("Horrible", "Fantastic")
review=review.replace("HORRIBLE", "FANTASTIC")

if "dirty" in review or "Dirty" in review or "DIRTY" in review:
review=review.replace("dirty", "clean")
review=review.replace("Dirty", "Clean")
review=review.replace("DIRTY", "CLEAN")

if "disgusting" in review or "Disgusting" in review or "DISGUSTING" in review:
review=review.replace("disgusting", "sublime")
review=review.replace("Disgusting", "Sublime")
review=review.replace("DISGUSTING", "SUBLIME")

if "expensive" in review or "Expensive" in review or "EXPENSIVE" in review:
review=review.replace("expensive", "affordable")
review=review.replace("Expensive", "Affordable")
review=review.replace("Expensive", "AFFORDABLE")

if "moldy" in review or "Moldy" in review or "MOLDY" in review or "MOLDY" in review:
review=review.replace("moldy", "flavourful")
review=review.replace("Moldy", "Flavourful")
review=review.replace("MOLDY", "FLAVOURFUL")

if "frozen" in review or"Frozen" in review or "FROZEN" in review:
review=review.replace("frozen", "farm-fresh")
review=review.replace("Frozen", "Farm-fresh")
review=review.replace("FROZEN", "FARM-FRESH")

if "minutes" in review or "Minutes" in review or "min" in review or "Min" in review or "MIN" in review or "MINUTES" in review:
a=review.index("minutes")
if a<100:
i=a-3
j=a-1
n=int((review[i:j]))
review=review.replace(str(review[i:j]),"only "+str(int(n/2)))
if a>=100:
i=a-5
j=a-1
n=int(review[i:j])
review=review.replace(str(review[i:j]),"only "+str(n/2))



return review
hellwys1
2019-11-15 19:47:40 +08:00
如果没有怎么接触过编程,是可以理解的。
建议学习如何使用谷歌,自己去找,比在论坛问舒服多了。
ErrorMan
2019-11-15 19:53:17 +08:00
同楼上各位的想法,问题太过基础,靠搜索引擎就可以解决,而不应该花费更多时间来发帖问。
没有思路的话建议先去找些简单题做,基础不牢问到了题目也没有好效果,不可能每一题你都能来问。
另外在家复习自学并不是什么理由,网上能拿到的课程资源内容没那么系统,但是对你的基础状况来说已经非常够用了。
如果不知道去哪里的话,这里指路 coursera 和 中国 mooc
HHH01
2019-11-15 20:19:26 +08:00
@hellwys1 是这样,求人不如求己。发现这个论坛不太适合非程序员。
AlisaDestiny
2019-11-15 20:22:01 +08:00
xiaoming1992
2019-11-15 20:34:50 +08:00
把大问题切分成小问题一步一步来,比方说第一题,可以先写一个函数,分析一个单词是 UPPERCASE、lowercase 还是 Pascalcase,再写一个函数将小写单词转成相应的单词,再写个函数作正则替换,这个题目不就解决了吗
HHH01
2019-11-15 22:36:23 +08:00
@xiaoming1992 感谢,我已经解决了

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

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

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

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

© 2021 V2EX