Python Forum
Accumulator/looping problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accumulator/looping problem
#1
defining a function that considers every pair of consecutive characters in s. It returns a string with the letters o and p inserted between every pair of consecutive characters of s
-if the first character in the pair is uppercase, it inserts an uppercase O, if it's lowercase, it inserts the lowercase o.
-if the second character is uppercase, it inserts an uppercase P, if it's lowercase, it inserts the lowercase p.
-if at least one of the character is not a letter in the alphabet, it does not insert anything between that pair.
-if s has one or less characters, the function returns the same string as s.

This is how far I got to tackling this problem, although it doesn't give what is expected (after my code below there are some examples of what the function should do)

def oPify(s):
     newS= ''

     if s<=1:
          return s
     
     for i in s:
          newS = newS + s[i] + 'op'
     return newS    
Examples:
>>> oPify("aa")
’aopa’
>>> oPify("aB")
’aoPB’
>>> oPify("ooo")
’oopoopo’
>>> oPify("ax1")
’aopx1’
>>> oPify("abcdef22")
’aopbopcopdopeopf22’
>>> oPify("abcdef22x")
’aopbopcopdopeopf22x’
>>> oPify("aBCdef22x")
’aoPBOPCOpdopeopf22x’
>>> oPify("x")
’x’
>>> oPify("123456")
’123456’
Reply
#2
I'm not sure what your if statement is supposed to do. At the moment it will just cause an error. Perhaps you mean if len(s)<= 1:?

Note that the examples given only insert between letters. Going one letter at a time isn't going to work. You need to look at pairs of letters. Have you learned zip yet? If so, zip the string to the string with the first character missing. If not, you will have to loop through the indexes of the string, pulling the current index and the next index. Then you need to check both characters to make sure they are letters, and then check their case to figure out the case of the letters op that you will insert.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Oct-15-2017, 06:24 PM)ichabod801 Wrote: I'm not sure what your if statement is supposed to do. At the moment it will just cause an error. Perhaps you mean if len(s)<= 1:?

Note that the examples given only insert between letters. Going one letter at a time isn't going to work. You need to look at pairs of letters. Have you learned zip yet? If so, zip the string to the string with the first character missing. If not, you will have to loop through the indexes of the string, pulling the current index and the next index. Then you need to check both characters to make sure they are letters, and then check their case to figure out the case of the letters op that you will insert.

Can you show me how to do so with loops (i haven't learned zip) please? or show me another example
Reply
#4
I can show you how to do it with loops. I would prefer you try to do it with the two loops I described, and then I can help you fix any problems.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Oct-15-2017, 10:20 PM)ichabod801 Wrote: I can show you how to do it with loops. I would prefer you try to do it with the two loops I described, and then I can help you fix any problems.

This is what I have so far:

def oPify(s):
    newS= ''
 
    if len(s)<=1:
          return s
    for i in range(0, len(s), 2):
        newS= newS+s[i:i+2]
        if i.isalpha() in newS:
            if i.lower():
                s= s[i]+'op'+s[i+2]
Reply
#6
You're still trying to do it in one. You need to do it in two. Write two loops, one that goes through the indexes of the string, and an inner one that goes through the possible lengths from that index.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  accumulator pattern python_learner 8 7,503 Mar-10-2017, 11:11 AM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020