Exercise: Week 2-7

Posted petitherisson

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Exercise: Week 2-7相关的知识,希望对你有一定的参考价值。

 File artical.txt contains an article in English (please create the file by yourself and put any English article you picked in it).

Assume the article only contains ‘,’, ‘.’, ‘!’, ‘?’ and ‘...’ as punctuations, make a program to find out the longest word and print it.

artical.txt

Last week, thousands of student protesters streamed into Hong Kong Polytechnic University and occupied the campus as the citys violent political unrest reached fever pitch.

Once inside, they soon faced an impossible choice: stay inside until supplies run out, or leave the university and risk getting tear gassed and arrested for rioting, a charge which can fetch a 10-year prison sentence.
Police accused protesters of turning PolyU and other universities into "weapons factories" that "look like military training grounds" and surrounded the campus.
Police have been on the edge of the campus for more than two days, firing round after round of tear gas at protesters who responded with makeshift petrol bombs, catapults and bows and arrows.

 

with open(artical.txt, r) as f:
    text = f.read()
    text = text.replace(
,  )
    l = text.split( )
str = ‘‘
for item in l:
    for c in item:
        if c.isalpha() != True:
            item = item.replace(c, ‘‘)
    if len(item) > len(str):
        str = item
print(l)
print(str)

 

2019-11-20

16:50:16

以上是关于Exercise: Week 2-7的主要内容,如果未能解决你的问题,请参考以下文章

Week 5: Object Oriented Programming 9. Classes and Inheritance Exercise: int set

week3-栈和队列

week03-栈和队列

数字的总和和乘积Javascript

Ray Tracing The Next Week 超详解 光线追踪2-7 任意长方体 && 场景案例

在嵌套for循环内,打印字符串“。”并在满足条件时将其替换为其他字符。 MOOC Java Week 10'Dungeon'