Character trigrams X41790


Statement
 

pdf   zip

thehtml

A character trigram is a group of three consecutive characters as they appear in a word. For example, the word "hello" contains the trigrams "hel", "ell", and "llo".

  1. Write a function that receives a word and returns a list of its character trigrams.
    >>> trigrams('hello')
    ['hel', 'ell', 'llo']
    >>> trigrams('crocodile')
    ['cro', 'roc', 'oco', 'cod', 'odi', 'dil', 'ile']
    >>> trigrams('hi')
    []
    
  2. Write a program that reads a sequence of words and prints the pairs of consecutive words in the sequence that have at least one trigram in common. The program must use the trigrams function defined above.

    Hint: It may be useful to define another function that checks whether two lists of trigrams have at least a common element

Input The input is a sequence of words, all of them contain only lowercase letters.

Output The output is a list of word pairs that are consecutive in the input sequence and have some common trigram.

Public test cases
  • Input

    bye hello belly mainly finland

    Output

    hello belly
    mainly finland
    
  • Input

    hello hi nope uhm uh belly bye coconut juice 

    Output

    
            
                                
  • Input

    hello
    

    Output

    
            
                                
  • Input

    see you later crocodile coconut orinoco megarin nutmeg
    

    Output

    crocodile coconut
    coconut orinoco
    orinoco megarin
    megarin nutmeg
    
  • Information
    Author
    Language
    English
    Official solutions
    Python
    User solutions
    Python