Question: 1: Write a function that takes a list of strings, and insert spaces to every string in the list according to the following rules: If
1:
Write a function that takes a list of strings, and insert spaces to every string in the list according to the following rules:
-
If the string is empty, change the empty string to one space
-
Else if the string length is less than or equal to 4, insert a space every character
-
Else if the string length is less than or equal to 8, insert a space every two characters
-
Else insert a space every three characters
Return the new list of strings after inserting spaces.
Notes:
-
Don't insert a space at the end, except when the string is empty.
-
You may find the built-in function str.join() helpful.
Requirements: assert statements, one-line list comprehension.
"""
>>> insert_spaces(["", "hola"])
[" ", "h o l a"]
>>> insert_spaces(["sean", "marina", "cumberbatch"])
["s e a n", "ma ri na", "cum ber bat ch"]
>>> insert_spaces(["I love Python!!"])
["I l ove Py tho n!!"]
"""
2:
Write a function that finds the greatest single digit divisor (integers from 1 to 9) that each whole number from lower to upper (both integer, both inclusive) is divisible by. Return a dictionary with keys being each number from lower to upper and their values being the highest divisor.
Requirements: assert statements, list comprehension
Construct the returned dictionary with dictionary comprehension, which is a dictionary version of list comprehension. The format is:
{key : value for in ...}.
If you choose not to do the extra credit, you are allowed to use loops for only dictionary construction.
"""
>>> find_greatest_divisor(20, 27)
{20: 5, 21: 7, 22: 2, 23: 1, 24: 8, 25: 5, 26: 2, 27: 9}
>>> find_greatest_divisor(1, 10)
{1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 5}
>>> find_greatest_divisor(11, 19)
{11: 1, 12: 6, 13: 1, 14: 7, 15: 5, 16: 8, 17: 1, 18: 9, 19: 1}
>>> find_greatest_divisor(98, 25)
Traceback (most recent call last):
AssertionError
"""
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
