Write a function, get_most_active_user, that takes the borrowed dictionary
and returns the user who borrowed the most books.
If there are multiple users with the same maximum, return any one of them. Example:
>>> borrowed = {"Alice": ["1984", "The Hobbit"], "Bob": ["Python"]}
>>> get_most_active_user(borrowed)
'Alice'
Use dictionaries get method to write histogram more concisely. You should be able to eliminate the if statement.
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"},
"Avatar": {"rating": 8, "review": "Immersive"}
},
"Charlie": {
"Inception": {"rating": 10, "review": "Amazing"},
"Titanic": {"rating": 6, "review": "Emotional"},
"Avatar": {"rating": 7, "review": "Nice"}
}
}
Write a function, get_similarity(user1: str, user2: str, users: dict),
that returns a similarity score between two users based on ratings of common movies.
Use the sum of absolute differences in rating (lower score = more similar).
If no common movies, return None. Example:
>>> get_similarity("Alice", "Charlie", users)
3 # |9-10| + |7-6| + |8-7| = 1+1+1 = 3
Write a function that takes a list of integers, t, it returns sum of all integers and deletes all
elements from t.
>>> a = [4, 3, 1, 2, 5, 2]
>>> d = get_sum(a)
>>> d
17
>>> a
[]
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')]
def dosmth(s, chars):
res = ""
for letter in s:
if letter in chars:
res = res + letter
return len(res)
Given the above script, what are the results of the following expressions:
| dosmth('python programming', 'pom'): | ||
| dosmth('python programming', 'arr'): |
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 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)