2 people like it.

How many words can you spell on a calculator?

We all know about BOOBIES - but how many other dictionary words can you spell upside-down on a calculator?

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
85: 
86: 
87: 
88: 
89: 
90: 
91: 
92: 
93: 
module CalculatorWords

open System.Net
open System.Collections.Generic

// Basic mappings:
let substitutions = 
    [
        ('O', '0')
        ('I', '1')
        ('Z', '2')
        ('E', '3')
        ('H', '4')
        ('S', '5')
        ('L', '7')
        ('B', '8')
        ('G', '9')
    ]

// Map of mappings:
let subsMap = substitutions |> Map.ofList

// Letters which are in the mappings:
let numberLetters = substitutions |> List.map (fun subs -> fst(subs))

// Read a file from a url but as if it's local for performance:
let urlReader (url : string) =
    let req = WebRequest.Create(url, Timeout = 1000 * 60 * 20)
    try
        let resp = req.GetResponse()
        let stream = resp.GetResponseStream()
        let tempFileName = System.IO.Path.GetTempFileName()
        let tempFileStream = new System.IO.FileStream(tempFileName, System.IO.FileMode.Truncate)
        stream.CopyTo(tempFileStream)
        tempFileStream.Seek(int64(0), System.IO.SeekOrigin.Begin) |> ignore
        new System.IO.StreamReader(tempFileStream)
    with
        | _ as ex -> failwith ex.Message

// Read a word list and break it up into non-trivial, uppercase words:
let words() =
    let reader = urlReader "http://unix-tree.huihoo.org/V7/usr/dict/words.html"
    seq {
            while not (reader.EndOfStream) do
            yield (reader.ReadLine().ToUpper())
    } 
    |> Seq.skip 17 // Skip HTML
    |> Seq.filter (fun word -> word.Length > 2)
    |> Seq.cache

// Check if a word consists of calculator-letters:
let goodWord (word : string) =
    // The word isn't good if it has any letters which aren't one of our 'number' letters:
    word 
    |> String.exists (fun wordLetter -> ( numberLetters 
                                          |> List.tryFindIndex (fun numberLetter -> numberLetter = wordLetter
                                        ) = None))
    |> not

// Translate a word into its calculator-letters equivalent:
let translate (word : string) =
    word
    |> String.map (fun letter -> subsMap.Item letter)
    |> Array.ofSeq
    |> Array.rev
    |> Array.fold (fun acc elem -> sprintf "%s%c" acc elem) ""

// Get all the words which can be spelt on a calculator:
let calculatorWords =
    words()
    |> Seq.filter (fun word -> goodWord word)

// Count the calculator words:
let countWords = 
    calculatorWords |> Seq.length
// 179

// Print calculator words in plain and calculator versions:
let printWords() =
    calculatorWords
    |> Seq.iter (fun word -> printfn "%s (%s)" word (translate word))
// BEE (338)
// BEEBE (38338)
// BEG (938)
// BEIGE (39138)
// BEL (738)
// BELIE (31738)
// BELL (7738)
// ...
// ZIG (912)
// ZOE (302)
// ZOO (002)
 
module CalculatorWords
namespace System
namespace System.Net
namespace System.Collections
namespace System.Collections.Generic
val substitutions : (char * char) list

Full name: CalculatorWords.substitutions
val subsMap : Map<char,char>

Full name: CalculatorWords.subsMap
Multiple items
module Map

from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> =
  interface IEnumerable
  interface IComparable
  interface IEnumerable<KeyValuePair<'Key,'Value>>
  interface ICollection<KeyValuePair<'Key,'Value>>
  interface IDictionary<'Key,'Value>
  new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
  member Add : key:'Key * value:'Value -> Map<'Key,'Value>
  member ContainsKey : key:'Key -> bool
  override Equals : obj -> bool
  member Remove : key:'Key -> Map<'Key,'Value>
  ...

Full name: Microsoft.FSharp.Collections.Map<_,_>

--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val ofList : elements:('Key * 'T) list -> Map<'Key,'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Map.ofList
val numberLetters : char list

