Alpha DevCon 2018
Results 1 to 14 of 14

Thread: Programming Puzzle 8 - Sort of a Sort

  1. #1
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,299

    Default Programming Puzzle 8 - Sort of a Sort

    Date Created: 10-Jun-2011 10:59:01 AM
    Last Updated: 10-Jun-2011 10:59:01 AM
    Created By : Tom
    Updated By : Tom
    Beginning Xbasic Programming

    Puzzle 8 - Sort of a Sort

    You solved Puzzle_6, and then Puzzle 7, but your sales manager now tells you
    that the list of product sales shouldn't have been sorted by the product_id.
    She needs the list of sales by product sorted (descending) by the sales amount.
    She wants the product that generated the most dollars first, and the product
    that generated the fewest dollars last. She doesn't think this will
    be very difficult for you because you've already computed the total dollars
    for each product. Can you have the script done by lunch time?

    Hah! Just like a manager, right? [ Doggone managers. What do they know, anyway? ]

    Your job now is to write an xbasic script that will list each product
    and for each product show the total dollars generated by that item
    across all the invoices. But display the list in descending order based
    on total dollars generated by each item.

    Instructions:

    a) you may not use any of the built in summary functions, methods,
    or operations in Alpha Five. We're doing this one by hand.

    b) you may open the table only once

    c) your output should be directed to the trace window

  2. #2
    Alpha Software Employee Lenny Forziati's Avatar
    Real Name
    Lenny Forziati
    Join Date
    Nov 2001
    Location
    Alpha Software
    Posts
    4,640

    Default Re: Programming Puzzle 8 - Sort of a Sort

    Quote Originally Posted by Tom Cone Jr View Post
    Can you have the script done by lunch time?

    Hah! Just like a manager, right? [ Doggone managers. What do they know, anyway? ]
    Tom, a good manager would know you are an Xbasic wiz and wait patiently in your office while you whip this up in 30 seconds since it's such a simple change. Why wait until lunch time?

    Lenny Forziati
    Vice President, Internet Products and Technical Services
    Alpha Software Corporation

  3. #3
    "Certified" Alphaholic
    Real Name
    Andrew Schone
    Join Date
    Dec 2005
    Location
    Kansas
    Posts
    1,044

    Default Re: Programming Puzzle 8 - Sort of a Sort

    I like this manager, gonna keep me in business for a long time.
    Attached Files Attached Files

  4. #4
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,299

    Default Re: Programming Puzzle 8 - Sort of a Sort

    Andy, that's sweet.

    Because you're using a property array to hold the computed sums, displaying the list in sorted order becomes very easy.

    In my nested loop approach, on the contrary, I have more work to do. Will post results later on.

    -- tom

    ps. Your contribution to this exercise will be more valuable if you include comments explaining
    what your script is doing.
    Last edited by Tom Cone Jr; 06-10-2011 at 12:38 PM.

  5. #5
    "Certified" Alphaholic Stan Mathews's Avatar
    Real Name
    Stan Mathews
    Join Date
    Apr 2000
    Location
    Bowling Green, KY
    Posts
    25,011

    Default Re: Programming Puzzle 8 - Sort of a Sort

    Don't laugh, Al.

    Puzzle 8 kludge.txt

  6. #6
    Moderator
    Real Name
    Alan Buchholz
    Join Date
    Oct 2000
    Location
    Delavan, Wisconsin
    Posts
    9,481

    Default Re: Programming Puzzle 8 - Sort of a Sort

    Quote Originally Posted by Stan Mathews View Post
    Don't laugh, Al.
    Who?? me??

    <Where's my Alfred E Neuman icon???>
    Al Buchholz
    Bookwood Systems, LTD
    Weekly QReportBuilder Webinars Thursday 1 pm CST

    Occam's Razor - KISS
    Normalize till it hurts - De-normalize till it works.
    Advice offered and questions asked in the spirit of learning how to fish is better than someone giving you a fish.
    When we triage a problem it is much easier to read sample systems than to read a mind.

  7. #7
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,299

    Default Re: Programming Puzzle 8 - Sort of a Sort

    Here's my solution.

    -- tom

  8. #8
    Member
    Real Name
    Jeb Richardson
    Join Date
    Aug 2011
    Location
    Bowling Green, KY
    Posts
    26

    Default Re: Programming Puzzle 8 - Sort of a Sort

    Here's my attempt for puzzle 8.

    Puz8.txt

  9. #9
    "Certified" Alphaholic Mike Wilson's Avatar
    Real Name
    mike wilson
    Join Date
    Apr 2005
    Location
    Michigan
    Posts
    4,127

    Default Re: Programming Puzzle 8 - Sort of a Sort

    Jeb,
    Are you and Stan Mathews the same guy?
    Mike W
    __________________________
    "I rebel in at least small things to express to the world that I have not completely surrendered"

  10. #10
    Member
    Real Name
    Jeb Richardson
    Join Date
    Aug 2011
    Location
    Bowling Green, KY
    Posts
    26

    Default Re: Programming Puzzle 8 - Sort of a Sort

    No, I just started working under Stan. He's teaching me all about Alpha and how we (Houchens) use it. So in a nut shell I'm learning what he does and how he does it!

    -Jeb

  11. #11
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,299

    Default Re: Programming Puzzle 8 - Sort of a Sort

    Jeb,

    Thanks for taking a crack at it.

    The upper bound of your loop is a table record number. This number is found by fetching the last record after the table opens. However, after finding that record number you re-order the table basedon the product_id. Be advised that this could change which record is "Last". A better approach would be to order the records first, then fetch the "last" in that sort sequence. i.e. the last "product_id", not the last record number. Instead of using a For ... Next loop based on incrementing physical record numbers, I believe it's better to use a while ... end while loop based on product_id's.

    Does this make sense?

    -- tom

  12. #12
    Member
    Real Name
    Jeb Richardson
    Join Date
    Aug 2011
    Location
    Bowling Green, KY
    Posts
    26

    Default Re: Programming Puzzle 8 - Sort of a Sort

    Yes. I only tried using the for...next to switch things up. But now i see your point. However, i was using my "last" variable just to get a count of records in that table. That way i could use the "last" variable in my loop. I used item.recno() and it returned 129 i believe. And i used this to allow my loop to run from 1-129("last"). Thanks, Tom!

    -Jeb
    Last edited by jeb richardson; 08-10-2011 at 09:42 AM.

  13. #13
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,299

    Default Re: Programming Puzzle 8 - Sort of a Sort

    Right.

    But when you change the sort sequence you do not change the physical record numbers. The new "last" record may have a different "record number". The "last" record by sort sequence is not the same as the "last" physical record in the table. And, they will often ( if not usually ) have different "record numbers". The <tbl>.recno() method is looking at physical record numbers, not logical record positions in the current sort sequence.

  14. #14
    "Certified" Alphaholic
    Real Name
    Andrew Schone
    Join Date
    Dec 2005
    Location
    Kansas
    Posts
    1,044

    Default Re: Programming Puzzle 8 - Sort of a Sort

    I avoid using record numbers in code like the plague. You can put the table in record number order, fetch the last record and return record number 200. However the table only has 2 records in it. This can happen when records are deleted but the table is not packed. When the table is packed the records are renumbered. So in this example once you pack the table the last record will report a record number of 2.

    Retrieving the record count is the appropriate method to get the correct total of un-deleted records in a table.

Similar Threads

  1. Programming Puzzle 2 - Another Simple Loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 16
    Last Post: 12-20-2014, 10:38 AM
  2. Programming Puzzle 1 - Simple Loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 32
    Last Post: 12-26-2011, 01:38 PM
  3. Programming Puzzle 3 - Fractions, anyone?
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 7
    Last Post: 11-08-2011, 05:40 AM
  4. Programming Puzzle 7 - Yet another nested loop!
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 4
    Last Post: 08-08-2011, 05:41 PM
  5. Sort by Logical or Numeric field works, but sort by one then the other causes error
    By al mecklenburg in forum Application Server Version 8
    Replies: 4
    Last Post: 01-19-2008, 12:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •