Now that we are writing functions, make sure your work today (and all future assignments) follows our class Python style guidelines and you include function comments. Be careful of your indentation!
Create a function named in_both
which takes two strings, string1
and string2
as parameters and returns a string containing letters which occur in both strings. The returned string should NOT have repeating letters. If no letters are shared by the two strings, then an empty string is printed (''
).
In the main function, call in_both
and print the result to test it works. Use the following example function calls and verify them against the example output below.
Main Function Calls:
in_both("art", "ate")
in_both("hello", "world")
in_both("hello", "zap")
Expected Output (respectively):
at
lo
Create a function named get_first_half
that returns the first half of a string. The function should take one string parameter named string_value
and return a new string made up of the first half of string_value
. HINT You might need to do something special if the length of the string is odd
In the main function, call get_first_half
and print the results to test it works. Use the following example function calls and verify them against the example output below.
Main Function Calls:
get_first_half('a')
get_first_half('ab')
get_first_half('abc')
Example Printed Output (respectively):
a
a
ab
You will create a function named decode
that can decode a secret string (similar to Chapter 3.2.5 in your book). Your function will take two string parameters secret_word
and key
and will return a new string with the decoded message.
NOTE: If secret_word
contains spaces DO NOT try to decode them, simply add them to the decoded message.
In the main function, call decode
and print the results to test it works. Use the following example function calls and verify them against the example output below.
Main Function Calls:
decode('ylkku aujkw', 'hfxwlgcyrbzkdsunpjoetvaqmi')
Example Printed Output (respectively):
hello world
You will create a function named encode
to encode normal strings into a secret message. Your function will take two string parameters phrase
and key
. The function will return the encoded string. NOTE: If phrase
contains spaces DO NOT try to encode them, simply add them to the decoded message.
In the main function, call encode
and print the results to test it works. Use the following example function calls and verify them against the example output below.
Main Function Calls:
encode('computerscience', 'hfxwlgcyrbzkdsunpjoetvaqmi')
encode('hello world','hfxwlgcyrbzkdsunpjoetvaqmi')
Example Printed Output (respectively):
xudnteljoxrlsxl
ylkku aujkw