Strange behaviour with String reversal function - python

I am using recursion to reverse a string. I am able to reverse a string successfully using following code.
def rev(string):
if len(string)==0:
return string
if len(string)==1:
return string
else:
s=string
string = s[-1]+rev(s[:-1])
return string
But when i am replacing return with print it is printing only the first two characters of answer and then throwing an error saying
"TypeError: cannot concatenate 'str' and 'NoneType' objects"
Example. when give 'StackOverflow' to function and returned a value it returns 'wolfrevOkcatS' corrrectly
but when I am trying to print the answer in the function itself, it is only printing "tS" giving the error above.

I suspect that you did something like this:
def rev(string):
if len(string)==0:
print string
if len(string)==1:
print string
else:
s=string
string = s[-1]+rev(s[:-1])
print string
Let's get right to the problem. Supposed the string you are trying to reverse is abc. In the first call, you will hit the else clause and try to evaluate:
'c' + rev('ab')
The problem is now that your function has no return statements, rev('ab') returns None. And python raises an error when you try to concatenate a string with None.
With the return statements. rev('ab') returns 'ba' and the plus sign can be used to concatenate 'c' with 'ba'.
As an aside, if you are ever trying to solve this problem in production code, you should be using the built in tools.
reversed_str = myStr[::-1]
reversed_str_iterator = reversed(myStr)

If you're replacing return with print, your function will not return anything. In Python, that is the same as returning None. This breaks your recursive step where you concatenate the last letter of the string with the return value of the recursive call. Instead, you should just let the recursive call do its own printing (just call it), after print the last letter in a separate statement:
else:
print(s[-1], end="") # or print s[-1], (with the comma!) if you're on Python 2
rev(s[:-1])
Note that you probably want your base cases to print a newline in this situation, since otherwise you won't get one at all.

If you don't return from a function, then the function implicitly returns None. By replacing return with print, you break the recursive functionality; it recurses, but the values computed in the recursive calls are not returned and used. If you want to print intermediate results for your own edification, do so, but do so just before each return statement, not in place of the return statements.

To reverse a string/list in Python use list[::-1]
Demo
>>> my_str = 'StackOverflow'
>>> my_str[::-1]
'wolfrevOkcatS'

Related

Why do some methods of built-in types operate in-place and some don't?

dog = 'penny'
print(dog.title())
dog_names = ['pete', 'luke', 'shane']
print(dog_names.remove('shane'))
Why does Python return an output of Penny for dog.title() but None for
dog_names.remove('shane')? Why can I not print the list dog_name with the method remove while I can use the method title on dog?
I understand that I get None because dog_name.remove has no return, but how does dog.title have a return?
The title() function is a pre-defined function in Python which is used to covert the first character of the string into uppercase and the remaining characters into lowercase and return a new string.
in your example if you run print(dog) you can see that penny is all lowercase, but if you run print(dog.title()) you can see that the first letter in Penny which is P is uppercase and the remaining is lowercase
In order to answer your question, first, you need to understand what's being returned.
When calling print on a function or method, it will display the returned value.
With that being said, you can check the return value of both functions from the documentation
https://docs.python.org/2/tutorial/datastructures.html#more-on-lists
list.remove(x) Remove the first item from the list whose value is x.
It is an error if there is no such item.
https://docs.python.org/3.7/library/stdtypes.html#str.title
str.title() Return a titlecased version of the string where words
start with an uppercase character and the remaining characters are
lowercase.
Notice how title() returns the titled case string, that's why it's printed.
On the other hand remove() doesn't return anything, in this case, python has a default return value of None (Source) that's why it's printed when calling remove()
A function/method in any programming language performs a specific task. A task could be performed on the inputs passed to the function. After performing the task, the function may want to give the result back to the caller of the function. It may do that by:
By modifying the parameters passed to it or By altering the object on which method was called. (For example, remove() method of list data type)
By returning the result to the caller. (title() method)
A combination of (1) and (2)
Your remove() method doesn't return anything back to the caller. Instead it just deletes an element from the list. To print the list after removing an element:
dog_names.remove('shane')
print(dog_names)

String manipulation using list comprehension

I am trying below code and expecting output as,
matching string where every even letter is uppercase, and every odd letter is lowercase, however my output is list of all uppercase characters.
def myfunc(*args):
return ''.join([args[i].upper() if i%2==0 else args[i].lower() for i in range(len(args))])
How can I get expected output ? I tried same list comprehension syntax to test and it works fine. What's wrong with this specific code ?
By writing *args in your function declaration, it means that you're iterating over a number of strings and not just one string. For example:
myfunc('hello', 'goodbye')
Your function will iterate over a hello and goodbye, with hello having an index of 0 which is even number and thus converting its characters to uppercase, whereas goodbye has an index of 1 which is odd and thus covnerting its characters to lowercase:
HELLOgoodbye
If you wish to call your function for only one string, you have to remove the * from *args or inserting the string's characters one by one:
myfunc('h','e','l','l','o')
So, the declaration and implementation of your function should look like this:
def myfunc(args):
return ''.join([args[i].upper() if i%2==0 else args[i].lower() for i in range(len(args))])
Calling myfunc('hello') will return the correct result.
The problem is, you're using var-args. That's wrapping the string passed with a list, then you're iterating over that list (which only contains one string). This means your code does work to some extent; its just alternating the case of each whole word instead of each character.
Just get rid of the var-args:
def myfunc(word):
return ''.join([word[i].upper() if i%2==0 else word[i].lower() for i in range(len(word))])
print(myfunc("hello world"))
# HeLlo WoRlD
I'll just note, there's a few ways this could have been debugged. If you had checked the contents of args, you would have seen that it only contained a single element when one argument was passed, and two when two arguments were passed. If you had passed two arguments in, you would have also noticed that it alternated the case of each word as a whole. That would have likely given you enough hints to figure it out.
The above answers are correct but they ask you to modify the current syntax. In general you should try to understand how *args works.
In python, *args in a function definition is used to receive variable number of arguments. Such cases arise when we don't know in advance how many arguments will be passed.
For example :
def myFun(*argv):
for arg in argv:
print (arg)
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks')
Now coming back to your code, you can make it run by selecting the first argument from the args list. See below :
def myfunc(*args):
return ''.join([args[0][i].upper() if i%2==0 else args[0][i].lower() for i in range(len(args[0]))])
print(myfunc("input"))
Help the community by selecting the best answer for this question.

Trouble with Palindrome function in python

I'm having trouble in an online course for python, specifically a palindrome problem These are the instructions, but the function must be case-insensitive and not see spaces. I think the issue is in my return blocks or my flow. I think I need to use the lower function, but I'm honestly not sure.
def student_func(x):
for string in x:
x.lower()
y = x.replace(" ", "")
if y[::-1]==y:
return True
else:
return False
You actually have two separate problems in your code—and you're right that one of them is with lower and the other is with the return flow.
First, x.lower() doesn't modify x in-place. In fact, strings are immutable; nothing modifies them in-place. If you look up the interactive help or the online docs, it says:
Return a copy of the string with all the cased characters [4] converted to lowercase.
So, you need to do the same thing with lower that you do with replace: assign the result to a variable, and use that:
y = x.lower()
z = y.replace(" ", "")
Or you can reuse the same variable:
x = x.lower()
… or chain the two calls together:
y = x.lower().replace(" ", "")
As a side note, unless you're using Python 2, you should consider whether you want casefold instead of lower. For English it makes no difference, but for other languages it can.
Meanwhile, you're doing for string in x:, but then ignoring string.
If x is just a single word, you don't want to loop over it at all.
If x is a list of words, then the for string in x: is correct, but then you have to use string inside the loop, not x. Plus, you can't just return True or return False—that will exit the function as soon as you test the first word, meaning the rest of them never get tested. I'm not sure whether you want to return True if there are any pallidromes, or if they're all palindromes, or if you want to return a list of booleans instead of a single one, or what, but you can't just return the first one.
It would probably be a lot clearer if you used better names, like words instead of x and word instead of string.
Anyway, I can't tell you the right way to fix this since I don't know what you're trying to do, but hopefully this explains enough that you can fix it yourself.
Giving away the solution defeats the purpose of the exercise
your approach is more or less correct.
convert string to a standard case
remove whitespace
check if reverse of the string is equal to the original string
The error lies in how you are using the python API.
check what each of the functions do, and what they return.
a good idea is to run help(function) to see what the function's documentation has to say about it.
try help(x.lower) (note: not help(x.lower())) and see what the return value is.

Not getting the desired output in python

enter image description hereI want to reverse a number. If 92 is typed the result should be 29. The code is given below
def intreverse(n) :
a=str(n)
b=a[::-1]
c=int(b)
print (c)
But i am getting the result as actual output as
29/n
none/n
Why?
The course you use probably wants you to write functions with returns. As your function has no return, you get None in the end. And for the newline character \n: As you print your answer, print puts a newline character after what you print. If you delete the print statement and put a return statement instead as suggested, both problems will be solved.
It looks like you are getting newlines in your text input (possibly two?). I would fix this with
a=str(n).strip()
also, you are printing the result, I think you want to return it. Since the function does not return anything, a = intreverse('29') will assign None to a. So you want:
def intreverse(n) :
a=str(n).strip()
b=a[::-1]
c=int(b)
return c
Or just for the obligatory one-liner:
def intreverse(n):
return int(str(n).strip()[::-1])

String Delimiter in Python

I want to do split a string using "},{" as the delimiter. I have tried various things but none of them work.
string="2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3 "
Split it into something like this:
2,1,6,4,5,1
8,1,4,9,6,6,7,0
6,1,2,3,9
2,3,5,4,3
string.split("},{") works at the Python console but if I write a Python script in which do this operation it does not work.
You need to assign the result of string.split("},{") to a new string. For example:
string2 = string.split("},{")
I think that is the reason you think it works at the console but not in scripts. In the console it just prints out the return value, but in the script you want to make sure you use the returned value.
You need to return the string back to the caller. Assigning to the string parameter doesn't change the caller's variable, so those changes are lost.
def convert2list(string):
string = string.strip()
string = string[2:len(string)-2].split("},{")
# Return to caller.
return string
# Grab return value.
converted = convert2list("{1,2},{3,4}")
You could do it in steps:
Split at commas to get "{...}" strings.
Remove leading and trailing curly braces.
It might not be the most Pythonic or efficient, but it's general and doable.
I was taking the input from the console in the form of arguments to the script....
So when I was taking the input as {{2,4,5},{1,9,4,8,6,6,7},{1,2,3},{2,3}} it was not coming properly in the arg[1] .. so the split was basically splitting on an empty string ...
If I run the below code from a script file (in Python 2.7):
string="2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3 "
print string.split("},{")
Then the output I got is:
['2,1,6,4,5,1', '8,1,4,9,6,6,7,0', '6,1,2,3,9', '2,3,5,4,3 ']
And the below code also works fine:
string="2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3 "
def convert2list(string):
string=string.strip()
string=string[:len(string)].split("},{")
print string
convert2list(string)
Use This:
This will split the string considering },{ as a delimiter and print the list with line breaks.
string = "2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3"
for each in string.split('},{'):
print each
Output:
2,1,6,4,5,1
8,1,4,9,6,6,7,0
6,1,2,3,9
2,3,5,4,3
If you want to print the split items in the list only you can use this simple print option.
string = "2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3"
print string.split('},{')
Output:
['2,1,6,4,5,1', '8,1,4,9,6,6,7,0', '6,1,2,3,9', '2,3,5,4,3']
Quite simply ,you have to use split() method ,and "},{" as a delimeter, then print according to arguments (because string will be a list ) ,
like the following :
string.split("},{")
for i in range(0,len(string)):
print(string[i])

Categories

Resources