Post by tenochtitlanuk on May 14, 2020 15:09:15 GMT -5
I thought much the same ways. Especially the need to think and optimise... Once I knew it worked for the original two examples, I did a kind of Monte Carlo method generating k and s at random but in range. Immediately showed how slow ten digits could get!
It's quite fun to watch for the smaller k values, but for 5 or more digits gets slow.....
EDIT (updated code)
Typical grabbed output..
Testing 4 digit numbers, between 1023 and 9876 whose digits sum to 6 1023 is OK for sum = 6 3210 is OK for sum = 6 Found
Testing 2 digit numbers, between 10 and 98 whose digits sum to 20 None
Testing 2 digit numbers, between 10 and 98 whose digits sum to 10 19 is OK for sum = 10 91 is OK for sum = 10 Found
Testing 2 digit numbers, between 10 and 98 whose digits sum to 3 12 is OK for sum = 3 30 is OK for sum = 3 Found
Testing 7 digit numbers, between 1023456 and 9876543 whose digits sum to 38 1256789 is OK for sum = 38 9876530 is OK for sum = 38 Found
Testing 3 digit numbers, between 102 and 987 whose digits sum to 11 128 is OK for sum = 11 920 is OK for sum = 11 Found
Testing 6 digit numbers, between 102345 and 987654 whose digits sum to 15 102345 is OK for sum = 15 543210 is OK for sum = 15 Found
Testing 5 digit numbers, between 10234 and 98765 whose digits sum to 36 None
Testing 5 digit numbers, between 10234 and 98765 whose digits sum to 9 None
As before I append my code lower on this page!
' Weekly Challenge problem #2
' You have two whole numbers k and s ( 1 <= k <= 10, 1 <= s <= 100).
' Find the biggest and the smallest numbers so that: ' - each one is made up out of non-repeating digits ' - each one has k digits ' - each one doesn't start with a 0 ' - the sum of digits is s
' If there are no numbers that satisfy these conditions then output "none" ' k s data 4, 6 'max = 3210, min = 1023 data 2, 20 'none data 2, 10 'max = 91, min = 19 data 2, 3 'max = 30, min = 12 data 7, 38 'max = 9876530, min = 1256789 data 3, 11 'max = 920, min = 128 data 6, 15 'max = 543210, min = 102345 data 5, 36 'none data 5, 9 'none
open "anaysis2.txt" for output as #fOut
global digit$, k, s, finished, result$ digFwd$ ="0123456789" digRev$ ="9876543210" order$ ="1023456789" ' smallest number is sum of leftmost k characters of this.
for d =1 to 9 read k, s result$ ="None"
biggestPoss =val( left$( digRev$, k)) lowestPoss =val( left$( order$, k)) #fOut " Testing "; k; " digit numbers, between ";lowestPoss; " and "; biggestPoss; " whose digits sum to "; s print " Testing "; k; " digit numbers, between ";lowestPoss; " and "; biggestPoss; " whose digits sum to "; s
for n =lowestPoss to biggestPoss call test n if finished =1 then exit for next n
sub quit h$ close #fOut end sub
sub test n ' testNumber finished =0 n$ =str$( n) available$ =digit$ for p =1 to len( n$) sum =sum +val( mid$( n$, p, 1)) next p if ( sum =s) and digitsDifferent( n$) =1 then #fOut n$; " is OK for sum = "; s: print n$; " is OK for sum = "; s: result$ ="Found"
'if ( sum >10^k) then finished =1': print "None." scan end sub
function digitsDifferent( n$) digitsDifferent =1 for q =1 to len( n$) -1 currentDigit$ =mid$( n$, q, 1) if instr( mid$( n$, q +1), currentDigit$) then digitsDifferent =0 next q end function
Post by tenochtitlanuk on May 10, 2020 16:31:27 GMT -5
Tried again on another machine. Still get Com1 as an invalid handle. One small success- managed to get 'cutecom' in Linux on the menu selection to work withoud needing 'sudo cutecom' at terminal as previously. But any comms terminals installed under Wine fail to see the port.. Out of time and ideas. Not a deal-breaker for me, but very annoying! Thanks for your suggestions- perhaps you'll think of something which should have'seemed obvious to me!
Post by tenochtitlanuk on May 9, 2020 11:55:16 GMT -5
Thanks for taking the time to give useful suggestions. And further thoughts!
I still can't get it working under Wine. Tried symlink, group access, Registry editing, resetting... I wonder if I did the registry edit wrongly? My set-up is the same as yours.. Mint 19.3 Cinnamon, Wine 4.0.5
Very frustrating to see the device steadily streaming the ten channels from a Linux serial terminal, but be unable to access them in LB/Wine
'You are given a list of numbers and a number k. 'You should print out whether any two numbers from the list add up to k. 'Example: given [10, 15, 3, 7] and k of 17, print "yes" since 10 + 7 is 17. 'Bonus points: can you do this in one pass?
dim num( 100)
for check =1 to 50 n$ =n$ +str$( int( 30 *rnd( 1))) if check <>50 then n$ =n$ +"," next check
do e$ =word$( n$, cnt, ",") a =val( e$) num( cnt) =a cnt =cnt +1 loop until e$ =""
cnt =cnt -1
for first =1 to cnt -1 if num( first) <sum then for second =first +1 to cnt if num( first) +num( second) =sum then print num( first); " + "; num( second); "= "; sum next second end if next first
open "page.txt" for output as #fOut #fOut page$; close #fOut
The saved text included headings and data. Too lazy to write any parsing code, BUT.. searching in a text editor for as an example 'Previous Close' and find this section.. Previous Close</span></td><td class="Ta(end) Fw(600) Lh(14px)" data-test="PREV_CLOSE-value" data-reactid="15"><span class="Trsdu(0.3s) " data-reactid="16">17.26</spa.... The data associated with that heading is correctly found. I helped another LB poster do something similar downloading from a webpage and parsing out data- in his case to generate barcodes and printing them..
It's MESSY to search like this in LB, but straightforward. Only worth while if you have only one or a few target pages.
Post by tenochtitlanuk on May 2, 2020 5:49:08 GMT -5
I didn't really understand scaling and rotating fonts, so wrote a test program to try the parameters over various ranges. Learned it rotates around bounding box bottom lefthand corner was one useful outcome.
A serendipitous outcome was this graphic! Uses cycling colours and rotates/scales the character '!' each time.
Cheered ME at least up- at present locked down at home for Corvid isolation, with modem down ( so VERY slow access to web via mobile phone data in a one-bar-if-you-are-lucky house in a blackspot phone area), and with a plumbing fault that has meant we've had no hot water from taps for three months! At least the Spring garden and countriside are beautiful...
WindowWidth =600 WindowHeight =600
open "test" for graphics_nsb as #mg
#mg "trapclose [quit]"
hw = hwnd( #mg)
calldll #user32, "GetDC", hw as ulong, hdc as ulong
calldll #gdi32, "SetBkMode", hdc as long, 1 As long, result As long
#mg "home ; fill darkblue ; color cyan ; backcolor darkblue" #mg "fill darkblue ; color cyan ; backcolor darkblue"
weight = 0 ' how much to emphasize /widen the strokes height =470 ' how big to make font ?in pixels?
for angle =0 to -10800 step -50' 3 revolutions- rotates about bottom left-hand od character bounding box
height =height -2 ' progressively reduce font size
calldll #gdi32, "CreateFontA",_ height as long, 0 as long,_ angle as long, angle as long,_ weight as long,_ 0 as long, 0 as long, 0 as long, 0 as long, 0 as long, 0 as long, 0 as long, 0 as long,_ "Arial" as ptr,_ hfont as ulong
calldll #gdi32, "SelectObject", hdc as ulong, hfont as ulong, oldhfont as ulong
Post by tenochtitlanuk on Apr 20, 2020 4:05:08 GMT -5
Looks like the parsing routine thinks variables with names like E2 are numerics in scientific notation , ie 1 times ten squared.
If so, an LB bug- avoid by using variables with helpful names like 'endElevation'. That has the side effect of making your code flow easier to follow by others- and yourself looking back at it! Another example- 'Q' means nothing to a human reader. 'discharge' does.
PS My first acquaintancee with coding for a real computer was Titan Autocode, which became Fortran. It had HUGELY restricted rules on names of variables and whether they would be integer or float.
for i =1 to 10000 scan r =int( 40 +216 *rnd( 1)) g =int( 40 +216 *rnd( 1)) b =int( 40 +216 *rnd( 1)) fillCol$ =str$( r) +" " +str$( g); " " +str$( b) #wg "backcolor "; fillCol$ targetcolor =0 ' this is the colour black of the outline to fill out to. x =int( 20 +230 *rnd( 1)) y =int( 20 +230 *rnd( 1))
calldll #gdi32, "ExtFloodFill",_ hdc as ulong,_ x as long,_ y as long,_ targetcolor as long,_ _FLOODFILLBORDER as long,_ ' ' ie fill out 'til this colour is met... result as long
'calldll #kernel32, "Sleep", 40 as long, ret as void if i >9900 then ' gives time for all panes to be ( probably) filled... #wg "getbmp scr 1 1 285 295" bmpsave "scr", "s" +right$( "0000" +str$( i), 4) +".bmp" end if
[quit] calldll #user32, "ReleaseDC", hw as ulong, hdc as ulong, ret as void 'release the DC close #wg end