Course of Raku / Essentials / Associative data types / Exercises

Replace with antonyms

Problem

Create a program that replaces all the words from the command line with their antonyms. If the word is missing in the dictionary, do not modify it and print as-is.

Use a hard-coded hash to keep the dictionary of antonyms. For your convenience, here is a prepared list of about 200 pairs that you can directly copy and paste into your program: dictionary.raku. All the pairs are kept in both directions, e.g.:

my %dictionary =
    'above' => 'below',
    'absent' => 'present',
    # . . .
    'below' => 'above',
    # . . .
    'present' => 'absent',
    # . . .
;

Make the program as simple as possible and print the words line by line.

Example

Run the program and pass a few words to it. Make sure that at least some of the words can be found in the dictionary.

$ raku replace-with-antonyms.raku a quiet teacher wants to buy some salt
a
noisy
student
wants
to
sell
some
sugar

Solution

See the solution

Next exercise

💪 Purchase table

Course navigation

Positional data types / Subscripting ranges   |   Creating and calling functions

Translations of this page: EnglishDeutschEspañolItalianoLatviešuNederlandsБългарскиРусскийУкраїнська