Question

In: Computer Science

1. Adhere to the naming conventions discussed in class for variable names, program names, and function...

1. Adhere to the naming conventions discussed in class for variable names, program names, and function names

2. Use meaningful names for variables

3. Follow the coding standards and homework submission rules as covered in class.

4. If there are more than one word in the variable use all lowercase and separate them with underscores, e.g., first_name, shipping_cost.

5. Include appropriate comments in the code, but not too many.

6. Output should be displayed exactly as shown for each problem below. The output given is sample output. Other numbers/input could be entered.

7. If your program does not compile, you will not receive more than 20% of the maximum points.

8. If your program compiles but does not execute, you will not receive more than 35% of the maximum points.

9. If your program compiles and executes yet does not give the required output, you will not receive more than 50% of the maximum points.

10. The programs will be in one file named hw3.py. The module will be in one file named number_functions.py. Zip hw3.py and number_functions.py into file hw3.zip and upload to Canvas. Only .zip files will be accepted. If the file is not named correctly, 10 points will be deducted.

11. Each program will be separated by a comment with the problem number. If this format is not followed, 10 points will be deducted.

12. Your name will be a comment at the top of the file. If your name is not in the file as a comment, 10 points will be deducted.

13. If you use Python constructs on assignments that we have not yet discussed in class, 20 points will be deducted.

14. If you do not adhere to our coding standards, 20 points will be deducted.

#Problem 2

Write function write_stock that asks the user to input a company name, its associated ticker symbol (eg. Exxon, XOM), and the price per share of the stock. These are then written to file stock.txt one item per line. The user will type in ‘quit’ to stop.

Write function get_share_value that accepts a company name and the number of shares. The function will search file stock.txt and return a list of two values; the ticker symbol and the total value of the shares. This function must use an exception handler to insure the file is valid, that the price can be converted to a number, and catch any other exception that might be raised.

Write a program that calls write_stock and get_share_value. The program will ask the user for the company name and number of shares for read_stock and print the answer.

Sample output might look as follows.

Enter company name; enter quit to stop: Amazon

Enter ticker symbol: amzn

Enter price: 1000

Enter company name; enter quit to stop: Apple

Enter ticker symbol: aapl

Enter price: 125

Enter company name; enter quit to stop: Microsoft

Enter ticker symbol: msft

Enter price: 250

Enter company name; enter quit to stop: quit

What stock did you buy?: Apple

How many shares?: 10

Your total value for aapl is $1250.00

#Problem 3

Write function determine_ sums that accepts a list of values. The function will return a list of two numbers; the first is the sum of the positive numbers and the second is the sum of the negative numbers.

Write a program that defines a list as [1, 5, 7, -2, 6, -8, 10, -4, -20] and calls determine_sums. The program will print the output. Assume the company name will start with a capital letter and the ticker symbol will be all lowercase. This will give the following output.

[29, -34]

There is no user input on this problem.

#Problem 4

Write function write_students that asks the user for student records and writes them to file students.txt. A record of a student includes name, major and GPA. The user can enter as many records as they want until they type in quit.

Write function read_students that accepts a major. The function returns the highest GPA for that major.

Write a program that calls write_students and read_students and prints the major and highest GPA. The program will ask the user for the major for which to search. Assume all names and majors will be lower case.

Sample input/output might look as follows:

Enter student name; enter quit to stop: jones

Enter major: insy

Enter gpa: 2.0

Enter student name; enter quit to stop: smith

Enter major: insy

Enter gpa: 3.25

Enter student name; enter quit to stop: willis

Enter major: mana

Enter gpa: 3.25

Enter student name; enter quit to stop: quit

Enter major: insy

The highest GPA for insy majors is 3.25

#Problem 5

Write module number_functions that includes:

Function write_numbers that writes 50 random numbers between 1 and 15 to file numbers.txt.

Function read_numbers that reads the file numbers.txt and returns a list of the numbers.

Write function count_numbers that accepts the list of numbers and returns how many numbers are in the list.

Function find_even_odd that accepts the list of numbers and returns a list of two numbers; how many odd numbers are in the list and how many even numbers are in the list.

Write a program that calls these functions in the order above and prints the returned data.

Sample output might be the following. But yours may vary as we are generating random numbers. The count will always be 50, but you need to code the function as if you do not know that.

['12', '8', '3', '18', '1', '5', '3', '18', '6', '10', '20', '8', '7', '4', '17', '16', '8', '11', '14', '3', '13', '13', '5', '17', '11', '9', '16', '19', '13', '18', '6', '1', '9', '19', '5', '14', '13', '15', '10', '12', '20', '16', '14', '5', '10', '20', '17', '11', '18', '3']

50

[26, 24]

Solutions

Expert Solution

# Problem 2
# function that asks the user to input a company name, its associated ticker symbol and the price per share of the stock.
# These are then written to file stock.txt one item per line. The user will type in ‘quit’ to stop.
def write_stock():
    # open file for writing
    f = open("stock.txt", "w")
    # loop to get inputs
    while(1):
        company_name = input("Enter company name; enter quit to stop: ")
        # if input is quit , exit loop
        if(company_name=="quit"):
            break
        # else read remaining data and write to file
        ticker_symbol = input("Enter ticker symbol: ")
        stock_price = float(input("Enter price: "))
        f.write(company_name+" ")
        f.write(ticker_symbol+" ")
        f.write(str(stock_price)+"\n")
    f.close()
  
# function that accepts a company name and the number of shares.
# The function will search file stock.txt and
# return a list of two values; the ticker symbol and the total value of the shares.
def get_share_value(company_name, share_count):
    output_list = []
    try:
        f = open("stock.txt", "r")
        for x in f:
            data = x.split(" ")
            if(data[0]==company_name):
                output_list.append(data[1])
                value = float(data[2])*share_count
                output_list.append(value)
                return output_list
    except:
        print("Error occured while reading file")
    return output_list
  
#Problem 3
# function that accepts a list of values.
# return a list of two numbers; the first is the sum of the positive numbers and the second is the sum of the negative numbers
def determine_sums(list):
    negative_sum = 0;
    positive_sum = 0;
    output_list = []
    for number in list:
        if number<0 :
            negative_sum = negative_sum + number
        elif number>0:
            positive_sum = positive_sum + number
    output_list.append(positive_sum)
    output_list.append(negative_sum)
    return output_list
  
write_stock()
company_name = input("What stock did you buy?: ")
share_count = int(input("How many shares?: "))
data = get_share_value(company_name, share_count)
if len(data)==0:
    print("No such company found")
else:
    print("Your total value for", data[0],"is ", data[1],"\n")

number_list = [1, 5, 7, -2, 6, -8, 10, -4, -20]
sums = determine_sums(number_list)
print(sums)


screenshot of program for referencing indentation

OUTPUT

Completed Problem 2 and 3.

Post a new question for 4 and 5.


Related Solutions

Please focus on Nurse Practitioners 1. Discuss the controversy surrounding the naming conventions for APRNs (example...
Please focus on Nurse Practitioners 1. Discuss the controversy surrounding the naming conventions for APRNs (example DNP prepared nurse practitioners using the title "doctor")
Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment...
Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment Create a class GradesGraph that represents a grade distribution for a given course. Write methods to perform the following tasks: • Set the number of each of the letter grades A, B, C, D, and F. • Read the number of each of the letter grades A, B, C, D, and F. • Return the total number of grades. • Return the percentage of...
Using LIST and FUNCTION Write a program in Python that asks for the names of three...
Using LIST and FUNCTION Write a program in Python that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
This class will model a class variable to hold a mathematical function of up to 10 terms.
// Complete the implementation of the following class.// This class will model a class variable to hold a mathematical function of up to 10 terms.// The function will have one independent variable.// Terms that the function will model are:// Monomials of the form: 5.3x^0.5// Trigonometric terms sin(x), cos(x), tan(x) of the form: 1.2cos(3.0x)//public class Function { // PLEASE leave your class name as Function   // CLASS VARIABLES     // use any collection of variables as you see fit.     // CONSTRUCTORS   //...
Please describe the overall mechanism for DNA polymerase function as discussed in class. What is the...
Please describe the overall mechanism for DNA polymerase function as discussed in class. What is the role of conformational changes in insuring substrate specificity? Do DNA polymerases share a universally conserved mechanism?
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.
Regarding the employee stock option right to buy program discussed in class, what is the correct...
Regarding the employee stock option right to buy program discussed in class, what is the correct answer in the following example. An employee receives 100 shares of stock at $20. A year later the employee gets another 100 shares at $35 and a year later they receive another100 shares at $35. At some point after that the employee decides to sell all the shares at a $50 price when the stock rises to $50. How much will they personally profit?...
1) Describe the structure and function of a sphincter. State the names and locations of the...
1) Describe the structure and function of a sphincter. State the names and locations of the sphincter in GI tract 2) what are the functions of the following a) mesentery b)mesocolon c)falciform ligament d)greater omentum
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
Consider a firm with the additive production function we discussed in class: f(K, L) = 2√...
Consider a firm with the additive production function we discussed in class: f(K, L) = 2√ L + 2√ K. (a) Derive the firm’s long run demand curve for labor, as well as the firm’s long run demand curve for capital. (b) Notice that the firm’s long run labor and capital demand curves do not exhibit any substitution effects. That is, if the price of labor increases, the firm’s use of capital does not change. Why do you think this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT