Skip to main content
留学咨询

辅导案例-FIT1045

By May 15, 2020No Comments

FIT1045 Algorithms and programming in Python, S2-2019Assignment 1 (value 10%)Due: Friday 30th August 2019, 11:55 pm.ObjectivesThe objectives of this assignment are:• To demonstrate the ability to implement algorithms using basic data structures and operations on them.• To gain experience in designing an algorithm for a given problem description and implementing thatalgorithm in Python.Submission Procedure1. Save your files into a zip file called yourStudentID yourFirstName yourLastName.zip2. Submit your zip file containing your solution to Moodle.3. Your assignment will not be accepted unless it is a readable zip file.Important Note: Please ensure that you have read and understood the university’s policies on plagiarismand collusion available at http://www.monash.edu.au/students/policies/academic-integrity.html. Youwill be required to agree to these policies when you submit your assignment.A common mistake students make is to use Google to find solutions to the questions. Once you have seen asolution it is often difficult to come up with your own version. The best way to avoid making this mistake isto avoid using Google. You have been given all the tools you need in workshops. If you find you are stuck, feelfree to ask for assistance on Moodle, ensuring that you do not post your code.Marks: This assignment will be marked both by the correctness of your code and by an interview withyour lab demonstrator, to assess your understanding. This means that although the quality of your code(commenting, decomposition, good variable names etc.) will not be marked directly, it will help to write cleancode so that it is easier for you to understand and explain.This assignment has a total of 12 marks and contributes to 10% of your final mark. For each day an assignmentis late, the maximum achievable mark is reduced by 10% of the total. For example, if the assignment is late by3 days (including weekends), the highest achievable mark is 70% of 12, which is 8.4. Assignments submitted 7days after the due date will normally not be accepted.Detailed marking guides can be found at the end of each task. Marks are subtracted when you are unable toexplain your code via a code walk-through in the assessment interview. Readable code is the basis of aconvincing code walk-through.1Task 1: Strings (3.5 Marks)Create a Python module called strings.py. Within this module implement the following three tasks. For thismodule you may not use the Python sequence method s.count(x).Part A: Code Breaker (1 Mark)Write a function decode(string, n) that takes in an encoded message as a string and returns the decodedmessage.Input: a string string and a positive integer n.Output: the decoded string. The string is decoded by discarding the first n characters from the input string,keeping the next n characters, discarding the next n characters, and so on, until the input string isexhausted.There is no guarantee at which point in the decoding (keep or discard) the string will be exhausted. There isno guarantee that the length of string will be a multiple of n.Examplesa) Calling decode(‘#P#y#t#h#o#n#’, 1) returns ‘Python’.b) Calling decode(‘AxYLet1x3’s T74codaa7e!’, 3) returns ‘Let’s code!’.Part B: Pattern Finder (1 Mark)Write a function pattern count(string, pat) that counts how many times a pattern occurs within a text.Input: a string string and a string pat, both comprising both upper and lower case alphanumeric andnon-alphanumeric characters.Output: an integer count of the number of times pat occurs within string. The count is case sensitive. (SeeExample b.)Examplesa) Calling pattern count(‘bcabcabca’, ‘abc’) returns 2.b) Calling pattern count(‘aaaa’, ‘aa’) returns 3.c) Calling pattern count(‘A long time ago in a galaxy far, far away…’, ‘a’) returns 8.d) Calling pattern count(‘If you must blink, do it now.’, ‘code’) returns 0.Part C: Palindromes (1.5 Marks)A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. Write a functionpalindrome(string) that determines whether or not a given string is a palindrome. In order to receive fullmarks, the function must determine whether the alphanumerical characters create a palindrome (see Examples).Input: a string string comprising both upper and lower case alphanumeric and non-alphanumeric characters.Output: a boolean, True if the string when converted to lower case alphanumeric characters is a palindrome;otherwise False.Examplesa) Calling palindrome(‘RacecaR’) returns True because ‘RacecaR’ reads the same backwards as forwards.b) Calling palindrome(‘66racecar77’) returns False because ‘66racecar77’ does not read the same backwardsas forwards.c) Calling palindrome(‘Racecar’) returns True because ‘racecar’ reads the same backwards as forwards.d) Calling palindrome(‘Madam, I’m Adam’) returns True because ‘madamimadam’ reads the same backwardsas forwards.e) Calling palindrome(‘#4Satire: Veritas4’) returns True because ‘4satireveritas4’ reads the same back-wards as forwards.2Marking Guide (total 3.5 marks)Marks are given for the correct behavior of the different functions:(a) 1 mark for decode.(b) 1 mark for pattern count.(c) 0.5 marks if palindrome can correctly identify that a string with no processing is a palindrome (seeExamples a and b), 1.5 marks if palindrome can correctly identify that a string when converted to lowercase alphanumeric characters is a palindrome (see Examples c, d, and e).3Task 2: Friends (4.5 Marks)You have been employed by Monash to analyse friendship groups on campus. Individuals have been allocated anumber between 0 and the number of people on campus (minus one), and their friendships have been recordedas edges between numbered vertices. We assume friendships are always bi-directional. Monash has implementedthis graph in a Python-readable format as both an adjacency matrix and an adjacency list.Create a Python module called friends.py. Within this module implement the following three tasks.Ensure you follow the inputs for each function correctly – one function takes an adjacency list, two functionstake an adjacency matrix.Part A: Popular (1.5 Marks)Write a function popular(graph list, n) that returns a list of people who have at least n friends. Each personis identified by the number of their vertex.Input: a nested list graph list that represents a graph as an adjacency list, that models the friendships atMonash; and a non-negative integer n.Output: a list of integers, where each integer is a person with at least n friends. If no person has at least nfriends, return an empty list. The list may be in any order.Examplesclayton_list = [ [1,2,3], [0,3], [0,4], [0,1], [2] ]The example graph clayton list is provided for illustrative purpose. Your implemented function must be ableto handle arbitrary graphs in list form.a) Calling popular(clayton list,2) returns [0,1,2,3].b) Calling popular(clayton list,3) returns [0].c) Calling popular(clayton list,0) returns [0,1,2,3,4].Part B: Friend of a Friend (1.5 Marks)Write a function foaf(graph matrix, person1, person2) that determines whether two people have exactlyone degree of separation: that is, whether two (distinct)1 people are not friends, but have at least one friend incommon.Input: a nested list graph matrix that represents a graph as an adjacency matrix, that models the friendshipsat Monash; an integer person1, and an integer person2, where 0 ≤ person1, person2 < number of peopleon campus, and person1 6= person2.Output: a boolean, True if the two people are not friends and they have at least one friend in common;otherwise False.Examplesclayton_matrix = [ [0,1,1,1,0],[1,0,0,1,0],[1,0,0,0,1],[1,1,0,0,0],[0,0,1,0,0] ]The example graph clayton matrix is provided for illustrative purpose. Your implemented function must beable to handle arbitrary graphs in matrix form.a) Calling foaf(clayton matrix,0,4) returns True as 0 and 4 are both friends with 2.b) Calling foaf(clayton matrix,0,3) returns False as 0 and 3 are friends directly.c) Calling foaf(clayton matrix,1,4) returns False as 1 and 4 have no friends in common.1 The functions may return True or False at your discretion for instances where person1 is person2. This case will not bemarked.4Part C: Society (1.5 Marks)Write a function society(graph matrix, person) that determines whether a person has two friends who arealso friends with each other.Input: a nested list graph matrix that represents a graph as an adjacency matrix, that models the friendshipsat Monash; and an integer person, where 0 ≤ person < number of people on campus.Output: a boolean, True if person has at least two friends who are also friends with each other; otherwiseFalse.Examplesa) Calling society(clayton matrix,0) returns True as 1 and 3 are both friends with 0.b) Calling society(clayton matrix,2) returns False as 2 is friends with 0 and 4, but 0 and 4 are not friendswith each other.Marking Guide (total 4.5 marks)Marks are given for the correct behavior of the different functions:(a) 1.5 marks for popular.(b) 1.5 marks for foaf.(c) 1.5 marks for society.5Task 3: Cards (4 Marks)A friend of yours is developing a prototype for a website on which people can play poker. They have contactedyou to help them implement some of the project’s backend: specifically, they would like some help telling whatkind of hand a player has played. You have agreed to implement four functions to assist them.In the backend, your friend has decided to represent cards as tuples comprising an integer and a string:(rank, suit). For example, the seven of clubs would be represented as (7, ‘C’). As this is a prototype,your friend has told you that no face cards will be used (that is, rank is always an integer, where 2 ≤ rank≤ 10, unless otherwise specified). There are four suits, with the following string representations: clubs: ‘C’,diamonds: ‘D’, hearts: ‘H’, spades: ‘S’. As this is poker, which is only played with one deck of cards, therewill never be duplicates, and each hand will contain exactly five cards.Create a Python module called cards.py. Within this module implement the following four tasks. For thismodule you may not use the Python inbuilt function sorted() or the Python method list.sort().Part A: Suits (1 Mark)Write a function suits(hand) that sorts a given hand into the four suits. As there is no standard ranking ofsuits in poker, we will order alphabetically: clubs, diamonds, hearts, spades.Input: a list hand containing five cards.Output: a nested list containing four lists of cards, with each internal list corresponding to a suit. If thereare no cards of that suit in the hand, the internal list for that suit is empty. Cards may be in any orderwithin their suit-list.Part B: Flush (1 Mark)Write a function flush(hand) that determines whether a given hand is a flush. In poker, a flush is a hand thatcontains five cards of the same suit.Input: a list hand containing five cards.Output: a boolean, True if all five cards in hand are the same suit, False if not.Part C: Straight (2 Marks)Write a function straight(hand) that determines whether a given hand is a straight. In poker, a straight is ahand that contains five cards of sequential rank. The cards may be of different suits.Your friend has asked you an additional favour: they would like to use this function in various other gamesthey may implement in the future, so instead of checking for a five-card straight, they would like you to checkfor an n card straight, where n is the number of cards in the hand. Like in poker, the cards may be of differentsuits.Input: a list hand containing at least three cards, where each card may have a rank of some number greaterthan zero.Output: a boolean, True if the ranks of the cards in hand may be ordered into an unbroken sequence withno rank duplicated, False if not.Examplesa) Calling suits([(4,‘C’),(8,‘H’),(3,‘C’),(2,‘D’),(8,‘C’)]) returns [[(4,‘C’),(3,‘C’),(8,‘C’)],[(2,‘D’)], [(8,‘H’)], []]. As there are multiple clubs, the clubs cards may be in any order within theirinternal list.b) Calling flush([(4,‘D’),(8,‘D’),(3,‘D’),(2,‘D’),(10,‘D’)]) returns True.c) Calling flush([(4,‘C’),(8,‘H’),(3,‘C’),(2,‘D’),(8,‘C’)]) returns False.d) Calling straight([(4,‘C’),(2,‘D’),(5,‘S’),(6,‘H’),(3,‘D’)]) returns True.e) Calling straight([(2,‘C’),(3,‘D’),(4,‘S’),(5,‘H’),(5,‘D’)]) returns False.f) Calling straight([(2,‘C’),(3,‘D’),(4,‘S’),(5,‘H’),(7,‘D’)]) returns False.g) Calling straight([(12,‘C’),(13,‘D’),(11,‘D’)]) returns True as the ranks of the cards form an unbro-ken sequence, [11,12,13], with no rank duplicated.6Marking Guide (total 4 marks)Marks are given for the correct behavior of the different functions:(a) 1 mark for suits.(b) 1 mark for flush.(c) 1 mark for straight if it is able to handle a normal poker five-card hand with card ranks between 2 and10 inclusive (see Examples d, e, and f); 2 marks for straight if it is able to handle an arbitrarily largehand with arbitrary ranks (see Example g).7

admin

Author admin

More posts by admin