LAB RESTRICTIONS, PLEASE READ: Do not add any imports, the ones that you need will be given to you. You may not use any lists or list methods, or any while loops. Within your loops, you MUST NOT use any break or continue statements. Please also do not use try-except statements, you should be able to anticipate or prevent any errors from happening at all!
def loopy_madness(string1: str, string2: str) -> str: """ Given two strings <string1> and <string2>, return a new string that contains letters from these two strings "interwoven" together, starting with the first character of <string1>. If the two strings are not of equal length, then start looping "backwards-and-forwards" in the shorter string until you come to the end of the longer string.
"interwoven" (or "interweaving") means constructing a new string by taking the first letter from the first string, adding the first letter of the second string, adding the second letter of the first string, adding the second letter of the second string, and so on.
"backwards-and-forwards" is a custom looping term. First the loop starts at position 1 (index 0) and goes until position n (i.e., the end). Once the loop reaches position n, it goes backwards, starting at position n - 1 and goes to position 1 (index 0). This repeats until the two strings are interwoven. For example, the backwards-and-forwards operations of "abc" would be "abcbabcba..."
Examples: If you are given "abc" and "123", then the output string is "a1b2c3". This is after taking "a" from the first string, adding "1" from the second string, adding "b" from the first string, and so on.
Things get more interesting when you are given two strings that differ in length. For example, if you are given "abcde" and "12", then the output would be "a1b2c1d2e1". Notice how the shorter string loops around when it runs out of characters, and continues looping until the longer string is exhausted.
Another example of the "backwards-and-forwards" implementation given two strings of differing length: "abcdfe" and "123", then the output would be "a1b2c3d2f1e2".
Note that the first string could be shorter too, for example, given "ab" and "123", the output would be "a1b2a3".
Precondition: both input strings will NOT be empty.
Hint: a good sanity check is to ensure that your output string is exactly twice the length of the longer input string :) >>> loopy_madness("abc", "123") "a1b2c3" >>> loopy_madness("abc", "abc") "aabbcc" >>> loopy_madness("abc", "@#$") "a@b#c$" >>> loopy_madness("1", "abc") "1a1a1a" """
Transcribed Image Text:lab5.py - E:\csc108\Labs\lab5.py
Eile Edit View Navigate Code Refactor Run Iools VCS Window Help
csc108) Labs
lab5.py
lab5.py x
2
UTM: CSC108, Fall 2021
3.
Practical Lab 5
5.
Instructors: Michael Liut, Andrew Petersen, Andi Bergen,
9.
Tingting Zhu, Pooja Vashisth, and Sonya Allin
8.
This code is provided solely for the personal and private use of
6.
students taking the CSC108 course at the University of Toronto.
11
Copying for purposes other than this use is expressly prohibited.
12
All forms of distribution of this code,
whether as given or with
13
any changes, are expressly prohibited.T
15
All of the files in this directory and all subdirectories are:
Copyright (c) 2021 Michael Liut, Haocheng Hu
LAB RESTRICTIONS, PLEASE READ:
Do not add any imports, the ones that you need will be given to you.
You may not use any lists or list methods, or any while loops.
Within your loops, you MUST NOT use any break or continue statements.
Please also do not use try-except statements, you should be able to anticipate
or prevent any errors from happening at all!
Edef sum string(string: str) -> int:
Given a string <string>, return the sum of this string, as computed by the
formula given below:
レ2
E TODO
Problems
Terminal
Python Packages
Python Console
五
Transcribed Image Text:Project
Favorites
Structure
lab5.py - E:\csc108\Labs\lab5.py
E File Edit View Navigate Code Refactor Run Tools VCS Window Help
csc108 ) Labs) e lab5.py
s lab5.py X
def substring_with_largest_sum(string: str) -> str:
89
67
Given a string <string>, return the substring with the largest sum (as
calculated by sum string ()) that exists within <string>. If there are
69
71
72
multiple substrings with equally large sums, return the first one that
73
occurs. The substring could just be the whole string itself, or it could
74
be empty.
75
To calculate the sum of the substring, please use the function sum_string()
which you have implemented above as a helper.
LL
78
For example, given the string "321", there are 7 possible substrings:
, "3", "2", "1", "32", "21", "321". For each one of them, their sum can
be calculated as per the sum string() function which you have already
implemented. By using your understanding of loops, implement this function
%3D
%3D
08
81
%3D
II
82
83
to return the substring with the largest sum by checking all of the
substrings of the input string. So, "32" evaluates to 1, "21" also
%3D
%3D
85
evaluates to 1, "321" evaluates to 2, resulting in "3" being the largest
possible substring as it evaluates to 3.
98
>>> substring_with_largest_sum ("321")
3
88
>>> substring_with_largest_sum("329")
68
06
91
329
>>> substring_with_largest_sum ("1111")
%3D
92
>>> substring_with_largest_sum ("5656565656")
76
95
"656565656"
96
total = 0
LO
E TODO
uny
O Problems
Terminal
Python Packages
e Python Console
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.