Write a function that takes a dictionary whose values are strings, and returns sum of length
all of values.
Example:
>>> get_string({1: "ani", 2: "o", -1: "pse"})
7
Write a function, process_text, that takes a string and:
- Replaces all punctuation with spaces
- Converts the string to lowercase
- Splits the string into words
- Returns a dictionary with word frequencies
>>> process_text("Hello, world! Hello again.")
{'hello': 2, 'world': 1, 'again': 1}
users = {
"Alice": {
"Inception": {"rating": 9, "review": "Mind-bending!"},
"Titanic": {"rating": 7, "review": "Too long, but emotional"},
"The Matrix": {"rating": 10, "review": "Revolutionary sci-fi"}
},
"Bob": {
"Inception": {"rating": 8, "review": "Loved the visuals"},
"Interstellar": {"rating": 10, "review": "Masterpiece"},
"The Dark Knight": {"rating": 9, "review": "Best superhero movie"}
},
"Charlie": {
"Titanic": {"rating": 9, "review": "Very emotional"},
"Avatar": {"rating": 8, "review": "Visually stunning"},
"Gladiator": {"rating": 9, "review": "Epic historical drama"}
}
}
Write a function, suggest_movie(user: str, users: dict) that suggests a movie
to a user based on what similar users have rated highly,
which the current user hasn't rated. Use the user with the lowest similarity score. Example:
>>> suggest_movie("Alice", users)
"Interstellar"
>>> suggest_movie("Bob", users)
"The Matrix"
Write a function that takes a dictionary, d, whose keys are numbers and values are strings
and returns a new dictionary by adding 2 to each key and '!' to each value. Example:
>>> add_to_dict({1: 'a', 4: 'jo be'})
{3: 'a!', 6: 'jo be!'}
Write a function that takes two lists of numbers (with the same length), t1 and t2,
and returns a new list which item of index i is sum of t1[i] and t2[i].
Example:
>>> add_lists([1, 2, 3], [1, 3, 1])
[2, 5, 4]
Write a function that takes a positive integer, i and returns a dictionary that maps
integers from 1 to i. Each integer is going to map itself. Example:
>>> create_dict(3)
{1: 1, 2: 2, 3: 3}
>>> create_dict(1)
{1: 1}
def ani(word, char):
index = 0
t = 0
while index < len(word):
if word[index] == char:
t = t + index
index = index + 1
return t
def add(word, ch1, ch2):
return ani(word, ch1) + ani(word, ch2)
Given the above script, what are the results of the following expressions:
| add('kungfu panda', 'k', 'u'): | ||
| add('kungfu panda', 'f', 'g'): |
def update(lst, i, j):
lst[i][j] = lst[i-1][j] + 2
nums = [[0, 0], [1, 1], [2, 2]]
update(nums, 1, 1)
print(nums)
update(nums, -1, 0)
print(nums)
Given the above script, write the printed values in the order that they appear.
| 1: | ||
| 2: |
Complete execution flow of the following program
def histogram(s): d = {} for c in s: d[c] = d.get(c, 0) + 1 return d h = histogram('oob')
Complete execution flow of the following program
def add_number(d, n): for key in d: d[key] = d[key] + n nums = {'a': 1, 2: 4} nums = add_number(nums, -4)