Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #44097

Re: Scrabble problem

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: Scrabble problem
Date 2015-12-06 02:00 +0000
Organization A noiseless patient Spider
Message-ID <n404r9$3st$1@dont-email.me> (permalink)
References <n3v8fa$otf$1@dont-email.me>

Show all headers | View raw


WJ wrote:

> Given a rack of letters, find all of the words in
> a file containing one word per line that can be
> formed from that rack.
> 
> 
> Ruby:
> 
> def good_word? word, sorted_rack
>   word.split("").group_by{|x| x}.map{|k,v| v.join}.
>   all?{|s| sorted_rack.index s}
> end
> 
> def find_in_file filename, rack
>   sorted_rack = rack.split("").sort.join
>   IO.readlines(filename).map(&:strip).
>   select{|word| good_word? word, sorted_rack}
> end
> 
> 
> find_in_file "words", "qwerty"
>     ==>["e", "er", "et", "q", "r", "re", "rye", "t", "te", "ter", "tr", "trey",
> "try", "tw", "tyr", "tyre", "we", "wet", "wry", "y", "ye", "yet", "yew"]
> 
> This was easy in Ruby, but I feel sorry for anyone who has to solve
> this in the Toy Language (ANS Forth).

Ocaml:

#load "str.cma";;
open Str;;
open String;;

let good_word word rack =
  let rack = ref rack in
  List.for_all
    (fun s ->
      if contains !rack s.[0] then
        (rack := replace_first (regexp s) "" !rack ; true)
      else
        false)
    (split (regexp "") word);;

let find_in_file filename rack=
  let chan=open_in filename in
  let rec go()=
    try
      let word=input_line chan in
        if good_word word rack then word::go() else go()
    with _->close_in chan;[]
  in go();;


find_in_file "words" "qwerty";;
 ===>
["e"; "er"; "et"; "q"; "r"; "re"; "rye"; "t"; "te"; "ter"; "tr"; "trey";
 "try"; "tw"; "tyr"; "tyre"; "we"; "wet"; "wry"; "y"; "ye"; "yet"; "yew"]

-- 
I would hesitate to describe America as a nation at all in 1997, because it is
something very different: a polyethnic federation administered by a single
ruling class....  [T]he accumulated rot in America has now reached a point
where the pillars of society itself are failing.  --- Ambrose Evans-Pritchard

Back to comp.lang.forth | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Scrabble problem "WJ" <w_a_x_man@yahoo.com> - 2015-12-05 17:55 +0000
  Re: Scrabble problem Julian Fondren <julian.fondren@gmail.com> - 2015-12-05 11:41 -0800
    Re: Scrabble problem Julian Fondren <julian.fondren@gmail.com> - 2015-12-05 11:51 -0800
  Re: Scrabble problem "WJ" <w_a_x_man@yahoo.com> - 2015-12-06 02:00 +0000

csiph-web