- May 15, 2020
CSCA08H Assignment 1Deadline: Tuesday October 1 2019 by 4:00pm Wednesday Oct 2 2019 at 6:00 p.m.(to accommodate those of us attending the Global Climate Strike)Late policy: There are penalties for submitting the assignment after the due date.These penalties depend on how many hours late your submission is. Please see thecourse website for more information.Goals of this AssignmentUse the Function Design Recipe to plan, implement, and test functions.Write function bodies using variables, numeric types, strings, and conditionalstatements. (You can do this whole assignment with only the concepts fromWeeks 1, 2, and 3 of the course.)Learn to use Python 3, Wing 101, provided starter code, a checker module, andother tools.Tweet AnalyserThis assignment is based on the social network company Twitter. Twitter allows usersto read and post tweets that are between 1 and 280 characters long, inclusive. In thisassignment, you will be writing functions that (we imagine) are part of the programsthat manage Twitter feeds.Here are some example tweets:Standing ovation as Setsuko Thurlow is awarded a Doctor of Laws degree, honoriscausa, by the University of Toronto @UofT for her tireless nuclear disarmament workand contributions to the Treaty on the Prohibition of Nuclear Weapons [email protected] ICANCongratulations to our class of 2019 #UofTGrad19#UofT’s @ProbabilityProf @UofTStatSci created a mathematical model at the start ofthe playoffs to figure out the team’s odds of winning. He predicts their home-courtadvantage will give them an edge. http://bit.ly/ProbProfSome terminologyWe will use the following terms in this assignment.tweet: A message posted on Twitter. For our assignment, a valid tweet isbetween 1 and MAX_TWEET_LENGTH characters long (inclusive). MAX_TWEET_LENGTH is aconstant.tweet word: A word in a tweet. For our assignment, a valid tweet word containsonly alphanumeric characters and underscores. For example, pink_elephant is avalid tweet word, while bits&pieces is not (In fact, bits&pieces has two valid tweetwords, bits and pieces, with an ampersand (&) between them.)hashtag: A word in a tweet that begins with the hash symbol. Twitter uses thenumber sign (#) as the hash symbol. For our assignment, we’ll use the constantHASHTAG_SYMBOL to represent the hash symbol. Hashtags are used to label importantwords or terms in a tweet. A valid hashtag has the hash symbol as its firstcharacter and the rest of the characters form a valid tweet word. In other words,a hashtag begins with the hash symbol, and contains all alphanumeric charactersand underscores up to (but not including) the first non-alphanumeric character(such as space, punctuation, etc.) or the end of the tweet. A hashtag mustcontain at least one alphanumeric character.#UofT, #cscA08, and #Go_Raptors are three examples of hashtags on Twitter.Note that a hashtag is not a valid tweet word, because it has the hash symbol asits first character.mention: A word in a tweet that begins with the mention symbol. Twitter usesthe at-sign (@) as the mention symbol. For our assignment, we’ll use the constantMENTION_SYMBOL to represent the mention symbol. Mentions are used to direct amessage at or about a particular Twitter user, so the word should be a Twitterusername (but for the purposes of this assignment, we will not check if the wordthat follows the MENTION_SYMBOL is a real username — we’ll just assume it). For ourpurposes, the definition of a mention is very similar to that of a hashtag. A validmention has the mention symbol as its first character and the rest of thecharacters form a valid tweet word. In other words, a mention begins with the at-sign, and contains all alphanumeric characters and underscores up to (but notincluding) the first non-alphanumeric character (such as space, punctuation, etc.)or the end of the tweet. A mention must contain at least one [email protected], @UN_Women, and @UofTGrad2019 are three examples of Twittermentions.Note that a mention is not a valid tweet word, because it has the mention symbolas its first character.Here are some more interesting examples of how we will treat valid tweet words,hashtags, and mentions in this assignment.In the tweetRaptors win championship,#NBAFINALS, Go @Raptors!!! #WeTheNorthwe have four valid tweet words (Raptors, win, championship, and Go), two hashtags(#NBAFINALS and #WeTheNorth), and one mention (@Raptors). It is important to notethat in this example there is no space between the first comma and the hashtag#NBAFINALS, there is a comma immediately following the hashtag #NBAFINALS, thereare three exclamation marks immediately following the mention @Raptors, andthere are more than one space after the exclamation marks. All these are valid ina tweet. Also note that the first occurrence of the word Raptors is not consideredto be a mention, because it does not have the mention symbol.In the [email protected] welcomes its 2019 graduates! #UofTGrad2019#graduation!we have four valid tweet words (welcomes, its, 2019, and graduates), two hashtags(#UofTGrad2019 and #graduation), and one mention (@UofT). It is important to notethat in this example there is no space between hashtags #UofTGrad2019 and#graduation. This is also valid in a tweet.Some more obscure yet valid examples:In something#something_else we consider something is a valid tweet word and#something_else is a hashtag.In [email protected]#whatsoever?! we consider no is a tweet word, @spaces is a mention,and #whatsoever is a hashtag.For a complete list of Twitter terms, check out the Twitter glossary.Starter codeFor this assignment, we are giving you some files, including a Python starter codefiles. Please download the Assignment 1 Files and extract the zip archive.Starter code: tweet.pyThis file contains some constants, the header and the complete docstring (but notbody) for the first function you are to write. Your job is to complete this file.Checker: a1_checker.pyWe have provided a checker program that you should use to check your code.See below for more information about a1_checker.py.ConstantsConstants are special variables whose values do not change once assigned. A differentnaming convention (uppercase pothole) is used for constants, so that programmersknow to not change their values. For example, in the starter code, the constantMAX_TWEET_LENGTH is assigned the value 50 at the beginning of the module and the valueof MAX_TWEET_LENGTH should never change in your code. When writing your code, if youneed to use the value of the maximum tweet length, you should use MAX_TWEET_LENGTH.The same goes for the other constant values.Using constants simplifies code modifications and improves readability. If we laterdecide to use a different tweet length, we would only have to change the length in oneplace (the MAX_TWEET_LENGTH assignment statement), rather than throughout theprogram.What to doIn the starter code file tweet.py, complete the following function definitions. Use theFunction Design Recipe that you have been learning in this course . We have includedthe type contracts in the following table; please read through the table to understandhow the functions will be used.We will be evaluating your docstrings in addition to your code. Please include twoexamples in your docstrings. You will need to paraphrase the full descriptions of thefunctions to get an appropriate docstring description.Function name:(Parameter types)-> Return typeFull Description (paraphrase to get a proper docstringdescription)is_valid_tweet:(str) -> boolThe parameter represents a potential tweet. The functionshould return True if and only if the tweet contains between 1and MAX_TWEET_LENGTH characters, inclusive.compare_tweet_lengths:(str, str) -> intThe two parameters represent valid tweets. This function mustreturn one of three integers: 1 (if the first tweet is longer thanthe second), -1 (if the second tweet is longer than the first), or0 (if the tweets have the same length).add_hashtag:(str, str) -> strThe first parameter represents a valid tweet. The secondparameter represents a valid tweet word.Appending a space, a hash symbol, and the tweet word to theend of the original tweet will result in a potential tweet. If thepotential tweet is a valid tweet, the function should return thepotential tweet. If the potential tweet is not a valid tweet, thefunction should return the original tweet.For example (assuming the hash symbol is ‘#’), if the firstargument is ‘I like’ and the second argument is ‘cscA08’, thenthe function should return ‘I like #cscA08′, ifMAX_TWEET_LENGTH is at least 14. Otherwise, it should return’I like’.The first parameter represents a valid tweet, and the secondparameter represents a valid tweet word. This function shouldreturn True if and only if the tweet contains a hashtag made upcontains_hashtag:(str, str) -> boolof the hash symbol and the tweet word. For example (assumingthe hash symbol is ‘#’), if the first argument is ‘I like #cscA08’,and the second argument is ‘cscA08’, then the function shouldreturn True.Notes: If the first argument is ‘I like #cscA08’, and the secondargument is ‘csc’, then the function should return False. Also, ifthe first argument is ‘I like #cscA08, #mat137, and #phl101’, andthe second argument is cscA08, the function should return True.Hint: Use the helper function clean that is provided in thestarter code.is_mentioned:(str, str) -> boolThe first parameter represents a valid tweet, and the secondparameter represents a valid tweet word. This function shouldreturn True if and only if the tweet contains a mention made upof the mention symbol and the tweet word. For example(assuming the mention symbol is ‘@’), if the first argument is’Go @Raptors!’, and the second argument is ‘Raptors’, then thefunction should return True.Hint: This function is very similar to the functioncontains_hashtag. What can you do to avoid writing the samecode twice?add_mention_exclusive:(str, str) -> strThe first parameter represents a valid tweet and the secondparameter represents a valid tweet word. Appending a space, amention symbol, and the tweet word to the end of the originaltweet will result in a potential tweet. If the potential tweet isvalid and the original tweet contains the given tweet word, thefunction should return the potential tweet. In all other cases,the function should return the original tweet. Note that if thetweet word is mentioned in the original tweet (i.e., it appearswith a MENTION_SYMBOL as a first character), then thefunction should return the original tweet.For example (assuming the mention symbol is ‘@’), if the firstargument is ‘Go Raptors!’ and the second argument is ‘Raptors’,then the function should return ‘Go Raptors! @Raptors’. If, on theother hand, the first argument is ‘Go @Raptors!’ and the secondargument is ‘Raptors’, then the function should return theoriginal tweet ‘Go @Raptors!’.Hint: Can you use one of your other functions as a helperfunction?The parameter represents a message. This function shouldreturn the minimum number of tweets that would be requiredFunctions to write for A1num_tweets_required:(str) -> intto communicate the entire message. Recall the maximumlength of a tweet is MAX_TWEET_LENGTH.Hint: The ceil function in the math module is useful here.get_nth_tweet:(str, int) -> strThe first parameter represents a message that a Twitter userwould like to post, and the second parameter, n, represents aninteger greater than or equal to 0. If the message contains toomany characters, it would need to be split up into a sequenceof tweets. All of the tweets in the sequence, except possibly thelast tweet, would be of length MAX_TWEET_LENGTH. This functionshould return the nth valid tweet in the sequence of tweets.Note that the first tweet in the sequence has index 0, thesecond tweet in the sequence has index 1, and so on. If thevalue of the second parameter is too large, so there is noindex-n tweet in the sequence, this function should return anempty string.Using ConstantsAs we discuss in section Constants above, your code should make use of the providedconstants. If the value of one of those constants were changed, and your programrerun, your functions should work with those new values.For example, if MAX_TWEET_LENGTH were changed, then your functions should workaccording to the new maximum tweet length.Your docstring examples should reflect the given values of the constants in theprovided starter code, and do not need to change.No Input or OutputYour tweet.py file should contain the starter code, plus the function definitions specifiedabove. tweet.py must not include any calls to the print and input functions. Do not addany import statements. Also, do not include any function calls or other code outside ofthe function definitions.How should you test whether your code worksFirst, run the checker and review ALL output — you may need to scroll. You shouldalso test each function individually by writing code to verify your functions in thePython shell. For example, after defining function compare_tweet_lengths, you might callit from the shell (e.g., compare_tweet_lengths(‘I love’, ‘programming’)) to check whetherit returns the right value (-1). One call usually isn’t enough to thoroughly test thefunction — for example, we should also test compare_tweet_lengths(‘programming’, ‘isfun’) where it should return 1 and compare_tweet_lengths(‘this course’, ‘is for me!!’)where it should return 0.A1 CheckerWe are providing a checker module a1_checker.py that tests two things:whether your code follows the Python style guidelines, andwhether your functions are named correctly, have the correct number ofparameters, and return the correct types.To run the checker, open a1_checker.py and run it. Note: the checker file should be inthe same directory as your tweet.py, as provided in the starter code zip file. We haveposted a demo of the checker being run and included it in the Week 3 Prepareexercises on PCRS. Be sure to scroll up to the top and read all messages.If the checker passes for both style and types:Your code follows the style guidelines.Your function names, number of parameters, and return types match theassignment specification. This does not mean that your code workscorrectly in all situations. We will run a different set of tests on your code onceyou hand it in, so be sure to thoroughly test your code yourself beforesubmitting.If the checker fails, carefully read the message provided:It may have failed because your code did not follow the style guidelines. Reviewthe error description(s) and fix the code style. Please see the PyTA documentationfor more information about errors.It may have failed because:you are missing one or more functions,one or more of your functions is misnamed,one or more of your functions has the incorrect number or type ofparameters, orone of more of your function return types does not match the assignmentspecification.Read the error message to identify the problematic function, review the functionspecification in the handout, and fix your code.Make sure the checker passes before submitting.MarkingThese are the aspects of your work that may be marked for A1:Coding style (20%):Make sure that you follow Python style guidelines that we have introducedand the Python coding conventions that we have been using throughout thesemester. Although we don’t provide an exhaustive list of style rules, thechecker tests for style are complete, so if your code passes the checker, thenit will earn full marks for coding style with one exception: docstrings may beevaluated separately. For each occurrence of a PyTA error, one mark (out of20) deduction will be applied. For example, if a C0301 (line-too-long) erroroccurs 3 times, then 3 marks will be deducted.All functions, including helper functions, should have complete docstringsincluding preconditions when you think they are necessary.Correctness (80%): Your functions should perform as specified. Correctness, asmeasured by our tests, will count for the largest single portion of your marks.Once your assignment is submitted, we will run additional tests not provided inthe checker. Passing the checker does not mean that your code will earn fullmarks for correctness.No Remark RequestsNo remark requests will be accepted. A syntax error could result in a grade of 0 on theassignment. Before the deadline, you are responsible for running your code and thechecker program to identify and resolve any errors that will prevent our tests fromrunning.What to Hand InThe very last thing you do before submitting should be to run the checkerprogram one last time.Otherwise, you could make a small error in your final changes before submitting thatcauses your code to receive zero for correctness.Submit tweet.py on MarkUs by following the instructions on the course website.Remember that spelling of filenames, including case, counts: your file must be namedexactly as above.