Question

Python problem.3. (6 pts) Define the following four sorting functions: each takes an argument that is a list of int or str or both values (otherwise raise an AssertionError exception with an appropriate error message) that will be sorted in a different way. Your function bodies must contain exactly one assert statement followed by one return statement. In parts a-c, create no other extra/temporary lists other than the ones returned by calling sorted. a. (2 pts) Define the mixed sort1 function, which returns the list sorted increasing by the int values and int equivalent of the str values. For example, mixed sort! ([13.1, 2.4] ) returns [1,2·13 ,4J. calling mixed sort1(1) prints AssertionError: qlsolution.mixed sortl: 1 non int/str list b. (2 pts) Define the mixed sort2 function, which returns the list sorted first increasing by the int values and then sorted increasing by the int equivalent of the str values. For example, mixed sort ([3,1, 2,4]) returns [1,4, 2, 3] c. (1 pt) Define the mixed sort3 function, which returns the list sorted first increasing by the int values and then sorted decreasing by the int equivalent of the str values. For example, mixed sort([3,1, 2,4]) returns [1,4, 3, 2. Hint: for the key argument, I used a conditional if in the lambda to choose a different tuple for int and str values, so they compare correctly. d. (1 pt) Define the mixed sort4 function, which returns values in the same order as mixed sort3, but here all the values returned are int. For example, mixed sort([3,1, 2,4]) returns [1,4,3,2]. You may create a temporary list in this function. Hint: I created it using a comprehension.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
"""
File Name : sorting.py
Author : __
Date : 10/10/2017
Description :

"""


def mixed_sort1(input_list):
    """
    This function sort the list increasing order by int value of string
    :param input_list:
    :return:
    """
    assert isinstance(input_list, list), "q1solution.mixed_sort1 : " + str(input_list) + " non int/str list"
    assert all(
        isinstance(item, int) or isinstance(item, str) for item in input_list), "q1solution.mixed_sort1 : " + str(
        input_list) + " non int/str list"
    return sorted(input_list, key=lambda x: int(x))


def mixed_sort2(input_list):
    """
    This function sort the list increasing order by int and then increasing  order by string
    :param input_list:
    :return:
    """

    assert isinstance(input_list, list), "q1solution.mixed_sort1 : " + str(input_list) + " non int/str list"
    assert all(
        isinstance(item, int) or isinstance(item, str) for item in input_list), "q1solution.mixed_sort1 : " + str(
        input_list) + " non int/str list"
    return sorted(input_list, key=lambda x: (isinstance(x, str), x))


def mixed_sort3(input_list):
    """
    This function sort the list increasing order by int and decreasing order by string
    :param input_list:
    :return:
    """
    assert isinstance(input_list, list), "q1solution.mixed_sort1 : " + str(input_list) + " non int/str list"
    assert all(
        isinstance(item, int) or isinstance(item, str) for item in input_list), "q1solution.mixed_sort1 : " + str(
        input_list) + " non int/str list"
    return sorted(input_list, key=lambda x: (False, x) if isinstance(x, int) else (True, -int(x)))


def mixed_sort4(input_list):
    """
    This function sort the list increasing order by int and decreasing order by string and using
    list comprehension convert each element to int type and return
    :param input_list:
    :return:
    """

    assert isinstance(input_list, list), "q1solution.mixed_sort1 : " + str(input_list) + " non int/str list"
    assert all(
        isinstance(item, int) or isinstance(item, str) for item in input_list), "q1solution.mixed_sort1 : " + str(
        input_list) + " non int/str list"
    return [int(x) for x in sorted(input_list, key=lambda x: (False, x) if isinstance(x, int) else (True, -int(x)))]


if __name__ == '__main__':
    print("mixed_sort1:", mixed_sort1(['3', 1, '2', 4]))
    print("mixed_sort2:", mixed_sort2(['3', 1, '2', 4]))
    print("mixed_sort3:", mixed_sort3(['3', 1, '2', 4]))
    print("mixed_sort4:", mixed_sort4(['3', 1, '2', 4]))

mixed sortl: [1, 2, 3, 4] mixed sort2: [1, 4, 2, 3] mixed sort3: [1, 4, 3, 2] mixed sort4: [1, 4, 3, 2] Process fin

Add a comment
Know the answer?
Add Answer to:
Python problem. 3. (6 pts) Define the following four sorting functions: each takes an argument that...
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
  • In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of...

    In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of lists and functions with multiple inputs and multiple outputs. Your task is to implement the separate_and_sort function. This function takes two input parameters: 1. A list containing strings and integers in any order. 2. An optional integer parameter size which controls how many items in the list the function will process. If the length of the list is smaller than the value of size,...

  • In C++ 9) (5 pts) Complete the following function that takes 3 arguments of a circle...

    In C++ 9) (5 pts) Complete the following function that takes 3 arguments of a circle : - radius (input argument) - area (output argument, to be calculated) - circumference (output argument, to be calculated) All arguments are double type. If a radius is negative, the function returns false, otherwise it returns true. The function does only calculation, and does nothing else. Assume that all #include are already there                                     // Fill in your Function prototype bool circleAreaAndCircumference ( _______,...

  • in python A. Define a function called contains_only_integers() that takes a tuple, returns True if all...

    in python A. Define a function called contains_only_integers() that takes a tuple, returns True if all the items in the tuple are integers(2), and returns False otherwise. For example, contains_only_integers (3, 5, 17, 257, 65537) ), contains_only_integers( (-1,) ), and contains_only_integers( ) should all return True, but contains_only_integers (2.0,4.0)) and contains_only_integers (8, 4, "2", 1)) should both return false. Your function should use a while loop to do this calculation. 121 Hint: the is instance() built-in function provides the most...

  • Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n...

    Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n as parameters and returns a NEW list which contains the n largest values in the parameter list. The values in the returned list should be in increasing order. The returned list must always be of length n. If the number of values in the original list is less than n, the value None should be repeated at the end of the returned list to...

  • (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std;...

    (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std; // sorted array in descending order int list[SIZE] (23, 19, 17, 13, 11, 7, 5, 3, 1, 0); // recursively binary search the array list for key // return the index to list if key is found. else return -1 int recBinarySearch (int key) // Please implement the recursive function.. Please implement the C++ function recBinarySearch that recursively binary searches the value key in...

  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • Create a python add the following functions below to the module. Each section below is a...

    Create a python add the following functions below to the module. Each section below is a function that you must implement, make sure the function's names and parameters match the documentation (Copy-Paste). DO NOT put the functions in an if-name-main block. 1. def productSum(x: int, y: int, z: int) -> int This function should return: The product of x and y, if the product of x and y is less than z. Else it should return the sum of x...

  • 1. Define a function in python that returns the sum of the following 4 lists. Remember...

    1. Define a function in python that returns the sum of the following 4 lists. Remember to store and re- use your code instead of summing the values for each list 4 times. Hence, define a function that takes as an argument a list and returns the sum of values. Copy the following lists and paste them in a Python file after you define the sum_of_values function Call the function and pass to it a different list each call as...

  • Question 2 - Objects You are to implement the following functions: getCharacterFrequency -10pts This function takes...

    Question 2 - Objects You are to implement the following functions: getCharacterFrequency -10pts This function takes a single string (str) argument and returns an object. o The object's properties will be the unique letters present in str The value of each property will be the frequency of each character present in the string. NOTE: Uppercase and Lowercase letters should be grouped in the same count. For example, the word Babble would result in the following object "B3, 11 This count...

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