码迷,mamicode.com
首页 > 其他好文 > 详细

来自百度贴吧的练习题 :求最长单词的长度和最短单词的长度。

时间:2014-11-06 21:30:17      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   for   sp   div   on   log   cti   

  In the function ex5 write code that will input a line of text, split it into words, and display these 
words one per line, and also print the length of the longest and shortest words. You should 
regard any sequence of consecutive non-space characters as a single word; there may be more 
than one space between adjacent words in the input line, but the output must not contain any 
blank lines.
大体意思是输入一行英文,分解这句话,逐行打印每个单词,并且比较每个单词的大小,输出最长和最短的单词的长度。

 

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-
line = "In the function ex5 write code that will input a line of text, split it into words, and display these words one per line, and also print the length of the longest and shortest words"
char_split = line.split()
lens = [None,0]
for chr in char_split:
    chr_len = len(chr)
    print chr_len,chr
    if lens[0] == None:
        lens[0] = chr_len
    if chr_len < lens[0]:
        lens[0] = chr_len
        
    if chr_len > lens[1]:
        lens[1] = chr_len

print "the longest word‘s lenth is %d,the shortest word‘s lenth is %d." % (lens[1],lens[0])

 

英语比较差,所以最后的英语输出部分,请自行忽略。

来自百度贴吧的练习题 :求最长单词的长度和最短单词的长度。

标签:blog   io   ar   for   sp   div   on   log   cti   

原文地址:http://www.cnblogs.com/sageskr/p/4079828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!