Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


COMP10001 Foundations of Computing

Tutorial Questions: Week 3-2


Discussion

1. In what situations would we use a “dictionary”. How is it structured, how do we add and delete items?

A:   A dictionary holds relations between keys and values.  Its useful for counting frequencies or storing information about

things. Dictionaries are accessed in a similar way to other sequences, by using index notation.  Values stored are retrieved by indexing with the associated key (d[key]).  Values are added by indexing with assignment (d[key]  =  value) and deleted with the .pop(key) method, which takes as an argument the key we wish to delete.

2. What is the difference between using the .pop() method on a dictionary and using it on a list?

A:   On a list:  .pop() called without an index argument removes the last item in the list. Called with an index .pop(index)

deletes the item at that index in the list. Both times it will return the object it has deletedfrom the list.

On a dictionary:  .pop(key) deletes the key:value pair associated with that key in the dictionary, returning the value it has removed. Without an argument,  .pop() will not work because unlike lists, dictionaries dont have an inherent ordering so need the key to know which value to delete.

3. In what situations would we use a“set”? How does it differ to a list and a dictionary?

A:   A set stores a collection of unique objects. We might use one when storing a set of numbers or other unique objects, or when we

want to remove duplicatesfrom some other sequence. The set operations can be very useful too.

4. What key operations can you perform on sets? How do you add and remove items from them?

A:   The   three   main   operations   are   union:     s1   |  s2  or   s1.union(s2);    intersection:     s1  &  s2  or

s1.intersection(s2); and difference:  s1  -  s2 or s1.difference(s2). Adding an item is pos- sible with the .add(item) method and removing an item is done with the .remove(item) method. Note that since dictionaries use empty braces  {}, in order to create an empty set we need to use set ().

5. What is the difference between sorted () and .sort() when applied to a list? What does it mean to edit an object“in-place”?

A:   Say  were  talking  about a  list my_list.    Both  sorted (my_list) and my_list.sort() will  sort

my_list.   sorted (my_list) will return a new list which contains the items of my_list in sorted order. my_list is left unchanged by thisfunction. my_list.sort() on the other hand, will mutate my_list, changing the order of its items to sort it. Nothing is returned from this method (None if you try to assign its output) because it does its work directly on the list. The original order of items in my_list is overwritten.

Editing an object in-place means mutating it: editing it directly without creating a copy or returning a new object. It can be dangerous if youre not sure you want your data to be changed so be careful!

Now try Exercises 1 – 3

 

Exercises

1. Evaluate the following given the assignment d  =   { "R" :  0,  "G" :  255,  "B" :  0,

"other " :   { "opacity " :  0.6}}.  Specify whether the value of d changes as a result.  Assume d is reset to its original value after each.

(a)  "R"in  d

A:   True

(b) d[ "R"]

A:   0

(c) d[ "R"]  =  255

A:   New value of d:

{ 'R ' :  255,   'G ' :  255,   'B ' :  0,   ' other ' :   { ' opacity ' :  0.6}} (d) d[ "A"]

A:   KeyError:   'A '

(e) d[ "A"]  =  50

A:   New value of d:

{ 'R ' :  0,   'G ' :  255,   'B ' :  0,   ' other ' :   { ' opacity ' :  0.6},   'A ' :  50} (f) d.pop( "G")

A:   255

New value of d:

{ 'R ' :   0,   'B ' :   0,   ' other ' :   { ' opacity ' :   0.6}}


(g) d[ "other " ][ "blur " ]  =  0.1

A:   New value of d:

{ 'R ' :  0,   'G ' :  255,   'B ' :  0,   ' other ' :   { ' opacity ' :  0.6,   'blur ' :  0.1}} (h) d.items()

A:   dict_items([( 'R ',  0),   ( 'G ',  255),   ( 'B ',  0),   ( ' other ' ,   { ' opacity ' :  0.6})])

2. Evaluate the following given the assignment s1  =   {1,  2,  4} and s2  =   {3,  4,  5}.  If s1 or s2

change as a result, give their new value. Assume s1 and s2 are reset to their original values after each.

s1.add(7)

A:New valuefor s1:     {1,  2,  4,   7}

s1.add(2)

A:s1 does not change (2 is already in the set)

(c)  s2.remove(5)

A:New valuefor s2:

{3,  4}

3. What is the output of this code? Why?


(d)  s1  &  s2 A: {4}

s1.union(s2)

A: {1,  2,  3,  4,  5}

s1  -  s2

A: {1,  2}



def  mystery(x):

x.append(5)

x[0]  +=  1

print ( "mid-mystery : " ,  x)

my_list  =   [1,2]

print (my_list)

mystery(my_list)

print (my_list)

mystery(my_list.copy())

print (my_list)


A:

[1,  2]                                                                                                                                              mid-mystery:   [2,  2,  5]                                                                                                          [2,  2,  5]                                                                                                                                       mid-mystery:   [3,  2,  5,  5]                                                                                                   [2,  2,  5]                                                                                                                                      

The mystery() function is a mutating function, as it changes the elements of the list argument it is given.  In the rst example, thefunction is called with the original list and therefore it is mutated: the value printed mid-mystery is reflected outside thefunction. The second time, a copy of the original list is passed as an argument, meaning any change made inside thefunction are not applied to the list, as we can see with how the last two lines printed are different.

If the mystery()function were changed to alter a string, we would see that no matter how a string is passed into afunction, it can never be mutated because it is an immutable type. Returning a new string is the only way to pass changes outside afunction.

 

Problems

1. Write a function which takes a string as input and prints the frequency of each character in the string using a dictionary.

A:

def  freq_count(words):                                                                                                           freqs  =   {}                                                                                                                             for  letter  in  words:                                                                                                      if  letter  in  freqs:                                                                                                freqs[letter]  +=  1                                                                                         else :                                                                                                                               freqs[letter]  =  1                                                                                           for  key  in  freqs:                                                                                                             print (key,  freqs[key])                                                                                       


2. Write a function which takes two lists as input and returns a list containing the numbers which they both have in common.

A:


def  unique(list_1,  list_2):

set_1  =  set (list_1)

set_2  =  set (list_2)

common  =  set_1  &  set_2

return  list (common)