Recursive palindrome check - having issues when recalling the functionRecursive Function palindrome in Pythonpython and palindromesCheck Palindrome using recursionrecursion of palindrome functionCheck for palindrome recursivelycheck palindrome by recursion in cPalindrome function using recursionRecursive function finding PalindromeRecursive palindrome check with JavaScriptPalindrome check with recursive function without slicing and loops
Everyone Gets a Window Seat
Would a horse be sufficient buffer to prevent injury when falling from a great height?
Bothered by watching coworkers slacking off
Could the Queen overturn the UK Supreme Court ruling regarding prorogation of Parliament?
Is there a pattern for handling conflicting function parameters?
What does a textbook look like while you are writing it?
What's the correct way to determine turn order in this situation?
GPLv3 forces us to make code available, but to who?
How to say "respectively" in German when listing (enumerating) things
How are proofs normally constructed in a write up, in one line or split up into multiple lines?
How is this situation not a checkmate?
Should I be an author on another PhD student's paper if I went to their meetings and gave advice?
How to "Start as close to the end as possible", and why to do so?
Can a passenger predict that an airline or a tour operator is about to go bankrupt?
Giving a good fancy look to a simple table
The difference of Prime in Solve doesn't work
Booting Ubuntu from USB drive on MSI motherboard -- EVERYTHING fails
Generating numbers with cubes
How do we know Nemesis is not a black hole (or neutron star)?
If I travelled back in time to invest in X company to make a fortune, roughly what is the probability that it would fail?
Re-entering the UK after overstaying in 2008
Manager told a colleague of mine I was getting fired soon
Avoiding dust scattering when you drill
Rank-one positive decomposition for a entry-wise positive positive definite matrix
Recursive palindrome check - having issues when recalling the function
Recursive Function palindrome in Pythonpython and palindromesCheck Palindrome using recursionrecursion of palindrome functionCheck for palindrome recursivelycheck palindrome by recursion in cPalindrome function using recursionRecursive function finding PalindromeRecursive palindrome check with JavaScriptPalindrome check with recursive function without slicing and loops
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
The problem is simple, check if palindrome or not using recursion. They also provided a template so I can't change that.
The template:
def isPalindrome(s): # Wrapper function
def isPalindromeRec(s,low,high):
""" Recursive function which checks if substring s[low ... high] is palindrome
returns a True/False value"""
n = len(s)
return isPalindromeRec(s,0,n-1)
I am nearly there but I think I am having trouble understanding how recursion exactly works. (especially how the values changes in the recursion)
My code:
def isPalindrome(s): # Wrapper function
def isPalindromeRec(s,low,high):
if len(s)<=1:
return True
else:
if s[0]==s[len(s)-1]:
return isPalindromeRec(s[low+1:high],low+1,high-1)
else:
return False
n = len(s)
return isPalindromeRec(s,0,n-1)
print(isPalindrome("anna"))
print(isPalindrome("civic"))
print(isPalindrome("a"))
print(isPalindrome("tx1aa1xt"))
print(isPalindrome(""))
print(isPalindrome("Civic"))
print(isPalindrome("ab"))
this is the output:
runfile('/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 7/Problem2.py', wdir='/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 7')
True
True
True
False
True
False
False
the first false should be True.
Thank you for your help!
python-3.x recursion palindrome
add a comment
|
The problem is simple, check if palindrome or not using recursion. They also provided a template so I can't change that.
The template:
def isPalindrome(s): # Wrapper function
def isPalindromeRec(s,low,high):
""" Recursive function which checks if substring s[low ... high] is palindrome
returns a True/False value"""
n = len(s)
return isPalindromeRec(s,0,n-1)
I am nearly there but I think I am having trouble understanding how recursion exactly works. (especially how the values changes in the recursion)
My code:
def isPalindrome(s): # Wrapper function
def isPalindromeRec(s,low,high):
if len(s)<=1:
return True
else:
if s[0]==s[len(s)-1]:
return isPalindromeRec(s[low+1:high],low+1,high-1)
else:
return False
n = len(s)
return isPalindromeRec(s,0,n-1)
print(isPalindrome("anna"))
print(isPalindrome("civic"))
print(isPalindrome("a"))
print(isPalindrome("tx1aa1xt"))
print(isPalindrome(""))
print(isPalindrome("Civic"))
print(isPalindrome("ab"))
this is the output:
runfile('/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 7/Problem2.py', wdir='/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 7')
True
True
True
False
True
False
False
the first false should be True.
Thank you for your help!
python-3.x recursion palindrome
add a comment
|
The problem is simple, check if palindrome or not using recursion. They also provided a template so I can't change that.
The template:
def isPalindrome(s): # Wrapper function
def isPalindromeRec(s,low,high):
""" Recursive function which checks if substring s[low ... high] is palindrome
returns a True/False value"""
n = len(s)
return isPalindromeRec(s,0,n-1)
I am nearly there but I think I am having trouble understanding how recursion exactly works. (especially how the values changes in the recursion)
My code:
def isPalindrome(s): # Wrapper function
def isPalindromeRec(s,low,high):
if len(s)<=1:
return True
else:
if s[0]==s[len(s)-1]:
return isPalindromeRec(s[low+1:high],low+1,high-1)
else:
return False
n = len(s)
return isPalindromeRec(s,0,n-1)
print(isPalindrome("anna"))
print(isPalindrome("civic"))
print(isPalindrome("a"))
print(isPalindrome("tx1aa1xt"))
print(isPalindrome(""))
print(isPalindrome("Civic"))
print(isPalindrome("ab"))
this is the output:
runfile('/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 7/Problem2.py', wdir='/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 7')
True
True
True
False
True
False
False
the first false should be True.
Thank you for your help!
python-3.x recursion palindrome
The problem is simple, check if palindrome or not using recursion. They also provided a template so I can't change that.
The template:
def isPalindrome(s): # Wrapper function
def isPalindromeRec(s,low,high):
""" Recursive function which checks if substring s[low ... high] is palindrome
returns a True/False value"""
n = len(s)
return isPalindromeRec(s,0,n-1)
I am nearly there but I think I am having trouble understanding how recursion exactly works. (especially how the values changes in the recursion)
My code:
def isPalindrome(s): # Wrapper function
def isPalindromeRec(s,low,high):
if len(s)<=1:
return True
else:
if s[0]==s[len(s)-1]:
return isPalindromeRec(s[low+1:high],low+1,high-1)
else:
return False
n = len(s)
return isPalindromeRec(s,0,n-1)
print(isPalindrome("anna"))
print(isPalindrome("civic"))
print(isPalindrome("a"))
print(isPalindrome("tx1aa1xt"))
print(isPalindrome(""))
print(isPalindrome("Civic"))
print(isPalindrome("ab"))
this is the output:
runfile('/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 7/Problem2.py', wdir='/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 7')
True
True
True
False
True
False
False
the first false should be True.
Thank you for your help!
python-3.x recursion palindrome
python-3.x recursion palindrome
edited Mar 28 at 21:30
Rayan Al Hobayb
asked Mar 28 at 21:10
Rayan Al HobaybRayan Al Hobayb
377 bronze badges
377 bronze badges
add a comment
|
add a comment
|
1 Answer
1
active
oldest
votes
Rewrote it:
def isPalindrome(s):
def isPalindromeRec(s,low,high):
if (low == high):
return True
if (s[low] != s[high]) :
return False
if (low < high + 1) :
return isPalindromeRec(s, low + 1, high - 1);
return True
n = len(s)
if (n == 0) :
return True
return isPalindromeRec(s, 0, n - 1);
print(isPalindrome("anna"))
print(isPalindrome("civic"))
print(isPalindrome("a"))
print(isPalindrome("tx1aa1xt"))
print(isPalindrome(""))
print(isPalindrome("Civic"))
print(isPalindrome("ab"))
output:
True
True
True
True
True
False
False
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406884%2frecursive-palindrome-check-having-issues-when-recalling-the-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Rewrote it:
def isPalindrome(s):
def isPalindromeRec(s,low,high):
if (low == high):
return True
if (s[low] != s[high]) :
return False
if (low < high + 1) :
return isPalindromeRec(s, low + 1, high - 1);
return True
n = len(s)
if (n == 0) :
return True
return isPalindromeRec(s, 0, n - 1);
print(isPalindrome("anna"))
print(isPalindrome("civic"))
print(isPalindrome("a"))
print(isPalindrome("tx1aa1xt"))
print(isPalindrome(""))
print(isPalindrome("Civic"))
print(isPalindrome("ab"))
output:
True
True
True
True
True
False
False
add a comment
|
Rewrote it:
def isPalindrome(s):
def isPalindromeRec(s,low,high):
if (low == high):
return True
if (s[low] != s[high]) :
return False
if (low < high + 1) :
return isPalindromeRec(s, low + 1, high - 1);
return True
n = len(s)
if (n == 0) :
return True
return isPalindromeRec(s, 0, n - 1);
print(isPalindrome("anna"))
print(isPalindrome("civic"))
print(isPalindrome("a"))
print(isPalindrome("tx1aa1xt"))
print(isPalindrome(""))
print(isPalindrome("Civic"))
print(isPalindrome("ab"))
output:
True
True
True
True
True
False
False
add a comment
|
Rewrote it:
def isPalindrome(s):
def isPalindromeRec(s,low,high):
if (low == high):
return True
if (s[low] != s[high]) :
return False
if (low < high + 1) :
return isPalindromeRec(s, low + 1, high - 1);
return True
n = len(s)
if (n == 0) :
return True
return isPalindromeRec(s, 0, n - 1);
print(isPalindrome("anna"))
print(isPalindrome("civic"))
print(isPalindrome("a"))
print(isPalindrome("tx1aa1xt"))
print(isPalindrome(""))
print(isPalindrome("Civic"))
print(isPalindrome("ab"))
output:
True
True
True
True
True
False
False
Rewrote it:
def isPalindrome(s):
def isPalindromeRec(s,low,high):
if (low == high):
return True
if (s[low] != s[high]) :
return False
if (low < high + 1) :
return isPalindromeRec(s, low + 1, high - 1);
return True
n = len(s)
if (n == 0) :
return True
return isPalindromeRec(s, 0, n - 1);
print(isPalindrome("anna"))
print(isPalindrome("civic"))
print(isPalindrome("a"))
print(isPalindrome("tx1aa1xt"))
print(isPalindrome(""))
print(isPalindrome("Civic"))
print(isPalindrome("ab"))
output:
True
True
True
True
True
False
False
answered Mar 28 at 22:12
RalphRalph
2272 silver badges10 bronze badges
2272 silver badges10 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406884%2frecursive-palindrome-check-having-issues-when-recalling-the-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown