Some source code

N

nnes

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== 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(str[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.txt','w')
for i in range(len(content)):
result = HashString(content,'\"')
cptr=sum = 0
for j in range(len(result)):
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(sum)
print result
#
# Creating Ouput file
#
OFile.close();
File.close();
print "\n....\tdone view output in \'output.txt\'.......\n"


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

Roughly does this:

file('output.txt','w').writelines(['%s,"%s"\n' % (line[:-1],
sum(map(int,line[1:-2].split('","')))) for line in
file('input.txt')])
 
A

Adam Przybyla

nnes said:
file('output.txt','w').writelines(['%s,"%s"\n' % (line[:-1],
sum(map(int,line[1:-2].split('","')))) for line in
file('input.txt')])
file('output.txt','w').writelines(['%s,"%s"\n' %
(line[:-1],sum(eval(line[1:-2]))) for line in file('input.txt')])
Regards
Adam Przybyla
 
J

Jarek Zgoda

Adam Przybyla said:
file('output.txt','w').writelines(['%s,"%s"\n' % (line[:-1],
sum(map(int,line[1:-2].split('","')))) for line in
file('input.txt')])
file('output.txt','w').writelines(['%s,"%s"\n' %
(line[:-1],sum(eval(line[1:-2]))) for line in file('input.txt')])

You still call this code "pythonic"?
 
N

nnes

Adam Przybyla said:
nnes said:
file('output.txt','w').writelines(['%s,"%s"\n' % (line[:-1],
sum(map(int,line[1:-2].split('","')))) for line in
file('input.txt')])
file('output.txt','w').writelines(['%s,"%s"\n' %
(line[:-1],sum(eval(line[1:-2]))) for line in file('input.txt')])
Regards
Adam Przybyla
You mean:

file('output.txt','w').writelines(['%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
 
M

Matt Gerrans

You still call this code "pythonic"?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top