logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

Create a Python module called complexity.py. Within this module implement the following task for this module you may not use the Python built-in function pow

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

 Algorithms and programming in Python

Objectives

The objectives of this assignment are:

To demonstrate the ability to implement algorithms using basic data structures and operations on

To gain experience in designing an algorithm for a given problem description and implementing that algorithm in Python.

To demonstrate an understanding of complexity, and to the ability to implement algorithms of a given complexity.

Task 1: Complexity (4 Marks)

Create a Python module called complexity.py. Within this module implement the following task. For this module you may not use the Python built-in function pow(x) and you may not use the power operator **. You may not import any other libraries or modules.

Part A: Peaks (2 Marks)

Sometimes in life we care more about finding peaks than we care about finding the maximum. For example, during semester you know that the week containing the maximum amount of assessment is (probably) during exam block. But you will also have weeks in which that particular week contains more assessment than the week directly before it, or directly after it. As such, if you are planning on going to the movies with friends, you should probably do it in a week that is not an assessment peak.

Write a function local peak(lst) that takes in a list of numbers, and returns the index of a local peak.

Input: a list lst containing two or more numbers, where no number is the same as its neighbour.

Output: an integer indicating the index of a local peak. If there is more than one peak in the given list, the index of any peak may be returned. A peak is defined as a number that is greater than its neighbours. A peak may be the first or last number in the list, in which case it must be greater than its only neighbour.

For full marks, this function must have a complexity of O(log(N )), where N is the length of lst. Be aware that some Python functions have a higher complexity than you might think. One example is slicing, which has a complexity of O(k) where k is the length of the slice.

Examples

  1. Calling local peak([0,2,4,6,5,2]) returns 3.

  2. Calling local peak([-7,-6,-2,-10,-5,-11]) returns either 2 or 4.

  3. Calling local peak([8.8,8.6,8.1,8.2,8.1,8.01]) returns either 0 or 3.

Part B: Powers (2 Marks)

Write a function power(n, p) that takes a number and a power and returns the number raised to the given power.

Input: a number n and a non-negative integer p.

Output: a number that is the evaluation of np

For full marks, this function must have a complexity of O(log(p)).

Examples

  1. Calling power(2,8) returns 256.

Marking Guide (total 4 marks)

Marks are given for the correct behavior of the different functions:

  • 1 mark for the correct behaviour of local peak, and 1 mark for implementing the function with a complexity of O(log(N )). You must be able to explain why the function has a complexity of O(log(N )) to receive the full

  • 1 mark for the correct behaviour of power, and 1 mark for implementing the function with a complexity of O(log(p)). You must be able to explain why the function has a complexity of O(log(p)) to receive the full

Note: marks will be deducted for including print statements or function calls in submitted module.

Task 2: Birthday Card (6 Marks)

Your good friend, Xanthe, who also lives in Melbourne, is turning 21 in a few weeks. To celebrate the occassion you would like to give Xanthe a birthday card that has been signed by all of her friends. The only problem is Xanthe is incredibly well travelled, and has friends all through the country and the world.

To figure out the best way to get the card to all of these friends before Xanthe’s birthday, you have turned to graph theory. Every city and town in which a friend of Xanthe lives has been allocated a number between 1 and the number of friends (minus one), and Melbourne has been allocated the number 0. You have recorded postage times between locations as edge weights. You have found that all pairs of locations are such that the postage time from A to B is the same as the postage time from B to A, so your graph is undirected. You have also found that there is a postage connection between every pair of locations, so your graph is connected and complete. You have implemented this graph as an adjacency matrix, where each cell represents the weight of an edge. All postage times are given in integers, representing how many days the card would take to travel between the two locations. (I.e. an edge weight of 2 between A and B means it would take two days to get the card between location A and location B.)

Create a Python module called card.py. Within this module implement the following three tasks. You are encouraged to decompose the given tasks into additional functions. You may not import any other libraries or modules.

Part A: Time (1 Mark)

Write a function post time(graph, path) that returns how long it will take Xanthe’s card to travel the given path.

Input: a nested list graph that represents a graph as an adjacency matrix, that models the postage routes and times between Xanthe’s friends; and a list of integers path that represents a proposed path in the graph. You may assume the given path exists in the graph.

Output: an integer that is the number of days it would take Xanthe’s card to travel the given path.

Examples

postage1 =  [ [0,1,1,3,2],

[1,0,4,5,1],

[1,4,0,1,3],

[3,5,1,0,1],

[2,1,3,1,0] ]

 

postage2 =  [ [0,2,2,1,2],

[2,0,1,1,2],

[2,1,0,1,4],

[1,1,1,0,1],

[2,2,4,1,0] ]

The example graphs postage1 and postage2 are provided for illustrative purpose. Your implemented function must be able to handle arbitrary graphs in matrix form.

  1. Calling post time(postage1, [0,1,4,3,2,0]) returns 5.

  2. Calling post time(postage1, [0,3,1,2,3]) returns 13.

 

Part B: Where to post? (2.5 Marks)

Write a function post route(graph) that uses a greedy approach to find a fast (but not necessarily the fastest) route by which to send the card. The metric for the greedy approach is: send the card to the location that takes the shortest time to get to that the card has not yet been visited; or, if all locations have been visited, send the card back to Melbourne. If there are multiple locations to chose from, all with the same distance, choose the location that has been allocated the smaller number.

Input: a nested list graph that represents a graph as an adjacency matrix, that models the postage routes and times between Xanthe’s friends.

Output: a list of integers, where each integer is a location, ordered such that visiting each location in the order given creates a cycle that begins and ends in Melbourne. The only number that can appear twice is 0.

 

Examples

  1. Calling post route(postage1) returns [0,1,4,3,2,0].

  2. Calling post route(postage2) returns [0,3,1,2,4,0].

 

Part C: Can it be done? (2.5 Marks)

Write a function on time(graph,days) that determines whether there exists a postage route that can be completed before Xanthe’s birthday.

Input: a nested list graph that represents a graph as an adjacency matrix, that models the postage routes and times between Xanthe’s friends; and a non-negative integer days, which is the number of days until Xanthe’s birthday.

Output: a boolean, True if there exists a cycle beginning and ending at Melbourne, that visits every location, and that has a weight equal or less than days; otherwise False.

You must cite any lecture code you use.

Examples

Calling on time(postage1,5) returns True, because there is a cycle in graph postage1 with weight 5 that fits the criteria: [0,1,4,3,2,0].

Calling on time(postage2,8) returns True, because there is a cycle in graph postage2 with weight 8 that fits the criteria: [0,1,2,3,4,0].

Calling on time(postage2,5) returns False, because there is no cycle in graph postage2 with weight 5 that fits the

 

Marking Guide (total 6 marks)

Marks are given for the correct behavior of the different functions:

  • 1 mark for post time.

  • 5 marks for post route.

  • 5 marks for on time.

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

816 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

828 Answers

Hire Me
expert
Husnain SaeedComputer science

716 Answers

Hire Me
expert
Atharva PatilComputer science

829 Answers

Hire Me
March
January
February
March
April
May
June
July
August
September
October
November
December
2025
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
SunMonTueWedThuFriSat
23
24
25
26
27
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
00:00
00:30
01:00
01:30
02:00
02:30
03:00
03:30
04:00
04:30
05:00
05:30
06:00
06:30
07:00
07:30
08:00
08:30
09:00
09:30
10:00
10:30
11:00
11:30
12:00
12:30
13:00
13:30
14:00
14:30
15:00
15:30
16:00
16:30
17:00
17:30
18:00
18:30
19:00
19:30
20:00
20:30
21:00
21:30
22:00
22:30
23:00
23:30