Site icon Tutor Bin

University of Maryland Create a Simple Encoder Program Python Coding Task

University of Maryland Create a Simple Encoder Program Python Coding Task

Description

  create a simple encoder/decoder program for secret messages. The program has been broken into two parts. You can submit them both as one single file to Brightspace as the programs build upon each other.

Part 2a

Begin by writing the following functions using the IPO notation below. Test your code using the sample program below each function header.

Function #1

# function:   ascii_shift
# input:      a word to shift (String) and the direction to shift by (String, "up" or "down")
# processing: shifts each uppercase character in the supplied word to another position in the ASCII
#             table. the new position is dictated by the direction value.  for example,
#             if word = "APPLE" and direction = "up" the newly generated word would be:
#
#             BQQMF
#
#             because we added +1 to each character. if we were to call the function with
#             word = "BQQMF" and direction = "down" the newly generated word would be:
#           
#             APPLE
#
#             because we added -1 to each character, which shifted each character down by
#             one position on the ASCII table.
#
#             non-uppercase characters should be ignored.  for example, given the string
#             "APPLE pear peach 123" and direction of "up" the function should return:
#
#             BQQMF pear peach 123
#
#             shifting beyond the end of the range of the uppercase characters should result
#             in the letter cycling around to the other side of the alphabet. for example,
#             encoding "XYZ" up would result in "YZA".  likewise, shifting "ABC" down
#             would result in "ZAB".  
#
#             in the event that an empty string is supplied no shift will occur and an empty
#             string should be returned.
#
#             in the event that an invalid shift direction is supplied no shift will occur and
#             the original string should be returned.  valid directions are "up" and "down"
#             (case sensitive)
#
# output:     returns the newly generated word

Sample Function Calls

print( ascii_shift("apple", "up") ) # apple
print( ascii_shift("APPLE", "up") ) # BQQMF
print( ascii_shift("APPLE", "down") ) # ZOOKD
print( ascii_shift("APPLE PIE and ZEBRAS", "up") ) # BQQMF QJF and AFCSBT
print( ascii_shift("APPLE PIE and ZEBRAS", "down") ) # ZOOKD OHD and YDAQZR
print( ascii_shift("APPLE PIE and ZEBRAS", "pikachu") ) # APPLE PIE and ZEBRAS
print( ascii_shift("", "up") ) # (nothing prints, empty string)

Function #2

# function:   shift_right
# input:      a word to shift (String)
# processing: shifts all characters in the string to the right. the last character in the string
#             will be shifted to the beginning of the string.  for example:
#
#             apple -> eappl
#
#             in the event that an empty string is supplied no shift will occur and an empty
#             string should be returned
#
# output:     returns the newly generated word

Sample Function Calls

print(shift_right("apple")) # eappl
print(shift_right("hello, world!")) # !hello, world
print(shift_right("")) # (nothing prints, empty string)

Function #3

# function:   shift_left
# input:      a word to shift (String)
# processing: shifts all characters in the string to the left. the first character in the string
#             will be shifted to the end of the string.  for example:
#
#             apple -> pplea
#
#             in the event that an empty string is supplied no shift will occur and an empty
#             string should be returned
#
# output:     returns the newly generated word

Sample Function Calls

print(shift_left("apple")) # pplea
print(shift_left("hello, world!")) # ello, world!h
print(shift_left("")) # (nothing prints, empty string)

Function #4

# function:   flip
# input:      a word to flip (String)
# processing: flips the first half of the string with the second half of the string.
#             if the string has an even number of characters this function will work as follows:
#
#             ABCD -> CDAB
#
#             if the string has an odd number of characters this function will work as follows:
#
#             ABCDE -> DECAB
#
#             in the event that an empty string is supplied no flip will occur and an empty
#             string should be returned
#
# output:     returns the newly generated word

Sample Function Calls

print(flip("ABCDEFG")) # EFGDABC
print(flip("123456")) # 456123
print(flip("")) # (nothing prints, empty string)

Function #5

# function:   add_letters
# input:      a word to scramble (String) and a number of letters (integer)
#             you can always assume that the number of letters supplied is
#             a positive number.
# processing: adds a number of random letters (A-Z) after each letter
#             in the supplied word. for example, if word="CAT" and num=1
#             we could generate any of the following:
#             CZAQTR
#             CWARTS
#             CEAETT
#
#             if word="CAT" and num=2 we could generate any of the following:
#             CRTAHFTUI
#             CNNANYTJN
#             CZAAAITYM
#
#             if the word is an empty string simply return an empty string.
#
# output:     returns the newly generated word

Sample Program

# define original word
original = "HELLO!"

# loop to demonstrate the function
for num in range(1, 5):

   # scramble the word using 'num' extra characters
   scrambled = add_letters(original, num)

   # output
   print ("Adding", num, "random characters to", original, "->", scrambled)

Sample Output

Adding 1 random characters to HELLO! -> HQETLPLMOF!O
Adding 2 random characters to HELLO! -> HOYEPWLAALEHOKS!SE
Adding 3 random characters to HELLO! -> HSQKEFDBLHNSLKNVOPBI!UCQ
Adding 4 random characters to HELLO! -> HZZOLEMJMQLFHPHLRETEOGXNY!RPVP

Hint: you will need to use a loop to generate the required # of random characters, and you will need to use some random number functions as well. Think about the algorithm before you start coding! Draw out the steps you think you need to take on a piece of paper. For example: “Start with the first character in the source word. Then generate ‘num’ new random characters and concatente these characters. Then move onto the next character in the source word and repeat the process”.

Function #6

# function:   delete_characters
# input:      a word to analyze (String) and the number of characters to remove (integer)
#             you can always assume that the number of characters to remove will be a postive number.
# processing: the function starts at the first position in the supplied word and keeps it.
#             it then removes "num" characters from the word. the process is repeated again
#             if the word contains additional characters - the next character is kept
#             and "num" more characters are removed.  For example, if word="cZaYtU" and
#             num=1 the function will generate the following:
#        
#             cat (keeping character 0, removing character 1, keeping character 2, removing
#             character 3, keeping character 4, removing character 5)
#
# output:     returns the newly unscrambed word

Sample Program

word1 = "HdeulHlHom!t"
word2 = "HTLedklFNljioMH!bi"
word3 = "HHHZeZrflqSflzOiosNU!jBk"
word4 = "HFtRKeivFllRNlUlGTaooYwoH!JpXL"

unscrambled1 = delete_characters(word1, 1)
print ("Removing 1 character from", word1, "->", unscrambled1)

unscrambled2 = delete_characters(word2, 2)
print ("Removing 2 characters from", word2, "->", unscrambled2)

unscrambled3 = delete_characters(word3, 3)
print ("Removing 3 characters from", word3, "->", unscrambled3)

unscrambled4 = delete_characters(word4, 4)
print ("Removing 4 characters from", word4, "->", unscrambled4)

Sample Output

Removing 1 character from HdeulHlHom!t -> Hello!
Removing 2 characters from HTLedklFNljioMH!bi -> Hello!
Removing 3 characters from HHHZeZrflqSflzOiosNU!jBk -> Hello!
Removing 4 characters from HFtRKeivFllRNlUlGTaooYwoH!JpXL -> Hello!

Hint: String slicing may make your life a lot easier when writing this function!

Part 2a is an intermediate step towards the final version of this program. You do not need to turn in this part of the program – keep working and turn in part 2b when you are finished. You only need to turn in part 2b.

Part 2b

Now you are going to write an “encoder / decoder” program that makes use of your six cryptographic functions. Begin by writing a program that continually asks the user to enter in an encoding pattern and a word to encode. The user should also be able to end the program using a sentinel value.

If the user chooses to encode a word you should do the following:

  • Ask the user for an “encoding pattern” string. This string will contain instructions on how to encode / decode a string. The valid commands that this string can contain are the following (case insensitive – either upper or lowercase letters should be accepted):
    • ‘A’ = add 1 random character after each character (i.e. cat -> cZaZtZ)
    • ‘X’ = delete 1 character after each character (i.e. cZaZtZ -> cat)
    • ‘F’ = flip the string
    • ‘U’ = ASCII shift all characters by +1
    • ‘D’ = ASCII shift all characters by -1
    • ‘L’ = Shift all characters to the left by 1
    • ‘R’ = Shift all characters to the right by 1
  • Next, ask them to enter in a phrase that they want to encode. Convert this all alphabetic characters in this string to uppercase.
  • Finally, apply the desired encoding pattern to their string, giving them feedback during the process. Apply the pattern one operation at a time, from left to right. Any invalid or unrecognized commands should be ignored and reported to the user.
  • Hint: use your functions!
  • The sentinel value should be case insensitive (i.e. any case variation of ‘end’ should be accepted)

Here are a few sample runnings of the program:

Enter an encoding pattern, 'end' to end: AUF
Enter a word to encode/decode: APPLE
* Adding 1 letter between all characters: AKPJPILDEM
* ASCII shifting up: BLQKQJMEFN
* Flipping: JMEFNBLQKQ
Final encoding / decoding: JMEFNBLQKQ

Enter an encoding pattern, 'end' to end: FDX
Enter a word to encode/decode: JMEFNBLQKQ
* Flipping: BLQKQJMEFN
* ASCII shifting down: AKPJPILDEM
* Deleting 1 character: APPLE
Final encoding / decoding: APPLE

Enter an encoding pattern, 'end' to end: auf
Enter a word to encode/decode: apple
* Adding 1 letter between all characters: AXPFPRLGES
* ASCII shifting up: BYQGQSMHFT
* Flipping: SMHFTBYQGQ
Final encoding / decoding: SMHFTBYQGQ

Enter an encoding pattern, 'end' to end: fdx
Enter a word to encode/decode: SMHFTBYQGQ
* Flipping: BYQGQSMHFT
* ASCII shifting down: AXPFPRLGES
* Deleting 1 character: APPLE
Final encoding / decoding: APPLE

Enter an encoding pattern, 'end' to end: LLLAAADDD
Enter a word to encode/decode: this is a secret message
* Shifting left: HIS IS A SECRET MESSAGET
* Shifting left: IS IS A SECRET MESSAGETH
* Shifting left: S IS A SECRET MESSAGETHI
* Adding 1 letter between all characters: SZ OIFSK WAB YSDEOCMRREWTJ IMMEQSBSDADGJEATXHHIL
* Adding 1 letter between all characters: SZZW EOVIYFVSIKT KWIAXBJ NYASDDWEZOCCHMDRNRREEWGTLJG NIWMIMMEKQJSOBGSNDZAVDPGCJDEWAETUXRHIHJIRLY
* Adding 1 letter between all characters: SCZLZUWL IEXOYVZIYYLFGVPSPIVKUTX OKBWLIVAAXSBQJL ENPYSAWSRDADXWCEKZDOFCICSHQMNDORJNWRQRSEVEUWAGLTBLKJZGC XNBIGWQMMIRMZMPEGKXQTJUSFONBGGTSANFDCZPAEVDDWPCGTCSJEDGEYWSAYEDTIUAXVRYHXIBHQJMIBRWLUYD
* ASCII shifting down: RBYKYTVK HDWNXUYHXXKEFUOROHUJTSW NJAVKHUZZWRAPIK DMOXRZVRQCZCWVBDJYCNEBHBRGPLMCNQIMVQPQRDUDTVZFKSAKJIYFB WMAHFVPLLHQLYLODFJWPSITRENMAFFSRZMECBYOZDUCCVOBFSBRIDCFDXVRZXDCSHTZWUQXGWHAGPILHAQVKTXC
* ASCII shifting down: QAXJXSUJ GCVMWTXGWWJDETNQNGTISRV MIZUJGTYYVQZOHJ CLNWQYUQPBYBVUACIXBMDAGAQFOKLBMPHLUPOPQCTCSUYEJRZJIHXEA VLZGEUOKKGPKXKNCEIVORHSQDMLZEERQYLDBAXNYCTBBUNAERAQHCBECWUQYWCBRGSYVTPWFVGZFOHKGZPUJSWB
* ASCII shifting down: PZWIWRTI FBULVSWFVVICDSMPMFSHRQU LHYTIFSXXUPYNGI BKMVPXTPOAXAUTZBHWALCZFZPENJKALOGKTONOPBSBRTXDIQYIHGWDZ UKYFDTNJJFOJWJMBDHUNQGRPCLKYDDQPXKCAZWMXBSAATMZDQZPGBADBVTPXVBAQFRXUSOVEUFYENGJFYOTIRVA
Final encoding / decoding: PZWIWRTI FBULVSWFVVICDSMPMFSHRQU LHYTIFSXXUPYNGI BKMVPXTPOAXAUTZBHWALCZFZPENJKALOGKTONOPBSBRTXDIQYIHGWDZ UKYFDTNJJFOJWJMBDHUNQGRPCLKYDDQPXKCAZWMXBSAATMZDQZPGBADBVTPXVBAQFRXUSOVEUFYENGJFYOTIRVA

