ISF DP Computer Science

Substitution Ciphers #

In this lab you will be introduced to functions by constructing multiple substitution ciphers.


Syllabus Topics [SL] #

  • B2.3.1 Construct programs that implement the correct sequence of code instructions to meet program objectives.
  • B2.3.4 Construct functions and modularization.

Key Vocabulary #

WordDefinition
FunctionA reusuable block of code that takes input and gives an output.
Parameter/ArgumentThe input value(s) of a function. There can be many parameters.
Return valueThe value that a function outputs. There can only be one output.
ModularizationThe process of dividing a larger task into smaller parts that work independently.
ScopeWhere a variable can be accessed in the code.
Local ScopeA variable defined inside a function, loop, or condition that may only be used within that indented block.
Global ScopeA variable defined in the main body of a program, may be used thorughout the program.
Infinite LoopA loop that will never end.
CastingConvert a variable from one data type to another

[0] Set up #

πŸ’» Go to your dpcs folder and create a new folder for this unit.

cd ~/desktop/dpcs/
mkdir unit01_cryptography
cd unit01_cryptography

πŸ’» Clone your repo. This will copy it onto your computer. Be sure to replace yourGithubUsername with your actual username.

git clone https://github.com/isf-dp-cs/lab_substitution_ciphers_yourGithubUsername
πŸ’» In the Terminal, type the following command to open the lab folder.
cd lab_substitution_ciphers_yourGithubUsername

πŸ’» Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.

poetry shell
πŸ‘Ύ πŸ’¬ Exiting the poetry shell

When you want to exit the shell, you can type exit or ^D


[1] What is a function? #

A function is a reusable block of code. We often think of functions as taking an input of information, transforming the information, and returning an output of information.

In this example, the pluralize() function takes any string as an input, and returns the string in its pluralized form.

1
2
3
4
5
6
def pluralize(word):
    if word[-1] == 's' or word[-1] == 'ch' or word[-1] == 'sh':
        word = word + 'es'
    else:
        word = word + 's'
    return word
  • line 1 defines the function with an argument/parameter
  • line 2-5 contains a conditional to check the final letters of the noun. Most English nouns are pluralized by adding β€œs” (β€œtree” becomes β€œtrees”). But nouns ending in β€œs”, β€œch”, or β€œsh” are pluralized by adding β€œes” (β€œbeach” becomes β€œbeaches”).
  • line 6 returns the pluralized word

To use this function you must call it. Functions make it really easy to reuse code, becuase you can them an unlimited amount of times.

1
2
pluralize('apple')  # returns 'apples'
pluralize('beach')  # returns 'beaches'

To store the return value of the function:

1
2
plural_apple = pluralize('apple')  # stores 'apples'
pluaral_beach = pluralize('beach')  # stores 'beaches'
πŸ‘Ύ πŸ’¬ Function Practice

πŸ’» You can practic writing functions at codingbat.com


[2] Substitution Cipher #

A cipher is an algorithm for disguising a message. In this lab you will write a series of substitution ciphers to disguise a message.

πŸ’» Open substitution_ciphers.py


reverse_message() #

πŸ’» Construct the code for the reverse_message() function. You may not use .reverse().

  • inputs - original_message (str)
  • output - returns the new message reversed

πŸ’» Test your function at the bottom of the file. Be sure to test multiple cases.

if __name__ == "__main__":
    reversed_message = reverse_message('hello world')
    print(reversed_message)
    # OUPUT: dlrow olleh

πŸ’» Once you have it working, decrypt the secret messages.


character_to_integer() #

πŸ’» Construct the code for the character_to_integer() function.

  • inputs - original_message (str), character (str), integer(int)
  • output - returns the new message with all instances of the character, replaced by the integer

πŸ’» Test your function at the bottom of the file. Be sure to test multiple cases.

if __name__ == "__main__":
    replace_p = character_to_integer('apple','p',5)
    print(replace_p)
    # OUPUT: a55le

πŸ’» Once you have it working, decrypt the secret message.


find_replace() #

πŸ’» Construct the code for the find_replace() function.

  • inputs - original_message (str), find_test (str), replace_text (int)
  • output - returns a new message with all instances of the find_test, replaced by the replace_text

πŸ’» Test your function at the bottom of the file. Be sure to test multiple cases.

if __name__ == "__main__":
    replace_p = character_to_integer('apple','p',5)
    print(replace_p)
    # OUPUT: a55le

πŸ’» Once you have it working, decrypt the secret message.


[3] Deliverables #

⚑✨ Once you complete the lab, be sure to complete these two steps:

✏️ Go to your Syllabus Content Checklist in your Google Drive and update it accordingly.

πŸ’» Push your work to Github

  • git status
  • git add -A
  • git status
  • git commit -m "describe your code here"
  • git push
  • remote


[4] Extensions #

reverse_each_word() #

πŸ’» Inextension_ciphers.py, construct the code for the reverse_each_word() function. Be sure to consider:

  • what existing function can you utilize?
  • what should it input and output?

πŸ’» Test your function at the bottom of the file. Be sure to test multiple cases.

if __name__ == "__main__":
    reversed_message = reverse_each_word('hello world')
    print(reversed_message)
    # OUPUT: olleh dlrow

string_to_integer() #

πŸ’» Inextension_ciphers.py, construct the code for the string_to_integer() function. It will transform each character to its coresponding index in the English alphabet. Be sure to consider:

  • what should it input and output

πŸ’» Test your function at the bottom of the file. Be sure to test multiple cases.

if __name__ == "__main__":
    converted_message = string_to_integer('abcd')
    print(converted_message)
    # OUPUT: 0123

    converted_message2 = string_to_integer('hello')
    print(converted_message)
    # OUPUT: 74111114