Some source code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nnes

    Some source code

    For those that like to read source code, or just wondered how a C programmer
    would use Python I post some code written by a friend which I pestered to at
    least try Python out. He did not seem too impressed after the exercise, but said
    it was not that painfull :-).

    It might be of interest to those wondering how python is being used and misused
    :-). I guess teachers using Python to teach can confirm or deny the benefits of
    certain language decisions daily by looking at the source code of their
    students.

    Anyway here we go:

    #
    # Reading File and loading it

    #Hashing a string with a specified separator
    # and storing results in an Array
    #
    def HashString(str, sep):
    lim = len(str)
    array=[]
    indexes=[]
    cptr =0
    for i in range(lim):
    if str[i]== sep and i < lim:
    indexes.append( i)
    cptr+=1

    lim = len(indexes)
    cptr =j=0
    for i in range(lim-1):
    if(str[j] == sep and j ==0) :
    j+=1
    cptr+=1
    array.append(st r[j:indexes[cptr]])
    j=indexes[cptr]+1
    cptr+=1

    return array;
    #
    # End of HashString Function
    #

    File= open('input.txt ','r')
    content=[]
    content = File.readlines( )
    sum =0
    Result = []
    OFile = open('output.tx t','w')
    for i in range(len(conte nt)):
    result = HashString(cont ent[i],'\"')
    cptr=sum = 0
    for j in range(len(resul t)):
    sum += int(result[cptr])
    if(j>0):
    OFile.write("," )
    OFile.write("\" "+result[cptr]+"\"")
    cptr+=2
    if cptr >= len(result) : break;
    OFile.write(",\ ""+sum.__str__( )+"\"\n")
    result.append(s um)
    print result
    #
    # Creating Ouput file
    #
    OFile.close();
    File.close();
    print "\n....\tdo ne view output in \'output.txt\'. ......\n"


    ############### ############### #########

    Roughly does this:

    file('output.tx t','w').writeli nes(['%s,"%s"\n' % (line[:-1],
    sum(map(int,lin e[1:-2].split('","'))) ) for line in
    file('input.txt ')])
  • Matt Gerrans

    #2
    Re: Some source code

    I like the clever use of 'cptr' -- that would be silly in any language.


    Comment

    • Adam Przybyla

      #3
      Re: Some source code


      nnes <pruebauno@lati nmail.com> wrote:[color=blue]
      > file('output.tx t','w').writeli nes(['%s,"%s"\n' % (line[:-1],
      > sum(map(int,lin e[1:-2].split('","'))) ) for line in
      > file('input.txt ')])[/color]
      file('output.tx t','w').writeli nes(['%s,"%s"\n' %
      (line[:-1],sum(eval(line[1:-2]))) for line in file('input.txt ')])
      Regards
      Adam Przybyla

      Comment

      • Jarek Zgoda

        #4
        Re: Some source code

        Adam Przybyla <adam@gliwice.p l> pisze:
        [color=blue][color=green]
        >> file('output.tx t','w').writeli nes(['%s,"%s"\n' % (line[:-1],
        >> sum(map(int,lin e[1:-2].split('","'))) ) for line in
        >> file('input.txt ')])[/color]
        > file('output.tx t','w').writeli nes(['%s,"%s"\n' %
        > (line[:-1],sum(eval(line[1:-2]))) for line in file('input.txt ')])[/color]

        You still call this code "pythonic"?

        --
        Jarek Zgoda

        Comment

        • nnes

          #5
          Re: Some source code

          Adam Przybyla <adam@gliwice.p l> wrote in message news:<c1adgj$qg l$1@atlantis.ne ws.tpi.pl>...[color=blue]
          > nnes <pruebauno@lati nmail.com> wrote:[color=green]
          > > file('output.tx t','w').writeli nes(['%s,"%s"\n' % (line[:-1],
          > > sum(map(int,lin e[1:-2].split('","'))) ) for line in
          > > file('input.txt ')])[/color]
          > file('output.tx t','w').writeli nes(['%s,"%s"\n' %
          > (line[:-1],sum(eval(line[1:-2]))) for line in file('input.txt ')])
          > Regards
          > Adam Przybyla[/color]
          You mean:

          file('output.tx t','w').writeli nes(['%s,"%s"\n' % (line[:-1],
          sum(eval(line[1:-2].replace('"','' )))) for line in file('input.txt ')])

          input.txt in the form of:
          "1","2","3"
          "4","5","6"

          And both versions are exactly 132 bytes long. It is a draw :-P

          Comment

          • Matt Gerrans

            #6
            Re: Some source code

            > You still call this code "pythonic"?

            May as well do it in Perl, where it will be shorter and even more
            unreadable!


            Comment

            Working...