Skip to content

Instantly share code, notes, and snippets.

@draegtun
Created December 8, 2016 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draegtun/9154ea7920191600168399c54bf1cc06 to your computer and use it in GitHub Desktop.
Save draegtun/9154ea7920191600168399c54bf1cc06 to your computer and use it in GitHub Desktop.
Rebol [
see: https://www.reddit.com/r/dailyprogrammer/comments/5go843/20161205_challenge_294_easy_rack_management_1/
]
scrabble: function [rack word] [
rack: reverse sort copy rack
score: 0
score-and-remove: func [s] [
score: score + score-letter s/1
remove s
]
foreach letter word [
let: charset reduce [letter #"?"]
either found? f: find rack let [score-and-remove f] [return false]
]
score
]
scrabble?: function [rack word] [to-logic scrabble rack word]
score-letter: func [letter] [
if letter == #"?" [return 0]
pick [1 3 3 2 1 4 2 4 1 8 5 1 3 1 1 3 10 1 1 1 1 4 4 8 4 10] (to-integer letter) - 96
]
dictionary: array [20 0] use [w] [
parse to-string read https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/dotnetperls-controls/enable1.txt [
any [
copy w to [newline | end] (attempt [append dictionary/(length? w) w])
skip
]
]
]
longest: func [rack /local len] [
len: length? rack
until [
foreach word dictionary/:len [
if scrabble rack word [return word]
]
-- len
zero? len
]
none
]
highest: func [rack /local len] [
len: length? rack
last sort/skip collect [
until [
foreach word dictionary/:len [
if score: scrabble rack word [keep reduce [score word]]
]
-- len
zero? len
]
] 2
]
print scrabble? "ladilmy" "daily" ;-> true
print scrabble? "eerriin" "eerie" ;-> false
print scrabble? "orrpgma" "program" ;-> true
print scrabble? "orppgma" "program" ;-> false
print scrabble? "pizza??" "pizzazz" ;-> true
print scrabble? "piizza?" "pizzazz" ; -> false
print scrabble? "a??????" "program" ; -> true
print scrabble? "b??????" "program" ; -> false
print longest "dcthoyueorza" ;-> "coauthored"
print longest "uruqrnytrois" ;-> "turquois"
print longest "rryqeiaegicgeo??" ;-> "greengrocery"
print longest "udosjanyuiuebr??" ;-> "subordinately"
print longest "vaakojeaietg????????" ;-> "ovolactovegetarian"
print highest "dcthoyueorza" ; "zydeco"
print highest "uruqrnytrois" ; "squinty"
print highest "rryqeiaegicgeo??" ; "reacquiring"
print highest "udosjanyuiuebr??" ; "jaybirds"
print highest "vaakojeaietg????????" ; "straightjacketed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment