#๐Ÿ”’ Reversing a string and subsequently reversing the case for each character

59 messages ยท Page 1 of 1 (latest)

safe radish
#

def main():
myString = "aBcdEF"
str_changed = reverseAndOpposite(myString)
print(str_changed)

def reverseAndOpposite(a_str):
if len(a_str) == 1:
return a_str

else:
    new_str = reverseAndOpposite(a_str[1:]) + a_str[0]

reverse = ""

for i in range(len(new_str)):
    if new_str[i].isalpha():
        if new_str[i].islower():
            reverse += new_str[i].upper()
        elif new_str[i].isupper():
            reverse += new_str[i].lower()
    else:
        reverse += new_str[i]
return reverse

When I run it, the expected output should be "AbCDef", but instead I get "fedCBA". I cannot tell where the logic is wrong, any help?

kindred pantherBOT
#

@safe radish

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

jovial merlin
safe radish
#

the logic seems sound to me, but im confused by the output

#

the first three letters are lower and the second three are upper

jovial merlin
jovial merlin
# safe radish yes

python already have the .swapcase() method for strings
and a string can easily be reversed by using [::-1]

#

!e

print("aBcdEF".swapcase()[::-1])
kindred pantherBOT
safe radish
jovial merlin
safe radish
#

my bad this is my first time posting here

#

anyway yeah ive been racking my brain over how my output is wrong lol

#

bcos everything looks correct to me

jovial merlin
jovial merlin
#

@safe radish what is your constraints and requirements for the task?

safe radish
jovial merlin
#

or i should say: what's left of the string in the final iteration of the recursion

jovial merlin
#

@safe radish are you allowed to do the loop you are doing with the for i in range(len(new_str)): if the assignment was to "only use iterations and recursion to reverse a string and modify each character", shouldn't you do that to each character at the same time that you are reversing the characters in the recursion?

jovial merlin
# safe radish ah yes

i think your best bet would be to just do an early return for the empty string when there is no more letters to process in the string

safe radish
safe radish
jovial merlin
safe radish
#

but any idea why the output is wrong tho?

#

its not like one part of it is wrong but its just completely wrong lol

jovial merlin
#

@safe radish you really only have to process one character at a time, which i think is what they want you to do in this assignment as you are working with recursion

jovial merlin
#

@safe radish how is it going?
just to let you know, i need to leave soon

fathom tundra
#

@safe radish not sure if you are still here but

#

i recommend not using a recursive solution because it won't be as efficient. however, if you must, then the problem with your solution has to do with your for loop inside of a recursive solution. If you are doing a recursive solution, you don't need that loop.

#

after you handle your base case, you need to make sure you flip the cases. that's basically an if statement checking if it is upper case or lower case. i dont use python but there is probably a function that lets you check that.

#

i just looked it up and there is a .isupper() method

jovial merlin
fathom tundra
jovial merlin
#

this post maybe should be marked as homework if that is what it is

fathom tundra
#

where you have + a_str[0], you need to make sure you flip the case for that

jovial merlin
fathom tundra
#

def main():
    myString = "aBcdEF"
    str_changed = reverseAndOpposite(myString)
    print(str_changed)

def getOppositeCase(char):
  if char.isupper():
    return char.lower() 
  return char.upper()

def reverseAndOpposite(a_str):
    if len(a_str) == 1:
        return getOppositeCase(a_str[0])
    else:
        return reverseAndOpposite(a_str[1:]) + getOppositeCase(a_str[0])
#

something like that should work

#

probably some syntax mistake in there though

#

just learn from it

jovial merlin
fathom tundra
#

@safe radish sorry for the pings but i highly recommend you learn what the "recursive leap of faith". it will give you a very intuitive understanding of recursion.

fathom tundra
#

if its someone like that, i rather just give them a code and let them run off with. if it works, so be it. if it doesn't, i dont usually help them.

#

i do think sometimes its more intuitive to see a solution first, especially in recursion because you'll see the same pattern over and over again:

  1. base case(s)
  2. recursive step
jovial merlin
jovial merlin
#

also, a little style guide, when it comes to python snake_case is preferred for variables and functions/methods over camelCase

kindred pantherBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.