1、FizzBuzz:编写一个程序 打印1-100的数字。碰到3的倍数时,打印"Fizz",碰到5的倍数时,打印“Buzz”. 如果是3和5的倍数,打印"FizzBuzz",其余则打印数字。
class FizzBuzz: def __init__(self): for i in range(1,101): if i%3==0: print("Fizz") elif i%5 ==0: print("Buzz") elif i%3==0 and i%5==0: print("FizzBuzz") else: print(i) FizzBuzz = FizzBuzz() FizzBuzz
2、顺序搜索
class SearchAlgorithm: def Sequential_search(self,num_list,n): for i in num_list: if i == n: return True break else: return False num_list = range(0,100) s1 = SearchAlgorithm() print(s1.Sequential_search(num_list,200))
3、回文词。
class Palindrome: def word(self,word): word = word.lower() if word[::-1]==word: return True else: return False word = Palindrome() print(word.word("mom"))
4、变位词
class Anagram: def word(self,word1,word2): word1 = word1.lower() word2 = word2.lower() if sorted(word1)==sorted(word2): return True else: return False word = Anagram() print(word.word("iceman", "cinema"))