V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
HHH01
V2EX  ›  Python

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

  •  
  •   HHH01 · 2019-11-15 16:05:40 +08:00 · 3398 次点击
    这是一个创建于 1617 天前的主题,其中的信息可能已经有所发展或是发生改变。

    又遇到难题了,人不在学校还没法跟同学们一块讨论,只能求助 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 条附言  ·  2019-11-15 20:33:14 +08:00
    如果有人比较忙不愿意回答简单问题,我的建议是,可以不回复,或者 pass。
    有些问题对有经验的人来说可能很简单,但对初学者可能不容易,初衷只是来此地学习求教,但不需要各位提供出题目以外的帮助,感谢。
    InkStone
        1
    InkStone  
       2019-11-15 16:18:12 +08:00   ❤️ 1
    我不是很明白你究竟卡在哪里了……
    第一道题不就是替换一下字符串吗,N minutes 可以用正则匹配。第二道题你的思路肯定是可行的……不过最好还是学习一下正统的排序算法。
    cigarzh
        2
    cigarzh  
       2019-11-15 16:27:12 +08:00   ❤️ 1
    基础太差,建议复习
    HHH01
        3
    HHH01  
    OP
       2019-11-15 16:27:42 +08:00
    @InkStone 啥是正则匹配?
    HHH01
        4
    HHH01  
    OP
       2019-11-15 16:32:35 +08:00
    @InkStone 大小写的问题不是很明白,比如我一个词中某一个字母大写
    @cigarzh 谢谢,刚开始学习,总共才上了 5 节课,当然这不是借口,确实基础差。
    jmc891205
        5
    jmc891205  
       2019-11-15 17:15:12 +08:00
    "You can assume the original review is capitalized sensibly."
    cherbim
        6
    cherbim  
       2019-11-15 17:25:01 +08:00
    基础太差,建议留级
    scukmh
        7
    scukmh  
       2019-11-15 17:27:30 +08:00
    基础太差,建议复读
    rioshikelong121
        8
    rioshikelong121  
       2019-11-15 17:28:32 +08:00
    基础太差,不如。。
    InkStone
        9
    InkStone  
       2019-11-15 17:29:15 +08:00
    @ryanjmliao

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

    在国外念书你应该明白,上课讲了多少根本不是你会与不会的理由,上课讲的浅不代表对学生的要求低。
    HHH01
        19
    HHH01  
    OP
       2019-11-15 18:26:58 +08:00
    @Yvette 我都有说明吧,比如第一题,我卡住的地方是大小写,在第二题,我也把自己的思路说出来,我是希望在这里求问,能获得点播,我从来没有伸手要答案,我也没这个习惯,你可以看看我之前发的求教帖子,我没要求大家都把答案贴出来,我都是希望大家给思路,一起讨论。
    如果觉得这个帖子对你没有价值或者不想回答,大可以不必要回复,但是没必要冷嘲热讽。如果探讨题目本身那我很感谢。
    HHH01
        20
    HHH01  
    OP
       2019-11-15 18:42:07 +08:00
    @JerryCha 不瞒你说,因为签证问题,我还没去上过一节课,也还没见到过同学,所以是在家看课件自学,我只能说你说的很对,感谢提醒。
    HHH01
        21
    HHH01  
    OP
       2019-11-15 19:08:28 +08:00
    @InkStone 还没学到,,,
    HHH01
        22
    HHH01  
    OP
       2019-11-15 19:10:19 +08:00
    @InkStone 我想到的是用 index 定位到 minutes,然后再减去三位数,得到数值的定位,再取出数值,除以二
    HHH01
        23
    HHH01  
    OP
       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
        24
    hellwys1  
       2019-11-15 19:47:40 +08:00
    如果没有怎么接触过编程,是可以理解的。
    建议学习如何使用谷歌,自己去找,比在论坛问舒服多了。
    ErrorMan
        25
    ErrorMan  
       2019-11-15 19:53:17 +08:00
    同楼上各位的想法,问题太过基础,靠搜索引擎就可以解决,而不应该花费更多时间来发帖问。
    没有思路的话建议先去找些简单题做,基础不牢问到了题目也没有好效果,不可能每一题你都能来问。
    另外在家复习自学并不是什么理由,网上能拿到的课程资源内容没那么系统,但是对你的基础状况来说已经非常够用了。
    如果不知道去哪里的话,这里指路 coursera 和 中国 mooc
    HHH01
        26
    HHH01  
    OP
       2019-11-15 20:19:26 +08:00
    @hellwys1 是这样,求人不如求己。发现这个论坛不太适合非程序员。
    AlisaDestiny
        27
    AlisaDestiny  
       2019-11-15 20:22:01 +08:00
    xiaoming1992
        28
    xiaoming1992  
       2019-11-15 20:34:50 +08:00 via Android   ❤️ 1
    把大问题切分成小问题一步一步来,比方说第一题,可以先写一个函数,分析一个单词是 UPPERCASE、lowercase 还是 Pascalcase,再写一个函数将小写单词转成相应的单词,再写个函数作正则替换,这个题目不就解决了吗
    HHH01
        29
    HHH01  
    OP
       2019-11-15 22:36:23 +08:00
    @xiaoming1992 感谢,我已经解决了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   919 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:10 · PVG 06:10 · LAX 15:10 · JFK 18:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.