Search
In practice 03, you will create a single Python file main.py. In this file you will write (or complete) the following functions, each of which solves a particular problem:
main.py
task01(strings)
task02(s)
task03(s)
task04(s)
task05(s)
task06(s, alphabet)
Both filename and names of the functions have to be exactly the same as described.
When submitting your solution:
You can obtain maximum of 2 points.
In starter code which is provided bellow each function contains docstring with short desription. Docstrings also contain examples of usage. Those examples can be used for testing your functions using doctest module. To run those tests you can simply run the main module as main script.
main
Below is a basic code structure you can use. Copy-paste the following code and complete each function (task01, task02, task03, …) according to description in the function docstring. Then submit your final main.py.
task01
task02
task03
def task01(strings): """Splits each string in the list of strings into halves and constructs two new strings. String A consists of first halves of all strings in the input list. String B consists of second halves of all strings in the input list. The order of halves in A and B corresponds to the order of their respective strings in the input list. If a string has an odd length, its first half includes the middle character and the second half starts immediately after. Function returns tuple containing strings A and B. :param strings: list of strings :return: tuple containing two strings Example: >>> strings = ['bcdef', 'gk', 'lmnoprs'] >>> task01(strings) ('bcdglmno', 'efkprs') """ pass def task02(s): """ Function counts number of substrings found in input string which contain exactly one symbol 0 and exactly one symbol 1. If two identical substrings occur at different positions in the original string, each of these substrings is counted separately. :param s: string :return: (integer) number of substrings which contain exactly one symbol 0 and exactly one symbol 1 Examples: >>> task02('n0v10se11g1c01') 13 >>> task02('010100100010111011010') 14 """ pass def task03(s): """ Identifies and removes all constant substrings from the given string in repeated steps. A substring S1 of string S is considered a constant substring if it satisfies all of the following conditions: 1. The length of S1 is greater than 1. 2. All characters in S1 are identical. 3. The character immediately before S1 is either non-existent (S1 appears at the beginning of S) or different from the characters in S1. 4. The character immediately after S1 is either non-existent (S1 appears at the end of S) or different from the characters in S1. The function removes all constant substrings from S in repeated steps: - In each step, all constant substrings present in S at that moment are removed. - This process continues until S contains no constant substrings. :param s: string which might contain constant substrings :return: string after removing all constant substrings Examples: >>> task03('abcxxcbdyyyzzzzfgzzgf') 'ad' >>> task03('xxyzzyzzyxx') '' """ pass def task04(s): """ Returns list of longest substrings found in given string s which contain only one word 'May' If multiple substrings share the same maximum length all of them will be in the output list and the order will be the same as the order they appear in s from left to right. In case that word 'May' does not appear in s at all, return empty list. :param s: string :return: list of strings Examples: >>> task04('MayMayzzAprilzzMayFebruaryJanuaryMayzz') ['ayzzAprilzzMayFebruaryJanuaryMa'] >>> task04('MayMayMayMay') ['ayMayMa', 'ayMayMa'] >>> task04('xxMayzzMayttMayuuMaywwMayrr') ['ayzzMayttMa', 'ayttMayuuMa', 'ayuuMaywwMa'] >>> task04('helloWorld') [] """ pass def task05(s): """ Returns list of longest substrings found in given string s which contain only one word 'May' and do not contain name of any other month of the year. Valid names of the months are: January, February, March, April, May, June, July, August, September, October, November, December They are considered only in this format, with the capital first letter. If multiple substrings share the same maximum length all of them will be in the output list and the order will be the same as the order they appear in s from left to right. In case that there is no substring meeting the described criteria, return empty list. :param s: string :return: list of strings Examples: >>> task05('MayMayzzAprilzzMayFebruaryJanuaryMayzz') ['prilzzMayFebruar'] >>> task05('xJuneMayJulyMaybeAugustMayMayMaybeJanuaryx') ['ulyMaybeAugus', 'ayMaybeJanuar'] >>> task05('helloWorld') [] """ pass def task06(s, alphabet): """Encrypts string s using rot13 alghoritm Rot13 alghoritm swap letters from the beginning of the alphabet with the letters at the end of the alphabet. Example: The aplhabet contains 26 characters. If we divide the alphabet into two halves, we get: 'abcdefghijklm' 'nopqrstuvwxyz' First letter in first half of the alphabet will be swapped with first letter in second half and so on. For example 'a' will be replaced with 'n' and vice versa. Encoding is the same as decoding. There has to be even number of letters in used alphabet. Rises an exception if number of characters in alphabet is odd. :param s: string to be encrypted :param alphabet: string containing all possible characters that may appear in s :return: encrypted string Examples: >>> task06('hello', 'abcdefghijklmnopqrstuvwxyz') 'uryyb' >>> task06('1s5cs9gjrql', '0123456789abcdefghijklmnopqrstuvwxyz') 'january1983' >>> task06('2025', '3658921470') '3934' """ pass if __name__ == '__main__': import doctest doctest.testmod(verbose=True)