Determines number of occurrences of a given letter in string

Assignment Help Python Programming
Reference no: EM131149075

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',\
‘https://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( ‘https://xkcd.com/1319/' )

If the user answers 2, then open a browser window with Google maps showing the address of the business. webbrowser.open( ‘https://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( ‘https://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( https://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

Reference no: EM131149075

Questions Cloud

Using an amortization schedule calculate his payoff amount : Let's say that the restaurant owner in Problem 4 above decides to go with the amortized loan option and after having paid 2 payments decides to pay off the balance. Using an amortization schedule calculate his payoff amount.
What is the serving size stated on the label : What is the "serving size" stated on the label? Does this serving size match the portion you typically consume of this food in one sitting? Is it bigger or smaller than what you typically consume?
Prolog program to implement the russian multiplication : Write a Prolog procedure to split a given list into two separate lists, one containing integers and the other one real numbers. All other items in the given list should be ignored.
Under which of the three options will the owner pay : Under which of the three options will the owner pay the least interest and why
Determines number of occurrences of a given letter in string : 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.
What is your intuition about the validity : Who are the key "players" in the politics of the world's fisheries? In other words, who has a stake in fisheries?  What is your intuition about the validity or "truth" of the viewpoint expressed in the article
What is the cost of the equipment that she purchased today : Jane Bryant has just purchased some equipment for her beauty salon. She plans to pay the following amounts at the end of the next five years: $8,250, $8,500, $8,750, $9,000, and $10,500. If she uses a discount rate of 10 percent, what is the cost of ..
How much money will she have accumulated in her account : If Mary deposits $4000 a year for three years, starting a year from today, followed by 3 annual deposits of $5000, into an account that earns 8% per year, how much money will she have accumulated in her account at the end of 10 years?
What food color group do tend to get most servings from : The goal is to eat TWO ½ cup servings from each group EVERY day. Did you accomplish that on any day? If so, how many days? What food color group do you tend to get the most servings from?

Reviews

Write a Review

Python Programming Questions & Answers

  Project will be a simple, working program

This programming project will be a simple, working program, using Python language, which utilizes a good design process and includes:Sequential, selection, and repetitive programming statements as well as,At least one function call.

  The program should allow the student

The program should allow the student to enter the answer. If the answer is correct, a message of congratulations should be displayed. If the answer is incorrect, a message showing the correct answer should be displayed.

  Data file is a comma separated

The data file is a comma separated text values stored in a file with '.CSV' extension. The file has five columns corresponding to employee data fields listed above.

  Determine which value is on the most dice

Determine which value is on the most dice, and set those dice aside so they won't be re-rolled. Repeat steps (2) and (3) until you're out of rolls (in Yahtzee, it's a maximum of 3 rolls)

  Write a program to that displays a table

Write a program to that displays a table of the Celsius temperatures 0 through 20 and their Fahrenheit and Kelvin equivalents

  Create functions to simplify your code

Create functions to simplify your code. If you find yourself writing the same code over and over again, it should probably be made into a function.

  Disabled individuals open jars and containers

What variables are you going to need and what will be their datatype and how would you break this problem into numerous smaller units?

  Write python program isosceles tri equilateral tri rectangle

Write python program Isosceles Tri Equilateral Tri Rectangle, Write another "driver" script called project1.py which imports the polygon.py module, reads an input file of polygonal data and writes another file of areas and perimeters.

  Write a program using the ''requetinteger''

using python/jython programming write a program using the 'requetInteger' function that will ask the user to type a value that will draw a line from one point on a picture to another. I don't need specific help just a gerneral idea.

  Develop and test a python program

Develop and test a Python program that displays the day of the week that the following holidays fall on for a year entered by the user New Year's Eve

  Assume that the variables gpa , deanslist and studentname

Assume that the variables gpa , deansList and studentName , have been initialized. Write a statement that adds 1 to deansList and prints studentName to standard out if gpa exceeds 3.5.

  Assignment work in python

Write a function interleave two pictures take the first 20 pixels from the first picture and then 20 pixels from the second picture and then the next 20 pixels from the first picture and then the next 20 pixels from the second picture and so on till ..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd