Ask Python Expert

Please complete Lab A and B during today's session.

Lab A and B File Requirements:

Lab A

Lab B

LabA_chk1.py

LabB_chk1.py

LabA_chk2.py

LabB_chk2.py

LabA_chk3.py

LabB_chk3.py

Lab A Overview

This lab explores use of lists and logic for analyzing real data provided by Yelp for restaurants near RPI. You will use a module that will help you with parsing files. We will learn about files very soon, but feel free to look at the code and ask about what it is doing during the lab.

To get started, please create a folder in your Dropbox for Lab 3 and download the lab03_util.py and yelp.txt files from the course website into this new folder. The Python file is a module for reading the second (text input) file. First, take a look at the lab03_util.py file. You can see it has functions for parsing the file, but no code to call these functions. That is a simple way to think of a module. We can use these files in our code to simplify some tasks.

Checkpoint 1

Let's use the given module to read this file into a list. Create a new file called check1.py in the same folder as the files from the zip folder and include the following code inside:

import lab03_util
restaurants = lab03_util.read_yelp(‘yelp.txt')

Use a few print statements to see the contents of the list. As the list is large, let's look at the first element: print restaurants[0]

We will get:

["Mekas Lounge", 42.74, -73.69, \ ‘407 River Street+Troy, NY 12180',\
‘http://www.yelp.com/biz/mekas-lounge-troy', \ ‘Bars', \
[5, 2, 4, 4, 3, 4, 5]]

The variable restaurants contains a list. Each element of this list corresponds to a specific restaurant.

From the above example, we see that the first element of the restaurants list is also a list. The information provided for each restaurant is: name, latitude, longitude, street address, URL, the restaurant category, and a list of scores given by Yelp users. Yes, the last element here is yet another list!

Your job in the first checkpoint is to print information for a single restaurant by writing a function called print_info(). Below is the result of printing the first two restaurants in the list:

Mekas Lounge (Bars)

407 River Street
Troy, NY 12180

Average Score: 3.86

Tosca Grille (American (New)) 200 Broadway

Troy, NY 12180
Average Score: 2.50

The first line shows the name and the category in parentheses. The second and third lines both come from the address (note the use of the TAB here). The final line is the average score, obtained by taking the average of the last entry in the restaurant. How do we split the address into two lines? There is a very useful function called split() that splits a string into a list based on a given delimiter. For example:

>>> title = "The,Old,Man,and,the,Sea"
>>> title.split( "," ) [
‘The', ‘Old', ‘Man', ‘and', ‘the', ‘Sea']
>>> title = "The!Old!Man!and!the!Sea"
>>> title.split( "!" )

[‘The', ‘Old', ‘Man', ‘and', ‘the', ‘Sea']

To get you started, here is the basic organization for printing just the name of a restaurant. import lab03_util

def print_info( restaurant ): print restaurant[0]

####### main code starts here
restaurants = lab03_util.read_yelp( ‘yelp.txt' ) print_info( restaurants[0] )

To complete Checkpoint 1, show a mentor the code and the output. Checkpoint 2

Copy check1.py to check2.py and continue to work on check2.py. Modify your code to ask the user for the id of a restaurant between 1 and 155 (humans don't need to know about list ids starting at 0). Assume the user enters a number. If the user enters a value outside of the range 1-155, print a warning and do nothing else.

If the user entered a valid index, print the information for the restaurant corresponding to this index (remember that index 1 corresponds to list index 0). Test your code well to make sure that you only print a restaurant for a valid index.

The second task in this part is to improve on the print function by changing the average score computation. Given the scores for a list, drop the max and the min, calculating the average of the rest. Note that you do not actually have to explicitly remove the max, min, just subtract them from the sum.

Given this average of the remaining scores, print one of the following based on the score:

Score

Output

0 to 2

This restaurant is rated bad, based on x reviews.

2 up to 3

This restaurant is rated average, based on x reviews

3 up to 4

This restaurant is rated above average, based on x reviews.

4 up to 5

This restaurant is rated very good, based on x reviews.

Note that x is the real number of reviews for this restaurant that are used in calculating the average. Beware! It does not make sense to remove max and min if there are less than three reviews for a restaurant. In that case, we should use the average of all the values (another if statement!).

To complete Checkpoint 2, show a mentor the code and the output. Please check to make sure your code follows the structure we require: first imports, then functions, then the actual code. Test your code with values 8, 22, 33, 44, and other valid and invalid values.

Checkpoint 3

Copy check2.py to check3.py and continue to work on check3.py. We will add a final flair in this part to your program from part 2.

Your program should work exactly as it did in part 2. After printing the restaurant info, ask the user the following:

What would you like to do next? 1. Visit the homepage 2. Show on Google Maps 3. Show directions to this restaurant Your choice (1-3)? ==> For all of these options, using formatted strings will really simplify your life!

If the user answers 1, then open a browser window using the following command (but use the URL for the business instead of this address). Remember to import module webbrowser first of course.

webbrowser.open( ‘http://xkcd.com/1319/' )

If the user answers 2, then open a browser window with Google maps showing the address of the business. webbrowser.open( ‘http://www.google.com/maps/place/business-address-goes-here' )

If the user answers 3, then open a browser window with Google maps with the address of the business and Rensselaer. Here is an example call:

webbrowser.open( ‘http://www.google.com/maps/dir/business-address/rpi-address' ) For example, to find the location of Rensselaer, you can use the following call: webbrowser.open( http://www.google.com/maps/place/110 8th Street Troy NY 12180 ) Luckily, Google can handle spaces or even pluses in an address.

If the user answers anything else, your program does nothing.

To complete Checkpoint 3, show a mentor the code and the output.

Note. This lab had a lot of different components. As a result, it can really benefit from structuring your code in an easy way so that you can quickly modify and improve it. Take the time to show your code to your TAs and mentors, and also work with them in restructuring it.

This will be crucial in the future when we write (even) longer code. It helps to design functions to do simple and very focused things only, for example just printing information regarding a specific restaurant at a certain index.

Lab B Overview

In this lab, you will write a series of short Python programs to manipulate strings, read input from the user, and display output. Start by making a folder for Lab 4 in your Dropbox where you keep your Computer Science 1 material, then start working on the following three checkpoints.

Checkpoint 1: Functions and Framing Four-Letter Words

Create a new program, check1.py, and open it in the WingIDE. There are two parts to this checkpoint:

• Write a function called framed() that creates a text frame around a given word. This function should accept a single string as an argument. Add code to call your function and test it, e.g.

framed( 'CSCI' ) or framed( 'darn' )

• Next, add code to use the raw_input() function to read a four-letter word into a string. Verify that the string contains exactly four characters, then pass this string to the framed() function you just wrote. The output when you run your program should look like this:
Enter a four-letter word: CSCI

**********
** CSCI **
**********

When the user enters invalid input, your program should look like this:

Enter a four-letter word: bananas
ERROR: 'bananas' is not a four-letter word.

When you have this working, show it to a mentor. Make sure your function follows the program structure we discussed and required in the homeworks. Congratulations, you have completed Checkpoint 1.

Checkpoint 2: Framing Other Words

Be sure you save check1.py and make a copy of it called check2.py. You will modify this for Checkpoint 2. Requiring the user to type a word that is exactly four letters is limiting, so let's expand your program to accept strings of different lengths. More specifically, allow the user to enter a string containing at least four characters and at most twelve characters (otherwise, display the error message shown below).

Also, have the user input the character to use for the border. If the user enters multiple characters, use the first character entered (and display a warning message). Example program runs are shown below:

Enter a word: CSCI

Enter border character: *
**********
** CSCI **
********** or

Enter a word: fiddlesticks Enter border character: #
##################
## fiddlesticks ##
##################

or

Enter a word: huh ERROR: 'huh' is too short.

or

Enter a word: arghhhhhhhhhhhhhhhhhh ERROR: 'arghhhhhhhhhhhhhhhhhh' is too long.

or
Enter a word: fiddlesticks Enter border character: $*#

WARNING: '$*#' is too long, using '

for border
$$$$$$$$$
$ fiddlesticks $
$$$$$$$$$

When you have this working, show it to a mentor. Congratulations, you have completed Checkpoint 2.
Checkpoint 3: Letter Counting

In this last checkpoint, you will write a new program that determines the number of occurrences of a given letter in a string. To do so, you are only allowed to use the replace() and len() functions.

Example program runs are shown below:

Enter a sentence: the quick brown fox jumped over the lazy dog Enter character: t

The letter 't' appears in the sentence exactly 2 times

or

Enter a sentence: the quick brown fox jumped over the lazy dog Enter character: x The letter 'x' appears in the sentence exactly 1 time
or

Enter a sentence: the quick brown fox jumped over the lazy dog Enter character: s The letter 's' appears in the sentence exactly 0 times

or

Enter a sentence:

the quick brown fox jumped over the lazy dog Enter character: ? The letter '?' appears in the sentence exactly 0 times

When you have your program fully working, save your code, and then show the result to a mentor. Congratulations, you have finished Checkpoint 3 and are all done with Lab 4.

Attachment:- Lab_03_04.zip

Python, Programming

  • Category:- Python
  • Reference No.:- M91904540

Have any Question?


Related Questions in Python

Part i the assignment filesone of the most important

Part I: The Assignment Files One of the most important outcomes of this assignment is that you understand the importance of testing. This assignment will follow an iterative development cycle. That means you will write a ...

Homework -this homework will have both a short written and

Homework - This homework will have, both a short written and coding assignment. The problems that are supposed to be written are clearly marked. 1) (Written) Make heuristics Describe two heuristics for the slide problem ...

Tasksdemonstrate data scraping of a social network of

Tasks Demonstrate data scraping of a social network of choice. Develop technical documentation, including the development of the code & detailing the results. Provide a report on the findings, that includes research into ...

Assignment1 utilising python 3 build the following

Assignment 1. Utilising Python 3 Build the following regression models: - Decision Tree - Gradient Boosted Tree - Linear regression 2. Select a dataset (other than the example dataset given in section 3) and apply the De ...

Python programming assignment -you first need an abstract

Python Programming Assignment - You first need an abstract base class, called, Account which has the following attributes and methods: accountID: This attribute holds the ID assigned the account , if not provided set to ...

Learning outcomes lo3 - research develop and document a

Learning Outcomes LO3 - Research, develop, and document a basic security policy, and analyse, record, and resolve all security incidents LO4 - Identify and assess the threats to, and vulnerabilities of networks Assessmen ...

Question research pythons dictionary data type dictdiscuss

Question : Research Python's dictionary data type (dict). Discuss its interface and usage. Include examples. Discuss practical applications of dictionaries.

Questionwhat is a python development frameworkgive 3

Question What is a python development framework? Give 3 examples python development framework used today. and explain which development framework is used in which industry.

Below zero - ice cream storethe local ice-cream store needs

Below Zero - ice cream store The local ice-cream store needs a new ordering system to improve customer service by streamlining the ordering process. The manager of the store has found that many orders are incorrect and s ...

The second task in this assignment is to create a python

The second task in this assignment is to create a Python program called pancakes.py that will determine the final order of a stack of pancakes after a series of flips.(PYTHON 3) Problem Task In this problem, your input w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As