In 1863 — in the midst of the American Civil War — soldiers of the northern states (the Union) seized a bag of correspondence from rebels of the southern states (the Confederation) as it was about to cross Lake Pontchartrain. In one of the letters, a woman named Anna boasted of a trick she had played on a Boston newspaper. She had sent them a poem titled "The Gypsy's Wassail", which she assured them was written in Sanskrit:
Dro lseta red efnoc Evarbruossel, Bdog!
Dra, Geruaeb, Dnaht, ims Nosnhojeel!
Eojn Osnhojd, Nahti, msn Oskcajp Leh,
Ho eixi dnis tifm eh Tevig, ot!
The paper published this "beautiful and patriotic poem, by our talented contributor". A few days later a reader discovered the trick — it was simply English written in reverse:
God bless our brave Confederates, Lord!
Lee, Johnson, Smith, and Beauregard!
Help Jackson, Smith, and Johnson Joe,
To give them fits in Dixie, oh!
She had signed her name only "Anna", but in the same bag they found a letter from her sister to her husband, saying "Anna writes you one of her amusing letters", and this contained her signature and address. In his memoirs, Union general Wickham Hoffman recalled that he wrote her a letter back:
I told her that her letter had fallen into the hands of one of those ‘Yankee’ officers whom she saw fit to abuse, and who was so pleased with its wit that he should take great pleasure in forwarding it to its destination. That in return he had only to ask that when the author of ‘The Gypsy’s Wassail’ favored the expectant world with another poem, he might be honored with an early copy. Anna must have been rather surprised.
A sentence is reversed by putting all of its letters in reverse order. However, the original use of uppercase and lowercase letters is retained: if a position in the sentence was originally occupied by an uppercase letter then the letter that is put at that position during the reversal must be converted into uppercase, and similar for lowercase letters. All characters that are no letter remain just in place when reversing.
In the above schematic we show how the first sentence in the original text of the poem "The Gypsy's Wassail" is reversed. We have marked the positions with uppercase letters in orange, the positions with lowercase letters in blue and the positions where no letter occurs in green. Thin green arrows indicate where characters that are no letter remain in place. The thick blue arrows indicate that, for example, the first and the last letter of the sentence change positions when reversing the sentence. The fact that each position retains its color when reversing indicates that the positions of uppercase and lowercase letters are retained.
Note that the entire procedure is reversible: if a reversed sentence is reversed once more, the original sentence is obtained. Your task:
Write a function reverse that takes a sentence (str). The function must return the reversed sentence (str).
Write a function codec that takes the location (str) of a text file. The function must output the content of the text file with each individual line reversed. The function also has a second optional parameter that may take the location (str) of a text file. If a second argument is passed to the function, the function should not output the result but write it to a file at the given location.
Write a function filecomp that takes the locations (str) of two text files. The function must return a Boolean value (bool) that indicates whether the two files have exactly the same content.
In the following interactive session we assume the text file plaintext.txt1 to be located in the current directory.
>>> invert('God bless our brave Confederates, Lord!')
'Dro lseta red efnoc Evarbruossel, Bdog!'
>>> invert('Dro lseta red efnoc Evarbruossel, Bdog!')
'God bless our brave Confederates, Lord!'
>>> invert('Lee, Johnson, Smith, and Beauregard!')
'Dra, Geruaeb, Dnaht, ims Nosnhojeel!'
>>> invert('Help Jackson, Smith, and Johnson Joe,')
'Eojn Osnhojd, Nahti, msn Oskcajp Leh,'
>>> invert('To give them fits in Dixie, oh!')
'Ho eixi dnis tifm eh Tevig, ot!'
>>> codec('plaintext.txt2')
Dro lseta red efnoc Evarbruossel, Bdog!
Dra, Geruaeb, Dnaht, ims Nosnhojeel!
Eojn Osnhojd, Nahti, msn Oskcajp Leh,
Ho eixi dnis tifm eh Tevig, ot!
>>> codec('plaintext.txt3', 'ciphertext.txt4')
>>> codec('ciphertext.txt5')
God bless our brave Confederates, Lord!
Lee, Johnson, Smith, and Beauregard!
Help Jackson, Smith, and Johnson Joe,
To give them fits in Dixie, oh!
>>> codec('ciphertext.txt6', 'plaintext2.txt7')
>>> filecomp('plaintext.txt8', 'ciphertext.txt9')
False
>>> filecomp('plaintext.txt10', 'plaintext2.txt11')
True