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

  Define a function to calculate the values

Be sure to define a function to calculate the values and print the table displaying the interest rate in the first column, the monthly payment in the second column, and the total payment in the third column. Your program should not allow the user to ..

  Takes a directory containing files that record

Create a program called grading.py that takes a directory containing files that record how student's performed on their assignments and from these files determines each students grades in addition to the course statistics.

  Topic of effective and ethical communication

Design an algorithm and use it to write a Python program that reads the contents of the data file into a list. The program should then loop, to allow a user to check various numbers against those stored in the list.

  Without using the system function to call any bash commands

without using the system function to call any bash commands write a python program that will implement a simple version

  Given the strings s1 and s2 that are of the same length

Given the strings s1 and s2 that are of the same length, create a new string consisting of the first character of s1 followed by the first character of s2, followed by the second character of s1, followed by the second character of s2, and so on (in ..

  Calculate the total displacement of the system of springs

Calculate the total displacement of the system of springs - You are free to use any linear system solver from chapter 6, including the solvers that are part of the SciPy and/or numpy packages.

  The dictionary order based on the ascii order

Needless to say, the dictionary order based on the ASCII order is not what a real-world indexing software wants. So, we want to implement the dictionary order of strings in the standard wa

  Reinforce topic material on simple functions

Select 3 sets of test data that will demonstrate the correct 'normal' operation of your program. Select another 2 sets of test data that will demonstrate the "abnormal" operation of your program.

  Specializes in solving the equations ax=b by doolittle''s dec

Write a  program that specializes in solving the equations Ax=b by Doolittle's decomposition method, where a is the HIbber matrix of arbitrary size nxn

  Generate a plot with a point for every charging station

After you have imported your data, you should use matplotlib to generate a plot with a point for every charging station. Note that the first column of data is longitude, i.e., the y-values, and the second column is latitude, i.e., the x-values

  Explain a python program storing and processing simple bank

A Python program storing and processing simple bank records is in the early stages of development. The records are stored in a text file (bank.txt) that contains, for each bank customer, their given name, account number and balance.

  Write an expression that concatenates the string

Write an expression that concatenates the String variable suffix onto the end of the String variable prefix .

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