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
10 changes: 5 additions & 5 deletions Solutions/1_10/mortgage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
extra_payment_end_month = 108

while principal > 0:
month = month + 1
month += 1
principal = principal * (1+rate/12) - payment
total_paid = total_paid + payment
total_paid += payment

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

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

Comment on lines -14 to +23
Copy link
Author

Choose a reason for hiding this comment

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

Lines 14-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
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:

  • Replace if statement with if expression (assign-if-exp)

cost = portfolio_cost(filename)
print('Total cost:', cost)
6 changes: 2 additions & 4 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:
height = height * (3/5)
for bounce in range(1, 11):
height *= 3/5
print(bounce, round(height, 4))
bounce += 1
Comment on lines -4 to -8
Copy link
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:

  • Replace assignment with augmented assignment (aug-assign)
  • Replace while with for (while-to-for)

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
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:

  • Replace if statement with if expression (assign-if-exp)

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
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:

  • Replace if statement with if expression (assign-if-exp)

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
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:

  • Replace if statement with if expression (assign-if-exp)

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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


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
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:

  • Replace if statement with if expression (assign-if-exp)

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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
2 changes: 1 addition & 1 deletion Solutions/3_16/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


def main(args):
if len(args) != 2:
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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
2 changes: 1 addition & 1 deletion Solutions/3_18/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


def main(args):
if len(args) != 2:
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
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:

  • Replace if statement with if expression (assign-if-exp)

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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
2 changes: 1 addition & 1 deletion Solutions/4_10/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


def main(args):
if len(args) != 2:
Expand Down
3 changes: 1 addition & 2 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
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:

  • Inline variable that is immediately returned (inline-immediately-returned-variable)


def read_prices(filename):
'''
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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
2 changes: 1 addition & 1 deletion Solutions/4_4/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


def main(args):
if len(args) != 2:
Expand Down
3 changes: 1 addition & 2 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
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:

  • Inline variable that is immediately returned (inline-immediately-returned-variable)


def read_prices(filename):
'''
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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
2 changes: 1 addition & 1 deletion Solutions/5_8/pcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


def main(args):
if len(args) != 2:
Expand Down
3 changes: 1 addition & 2 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
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:

  • Inline variable that is immediately returned (inline-immediately-returned-variable)


def read_prices(filename):
'''
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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


@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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


def tabulate_shares(self):
from collections import Counter
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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
5 changes: 1 addition & 4 deletions Solutions/6_3/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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
4 changes: 2 additions & 2 deletions Solutions/6_3/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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


@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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


def tabulate_shares(self):
from collections import Counter
Expand Down
5 changes: 1 addition & 4 deletions Solutions/6_7/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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
4 changes: 2 additions & 2 deletions Solutions/6_7/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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


@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
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:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


def tabulate_shares(self):
from collections import Counter
Expand Down
5 changes: 1 addition & 4 deletions Solutions/7_11/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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
5 changes: 1 addition & 4 deletions Solutions/7_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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
5 changes: 1 addition & 4 deletions Solutions/7_9/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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
5 changes: 1 addition & 4 deletions Solutions/8_1/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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
5 changes: 1 addition & 4 deletions Solutions/8_2/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,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
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:

  • Replace if statement with if expression (assign-if-exp)

records.append(record)

return records
Loading