andrew-s
7/7/2016 - 3:44 AM

four_letters_in_common.py

def four_letters_in_common(word,a_dictionary):
        count=0
        word=word.upper()
        word_length = len(word)
        if word_length<4:            
            return count
        elif word_length ==4:            
            for entry in a_dictionary:
                entry=entry.upper()
                if entry.find(word)!= -1:
                    count+=1                                  
            return count
        else:
            for entry in a_dictionary:
                entry=entry.upper()
                length = len(entry)
                if length<4:                    
                    pass                
                else:
                    first_position =0
                    last_position =4
                    finalsub=word[-4:-1]
                    current_substring=word[first_position:last_position]
                    while True:                        
                        if entry.find(current_substring)!= -1:
                            count+=1                            
                            break
                        else:
                            first_position +=1
                            last_position +=1                            
                            if last_position>word_length:                                
                                break
                            else:                                
                                current_substring=word[first_position:last_position]
            return count