# Prompts the user for a word and outputs the list of
# all subwords of the word of height 1.
#
# Written by Eric Martin for COMP9021


def extract_subwords(word):
    word = word.replace(' ', '')
    subwords = []
    subword = ''
    token = ''
    for c in word:
        if c == ',':
            if subword:
                subword += token + ', '
            token = ''
        elif c == '(':
            subword = token + '('
            token = ''
        elif c == ')':
            if subword:
                subwords.append(subword + token + ')')
                subword = ''
                token = ''
        else:
            token += c
    return subwords

word = input('Enter a word: ')
print('The subwords of "{:}" of height 1 are:\n    {:}'.
                                        format(word, extract_subwords(word)))

Resource created Wednesday 26 August 2015, 11:58:49 PM.

file: quiz_3_sol.py


Back to top

COMP9021 15s2 (Principles of Programming) is powered by WebCMS3
CRICOS Provider No. 00098G