Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ch4_challenge.py#14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
TouhidulHaider wants to merge10 commits intoLinkedInLearning:main
base:main
Choose a base branch
Loading
fromTouhidulHaider:patch-5
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletionStart/Ch 1/challenge.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,13 @@
# of "Ticker: Company -- $Price"

class Stock:
pass
def __init__(self, ticker, price, company):
self.ticker = ticker
self.price = price
self.company = company

def get_description(self):
return f"{self.ticker}: {self.company} -- {self.price}"

# ~~~~~~~~~ TEST CODE ~~~~~~~~~
msft = Stock("MSFT", 342.0, "Microsoft Corp")
Expand Down
30 changes: 24 additions & 6 deletionsStart/Ch 2/challenge.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,14 +12,32 @@
# For stocks: "Ticker: Company -- $Price"
# For bonds: "description: duration'yr' : $price : yieldamt%"

class Asset():
pass
class Asset(ABC):
def __init__(self, price):
self.price = price

class Stock():
pass
@abstractmethod
def get_description(self):
pass

class Bond():
pass
class Stock(Asset):
def __init__(self, ticker, price, company_name):
super().__init__(price)
self.ticker = ticker
self.company_name = company_name

def get_description(self):
return f"{self.ticker}: {self.company_name} -- ${self.price}"

class Bond(Asset):
def __init__(self, price, description, duration, yieldamt):
super().__init__(price)
self.description = description
self.duration = duration
self.yieldamt = yieldamt

def get_description(self):
return f"{self.description}: {self.duration}yr : ${self.price} : {self.yieldamt}%"


# ~~~~~~~~~ TEST CODE ~~~~~~~~~
Expand Down
12 changes: 12 additions & 0 deletionsStart/Ch 3/challenge.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,6 +23,12 @@ def __init__(self, ticker, price, company):
self.company = company
self.ticker = ticker

def __str__(self):
return f"{self.ticker}: {self.company_name} -- ${self.price}"

def __lt__(self, other):
return self.price < other.price


class Bond(Asset):
def __init__(self, price, description, duration, yieldamt):
Expand All@@ -31,6 +37,12 @@ def __init__(self, price, description, duration, yieldamt):
self.duration = duration
self.yieldamt = yieldamt

def __str__(self):
return f"{self.description}: {self.duration}yr : ${self.price} : {self.yieldamt}%"

def __lt__(self, other):
return self.yieldamt < other.yieldamt


# ~~~~~~~~~ TEST CODE ~~~~~~~~~
stocks = [
Expand Down
45 changes: 31 additions & 14 deletionsStart/Ch 4/challenge.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,32 +4,49 @@
# Challenge: convert your classes to dataclasses
# The subclasses are required to override the magic method
# that makes them sortable
from abc import ABC, abstractmethod
from dataclasses import dataclass

class Asset():
pass

from dataclasses import dataclass, field

@dataclass
class Asset(ABC):
price: float

@abstractmethod
def __lt__(self):
pass

@dataclass
class Stock(Asset):
pass
ticker: str
company: str

def __lt__(self, other):
return self.price < other.price

@dataclass
class Bond(Asset):
pass
description: str
duration: int
yieldamt: float

def __lt__(self, other):
return self.yieldamt < other.yieldamt

# ~~~~~~~~~ TEST CODE ~~~~~~~~~
stocks = [
Stock("MSFT", 342.0, "Microsoft Corp"),
Stock("GOOG", 135.0, "Google Inc"),
Stock("META", 275.0, "Meta Platforms Inc"),
Stock("AMZN", 120.0, "Amazon Inc")
Stock(ticker="MSFT",price=342.0,company="Microsoft Corp"),
Stock(ticker="GOOG",price=135.0,company="Google Inc"),
Stock(ticker="META",price=275.0,company="Meta Platforms Inc"),
Stock(ticker="AMZN",price=120.0,company="Amazon Inc")
]

bonds = [
Bond(95.31, "30 Year US Treasury", 30, 4.38),
Bond(96.70, "10 Year US Treasury", 10, 4.28),
Bond(98.65, "5 Year US Treasury", 5, 4.43),
Bond(99.57, "2 Year US Treasury", 2, 4.98)
]
Bond(price=95.31, description="30 Year US Treasury", duration=30, yieldamt=4.38),
Bond(price=96.70, description="10 Year US Treasury", duration=10, yieldamt=4.28),
Bond(price=98.65, description="5 Year US Treasury", duration=5, yieldamt=4.43),
Bond(price=99.57, description="2 Year US Treasury", duration=2, yieldamt=4.98)

try:
ast = Asset(100.0)
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp