20/06/2024

Week 2 Assignments

1. Clean format:

Input a line of strings of mixed cases, format each word (words are strings delimited by spaces) so that they are capitalised.  Output the formatted line. 

2. Stammer Bob: 

Bob stammers when facing long words.  He speaks all words with less than or equal to 5 letters fluently, but for each word with more than 5 letters, each extra letter causes him to repeat the word once more.  For example, when you say ‘Good Morning Bob’, he will have to reply ‘Good Morning Morning Morning George George’.  

Input a sentence, output how Bob actually pronounces it.

3. Preprocessing:

You work for a big data company.  They take vast number of texts from the media and perform data analysis.  Your job is to preprocess the text by attaching data points to the words so that they can be processed by the data scientists.  The rule is simple: for each input line, separate them into words (delimited by spaces), prefix each word with the sequence number of it in the line, and suffix it with the length of the word.  For example, for the input ‘giant leap for mankind’, you produce an output ‘0giant5 1leap4 2for3 3mankind7’.

Input a line, output the outcome of the preprocessing with the additional data points attached.

4. Lonely librarian:

You are the only librarian in the school library.  You get sick of keeping record of new books and writing labels, you decide to create some automation.  You want to keep entering names of new books, until at one point you enter ‘end’so the automation stops.  For each book entered, it’s added to your record, unless the same name already exists in the record.  When the automation stops, it prints a list of all the books entered.

Input a series of book names, output the list of books entered.

Extension: 

People make mistakes, a lonely librarian makes more.  When you discover a mistake, instead of entering the name of the book, you enter ‘del’ followed by the name of the book incorrectly entered.  The book is then removed from your list.  

Input a number of book names and delete commands, output the final list of books.

5. Letter from Moscow:

Your friend lives in Moscow.  He sends you email messages.  Because of the extreme cold, people have a different accent there.  They pause after each word, and at the end of each sentence they yell.  This even affects the way they write emails, for each word they write, they always add a comma after, and wherever we use a comma or full stop, they always use an ‘!’.  You may receive a message from Moscow saying ‘Good, day, mate! How, are, you! I, don’t, yell! I’m, just, friendly!’.

Input a line of message, output the result after translating to the Moscow accent.

6. The more the merrier:

You are organising 2 parties.  At one point it suddenly occurs to you: if people come to parties to have fun, why hold two when you can have everybody in one.  There’s twice the fun and you don’t have to work twice as hard.  You need to merge the 2 guest lists.  They are both sorted alphabetically.  You need a program to merge them so that the combined list is still sorted alphabetically.

Input 2 lines, each containing a list of names sorted alphabetically, output a combined list of names sorted alphabetically.

No comments: