Commit f1a4dedd authored by MichaelAng's avatar MichaelAng

[ban265] hw3 and hw4

parent 3e846885
......@@ -21,6 +21,16 @@ run_hw2:
$(DOCKER_BASE_CMD) \
ban265 "hw2.py"
.PHONY: run_hw3
run_hw3:
$(DOCKER_BASE_CMD) \
ban265 "hw3.py"
.PHONY: run_hw4
run_hw4:
$(DOCKER_BASE_CMD) \
ban265 "hw4.py"
.PHONY: fmt
fmt:
$(DOCKER_BASE_CMD) \
......
# Ang, Michael
# BAN 265 A
# Homework 3
# 2024-03-06
# https://www.dropbox.com/s/8ydqijf422reflq/PY_hw3.docx?e=1&dl=0
# Question 1
# BMI for a person is calculated as weight in kg / (height in meters * height in meters). You need to calculate everyone in the following list variables. (Hint: 23.31, 25.98, 24.54)
v_weight = [65, 87, 76]
v_height = [1.67, 1.83, 1.76]
print([round(w / h**2, 2) for w, h in zip(v_weight, v_height)])
# Question 2
# You need to calculate the sale price after discount for each item in the following dictionary variables. (Hint: Apple 58.5, Book 60.9, Computer 60.8)
v_original_price = {"Book": 87, "Computer": 76, "Apple": 65}
v_discount_rate = {"Apple": 0.1, "Computer": 0.2, "Book": 0.3}
print({k: round(v - (v * v_discount_rate[k]), 2) for k, v in v_original_price.items()})
# Question 3
# Identify the data type for the following data.
print(type(1)) # type: int
print(type("1")) # type: str
print(type([1, 2, 3])) # type: List[int]
print(type(["1", "2", "3"])) # type: List[str]
print(type("[1, 2, 3]")) # type: str
print(type({1: 5, 2: 8})) # type: dict
print(type({"1": 5, "2": 8})) # type: dict
print(type("{1: 5, 2: 8}")) # type: str
# Ang, Michael
# BAN 265 A
# Homework 4
# 2024-03-06
# https://www.dropbox.com/s/4c99e3pq5slmrg5/PY_hw4.docx?e=1&dl=0
# Question 1
# Download the following transaction data https://www.dropbox.com/s/k97psmvvom8p8jl/supermarket.csv?dl=0. (Warning: do not use excel to open and save. Excel will only save the first 1 million records and the dataset has more than 3 million records). This data has the information for each purchased item with its “Date, Transaction Number, UPC Number, Product Name, Customer Account, Qty, Regular Price, Sale Price, and Discount”. Please write the python code to show the Date, Transaction Number, UPC Number, Customer Account, and Sale Price for the 2000000th records. (Hint: 2016-09-06 11:54:56.513, T00700048761, P0007800000209, C0044000035045, 4.99)
import csv
target_record = 2_000_000
with open("record.csv", "r") as file:
reader = csv.DictReader(file)
for i, row in enumerate(reader):
if i == target_record:
print("Date: ", row.get("Date"))
print("Transaction Number: ", row.get("Transaction Number"))
print("Customer Account: ", row.get("Customer Account"))
print("Sale Price: ", row.get("Sale Price"))
import numpy as np
import pandas as pd
books = ["The Great Gatsby", "1984", "To Kill a Mockingbird"]
checked_out_books = {}
s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"])
action = input("Do you want to 'check in' or 'check out' a book? ").lower()
print(s)
if action == "check in":
book = input("Enter the name of the book to check in: ")
if book in checked_out_books:
del checked_out_books[book]
books.append(book)
print(f"{book} checked in successfully.")
else:
print("This book was not checked out.")
elif action == "check out":
print("Available books:")
for book in books:
print(book)
book = input("Enter the name of the book to check out: ")
if book in books:
user = input("Enter your name: ")
books.remove(book)
checked_out_books[book] = user
print(f"{book} checked out by {user}.")
else:
print("Book not available.")
else:
print("Invalid action")
Date,Transaction Number,UPC Number,Product Name,Customer Account,Qty,Regular Price,Sale Price,Discount
"2016-09-06 11:54:56.513","1","P0007800000209","A", "C0044000035045",4.99,1,5,0.1
"2016-09-06 11:54:56.513","2","P0007800000209","B", "C0044000035045",4.99,1,5,0.1
"2016-09-06 11:54:56.513","T00700048761","P0007800000209","C", "C0044000035045",4.99,1,5,0.1
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment