1 people like it.

Inverse Fizz Buzz

Inspired by this post: http://www.jasq.org/2/post/2012/05/inverse-fizzbuzz.html (fixed small typo, plus trying out this editing fssnip for the first time)

 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: 
let isFizzBuzz x = 
        (x % 3 = 0) || (x % 5 = 0)

let toFizzBuzz x =
        match (x % 3,x % 5) with
        | (0,0) -> "fizzbuzz"
        | (0,_) -> "fizz"
        | (_,0) -> "buzz"
        | _ ->  ""

let fizzBuzzCandidates =
        seq {1 .. System.Int32.MaxValue } 
        |> Seq.filter(isFizzBuzz) 
        

let fizzBuzzList start lenght =
        fizzBuzzCandidates
        |> Seq.skip start
        |> Seq.take lenght   

let rec InverseFizzBuzz  (index:int) (fizzBuzzInput:list<string>) = 
        let fizzBuzzNumbers  = fizzBuzzList index fizzBuzzInput.Length                   
        let fizzBuzzStrings = fizzBuzzNumbers |> Seq.map(toFizzBuzz)                      
        if (Seq.forall2(fun x y -> x = y) fizzBuzzStrings fizzBuzzInput) then
            fizzBuzzNumbers
        else
            InverseFizzBuzz (index + 1) fizzBuzzInput

let GetInverseFizzBuzz (fizzBuzzInput:list<string>) = 
        InverseFizzBuzz 0 fizzBuzzInput     
val isFizzBuzz : x:int -> bool

Full name: Script.isFizzBuzz
val x : int
val toFizzBuzz : x:int -> string

Full name: Script.toFizzBuzz
val fizzBuzzCandidates : seq<int>

Full name: Script.fizzBuzzCandidates
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

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

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
namespace System
type Int32 =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 3 overloads
    static val MaxValue : int
    static val MinValue : int
    static member Parse : s:string -> int + 3 overloads
    static member TryParse : s:string * result:int -> bool + 1 overload
  end

Full name: System.Int32
field int.MaxValue = 2147483647
module Seq

from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val fizzBuzzList : start:int -> lenght:int -> seq<int>

Full name: Script.fizzBuzzList
val start : int
val lenght : int
val skip : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.skip
val take : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
val InverseFizzBuzz : index:int -> fizzBuzzInput:string list -> seq<int>

Full name: Script.InverseFizzBuzz
val index : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val fizzBuzzInput : string list
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
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 fizzBuzzNumbers : seq<int>
property List.Length: int
val fizzBuzzStrings : seq<string>
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val forall2 : predicate:('T1 -> 'T2 -> bool) -> source1:seq<'T1> -> source2:seq<'T2> -> bool

Full name: Microsoft.FSharp.Collections.Seq.forall2
val x : string
val y : string
val GetInverseFizzBuzz : fizzBuzzInput:string list -> seq<int>

Full name: Script.GetInverseFizzBuzz

More information

Link:http://fssnip.net/bS
Posted:11 years ago
Author:Tony Abell
Tags: fizz buzz