Building Time Based Bins

John Machin sjmachin at lexicon.net
Sun Mar 20 05:26:58 CET 2005


On 19 Mar 2005 19:01:05 -0800, "MCD" <mcdesigns at walla.com> wrote:

>Hello, I'm new to python and this group and am trying to build some
>bins and was wondering if any of you could kindly help me out. I'm a
>bit lost on how to begin.

Are you (extremely) new to computer programming? Is this school
homework? The reason for asking is that the exercise requires no data
structure more complicated than a one-dimensional array of integers
(if one doubts that the times will always be in ascending order), and
*NO* data structures if one is trusting. It can be done easily without
any extra modules or libraries in just about any computer language
ever invented. So, it's not really a Python question. Perhaps you
should be looking at some basic computer programming learning. Python
*is* a really great language for that -- check out the Python website.

Anyway here's one way of doing it -- only the input and output
arrangements are Python-specific. And you don't need iter*.*.* (yet)
:-)

HTH,
John
===========================
C:\junk>type mcd.py
# Look, Ma, no imports!
lines = """\
1231 23 56
1232 25 79
1234 26 88
1235 22 34
1237 31 85
1239 35 94
"""
DUMMY = 9999
bintm = DUMMY
for line in lines.split('\n'): # in practice, open('input_file', 'r'):
    if not line: continue
    ilist = [int(fld) for fld in line.strip().split()]
    print "ilist:", ilist
    klock, lo, hi = ilist
    newbintm = ((klock + 4) // 5 * 5) % 2400
    print "bintm = %d, klock = %d, newbintm = %d" % (bintm, klock,
newbintm)
    if newbintm != bintm:
        if bintm != DUMMY:
            print "==>> %04d %02d %02d" % (bintm, binlo, binhi)
        bintm, binlo, binhi = newbintm, lo, hi
    else:
        binlo = min(binlo, lo)
        binhi = max(binhi, hi)
print "end of file ..."
if bintm != DUMMY:
    print "==>> %4d %2d %2d" % (bintm, binlo, binhi)

C:\junk>python mcd.py
ilist: [1231, 23, 56]
bintm = 9999, klock = 1231, newbintm = 1235
ilist: [1232, 25, 79]
bintm = 1235, klock = 1232, newbintm = 1235
ilist: [1234, 26, 88]
bintm = 1235, klock = 1234, newbintm = 1235
ilist: [1235, 22, 34]
bintm = 1235, klock = 1235, newbintm = 1235
ilist: [1237, 31, 85]
bintm = 1235, klock = 1237, newbintm = 1240
==>> 1235 22 88
ilist: [1239, 35, 94]
bintm = 1240, klock = 1239, newbintm = 1240
end of file ...
==>> 1240 31 94

C:\junk>
================================






More information about the Python-list mailing list