In this code:
def is_even(x): if x == 0: return True else: return is_odd(x-1) def is_odd(x): return not is_even(x) print(is_even(2)) print(is_odd(2))
I continue this in my head and wonder how it works. It seems to me that in the end xto is_even(x)be returned Trueeach time. However, when I run the code, it works fine and returns Trueand Falseappropriate. Can someone explain how this happens?
x
is_even(x)
True
False
I understand the basic concept of recursion and fully understand how the famous factorial example works. However, it’s hard for me to hug my head.
Feeling unulocal right now ...
It always helps to decompose the repetition relation until you find its base case.
is_even(2) => return is_odd(1) => return not is_even(1) => return not is_odd(0) => return not not is_even(0) => return not not True => return True ---- (1) is_odd(2) => return not is_even(2) => return not True [from (1)] => return False
, , is_even(n) [not not not ... n times] True, is_odd(n) [not not not ... n - 1 times] True. , not , , n (aha!). , , ,
is_even(n)
[not not not ... n times] True
is_odd(n)
[not not not ... n - 1 times] True
not
n
n % 2 == 0
print, , :
print
from __future__ import print_function def is_even(x): if x == 0: print('True') return True else: return is_odd(x-1) def is_odd(x): print('not', end=' ') return not is_even(x) >>> is_even(5) not not not not not True False >>> is_odd(5) not not not not not not True True
, print :
def is_even(x): print('check if {} is even'.format(x)) if x == 0: return True else: return is_odd(x-1) def is_odd(x): print('check if {} is odd'.format(x)) return not is_even(x)
:
>>> print(is_even(2)) check if 2 is even check if 1 is odd check if 1 is even check if 0 is odd check if 0 is even True >>> print(is_odd(2)) check if 2 is odd check if 2 is even check if 1 is odd check if 1 is even check if 0 is odd check if 0 is even False
, 0. , not is_odd. not, True, False.
0
is_odd
; is_even.
is_even
, , 0.
. - is_even(0) is_odd(0). - . - is_odd, , , , .
is_even(0)
is_odd(0)
- : , , - , return is_odd(x-1).
return is_odd(x-1)
: True, is_even. x, x - , .
, ( is_odd):
def is_even (x): res = True while x: res = not res x -= 1 return res
, , , , , " ". , is_odd, . True , nots, , nots , , false.
Source: https://habr.com/ru/post/1678742/More articles:Tensorflow Trying to use uninitialized AUC / AUC / auc / false_positives value - pythonhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1678738/how-to-force-jenkins-to-reload-a-jenkinsfile&usg=ALkJrhiqQ5wO0tOkmWe3ecn2Mn92ihlCrwConflicting modules. LoggerFactory is not a LoglogContext, but Logback is in the classpath - javaХранилище Google Cloud на dev_appserver в облачной оболочке - google-app-engineCreate Closest Teams Based on C # Staff Schedules - c #iOS mobile: прокрутка в iFrame внутри прокручиваемого родителя - htmlСборник Visual Studio Team Services не может найти библиотеку - tfsiOS11 Core NFC and ISO 14443 - iosStructure and Organization of Airflow Dags and Tasks - airflowUsing NSKeyValueObservation to Observe a Value in UserDefaults - iosAll Articles