Enter an encoding pattern, 'end' to end: UUUXXXRRR
Enter a word to encode/decode: PZWIWRTI FBULVSWFVVICDSMPMFSHRQU LHYTIFSXXUPYNGI BKMVPXTPOAXAUTZBHWALCZFZPENJKALOGKTONOPBSBRTXDIQYIHGWDZ UKYFDTNJJFOJWJMBDHUNQGRPCLKYDDQPXKCAZWMXBSAATMZDQZPGBADBVTPXVBAQFRXUSOVEUFYENGJFYOTIRVA
* ASCII shifting up: QAXJXSUJ GCVMWTXGWWJDETNQNGTISRV MIZUJGTYYVQZOHJ CLNWQYUQPBYBVUACIXBMDAGAQFOKLBMPHLUPOPQCTCSUYEJRZJIHXEA VLZGEUOKKGPKXKNCEIVORHSQDMLZEERQYLDBAXNYCTBBUNAERAQHCBECWUQYWCBRGSYVTPWFVGZFOHKGZPUJSWB
* ASCII shifting up: RBYKYTVK HDWNXUYHXXKEFUOROHUJTSW NJAVKHUZZWRAPIK DMOXRZVRQCZCWVBDJYCNEBHBRGPLMCNQIMVQPQRDUDTVZFKSAKJIYFB WMAHFVPLLHQLYLODFJWPSITRENMAFFSRZMECBYOZDUCCVOBFSBRIDCFDXVRZXDCSHTZWUQXGWHAGPILHAQVKTXC
* ASCII shifting up: SCZLZUWL IEXOYVZIYYLFGVPSPIVKUTX OKBWLIVAAXSBQJL ENPYSAWSRDADXWCEKZDOFCICSHQMNDORJNWRQRSEVEUWAGLTBLKJZGC XNBIGWQMMIRMZMPEGKXQTJUSFONBGGTSANFDCZPAEVDDWPCGTCSJEDGEYWSAYEDTIUAXVRYHXIBHQJMIBRWLUYD
* Deleting 1 character: SZZW EOVIYFVSIKT KWIAXBJ NYASDDWEZOCCHMDRNRREEWGTLJG NIWMIMMEKQJSOBGSNDZAVDPGCJDEWAETUXRHIHJIRLY
* Deleting 1 character: SZ OIFSK WAB YSDEOCMRREWTJ IMMEQSBSDADGJEATXHHIL
* Deleting 1 character: S IS A SECRET MESSAGETHI
* Shifting right: IS IS A SECRET MESSAGETH
* Shifting right: HIS IS A SECRET MESSAGET
* Shifting right: THIS IS A SECRET MESSAGE
Final encoding / decoding: THIS IS A SECRET MESSAGE

