Question: Dictionary *[10 points]* Now let's talk about dictionaries. It's very versatile. Checking if an element exists in a dictionary is always the first step before
![Dictionary *[10 points]* Now let's talk about dictionaries. It's very versatile.](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f39dbd5981f_66066f39dbcea64c.jpg)


![and see what's its behavior. [ ] \# demo 1 d={ 'shuyi':](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f39dbf1fcd6_66266f39dbec0c14.jpg)
Dictionary *[10 points]* Now let's talk about dictionaries. It's very versatile. Checking if an element exists in a dictionary is always the first step before including it. You can try to run the following cell and see what's its behavior. [ ] \# demo 1 d={ 'shuyi': ['grad'] } \# add a new key-value pair to the dictionary d[ 'fraser'] =[ 'grad'] [ ] \# demo 2 d={ 'shuyi' : 88 \# increment the number for 'fraser' by one; \# will throw an error because 'fraser' was not a key \# other languages will have different behaviors, they could initiate a value of and then increment \# but in Python this is not acceptable d[ 'fraser' ]+=1 \# this is equivalent to d[ 'fraser' ]=d[ 'fraser' ]+1, which involves a value retrieval first [3points] Check if element is a key in the given dictionary. Store your boolean result in variable x [ ] element = 'Argentina' d={ 'China': 'Asia', 'Belarus': 'Europe', 'USA': 'North America', 'Egypt': 'Africa' } \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# \# YOUR CODE HERE x= \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# x *[3 points] Check if element is a value in the given dictionary. Store your boolean result in variable [ ] element = 'Oceania' d={ 'China': 'Asia', 'Belarus': 'Europe', 'USA': 'North America', 'Egypt': 'Africa' } \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# \# YOUR CODE HERE x= \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# x *[3 points] Dictionaries can be quite handy insolving real-world problems. For example, can you write a function to find if sum of any two numbers equal a given value? If yes, can you find their corresponding indices? Maybe we can start with an intuitive solution using loops. - A test case: given the target sum 6 , the list [1,2,3,2,5] has one pair of elements (1,5) that add up to that target value. Their indicies 0,4 are returned. - Another test case: given the target sum 9 , the list [1,2,3,2,5] does not have any pair of lements that add up to that target value. False is returned. - In our test cases, you are guaranteed with one such pair, if the pair exists [ ] def two_sum_1(1, target ) : \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# \# YOUR CODE HERE \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# *[1 points]* Now you have a naive solution using loops. Can you solve it using dictionaries? [ ] def two_sum_2(1, target ): \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# \# YOUR CODE HERE \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# x= two_sum_2 2([1,2,3,2,5],6) print(x)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
