I use operator and to return on python code.
mission is 'greatest common divisor's value of list
I applied method to Euclidean Algorithm
0 def gcd(a, b):
1 if a > b:
2 return b if a%b == 0 else gcd(a%b, b)
3 elif a < b:
4 return a if b%a == 0 else gcd(a, b%a)
5 else:
6 return a or b
6 line, It doesn't matter which one a or b
but there's something wrong with that.
andwe need to know what it's return True or False
| A | B | result | boolean |
|---|---|---|---|
| 0 | 0 | 0 | False |
| 0 | 1 | 0 | False |
| 1 | 0 | 0 | False |
| 1 | 1 | 1 | True |
and check the first element, if False, returns the element.
if both all correct, return the last element
>>> a = 10 and 6
>>> a
6
>>> a = 6 and 10
>>> a
10
>>> a = 10 and 0
>>> a
0
>>> a = 0 and 5
>>> a
0
oror operator is easy :)
if one thing is True, returns True
| A | B | result | booelan |
|---|---|---|---|
| 0 | 0 | 0 | False |
| 0 | 1 | 1 | True |
| 1 | 0 | 1 | True |
| 1 | 1 | 1 | True |
or check the first element, if True, returns the element.
because which don't need to check no more
>>> a = 7 or 10
>>> a
7
>>> a = 0 or 77
>>> a
77
>>> a = 7 or 10
>>> a
7
>>> a = 10 or 7
>>> a
10
before I understood it as a 'bit operator'
that's wrong !
2022.07.29. first commit