Question: I need the python code please. 4.32 LAB: Magic String The code template below defines a function contains_magic() that has two arguments: the first, my_string,
I need the python code please.


4.32 LAB: Magic String The code template below defines a function contains_magic() that has two arguments: the first, my_string, is a string literal of unknown length, and the second, magic, is a three-character string. The function returns one of four possible values: 3, if magic is a substring of my_string: 2, if any two-character substring of magic is also a substring of my_string but magic is not. 1, if any of the characters in magic appears in my_string, but no two character substrings nor magic are substrings of my_string 0, if no characters in magic appear at all my_string. For example, suppose my_string is 'The main architect was Slartibartfast.'. Then if magic is 'xjb', contains_magic() returns 1 since there is only one 'b' but no 'x' and no 'j'. If magic is 'brx', contains_magic() returns 0 because, although 'b' and 'r' each appear, they do not appear in sequence together as 'br'. If magic is 'ibx', contains_magic() returns 2 since 'ib' appears in the string. If magic is 'arc', contains_magic() would return 3. Test yourself: what if magic were 'art'? (3) Complete the definition of contains_magic() below. In the main block, test your program with any values of my_string and magic you like. Unit tests will be run on your contains_magic() function, so it should work no matter what values my_string and magic have. You may assume magic always has only three unique characters (that need not be letters). You should assume case sensitivity, that is, for example, a and A are not the same letter. Some hints: 1. The expression magic in my_string returns True if the string literal labeled with the name magic is a substring of the string literal labeled with the name my_string. 2. magic[0] returns the oth (that is, the first) character in magic. magic[1] returns the 1st (that is, the second) character in magic, and so on. 3. magic[0:2] returns a string built from the oth and 1st characters of magic magic[1:3] contains the 1st and 2nd only. 289182.1640238 LAB ACTIVITY 4.32.1: LAB: Magic String 0/3 main.py Load default template... 1 def contains_magic (my_string, magic): 2 "Your solution goes here ! return my_result 5 if 7 8 9 name __main__': "Feel free to change the values of my_string and magic "" my_string = 'This example comes with the template. You can change it to whatever you like.' magic = 'xam print('contains_magic returns', contains_magic(my_string, magic)) Develop mode Submit mode Run your program as often as you'd like, before submitting for grading. Below, type any needed input values in the first box, then click Run program and observe the program's output in the second box. Enter program input (optional) If your code requires input values, provide them here. Run program Input (from above) main.py (Your program) Output (shown below)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
