Call/WhatsApp/Text: +44 20 3289 5183

Question: Which of the following is not a valid way to define the dictionary {'U': 1, 'C': 2, 'L': 3} in Python? a) dictionary = {} dictionary['U'] = 1 dictionary['C'] = 2 dictionary['L'] = 3

10 Nov 2022,5:28 PM

 

#Please make sure your Jupyter Notebook environment is running a Python 3 kernel import IPython

assert IPython.version_info[0] >= 3, "Your version of IPython is too old, please upd %autosave 120

 

 

 

 

QUESTION 1:

 

Which of the following is not a valid way to define the dictionary {'U': 1, 'C': 2, 'L': 3} in Python?

a)

 

dictionary = {} dictionary['U'] = 1 dictionary['C'] = 2 dictionary['L'] = 3

 

b)

 

dictionary = dict([ ('U', 1), ('C', 2), ('L', 3)

])

 

c)

 

dictionary = { ('U', 1), ('C', 2), ('L', 3)

}

 

d)

 

dictionary = {'U': 1, 'C': 2, 'L': 3}

 

 

 

 

In [ ]:

 

###ANSWER(1). Please assign the letter representing the correct answer to the variab ### (write it down between the "")

answer_1 = "c"

 

 

 

 

 

 

 

In [ ]:

 

#Please leave this cell blank.

 

 

 

 

QUESTION 2:

 

Two sets of flowers are defined.

 

f1 = {'Calla Lily', 'Daisy', 'Gardenia', 'Carnation', 'Orchid'} f2 = {'Tulip', 'Daisy', 'Gardenia', 'Dahlia', 'Orchid'}

 

Use a set operator to find the flowers that appear in f1 but not in f2, assign the result to the variable flowers_in_f1. Print flowers_in_f1, the expected output is shown below.

 

{'Carnation', 'Calla Lily'}

 

 

 

 

In [ ]:

 

###ANSWER(2)

f1 = {'Calla Lily', 'Daisy', 'Gardenia', 'Carnation', 'Orchid'} f2 = {'Tulip', 'Daisy', 'Gardenia', 'Dahlia', 'Orchid'} flowers_in_f1 = f1 - f2

print(flowers_in_f1)

 

 

 

In [ ]:

 

#Please leave this cell blank.

 

 

 

 

QUESTION 3:

 

Create a function called capitalise_word that takes a word as the argument. The function makes the input word's first letter a capital letter and returns the result.

 

 

Execute capitalise_word('jess'), the expected output is 'Jess'. Execute capitalise_word('abc'), the expected output is 'Abc'.

 

 

In [ ]:

 

###ANSWER(3)

def capitalise_word(word):

return word[0].upper()+word[1:]

 

 

 

In [ ]:

 

#Please leave this cell blank.

 

 

 

 

 

 

 

QUESTION 4:

 

A list which records today's orders of a bakery is defined.

 

orders = [('Bagel', 20), ('Bun', 20), ('Apple strudel', 3), ('Bun', 10), ('Tart', 3),

('Bagel', 10), ('Tart', 15), ('Bagel', 50)]

 

Create a dictionary that summarises the total number of orders for each item, assign the result to the variable order_dict. Print order_dict, the expected output is shown below.

 

{'Bagel': 50, 'Bun': 10, 'Apple strudel': 3, 'Tart': 15}

 

 

 

 

In [ ]:

 

###ANSWER(4).

orders = [('Bagel', 20), ('Bun', 20), ('Apple strudel', 3), ('Bun', 10), ('Tart', 3) ('Bagel', 10), ('Tart', 15), ('Bagel', 50)]

order_dict = dict() for order in orders:

if order in order_dict: order_dict[order[0]] += order[1]

else:

order_dict[order[0]] = order[1] print(order_dict)

 

 

 

In [ ]:

 

#Please leave this cell blank.

 

 

 

 

QUESTION 5:

 

A dictionary of the scores for 4 modules is defined below.

 

score = {'Programming': 90.71, 'Business': 75.94, 'Statistics': 71.27, 'Marketing': 69.56}

 

Calculate the average score for all the modules, and round the result to 2 decimal places. Define a string to the variable average_score. The string should follow this pattern: 'The average score is [average_score_of_all_modules].' Print average_score, the expected output is shown below.

 

The average score is 76.87.

 

 

 

 

In [ ]:

 

###ANSWER(5).

