首页 > 代码库 > [Python]不要问我为什么这么写。。。只能拿到零分!

[Python]不要问我为什么这么写。。。只能拿到零分!

 


来源:http://bbs.fishc.com/thread-57396-1-1.html

要求:

------------------------激动人心的分割线----------------------

这次的规则很长,请耐心看完。

你需要完成一系列的字符串处理。


单词翻转。
"Hello" → "olleH".
大小写交换。
"AsD" → "aSd".
数字统一换成9-原数字
"140" → "859" (9-1=8, 9-4=5, 9-0=9).
以及将下列的转换:
"," → "."
"!" → "?"
"(" → ")"
"{" → "}"
"[" → "]"
"<" → ">"
"@" → "#"
";" → ":"
多个空格变成一个。
"      " → " ".

重要: 不超过12行,以及每行不超过80个字符。

特殊: 除标准库以外的模块不可用。注明2.X与3.X. 否则你懂得~。


排名: 符合以上所有要求,少>效率。(最后一波看字符了。)

 


马丹,我写了108 行。。

技术分享
  1 def sepWord_reverse(var_str1):  2     var_str1 = var_str1 + "_"  3     str_len = len(var_str1)  4     var_str2 = ""  5     word_st = 0  6     word_end = 0  7     seek_status = 0    ## 0表示找寻下一个起始位置 1表示正在寻找下一个起始位置 2表示找到一个单词  8     temp_new = ""  9     index = 0 10     while index < str_len: 11         temp_new = var_str1[index] 12         if(seek_status == 0): 13             if((a<=temp_new<=z) or (A<=temp_new<=Z)):    ##找到单词起始 14                  word_st = index 15                  seek_status = 1 16             else:                    ##未找到单词起始 17                 seek_status = 0 18                 var_str2 += temp_new     19             index = index + 1 20             continue 21         elif(seek_status == 1): 22             if((a<=temp_new<=z) or (A<=temp_new<=Z)):  ##说明这个单词还没结束 23                 seek_status = 1 24                 index = index + 1 25             else: 26                 seek_status = 2 27                 word_end = index 28                  29             continue 30         elif(seek_status == 2): 31             temp_str = var_str1[word_st:word_end] 32             var_str2 += temp_str[::-1] 33             var_str2 += var_str1[index] 34             index = index + 1 35             seek_status = 0 36             continue 37          38     var_str2 = var_str2[0:str_len-1] 39     return var_str2 40      41 def twist(var_str): 42     var_str = sepWord_reverse(var_str)             #只反转单词 43     var_str = "_" + var_str 44     str_len = len(var_str) 45     var_ret = "" 46     for index in range(str_len): 47         temp = var_str[index] 48         if (A <= temp <= Z):         #大写变小写  49             var_ret += temp.lower() 50         elif(a <= temp <= z):        #小写变大写 51             var_ret += temp.upper() 52         elif(0 <= temp <= 9):        #数字统一换成9-原数字 53             var_ret += str(9-int(temp)) 54         elif(, == temp):                #"," → "." 55             var_ret += "." 56         elif(. == temp):                #"." → "," 57             var_ret += "," 58         elif(! == temp):                #"!" → "?" 59             var_ret += "?" 60         elif(? == temp):                #"?" → "!" 61             var_ret += "!" 62         elif(( == temp):                #"(" → ")" 63             var_ret += ")" 64         elif() == temp):                #")" → "(" 65             var_ret += "(" 66         elif({ == temp):                #"{" → "}" 67             var_ret += "}" 68         elif(} == temp):                #"}" → "{" 69             var_ret += "{" 70         elif([ == temp):                #"[" → "]" 71             var_ret += "]" 72         elif(] == temp):                #"]" → "[" 73             var_ret += "["             74         elif(< == temp):                #"<" → ">" 75             var_ret += ">" 76         elif(> == temp):                #">" → "<" 77             var_ret += "<" 78         elif(@ == temp):                #"@" → "#" 79             var_ret += "#" 80         elif(# == temp):                #"#" → "@" 81             var_ret += "@" 82         elif(; == temp):                #";" → ":" 83             var_ret += ":" 84         elif(: == temp):                #";" → ":" 85             var_ret += ";" 86         elif(  == temp): 87             if( != var_str[index-1] ): #前一个不是空格 88                 var_ret += " " 89         else: 90             var_ret += temp 91                                                  92     return var_ret[1:] 93      94 str1 = "Hello World!"  #正确 95 print(" + str1 + "  --->  " + twist(str1) + ") 96 str1 = "I`m 1st" 97 print(" + str1 + "  --->  " + twist(str1) + ") 98 str1 = "How are you? 905th." 99 print(" + str1 + "  --->  " + twist(str1) + ")100 str1 = "The code - ([{<;#>}])"101 print(" + str1 + "  --->  " + twist(str1) + ")102 str1 = "EMAIL        a@b.ru"103 print(" + str1 + "  --->  " + twist(str1) + ")104 str1 = ";-) 0_0 @__@"105 print(" + str1 + "  --->  " + twist(str1) + ")106 str1 = "      "107 print(" + str1 + "  --->  " + twist(str1) + ")108 ##################109 input("输入任意键退出")110 #list_ori = [‘,‘ , ‘!‘,‘(‘,‘{‘,‘[‘,‘<‘,‘;‘]
twist.py

 

 技术分享


 

[Python]不要问我为什么这么写。。。只能拿到零分!