又遇到难题了,人不在学校还没法跟同学们一块讨论,只能求助 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 ' ]
1
InkStone 2019-11-15 16:18:12 +08:00 1
我不是很明白你究竟卡在哪里了……
第一道题不就是替换一下字符串吗,N minutes 可以用正则匹配。第二道题你的思路肯定是可行的……不过最好还是学习一下正统的排序算法。 |
2
cigarzh 2019-11-15 16:27:12 +08:00 1
基础太差,建议复习
|
4
HHH01 OP |
5
jmc891205 2019-11-15 17:15:12 +08:00
"You can assume the original review is capitalized sensibly."
|
6
cherbim 2019-11-15 17:25:01 +08:00
基础太差,建议留级
|
7
scukmh 2019-11-15 17:27:30 +08:00
基础太差,建议复读
|
8
rioshikelong121 2019-11-15 17:28:32 +08:00
基础太差,不如。。
|
9
InkStone 2019-11-15 17:29:15 +08:00
|
10
JerryCha 2019-11-15 17:29:46 +08:00 1
问题 1 逐行读取按空格 split,按情况匹配,善用 python 的 in 减少 if-else 的数量。
问题 2 读完之后自己实现,[]( https://runestone.academy/runestone/books/published/pythonds/SortSearch/sorting.html) |
11
HHH01 OP @cherbim
@scukmh @rioshikelong121 要有基础也不会来宝地问了,说了总共才上 5 节课,以前也没学过,如果说 V2EX 是不能问基础题目的,那我以后就另寻他处问吧,打扰。 |
12
HHH01 OP @JerryCha 就是 in 用了很多了,我是想全部只用一个 if,然后 or '' xxx" in review,但是一想情况太多了,比如 Good, good, 或者 GOOD,相当于每一个都有三遍。。。那我的用 7x3=21 个 or in
|
14
Yvette 2019-11-15 18:02:14 +08:00
@ryanjmliao 不是不欢迎问基础问题,而是你问的都是网上拿关键词一搜就能找到答案的问题。如果不知道怎么搜,可以说清楚你卡在了什么地方,不然你这样原题直接丢上来就问,太伸手党了
|
15
hakono 2019-11-15 18:03:05 +08:00 via iPhone
@ryanjmliao 直接把原始文本都处理成大写或小写不就好了? 原始文本留一份,改在原始文本里
|
16
amet 2019-11-15 18:08:57 +08:00 1
@ryanjmliao 正文中全小写,句首大写,表达强烈情感全部大写。你这么问 jmc891205 我还以为自己对 " capitalized sensibly." 的理解出了偏差,还去查了遍词典。
|
17
superrichman 2019-11-15 18:16:08 +08:00 via iPhone
帮忙做 assignment 要收费的 /手动狗头
|
18
JerryCha 2019-11-15 18:21:33 +08:00 1
@ryanjmliao
善用 lower、upper,善用 title,善用网络资料查找自带方法。https://www.runoob.com/python3/python3-string.html 在国外念书你应该明白,上课讲了多少根本不是你会与不会的理由,上课讲的浅不代表对学生的要求低。 |
19
HHH01 OP @Yvette 我都有说明吧,比如第一题,我卡住的地方是大小写,在第二题,我也把自己的思路说出来,我是希望在这里求问,能获得点播,我从来没有伸手要答案,我也没这个习惯,你可以看看我之前发的求教帖子,我没要求大家都把答案贴出来,我都是希望大家给思路,一起讨论。
如果觉得这个帖子对你没有价值或者不想回答,大可以不必要回复,但是没必要冷嘲热讽。如果探讨题目本身那我很感谢。 |
20
HHH01 OP @JerryCha 不瞒你说,因为签证问题,我还没去上过一节课,也还没见到过同学,所以是在家看课件自学,我只能说你说的很对,感谢提醒。
|
22
HHH01 OP @InkStone 我想到的是用 index 定位到 minutes,然后再减去三位数,得到数值的定位,再取出数值,除以二
|
23
HHH01 OP 这是我想到的第一题,但好像不完美,特别是,当时间为三位数比如 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 |
24
hellwys1 2019-11-15 19:47:40 +08:00
如果没有怎么接触过编程,是可以理解的。
建议学习如何使用谷歌,自己去找,比在论坛问舒服多了。 |
25
ErrorMan 2019-11-15 19:53:17 +08:00
同楼上各位的想法,问题太过基础,靠搜索引擎就可以解决,而不应该花费更多时间来发帖问。
没有思路的话建议先去找些简单题做,基础不牢问到了题目也没有好效果,不可能每一题你都能来问。 另外在家复习自学并不是什么理由,网上能拿到的课程资源内容没那么系统,但是对你的基础状况来说已经非常够用了。 如果不知道去哪里的话,这里指路 coursera 和 中国 mooc |
27
AlisaDestiny 2019-11-15 20:22:01 +08:00
|
28
xiaoming1992 2019-11-15 20:34:50 +08:00 via Android 1
把大问题切分成小问题一步一步来,比方说第一题,可以先写一个函数,分析一个单词是 UPPERCASE、lowercase 还是 Pascalcase,再写一个函数将小写单词转成相应的单词,再写个函数作正则替换,这个题目不就解决了吗
|
29
HHH01 OP @xiaoming1992 感谢,我已经解决了
|