 PROGRAMMING

# m9/ PYTHON REMEMBERS

Last update:   23-07-2021

### INCREMENTAL

p104 ! The goal of incremental development is to avoid long debugging. By Pythagorean Theorem, the distance betwee two points is:
$d = \sqrt{(x2-x1)^2 + (y2-y1)^2}$
p104 The first step will be to define the function, with wrong answer but syntactically correct. def distance(x1, y1, x2, y2):
return 0.0

d = distance(1, 2, 4, 6)
print(d)   # 0,0

The next step is to find the differences x2-x1, y2-y1. def distance(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
print('dx: ', dx)  # 3
print('dy: ', dy)  # 4
return 0.0

d = distance(1, 2, 4, 6)
print(d)  # 0,0
... 2 lines 
Then we compute the sum of squares. def distance(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
sum = dx**2 + dy**2
print('sum: ', sum)  # 25
return 0.0

d = distance(1, 2, 4, 6)
print(d)  # 0,0
... 2 lines 
Finally, you can use math.sqrt() to compute the result. import math

def distance(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
sum = dx**2 + dy**2
return math.sqrt(sum)

d = distance(1, 2, 4, 6)
print(d)  # 5,0
... 2 lines """
Use incremental development to write a function
The function returns the length of the hypotenuse of a right triangle

The function gets the other two legs as arguments
Record each stage of the development """

# SOLUTION

def hypotenuse(a, b):
???

solution code """
Write a function called is_palindrom
You can use built-in funciton len to check the string length

A palindorm is a word that is spelled the same backward and forward
Example: noon, redivider

def first(word): return word
def last(word): return word[-1]
def middle(word): return word[1:-1]  """

# SOLUTION

def is_palindrome(word):
???

solution code
Questions

A B C D E F
🔔
1/2