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}
Write a function that takes a dictionary and returns sum of all integers (whether they are
keys or values).
Example:
>>> get_sum({1: 'a', 2: 6, 12: 'po', 'jo': 4})
25
Write a function that takes a list of strings and a character, t and char
and returns count of strings which start with char (case insensitive).
>>> startswith_count(['A', 'a', 'bs', 'Ne', 'Aron'], 'a')
3
>>> count = startswith_count(['A', 'Barber', 'Bask', 'bs', 'Ne', 'BS', 'BS'], 'B')
>>> count
5
users = {
"Alice": {
"Inception": {"rating": 9, "review": "Mind-bending!"},
"Titanic": {"rating": 7, "review": "Too long, but emotional"}
},
"Bob": {
"Inception": {"rating": 8, "review": "Loved the visuals"},
"Interstellar": {"rating": 10, "review": "Masterpiece"}
}
}
Write a function, get_top_rated_movies(users: dict, min_ratings: int),
that returns a list of movies sorted from the highest average ratings to lowest average ratings,
but only include those with at least min_ratings users. Example:
>>> get_top_rated_movies(users, min_ratings=2)
[(8.5, 'Inception')]
>>> get_top_rated_movies(users, min_ratings=1)
[(10.0, 'Interstellar'), (8.5, 'Inception'), (7.0, 'Titanic')]
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}
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]
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: |
def dosmth(s, char):
c = 0
for i in range(len(s)):
if s[i] == char:
c = c + i
return c
Given the above script, what are the results of the following expressions:
| dosmth('baba', 'b'): | ||
| dosmth('nana', 'a'): |
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)
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')