Enter an encoding pattern, 'end' to end: RRRFAUU
Enter a word to encode/decode: hello, world
* Shifting right: DHELLO, WORL
* Shifting right: LDHELLO, WOR
* Shifting right: RLDHELLO, WO
* Flipping: LO, WORLDHEL
* Adding 1 letter between all characters: LAOA,T YWTOXROLDDMHPEZLE
* ASCII shifting up: MBPB,U ZXUPYSPMEENIQFAMF
* ASCII shifting up: NCQC,V AYVQZTQNFFOJRGBNG
Final encoding / decoding: NCQC,V AYVQZTQNFFOJRGBNG

Enter an encoding pattern, 'end' to end: DDXFLLL
Enter a word to encode/decode: NCQC,V AYVQZTQNFFOJRGBNG
* ASCII shifting down: MBPB,U ZXUPYSPMEENIQFAMF
* ASCII shifting down: LAOA,T YWTOXROLDDMHPEZLE
* Deleting 1 character: LO, WORLDHEL
* Flipping: RLDHELLO, WO
* Shifting left: LDHELLO, WOR
* Shifting left: DHELLO, WORL
* Shifting left: HELLO, WORLD
Final encoding / decoding: HELLO, WORLD

Enter an encoding pattern, 'end' to end: pikachu
Enter a word to encode/decode: hello
* 'P' is an invalid command, ignoring
* 'I' is an invalid command, ignoring
* 'K' is an invalid command, ignoring
* Adding 1 letter between all characters: HLEBLYLJOW
* 'C' is an invalid command, ignoring
* 'H' is an invalid command, ignoring
* ASCII shifting up: IMFCMZMKPX
Final encoding / decoding: IMFCMZMKPX

Enter an encoding pattern, 'end' to end: end

Part 2c (extra credit)

Write the following functions to add some additional features to your cryptography program:

# function:   add_padding
# input:      a string to pad and the amount of padding to add (integer).  you can
#             always assume that the amount of padding to add will be a positive integer.
# processing: adds a random sequence of uppercase characters to the beginning and
#             end of the string. for example, if the function is called with
#             the string = 'apple' and amount = 5 the result could be:
#
#             XYZPQappleWRUTS
#
# output:     returns the newly constructed string
# SAMPLE CODE
print( add_padding('apple', 5) ) # DNNRXappleCDIVK
print( add_padding('pear', 3) ) # RYCpearJEE
print( add_padding('', 4) ) # PEJKQRUC
print( add_padding('courant', 10) ) # BTXOVLESFNcourantHHNDWAXDKN
# function:   remove_padding
# input:      a string to pad and the amount of padding to remove.  you can always
#             assume that the amount of padding to remove will be a positive integer.
# processing: removes a certain number of characters from the beginning and
#             end of the string. for example, if the function is called with
#             the string = 'XYZPQappleWRUTS' and amount = 5 the result would be:
#
#             apple
#
# output:     returns the newly constructed string
# SAMPLE CODE
print( remove_padding('DNNRXappleCDIVK', 5) ) # apple
print( remove_padding('RYCpearJEE', 3) ) # pear
print( remove_padding('PEJKQRUC', 4) ) # (empty string is returned)
print( remove_padding('BTXOVLESFNcourantHHNDWAXDKN', 10) ) # courant
# function:   swap_characters
# input:      a string
# processing: swaps all characters that occupy even indexes with their neighbor
#             to the right.  for example:
#
#             apples -> palpse
#
#             if a string has an odd number of characters then the last character
#             will not swap.  for example:
#
#             apple -> palpe
#
# output:     returns the newly constructed string
# SAMPLE CODE
print( swap_characters('apples') ) # palpse
print( swap_characters('apple') ) # palpe
print( swap_characters('XY') ) # YX
print( swap_characters('X') ) # X
print( swap_characters('') ) # (empty string is returned)
# function:   vowel_cycle
# input:      a word to shift (String) and the direction to shift by (String, "up" or "down")
#             you can always assume that a direction value of "up" or "down" is supplied to the function.
# processing: shifts all uppercase vowel characters ("A", "E", "I", "O", "U") with the
#             next vowel in the sequence.  for example, cycling the string "APPLEJUICE"
#             with a direction of "up" would result in the following:
#
#             APPLEJUICE -> EPPLIJAOCI
#
#             cycling the string "APPLEJUICE" with a direction of "down" would result in
#             the following:
#
#             APPLEJUICE -> UPPLAJOECA             
#
# output:     returns the newly constructed string
# SAMPLE CODE
print( vowel_cycle('APPLEJUICE', 'up') ) # EPPLIJAOCI
print( vowel_cycle('APPLEJUICE', 'down') ) # UPPLAJOECA
print( vowel_cycle('applejuice', 'down') ) # applejuice
print( vowel_cycle('', 'up') ) # (empty string is returned)

