def read_file(path):
    ''' Given the file path (file name) of a plain text file, returns
    the content of the file as a string. '''
    with open(path, 'rt', encoding='utf-8') as f:
        return f.read()
    

def is_legal(word, required_char, available_chars):
    ''' Return True if word is legal in word knot, 
    False otherwise.
    
    Words are legal if:
     - the word has at least 4 characters, and
     - the required character is included in the word, and
     - all characters in the word are in the list of available characters
    '''
    
    # Regler i ordknute:
    # -- Lovlige ord må inneholde S
    # -- Lovlige ord må ha minst 4 tegn
    # -- Lovlige ord inneholder kun tegn fra TYFGSMI

    if required_char not in word:
        return False
    if len(word) < 4:
        return False
    
    for character in word:
        if character not in available_chars:
            return False
    
    return True
    

if __name__ == '__main__':
    word_list_string = read_file('nsf2022.txt')
    all_words = word_list_string.split('\n')

    legal_words = []
    for word in all_words:
        if is_legal(word, 's', 'tyfgsmi'):
            legal_words.append(word)

    print(legal_words)