Full name: CalculatorWords.numberLetters
Multiple items
type List<'T> =
  new : unit -> List<'T> + 2 overloads
  member Add : item:'T -> unit
  member AddRange : collection:IEnumerable<'T> -> unit
  member AsReadOnly : unit -> ReadOnlyCollection<'T>
  member BinarySearch : item:'T -> int + 2 overloads
  member Capacity : int with get, set
  member Clear : unit -> unit
  member Contains : item:'T -> bool
  member ConvertAll<'TOutput> : converter:Converter<'T, 'TOutput> -> List<'TOutput>
  member CopyTo : array:'T[] -> unit + 2 overloads
  ...
  nested type Enumerator

Full name: System.Collections.Generic.List<_>

--------------------
List() : unit
List(capacity: int) : unit
List(collection: IEnumerable<'T>) : unit
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val subs : char * char
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val urlReader : url:string -> System.IO.StreamReader

Full name: CalculatorWords.urlReader
val url : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val req : WebRequest
type WebRequest =
  inherit MarshalByRefObject
  member Abort : unit -> unit
  member AuthenticationLevel : AuthenticationLevel with get, set
  member BeginGetRequestStream : callback:AsyncCallback * state:obj -> IAsyncResult
  member BeginGetResponse : callback:AsyncCallback * state:obj -> IAsyncResult
  member CachePolicy : RequestCachePolicy with get, set
  member ConnectionGroupName : string with get, set
  member ContentLength : int64 with get, set
  member ContentType : string with get, set
  member Credentials : ICredentials with get, set
  member EndGetRequestStream : asyncResult:IAsyncResult -> Stream
  ...

Full name: System.Net.WebRequest
WebRequest.Create(requestUri: System.Uri) : WebRequest
WebRequest.Create(requestUriString: string) : WebRequest
val resp : WebResponse
WebRequest.GetResponse() : WebResponse
val stream : System.IO.Stream
WebResponse.GetResponseStream() : System.IO.Stream
val tempFileName : string
namespace System.IO
type Path =
  static val DirectorySeparatorChar : char
  static val AltDirectorySeparatorChar : char
  static val VolumeSeparatorChar : char
  static val InvalidPathChars : char[]
  static val PathSeparator : char
  static member ChangeExtension : path:string * extension:string -> string
  static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
  static member GetDirectoryName : path:string -> string
  static member GetExtension : path:string -> string
  static member GetFileName : path:string -> string
  ...

Full name: System.IO.Path
System.IO.Path.GetTempFileName() : string
val tempFileStream : System.IO.FileStream
Multiple items
type FileStream =
  inherit Stream
  new : path:string * mode:FileMode -> FileStream + 14 overloads
  member BeginRead : array:byte[] * offset:int * numBytes:int * userCallback:AsyncCallback * stateObject:obj -> IAsyncResult
  member BeginWrite : array:byte[] * offset:int * numBytes:int * userCallback:AsyncCallback * stateObject:obj -> IAsyncResult
  member CanRead : bool
  member CanSeek : bool
  member CanWrite : bool
  member EndRead : asyncResult:IAsyncResult -> int
  member EndWrite : asyncResult:IAsyncResult -> unit
  member Flush : unit -> unit + 1 overload
  member GetAccessControl : unit -> FileSecurity
  ...

Full name: System.IO.FileStream

--------------------
System.IO.FileStream(path: string, mode: System.IO.FileMode) : unit
   (+0 other overloads)
System.IO.FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: System.IO.FileAccess) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, access: System.IO.FileAccess) : unit
   (+0 other overloads)
System.IO.FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: System.IO.FileAccess, bufferSize: int) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, access: System.IO.FileAccess, share: System.IO.FileShare) : unit
   (+0 other overloads)
System.IO.FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: System.IO.FileAccess, bufferSize: int, isAsync: bool) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, access: System.IO.FileAccess, share: System.IO.FileShare, bufferSize: int) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, access: System.IO.FileAccess, share: System.IO.FileShare, bufferSize: int, options: System.IO.FileOptions) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, access: System.IO.FileAccess, share: System.IO.FileShare, bufferSize: int, useAsync: bool) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, rights: System.Security.AccessControl.FileSystemRights, share: System.IO.FileShare, bufferSize: int, options: System.IO.FileOptions) : unit
   (+0 other overloads)
type FileMode =
  | CreateNew = 1
  | Create = 2
  | Open = 3
  | OpenOrCreate = 4
  | Truncate = 5
  | Append = 6

Full name: System.IO.FileMode
field System.IO.FileMode.Truncate = 5
System.IO.Stream.CopyTo(destination: System.IO.Stream) : unit
System.IO.Stream.CopyTo(destination: System.IO.Stream, bufferSize: int) : unit
System.IO.FileStream.Seek(offset: int64, origin: System.IO.SeekOrigin) : int64
Multiple items
val int64 : value:'T -> int64 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int64

--------------------
type int64 = System.Int64

Full name: Microsoft.FSharp.Core.int64

--------------------
type int64<'Measure> = int64

Full name: Microsoft.FSharp.Core.int64<_>
type SeekOrigin =
  | Begin = 0
  | Current = 1
  | End = 2

Full name: System.IO.SeekOrigin
field System.IO.SeekOrigin.Begin = 0
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Multiple items
type StreamReader =
  inherit TextReader
  new : stream:Stream -> StreamReader + 9 overloads
  member BaseStream : Stream
  member Close : unit -> unit
  member CurrentEncoding : Encoding
  member DiscardBufferedData : unit -> unit
  member EndOfStream : bool
  member Peek : unit -> int
  member Read : unit -> int + 1 overload
  member ReadLine : unit -> string
  member ReadToEnd : unit -> string
  ...

Full name: System.IO.StreamReader

--------------------
System.IO.StreamReader(stream: System.IO.Stream) : unit
System.IO.StreamReader(path: string) : unit
System.IO.StreamReader(stream: System.IO.Stream, detectEncodingFromByteOrderMarks: bool) : unit
System.IO.StreamReader(stream: System.IO.Stream, encoding: System.Text.Encoding) : unit
System.IO.StreamReader(path: string, detectEncodingFromByteOrderMarks: bool) : unit
System.IO.StreamReader(path: string, encoding: System.Text.Encoding) : unit
System.IO.StreamReader(stream: System.IO.Stream, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool) : unit
System.IO.StreamReader(path: string, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool) : unit
System.IO.StreamReader(stream: System.IO.Stream, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool, bufferSize: int) : unit
System.IO.StreamReader(path: string, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool, bufferSize: int) : unit
val ex : exn
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
property System.Exception.Message: string
val words : unit -> seq<string>

Full name: CalculatorWords.words
val reader : System.IO.StreamReader
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
property System.IO.StreamReader.EndOfStream: bool
System.IO.StreamReader.ReadLine() : string
module Seq

from Microsoft.FSharp.Collections
val skip : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.skip
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val word : string
property System.String.Length: int
val cache : source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.cache
val goodWord : word:string -> bool

Full name: CalculatorWords.goodWord
module String

from Microsoft.FSharp.Core
val exists : predicate:(char -> bool) -> str:string -> bool

Full name: Microsoft.FSharp.Core.String.exists
val wordLetter : char
val tryFindIndex : predicate:('T -> bool) -> list:'T list -> int option

Full name: Microsoft.FSharp.Collections.List.tryFindIndex
val numberLetter : char
union case Option.None: Option<'T>
val translate : word:string -> string

Full name: CalculatorWords.translate
val map : mapping:(char -> char) -> str:string -> string

Full name: Microsoft.FSharp.Core.String.map
val letter : char
property Map.Item: char -> char
module Array

from Microsoft.FSharp.Collections
val ofSeq : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Array.ofSeq
val rev : array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.rev
val fold : folder:('State -> 'T -> 'State) -> state:'State -> array:'T [] -> 'State

Full name: Microsoft.FSharp.Collections.Array.fold
val acc : string
val elem : char
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val calculatorWords : seq<string>

Full name: CalculatorWords.calculatorWords
val countWords : int

Full name: CalculatorWords.countWords
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
val printWords : unit -> unit

Full name: CalculatorWords.printWords
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view Test code New version

More information

Link:http://fssnip.net/aY
Posted:12 years ago
Author:Kit Eason
Tags: puzzles