def substitute(text, replace):
    return ' '.join([replace.get(word, word) for word in text.split()])

print('Løsning 1')

def rewrite(text, initial, hide, upper):
    rules_hide = {word: '*' * len(word) for word in hide}
    rules_initial = {word: word[0] + '*' * (len(word) - 1) for word in initial}
    rules_upper = {word: word.upper() for word in upper}

    print(f'{rules_hide=}, {rules_initial=}, {rules_upper=}')

    rules = rules_hide | rules_initial | rules_upper

    new_text = substitute(text, rules)
    return new_text


text = 'Python rocks but Java sucks'

print(rewrite(text, initial=['sucks'], hide=['Java'], upper=['Python']))

print('Løsning 2')

def rewrite(text, initial=(), hide=(), upper=()):
    rules = {word: '*' * len(word) for word in hide}
    rules |= {word: word[0] + '*' * (len(word) - 1) for word in initial}
    rules |= {word: word.upper() for word in upper}

    return substitute(text, rules)

text = 'Python rocks but Java sucks'

#print(rewrite(text, initial=['sucks'], hide=['Java'], upper=['Python']))
print(rewrite(text, initial=['sucks'], upper=['Python']))

def eliminate(text, forbidden):
    return rewrite(text, hide=forbidden)

print(eliminate(
    'Python Java and C are popular languages but Java and C '
    'are statically typed whereas Python is dynamically typed', 
    ['Java', 'C', 'statically']
))
