p14: Sum of Positive and Negative Numbers

Write a program that reads in X whole numbers and outputs (1) the sum of all positive numbers, (2) the sum of all negative numbers, and (3) the sum of all positive and negative numbers. The user can enter the X numbers in any different order every time, and can repeat the program if desired.

In order to receive credit, you may not use python built-in function sum().

=== Sample Run ===
How many numbers would you like to enter? 4
Please enter number 1: 3
Please enter number 2: -4
Please enter number 3: -6
Please enter number 4: 5
------
The sum of negative numbers = -10
The sum of positive numbers = 8
The sum of all numbers = -2

Would you like to repeat (y/n)? n

p24: CodingBat Strings

Complete the following codingbat exercises:

1 string_times
2 extra_end
3 make_tags
4 combo_string
5 count_code
6 xyz_there

p25: File Write and Read

1) User enters a file name (such as "myMovies.txt").

2) User enters the titles of 4 of their favorite moveis (use a loop!).

3) Program Writes the 4 titles to a file, one per line, then closes the file (use a loop!).

4) Program Reads the 4 titles from "myMovies.txt" stores them in a list and shows the list

5) The program writes the titles in reverse order into a file "reverseOrder.txt"

Sample Run:
Please enter a file name: myMovies.txt
Please enter a movie title #1: movie1
Please enter a movie title #2: movie2
Please enter a movie title #3: movie3
Please enter a movie title #4: movie4
Writing the 4 movie titles to file 'myMovies.txt'
Reading the 4 movie titles from file into a list: [movie1, movie2, movie3, movie4]
Writing the 4 movie titles in revers to 'reverseOrder.txt'
Note: Do not use reverse() , reversed()
Content of myMovies.txt:
movie1
movie2
movie3
movie4
Content of reverseOrder.txt
movie4
movie3
movie2
movie1

p26: Split a Sentence and Count Words

Write a program that asks the user to enter a sentence, and then write it into a file. Your program will

1) Read the contents of the file and count how many words are in the file,

2) show the last word in the file

3) ask the user to enter their own word, and count how many times their word appears in the file

Sample Run:
Please enter a sentence: The fox and the dog
There are 5 words in the sentence you entered
The last word is 'dog'
Please enter a word to search: the
The word 'the' appears 2 times

p27: Sunspots

Write a program which reads the data from sunspots.txt : see image.

...and computes the average for each year, writing them to a file averages.txt (shown below) one per line: see image.

Hints:
# Open the File to Read
myFile = open('sunspots.txt', 'r')
# Read the data from file into a list
listOfLines = myFile.read().splitlines()
print(listOfLines)
# Each element is a new line from the file
listOfElementsInOneLine = listOfLines[0].split() # split each line by spaces
print(listOfElementsInOneLine) # shows list of strings ['1945','18.5','11.8',...,'28.4']
# Convert each of the strings to float in order to do math with them!

p28: Data Mining Project

Data mining is the process of sorting through large amounts of data and picking out relevant information. It is used by business/government intelligence organizations, and financial analysts, and is increasingly being used in the sciences to extract information from the enormous data sets generated by modern experimental and observational methods.

You have a file table.csv, which is a "comma separated" data file with Google stock prices.

A csv file stands for "comma separated values".

This means that all values in the file are separated by comma.

If you were to open table.csv it in notepad++ it looks like: see image.

If you open table.csv file in Excel, it looks like this: see image.

Write a python program to calculate the average for each month of every year, using the template below:

#name: get_data_list
#param: FILE_NAME - the file's name you saved for the stock's prices
#brief: get a list of the stock's records' lists
#return: a list of lists
def get_data_list (FILE_NAME):
return data_list


#name: get_monthly_averages
#param: data_list - the list that you will process
#brief: get a list of the stock's monthly averages and their corresponding dates
#return: a list
def get_monthly_averages (data_list):
return monthly_average_list

#name: print_info
#param: monthly_average_list - the list that you will process
#brief: print the monthly averages of Google stock
#return: None

def print_info (monthly_average_list):
# show monthly averages of Google stock

# 1) call get_data_list(FILE_NAME) function to get the data list.
# Return into variable data_list

# 2) call get_monthly_averages(data_list) function with variable data_list from above as argument.
# Return into variable monthly_average_list

# 3) call print_info(monthly_average_list) function with variable monthly_average_list from above as argument.
# Show the monthly_average_list as shown in the sample run below

You will calculate the averages for every month, store them in a list, and then show the list to the user:

