Why this Palindrome Recursive Function giving me True in all cases? [duplicate] - python

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 1 year ago.
I have tried to write a recursive version of the Palindrome function. But it is giving me True in all the cases. why?
def isPalindrome(inputString):
if len(inputString)==0 or 1:
return True
elif inputString[0]==inputString[-1]:
return isPalindrome(inputString[1:-1])
else:
return False
Why False is not working for non Palindrome Text.

Short circuit evaluation.
def is_palindrome(input_string):
if len(input_string) == 0 or len(input_string) == 1:
return True
elif input_string[0] == input_string[-1]:
return is_palindrome(input_string[1:-1])
else:
return False
print(is_palindrome('your_string')) # False

Related

Match statement case int 1 matches with boolean value True [duplicate]

This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Why is bool a subclass of int?
(3 answers)
Closed 13 days ago.
a = True
match a:
case False:
print("It is false")
case 2:
print('It is 2 true.')
case 1:
print('It is 1 true.')
>>> It is 1 true.
Why does the program execute the last case statement?

why is this code giving me the output "b" instead of "c" [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 7 months ago.
what will be the output of this code and why
options = "year2"
options1 = "semester1"
if (options == "year1") and (options1 == "semester1"):
print("a")
elif (options == "year1" or "year3" or "year4") and (options1 == "semester2" or "semester3"):
print("b")
elif (options == "year2" or "year3" or "year4") and (options1 == "semester1"):
print("c")
else:
print("d")
You are looking for something like
options in ("year1", "year2", ...)
The Misunderstanding here is, that you connect with or
<options == "year1"> or <"year3"> ...
<options == "year1"> might or migth not be true
<"year3"> is equivalent to bool("year3") which will be always true

No output after calling function (Python) [duplicate]

This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 6 months ago.
just a total newbie here. I am learning Python and was trying to call the function as below, yet I cannot see any output.
nums = [0,1,2,7,14]
def has_lucky_num(nums):
#return numbers that can be divisible by 7
for num in nums:
if num % 7 == 0:
return True
return False
has_lucky_num(nums)
Can someone tell me why it did not work? Thanks in advance.
Your program isn't printing anything because it doesn't have a print() command anywhere. Try the following line : print(has_lucky_nums(nums)), and it should print your True or False result! So you would have the following code :
nums = [0,1,2,7,14]
def has_lucky_num(nums):
#return numbers that can be divisible by 7
for num in nums:
if num % 7 == 0:
return True
return False
print(has_lucky_num(nums))
Your function has a problem:
You must put the return False after loop because if you don't it return immediately after checking first item
So function code must be this:
nums = [0,1,2,7,14]
def has_lucky_num(nums):
#return numbers that can be divisible by 7
for num in nums:
if num % 7 == 0:
return True
return False
If you want to see result you must print the result returns by the function has_lucky_num()
So all you have to do is to add a print like this:
Instead Of:
has_lucky_num(nums)
Replace With:
print(has_lucky_num(nums))
Output:
True
Return True and Return False won't actually display anything. You need to do something with those values, for instance:
if has_lucky_num(nums) == True:
print(f'{nums} has a lucky number!')
else:
print(f'{nums} has a lucky number!')
```

Explain how python execute this function? [duplicate]

This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 2 years ago.
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a "hit" on an even number, we return True
if number % 2 == 0:
return True
else:
return False
now when execute function
[1]-check_even_list([1,2,3])
False
[2]-check_even_list([2,1,3])
True
why this True when #3 is return False ???
[3]-check_even_list([1,4,2])
False
Why this False when #2 is return True ???
Your code will only ever check the first element, once a return statement is hit, no further code in your function will execute. Instead you could modify your code to
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a "hit" on an even number, we return True
if number % 2 == 0:
return True
# After loop, must not have hit an even number
return False
For conciseness, you could also write the equivalent
def check_even_list(num_list):
return all(i % 2 == 0 for i in num_list)
You are using "early return statements". What your function does is actually only evaluating the first element of your list.
Here's I would write the function:
def check_even_list(num_list):
return all(map(lambda x: x % 2 == 0, num_list))
This is because once return statement is executed, function is not executed any more.
Use this code:
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a "hit" on an even number, we return True
if number % 2 == 0:
return True
# After loop is completed and no even values encountered
return False
and if you want to do just the opposite i.e. return false once an odd number is hit:
def check_odd_list(num_list):
# Go through each number
for number in num_list:
# Once we get a "hit" on an odd number, we return True
if number % 2 != 0:
return False
# After loop is completed and no odd values encountered
return True

Why does this iteration didn't return anything out? [duplicate]

This question already has an answer here:
Python recursion with list returns None [duplicate]
(1 answer)
Closed 7 years ago.
def is_power(a,b):
if a<b:
is_power(b,a)
elif a==b:
return True
else:
if a%b !=0:
return False
else:
is_power((a/b),b)
is_power(2,32)
I don't know why it didn't show anything out, but when I print the last line of the function "is_power((a/b),b)", it shows:
True
None
None
None
i write and run it in ipython notebook and python's version is 2.7.10.1
def is_power(a,b):
if a<b:
return is_power(b,a)
elif a==b:
return True
else:
if a%b !=0:
return False
else:
return is_power((a/b),b)
You're running a recursive function without returning anything on steps.
is_power(2, 32)
First step : if a < b: return is_power(32, 2)
Second step : (else condition): return is_power(16, 2)
Thrid step : (else condition): return is_power(8, 2)
Fourth step : (else condition): return is_power(4, 2)
Fifth step : (else condition): return is_power(2, 2)
Sixth step : (elif a==b): return True
Result: True
If you miss any return statement, the code won't return anything else than None
You have insert a return statement to the appropriate rows and have to add to the end of the code : print is_power(x,y),
which call the is_power() function and the return values to the output.
Note in IPython is_power(x,y) alone works too.
def is_power(a,b):
if a<b:
return is_power(b,a)
elif a==b:
return True
else:
if a%b !=0:
return False
else:
return is_power((a/b),b)
print is_power(2,32)
Output:
True
You're program is made to return a Boolean, hence you will either get a True or a False.
If you want a different output, you'll have to code it to generate something else.
The only 2 return statements in your code are these :
elif a==b:
return True
and :
else:
if a%b !=0:
return False
Therefore the only outputs you can expect are True and False.

Categories

Resources