|
Post by svajoklis on May 13, 2020 9:48:56 GMT -5
Rod's solution is pretty nice though it still iterates through all previous values. That violates the optional objective of only iterating though values once since the instr$ searches the whole string where "modified" values are kept
|
|
ombre
New Member
Posts: 12
|
Post by ombre on May 13, 2020 10:31:11 GMT -5
Well none of the number pairs add up to 15, try changing 17 to 13 then you should get a match. I think there is a misunderstanding on how the program should work. You should not have to change the number so that the program works, but change the program so that «any» number works. The problem I found was that a number in a list, as an example 15, the instr command gives you a position but still analyses the 2 digits (1 and 5) for the search number. This is what gives the error. rem Challenge1 n$="10, 15, 3, 7, 14, 20, 9, 1, 11" pos=1 n=val(word$(n$,pos)):k=15 while n > 0 if instr(n$,str$(k-n), 1) > 0 then print n ,"oui" else print n, " non" end if pos=pos+1 n=val(word$(n$,pos)) wend The 10 number gives a positive answer because the «instr» command analyses not only de «15» as a whole but both the «1» and the «5» . 15-5=10 so "ok"
|
|
|
Post by tsh73 on May 13, 2020 11:07:27 GMT -5
For Instr you always could include delimiters (known trick) Like
n$="10 15 3 7 14 20 9 1 11"
f$="5" print "is ";f$;" is in list "; n$;"?" print instr(" "+n$+" ", " "+f$+" ")>0
f$="15" print "is ";f$;" is in list "; n$;"?" print instr(" "+n$+" ", " "+f$+" ")>0
f$="10" print "is ";f$;" is in list "; n$;"?" print instr(" "+n$+" ", " "+f$+" ")>0
f$="11" print "is ";f$;" is in list "; n$;"?" print instr(" "+n$+" ", " "+f$+" ")>0
f$="0" print "is ";f$;" is in list "; n$;"?" print instr(" "+n$+" ", " "+f$+" ")>0
is 5 is in list 10 15 3 7 14 20 9 1 11? 0 is 15 is in list 10 15 3 7 14 20 9 1 11? 1 is 10 is in list 10 15 3 7 14 20 9 1 11? 1 is 11 is in list 10 15 3 7 14 20 9 1 11? 1 is 0 is in list 10 15 3 7 14 20 9 1 11? 0
|
|
|
Post by Rod on May 14, 2020 9:30:31 GMT -5
Thank you Ombre and tsh73 I see my mistake now, I added a " ".
n$=" 10 15 3 7" num=13 pos=1 n=val(word$(n$,pos)) while n>0 if instr(n$," "+str$(num-n),1)>0 then print n,"yes" pos=pos+1 n=val(word$(n$,pos)) wend
|
|