Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Solutions/1_10/mortgage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
total_paid = total_paid + payment

if month >= extra_payment_start_month and month <= extra_payment_end_month:
principal = principal - extra_payment
principal -= extra_payment
total_paid = total_paid + extra_payment

print(month, round(total_paid,2), round(principal, 2))

Comment on lines -19 to +23

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 19-23 refactored with the following changes:

  • Replace assignment with augmented assignment (aug-assign)

print('Total paid', round(total_paid, 2))
print('Months', month)

Expand Down
6 changes: 1 addition & 5 deletions Solutions/1_33/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ def portfolio_cost(filename):
return total_cost

import sys
if len(sys.argv) == 2:
filename = sys.argv[1]
else:
filename = input('Enter a filename:')

filename = sys.argv[1] if len(sys.argv) == 2 else input('Enter a filename:')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 25-29 refactored with the following changes:

cost = portfolio_cost(filename)
print('Total cost:', cost)
4 changes: 1 addition & 3 deletions Solutions/1_5/bounce.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# bounce.py

height = 100
bounce = 1
while bounce <= 10:
for bounce in range(1, 11):
height = height * (3/5)
print(bounce, round(height, 4))
bounce += 1
Comment on lines -4 to -8

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 4-8 refactored with the following changes:

6 changes: 1 addition & 5 deletions Solutions/2_16/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ def portfolio_cost(filename):
return total_cost

import sys
if len(sys.argv) == 2:
filename = sys.argv[1]
else:
filename = input('Enter a filename:')

filename = sys.argv[1] if len(sys.argv) == 2 else input('Enter a filename:')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 26-30 refactored with the following changes:

cost = portfolio_cost(filename)
print('Total cost:', cost)
5 changes: 1 addition & 4 deletions Solutions/3_10/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ def parse_csv(filename, select=None, types=None, has_headers=True, delimiter=','
continue

# Make a dictionary or a tuple
if headers:
record = dict(zip(headers, row))
else:
record = tuple(row)
record = dict(zip(headers, row)) if headers else tuple(row)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_csv refactored with the following changes:

records.append(record)

return records
5 changes: 1 addition & 4 deletions Solutions/3_14/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ def parse_csv(filename, select=None, types=None, has_headers=True, delimiter=','
continue

# Make a dictionary or a tuple
if headers:
record = dict(zip(headers, row))
else:
record = tuple(row)
record = dict(zip(headers, row)) if headers else tuple(row)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_csv refactored with the following changes:

records.append(record)

return records
8 changes: 2 additions & 6 deletions Solutions/3_14/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ def portfolio_cost(filename):
Computes the total cost (shares*price) of a portfolio file
'''
portfolio = report.read_portfolio(filename)
return sum([s['shares']*s['price'] for s in portfolio])
return sum(s['shares']*s['price'] for s in portfolio)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function portfolio_cost refactored with the following changes:


import sys
if len(sys.argv) == 2:
filename = sys.argv[1]
else:
filename = input('Enter a filename:')

filename = sys.argv[1] if len(sys.argv) == 2 else input('Enter a filename:')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 13-17 refactored with the following changes:

cost = portfolio_cost(filename)
print('Total cost:', cost)
5 changes: 1 addition & 4 deletions Solutions/3_16/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ def parse_csv(filename, select=None, types=None, has_headers=True, delimiter=','
continue

# Make a dictionary or a tuple
if headers:
record = dict(zip(headers, row))
else:
record = tuple(row)
record = dict(zip(headers, row)) if headers else tuple(row)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_csv refactored with the following changes:

records.append(record)

return records
4 changes: 2 additions & 2 deletions Solutions/3_16/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def portfolio_cost(filename):
Computes the total cost (shares*price) of a portfolio file
'''
portfolio = report.read_portfolio(filename)
return sum([s['shares'] * s['price'] for s in portfolio])
return sum(s['shares'] * s['price'] for s in portfolio)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function portfolio_cost refactored with the following changes:


def main(args):
if len(args) != 2:
raise SystemExit('Usage: %s portfoliofile' % args[0])
raise SystemExit(f'Usage: {args[0]} portfoliofile')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

filename = args[1]
print('Total cost:', portfolio_cost(filename))

Expand Down
2 changes: 1 addition & 1 deletion Solutions/3_16/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def portfolio_report(portfoliofile, pricefile):

def main(args):
if len(args) != 3:
raise SystemExit('Usage: %s portfile pricefile' % args[0])
raise SystemExit(f'Usage: {args[0]} portfile pricefile')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

portfolio_report(args[1], args[2])

if __name__ == '__main__':
Expand Down
5 changes: 1 addition & 4 deletions Solutions/3_18/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s
continue

# Make a dictionary or a tuple
if headers:
record = dict(zip(headers, row))
else:
record = tuple(row)
record = dict(zip(headers, row)) if headers else tuple(row)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_csv refactored with the following changes:

records.append(record)

return records
4 changes: 2 additions & 2 deletions Solutions/3_18/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def portfolio_cost(filename):
Computes the total cost (shares*price) of a portfolio file
'''
portfolio = report.read_portfolio(filename)
return sum([s['shares'] * s['price'] for s in portfolio])
return sum(s['shares'] * s['price'] for s in portfolio)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function portfolio_cost refactored with the following changes:


def main(args):
if len(args) != 2:
raise SystemExit('Usage: %s portfoliofile' % args[0])
raise SystemExit(f'Usage: {args[0]} portfoliofile')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

filename = args[1]
print('Total cost:', portfolio_cost(filename))

Expand Down
2 changes: 1 addition & 1 deletion Solutions/3_18/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def portfolio_report(portfoliofile, pricefile):

def main(args):
if len(args) != 3:
raise SystemExit('Usage: %s portfile pricefile' % args[0])
raise SystemExit(f'Usage: {args[0]} portfile pricefile')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

portfolio_report(args[1], args[2])

if __name__ == '__main__':
Expand Down
5 changes: 1 addition & 4 deletions Solutions/3_7/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ def parse_csv(filename, select=None, types=None, has_headers=True, delimiter=','
row = [func(val) for func, val in zip(types, row)]

# Make a dictionary or a tuple
if headers:
record = dict(zip(headers, row))
else:
record = tuple(row)
record = dict(zip(headers, row)) if headers else tuple(row)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_csv refactored with the following changes:

records.append(record)

return records
5 changes: 1 addition & 4 deletions Solutions/4_10/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s
continue

# Make a dictionary or a tuple
if headers:
record = dict(zip(headers, row))
else:
record = tuple(row)
record = dict(zip(headers, row)) if headers else tuple(row)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_csv refactored with the following changes:

records.append(record)

return records
4 changes: 2 additions & 2 deletions Solutions/4_10/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def portfolio_cost(filename):
Computes the total cost (shares*price) of a portfolio file
'''
portfolio = report.read_portfolio(filename)
return sum([s.cost() for s in portfolio])
return sum(s.cost() for s in portfolio)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function portfolio_cost refactored with the following changes:


def main(args):
if len(args) != 2:
raise SystemExit('Usage: %s portfoliofile' % args[0])
raise SystemExit(f'Usage: {args[0]} portfoliofile')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

filename = args[1]
print('Total cost:', portfolio_cost(filename))

Expand Down
5 changes: 2 additions & 3 deletions Solutions/4_10/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def read_portfolio(filename):
select=['name','shares','price'],
types=[str,int,float])

portfolio = [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ]
return portfolio
return [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_portfolio refactored with the following changes:


def read_prices(filename):
'''
Expand Down Expand Up @@ -63,7 +62,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'):

def main(args):
if len(args) != 4:
raise SystemExit('Usage: %s portfile pricefile format' % args[0])
raise SystemExit(f'Usage: {args[0]} portfile pricefile format')
Comment on lines -66 to +65

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

portfolio_report(args[1], args[2], args[3])

if __name__ == '__main__':
Expand Down
5 changes: 1 addition & 4 deletions Solutions/4_4/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s
continue

# Make a dictionary or a tuple
if headers:
record = dict(zip(headers, row))
else:
record = tuple(row)
record = dict(zip(headers, row)) if headers else tuple(row)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_csv refactored with the following changes:

records.append(record)

return records
4 changes: 2 additions & 2 deletions Solutions/4_4/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def portfolio_cost(filename):
Computes the total cost (shares*price) of a portfolio file
'''
portfolio = report.read_portfolio(filename)
return sum([s.cost() for s in portfolio])
return sum(s.cost() for s in portfolio)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function portfolio_cost refactored with the following changes:


def main(args):
if len(args) != 2:
raise SystemExit('Usage: %s portfoliofile' % args[0])
raise SystemExit(f'Usage: {args[0]} portfoliofile')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

filename = args[1]
print('Total cost:', portfolio_cost(filename))

Expand Down
5 changes: 2 additions & 3 deletions Solutions/4_4/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def read_portfolio(filename):
select=['name','shares','price'],
types=[str,int,float])

portfolio = [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ]
return portfolio
return [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_portfolio refactored with the following changes:


def read_prices(filename):
'''
Expand Down Expand Up @@ -62,7 +61,7 @@ def portfolio_report(portfoliofile, pricefile):

def main(args):
if len(args) != 3:
raise SystemExit('Usage: %s portfile pricefile' % args[0])
raise SystemExit(f'Usage: {args[0]} portfile pricefile')
Comment on lines -65 to +64

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

portfolio_report(args[1], args[2])

if __name__ == '__main__':
Expand Down
5 changes: 1 addition & 4 deletions Solutions/5_8/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s
continue

# Make a dictionary or a tuple
if headers:
record = dict(zip(headers, row))
else:
record = tuple(row)
record = dict(zip(headers, row)) if headers else tuple(row)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_csv refactored with the following changes:

records.append(record)

return records
4 changes: 2 additions & 2 deletions Solutions/5_8/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def portfolio_cost(filename):
Computes the total cost (shares*price) of a portfolio file
'''
portfolio = report.read_portfolio(filename)
return sum([s.cost() for s in portfolio])
return sum(s.cost() for s in portfolio)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function portfolio_cost refactored with the following changes:


def main(args):
if len(args) != 2:
raise SystemExit('Usage: %s portfoliofile' % args[0])
raise SystemExit(f'Usage: {args[0]} portfoliofile')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

filename = args[1]
print('Total cost:', portfolio_cost(filename))

Expand Down
5 changes: 2 additions & 3 deletions Solutions/5_8/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def read_portfolio(filename):
select=['name','shares','price'],
types=[str,int,float])

portfolio = [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ]
return portfolio
return [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_portfolio refactored with the following changes:


def read_prices(filename):
'''
Expand Down Expand Up @@ -63,7 +62,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'):

def main(args):
if len(args) != 4:
raise SystemExit('Usage: %s portfile pricefile format' % args[0])
raise SystemExit(f'Usage: {args[0]} portfile pricefile format')
Comment on lines -66 to +65

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

portfolio_report(args[1], args[2], args[3])

if __name__ == '__main__':
Expand Down
5 changes: 1 addition & 4 deletions Solutions/6_12/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s
continue

# Make a dictionary or a tuple
if headers:
record = dict(zip(headers, row))
else:
record = tuple(row)
record = dict(zip(headers, row)) if headers else tuple(row)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_csv refactored with the following changes:

records.append(record)

return records
2 changes: 1 addition & 1 deletion Solutions/6_12/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def portfolio_cost(filename):

def main(args):
if len(args) != 2:
raise SystemExit('Usage: %s portfoliofile' % args[0])
raise SystemExit(f'Usage: {args[0]} portfoliofile')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

filename = args[1]
print('Total cost:', portfolio_cost(filename))

Expand Down
4 changes: 2 additions & 2 deletions Solutions/6_12/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def __getitem__(self, index):
return self._holdings[index]

def __contains__(self, name):
return any([s.name == name for s in self._holdings])
return any(s.name == name for s in self._holdings)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Portfolio.__contains__ refactored with the following changes:


@property
def total_cost(self):
return sum([s.shares * s.price for s in self._holdings])
return sum(s.shares * s.price for s in self._holdings)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Portfolio.total_cost refactored with the following changes:


def tabulate_shares(self):
from collections import Counter
Expand Down
2 changes: 1 addition & 1 deletion Solutions/6_12/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'):

def main(args):
if len(args) != 4:
raise SystemExit('Usage: %s portfile pricefile format' % args[0])
raise SystemExit(f'Usage: {args[0]} portfile pricefile format')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

portfolio_report(args[1], args[2], args[3])

if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion Solutions/6_12/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def ticker(portfile, logfile, fmt):

def main(args):
if len(args) != 4:
raise SystemExit('Usage: %s portfoliofile logfile fmt' % args[0])
raise SystemExit(f'Usage: {args[0]} portfoliofile logfile fmt')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

ticker(args[1], args[2], args[3])

if __name__ == '__main__':
Expand Down
5 changes: 1 addition & 4 deletions Solutions/6_15/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s
continue

# Make a dictionary or a tuple
if headers:
record = dict(zip(headers, row))
else:
record = tuple(row)
record = dict(zip(headers, row)) if headers else tuple(row)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_csv refactored with the following changes:

records.append(record)

return records
2 changes: 1 addition & 1 deletion Solutions/6_15/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def portfolio_cost(filename):

def main(args):
if len(args) != 2:
raise SystemExit('Usage: %s portfoliofile' % args[0])
raise SystemExit(f'Usage: {args[0]} portfoliofile')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

filename = args[1]
print('Total cost:', portfolio_cost(filename))

Expand Down
2 changes: 1 addition & 1 deletion Solutions/6_15/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'):

def main(args):
if len(args) != 4:
raise SystemExit('Usage: %s portfile pricefile format' % args[0])
raise SystemExit(f'Usage: {args[0]} portfile pricefile format')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

portfolio_report(args[1], args[2], args[3])

if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion Solutions/6_15/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def ticker(portfile, logfile, fmt):

def main(args):
if len(args) != 4:
raise SystemExit('Usage: %s portfoliofile logfile fmt' % args[0])
raise SystemExit(f'Usage: {args[0]} portfoliofile logfile fmt')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

ticker(args[1], args[2], args[3])

if __name__ == '__main__':
Expand Down
Loading