Once you are happy with your functions you should update your main program so that these newly written features can be used during the encryption / decryption process. The new commands your program should recognize are as follows:

  • ‘P’ = Padding the word with 1 letter on each side
  • ‘Q’ = Remove padding of 1 letter on each side
  • ‘S’ = Swap characters
  • ‘V’ = Vowel cycle up
  • ‘W’ = Vowel cycle down

In addition, your program should produce a “reversal pattern” string after each encryption. This string should contain the instructions necessary to reverse the encryption process. For example, the reverse of ‘AAAUUUF’ (add, add, add, up, up, up, flip) would be ‘FDDDXXX’ (flip, down, down, down, remove, remove, remove). Here’s a sample running of the program. Note that this extra credit program is not available in the automated online program tester.

Enter an encoding pattern, 'end' to end: PPPSVVV
Enter a word to encode/decode: APPLE
* Padding with 1 letter: VAPPLER
* Padding with 1 letter: XVAPPLERE
* Padding with 1 letter: OXVAPPLERES
* Swapping characters: XOAVPPELERS
* Cycling vowels up: XUEVPPILIRS
* Cycling vowels up: XAIVPPOLORS
* Cycling vowels up: XEOVPPULURS
Final encoding / decoding: XEOVPPULURS
Reversal pattern: WWWSQQQ

Enter an encoding pattern, 'end' to end: WWWSQQQ
Enter a word to encode/decode: XEOVPPULURS
* Cycling vowels down: XAIVPPOLORS
* Cycling vowels down: XUEVPPILIRS
* Cycling vowels down: XOAVPPELERS
* Swapping characters: OXVAPPLERES
* Removing padding of 1 letter: XVAPPLERE
* Removing padding of 1 letter: VAPPLER
* Removing padding of 1 letter: APPLE
Final encoding / decoding: APPLE
Reversal pattern: PPPSVVV

Enter an encoding pattern, 'end' to end: ALPUUVVV
Enter a word to encode/decode: PIKACHU
* Adding 1 letter between all characters: PGIGKCAKCKHRUA
* Shifting left: GIGKCAKCKHRUAP
* Padding with 1 letter: XGIGKCAKCKHRUAPJ
* ASCII shifting up: YHJHLDBLDLISVBQK
* ASCII shifting up: ZIKIMECMEMJTWCRL
* Cycling vowels up: ZOKOMICMIMJTWCRL
* Cycling vowels up: ZUKUMOCMOMJTWCRL
* Cycling vowels up: ZAKAMUCMUMJTWCRL
Final encoding / decoding: ZAKAMUCMUMJTWCRL
Reversal pattern: WWWDDQRX

Enter an encoding pattern, 'end' to end: WWWDDQRX
Enter a word to encode/decode: ZAKAMUCMUMJTWCRL
* Cycling vowels down: ZUKUMOCMOMJTWCRL
* Cycling vowels down: ZOKOMICMIMJTWCRL
* Cycling vowels down: ZIKIMECMEMJTWCRL
* ASCII shifting down: YHJHLDBLDLISVBQK
* ASCII shifting down: XGIGKCAKCKHRUAPJ
* Removing padding of 1 letter: GIGKCAKCKHRUAP
* Shifting right: PGIGKCAKCKHRUA
* Deleting 1 character: PIKACHU
Final encoding / decoding: PIKACHU
Reversal pattern: ALPUUVVV

Enter an encoding pattern, 'end' to end: end

Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."

Exit mobile version