diff --git a/BugsDocumentation.md b/BugsDocumentation.md new file mode 100644 index 0000000..1f494c8 --- /dev/null +++ b/BugsDocumentation.md @@ -0,0 +1,26 @@ +Documentaion of Bugs found +1. snippets/__init__.py + - import code in snippets.__init__ had been commented out and so the import were not happening +2. loop.py + - line 9: initialize dict instead of list +3. loop.py + - line 11: forgot to add range(10) + - line 13: NameError cause of using undefined variable + - line 13: use list.push instead of list.append +4. io.py + - line 16: list comprehension should use the [] symbols instead of {} + - line 18: syntax error, there is no such operator as !===, instead use '==' operator + - line 20: use python inbuilt sum + - line 29: use python inbuilt sum + - line 38: use python inbuilt sum + - line 39: use python inbuilt len + - line 15, 24, 34 + - use [] to access the values of the dict + - line 18: should avoid using is operator for immutable types such as strings and numbers, the result is unpredictable, instead use == + - line 45: test for division by zero, by checking if sum is zero + +5. foobar.py + - line 8: remove default list argument and initialize it inside the function (explanation below) + + Please note: Any expressions in default arguments are always calculated when the function is defined and not when it is called. So when function is called the first time the 'bar' list is created, any subsequntial calls will still use the same list. To avoid this just define bar inside the function and not as a default argument. + diff --git a/snippets/__init__.py b/snippets/__init__.py index 637acd7..c95361b 100644 --- a/snippets/__init__.py +++ b/snippets/__init__.py @@ -1,8 +1,8 @@ -# from .loop import lambda_array -# from .io import ( -# read_file, -# calculate_unpaid_loans, -# calculate_paid_loans, -# average_paid_loans -# ) +from .loop import lambda_array +from .io import ( + read_file, + calculate_unpaid_loans, + calculate_paid_loans, + average_paid_loans +) from .foobar import foo diff --git a/snippets/foobar.py b/snippets/foobar.py index 61c5243..f0277e8 100644 --- a/snippets/foobar.py +++ b/snippets/foobar.py @@ -5,6 +5,7 @@ """ -def foo(bar=[]): +def foo(): + bar = [] bar.append("baz") return bar diff --git a/snippets/io.py b/snippets/io.py index c3f92bc..484a182 100644 --- a/snippets/io.py +++ b/snippets/io.py @@ -12,30 +12,38 @@ 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) + try: + loans = data["loans"] + unpaid_loans = [ + loan["amount"] for loan in loans + if loan["status"] == "unpaid" + ] + except KeyError as error: + print(error, 'could not access some keys from the json format') + return sum(unpaid_loans) def calculate_paid_loans(data): - loans = data("loans") - paid_loans = [ - loan.amount for loan in loans - if loan.status is "paid" - ] - return sun(paid_loans) + try: + loans = data["loans"] + paid_loans = [ + loan["amount"] for loan in loans + if loan["status"] == "paid" + ] + except KeyError as error: + print(error, 'could not access some keys from the json format') + 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) + if(sum_paid_loans == 0): + return 0 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