Author Topic: how long is it?  (Read 2275 times)

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
how long is it?
« on: 26 July 2006, 23:32 »
I have to write a program that does the following (for work):

* Reads in a file of x,y points
* computes the distance between each point
* outputs a total distance
* can deal with multiple segments (segments are separated by '>')

Here's an example of a 3 segment file:
Code: [Select]
>
3 4
9 8
1 -4
7 9
>
2 5
-4 -6
12 118
2 4
>
-6 -63
-6 -21
-6 5
-4 87
From this example, I would want the length of each line segment, and then the total length.

Ideally, it wouldn't matter whether there was a tab or a space or comma or whatever between each column, as long as each column is well-defined.  And it would have to handle files (and segments) of arbitrary size.

So:
1. What language fits this application?  I was thinking Perl, even though I don't know much Perl.  Although the columnar nature of the data suggests awk.

2. Is the best structure going to be some giant 40-line control loop ({while a != EOF} or whatever)?  Or is there a more subtle way to do it?

PLEASE, don't respond with any code, because I really do want to write this program myself, whatever language it ends up in.  I just need help with the program design.

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: how long is it?
« Reply #1 on: 28 July 2006, 21:44 »
Okay, then, go ahead and respond with code.  I don't know enough to step over the > and start over with the computations.

piratePenguin

  • VIP
  • Member
  • ***
  • Posts: 3,027
  • Kudos: 775
    • http://piratepenguin.is-a-geek.com/~declan/
Re: how long is it?
« Reply #2 on: 28 July 2006, 21:58 »
When you reach a '>', if it's not at the beginning of the file, add the answer-so-far-for-this-segment to the complete-answer-so-far and then set answer-so-far-for-this-segment to 0 and continue (I smell a recursive function).....
"What you share with the world is what it keeps of you."
 - Noah And The Whale: Give a little love



a poem by my computer, Macintosh Vigilante
Macintosh amends a damned around the requested typewriter. Macintosh urges a scarce design. Macintosh postulates an autobiography. Macintosh tolls the solo variant. Why does a winter audience delay macintosh? The maker tosses macintosh. Beneath female suffers a double scum. How will a rat cube the heavier cricket? Macintosh calls a method. Can macintosh nest opposite the headache? Macintosh ties the wrong fairy. When can macintosh stem the land gang? Female aborts underneath macintosh. Inside macintosh waffles female. Next to macintosh worries a well.

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: how long is it?
« Reply #3 on: 28 July 2006, 22:18 »
Where do I even start?
Set total=0.  Get the first line.  If that line is a >, then move to the next line.  Set subtotal[0]=0.  Take the first number and put it into var[0], and the second number, and put it into var[1].  Move to the next line, and put the first number into var[2] and the second number into var[3].  Calculate the distance using var[0], [1], [2], [3].  Add the distance to subtotal[0].  Set var[0]=var[2] and var[1]=var[3].  Move to the next line and put the first number into var[2] and the second number into var[3].  Unless the next line starts with a >, then add subtotal[0] to total...

There's so many different places where a different looping process starts, or overlaps with another one.  I'm beginning to wonder if this is beyond me.

piratePenguin

  • VIP
  • Member
  • ***
  • Posts: 3,027
  • Kudos: 775
    • http://piratepenguin.is-a-geek.com/~declan/
Re: how long is it?
« Reply #4 on: 29 July 2006, 01:03 »
Quote from: worker201
Where do I even start?
Set total=0.  Get the first line.  If that line is a >, then move to the next line.  Set subtotal[0]=0.  Take the first number and put it into var[0], and the second number, and put it into var[1].  Move to the next line, and put the first number into var[2] and the second number into var[3].  Calculate the distance using var[0], [1], [2], [3].  Add the distance to subtotal[0].  Set var[0]=var[2] and var[1]=var[3].  Move to the next line and put the first number into var[2] and the second number into var[3].  Unless the next line starts with a >, then add subtotal[0] to total...