Sample Run:
monthly_average_list[ 0] = ['Month', 'Year', 'Average']
monthly_average_list[ 1] = ['09', '2008', '437.70']
monthly_average_list[ 2] = ['08', '2008', '485.91']
monthly_average_list[ 3] = ['07', '2008', '510.03']
monthly_average_list[ 4] = ['06', '2008', '556.32']
monthly_average_list[ 5] = ['05', '2008', '575.92']
monthly_average_list[ 6] = ['04', '2008', '497.58']
monthly_average_list[ 7] = ['03', '2008', '440.33']
monthly_average_list[ 8] = ['02', '2008', '503.80']
monthly_average_list[ 9] = ['01', '2008', '611.81']
monthly_average_list[10] = ['12', '2007', '695.40']
monthly_average_list[11] = ['11', '2007', '676.37']
monthly_average_list[12] = ['10', '2007', '635.39']
monthly_average_list[13] = ['09', '2007', '540.43']
monthly_average_list[14] = ['08', '2007', '509.83']
monthly_average_list[15] = ['07', '2007', '532.48']
monthly_average_list[16] = ['06', '2007', '515.02']
monthly_average_list[17] = ['05', '2007', '473.01']
monthly_average_list[18] = ['04', '2007', '472.50']
monthly_average_list[19] = ['03', '2007', '452.91']
monthly_average_list[20] = ['02', '2007', '467.22']
monthly_average_list[21] = ['01', '2007', '490.58']
monthly_average_list[22] = ['12', '2006', '473.50']
monthly_average_list[23] = ['11', '2006', '485.63']
monthly_average_list[24] = ['10', '2006', '440.53']
monthly_average_list[25] = ['09', '2006', '397.06']
monthly_average_list[26] = ['08', '2006', '377.09']
monthly_average_list[27] = ['07', '2006', '403.53']
monthly_average_list[28] = ['06', '2006', '393.59']
monthly_average_list[29] = ['05', '2006', '383.80']
monthly_average_list[30] = ['04', '2006', '413.78']
monthly_average_list[31] = ['03', '2006', '358.87']
monthly_average_list[32] = ['02', '2006', '370.00']
monthly_average_list[33] = ['01', '2006', '445.71']
monthly_average_list[34] = ['12', '2005', '418.95']
monthly_average_list[35] = ['11', '2005', '399.14']
monthly_average_list[36] = ['10', '2005', '322.47']
monthly_average_list[37] = ['09', '2005', '304.24']
monthly_average_list[38] = ['08', '2005', '286.92']
monthly_average_list[39] = ['07', '2005', '298.21']
monthly_average_list[40] = ['06', '2005', '287.55']
monthly_average_list[41] = ['05', '2005', '239.71']
monthly_average_list[42] = ['04', '2005', '199.21']
monthly_average_list[43] = ['03', '2005', '181.16']
monthly_average_list[44] = ['02', '2005', '195.01']
monthly_average_list[45] = ['01', '2005', '192.85']
monthly_average_list[46] = ['12', '2004', '181.77']
monthly_average_list[47] = ['11', '2004', '177.50']
monthly_average_list[48] = ['10', '2004', '153.23']
monthly_average_list[49] = ['09', '2004', '113.23']
monthly_average_list[50] = ['08', '2004', '105.26']

p30: Protein Weight

In a weighted alphabet, every symbol is assigned a positive real number called a weight. A string formed from a weighted alphabet is called a weighted string, and its weight is equal to the sum of the weights of its symbols.

The standard weight assigned to each member of the 20-symbol amino acid alphabet is the monoisotopic mass of the corresponding amino acid.

1) The mass of each possible amino acid is given in the file aa.txt see image.

- Read the above file and put its contents into a dictionary using the format: dictionary [ 'Letter' ] = value

2) Ask the user to enter an amino acid string consisting of only the letters shown in file aa.txt - if the user enters an incorrect letter, then the program asks for another string

3) Calculate the total weight of the amino acid

a) use characters of the string as keys for the dictionary from (1)

b) sum the weights for all letters and show the total weight

p31: Read a Web Page, Regex

# readAWebPage.py
# Write a Python program which:
# Connects to a URL "http://www.gavilan.edu/dir/" (see examplesPageRead.py)
# WRITES all the html into a text file (dataFile.txt) as a string,
# Searches the text file and makes a listOfEmails, (see regexExamples.py)
# Makes a nonRepeatListOfEmails
# Writes those emails to a text file (emails.txt), one per line "Email # : someone@gavilan.edu"
# Close the above file
# Reopen the above file to READ and search it for 'stoykov' (see examplesPageRead.py)
# Print the line where you found that email
# "Line where email found: Email 19 : stoykov@gavilan.edu"
# NOTE: see examplesPageRead.py for file Input and Output

Including the above comments, the program takes 48 lines of code. Your program will generate 2 text files. dataFile.txt, and emails.txt. I don't need them, but verify that they look like what you see below.

emails.txt:
Email 0: webmaster@gavilan.edu
Email 1: mabbass@gavilan.edu
Email 2: dachterman@gavilan.edu
Email 3: jadams@gavilan.edu
Email 4: taddison@gavilan.edu
..
..
..
Email 250: dmunoz@gavilan.edu
Email 251: dmuscari@gavilan.edu
Email 252: nnaranjo@gavilan.edu
Email 253: jnari@gavilan.edu
Email 254: enelson@gavilan.edu
Email 255: tnewman@gavilan.edu
...

dataFile.txt: (should be many lines long, and filled with what you see)

Program Output (Line number can change, if people get added or removed to the list over time:

Line where email found: Email 16 : carballo@gavilan.edu
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.