|
Post by tsh73 on Oct 21, 2022 16:55:39 GMT -5
This is the school textbook problem, 6th form It is supposed to be solved by pen and paper (done that)
But How to make computer solve it? What data structures to use to represent problem? Looks rather challenging to me...
'There was a box of apples and pears 'Some are big, some are small; 'some are yellow, rest are green 'There are no small pears 'and no small green apples 'Some numbers are given: '25 Apples, 17 pears '32 big fruits '28 yellow ones. 'There are two more green apples then green pears. ' 'Find the number of big yellow apples
So. There is (25 Apples, 17 pears) = 42 fruits total 7 given restrictions
How can one solve it?
|
|
|
Post by pierre on Oct 23, 2022 8:14:36 GMT -5
Here is my first attempt. Answer is 7.
Structures used : FRUITS, SIZE, COLOR & COMBO.
'apples & pears '-------------------------------------------------------------------------------------------
struct fruits,_ apples as short,_ pears as short
struct size,_ bigfruits as short,_ bigapples as short,_ bigpears as short,_ smallfruits as short,_ smallapples as short,_ smallpears as short
struct color,_ greenfruits as short,_ yellowfruits as short struct combo,_ biggreenfruits as short,_ biggreenapples as short,_ biggreenpears as short,_ bigyellowapples as short,_ bigyellowpears as short,_ smallgreenapples as short,_ smallgreenpears as short,_ smallyellowapples as short,_ smallyellowpears as short
'-------------------------------------------------------------------------------------------
print"FRUITS :" fruits.pears.struct = 17 print "pears = ";fruits.pears.struct fruits.apples.struct = 25 print "apples = ";fruits.apples.struct print '-------------------------------------------------------------------------------------------- print "SIZE :" size.bigfruits.struct = 32 print "big fruits = ";size.bigfruits.struct size.smallfruits.struct = fruits.apples.struct + fruits.pears.struct - size.bigfruits.struct print "---> small fruits = ";size.smallfruits.struct size.smallpears.struct = 0 print "small pears = ";size.smallpears.struct size.smallapples.struct = size.smallfruits.struct - size.smallpears.struct print "---> small apples = ";size.smallapples.struct size.bigpears.struct=fruits.pears.struct-size.smallpears.struct print "---> big pears = ";size.bigpears.struct size.bigapples.struct=size.bigfruits.struct-size.bigpears.struct print "---> big apples = ";size.bigapples.struct print '---------------------------------------------------------------------------------------------- print "COLOR :" color.yellowfruits.struct = 28 print "yellow fruits = ";color.yellowfruits.struct color.greenfruits.struct = fruits.apples.struct + fruits.pears.struct - color.yellowfruits.struct print "---> green fruits = ";color.greenfruits.struct print '------------------------------------------------------------------------------------------------ print "COMBO :" print "small pears = ";size.smallpears.struct combo.smallgreenpears.struct=0 print "---> small green pears = ";combo.smallgreenpears.struct combo.smallyellowpears.struct=0 print "---> small yellow pears = ";combo.smallyellowpears.struct combo.smallgreenapples.sruct=0 print "small green apples = ";combo.smallgreenapples.sruct '------------------ corrected typo ------------------- combo.smallyellowapples.struct=size.smallapples.struct-combo.smallgreenapples.sruct print "---> small yellow apples = "; combo.smallyellowapples.struct '------------------------------------------------------ print "Two more green apples then green pears :" combo.biggreenfruits.struct= color.greenfruits.struct - combo.smallgreenpears.struct - combo.smallgreenapples.struct print "---> big green fruits : ";combo.biggreenfruits.struct combo.biggreenpears.struct=(combo.biggreenfruits.struct - 2)/2 print "---> big green pears = ";combo.biggreenpears.struct combo.biggreenapples.struct=combo.biggreenfruits.struct - combo.biggreenpears.struct print "---> big green apples = ";combo.biggreenapples.struct combo.bigyellowpears.struct=size.bigpears.struct-combo.biggreenpears.struct print "---> big yellow pears = ";combo.bigyellowpears.struct combo.bigyellowapples.struct=size.bigapples.struct-combo.biggreenapples.struct print "---> big yellow apples = ";combo.bigyellowapples.struct;" <----QED" '-------------------------------------------------------------------------------------------------
print print "Controls :" print "FRUITS : ";fruits.apples.struct + fruits.pears.struct print "SIZE : ";size.bigapples.struct + size.bigpears.struct + size.smallapples.struct + size.smallpears.struct print "COLOR : ";color.greenfruits.struct + color.yellowfruits.struct print "COMBO : ";combo.biggreenapples.struct + combo.bigyellowapples.struct + combo.smallgreenapples.struct +_ combo.smallyellowapples.struct + combo.biggreenpears.struct + combo.bigyellowpears.struct + combo.smallgreenpears.struct +_ combo.smallyellowpears.sruct
|
|
|
Post by tsh73 on Oct 23, 2022 14:23:13 GMT -5
Thanks for posting. Yes 7 is the right answer.
I think pen-and-paper solution goes along the same path and it seems to me that using LB structs does not differs much from just naming variables "smallpears", "bigyellowapples" etc?
I were thinking about something allowing to count things like "smallfruits" based on "combo" values.
|
|
|
Post by tsh73 on Oct 23, 2022 16:20:45 GMT -5
Just for a record, I've got brute force solution (nested loops, that kind of stuff) It work surprisingly fast
|
|
|
Post by pierre on Oct 23, 2022 16:36:27 GMT -5
Thanks for posting. Yes 7 is the right answer. I think pen-and-paper solution goes along the same path and it seems to me that using LB structs does not differs much from just naming variables "smallpears", "bigyellowapples" etc? I were thinking about something allowing to count things like "smallfruits" based on "combo" values. Yes, definitely, the way of thinking derives directly from the "pen-and-paper" solution. I agree that using LB structs means much more writing and just naming variables would have been simpler. I often use LB structs as if it were kind of "User Defined Types". It helps me to see the structures clearly. For me, this kind problem required a top-down approach. For example "smallfruits" are not based on "combo" values which are not yet known at that time, but on "total Fruits" less "big Fruits" (42-32 = 10). Then, that value helps us to determine that the small fruits are actually all small yellow apples and so on and so forth. You can see all the "combo" values summed up in the final controls PS I have corrected a little typo in the formula for the small yellow apples. This does not change the final result.
|
|
|
Post by pierre on Oct 23, 2022 16:39:54 GMT -5
Just for a record, I've got brute force solution (nested loops, that kind of stuff) It work surprisingly fast it would be interesting to see how you did that .....
|
|
|
Post by tsh73 on Oct 23, 2022 17:04:02 GMT -5
Sure I'll post it. Let problem brew for a day or two - I wonder if someone else will give it a try?
|
|
|
Post by metro on Oct 23, 2022 22:03:52 GMT -5
A wise (young) man on the old forum stated "there's more than one way to skin a cat" Multi-dimensioned array, in memory SQLite, Structs or basicprogramming.blogspot.com/2016/07/a-dictionary-lookup-mini-application.htmlI just went for loading up a simple array. ' My Paper version 'WHAT WE KNOW Apples = 25 Pears =17 TotalBigFruit =32 BigPears =17 TotalYellowFruit=28 TotalFruit =Apples+Pears
'ASSUMPTIONS BigApples = TotalBigFruit- BigPears SmallApples = Apples-BigApples GreenFruit =TotalFruit- TotalYellowFruit BigYellowFruit = TotalYellowFruit-SmallApples 'all small apples are yellow GreenApples =((GreenFruit-2)/2)+2 BigYellowApples =BigApples-GreenApples
'Print "Green Apples = ";GreenApples 'print "total fruit = ";TotalFruit 'Print "BigApples = "; BigApples 'print "small Apples ="; SmallApples 'Print "num of Green fruit =";GreenFruit 'print "Big Yellow fruit = "; BigYellowFruit print print "Big Yellow Apples = ";BigYellowApples
or loopy loop TF=42: BYA=0 GLOBAL TF,BYA 'TOTAL FRUIT & BIG YELLOW APPLE DIM Allfruits$(TF)
'************************************************* ' MAINLINE '************************************************* CALL NAME.FRUIT CALL SIZE.FRUIT CALL SET.COLOUR CALL PRINT.EM WAIT
'************************************************* SUB NAME.FRUIT 'NAME EACH PIECE '************************************************* FOR c = 1 to TF SELECT CASE CASE (c<18) Allfruits$(c)= "PEAR" CASE (c > 17) Allfruits$(c) = "APPLE" END SELECT NEXT END SUB
'************************************************* SUB SIZE.FRUIT 'BIG FRUIT ALL PEARS ARE BIG SO 'THEY WERE NAMED FIRST ABOVE '************************************************* BF=32 'TOTAL BIG FRUIT
FOR c = 1 to TF SELECT CASE CASE (c<33) Allfruits$(c)= Allfruits$(c)+"-"+"BIG"+"-" CASE (c > 32) Allfruits$(c)= Allfruits$(c)+"-"+"SMALL"+"-" END SELECT NEXT END SUB
'************************************************* SUB SET.COLOUR 'CALC BIG GREEN APPLES & GREEN PEARS '************************************************* TotYellow =28 GreenFruit =TF-TotYellow GreenApples =((GreenFruit-2)/2)+2 'THEREFORE GreenPears =GreenFruit-GreenApples
'************************************************* 'GREEN PEARS UPDATE ROW '************************************************* FOR c = 1 to GreenPears Allfruits$(c) = Allfruits$(c)+ "GREEN" NEXT
'************************************************* 'YELLOW PEARS UPDATE ROW '************************************************* FOR c = 1 to TF IF INSTR(Allfruits$(c),"PEAR") AND WORD$(Allfruits$(c),3,"-")="" THEN Allfruits$(c) = Allfruits$(c)+ "YELLOW" END IF NEXT
'************************************************* 'SET BIG GREEN APPLES '************************************************* FOR c = 1 to TF IF INSTR(Allfruits$(c),"APPLE") AND INSTR(Allfruits$(c),"BIG") THEN Allfruits$(c) = Allfruits$(c)+ "GREEN" GreenApples=GreenApples-1 IF GreenApples=0 then exit for END IF NEXT
'************************************************* 'NO SMALL GREEN APPLES SO THEY MUST BE YELLOW '************************************************* FOR c = 1 to TF IF INSTR(Allfruits$(c),"APPLE") AND INSTR(Allfruits$(c),"SMALL")THEN Ycount=Ycount+1 Allfruits$(c) = Allfruits$(c)+ "YELLOW" END IF NEXT
'************************************************* 'COUNT & COLOUR BIG YELLOW APPLES '************************************************* FOR c = 1 to TF IF INSTR(Allfruits$(c),"APPLE") AND INSTR(Allfruits$(c),"BIG") AND WORD$(Allfruits$(c),3,"-")="" THEN BYA=BYA+1 Allfruits$(c) = Allfruits$(c)+ "YELLOW" END IF NEXT
END SUB
'************************************************* SUB PRINT.EM 'PRINT RESULTS '************************************************* FOR c = 1 to TF PRINT Allfruits$(c) NEXT PRINT: PRINT "THERE ARE "; BYA;" BIG YELLOW APPLES" END SUB
|
|
|
Post by tsh73 on Oct 24, 2022 1:01:05 GMT -5
Wow. Interesting way
|
|
|
Post by tsh73 on Oct 24, 2022 16:18:55 GMT -5
Brute force approach. Since we have total 42 fruits and 2^3=8 "fully qualified" parts (big green apples etc), that gives us 8 loops by 42. 42^8, looks pretty long, eh? But in the conditions 3 parts of 8 stated to be 0 (small green apples etc) Also we do not need loop by last one 'cause sum should be 42 That leaves just 42^4 And we could rearrange loops to cut variants that would not fit early... May be not so dumb brute force after all. But you see? To get amount of apples I just use aby+asy+abg+asg - anything with "a" and every other thing does that. Feels really cludgy to me (albeit fast) I really want something along sum (fruits.num) where fruits.kind like %apples% Probably should try to rewrite on something SQL... Or make some UDFs to mimic just that 'math puzzle 'brute force approach by tsh73, Oct 2022 'interesting but removing 0-variables from conditions does not speed thing up 'Probably it just too fast as it is
'There was a box of apples and pears 'Some are big, some are small; 'some are yellow, rest are green 'There are no small pears 'and no small green apples 'Some numbers are given: '25 Apples, 17 pears '32 big fruits '28 yellow ones. 'There are two more green apples then green pears. ' 'Find the number of big yellow apples ' a/p b/s y/g 'all possible variants 'aby 'by coincendence, required one 'abg 'asy 'asg=0 'pby 'pbg 'psy=0 'psg=0 'gives us 5 variables. last one could be deduced by subtracting sum from 42 '(as apples + pears = 25+17=42) 'surprisingly 4 nested loops ends in reasonable time
'move loop by pears first so we could check for "17 pears" 'now move loop by yellows so we can check for "28 yellow"
t0=time$("ms") asg=0 psy=0 psg=0
for pby = 0 to 42 for pbg = 0 to 42 if pbg+pby+psy+psg<>17 then [contPbg] for aby = 0 to 42 for asy = 0 to 42 SCAN 'print pby, pbg, aby, asy if pby+aby+asy+psy<>28 then [contAsy] abg=42-aby-asy-asg-pby-pbg-psy-psg 'print abg if abg<0 then [contAsy] 'negative apples does not make sense! if aby+asy+abg+asg<>25 then [contAsy] if pby+pbg+abg+aby<>32 then [contAsy] '"two more" condition if abg+asg<>pbg+psg+2 then [contAsy] 'else we got it print "aby abg asy asg pby pbg psy psg" print aby;" ";abg;" ";asy;" ";asg;" ";pby;" ";pbg;" ";psy;" ";psg [contAsy] next next [contPbg] next next t1=time$("ms")
print "Found in ";t1-t0; " ms"
|
|
|
Post by pierre on Oct 25, 2022 11:05:29 GMT -5
Wow, that is a whole different approach ! But not very fast..
Obviously metro is the winner, both in simplicity and in speed:
- metro (pen & paper) 20 ms - pierre (pen & paper) 166 ms - metro (loopy loop ) 247 ms - tsh73 (brute force) 710 ms
|
|
bplus
Full Member
Posts: 127
|
Post by bplus on Oct 25, 2022 14:43:04 GMT -5
I worked it on paper first and then tried to translate how I did it with code, I came up with this:
' Apples and Pears 2 ' Problem from tsh73 JB Forum https://justbasiccom.proboards.com/thread/905/math-puzzle-challenge 'There was a box of apples and pears 'Some are big, some are small; 'some are yellow, rest are green 'There are no small pears 'and no small green apples 'Some numbers are given: '25 Apples, 17 pears '32 big fruits '28 yellow ones. 'There are two more green apples then green pears. ' 'Find the number of big yellow apples '====================================================================================================
Dim names$(16), values(16) 'the 8 combos of Fruit(2) * Size(2) * Color(2) ' Use -1 to tell code that that value has not been determined yet names$(1) = "ASY" : values(1) = -1 ' for now names$(2) = "ASG" : values(2) = 0 ' stays 0 names$(3) = "ALY" : values(3) = -1 ' > we want answer to this names$(4) = "ALG" : values(4) = -1 ' for now names$(5) = "PSY" : values(5) = 0 ' stays = 0 no SP's names$(6) = "PSG" : values(6) = 0 ' stays = 0 no SP's names$(7) = "PLY" : values(7) = -1 ' for now names$(8) = "PLG" : values(8) = -1 ' for now
names$(9) = "A" : values(9) = 25 names$(10) = "P" : values(10) = 17 names$(11) = "L" : values(11) = 32 names$(12) = "Y" : values(12) = 28 names$(13) = "F" : values(13) = values(9) + values(10) names$(14) = "S" : values(14) = values(13) - values(11) names$(15) = "G" : values(15) = values(13) - values(12)
'There are two more green apples then green pears ' values(15) is total of green fruit no small fruit is green so total green fruit is total of L ' value(15) = alg + plg ' values(15) = (plg + 2) + plg 'There are two more green apples then green pears ' values(15) = 2 * plg + 2 ' plg = (values(15) - 2)/2 ' plg = values(8) values(8) = (values(15) - 2) / 2 ' this is num
' early checking of code, turns out I never needed sum(Letter$) 'Print sum("A"), sum("P") 'print sum("S"), sum("L") 'print sum("Y"), sum("G") 'print values(14), values(15)
Q$ = "APSLYG" ' qualities ' notice there are 3 or 4 values not = -1 for S so the 4th one = the sumS - the total values of others, simple algebra
[SolveAgain] sf = 0 for L = 1 to 6 test$ = Solve$(mid$(Q$, L, 1)) If test$ <> "" then print "Solved! " + test$ : sf = 1 next if sf = 1 then goto [SolveAgain] print "Finished Solving" print print " OK this is breakdown of all fruit:" print " Sum of Apples is ";sum("A") print " Sum of Pears is ";sum("P") print " Sum of Large is ";sum("L") print " Sum of Small is ";sum("S") print " Sum of Green is ";sum("G") print " Sum of Yellow is ";sum("Y") print print " Each type:" for i = 1 to 8 print " ";names$(i);" = ";values(i) next
function Solve$(letter$) for N = 1 to 8 if instr(names$(N), letter$)> 0 then if values(N) = -1 then count = count + 1 : saveN = N else total = total + values(N) end if end if next if count = 1 then ' only one value not established yet we can figure it's value for j = 9 to 15 if names$(j) = letter$ then ' find the letter and it's value, subtract total of others values(saveN) = values(j) - total ' this fills in a missing value!!! Solve$ = names$(saveN) + " = " + str$(values(saveN)) exit function end if next end if end function
' never used for solving but helped me build Solve from it! did use for reporting function sum(letter$) for N = 1 to 8 if instr(names$(N), letter$) then sum = sum + values(N) next end function
' from earlier post
'3 pairs of mutually exclusive qualities 'A for Apple P for Pear, S for small L for Large, Y for Yellow G for green 'A + P = 42 total fruit '42 - 32L = 10 S '42 - 28Y = 14 G
'Summary ' A/P = 25/17 : S/L = 10/32 : Y/G = 28/14
' There are 8 combos of quality Fruit(2) * Size(2) * Color(2) = 8 'ASY 'ASG = 0 'no 'ALY > we want answer to this 'ALG 'PSY = 0 no SP's 'PSG = 0 no SP's 'PLY 'PLG
' ALG = PLG + 2 There are two more green apples then green pears. ' 14 G so PLG + PLG + 2 = 14 ' 2 * PLG = 12 ' PLG = 6 ' then ALG = 8
' Total Pears = 17 = PLY + PLG ' 17 = PLY + 6 ' 11 = PLY '
' Now we know this table of Apples and Pears:
' ASY = 10 all small fruit are apples ' ASG = 0 'no small green apples ' ALY > we want answer to this ' ALG = 8
' PSY = 0 no small pears ' PSG = 0 no small pears ' PLY = 11 because total Pears = 17 - PLG = PLY ' PLG = 6
' All Apples is 25 = ASY + ASG + ALY + ALG ' 25 = 10 + 0 + ALY + 8 ' 25 - 18 = ALY ' 7 = ALY
' check 'ASY = 10 all small fruit are apples 'ASG = 0 no small green apples 'ALY = 7 > we want answer to this 'ALG = 8 ' = 25 apples
'PSY = 0 no small pears 'PSG = 0 no small pears 'PLY = 11 'PLG = 6 ' = 17 Pears
|
|
|
Post by tsh73 on Oct 26, 2022 5:27:49 GMT -5
Trying to grokk bplus' code. Not here yet - thinking is hard after all.
|
|
|
Post by metro on Oct 26, 2022 7:43:37 GMT -5
Trying to grokk bplus' code. Not here yet - thinking is hard after all. Clearly, I do not understand the expected outcome. however I failed year 12 and have had no work history in programming (just read a book) so I'm not too perturbed. Great fun had still being had.
Had a quick re-thing and concluded anything pear related can be ignored, so cut down version....
'There was a box of apples and pears 'Some are big, some are small; 'some are yellow, rest are green 'There are no small pears 'and no small green apples 'Some numbers are given: '25 Apples, 17 pears '32 big fruits '28 yellow ones. 'There are two more green apples THEN green pears. ' 'Find the number of big yellow apples
t0=time$("ms") Apples = 25 Pears = 17 TBF = 32 'totalbigfruit BigPears = 17 TYF = 28 'TotalYellow fruit TF =Apples+Pears BA=TBF-BigPears
'ASSUMPTIONS TGF= TF-TYF 'TOTALGreenFruit , TotalFruit,TotalYellowfruit BigApples = TBF- BigPears SmallApples = Apples-BigApples BigYellowFruit = TYF-SmallApples 'all small apples are yellow GreenFruit = TF- TYF GreenApples = ((GreenFruit-2)/2)+2 GreenPears = (GreenFruit-2)/2 FirstBYA = Pears+GreenApples BigYellowApples = BigApples-GreenApples PearBigGreen = TotalBigFruit-GreenPears 'print GreenPears 'Print "Green Apples = ";GreenApples 'print "total fruit = ";TotalFruit 'Print "BigApples = "; BigApples 'print "small Apples ="; SmallApples 'Print "num of Green fruit =";GreenFruit 'print "Big Yellow fruit = "; BigYellowFruit 'print "Big Yellow Apples = ";BigYellowApples '============================================================= 'WE have no interest in Pears or green apples or small apples PRINT"Big Yellow Apples = ";Apples-SmallApples-GreenApples '=============================================================
Dim Allfruits$(TF)
FOR c = 1 to TF ' IF c <= GreenPears THEN Allfruits$(c)= "PEAR"+"-"+"BIG"+"-"+ "GREEN" ' IF c > GreenPears and c <= Pears THEN Allfruits$(c)= "PEAR"+"-"+"BIG"+"-"+ "YELLOW" ' IF c > Pears and c <= TBF THEN Allfruits$(c)= "APPLE" +"-"+"BIG"+"-" ' IF c > TBF THEN Allfruits$(c)= "APPLE" +"-"+"SMALL" +"-" '+ "YELLOW" ' IF c > Pears and c <= FirstBYA THEN Allfruits$(c)= Allfruits$(c)+ "GREEN" IF c > FirstBYA THEN ABY= ABY+1 ' print Allfruits$(c) NEXT
ABY=ABY-SmallApples t1=time$("ms") print "Found in ";t1-t0; " ms"
print "Big Yellow Apples = ";ABY END
|
|
|
Post by meerkat on Oct 26, 2022 10:10:05 GMT -5
Ok! Someone had to do it. I tried it using memory SQLite. It's fast, but doubt this is the best way to do it. It's in RunBasic simply because it's easier to write in RB than LB. But should be easy to convert to LB if anyone is interested.
Anyway - here it is as a comparison..
'There was a box of apples and pears 'Some are big, some are small; 'some are yellow, rest are green 'There are no small pears 'and no small green apples 'Some numbers are given: '25 Apples, 17 pears '32 big fruits '28 yellow ones. 'There are two more green apples then green pears. ' ------------------- 'Find the number of big yellow apples
t$ = chr$(9) 'tab sqliteconnect #mem, ":memory:" mem$ = "CREATE TABLE box(type,colr,siz)" #mem execute(mem$) ' create in memory db
begTime = time$("ms") ' ------------------------------------ ' seed the db with apples and make some yellow some green ' no small green apples please ' ------------------------------------ for i = 1 to 25 ' gimme 25 Apples colr$ = "Y" siz$ = "S" if (i and 1) then ' No small green apples colr$ = "G" siz$ = "B" end if mem$ = "INSERT INTO box VALUES('A','";siz$;"','";colr$;"')" #mem execute(mem$) next i ' ------------------------------------------------- ' seed the db with pears ' no small pears please ' ------------------------------------------------- siz$ = "B" ' All pears are big for i = 1 to 17 ' gimme 17 pears colr$ = "Y" ' some yellow some green if (i and 1) then colr$ = "G" mem$ = "INSERT INTO box VALUES('P','";siz$;"','";colr$;"')" #mem execute(mem$) next i
' --------------------------------------- ' Just a look to see whats in the box ' --------------------------------------- mem$ = "SELECT * FROM box ORDER BY type,siz,colr" #mem execute(mem$) WHILE #mem hasanswer() #row = #mem #nextrow() type$ = #row type$() siz$ = #row siz$() colr$ = #row colr$() num = num + 1 print num;t$;type$;t$;siz$;t$;colr$ WEND
' ----------------------------------------- ' need 28 yellow fruits ' if we need more yellow then change some green to yellow ' ----------------------------------------- mem$ = "SELECT count(*) as totY,colr FROM box WHERE colr = 'Y'" #mem execute(mem$) #row = #mem #nextrow() totY = #row totY() need = 28 - totY ' how many do we need to change
print "Total yellow:";totY;t$;"Need 28";t$;"Change:";need mem$ = "UPDATE box set colr = 'Y' WHERE rowid IN (SELECT rowid FROM box WHERE colr = 'G' LIMIT ";need;")" #mem execute(mem$)
' ------------------------------------------- ' need 2 more green apples than green pares ' if need more then change some green pares to yellow ' and some yellow apples to green ' ------------------------------------------- mem$ = "SELECT *,count(*) as t FROM box WHERE colr = 'G' GROUP BY type" #mem execute(mem$) WHILE #mem hasanswer() #row = #mem #nextrow() type$ = #row type$() colr$ = #row colr$() t = #row t() print type$;t$;colr$;t$;t if type$ = "P" then totGP = t if type$ = "A" then totGA = t WEND need = (totGP - totGA)/2 + 1 print "need more green apples ";need if need > 0 then ' change some green pares to yellow mem$ = "UPDATE box set colr = 'G' WHERE rowid IN (SELECT rowid FROM box WHERE type = 'A' and siz = 'B' and colr = 'Y' LIMIT ";need;")" #mem execute(mem$) mem$ = "UPDATE box set colr = 'Y' WHERE rowid IN (SELECT rowid FROM box WHERE type = 'P' and colr = 'G' LIMIT ";need;")" #mem execute(mem$) end if
' -------------------------------------------------------- ' just checking the results ' -------------------------------------------------------- print "' --------------- check green apples and pares" mem$ = "SELECT *,count(*) as t FROM box WHERE colr = 'G' GROUP BY type" #mem execute(mem$) WHILE #mem hasanswer() #row = #mem #nextrow() type$ = #row type$() colr$ = #row colr$() t = #row t() print type$;t$;colr$;t$;t WEND need = (totGP - totGA)/2 + 1 print "need more green apples ";need
' ------------------------------------------- ' need 32 big fruits ' if need change some Small to Big ' -------------------------------------------- mem$ = "SELECT count(*) as totB FROM box WHERE siz = 'B'" #mem execute(mem$) #row = #mem #nextrow() totB = #row totB() need = 32 = totB print "Total big:";totB;t$;"Need:";need
mem$ = "UPDATE box set siz = 'B' WHERE rowid IN (SELECT rowid FROM box WHERE siz = 'S' LIMIT ";need;")" #mem execute(mem$)
' ------------------------------------------ ' just checking totals by type ' ------------------------------------------ mem$ = "SELECT count(*) as t,* FROM box GROUP BY type" #mem execute(mem$) WHILE #mem hasanswer() #row = #mem #nextrow() type$ = #row type$() t = #row t() print type$;t$;t WEND
' -------------------------------------- ' The results ' totals by type, size, color ' -------------------------------------- print "---- Totals by type siz colr ----" mem$ = "SELECT count(*) as t,* FROM box GROUP BY type,siz,colr" print mem$ #mem execute(mem$) WHILE #mem hasanswer() #row = #mem #nextrow() type$ = #row type$() siz$ = #row siz$() colr$ = #row colr$() t = #row t() print type$;t$;siz$;t$;colr$;t$;t WEND endTime = time$("ms") print "Total Time:";endTime - begTime
#mem disconnect() end
|
|