1. Homepage
  2. Programming
  3. MATH20621 Programming with Python - Mini test 2

MATH20621 Programming with Python - Mini test 2

Engage in a Conversation
UKMATH20621Programming with PythonPythonUniversity of Manchester

Mini test 2

This mini test consists of three Python functions to be written, one for each problem. The task is to prepare a Python script using the below code template and complete the functions with the appropriate code. Submit your Python script via upload on Blackboard until Friday, 18 November 2022 at 1pm the latest. Aim to submit an hour earlier as the deadline is strict and late submissions will not be accepted. CourseNana.COM

Note on independent work

You need to complete this mini test independently on your own, but you are allowed to use online resources and all lecture notes and exercise solutions. The lecture notes from weeks 1 to 7 contain all that is required to solve the below problems. You are not allowed to ask other humans for help. In particular, you are not allowed to send, give, or receive Python code to/from classmates and others. CourseNana.COM

Code template

Use the below template to complete the assignment. You can include any other functions in your script, but please make sure that all code is within a function. Your script shouldn't do anything when run, only after calling a function. In particular, all testing should be be done from within a main() function. CourseNana.COM

You can also add any other functions which you might find useful. Only the three required functions nested , len_count , and transpose will be marked, not the testing code. CourseNana.COM

It is good practice to include docstrings for each function, but in this mini test they won't be marked. Important: Do not alter the names of the three required functions and ensure they can be called with the parameters precisely as specified, and that they return (not print!) values of the correct data types. The three functions will be marked automatically and failure to follow the problem description will result in loss of marks. CourseNana.COM

You can also download the template as a .py file from https://personalpages.manchester.ac.uk/staff/stefan.guettel/py/minitest2.py CourseNana.COM

In [1]: 
"""
MATH20621 - Mini test 2
Student name: add your name
Student id:
add your id number
Student mail: your.name@student.manchester.ac.uk
"""
# Feel free to add other functions you find useful.

# Problem 1

def nested(p1, p2):
        """
        add a docstring
        """
        # TODO: add your code

        return

# Problem 2

def len_count(s):
        """
        add a docstring
        """

        # TODO: add your code
        return

# Problem 3

def transpose(L):
        """
        add a docstring
        """
        # TODO: add your code

        return

# main() function for all the testing
def main():

        # do your testing here, for example

        print("should return True:", nested("-13.11, 7.5", "17.19, 23.31, 1.41"))

main() # call main() function to run all tests

should return True: None CourseNana.COM

Feedback

As this is part of the course assessment, we will not be able to help you solving these problems in the lab classes or elsewhere. However, if you provided a valid email address in the docstring at the beginning of your .py file, you will receive a feedback report with a score for each function. Please read the problem descriptions carefully! The problems should (hopefully) be formulated in a way that there are no doubts. In case there remains a question on a problem formulation, please post it to the Blackboard Discussion Forum and it will be answered. CourseNana.COM

Problem 1

Write a function nested(p1, p2) that accepts two vectors p1 and p2 . Each of the vectors is provided as a string in the format "x, y, ..., z" (a sequence of floating-point numbers (elements) separated by a single comma and space combination). We say that the elements of p1 are nested by the elements of p2 if between any two neighboring elements of p2 there is at most one element of p1 . Here neighboring is to be understood as points being neighbors on the real number line: for an ordered set of n real numbers x each pair x and CourseNana.COM

The function returns the Boolean value True if the elements of p1 are nested by the elements of p2 ; otherwise the function returns the Boolean value False . CourseNana.COM

If an element in one of the vectors cannot be interpreted as a float value, the function should raise a TypeError exception. If p1 and p2 have an element in common, the function should return the value None of NoneType. CourseNana.COM

Examples: CourseNana.COM

  • nested("-13.11, 7.5", "17.19, 23.31, 1.41") should return True because −13.11 is in none of the intervals [1.41, 17.19] and [17.19, 23.31], and 7.5 is the only element in the interval [1.41, 17.19]
  • nested("20, 21.1", "17.19, 23.31, 1.41") should return False because 20 and 21.1 are both contained in the interval [17.19, 23.31]
  • nested("-13.11, 7.5, -3.2", "17.19, 7.5") should return None as both vectors have the element 7.5 in common
  • nested("-13.11, 7.5, -3.2", "17.19, xyz") should raise a TypeError Hint: A sorting of the values in p2 might greatly simplify this problem.

Problem 2

Write a function len_count(s) that takes a string argument s and then returns a list of tuples of the form (len, c) . Both len and c are integers, where c corresponds to the number of occurrences of words of length len in s . CourseNana.COM

The returned list is sorted by number of occurrences, with the most frequent word length appearing first. If two word lengths appear the same number of times in s , the count of longer words should come first. CourseNana.COM

Words in s are separated by single spaces and the function should not make a distinction between words being capitalized or not. Furthermore, commas ( , ) and full stops ( . ) should be ignored. You can assume that there are no other special characters in s . CourseNana.COM

Example: len_count("The University of Manchester is the best university, in manchester, and in the world.") CourseNana.COM

should return the list [(10, 4), (3, 4), (2, 4), (5, 1), (4, 1)] CourseNana.COM

Note: It is up to you whether you want to use one of Python's in-built sorting functions for this, or perhaps a variant of the my_sort function from the week 4 exercises. Also, don't worry too much if you struggle to get the full sorting right and just manage to sort by number of occurences; you'll get partial credits for that. CourseNana.COM

Problem 3

Write a function transpose(L) that takes as argument a list L representing a matrix with integer elements. The list L is a list of lists (referred to as sublists), each of which is a list of integers. Each of the sublists needs to have the same number of elements for the matrix to be well defined. CourseNana.COM

The function returns the transpose of the matrix L in the same format, i.e. as a list of lists with integer entries. When calling the transpose(L) function, the original list L should not be modified. If the lists in L do not have the same length, an IndexError exception should be raised. If there is a non-integer element in the matrix, a ValueError exception should be raised. (If both of these exceptions apply, it is up to you which one to raise.) Example 1: For the list L = [[5, 3, 0], [6, 0, 0]] CourseNana.COM

a call to transpose(L) should return the list L = [[5, 6], [3, 0], [0, 0]] Example 2: For the list L = CourseNana.COM

[[5, 3, 0], [6, 0]] CourseNana.COM

a call to transpose(L) should raise an IndexError . Example 3: For the list L = CourseNana.COM

[[5, 3, 0], [6, 1, 'x']] CourseNana.COM

a call to transpose(L) should raise a ValueError . CourseNana.COM

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
UK代写,MATH20621代写,Programming with Python代写,Python代写,University of Manchester代写,UK代编,MATH20621代编,Programming with Python代编,Python代编,University of Manchester代编,UK代考,MATH20621代考,Programming with Python代考,Python代考,University of Manchester代考,UKhelp,MATH20621help,Programming with Pythonhelp,Pythonhelp,University of Manchesterhelp,UK作业代写,MATH20621作业代写,Programming with Python作业代写,Python作业代写,University of Manchester作业代写,UK编程代写,MATH20621编程代写,Programming with Python编程代写,Python编程代写,University of Manchester编程代写,UKprogramming help,MATH20621programming help,Programming with Pythonprogramming help,Pythonprogramming help,University of Manchesterprogramming help,UKassignment help,MATH20621assignment help,Programming with Pythonassignment help,Pythonassignment help,University of Manchesterassignment help,UKsolution,MATH20621solution,Programming with Pythonsolution,Pythonsolution,University of Manchestersolution,