Ad

Alternative using lists utilizing previous idea of reversion to avoid sorting. Missing test for number with integer square root added.

Code
Diff
  • from math import sqrt
    def divisors(n):
        fa = [i for i in range(1, int(sqrt(n)) + 1) if not (n%i)]
        fa.extend([n//i for i in reversed(fa) if n//i > i])
        return fa
    • from math import sqrt
    • def divisors(n):
    • fa={i for i in range(1,int(sqrt(n)) + 1) if not(n%i)}
    • ct={n//i for i in fa}
    • return sorted(fa|ct)
    • fa = [i for i in range(1, int(sqrt(n)) + 1) if not (n%i)]
    • fa.extend([n//i for i in reversed(fa) if n//i > i])
    • return fa
Code
Diff
  • def square(n):
        result = (("*" *n+"\n")*n)[:-1]
        print(result) # ...write a function that prints...
        return result # result to be tested
    • def square(n):
    • print(("*" *n+"
    • ")*n) # to see the result
    • return (("*" *n+"\n")*n)[:-1]
    • result = (("*" *n+"
    • ")*n)[:-1]
    • print(result) # ...write a function that prints...
    • return result # result to be tested
Numbers
Data Types
Code
Diff
  • def kilogramsToGrams(kilograms=1):
        return kilograms * 1000
    • def kilogramsToGrams(kilograms):
    • def kilogramsToGrams(kilograms=1):
    • return kilograms * 1000
Code
Diff
  • def hypotenuse(a, b):
        return abs(complex(a, b))
    • def hypotenuse(a, b):
    • return abs(a + b * 1j)
    • return abs(complex(a, b))