Reputation: 49
I need to write a python function which capitalizes every even word within a string sentence, and also reverses every odd word within that string. For example:
aString = "Michelle ma belle these are words that go together well"
bString = "MICHELLE am BELLE eseht ARE sdrow THAT og TOGETHER llew "
I have a VERY basic understanding of how to do this but not really.
Here is what I have so far...
def RewordProgram(a):
L = input("enter a sentence, please")
if L[0:][::2]: # for even words
b = L.upper()
return b
else if L[1:][::2]: # for odd words
c = L[::-1]
return c
Can someone please help me understand what I am doing wrong? The if else function isn't working for me, and I have no idea how of how to re-compile b and c into a new string. Is that even possible?
Upvotes: 4
Views: 6421
Reputation: 2934
A fundamental rule in computer science is breaking problems down into subproblems. In your case, I recommend that you start by breaking the solution to the problem into several functions:
words
that takes the string as an argument and returns a list of words contained in it.upcase
that returns a string in upper case.reverse
that reverses a string.Once you've wrote these functions you'll need some other function to glue the pieces together. You'll need:
enumerate
).map
).I've included complete code below. I recommend you don't read it right away but use it as a help when you're stuck at one of the steps above.
# This function reverses the word. It's a bit tricky: reversed returns an
# enumerator representing a sequence of individual characters. It must be
# converted into a string by joining these characters using an empty string.
def reverse(string):
'''Reverse a string.'''
return ''.join(reversed(string))
# Upcasing is simpler. Normally, I would've inlined this method but I'm leaving
# it here just to make it easier for you to see where upcasing is defined.
def upcase(string):
'''Convert the string to upcase.'''
return string.upper()
# Normally, I'd use the split function from the re module but to keep things
# simple I called the split method on the string and told it to split the string
# at spaces.
def words(string):
'''Return a list of words in a string.'''
return string.split(' ')
# This is your function. It works as follows:
#
# 1. It defines an inner function _process_word that takes the index of the word
# and the word itself as arguments. The function is responsible for
# transforming a single word.
# 2. It splits the string into words by calling words(string).
# 3. It adds a zero-based index to each of the words with enumerate.
# 4. It applies _process_word to each and every item of the sequence in 3.
# 5. It joins the resulting words with spaces.
def capitalize_and_reverse(string):
def _process_word((index, word)):
if index % 2 == 0:
return upcase(word)
else:
return reverse(word)
return ' '.join(map(_process_word, enumerate(words(string))))
string = "Michelle ma belle these are words that go together well"
print capitalize_and_reverse(string)
# MICHELLE am BELLE eseht ARE sdrow THAT og TOGETHER llew
Upvotes: 2
Reputation: 4137
In your code you are looking at single characters, not words. L
is a string, so you need to use split()
to get the words. It's always a good idea to print out as much as possible if you are new to a problem. Printing the value of L[0:]
and L[0:][::2]
would be very helpful and something that tells you what path the execution takes.
L[0:][::2]
actually just returns every second character of the entire string. L[0:]
is the same as L
since it creates a string from index 0 to the end of the string... then [::2]
skips every second character.
print
is your friend! .. maybe combined with using a debugger, but prints will also do the job.
You can solve the problem with a generator expression:
text = "Michelle ma belle these are words that go together well"
r = ' '.join(w[::-1] if i % 2 else w.upper() for i, w in enumerate(text.split()))
print(r)
The verbose version of this would be (less scary):
words = []
for i, w in enumerate(text.split()):
# odd
if i % 2:
words.append(w[::-1])
else:
words.append(w.upper())
print(" ".join(words))
Also note the use of enumerate
that will return an (index, value)
tuple for each iteration.
Upvotes: 2