Python Intro The goal of this exercise is to get familiar with python and autograder. The projects for this class assume you use Python 3.6, but any version above 3.6 should generally work. Short...

1 answer below »
I need to fill in three python files which means that I should implement three py files from the given GitHub link.There is a test case and the codes should pass all test cases.


Python Intro The goal of this exercise is to get familiar with python and autograder. The projects for this class assume you use Python 3.6, but any version above 3.6 should generally work. Short version (for those who are familiar with python and want to jump in) 1. Download the python tutorial assignment https://github.com/pisan382/python-tutorial (Links to an external site.) 2. Fill in fill in portions of addition.py, buyLotsOfFruit.py, and shopSmart.py 3. Run the autograder: python3 autograder.py to confirm you get 3/3 4. Submit: addition.py, buyLotsOfFruit.py, and shopSmart.py Evaluation: Your code will be autograded for technical correctness. Do not change the names of any provided functions or classes within the code, or you will wreak havoc on the autograder. However, the correctness of your implementation – not the autograder’s judgements – will be the final judge of your score. If necessary, we will review and grade assignments individually to ensure that you receive due credit for your work. Extended Version Python Installation: If you do not have python 3.x installed, install it from https://www.python.org/downloads/ (Links to an external site.) You may have multiple versions of python installed, so check your current version using python3 --version or python --version Python Basics: There are multitude of python tutorial available.  A good starting place is https://docs.python-guide.org/intro/learning/ (Links to an external site.) To follow the below exercises, download the example python scripts from https://github.com/pisan382/python-basics (Links to an external site.)  The programming assignments in this course will be written in Python (Links to an external site.), an interpreted, object-oriented language that shares some features with both Java and Scheme. This tutorial will walk through the primary syntactic constructions in Python, using short examples. We encourage you to type all python shown in the tutorial onto your own machine. Make sure it responds the same way. You may find the Troubleshooting section helpful if you run into problems. It contains a list of the frequent problems previous students have encountered when following this tutorial. Invoking the Interpreter Python can be run in one of two modes. It can either be used interactively, via an interpeter, or it can be called from the command line to execute a script. We will first use the Python interpreter interactively. You invoke the interpreter using the command python at the Unix command prompt; or if you are using Windows that doesn’t work for you in Git Bash, using python -i. $ python3 Python 3.6.8 (default, Apr 2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> Operators The Python interpreter can be used to evaluate expressions, for example simple arithmetic expressions. If you enter such expressions at the prompt (>>>) they will be evaluated and the result will be returned on the next line. >>> 1 + 1 2 >>> 2 * 3 6 Boolean operators also exist in Python to manipulate the primitive True and False values. >>> 1 == 0 False >>> not (1 == 0) True >>> (2 == 2) and (2 == 3) False >>> (2 == 2) or (2 == 3) True Strings Like Java, Python has a built in string type. The + operator is overloaded to do string concatenation on string values. >>> 'artificial' + "intelligence" 'artificialintelligence' There are many built-in methods which allow you to manipulate strings. >>> 'artificial'.upper() 'ARTIFICIAL' >>> 'HELP'.lower() 'help' >>> len('Help') 4 Notice that we can use either single quotes ' ' or double quotes " " to surround string. This allows for easy nesting of strings. We can also store expressions into variables. >>> s = 'hello world' >>> print(s) hello world >>> s.upper() 'HELLO WORLD' >>> len(s.upper()) 11 >>> num = 8.0 >>> num += 2.5 >>> print(num) 10.5 In Python, you do not have declare variables before you assign to them. Exercise: Dir and Help Learn about the methods Python provides for strings. To see what methods Python provides for a datatype, use the dir and help commands: >>> s = 'abc' >>> dir(s) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] >>> help(s.find) Help on built-in function find: find(...) method of builtins.str instance S.find(sub[, start[, end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. >>> s.find('b') 1 Try out some of the string functions listed in dir (ignore those with underscores ‘_’ around the method name). Note: Ignore functions with underscores “_” around the names; these are private helper methods. Press ‘q’ to back out of a help screen. Built-in Data Structures Python comes equipped with some useful built-in data structures, broadly similar to Java’s collections package. Lists Lists store a sequence of mutable items: >>> fruits = ['apple', 'orange', 'pear', 'banana'] >>> fruits[0] 'apple' We can use the + operator to do list concatenation: >>> otherFruits = ['kiwi', 'strawberry'] >>> fruits + otherFruits >>> ['apple', 'orange', 'pear', 'banana', 'kiwi', 'strawberry'] Python also allows negative-indexing from the back of the list. For instance, fruits[-1] will access the last element 'banana': >>> fruits[-2] 'pear' >>> fruits.pop() 'banana' >>> fruits ['apple', 'orange', 'pear'] >>> fruits.append('grapefruit') >>> fruits ['apple', 'orange', 'pear', 'grapefruit'] >>> fruits[-1] = 'pineapple' >>> fruits ['apple', 'orange', 'pear', 'pineapple'] We can also index multiple adjacent elements using the slice operator. For instance, fruits[1:3], returns a list containing the elements at position 1 and 2. In general fruits[start:stop] will get the elements in start, start+1, ..., stop-1. We can also do fruits[start:] which returns all elements starting from the start index. Also fruits[:end] will return all elements before the element at position end: >>> fruits[0:2] ['apple', 'orange'] >>> fruits[:3] ['apple', 'orange', 'pear'] >>> fruits[2:] ['pear', 'pineapple'] >>> len(fruits) 4 The items stored in lists can be any Python data type. So for instance we can have lists of lists: >>> lstOfLsts = [['a', 'b', 'c'], [1, 2, 3], ['one', 'two', 'three']] >>> lstOfLsts[1][2] 3 >>> lstOfLsts[0].pop() 'c' >>> lstOfLsts [['a', 'b'], [1, 2, 3], ['one', 'two', 'three']] Exercise: Lists Play with some of the list functions. You can find the methods you can call on an object via the dir and get information about them via the help command: >>> dir(list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> help(list.reverse) Help on built-in function reverse: reverse(...) L.reverse() -- reverse \*IN PLACE\* >>> lst = ['a', 'b', 'c'] >>> lst.reverse() >>> ['c', 'b', 'a']` Tuples A data structure similar to the list is the tuple, which is like a list except that it is immutable once it is created (i.e. you cannot change its content once created). Note that tuples are surrounded with parentheses while lists have square brackets. >>> pair = (3, 5) >>> pair[0] 3 >>> x, y = pair >>> x 3 >>> y 5 >>> pair[1] = 6 TypeError: object does not support item assignment The attempt to modify an immutable structure raised an exception. Exceptions indicate errors: index out of bounds errors, type errors, and so on will all report exceptions in this way. Sets A set is another data structure that serves as an unordered list with no duplicate items. Below, we show how to create a set: >>> shapes = ['circle', 'square', 'triangle', 'circle'] >>> setOfShapes = set(shapes) Another way of creating a set is shown below: >>> setOfShapes = {‘circle’, ‘square’, ‘triangle’, ‘circle’} Next, we show how to add things to the set, test if an item is in the set, and perform common set operations (difference, intersection, union): >>> setOfShapes set(['circle', 'square', 'triangle']) >>> setOfShapes.add('polygon') >>> setOfShapes set(['circle', 'square', 'triangle', 'polygon']) >>> 'circle' in setOfShapes True >>> 'rhombus' in setOfShapes False >>> favoriteShapes = ['circle', 'triangle', 'hexagon'] >>> setOfFavoriteShapes = set(favoriteShapes) >>> setOfShapes - setOfFavoriteShapes set(['square', 'polygon']) >>> setOfShapes & setOfFavoriteShapes set(['circle', 'triangle']) >>> setOfShapes | setOfFavoriteShapes set(['circle', 'square', 'triangle', 'polygon', 'hexagon']) Note that the objects in the set are unordered; you cannot assume that their traversal or print order will be the same across machines! Dictionaries The last built-in data structure is the dictionary which stores a map from one type of object (the key) to another (the value). The key must be an immutable type (string, number, or tuple). The value can be any Python data type. Note: In the example below, the printed order of the keys returned by Python could be different than shown below. The reason is that unlike lists which have a fixed ordering, a dictionary is simply a hash table for which there is no fixed ordering of the keys (like HashMaps in Java). The order of the keys depends on how exactly the hashing algorithm maps keys to buckets, and will usually seem arbitrary. Your code should not rely on key ordering, and you should not be surprised if even a small modification to how your code uses a dictionary results in a new key ordering. >>> studentIds = {'knuth': 42.0, 'turing': 56.0, 'nash': 92.0} >>> studentIds['turing'] 56.0 >>> studentIds['nash'] = 'ninety-two' >>> studentIds {'knuth': 42.0, 'turing': 56.0, 'nash': 'ninety-two'} >>> del studentIds['knuth'] >>> studentIds {'turing': 56.0, 'nash': 'ninety-two'} >>> studentIds['knuth'] = [42.0, 'forty-two'] >>> studentIds {'knuth': [42.0, 'forty-two'], 'turing': 56.0, 'nash': 'ninety-two'} >>> studentIds.keys() ['knuth', 'turing', 'nash'] >>> studentIds.values() [[42.0, 'forty-two'], 56.0, 'ninety-two'] >>> studentIds.items() [('knuth', [42.0, 'forty-two']), ('turing',56.0), ('nash', 'ninety-two')] >>> len(studentIds) 3 As with nested lists, you can
Answered 1 days AfterJan 08, 2022

Answer To: Python Intro The goal of this exercise is to get familiar with python and autograder. The projects...

Chirag answered on Jan 09 2022
115 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here