【问题标题】:exercise 9.3 in How To Think Like A Computer Scientist如何像计算机科学家一样思考练习 9.3
【发布时间】:2014-03-18 23:51:57
【问题描述】:

练习 9.3。编写一个名为 Avoids 的函数,它接受一个单词和一串禁用字母,如果单词不使用任何禁用字母,则返回 True。 修改你的程序,提示用户输入一串禁止使用的字母,然后打印不包含这些字母的单词数。你能找到排除最少单词的 5 个禁止字母的组合吗? 拿到第一部分:

def avoids(word,forb):
    for letter in forb:
        if letter in word:
        return False
    return True

与第二个作斗争,这是我的尝试:

fin=open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 2.7\words.txt','r')

def no_contain():
   forb=raw_input('enter the forbidden letters')
   tot=0
   ct=0
   for line in fin:
       word=line.strip()
       for word in fin:
           tot+=1
           for letter in forb:
               if letter in word:
               ct+=1
       return tot-ct

得到一些有趣的答案。另外,什么时候应该使用ct=0...ct+=1 技术而不是ct=ct+1

【问题讨论】:

    标签: python string list search


    【解决方案1】:

    试试这个:

    def avoids(word, forb):
        for letter in forb:
            if letter in word:
                return False
        return True
    
    def no_contain(fin):
        forb = raw_input('enter the forbidden letters: ')
        count = 0
        for word in fin:
            if avoids(word.strip(), forb):
                count += 1
        return count
    
    if __name__ == '__main__':
        fin = open('words.txt', 'r')
        print no_contain(fin)
    

    何时使用ct = ct + 1 而不是ct = ct + 1

    其实它们在python中是一样的,都是对ct + 1的结果求值然后绑定到ct,因为int在python中的类是不可变的。

    【讨论】:

    • noooice!有效。如何在短时间内成为蟒妖?
    • 我也是python的新手,只是之前有一些C的经验。所以我的程序风格更像C,不是面向对象的;如果python是你的第一语言,借一本经典书籍系统地学习python,然后看看其他优秀程序员的程序,了解他们是如何思考的,他们是如何实现的,自己尝试一些项目,你无法成为编程专家短时间内,就有了 Peter Norvig 的经典article。希望对你有所帮助。
    【解决方案2】:

    ''' 练习 9.3。编写一个名为 Avoids 的函数,它接受一个单词和一串禁止的字母, 如果单词不使用任何禁止的字母,则返回 True。

    修改你的程序,提示用户输入一串禁止使用的字母,然后打印 不包含其中任何一个的单词的数量。你能找到5个禁止字母的组合吗 排除最少的单词? '''

    def avoids(strg, word):
        for letter in strg:
            if letter in word:
                return False;
        return True;
    
    fin = open('words.txt')
    def No_Contain(strg):
        count_word_no_contain = 0;
        for line in fin:
            result = avoids(strg, line.strip());
            if(result == True):
                count_word_no_contain += 1;
                print(line.strip());
        return count_word_no_contain;
    
    print(avoids("HUY STRAUSS", "LION"));
    strg = "lion king";
    print("The number of words doesn’t have the letters of the string", strg, "is: ", No_Contain(strg));
    

    ''' 结果: 没有字符串狮子王字母的单词数是:9971 '''

    【讨论】:

      【解决方案3】:
      forbidden=input("Enter the forbidden characters: ")  
      fin=open("words.txt")  
      
      def avoids():    
          for letter in fin:    # Searches the letter in the file
              word=letter.strip()    
              if forbidden not in word:    #If forbidden char is not in the file
                  print(word)    
      print(avoids())  
      

      【讨论】:

        猜你喜欢
        • 2013-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-25
        • 1970-01-01
        • 2013-05-01
        • 1970-01-01
        相关资源
        最近更新 更多