score = {'Programming': 90.71, 'Business': 75.94, 'Statistics': 71.27, 'Marketing': average_score = 'The average score is {:.2f}.'.format(sum(score.values())/len(score) print(average_score)

 

 

 

 

 

In [ ]:

 

#Please leave this cell blank.

 

 

 

 

QUESTION 6:

 

A dictionary with London areas as the keys and average renting prices as the values is defined.

 

rent_detail_london = {'Greenwich': 336, 'Stratford': 389, 'London Bridg e': 589, 'Angel': 359,

'Kings Cross': 467, 'Wembley': 401, 'Heathrow Airp ort': 238, 'Hyde Park': 429}

 

Write ONE LINE of code to define a new dictionary called affordable_area. The keys of affordable_area are the indices of areas with average prices lower than 400, and the values are the

corresponding areas. Print affordable_area, the expected output is shown below.

 

{0: 'Greenwich', 1: 'Stratford', 3: 'Angel', 6: 'Heathrow Airport'}

 

 

 

 

In [ ]:

 

###ANSWER(6).

rent_detail_london = {'Greenwich': 336, 'Stratford': 389, 'London Bridge': 589, 'Ang 'Kings Cross': 467, 'Wembley': 401, 'Heathrow Airport': 238, '

affordable_area = {idx:value for idx, value in enumerate(rent_detail_london) if rent print(affordable_area)

 

 

 

In [ ]:

 

#Please leave this cell blank.

 

 

 

 

QUESTION 7:

 

Create a function called special_number_checker that takes a non-negative integer as the argument. The function returns the boolean value True if the input integer has only one digit that appears twice and others that appear only once, and returns False otherwise.

 

 

Execute special_number_checker(23), the expected output is False. Execute special_number_checker(233), the expected output is True. Execute special_number_checker(251143), the expected output is True.

 

 

In [ ]:

 

###ANSWER(7).

def special_number_checker(x):

return len(str(x)) - len(set(str(x))) == 1

 

 

 

 

 

In [ ]:

 

#Please leave this cell blank.

 

 

 

 

QUESTION 8:

 

A list of ten integers is defined.

 

alist=[4, 7, 19, 5, 3, 10, 14, 27, 1, 2]

 

Create a dictionary where the keys are the integers and values are the lists of divisors among the numbers. Assign the dictionary to variable dict_num. Print dict_num, the expected output is shown below.

 

{4: [4, 1, 2], 7: [7, 1], 19: [19, 1], 5: [5, 1], 3: [3, 1], 10: [5, 10, 1, 2], 14: [7, 14, 1, 2], 27: [3, 27, 1], 1: [1], 2: [1, 2]}

 

 

 

In [ ]:

 

###ANSWER(8).

alist=[4, 7, 19, 5, 3, 10, 14, 27, 1, 2] dict_num = {}

for x in alist:

dict_num[x] = [i for i in alist if x%i==0] print(dict_num)

 

 

 

In [ ]:

 

#Please leave this cell blank.

 

 

 

 

QUESTION 9:

 

Create a function called validIP4 that takes a string of an IP4 address as the argument. The function returns the boolean value True if the IP4 address is valid, and returns False otherwise. A valid IP4 address has the format of x.x.x.x, where each x can be any integer between 0 and 255 (both inclusive).

 

 

Execute validIP4("255.255.255.255"), the expected output is True. Execute validIP4("1.11.0.255"), the expected output is True. Execute validIP4("255.255.255.256"), the expected output is False. Execute validIP4("255.255.255"), the expected output is False.

 

 

 

 

 

 

 

 

 

 

 

In [ ]:

 

###ANSWER(9). def validIP4(ip):

ip_numbers = [int(i) for i in ip.split(".")] ip_numbers_is_valid = [0<=i<=255 for i in ip_numbers]

return all(ip_numbers_is_valid) and len(ip_numbers_is_valid) == 4

 

 

 

In [ ]:

 

#Please leave this cell blank.

 

 

 

 

QUESTION 10:

 

Create a function called isAnagram which takes two strings as the arguments. The function returns the Boolean value True the two input strings are anagrams (same letters but in different orders, for example, 'abc' and 'bca'), and returns False otherwise. Note that exact same strings are NOT anagrams.

 

 

Execute isAnagram('abc', 'bca'), the expected output is True. Execute isAnagram('xyz', 'zzy'), the expected output is False. Execute isAnagram('tea', 'teea'), the expected output is False. Execute isAnagram('otto', 'ttoo'), the expected output is True.

 

 

In [ ]:

 

###ANSWER(10)

def isAnagram(s1, s2):

if len(s1) != len(s2) or s1 == s2: return False

dict1 = {i:s1.count(i) for i in s1} dict2 = {i:s2.count(i) for i in s2} return dict1 == dict2

 

 

In [ ]:

 

#Please leave this cell blank.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Expert answer

 

This Question Hasn’t Been Answered Yet! Do You Want an Accurate, Detailed, and Original Model Answer for This Question?

 

Ask an expert

Stuck Looking For A Model Original Answer To This Or Any Other
Question?


Related Questions

What Clients Say About Us

WhatsApp us