diff --git a/README.md b/README.md index f2c6435..d674be9 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/main.py b/main.py index 97aa0b9..2bc372a 100644 --- a/main.py +++ b/main.py @@ -13,6 +13,12 @@ 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" @@ -20,6 +26,7 @@ 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 diff --git a/snippets/__init__.py b/snippets/__init__.py index 637acd7..68516c9 100644 --- a/snippets/__init__.py +++ b/snippets/__init__.py @@ -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 + ) \ No newline at end of file diff --git a/snippets/foobar.py b/snippets/foobar.py index 61c5243..7ca6154 100644 --- a/snippets/foobar.py +++ b/snippets/foobar.py @@ -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 diff --git a/snippets/io.py b/snippets/io.py index c3f92bc..6c2efcd 100644 --- a/snippets/io.py +++ b/snippets/io.py @@ -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 diff --git a/snippets/loop.py b/snippets/loop.py index 08a2d33..fac5495 100644 --- a/snippets/loop.py +++ b/snippets/loop.py @@ -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