[Tutor] Difficult loop?

Emad Nawfal (عماد نوفل) emadnawfal at gmail.com
Thu Oct 16 03:32:49 CEST 2008


2008/10/15 Emad Nawfal (عماد نوفل) <emadnawfal at gmail.com>

>
>
> 2008/10/15 John Fouhy <john at fouhy.net>
>
>> 2008/10/16 Emad Nawfal (عماد نوفل) <emadnawfal at gmail.com>:
>> > One more question,
>> > I'm a linguistics person, I know some Java and some Python (and failed
>> to
>> > learn Prolog). What things do I need to learn to write such "difficult"
>> > scripts? Are there any recommendations? books, certain things to learn?
>>
>> Well, there's a few tutorials around that you could work through.  The
>> best way to learn, in my opinion, is to find something not-too-hard
>> that you want to do and then start building it.  Combine that with the
>> documentation and ask questions on this list when you get stuck.
>>
>> For example, you could look at the code I wrote and figure out how to
>> make the last change yourself :-)
>>
>
>
> I will.
> Thank you so much for your helpfulness.
>
>>
Hi John and Tutors,
I have added a function notDouble, and changed a couple of  lines (marked
below), and the results are exactly what I want. Please let me know whether
this is the right, or the good, way to do it.

shortVowels = 'aiuo'
double = '~'
def notDouble(c):  # I added this function
    return c not in double
def notSV(c):
   return c not in (shortVowels + '+' )

def nextSV(s, idx):
   """ Find the next short vowel, or ~ + short vowel.

   s :: string to search
   idx :: index to start from

   Return: short vowel, or ~ + short vowel, or _ if no following short
vowel.
   """

   try:
       if s[idx+1] in shortVowels:
           return s[idx+1]
       elif s[idx+1] == double:
           return s[idx+1:idx+3]
       else:
           return '_'
   except IndexError:
       return '_'

def processChar(s, idx):
   """ Process a single character of s according to the rules.

   s :: string to work with
   idx :: index to start from

   Output tuple: (preceeding, following, last)
   """

   preceeding = filter(notSV, s[:idx])[-5:]
   preceeding = filter(notDouble, preceeding)[-5:] # I added this
   preceeding = '_'*(5-len(preceeding)) + preceeding

   following = filter(notSV, s[idx+1:])[:5]
   following = filter(notDouble, following)[:5] # I added this
   following = following + '_'*(5-len(following))

   last = nextSV(s, idx)

   return preceeding, following, last

def process(s):
   """ Process and print string according to the rules. """

   for i, c in enumerate(s):
       if c in shortVowels + '+' + double: # I added double
           continue
       preceeding, following, last = processChar(s, i)
       print '  '.join(preceeding + c + following) + '  ' + last

if __name__ == '__main__':
   test = "AlmutasAhilwn"
   process(test)

>
>> --
>> John.
>>
>
>
>
> --
> لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
> الغزالي
> "No victim has ever been more repressed and alienated than the truth"
>
> Emad Soliman Nawfal
> Indiana University, Bloomington
> http://emnawfal.googlepages.com
> --------------------------------------------------------
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com
--------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081015/bb65b533/attachment-0001.htm>


More information about the Tutor mailing list