Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #54963
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: Forth on spoj.com |
| Date | 2017-02-18 07:06 +0000 |
| Organization | Institut fuer Computersprachen, Technische Universitaet Wien |
| Message-ID | <2017Feb18.080616@mips.complang.tuwien.ac.at> (permalink) |
| References | <189bb751-d153-48a5-afd4-894c32aefe5a@googlegroups.com> |
Julian Fondren <julian.fondren@gmail.com> writes:
>The challenges also seemed tricky in a bad way: the
>first challenge has you write a program that "stops processing input"
>when it reads in 42 on a line, this somehow meaning that it must still
>exhaust its input source after reading that number, which is a
>ludicrous thing to do.
It actually turns out that this helps make the solution shorter, see
below.
I took quite a while (and resorting to Google) to find that problem
(It seems they have ~20000 problems), it's
http://www.spoj.com/problems/TEST/
> [undefined] stdin [if] 0 constant stdin [then]
If the system does not have STDIN, isn't it better if the program
fails complaining about that rather than failing later at the
READ-FILE? If a system treats 0 as STDIN, one could test for that
system rather than for the absence of STDIN.
> 1024 8 * constant /buffer
> create buffer /buffer allot
>
> : ?end ( f x -- f x ) over 0= if bye then ;
>
> : flush-line ( -- )
> begin buffer /buffer stdin read-line throw ?end nip if exit then again ;
>
> : ?42 ( n -- )
> buffer over s" 42" compare if buffer swap type cr exit then
> drop begin flush-line again ;
>
> : go ( -- )
> begin
> buffer /buffer stdin read-line ?end throw
> if ?42 else drop flush-line then
> again ;
>
> go
Gforth-specific (but your program already is, because of STDIN):
stdin slurp-fid 2dup s" 42" search and nip - type bye
[not tested on spoj.com, I would have to make an account there]
Note that the AND NIP - sequence produces the right result in case
there is no 42 in the input; if I did not care about that case, I
would have written
stdin slurp-fid 2dup s" 42" search 2drop nip over - type bye
which is conceptually a little simpler, although the code is longer.
If the requirement was not to read past the 42, we could not use
SLURP-FID, but would have to use READ-LINE in a loop, resulting in a
longer program.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2016: http://www.euroforth.org/ef16/
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Forth on spoj.com Julian Fondren <julian.fondren@gmail.com> - 2017-02-17 21:29 -0800
Re: Forth on spoj.com anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-02-18 07:06 +0000
Re: Forth on spoj.com Julian Fondren <julian.fondren@gmail.com> - 2017-02-18 05:42 -0800
Re: Forth on spoj.com anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-02-18 14:56 +0000
Re: Forth on spoj.com Julian Fondren <julian.fondren@gmail.com> - 2017-02-18 12:58 -0800
Re: Forth on spoj.com albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-02-19 11:35 +0100
Re: Forth on spoj.com albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-04 14:58 +0200
Re: Forth on spoj.com Mark Wills <markwills1970@gmail.com> - 2017-04-04 07:40 -0700
Re: Forth on spoj.com Julian Fondren <julian.fondren@gmail.com> - 2017-04-04 07:52 -0700
Re: Forth on spoj.com albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-04 17:36 +0200
Re: Forth on spoj.com Mark Wills <markwills1970@gmail.com> - 2017-04-04 09:05 -0700
Re: Forth on spoj.com NN <november.nihal@gmail.com> - 2017-04-04 11:22 -0700
Re: Forth on spoj.com Julian Fondren <julian.fondren@gmail.com> - 2017-04-04 11:33 -0700
Re: Forth on spoj.com albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-04-05 02:12 +0200
Re: Forth on spoj.com rickman <gnuarm@gmail.com> - 2017-04-04 16:12 -0400
Re: Forth on spoj.com Julian Fondren <julian.fondren@gmail.com> - 2017-02-19 22:59 -0800
Re: Forth on spoj.com Lars Brinkhoff <lars.spam@nocrew.org> - 2017-04-03 20:06 +0200
csiph-web