0 people like it.

Shuffle letters

Added recursive version

 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: 
open System
open System.Linq

let marks = [","; "."; "?"; "!"; "?!"]
let rnd = new Random()

let rec insert v i l =
    match i, l with
    | 0, h -> v::h
    | i, h::t -> h::insert v (i - 1) t
    | _, [] -> failwith "index out of range"

let rec remove i l =
    match i, l with
    | 0, h::t -> t
    | i, h::t -> h::remove (i - 1) t
    | _, [] -> failwith "index out of range"

let shuffleWord (input:string) =
    let result = Array.create input.Length '\000'
    printf "%A| " input
    let rec _shuffle (inp:list<char>) ix =
        match ix with
        | 0 -> [inp.[0]]
        | _ ->
            let idx = rnd.Next(inp.Length - 1)
            inp.[idx] :: _shuffle (remove idx inp) (ix - 1)

    let w = 
        match input.Length with
        | l when l = 1 -> [input.[0]]
        | l when l < 3 -> [input.[0]; input.[1]]
        | l when l = 3 -> [input.[0]; input.[2]; input.[1]]
        | _ -> 
            let midPart = input.[1 .. input.Length - 2] |> List.ofSeq
            input.[0] :: (_shuffle midPart (midPart.Length - 1 )) @ [input.Last()]

    w
    |> List.map (fun (c:char) -> c.ToString())
    |> List.reduce (+)

let rec shuffle (input:string) =
    let rec _clear (input:string) marks =
        match marks with
        | [] -> [input]
        | h::t -> 
            if input.EndsWith(h) then (input.Substring(0, input.Length - 1)) :: [h]
            else _clear input t

    let rec _shuffle list =
        match list with
        | [] -> []
        | h::t -> ((_clear h marks) |> List.map shuffleWord) @ _shuffle t

    let arrayOfWords = (input.Split([|" "|], StringSplitOptions.None) |> List.ofArray)
    _shuffle arrayOfWords |> List.reduce (fun a b -> a + " " + b)


shuffle "By the way code below was used as brain practice to shuffle this text."
namespace System
namespace System.Linq
val marks : string list

Full name: Script.marks
val rnd : Random

Full name: Script.rnd
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
Random() : unit
Random(Seed: int) : unit
val insert : v:'a -> i:int -> l:'a list -> 'a list

Full name: Script.insert
val v : 'a
val i : int
val l : 'a list
val h : 'a list
val h : 'a
val t : 'a list
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val remove : i:int -> l:'a list -> 'a list

Full name: Script.remove
val shuffleWord : input:string -> string

Full name: Script.shuffleWord
val input : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val result : char []
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val create : count:int -> value:'T -> 'T []

Full name: Microsoft.FSharp.Collections.Array.create
property String.Length: int
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
val _shuffle : (char list -> int -> char list)
val inp : char list
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
Multiple items
val char : value:'T -> char (requires member op_Explicit)

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

--------------------
type char = Char

Full name: Microsoft.FSharp.Core.char
val ix : int
val idx : int
Random.Next() : int
Random.Next(maxValue: int) : int
Random.Next(minValue: int, maxValue: int) : int
property List.Length: int
val w : char list
val l : int
val midPart : char list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val ofSeq : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.List.ofSeq
(extension) Collections.Generic.IEnumerable.Last<'TSource>() : 'TSource
(extension) Collections.Generic.IEnumerable.Last<'TSource>(predicate: Func<'TSource,bool>) : 'TSource
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val c : char
Char.ToString() : string
Char.ToString(provider: IFormatProvider) : string
val reduce : reduction:('T -> 'T -> 'T) -> list:'T list -> 'T

Full name: Microsoft.FSharp.Collections.List.reduce
val shuffle : input:string -> string

Full name: Script.shuffle
val _clear : (string -> string list -> string list)
val marks : string list
val h : string
val t : string list
String.EndsWith(value: string) : bool
String.EndsWith(value: string, comparisonType: StringComparison) : bool
String.EndsWith(value: string, ignoreCase: bool, culture: Globalization.CultureInfo) : bool
String.Substring(startIndex: int) : string
String.Substring(startIndex: int, length: int) : string
val _shuffle : (string list -> string list)
Multiple items
val list : string list

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val arrayOfWords : string list
String.Split([<ParamArray>] separator: char []) : string []
String.Split(separator: string [], options: StringSplitOptions) : string []
String.Split(separator: char [], options: StringSplitOptions) : string []
String.Split(separator: char [], count: int) : string []
String.Split(separator: string [], count: int, options: StringSplitOptions) : string []
String.Split(separator: char [], count: int, options: StringSplitOptions) : string []
type StringSplitOptions =
  | None = 0
  | RemoveEmptyEntries = 1

Full name: System.StringSplitOptions
field StringSplitOptions.None = 0
val ofArray : array:'T [] -> 'T list

Full name: Microsoft.FSharp.Collections.List.ofArray
val a : string
val b : string

More information

Link:http://fssnip.net/jN
Posted:10 years ago
Author:
Tags: shuffle