又遇到难题了,人不在学校还没法跟同学们一块讨论,只能求助 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 ' ]
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.