Alpha DevCon 2018
Results 1 to 7 of 7

Thread: Programming Puzzle 6 - Another nested loop

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

    Default Programming Puzzle 6 - Another nested loop

    Beginning Xbasic Programming

    Puzzle 6 - Another Nested loop

    Your sales manager gives you the list of sold items found in the
    Invoice_items table. (see attachment).

    She has noticed that several products appear on multiple invoices.

    She wants to quantify this, and has asked you to write an xbasic script
    that will list each product and the number of separate invoices
    upon which each appears.

    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

    d) your list should be in ascending order based on the Product_id field

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

    Default Re: Programming Puzzle 6 - Another nested loop

    Not many answers yet. I'll attach mine as a text file so no one will see it unless they so desire.

    Fixed the test of having duplicate listing of the same item on one invoice.
    Attached Files Attached Files
    Last edited by Stan Mathews; 06-09-2011 at 04:46 PM. Reason: Fixed script.

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

    Default Re: Programming Puzzle 6 - Another nested loop

    Stan, my results agree with yours. I see now that it would be possible for the same item to be on an invoice more than once. My solution (to be revealed later) assumes that that does not happen, so for me, the count of invoice_item records for a given product is the same as the count of invoices that contained that product.

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

    Default Re: Programming Puzzle 6 - Another nested loop

    My eyes are getting old but I would swear that I see P012 on invoice 000021 twice. Once qty 3 and once qty 7.
    Likewise P023 on invoice 000026, qtys 7 and 5.
    Last edited by Stan Mathews; 06-09-2011 at 05:26 PM.

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

    Default Re: Programming Puzzle 6 - Another nested loop

    Key Functions
    Word_Exists()
    <array>.Find()
    Arrays - Automatically Inserting Elements into an Array
    <array>.Dump_Properties()


    Code:
    'Date Created: 09-Jun-2011 11:29:00 AM
    'Last Updated: 09-Jun-2011 04:16:52 PM
    'Created By  : andy
    'Updated By  : andy
    OPTION STRICT
    OPTION ENCRYPTED_TOKENS
    ON ERROR GOTO ERR_HANDLER
    
    DIM vInvoice_ItemsP as P
    
    DIM vOrderC as C
    DIM vFilterC as C
    DIM vFormatC as C
    
    DIM vIndxN as N
    
    DIM arrInvoice[0] as P
    DIM arrInvoice[0].ProductC as C
    DIM arrInvoice[0].InvoiceListC as C
    DIM arrInvoice[0].InvoiceCountN as N
    
    vInvoice_ItemsP = table.open("invoice_items",FILE_RW_SHARED)
    	vOrderC = "Product_id"
    	vInvoice_ItemsP.order(vOrderC)
    	vInvoice_ItemsP.fetch_first()
    	WHILE .not. vInvoice_ItemsP.fetch_eof()
    		vIndxN = arrInvoice.find(vInvoice_ItemsP.Product_Id, "ProductC")
    		IF vIndxN > 0 THEN
    			'Product found
    			IF word_exists(arrInvoice[vIndxN].InvoiceListC, vInvoice_ItemsP.invoice_nu,";") THEN
    				'do nothing
    			ELSE
    				arrInvoice[vIndxN].InvoiceListC = arrInvoice[vIndxN].InvoiceListC + ";" + vInvoice_ItemsP.invoice_nu
    				arrInvoice[vIndxN].InvoiceCountN = arrInvoice[vIndxN].InvoiceCountN + 1
    			END IF
    		ELSE	
    			'Product not Found
    			arrInvoice[].ProductC = vInvoice_ItemsP.product_id
    			arrInvoice[..].InvoiceListC = vInvoice_ItemsP.invoice_nu
    			arrInvoice[..].InvoiceCountN = 1
    		END IF
    		vInvoice_ItemsP.fetch_next()
    	END WHILE
    vInvoice_ItemsP.close()
    
    vFormatC = "ProductC|InvoiceCountN|InvoiceListC"
    trace.WriteLn(arrInvoice.dump_properties(vFormatC))
    
    
    
    
    
    END
    '-----------------------------------
    'Error handler
    '-----------------------------------
    ERR_HANDLER:
    	Dim err as N= error_code_get()
    	Dim msg as C= "Error # "+ err + crlf() + error_text_get(err)
    	msg = msg + crlf() + "Script: " + error_script_get()
    	msg = msg + crlf() + "Line #: " + error_line_number_get()
    
    	trace.writeln(msg)
    
    	ui_msg_box("Error", msg, ui_attention_symbol)
    	msg = ""
    	
    	END
    Last edited by aschone; 06-09-2011 at 05:36 PM.

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

    Default Re: Programming Puzzle 6 - Another nested loop

    Here's my solution.

    The attachment is the "exported" script. In this format the script can be easily "imported" into your copy of the puzzle database.

    -- tom

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

    Default Re: Programming Puzzle 6 - Another nested loop

    Tom, the first one I created puz6 was just like your script. However, I didn't want to settle for that. So I looked into a different approach to get the answer and with some help from Stan, the newpuz6 is a script that isn't like yours, but still delievers the same answer.

    puz6 - 08-08-2011 -1.txt

    NEWPUZ6 - 08-08-2011 -1.txt

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 5 - Nested loops
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 10
    Last Post: 08-08-2011, 02:06 PM
  5. Programming Puzzle 4 - A bug riddled riddle
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 18
    Last Post: 08-05-2011, 06:15 AM

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
  •