Hi, I have coached several teams on using Racket in programming contests. In our local contests, the most common input format is to have one line per dataset, and each dataset is typically several pieces of data separated by spaces. For this common input format, the 2htdp/batch-io teachpack is the most convenient tool.
Here is a document I give to beginner students about how to use the teachpack for the common case: https://docs.google.com/document/d/1rJ9psWwB6qbYrYSKriQi15IpgN1qjQSxE4riAPHhQE8/edit?usp=sharing But the advanced students sometimes run into files like the one you describe, where the input has a somewhat different. I teach the students to build up a little toolkit of useful functions that they can write very quickly once at the beginning of the contest -- functions that are useful for "slicing and dicing" any file into a list of datasets. Here are three such functions, that come in handy: #lang racket (require srfi/1) ; f->l stands for file->lines. We use the shorter name for less typing in a contest, and to avoid conflict with built-in file->lines ; A hard lesson we learned is that in contests, the input files aren't always "clean". Sometimes there are stray spaces at the ; front or end of certain lines, or extra blank lines at the end of the file. This version of file->lines sanitizes the input. (define (f->l file) (reverse (drop-while (curry equal? "") (reverse (map string-trim (file->lines file)))))) ; convert takes a string as an input and converts it to a number if it is a number, otherwise, leaves it alone (define (convert s) (if (string->number s)(string->number s) s)) ; string-parse takes a string which is strings and/or numbers separated by spaces, and returns a list of those elements ; So (string-parse "hello 1 2.0 world") returns (list "hello" 1 2.0 "world") (define (string-parse s) (map convert (string-split s))) Once these three functions are written (once at the beginning of the contest), you can tackle a problem like yours as follows: ; Now we solve the contest problem ; We don't need the initial number of lines, because Racket is awesome. ; Let's take a moment to pity the students who use languages that force them to read ; the data into a fixed-length array. ; Now, let's do it our way, and drop the number of lines with a simple call to rest. (define lines (rest (f->l "C:/temp/infile.dat"))) ; We can put a line into a format consumable by string-parse by getting rid of brackets and commas ; regexp-replace* is a very useful function to remember ; Make a habit out of using #px rather than #rx, as it supports some additional features that can be useful, ; and it's easier to just always use #px instead of trying to recall which features require which regexp type. (define datasets (for*/list ([line lines]) (string-parse (regexp-replace* #px"\\[|\\]|," line "")))) ; So that's all it took with our "library" of three functions in place. ; It just took two lines to massage the input file into a list of datasets. ; Now we implement the logic of processing a given dataset. ; If you know match, you might want to write it this way: (define (process-dataset ds) (match ds [`(,a ,b ,c) (printf "[~a, ~a, ~a]\n" (~a a #:width 2 #:align 'right #:pad-string "0 b (~r c #:min-width 4 #:precision 1 #:pad-string "0"))])) (for ([dataset datasets]) (process-dataset dataset)) ; Alternatively, if you don't know match, you can write it as follows, taking advantage of apply. (define (process-dataset a b c) (printf "[~a, ~a, ~a]\n" (~a a #:width 2 #:align 'right #:pad-string "0") b (~r c #:min-width 4 #:precision 1 #:pad-string "0"))) (for ([dataset datasets]) (apply process-dataset dataset)) With most problems, it only takes a few lines like this to manipulate the input file into a list of datasets, so my students have done well without a scanf function. Not that it wouldn't hurt to have it in specific situations, but it's usually not necessary. Good luck in the contest! -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.