Question

Python 3, nbgrader, pandas 0.23.4

Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of th

b) List Powers Write a function called list_powers This function will take a list, and return a new list where each element i

Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s): dictionary :dictionary reverse boolean, default: False Output(s): .sorted_keys: list Procedure(s) Get the keys from the input dictionary using the keys()method (this will return a list of keys) Use the sorted function to sort the list of keys. Pass in reverse to sorted to set whether to reverse the sort order Return the sorted list of dictionary keys In [ ]: # rouR CODE HERE raise NotImplementedError) In assert callable (sort_keys) assert isinstance(sort keys('B':2, 'A': 1>),list) assert sort_keys(('B':2, A: 1)'A, B' assert sort_keys((2: B'. 1: A'>, reverse True) [2, 1 In assert callable(sort_keys)
b) List Powers Write a function called list_powers This function will take a list, and return a new list where each element is exponentiated to a specified power. Input(s): collection collection of numbers power-int, default value: 2 Output(s): power_list- list of numbers Procedure(s: . Initialize an empty list to be filled and returned Loop through each element in the input collection Take the value to the power specified in power Append the result to the output list Return the list of powers In [ ]: # YOUR CODE HERE raise NotImplementedError) In assert callable(1ist_powers) assert isinstance(list_powers([1, 2]), list) assert list_powers ( (2, 4))4, 16 assert list powers(12, 4]. 3)8, 64 In :assert callable(list_powers)
0 1
Add a comment Improve this question Transcribed image text
Answer #1

Answer to a)

def sort_keys(dictionary, reverse=False):
    key_list = dictionary.keys()
    sorted(key_list, reverse=reverse)
    return key_list

raise NotImplementedError()
assert callable(sort_keys)
assert isinstance(sort_keys({'B': 2, 'A': 1}), list)
assert sort_keys({'B': 2, 'A': 1}) == ['A', 'B']
assert sort_keys({2: 'B', 1: 'A'}, reverse=True) == [2, 1]

assert callable(sort_keys)

Answer to b)

def list_powers(collection, power=2):
    power_list = []

    for i in collection:
        power_list.append(i ** power)

    return power_list
    

raise NotImplementedError()
assert callable(list_powers)
assert isinstance(list_powers([1, 2]), list)
assert list_powers([2, 4]) == [4, 16]
assert list_powers([2, 4], 3) == [8, 64]

assert callable(list_powers)
Add a comment
Know the answer?
Add Answer to:
Python 3, nbgrader, pandas 0.23.4 Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • 3. Write the function find sorted elt whose input is a vector sorted in increasing order...

    3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....

  • Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary....

    Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary. Each key in starDictionary is the name of a star (a string). Its value is a dictionary with two keys: the string 'distance' and the string 'type'. The value associated with key 'distance' is the distance from our solar system in light years. The value associated with key 'type' is a classification such as 'supergiant' or 'dwarf'. 2. a number, maxDistance 3. a string,...

  • Questions 1. How to create a comment in python? 2. The way to obtain user input...

    Questions 1. How to create a comment in python? 2. The way to obtain user input from command line 3. List standard mathematical operators in python and explain them 4. List comparison operators in python 5. Explain while loop. Give example 6. Explain for loop. Give example 7. How to create infinite loop? And how to stop it? 8. Explain a built-in function ‘range’ for ‘for’ loop 9. Explain break statement 10. Explain continue statement 11. Explain pass statement 12....

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • Task 1: Write a Python program that takes as input from the user the name of a file containing po...

    python code: Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT