Use dictionaries get method to write histogram more concisely. You should be able to eliminate the if statement.
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
Write a function that takes a string and returns a dictionary with string elements (letters)
as values and their corresponding indices as keys.
>>> create_dictionary('ani')
{0: 'a', 1: 'n', 2: 'i'}
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 strings of the same length, s1 and s2,
and returns a dictionary which maps s1[i] with s2[i].
Example:
>>> map_strings('ani', 'pse')
{'a': 'p', 'n': 's', 'i': 'e'}
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_most_rated_movies(users: dict)}, that returns a list of movies
sorted by how many users have rated them (most to least). Example:
>>> get_most_rated_movies(users)
[('Inception', 2), ('Titanic', 1), ('Interstellar', 1)]
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 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'): |
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')