An alternative approach (I found the Yorick's code to
be too slow for large # of calls) :
We can use file size to pick a random point in the
file. We can read and ignore text till next new line.
This will avoid outputting partial lines. Return the
next line (which I guess is still random :)).
In
mMem(filename)
getrandomline2("shaks12.txt")
Caveat : It will still skip 1st line during random
selection if its size exceed 4096 chars !!
--- Aditya Lal <[EMAIL PROTECTED]> wrote:
> An alternative approach (I found the Yorick's code
> to
> be too slow for large # of ca
A bug:
The function random.randint(a,b) include both ends
i.e. b is also included. Thus for file with single
line a=0,b=1 my algo will give an IndexError.
Significance of number 4096 :
file is stored in blocks of size 2K/4K/8K (depending
upon the machine). file seek for an offset goes block
by blo
Hi !!
I was trying to solve SPOJ (www.spoj.pl) problems - ADDREV (add reversed
numbers).
My solution clocked 0.58 seconds in SPOJ's computer as compared to best time
of 0.28. Interestingly my program spends 50% of its total execution time in
reading/parsing the input.
Following is the sample inpu
n.readlines()
for i in xrange(N) :
print solve(lines[i])
I will start working on psyco ... though not sure what is it ?
Thanks again Kent.
Cheers
Aditya
On 9/27/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> Aditya Lal wrote:
>
> > def rev(n) :
>
Hi Dick,
You are deleting from the SAME list that you are traversing. This results in
problems. You should just create a new list for the elements that are
well-formed.
astr = ...
lstA = ...
def wellFormed(word) :
for ch in word :
if ch not in astr :
return False
return True
fi
So, even though you created a new variable lstB it was actually modifying
lstA.
HTH
Aditya
On 10/12/07, Dick Moores <[EMAIL PROTECTED]> wrote:
>
> At 11:25 PM 10/11/2007, Aditya Lal wrote:
> >Hi Dick,
> >
> >You are deleting from the SAME list that you are traversing.
w.r.t. prob 2, there is no break/continue in the code that you have given. I
added the "break" statement after you remove the word from lstB and code
does seems to work for me.
if word in lstB:
lstB.remove(word)
print
print "Removed",
If the ease of use is the only answer then the size of the file should not
matter ideally. btw, how large is the file ? is it in MBs or GBs ? For
performance reasons, typically you should not have any problems using either
dictionary, array or list for file size of few KBs.
Like Kent said, if you
I think you need to use "raw_input" instead of "input". input "eval" the
input expression while "raw_input" just stores it. I find the module help
very handy when I am in doubt.
>>> print raw_input.__doc__
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is s
You can source the file in python provided that you make it python
friendly:-
STN_id[1]='AAA' instead of STN_id[1]=AAA
...
---
import re
# Read the file
fd = open('sitelocations')
lines = fd.readlines()
fd.close()
# Make it python friendly: put all values in 'single quotes'
cmd = '\n'.joi
On 10/27/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
>
>
> "Dick Moores" <[EMAIL PROTECTED]> wrote
>
> > if type(n) == 'int' or type(n) == 'long':
> > do something
>
> don't use strings
>
> if type(n) == int
>
> Or just use an instance of the same type:
>
> if type(n) == type(42)
>
> Alan G.
>
> __
the command and all your variables are populated ...
> exec(cmd)
>
>
> Thanks,
>
>
>
>
>
> > Thanks! This is helpful.. I like the RE approach as it's conceivable to
> > write a function...
> >
&g
On 10/27/07, John <[EMAIL PROTECTED]> wrote:
>
> The problem is the infies are also being used in a shell scripted
> environment, they are frequently updated and cannot be changed.
>
> So ideadly I could just define a function which sourced the file, assuming
> the variable names passed in the *arg
On 10/27/07, Dick Moores <[EMAIL PROTECTED]> wrote:
>
> Win XP, Python 2.5.1
>
>
> #!/usr/bin/env python
> #coding=utf-8
>
> n = 100 # 10 billion
> print "type of 10 billion is", type(n)
> n = 10 # 1 billion
> print "type of 1 billion is", type(n)
>
> raw_in
On 10/27/07, John <[EMAIL PROTECTED]> wrote:
>
> Note, i need the ns+1 because the 'source files are not zero indexed.
>
> On 10/27/07, John <[EMAIL PROTECTED] > wrote:
> >
> > Here's where I am:
> >
> >
> > def source(filename, vList):
> > """ takes a file object and a list of variables as input
On 10/29/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> Aditya Lal wrote:
> > or use types module
> >
> > import types
> >
> > if type(n) == types.IntType or type(n) == types.LongType :
> > blah!
>
> A few notes:
> - If you look at typ
On 10/31/07, Orest Kozyar <[EMAIL PROTECTED]> wrote:
>
> > Please post the entire traceback (omitting duplicate lines).
>
> Sorry, I should have included the traceback. I've revised the sample
> script
> so that it generates the traceback when run. The sample script is at the
> very bottom of thi
On 10/31/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> Aditya Lal wrote:
> > On 10/29/07, *Kent Johnson* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>
> wrote:
>
> > - Common Python practice is to prefer the least restrictive type
> check
> &
On 11/4/07, Thorsten Kampe <[EMAIL PROTECTED]> wrote:
>
> * linda.s (Sun, 4 Nov 2007 01:39:46 -0800)
> > On Nov 2, 2007 1:03 AM, ALAN GAULD <[EMAIL PROTECTED]> wrote:
> > > > > >I want to run an .exe file and get the output many times.
> > > >> Given that I know that you know about loops I have to
After quizzing newbies in C on swapping without 3rd variable, I found this
to be really *cool* construct to swap :)
x = 10
y = 20
x,y = y,x
--
Aditya
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
On Nov 13, 2007 8:29 AM, sith . <[EMAIL PROTECTED]> wrote:
> a = [[0,1,2,3,4,5],[1,2,3,4,5,6]]
> You cannot modify the same array when you are looping through it. You have
> to loop through the copy of the contents :- a[:].
>
> # Untested code
> for i in a[:]: # You are looping through the copy of
On Nov 13, 2007 7:06 PM, bob gailer <[EMAIL PROTECTED]> wrote:
> Aditya Lal wrote:
> > [snip]
>
> > for i in a[:] will make i point to the elements of the list
> To be more precise:
> a[:] is a copy of the list
> the for statement assigns each list element in t
On Nov 15, 2007 12:37 PM, sith . <[EMAIL PROTECTED]> wrote:
> a = [[1,2],[3,1.5],[5,6]]
> for i in a:
> print i
> if i[1]>i[0]:
> print "second index is larger"
> else:
> print "second index is smaller"
> [1, 2]
> second index is larger
> [3, 1.5]
> second index is smal
On Dec 3, 2007 4:29 PM, Ricardo Aráoz <[EMAIL PROTECTED]> wrote:
> Danny Yoo wrote:
> >> Hello:
> >> I'm seeing some strange behavior with lstrip operating
> >> on string representations of *nix-style file paths
> >> Example:
> > s = '/home/test/'
> > s1 = s.lstrip('/home')
> > s1
> >>
On Jan 10, 2008 11:11 AM, Allen Fowler <[EMAIL PROTECTED]> wrote:
> Hello,
>
> How can a make a python script run in "deamon mode"? (on a linux box)
>
> That is, I want to run the program via "python myfile.py" and have it drop
> me back to the command line. The program should continue running un
On 04/02/08 10:42 PM, "Eric Brunson" <[EMAIL PROTECTED]> wrote:
> dave selby wrote:
>> Hi all,
>>
>> I am not sure if this is a Python or bash issue :).
>>
>> In bash if I execute 'motion' with the following ...
>>
>> [EMAIL PROTECTED]:~/.kde/share/apps/kmotion$ motion &> /dev/null &
>> [1] 10
27 matches
Mail list logo