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(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[i],'\"')
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')]) 5 1340
I like the clever use of 'cptr' -- that would be silly in any language.
nnes <pr*******@latinmail.com> wrote: 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
Adam Przybyla <ad**@gliwice.pl> pisze: 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"?
--
Jarek Zgoda http://jpa.berlios.de/
Adam Przybyla <ad**@gliwice.pl> wrote in message news:<c1**********@atlantis.news.tpi.pl>... nnes <pr*******@latinmail.com> wrote: 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
> You still call this code "pythonic"?
May as well do it in Perl, where it will be shorter and even more
unreadable! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Edilmar |
last post by:
Hi,
First of all, I'm new in Python...
I have worked with manu langs and IDEs, like Delphi, VB, JBuilder, Eclipse,
Borland C++, Perl, etc...
Then, today I think IDEs like Delphi have a...
|
by: svilen |
last post by:
hello again.
i'm now into using python instead of another language(s) for
describing structures of data, including names, structure,
type-checks, conversions, value-validations, metadata etc....
|
by: |
last post by:
gcc provides the libc which have more functions than standard C libraries.
I saw in Windows API similar functions too, with identical parameters!
So, I ask again:
Why not, a committee specifies...
|
by: Lea |
last post by:
Not only does my boss want to hide files from some
developers, he wants to be able to hide different files
from different users. We already use source control to
ensure that people cannot check...
|
by: Michael B. |
last post by:
I was just thinking about this, specifically wondering if there's any
features that the C specification currently lacks, and which may be
included in some future standardization.
Of course, I...
| |
by: To Forum |
last post by:
HI,
I have the source code from SourceForge and try to use .NET 2003 to compile
the source code but there are errors and I could not go though the
compilation.
Have some one had this problem?...
|
by: mike |
last post by:
I help manage a large web site, one that has over
600 html pages... It's a reference site for ham radio
folks and as an example, one page indexes over
1.8 gb of on-line PDF documents.
The site...
|
by: GeorgeRXZ |
last post by:
Hi Friends,
I have some questions related to C Language.
1What is the difference between the standard C language and Non
standard C language ?
2which is better C Lanugage, C under Linux/...
|
by: thatos |
last post by:
When I run this program
import au.id.jericho.lib.html.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class DisplayAllElements {
public static void main(String args)...
|
by: Michael Schöller |
last post by:
Hello,
First of all english is not my natural language so please fogive me some bad
mistakes in gramatic and use of some vocables :).
I have a great problem here. Well I will not use it...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |