GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python-basics/solution.py at main · WolverineSportsAnalytics/python-basics Expand file tree/ Copy pathsolution.pyMore file actions 74 lines (58 loc) · 1.97 KB / Copy pathsolution.pyFile metadata and controls74 lines (58 loc) · 1.97 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 def hello_world(): '''Prints "Hello World!".''' print("Hello World!") return def sum(a, b): '''Accepts 2 numbers as parameters, returns sum of a and b.''' return a + b def sub(a, b): '''Accepts 2 numbers as parameters, returns subtraction of a and b.''' return a - b def product(a, b): '''Accepts 2 numbers as parameters, returns product of a and b.''' # CHALLENGE: use a for loop and your sum function to implement product ans = 0 for i in range(b): ans = sum(ans, a) return ans def divide(a, b): '''Accepts 2 numbers as parameters, returns a divided by b.''' # only pass in numbers that are divisible for sake of implementation # CHALLENGE: use a while loop and your sub function to implement divide ans = 0 while a > 0: a = sub(a, b) ans += 1 return ans def root(num): '''Accepts a number as a parameter, returns the sqrt of num.''' # only pass in numbers that are perfect squares for sake of implementation # leetcode easy # CHALLENGE: do not use any built-in Python functions # Basic approach, look up binary search approach if interested # base cases if num is 0 or num is 1: return num # Try all numbers until i*i equal to num. i = 1 result = 1 while result < num: i += 1 result = i * i return i def main(): '''The main function is where you will test all of your functions.''' hello_world() print("Testing sum:") print("EXPECTED: %d, ACTUAL: %d\n" % (5, sum(2, 3))) print("Testing sub:") print("EXPECTED: %d, ACTUAL: %d\n" % (5, sub(7, 2))) print("Testing product:") print("EXPECTED: %d, ACTUAL: %d\n" % (10, product(2, 5))) print("Testing divide:") print("EXPECTED: %d, ACTUAL: %d\n" % (5, divide(10, 2))) print("Testing root:") print("EXPECTED: %d, ACTUAL: %d\n" % (5, root(25))) # Add any additional test cases if needed if __name__ == "__main__": main() |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |