> I use racket for hackerrank and coding contests all the time, and I find it's 
> read syntax really useful. For instance, I would parse this into a list of 
> lists by doing 
> (for/list ([i (in-range (read))])
>   (map (lambda (x) (if (list? x) (cadr x) x)) (read))).
> 
> Then, to print out results, I normally do 
> (define (main n)
>   (unless (= 0 n)
>     (begin (printf "~a ... ~a\n", args...)
>       (main (sub1 n)))).
> 
> Another really useful helper is 
> (define (read->list n)
>   (if (= 0 n) '()
>     (cons (read) (read->list (sub1 n)))). 
> 
> You can customize these by doing for/vector, for/fold, etc., and there hasn't 
> been a hackerrank contest I've run into that I haven't been able to do with 
> some technique like this.

Correction, not "read syntax". My PL professor would fail me for that. I'd say 
that there are very few hackerrank and other contests input format that can't 
be parsed as an s-expression in some way (unless they do weird things with 
parentheses, in which case you have to use read-line/char/byte). 

Just note that a value with a comma in front of it is parsed as a list where 
the first item in the list is the unquote, and the second item is the value 
itself. Regardless, it's fairly easy to use the common list functions to 
extract the data you want.

-- 
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.

Reply via email to