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
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,43 @@ The requirements for this project are simple and are as follows:
Do not modify the existing logic, just find the bug in the code snippets and correct it.
The expected output when running the script is displayed in `main.py`
Do not modify the `main.py` file.


### Changes Documentation by module and function.

#### File init.py.
1. Import function from different files in the snippets module to enable direct import from main.py.


#### File foobar.py foo().

1. Default parameters cannot be mutable values change empty to a null value.
switch from `bar=[]` to `bar=None`.

#### File IO.py, calculate_unpaid_loans.

1. Fixed Typo on the return statement sun instead of sum function `line 20`.
2. Fixed Wrong construct of a List comprehension using curly brackets instead of square brackets.
3. Access dictionary correctly fix, from `loan.amount`, `loan.status` to `loan["amount"] ` and `loan["status"]`.
4. Fix to unpaid loan check on line 18, from `!== ` to `==`, to ensure its checking unpaid loans.
5. line 15 Wrong way to access dictionary value, `loans = data("loans")` fix to `loans = data["loans"]`.


#### File IO.py calculate_paid_loans.
1. Access dictionary correctly fix, from `loan.amount`, `loan.status` to `loan["amount"] ` and `loan["status"]`.
2. Fix loan status check on line 27, from identity check `is` to equality check `==`, to ensure its checking paid loans.
3. Fix typo on the return statement sun instead of sum line `29`.
4. line 24 Wrong way to access dictionary value, `loans = data("loans")` fix to `loans = data["loans"]`.


#### File IO.py average_paid_loans.
1. On line `38. sum_paid_loans = sun(paid_loans)` typo when calling the sum function.
2. On line `39` wrong function call `length` instead of len to check list size.
3. line 33 Wrong way to access dictionary value, `loans = data("loans")` fix to `loans = data["loans"]`.


#### File loop.py lambda_array.
1. Wrong initializing of array should be `lambda_methods=list()`
2. Line 11 wrong use of for loop since integer literal (10) is not a iterable, switched to use range function to fix.
3. Line 13 wrong variable name `lambdamethods` switch to `lambda_methods`.
4. Line 13 python list uses append function to add objects to a list and does not have a push function.
7 changes: 7 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
try:
lambdas = lambda_array()
json_file = read_file()

assert(type(calculate_unpaid_loans(json_file)) in [int, float]), "Value returned should be an int or float type"
assert(type(calculate_paid_loans(json_file)) in [int, float]), "Value returned should be an int or float type"
assert(type(average_paid_loans(json_file)) in [int, float]), "Value returned should be an int or float type"
assert (foo("fooo") == ["baz"]), "Should return 'baz' even with a default string literal argument"

assert (lambdas[0](10) == 19), "lambdas[0](10) should equal 19"
assert (len(json_file.get("loans")) == 15), "Number of loans should equal 15"
assert (calculate_unpaid_loans(json_file) == 11062), "Total unpaid loans should equal 11062"
assert (calculate_paid_loans(json_file) == 29493.85304), "Total paid loans should equal 29493.85304"
assert (average_paid_loans(json_file) == 2681.2593672727276), "Average of paid loans should equal 2681.2593672727276"
assert (foo() == ["baz"]), "This should return a single item 'baz'"
assert (foo() == ["baz"]), "When I call the function the second time I should still get a single element in the array"

print("All test passed successfully!! 😀")
except (
AssertionError, SyntaxError, TypeError
Expand Down
13 changes: 6 additions & 7 deletions snippets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# from .loop import lambda_array
# from .io import (
# read_file,
# calculate_unpaid_loans,
# calculate_paid_loans,
# average_paid_loans
# )
from .foobar import foo
from .loop import lambda_array
from .io import (calculate_paid_loans,
calculate_unpaid_loans,
read_file,
average_paid_loans
)
6 changes: 5 additions & 1 deletion snippets/foobar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"""


def foo(bar=[]):
def foo(bar=None):
if bar is None:
bar = list()
if type(bar) is not list:
bar = list()
bar.append("baz")
return bar
30 changes: 15 additions & 15 deletions snippets/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ def read_file():


def calculate_unpaid_loans(data):
loans = data("loans")
unpaid_loans = {
loan.amount for loan in loans
if loan.status !== "unpaid"
}
return sun(unpaid_loans)
loans = data["loans"]
unpaid_loans = [
loan["amount"] for loan in loans
if loan["status"] == "unpaid"
]
return sum(unpaid_loans)


def calculate_paid_loans(data):
loans = data("loans")
loans = data["loans"]
paid_loans = [
loan.amount for loan in loans
if loan.status is "paid"
loan["amount"] for loan in loans
if loan["status"] == "paid"
]
return sun(paid_loans)
return sum(paid_loans)


def average_paid_loans(data):
loans = data("loans")
loans = data["loans"]
paid_loans = [
loan.amount for loan in loans
if loan.status is "paid"
loan["amount"] for loan in loans
if loan["status"] == "paid"
]
sum_paid_loans = sun(paid_loans)
number_paid_loans = length(paid_loans)
sum_paid_loans = sum(paid_loans)
number_paid_loans = len(paid_loans)
average = (sum_paid_loans/number_paid_loans)
return average
6 changes: 3 additions & 3 deletions snippets/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

def lambda_array():
# initialize an empty array
lambda_methods = {}
lambda_methods = []
# implement a for loop to count from 0 to 9
for i in 10:
for i in range(10):
# append the lambda function to the array defined above
lambdamethods.push(lambda x: x + i)
lambda_methods.append(lambda x: x + i)

return lambda_methods