标签:UI ble function not append python meta ict message
会贴出原题和答案,答案不是最优的,也反映了我的学习过程,如果有时间会更新优化的代码。
#Write a function filter_long_words() that takes a list of words # and an integer n and returns the list of words that are longer than n. def filter_long_words(list,n): words = [] for word in list: if len(word)>n: words.append(word) return (words) print(filter_long_words([‘abc‘,‘long‘,‘filter‘,‘return‘],4))
#Write a version of a palindrome recognizer that also accepts phrase palindromes # such as "Go hang a salami I‘m a lasagna hog.", "Was it a rat I saw?", "Step on no pets", # "Sit on a potato pan, Otis", "Lisa Bonet ate no basil", "Satan, oscillate my metallic sonatas", # "I roamed under it as a tired nude Maori", "Rise to vote sir", or the exclamation "Dammit, I‘m mad!". # Note that punctuation, capitalization, and spacing are usually ignored. import re def palindrome_recognizer(words): album = [] word=re.sub(‘[^a-zA-Z]‘,‘‘,words).lower() for str in (word): album.insert(0, str) if word==(‘‘.join(album)): return True else: return False a=palindrome_recognizer("I am testing") print(a) b=palindrome_recognizer("Go hang a salami I‘m a lasagna hog.") print(b) c=palindrome_recognizer("Was it a rat I saw?") print(c) d=palindrome_recognizer("Sit on a potato pan, Otis") print(d) e=palindrome_recognizer("Dammit, I‘m mad!") print(e)
#A pangram is a sentence that contains all the letters of the English alphabet at least once, # for example: The quick brown fox jumps over the lazy dog. # Your task here is to write a function to check a sentence to see if it is a pangram or not. import re def pangram_check(sentence): alphabets=[‘a‘, ‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘,‘k‘,‘l‘,‘m‘,‘n‘,‘o‘,‘p‘,‘q‘,‘r‘,‘s‘,‘t‘,‘u‘,‘v‘,‘w‘,‘x‘,‘y‘,‘z‘] words = re.sub(‘[^a-zA-Z]‘, ‘‘, sentence).lower() i=0 for alphabet in alphabets: if alphabet in words: i=i+1 if i==26: return True else: return False a="The quick brown fox jumps over the lazy dog." print(pangram_check(a)) b="The quick brown fox jumps over the lazy cat." print(pangram_check(b))
#"99 Bottles of Beer" is a traditional song in the United States and Canada. # It is popular to sing on long trips, # as it has a very repetitive format which is easy to memorize, # and can take a long time to sing. The song‘s simple lyrics are as follows: # 99 bottles of beer on the wall, 99 bottles of beer. #Take one down, pass it around, 98 bottles of beer on the wall. #The same verse is repeated, each time with one fewer bottle. # The song is completed when the singer or singers reach zero. #Your task here is write a Python program capable of generating all the verses of the song. def Bottles_of_Beer(n): bottles=n while bottles >0: print(‘{0} bottles of beer on the wall,{0} bottles of beer‘.format(bottles)) print(‘Take one down,pass it around,{} bottles of beer on the wall‘.format(bottles-1)) bottles=bottles-1 Bottles_of_Beer(3)
#Represent a small bilingual lexicon as a Python dictionary in the following fashion # {"merry":"god", "christmas":"jul", "and":"och", "happy":gott", "new":"nytt", "year":"ar"} # and use it to translate your Christmas cards from English into Swedish. # That is, write a function translate() that takes a list of English words # and returns a list of Swedish words. def translate(English_words): dictionary={"merry":"god","christmas":"jul","and":"och","happy":"gott","new":"nytt","year":"ar"} Swedish=[] for words in English_words.lower().split(): if words in dictionary: Swedish.append(dictionary[words]) return(‘ ‘.join(Swedish)) a=‘Merry christmas‘ print(translate(a)) b=‘HAPPY NEW YEAR‘ print(translate(b)) c=‘Merry Christmas and Happy New Year‘ print(translate(c))
#Write a function char_freq() that takes a string and builds a frequency # listing of the characters contained in it. # Represent the frequency listing as a Python dictionary. # Try it with something like char_freq("abbabcbdbabdbdbabababcbcbab"). def char_freq(char): alphabets=set(char) for alphabet in alphabets: counts_dict = {alphabet:char.count(alphabet)} print (‘{}:{}‘.format(alphabet,counts_dict[alphabet])) a="abbabcbdbabdbdbabababcbcbab" char_freq(a)
‘‘‘ In cryptography, a Caesar cipher is a very simple encryption techniques in which each letter in the plain text is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it to communicate with his generals. ROT-13 ("rotate by 13 places") is a widely used example of a Caesar cipher where the shift is 13. In Python, the key for ROT-13 may be represented by means of the following dictionary: key = {‘a‘:‘n‘, ‘b‘:‘o‘, ‘c‘:‘p‘, ‘d‘:‘q‘, ‘e‘:‘r‘, ‘f‘:‘s‘, ‘g‘:‘t‘, ‘h‘:‘u‘, ‘i‘:‘v‘, ‘j‘:‘w‘, ‘k‘:‘x‘, ‘l‘:‘y‘, ‘m‘:‘z‘, ‘n‘:‘a‘, ‘o‘:‘b‘, ‘p‘:‘c‘, ‘q‘:‘d‘, ‘r‘:‘e‘, ‘s‘:‘f‘, ‘t‘:‘g‘, ‘u‘:‘h‘, ‘v‘:‘i‘, ‘w‘:‘j‘, ‘x‘:‘k‘, ‘y‘:‘l‘, ‘z‘:‘m‘, ‘A‘:‘N‘, ‘B‘:‘O‘, ‘C‘:‘P‘, ‘D‘:‘Q‘, ‘E‘:‘R‘, ‘F‘:‘S‘, ‘G‘:‘T‘, ‘H‘:‘U‘, ‘I‘:‘V‘, ‘J‘:‘W‘, ‘K‘:‘X‘, ‘L‘:‘Y‘, ‘M‘:‘Z‘, ‘N‘:‘A‘, ‘O‘:‘B‘, ‘P‘:‘C‘, ‘Q‘:‘D‘, ‘R‘:‘E‘, ‘S‘:‘F‘, ‘T‘:‘G‘, ‘U‘:‘H‘, ‘V‘:‘I‘, ‘W‘:‘J‘, ‘X‘:‘K‘, ‘Y‘:‘L‘, ‘Z‘:‘M‘} Your task in this exercise is to implement an encoder/decoder of ROT-13. Once you‘re done, you will be able to read the following secret message: Pnrfne pvcure? V zhpu cersre Pnrfne fnynq! Note that since English has 26 characters, your ROT-13 program will be able to both encode and decode texts written in English. ‘‘‘ import string def ROT_13(message): key = {‘a‘: ‘n‘, ‘b‘: ‘o‘, ‘c‘: ‘p‘, ‘d‘: ‘q‘, ‘e‘: ‘r‘, ‘f‘: ‘s‘, ‘g‘: ‘t‘, ‘h‘: ‘u‘, ‘i‘: ‘v‘, ‘j‘: ‘w‘, ‘k‘: ‘x‘, ‘l‘: ‘y‘, ‘m‘: ‘z‘, ‘n‘: ‘a‘, ‘o‘: ‘b‘, ‘p‘: ‘c‘, ‘q‘: ‘d‘, ‘r‘: ‘e‘, ‘s‘: ‘f‘, ‘t‘: ‘g‘, ‘u‘: ‘h‘, ‘v‘: ‘i‘, ‘w‘: ‘j‘, ‘x‘: ‘k‘, ‘y‘: ‘l‘, ‘z‘: ‘m‘, ‘A‘: ‘N‘, ‘B‘: ‘O‘, ‘C‘: ‘P‘, ‘D‘: ‘Q‘, ‘E‘: ‘R‘, ‘F‘: ‘S‘, ‘G‘: ‘T‘, ‘H‘: ‘U‘, ‘I‘: ‘V‘, ‘J‘: ‘W‘, ‘K‘: ‘X‘, ‘L‘: ‘Y‘, ‘M‘: ‘Z‘, ‘N‘: ‘A‘, ‘O‘: ‘B‘, ‘P‘: ‘C‘, ‘Q‘: ‘D‘, ‘R‘: ‘E‘, ‘S‘: ‘F‘, ‘T‘: ‘G‘, ‘U‘: ‘H‘, ‘V‘: ‘I‘, ‘W‘: ‘J‘, ‘X‘: ‘K‘, ‘Y‘: ‘L‘, ‘Z‘: ‘M‘} ROT_13_message=[] for words in message: if words ==" ": ROT_13_message.append(words) elif words in string.punctuation: ROT_13_message.append(words) else: ROT_13_message.append(key[words]) return(‘‘.join(ROT_13_message)) a=‘P?n‘ print(ROT_13(a)) b=‘Pnrfne pvcure? V zhpu cersre Pnrfne fnynq!‘ print(ROT_13(b))
46 Simple Python Exercises 16-22题
标签:UI ble function not append python meta ict message
原文地址:http://www.cnblogs.com/wangchao0203/p/6561961.html