Why this dual recursive code works in python and how? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
This code gives exactly what is says but how and why?
To my understanding, for instance the is_odd function if called with any value, the value will diminish into zero and will return True in the is_even() section and so will become False in is_odd() in turn. So every is_odd check supposed to become False but it's not, it just works on every odd/even check, why and how?
#!/bin/python
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_odd(2))
print(is_even(2))
print(is_odd(3))
print(is_even(3))
Output:
False
True
True
False

Note that is_even does not always return True but can also return is_odd(n-1), which in turn return not is_even(n-1), and so on.
Example for is_odd(3):
is_odd(3)
not is_even(3)
not is_odd(2)
not not is_even(2)
not not is_odd(1)
not not not is_even(1)
not not not is_odd(0)
not not not not is_even(0)
not not not not True
not not not False
not not True
not False
True
In general, you get a cascade of N negations for an even number, or N+1 negations for an odd number, and if the number of those negations is even, the result is True.

Related

when I run this code below, it doesn’t give me True or False [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
it doesn’t give an output
and I Know that the output should be True or False
some = [1, 9, 21, 3]
9 in some
As others have pointed out, you need to do something with the result. You can save it into a variable with:
var = 9 in some
or print it directly with:
print(9 in some)
It does return a TRUE/FALSE:
[In]: 3 in list([1,2,35])
[Out]: False
[In]: 3 in list([1,2,3,5])
[Out]: True
From the documentation:
"The operators in and not in test for membership. x in s evaluates
to True if x is a member of s, and False otherwise. x not in s returns
the negation of x in s. All built-in sequences and set types support
this as well as dictionary, for which in tests whether the dictionary
has a given key." docs

check if a pin is long 4 or 6 digits or not [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
I just started learning py and i tried to create a simple program that checks whether a pin is long a 4 or 6 digits or not, but somehow it doesn't work.
def validate_pin(pin):
if len(pin) == 4 or len(pin) == 6:
return true
else:
return false
The value passed to validate_pin() would need to be of type str for this to make sense. Consider a PIN number of 0001. That's a valid PIN number but when expressed as int it isn't. Therefore:
def validate_pin(pin):
return len(pin) == 4 or len(pin) == 6
Python booleans are capitalized, so you have to change true to True and false to False

Why it is the length of my list different than the length of the same list cast to a set? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Why it is showing True for list 1 if items in it are not same and False for second list if items it it are same. Please explain in details:
def test_distinct(data):
if len(data) == len(set(data)):
return True
else:
return False
When I run:
print(test_distinct([1,5,7,9]))
I get:
True
When I run:
print(test_distinct([2,4,5,5,7,9]))
I get:
False
Please explain why == True if items in list are not similar and false when they are similar
So effectively the set() will parse (or convert if you will) your list (or tuple) into a new "set" which will contain only distinct iterable elements.
In your first list: the len = 4 as is, and is 4 with any duplicates
removed (which there are not in your first list).
In your second list: the len = 6 as is, but len = 5 with the duplicate 5 removed (third and fourth items in your second list) .

What does i for i in s mean? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
def CodelandUsernameValidation(s):
if len(s)>4 and len(s)<25 and s[0].isalpha() and [i for i in s if i.isalnum() or i=="_"]!=[] and s[-1]!="_":
return True
else:
return False
# keep this function call here
print(CodelandUsernameValidation(input()))
It's a list comprehension if you expand, it'll look like this ->
result = []
for i in s: # will fetch element one by one from iterable
if i.isalnum() or i=="_": # checking condition
result.append(i) # if true, then append it to the list
This above code can be rewritten as -
result = [i for i in s if i.isalnum() or i=="_"]
That produces a list of those characters in s that are either alphanumeric or underlines. The code is actually incorrect, because it will pass if ANY of the characters are alphanumeric or underline, whereas the intent surely was that ALL of the characters must be alphanumeric or underline. Here's a better way to write it:
def CodelandUsernameValidation(s):
return 4 < len(s) < 25 and s[0].isalpha() and all(i.isalnum() or i=='_' for i in s) and s[-1] != '_'
print(CodelandUsernameValidation(input()))

Iterate through string of characters and look for int? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to iterate through a string with a mixture of letters and numbers. If it finds at least one number, it returns True, else, it returns false. How can I do this?
You can write a function that returns True when find at least one digit using the isdigit() method.
def has_digit(str):
for letter in str:
if letter.isdigit():
return True
return False
if __name__ == '__main__':
print(has_digit('abcdef'))
print(has_digit('abd5e'))
The output is:
False
True
You can write a function or not as you like. You can also write a ready string or you can use input function:
x = 'Jack77'
for i in x:
if i.isdigit():
print(True)
else:
print(False)
#==================
def checking_digits(x):
for i in x:
if i.isdigit():
return True
return False
print(checking_digits(x))
This will create a list of bool values for each character in the string. isdigit return True if the character is a digit, False otherwise. So if any of the bool values (or any the characters is a digit), it will return True
has_digits = any(i.isdigit() for i in text)

Categories