5 people like it.

String Manipulation

hackthissite.org programming level 12, string manipulation.

 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: 
(* Hackthissite.org Programming level 12. 

Problem text:
This level is about string manipulation.
In this challenge, you will be given a string. 
Take all the numbers from the string and classify them as 
composite numbers or prime numbers. 
You should assume all numbers are one digit, and neither 
number 1 nor number 0 counts. 

Find the sum of every composite number, 
then find the sum of every prime number. 
Multiply these sums together. 
Then, take the first 25 non-numeric characters of 
the given string and increment their ASCII value by one 
(for example, # becomes $). 
Take these 25 characters and concatenate the product to them. 
This is your answer.
Your answer should look like this: oc{lujxdpb%jvqrt{luruudtx140224

Summarised algorithm.
1. extract single numbers from string excluding 1 and 0
2. classify sequence as prime or composite.
3. sum primes and sum composites, multiply result
4. take first 25 non-numeric chars, increment ascii value by 1.
5. concat product from step 3 to the value of step 4.
*)
open System

let isValidNumber d = d > 47 && d < 58

// ensure no 0 or 1
let extractNumbers (str : string) = 
  str
  |> Seq.filter (fun x -> Char.IsDigit x && x <> '0' && x <> '1')
  |> Seq.map (string >> int)

let isPrime d =
  [2; 3; 5; 7] |> Seq.exists ((=) d)

let sum f = Seq.filter f >> Seq.sum

let getSumProduct input = 
  let intsOfInput = input       |> extractNumbers  
  let primes      = intsOfInput |> sum isPrime   
  let compos      = intsOfInput |> sum (not << isPrime)  
  
  primes * compos 

let first25Add (xs : string) = 
  xs |> Seq.filter (int >> isValidNumber >> not)
     |> Seq.take 25
     |> Seq.map (int >> (+) 1 >> char >> string)
     |> String.concat ""

let generateAnswer input = 
  first25Add input ^ (getSumProduct input |> string)
  |> System.Windows.Forms.Clipboard.SetText
namespace System
val isValidNumber : d:int -> bool

Full name: Script.isValidNumber
val d : int
val extractNumbers : str:string -> seq<int>

Full name: Script.extractNumbers
val str : string
Multiple items
val string : value:'T -> string

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

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

Full name: Microsoft.FSharp.Core.string
module Seq

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

Full name: Microsoft.FSharp.Collections.Seq.filter
val x : char
type Char =
  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 + 1 overload
    static val MaxValue : char
    static val MinValue : char
    static member ConvertFromUtf32 : utf32:int -> string
    static member ConvertToUtf32 : highSurrogate:char * lowSurrogate:char -> int + 1 overload
    static member GetNumericValue : c:char -> float + 1 overload
    ...
  end

Full name: System.Char
Char.IsDigit(c: char) : bool
Char.IsDigit(s: string, index: int) : bool
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
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 isPrime : d:int -> bool

Full name: Script.isPrime
val exists : predicate:('T -> bool) -> source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.exists
val sum : f:(int -> bool) -> (seq<int> -> int)

Full name: Script.sum
val f : (int -> bool)
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.sum
val getSumProduct : input:string -> int

Full name: Script.getSumProduct
val input : string
val intsOfInput : seq<int>
val primes : int
val compos : int
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val first25Add : xs:string -> string

Full name: Script.first25Add
val xs : string
val take : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
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
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
val concat : sep:string -> strings:seq<string> -> string

Full name: Microsoft.FSharp.Core.String.concat
val generateAnswer : input:string -> unit

Full name: Script.generateAnswer
namespace System.Windows
namespace System.Windows.Forms
type Clipboard =
  static member Clear : unit -> unit
  static member ContainsAudio : unit -> bool
  static member ContainsData : format:string -> bool
  static member ContainsFileDropList : unit -> bool
  static member ContainsImage : unit -> bool
  static member ContainsText : unit -> bool + 1 overload
  static member GetAudioStream : unit -> Stream
  static member GetData : format:string -> obj
  static member GetDataObject : unit -> IDataObject
  static member GetFileDropList : unit -> StringCollection
  ...

Full name: System.Windows.Forms.Clipboard
Windows.Forms.Clipboard.SetText(text: string) : unit
Windows.Forms.Clipboard.SetText(text: string, format: Windows.Forms.TextDataFormat) : unit

More information

Link:http://fssnip.net/gI
Posted:10 years ago
Author:David Klein
Tags: string manipulation