Write a function named evenOddString() that takes a single string parameter.
If the length of the string parameter:
is odd: evenOddString() should return the string parameter.
is even, evenOddString() should return the string parameter concatenated with itself.
This is my code so far:
def evenOddString():
len(var) % 2 != 0
First, your evenOddString() function needs to take a parameter. For example:
def evenOddString(the_string_parameter):
print(the_string_parameter)
To call that function, you would have something like:
evenOddString('abc123')
which would print out abc123 on the console. In your case, you will want check the length of the_string_parameter and do some stuff with it "if" the length is even or odd. The if and elif statements will be helpful. See http://docs.python.org/tutorial/controlflow.html for docs on flow control.
You also want to return a value back out of your function. If I wanted to return the string unchanged, I would do something like:
def evenOddString(the_string_parameter):
return the_string_parameter
Note that I'm being a little vague here as this sounds like a homework assignment and I don't want to simply write all the code for you. Since you sound like a new programmer just starting out and in need of a good tutorial, I highly recommend you work through http://learnpythonthehardway.org/
define a function that take a parameter - Your definition does not take a parameter
get the length of that parameter - No problems here
if the length is odd, return the return the parameter - Your condition is correct, but you're not doing anything with the condition
else: return the parameter concatenated with itself - not implemented
def control4OddString(myString):
if len(myString) % 2: return myString
else: return myString+myString
Related
I was trying to solve the following problem: Draw a star pattern that increases in every step (1st step: 1 star, 2nd step: 2 stars). E.g.
*
**
I am not sure why my code is not showing any output when I am writing return? When I am writing print, it is giving me the star output but also giving me None. May I know why return or print are not working properly? I am using Python 3.7. My code is:
def string(inp):
for i in range (inp):
return i*"*"
print (string(5))
range starts at 0, and return terminates a function, so that means string will always return an empty string.
Here's one possible option for getting your expected result:
def stars(n):
for i in range(1, n+1): # Add one to start and stop
print(i * "*") # Print inside the function
stars(2) # Don't print outside the function
Output:
*
**
If you need to print outside the function, you could use a generator:
def stars(n):
for i in range(1, n+1):
yield i * "*" # "yield" is like "return" but can be used more than once
for s in stars(2):
print(s) # Print each string that gets yielded
# Or print all at once, using the "splat" unpacking operator
print(*stars(5), sep='\n')
Using return won't print an output, use something like this:
def string(inp):
for i in range (inp):
print(i*"*")
string(5)
also this will only print 4, if you make it
for i in range(inp + 1):
It will work as intended,
hope this helps!
I will translate the code to plain English, as explicitly as I can:
Here are the rules that take a value `inp` and compute the `string` of that `inp`:
Letting `i` take on each integer value from 0 up to but not including `inp`:
We are done. The `string` of `inp` is equal to the string '*' repeated `i` times.
Compute the `string` of `5` and display it.
Hopefully the problem is evident: we can only be done with a task once, and i is equal to 0 at that point, so our computed value is an empty string.
When I am writing print, it is giving me the star output but also giving me None
From the described behaviour, I assume that you mean that you tried to replace the word return in your code with print, giving:
def string(inp):
for i in range (inp):
print(i*"*")
print (string(5))
That produces the triangle, of course, except that
Since i will be equal to 0 the first time through the loop, a blank line is printed; and since i will be equal to 4 the last time through the loop, there is no ***** line.
At the end, None is printed, as you describe. This happens because the value computed by string is the special value None, which is then printed because you asked for it to be printed (print(string(5))).
In Python, each call to a function will return a value when it returns, whether or not you use return and whether or not you specify a value to return. The default is this special None value, which is a unique object of its own type. It displays with the text None when printed, but is different from a string with that text (in the same way that the integer 5 is different from the string "5").
May I know why return or print are not working properly?
They are working exactly as designed. return specifies the result of calling the function, and can only happen once per function, and does not cause anything to be displayed. print displays what it is given.
If you wish to return multiple values from a call, then you need to work around that restriction - either by using a generator instead (as in #MadPhysicist's or #wjandrea's answers), or by using some single, structured datum that contains all those values (for example, a list, or a tuple).
A a re-entrant function that preserves state between calls is a generator. To make a generator function, change the keyword return to yield:
def string(n):
for i in range(n):
yield (i + 1) * '*'
Calling this version of string will return a generator that yields a new line of your desired output at each iteration.
To print:
for line in string(5):
print(line)
To print all at once:
print('\n'.join(string(5)))
This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 4 years ago.
Here is my program
def reverse(letters):
backwards = ""
i = len(letters) - 1
while i >= 0:
backwards = backwards + letters[i]
i = i - 1
print (backwards)
print (reverse("hello"))
It works, it prints out "olleh" but after, it prints "None" on a new line. And I'm asking why this is.
Obviously the program is to reverse a word, the code works, and without a function it doesn't print none, so I don't know why it does in the function. This is being used in another larger program, so I need it as a function, and because it's for school, I'm not allowed to simply use the .reverse() function. So I really need this code fixed rather than large changes, if possible.
function return None by default, so you should return backwards explicitly
also, you can use a pythonic way to solve the problem:
letters[::-1]
You can use a return statement to exit the function returning a value. If the function gets to the end without reaching a return statement, it will return None by default
def add1(x):
return x+1
def returnsNone():
pass
print(add1(2))
print(returnsNone())
Every function returns something in Python. If you don't explicitly return a value, Python has your function return None.
Your function doesn't actually return anything because print prints to stdout, while return actually returns a value. They may look the same in the REPL, but they're completely different.
So to fix your problem, return a value:
return backwards
Try just:
reverse(hello)
In fact with
print reverse(hello)
you are printing the return value of reverse. And that return value is None.
Let me give you few general advises:
reverse() in your code is a function with side effects (printing). You should avoid
functions with side effects when you don't need, consider having reverse() returning the word instead of printing it:
def reverse(letters):
backwards = ""
i = len(letters) - 1
while i >= 0:
backwards = backwards + letters[i]
i = i - 1
return backwards
print (reverse("hello"))
Moreover
i = len(letters) - 1
while i >= 0:
backwards = backwards + letters[i]
i = i - 1
is not easy to mantain and if you add functionality to the loop the decrement i=i-1 will be far from the place where it should 'conceptually' be. You should prefer having decrement together with the check:
for i in xrange(len(letters)-1,-1,-1):
backwards = backwards + letters[i]
When I'm lazy I write
myString = myString + fewChars
so I can understand you being lazy. But adding fewChars does not modify myString but creates a new one. If you are iteratively adding many chars, rarely the most efficient way is to add one by one. Consider using join().
For instance
letters = 'word'
lettersList = [letters[i] for i in xrange(len(letters)-1,-1,-1)]
myReversed ''.join(lettersList)
ok I agree is not readable and probably not even faster, but for larger strings it scales better than a newString = oldString + oneChar approach.
That said, more pythonic approaches
letters[::-1]
already suggested by someone faster than me typically works much better and are easy to be read by python programmers.
It makes sense if you think about it. Your reverse does not return anything - it just prints it's result. But when you write print (reverse("hello")), you are actually printing what reverse returns. Since it doesn't return anything, None is printed.
As mentioned earlier. If the python function does not come across a return statement it prints None by default. Making a small change as shown below fixes this:
def reverse(letters):
backwards = ""
i = len(letters) - 1
while i >= 0:
backwards = backwards + letters[i]
i = i - 1
return(backwards) # instead of print(backwards)
print(reverse("hello"))
I have to make a function called countLetterString(char, str) where
I need to use recursion to find the amount of times the given character appears in the string.
My code so far looks like this.
def countLetterString(char, str):
if not str:
return 0
else:
return 1 + countLetterString(char, str[1:])
All this does is count how many characters are in the string but I can't seem to figure out how to split the string then see whether the character is the character split.
The first step is to break this problem into pieces:
1. How do I determine if a character is in a string?
If you are doing this recursively you need to check if the first character of the string.
2. How do I compare two characters?
Python has a == operator that determines whether or not two things are equivalent
3. What do I do after I know whether or not the first character of the string matches or not?
You need to move on to the remainder of the string, yet somehow maintain a count of the characters you have seen so far. This is normally very easy with a for-loop because you can just declare a variable outside of it, but recursively you have to pass the state of the program to each new function call.
Here is an example where I compute the length of a string recursively:
def length(s):
if not s: # test if there are no more characters in the string
return 0
else: # maintain a count by adding 1 each time you return
# get all but the first character using a slice
return 1 + length( s[1:] )
from this example, see if you can complete your problem. Yours will have a single additional step.
4. When do I stop recursing?
This is always a question when dealing with recursion, when do I need to stop recalling myself. See if you can figure this one out.
EDIT:
not s will test if s is empty, because in Python the empty string "" evaluates to False; and not False == True
First of all, you shouldn't use str as a variable name as it will mask the built-in str type. Use something like s or text instead.
The if str == 0: line will not do what you expect, the correct way to check if a string is empty is with if not str: or if len(str) == 0: (the first method is preferred). See this answer for more info.
So now you have the base case of the recursion figured out, so what is the "step". You will either want to return 1 + countLetterString(...) or 0 + countLetterString(...) where you are calling countLetterString() with one less character. You will use the 1 if the character you remove matches char, or 0 otherwise. For example you could check to see if the first character from s matches char using s[0] == char.
To remove a single character in the string you can use slicing, so for the string s you can get all characters but the first using s[1:], or all characters but the last using s[:-1]. Hope that is enough to get you started!
Reasoning about recursion requires breaking the problem into "regular" and "special" cases. What are the special cases here? Well, if the string is empty, then char certainly isn't in the string. Return 0 in that case.
Are there other special cases? Not really! If the string isn't empty, you can break it into its first character (the_string[0]) and all the rest (the_string[1:]). Then you can recursively count the number of character occurrences in the rest, and add 1 if the first character equals the char you're looking for.
I assume this is an assignment, so I won't write the code for you. It's not hard. Note that your if str == 0: won't work: that's testing whether str is the integer 0. if len(str) == 0: is a way that will work, and if str == "": is another. There are shorter ways, but at this point those are probably clearest.
First of all you I would suggest not using char or str. Str is a built function/type and while I don't believe char would give you any problems, it's a reserved word in many other languages. Second you can achieve the same functionality using count, as in :
letterstring="This is a string!"
letterstring.count("i")
which would give you the number of occurrences of i in the given string, in this case 3.
If you need to do it purely for speculation, the thing to remember with recursion is carrying some condition or counter over which each call and placing some kind of conditional within the code that will change it. For example:
def countToZero(count):
print(str(count))
if count > 0:
countToZero(count-1)
Keep it mind this is a very quick example, but as you can see on each call I print the current value and then the function calls itself again while decrementing the count. Once the count is no longer greater than 0 the function will end.
Knowing this you will want to keep track of you count, the index you are comparing in the string, the character you are searching for, and the string itself given your example. Without doing the code for you, I think that should at least give you a start.
You have to decide a base case first. The point where the recursion unwinds and returns.
In this case the the base case would be the point where there are no (further) instances of a particular character, say X, in the string. (if string.find(X) == -1: return count) and the function makes no further calls to itself and returns with the number of instances it found, while trusting its previous caller information.
Recursion means a function calling itself from within, therefore creating a stack(at least in Python) of calls and every call is an individual and has a specified purpose with no knowledge whatsoever of what happened before it was called, unless provided, to which it adds its own result and returns(not strictly speaking). And this information has to be supplied by its invoker, its parent, or can be done using global variables which is not advisable.
So in this case that information is how many instances of that particular character were found by the parent function in the first fraction of the string. The initial function call, made by us, also needs to be supplied that information, since we are the root of all function calls and have no idea(as we haven't treaded the string) of how many Xs are there we can safely tell the initial call that since I haven't gone through the string and haven't found any or zero/0 X therefore here's the string entire string and could you please tread the rest of it and find out how many X are in there. This 0 as a convenience could be the default argument of the function, or you have to supply the 0 every time you make the call.
When will the function call another function?
Recursion is breaking down the task into the most granular level(strictly speaking, maybe) and leave the rest to the (grand)child(ren). The most granular break down of this task would be finding a single instance of X and passing the rest of the string from the point, exclusive(point + 1) at which it occurred to the next call, and adding 1 to the count which its parent function supplied it with.
if not string.find(X) == -1:
string = string[string.find(X) + 1:]
return countLetterString(char, string, count = count + 1)`
Counting X in file through iteration/loop.
It would involve opening the file(TextFILE), then text = read(TextFile)ing it, text is a string. Then looping over each character (for char in text:) , remember granularity, and each time char (equals) == X, increment count by +=1. Before you run the loop specify that you never went through the string and therefore your count for the number X (in text) was = 0. (Sounds familiar?)
return count.
#This function will print the count using recursion.
def countrec(s, c, cnt = 0):
if len(s) == 0:
print(cnt)
return 0
if s[-1] == c:
countrec(s[0:-1], c, cnt+1)
else:
countrec(s[0:-1], c, cnt)
#Function call
countrec('foobar', 'o')
With an extra parameter, the same function can be implemented.
Woking function code:
def countLetterString(char, str, count = 0):
if len(str) == 0:
return count
if str[-1] == char:
return countLetterString(char, str[0:-1], count+1)
else:
return countLetterString(char, str[0:-1], count)
The below function signature accepts 1 more parameter - count.
(P.S : I was presented this question where the function signature was pre-defined; just had to complete the logic.)
Hereby, the code :
def count_occurrences(s, substr, count=0):
''' s - indicates the string,
output : Returns the count of occurrences of substr found in s
'''
len_s = len(s)
len_substr = len(substr)
if len_s == 0:
return count
if len_s < len_substr:
return count
if substr == s[0:len_substr]:
count += 1
count = count_occurrences(s[1:], substr, count) ## RECURSIVE CALL
return count
output behavior :
count_occurences("hishiihisha", "hi", 0) => 3
count_occurences("xxAbx", "xx") => 1 (not mandatory to pass the count , since it's a positional arg.)
so basically a guy helped me improve my code. The problem is, it's still dead disappointing and does not work. What I wanna do, is reset the lenRecur.number so I could use the function again, using other strings and getting correct answers (not too big answers)
I guess, the problem is with hasattr. But I can't remove it, because if I do, my string length calculator won't work.
Anyway, even if I add lenRecur.number = 0 after the function, it still doesn't work.
It's like impossible, because when the function hits "return", it's done, period. If I reset it before "return", it will return 0, not correct answer so yeah I'm in deep trouble here.
def lenRecur(aStr):
if not hasattr(lenRecur, 'number'):
lenRecur.number = 0
'''
aStr: a string
returns: int, the length of aStr
'''
if aStr == '':
return lenRecur.number
else:
lenRecur.number += 1
return lenRecur(aStr[:-1])
P.s. the goal of my program(?) / script(?) is to measure the length of input string, without using input() method. As trying to re-create the length() method, with using more primitive means.
The script will have to have many different inputs, so it shall reset.
If you just want a recursive strength-length function, that's easy:
def len_recur(a_str):
if not a_str:
return 0
else:
return 1 + len_recur(a_str[1:])
Of course that's not tail-recursive, but then Python doesn't optimize tail recursion anyway, so it doesn't matter.
And if you want it to be tail recursive just for the hell of it—or because you've read Paul Butler's trick for Tail Recursion in Python and want to try it out—you still don't want to do that by storing the accumulator as an attribute of the function. Just use the usual trick of defining a local function (or using a mutable default parameter, if you prefer):
def len_tail_recur(a_str):
def tail(a_str, acc):
if not a_str:
return acc
else:
return tail(a_str[1:], acc+1)
return tail(a_str, 0)
If you want to convert this to a real tail-recursive function so it doesn't bomb on lists of 1001 elements, and don't understand the Paul Butler link above, see my answer to Get length of list in Python using recursion, which solves exactly this problem. (Another answer to that question also shows how to solve the problem with log N recursive calls instead of N, which is a different way of getting around the problem, unless you have impossibly long lists.)
All that being said, even though your implementation is the wrong way to do it, it actually works just fine. (So far, I've been PEP8-ifying your code to look more like idiomatic Python; from here on I'm just going to copy-paste as-is, but your real code should look like the above.)
def lenRecur(aStr):
if not hasattr(lenRecur, 'number'):
lenRecur.number = 0
'''
aStr: a string
returns: int, the length of aStr
'''
if aStr == '':
return lenRecur.number
else:
lenRecur.number += 1
return lenRecur(aStr[:-1])
print lenRecur('abc')
lenRecur.number = 0
print lenRecur('abcd')
This prints 3, then 4. Of course you have to set lenRecur.number from outside the function, because inside the function you still need the value. But you can solve that with the same kind of wrapper:
def lenRecur(aStr):
lenRecur.number = 0
'''
aStr: a string
returns: int, the length of aStr
'''
def recur(aStr):
if aStr == '':
return lenRecur.number
else:
lenRecur.number += 1
return recur(aStr[:-1])
return recur(aStr)
You don't have to use a state variable inside the function. If you want to make a recursive length calculator, then just do
def lenRecur (aStr):
if (aStr == ""):
return 0
else
return lenRecur (aStr [:-1]) + 1
Also note that this style has no error checking etc, but for the purposes of learning about recursion, it works fine.
If you're trying to understand recursion by implementing the length function using recursion then you can use something like this:
#!python
def lenRecur(something, curlen=0):
if something:
return lenRecur(something[1:], curlen+1)
else:
return curlen
... I won't claim that this is particularly good code. But it should work with any sort of sequence (string, list, tuple) ... anything for which the [1:] slice operation will work so long as it doesn't exceed the maximum recursion limit in your running Python instance.
In your example you're trying to implement a similar concept by using hasattr to "monkey patch" your function's object with a "number" attribute. In my example I'm using a default parameter as a way of passing the variable down into the recursive calls.
So in the initial call curlen is zero (calling this with the "optional" extra argument would give bogus results). From that call the function calls itself with a slice of the original sequence (string) that lops off the head (making it one shorter) and with the optional (curlen) argument incremented. At the end the string/sequence is of zero length, the zero is returned up through each of the previous (recursive) calls.
It's a lame way to accomplish this, and it could be a starting point for a discussion on tail recursion elimination (Google for it). But it will work without monkey patching your function/object.
I need to write a recursive function printPattern() that takes an integer n as a parameter and prints n star marks followed by n exclamation marks, all on one line. The function should not have any loops and should not use multiplication of strings. The printing of the characters should be done recursively only. The following are some examples of the behavior of the function:
>>>printPattern(3)
***!!!
>>>printPattern(10)
**********!!!!!!!!!!
This is what I have at the moment
def printPattern(n):
if n < 1:
pass
else:
return '*'*printPattern(n)+'!'*printPattern(n)
I know I am completely off, and this would be easier without recursion, but it is necessary for my assignment.
Q: What's printPattern(0)?
A: Nothing.
Q: What's printPattern(n), for n>=1?
A: *, then printPattern(n-1), then !.
Now you should be able to do it. Just remember to think recursively.
Recursion is based on two things:
a base case
a way to get an answer based off something closer to the base case, given something that's not the base case.
In your case, the simplest base case is probably 0 - which would print thing (the empty string). So printPattern(0) is ''.
So how do you get closer to 0 from your input? Well, probably by reducing it by 1.
So let's say that you are currently at n=5 and want to base your answer off something closer to the base case - you'd want to get the answer for n=5 from the one for n=4.
The output for n=5 is *****!!!!!.
The output for n=4 is ****!!!!.
How do you get from the output of n=4 to n=5? Well, you add a * on the front and a ! on the end.
So you could say that printPattern(5) is actually just '*' + printPattern(4) + '!'.
See where this is going?
Try this:
def printPattern(n):
if n <= 0:
return ''
return '*' + printPattern(n-1) + '!'
print printPattern(5)
> *****!!!!!