Lab Exam I (October 15, 2025)

solution

Piphilogy

Piphilology comprises the creation and use of mnemonic techniques to remember many digits of the mathematical constant π. The word is a play on the word "pi" itself and of the linguistic field of philology.

There are many ways to memorize π, including the use of piems, which are poems that represent π in a way such that the length of each word (in letters) represents a digit. Since it will not be possible to represent the digit 0 using this scheme, we will use a word of length 10 to represent 0. To make it easier to write piems, we shall extend this scheme to words of more than 10 letters as well by using a word of length 11 to represent 1, word of length 12 to represent 2, etc. Note that with this scheme we have multiple notations for digits. Hint: Use the % operation to determine digit from word.

Here is an example of a piem:

Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.

Write a Python program that reads the name of a folder from command line. The folder contains many files, each containing a poem that may or may not be a piem. The program should determine if the file contains a piem or not and prints the name of the file and a message indicating whether the file contains a piem or not. In case the file does not contain a piem, the program should also print the digits of the piem and the digits of π.

Few Hints:

Here is the code to find the names of files within the folder.

  import glob
  fnames = sorted(glob.glob(sys.argv[1] + "/*"))

Since the poems may contain special symbols such as commas, periods, exclamation etc., we should remove these symbols from the text of the poem before we determine whether it is a piem or not. The following code uses a package called re and a method to remove special characters and replace with the space character. The single quote used in words such as don't will be deleted and not replaced by space.

  import re

  with open(fname) as f:
    text = f.read().strip()
  text = re.sub("[^A-Za-z']", " ", text.strip())
  text = re.sub("'", "", text.strip())

The correct digits of π can be obtained by calling the following function.

def get_pi_digits():
  return "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989"

You should download files.zip, which contains all the files needed to complete the exam.

Sample Run:

$ python3 Piem.py piems

piems/p01.txt VALID PIEM!!

piems/p02.txt VALID PIEM!!

piems/p03.txt VALID PIEM!!

piems/p04.txt VALID PIEM!!

piems/p05.txt VALID PIEM!!

piems/p06.txt VALID PIEM!!

piems/p07.txt INVALID PIEM!!
piem_digits = 31415926535897932384626433832795728
pi_digits   = 31415926535897932384626433832795028

piems/p08.txt VALID PIEM!!

piems/p09.txt VALID PIEM!!

piems/p10.txt VALID PIEM!!

piems/p11.txt VALID PIEM!!
$ 

Lab Exam Policy

This is a closed-book exam! You may only have your cheat sheet and your IDE open. You may not use an internet browser to research the solution. There is only one programming question. Please read the question carefully. You will have 1 hour and 40 minutes to complete this exam. Note: Please leave time to submit your code using the following command on the tinman server:
sudo handin1301 e1 piem.py
To transfer the file to tinman, you may use FileZilla OR the following procedure: Open a Terminal/Powershell Window. cd into the folder containing piem.py. Then, type the following command (replace userid with your campusid):
sftp userid@tinman.cs.gsu.edu
you will be asked for your password; type it in (you may have to say "yes" to a question before password). once you see the sftp prompt, transfer the file as follows:
put piem.py
Then make sure you exit the sftp program:
exit