There's so many different places where a different looping process starts, or overlaps with another one.  I'm beginning to wonder if this is beyond me.
Well, you did already describe one way that would (probably) work...

This is what I'd do (in C, I don't know perl or python):

I'd have a 'point' structure, with 'x' and 'y' components. A 'distance' function that returns the distance between 2 given points. A 'previous_point' point, which will store NULL when we're at the beginning of a segment. A 'current_point' point. Then,,

I'd have a 'string' variable - a 7-char array (7 should be big enough..), which would store the current "fragment" of the input file we're parsing, retrieved with the fscanf function. Then you can compare string[0] to '>' so you can set 'previous_point' to NULL, else you can use the atoi function on 'string' and store the result in 'this_point.x' and then do something like 'fscanf (input_file, "%d", &this_point.y)' and then this_point is populated. And then check if previous_point is NULL, if it is then do nothing, otherwise do a 'total += difference (previous_point, this_point)' job. Then 'previous_point = current_point' And... If all of is happening in a while  (string[0] != EOF) type-loop (string being defined outside the loop (I completely messed that up)) it should work. Hopefully.

Alot of the specifics there I'm unsure of though, it's only after I'm in the write-compile-fix mood I can be sure of anything, with C.

If that's completely wrong, if it just doesn't work, it's the kinda thinking I'd start off with, anyhow.

(what a mess of a post, but I hope you got the idea..)
« Last Edit: 29 July 2006, 01:15 by piratePenguin »
"What you share with the world is what it keeps of you."
 - Noah And The Whale: Give a little love



a poem by my computer, Macintosh Vigilante
Macintosh amends a damned around the requested typewriter. Macintosh urges a scarce design. Macintosh postulates an autobiography. Macintosh tolls the solo variant. Why does a winter audience delay macintosh? The maker tosses macintosh. Beneath female suffers a double scum. How will a rat cube the heavier cricket? Macintosh calls a method. Can macintosh nest opposite the headache? Macintosh ties the wrong fairy. When can macintosh stem the land gang? Female aborts underneath macintosh. Inside macintosh waffles female. Next to macintosh worries a well.

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: how long is it?
« Reply #5 on: 3 August 2006, 21:41 »
I started reading up on fscanf, thinking about maybe using your ideas.  But then I strolled over to Void Main's site and asked him what he thought real quick.  He and another guy whipped up 4 versions of the program in as many hours, all in Perl.

So I guess this issue is settled.  Perl wins.

pofnlice

  • Member
  • **
  • Posts: 999
  • Kudos: 650
Re: how long is it?
« Reply #6 on: 4 August 2006, 08:59 »
sell out
Quote from: "Orethrius"
After all, running Windows without a decent anti-virus is like walking through a Red Light District after eating five metric tonnes of Viagra.

Dark_Me

  • Member
  • **
  • Posts: 302
  • Kudos: 314
Re: how long is it?
« Reply #7 on: 4 August 2006, 15:07 »
WTF? I didn't mean to post in this thread...
Capitalism kicks ass.
-Skyman
If your a selfish, self-centred prick, who is willing to leave half the world in poverty, then yes.
-Kintaro

piratePenguin

  • VIP
  • Member
  • ***
  • Posts: 3,027
  • Kudos: 775
    • http://piratepenguin.is-a-geek.com/~declan/
Re: how long is it?
« Reply #8 on: 4 August 2006, 15:51 »
[offtopic]you can delete your posts ;)[/offtopic]
"What you share with the world is what it keeps of you."
 - Noah And The Whale: Give a little love



a poem by my computer, Macintosh Vigilante
Macintosh amends a damned around the requested typewriter. Macintosh urges a scarce design. Macintosh postulates an autobiography. Macintosh tolls the solo variant. Why does a winter audience delay macintosh? The maker tosses macintosh. Beneath female suffers a double scum. How will a rat cube the heavier cricket? Macintosh calls a method. Can macintosh nest opposite the headache? Macintosh ties the wrong fairy. When can macintosh stem the land gang? Female aborts underneath macintosh. Inside macintosh waffles female. Next to macintosh worries a well.