[Tutor] Difficult loop?

Emad Nawfal (عماد نوفل) emadnawfal at gmail.com
Thu Oct 16 01:25:19 CEST 2008


2008/10/15 John Fouhy <john at fouhy.net>

> 2008/10/16 Emad Nawfal (عماد نوفل) <emadnawfal at gmail.com>:
> > Dear Tutors,
> > I needed a program to go through  a word like this "Almuta$r~id"
> >
> > 1-  a, i,u,  and o are the short vowels
> > 2-  ~ is the double consonant.
> > I wanted the program to do the following:
> >
> > - For each letter of the word, if the letter is not a short vowel, print
> the
> > letter with the 5 preceding letters (that are not short vowels),  then
> the
> > letter itself, then the 5 following letters (no short vowels), then the
> > short vowel following the letter.
> > - If the letter is followed by the double character "~", do the same, but
> > instead of printing just the vowel following the letter as the last
> > character, print the "~" + the short vowel
> > -  if the letter is not followed by a vowel, print an underscore as the
> last
> > character.
> >
> > - if there a "+" sign, ignore it.
> > - If there are fewer than  5 preceding or following letters, print an
> > underscore in place of each missing letter.
> >
> > For example, the word "Almuta$r~id" would be printed as follows:
> >
> > _  _  _  _  _  A  l  m  t  $  r  _
> > _  _  _  _  A  l  m  t  $  r  d  _
> > _  _  _  A  l  m  t  $  r  d  _  u
> > _  _  A  l  m  t  $  r  d  _  _  a
> > _  A  l  m  t  $  r  d  _  _  _  a
> > A  l  m  t  $  r  d  _  _  _  _  ~i
> > l  m  t  $  r  d  _  _  _  _  _  _
>
> Hi Emad,
>
> I had a crack at this.  My solution is below.  I think it is simpler
> than the one your friend provided :-)  Although I think there is
> something I don't understand about your rules, because I get slightly
> different results for your sample word:
>
> _  _  _  _  _  A  l  m  t  $  r  u
> _  _  _  _  A  l  m  t  $  r  ~  u
> _  _  _  A  l  m  t  $  r  ~  d  u
> _  _  A  l  m  t  $  r  ~  d  _  a
> _  A  l  m  t  $  r  ~  d  _  _  ~i
> A  l  m  t  $  r  ~  d  _  _  _  ~i
> l  m  t  $  r  ~  d  _  _  _  _  i
> m  t  $  r  ~  d  _  _  _  _  _  _
>
> Let me know if you have any trouble understanding any of the code:
>
> #####
>
> shortVowels = 'aiuo'
> 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.
>    """
>
>    for i, c in enumerate(s[idx+1:]):
>        if c in shortVowels:
>            return c
>        elif c == double:
>            # return c and the character after it.
>            # assuming ~ is always followed by a short vowel
>            return s[i+idx+1:i+idx+3]
>
>    # If we exit the loop without finding a short vowel or a ~
>    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 = '_'*(5-len(preceeding)) + preceeding
>
>    following = filter(notSV, s[idx+1:])[:5]
>    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 + '+':
>            continue
>        preceeding, following, last = processChar(s, i)
>        print '  '.join(preceeding + c + following) + '  ' + last
>
> if __name__ == '__main__':
>    test = "Almuta$r~id"
>    process(test)
>

Thank you so much John for thinking about this. The difference is here:
_  _  _  _  _  A  l  m  t  $  r  u
_  _  _  _  A  l  m  t  $  r  ~  u
_  _  _  A  l  m  t  $  r  ~  d  u

The focus  letter will always be # 6 on the line. A is not a short vowel,
and it is not followed by a short vowel, so the last character should be
"_", not a "u"

The same is true for lines 2 and 3 . The letter "l" is not followed by a
short vowel, so the last character should be "_"

Same for line 3 as well. "m" is not a short vowel, and it is not followed by
a short vowel

The purpose of this program is to calculate vector similarities in the
context of the letters in a machine learning approach called "Memory-based
learning"
-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"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/f4111106/attachment.htm>


More information about the Tutor mailing list