how to iterate through each set

Tom_chicollegeboy tbabula at gmail.com
Sun Nov 4 01:58:16 EDT 2007


On Nov 4, 12:47 am, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Sat, 03 Nov 2007 21:30:10 -0700, Tom_chicollegeboy
> <tbab... at gmail.com> declaimed the following in comp.lang.python:
>
>
>
> > It works only for first set. How should I implement another solution
> > that would move program to read next set? I think I should implement
> > nested while look, with first that reads until -1 and under it another
> > while loop that reads n times sets. But I have no idea how. Can you
> > help me?
>
>         Homework, huh? So I need to avoid a working solution...
>
>         Personally, based upon the nature of the data file, I'd base the
> entire algorithm on a control break which is signified by a line with a
> single "term", and ignore the "count" value of that term.
>
>         I'd have to ignore the first "control break" as there is no output
> at that time. That can be done by initially setting the total mileage to
> None (not to 0).
>
>         On a control break (len(input.split()) == 1): if total mileage is
> NOT None, output results; set total mileage and elapsed time to 0.
>
>         On a data line (len(input.split()) == 2): total mileage = total
> mileage + speed * (current time - elapsed time); elapsed time = current
> time.
>
>         Exit on EOF.
>
>         This algorithm will function even with erroneous input:
>
> 4                               <== only three actual values follow
> 20 2
> 30 6
> 10 7
> 2
> 60 1
> 30 5
> -1
>
>         Calling the function "speed" is misleading too -- the /input/ is
> speed and time, but the output wanted is mileage.
>
>         With the following data:
> -=-=-=-=-=-=-=-
> 4
> 20 2
> 30 6
> 10 7
> 2
> 60 1
> 30 5
> -1
> 10 1
> 25 1
> 5 10
> -=-=-=-=-=-=-=-=-
>
>         A quick program produces the following output (NOTE: I added a LOT
> of ouput statements so you can see the logic -- but I won't show the
> actual code).
>
> initialize total None, elapsed 0, trip count 0
> open data file
> loop over lines in input file, exit on null line
> input line is: [4]
> number of values in the line: 1
> one value, control break
> no trip data read before this control break
> resetting total mileage 0, elapsed 0, incrementing trip count 1
> input line is: [20, 2]
> number of values in the line: 2
> elapsed miles: 40
> input line is: [30, 6]
> number of values in the line: 2
> elapsed miles: 160
> input line is: [10, 7]
> number of values in the line: 2
> elapsed miles: 170
> input line is: [2]
> number of values in the line: 1
> one value, control break
> trip 1: 170 miles
> resetting total mileage 0, elapsed 0, incrementing trip count 2
> input line is: [60, 1]
> number of values in the line: 2
> elapsed miles: 60
> input line is: [30, 5]
> number of values in the line: 2
> elapsed miles: 180
> input line is: [-1]
> number of values in the line: 1
> one value, control break
> trip 2: 180 miles
> resetting total mileage 0, elapsed 0, incrementing trip count 3
> input line is: [10, 1]
> number of values in the line: 2
> elapsed miles: 10
> input line is: [25, 1]
> number of values in the line: 2
> elapsed miles: 10
> input line is: [5, 10]
> number of values in the line: 2
> elapsed miles: 55
> No terminating control break found
> Unfinished trip is:
> trip 3: 55 miles
>
>         By the way -- YOUR sample input data has ONLY TWO TRIPS -- so how
> can the correct output be THREE trips?
> --
>         Wulfraed        Dennis Lee Bieber               KD6MOG
>         wlfr... at ix.netcom.com             wulfr... at bestiaria.com
>                 HTTP://wlfraed.home.netcom.com/
>         (Bestiaria Support Staff:               web-a... at bestiaria.com)
>                 HTTP://www.bestiaria.com/

very helpful hints. thank you very much




More information about the Python-list mailing list