Activity - Practice with Chapter 4 and 5

Create a program called files-practice.py. Your code will accomplish the following tasks.

Pig Latin

  1. Write a function called pig_latin_word(word) that takes a single word as a string, then converts the word to Pig Latin and returns it. Recall the basic rules of Pig Latin: take the first letter of a word, put it at the end, then append “ay”. The only exception is if the first letter is a vowel, in which case we keep it as is and append “hay” to the end. For example, “boot” in Pig Latin is “ootbay”, and “image” is “imagehay”. It will be helpful to consider a container of vowels so that you may quickly check if a letter vowel. Additionally, you may use the slice operator to grab only parts of a string.

  2. Write a function called pig_latin(line) that takes an entire line of text as a string (including several words), and converts the text to Pig Latin. Hint: Separate the line into words, and call your previous function for each word to build up a string that is returned.

  3. Download the file info.txt, which has a few lines of text. Save it in the same directory as your python file. Write code to read each line of the file, convert each line to pig latin, and write the resulting pig-latin version of the text to the shell.

Chess

  1. Write a function called make_chess_board(n) that takes an integer n as an argument, which is the size of the n by n board. The function should construct and print the contents of the board to a file, created in Python, called board.txt. For example, calling the function to create a 3 by 3 board would appear as follows in the text file:
a1 b1 c1
a2 b2 c2
a3 b3 c3

Calling the function to create a 5 by 5 board would appear as follows in the text file:

a1 b1 c1 d1 e1
a2 b2 c2 d2 e2
a3 b3 c3 d3 e3
a4 b4 c4 d4 e4
a5 b5 c5 d5 e5

You may assume that the board will be no larger than 26 by 26.

Hint: Consider a nested for loop, where the outer loop iterates over numbers, and the inner loop iterates over letters. Construct each ‘cell’ as a string combination of letter and number to write to the file.

If you finish early

  1. Add code to encrypt info.txt using the caeser cipher, and save the encrypted text in encrypted-info.txt. Test that it worked by adding code to decrypt encrypted-info.txt using the caesar cipher, and outputing the contents to the shell.

  2. Add some additional Pig Latin rules to your converter. For example, words that start with “th”, “st”, “qu”, “pl” or “tr” should move both of those letters to the end. For example, “stop” is “opstay”, and “there” is “erethay” in Pig Latin. There are many other Pig Latin rules that you can find online if you want a true converter.

How to submit

Make sure you saved your work and that it runs without errors. Submit your python file to Moodle.