Paste
Copy
Cut


Options
  • Chegg Logo
    Here’s the best way to solve it.
    Solution

    Here's your code in python: def insert_substrings_into_string(s, substrs): res = "" tokens = [] word = "" i = 0 # extract substring to be inserted as list while(i < len(substrs)): if(substrs[i] == ','): i += 2 tokens.appen

    View the full answer
    answer image blur
Transcribed image text:
• insert_substrings_into_string(s, substrs): This function takes a string s and a string substrs. The substrs string will contain 0 or more comma-separated substrings. The string s will contain text with 0 or more special placeholder character sequences. These placeholder character sequences will always begin with the percent character %, followed by one or more digits (which form a non- negative number). The function will create a new string from the original string s where all the placeholder character sequences in s are replaced by the substrings in the string substrs. The placeholder @ should be replaced by the first substring in substrs (ie., everything before the first comma, if such a comma exists). The placeholder 81 should be replaced by the second substring in substrs (ie., everything between the first and second comma, if such a comma exists), and so on. The new string should then be returned. Note: If a placeholder sequence refers to an invalid index (e.g., * 10 when there are only five sub- strings in substrs), then include the sequence (e.g., %10) as is in the new string. Note: The order of the placeholder sequences in s may not always be increasing. For instance, 82 may appear before %1. Also, the same placeholder sequence could be repeated (e.g., %2 could appear at one point in s and again at a later point). Each instance of the same sequence would thus be replaced by the same value. Note: If a space follows a comma in the string substrs, do not include the space as part of the substring to be included in the returned string, >>> Insert_substrings_into_string("My favorite so ts 41!!!", "programming language, Python") "My favorite programming language is Python!!! S >>> insert_substrings_into_string("Once upon a midnight se, while IM, 42 and 3". V "dreary, pondered, weak, weary") Once upon a midnight dreary, while I pondered, weak and weary' >>> insert_substrings_into_string("Once upon a midnight 10, while I 11", "dreary") Once upon a midnight dreary, while I 1