6 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: 
59: 
60: 
(* 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.
*)
let isElem  x e  = Seq.exists ((=) e) x
let isPrime      = isElem [2; 3; 5; 7]  
let sumOf f : int seq -> int = Seq.filter f >> Seq.sum

let getSumProduct xs = 
  let extractNumbers =
    Seq.filter (isElem "23456789") >> Seq.map (string >> int)

  let intxs  =  extractNumbers xs  
  let primes = intxs |> sumOf isPrime   
  let compos = intxs |> sumOf (not << isPrime)  
  
  primes * compos 

let toString : char seq -> string = 
  Seq.map string >> String.concat ""

let incrChar (x : char) = char (1 + int x)

let take25NonIntsIncrByOne xs = 
  xs 
  |> Seq.filter (not << isElem "0123456789") 
  |> Seq.take 25
  |> Seq.map incrChar
  |> toString

let generateAnswer input = 
  take25NonIntsIncrByOne input ^ (getSumProduct input |> string)
  |> System.Windows.Forms.Clipboard.SetText

let input = "@yfe1ihmjbm@4shk$@jn8i5udt@w3ytg9$adra293ll@#1$boxeust8fe56ra41fu$ckfo9cuic0nk9c2xp2qmt$8d#bksgtuvss4tmw8bs91axn7gbyuz1kwewon0matt?rnk0q1jl3ml7tcya0pmir13ekm@z@v#uonxfo?vxa6d@#eg09rqlkqc3vwb#jcshizhh5w83jc9czsljzji46dapl$h8g#rny1lwcddh8m07xt8s$xngvzg2rwnedz1xxa2rey4t@4fvj0m?#l1rkf1@ox#2rsbpt7hdftltp9om26kd@xb@tvet25$j1f5dx3pfur$e3asyzv0tm9rseu2iogusr0z#hjyui35i4oeu?1qu8nio@tmgv7qigar5j#1mx707mxobh23i7wp#fn77f#bry$@#20h4w31bdnhs5woyupo3h@8gd8ukoqjnn41q9ekjy07fp4v761wqkck7ex#$q$gm82o#tmh3c7#1ju@j9p2t3m#qp5o4hnmy5enye48om?z5#hfhbc05yugmx57j3n3$gi?0rebwxdehtc$e431ldvy?v1xtej3mxxploweuyprfwnn0@#6l?tyss2m@8ge9rgq@#o0g5@s@ox7cdz181tcq4?f#i"

input |> generateAnswer
val isElem : x:seq<'a> -> e:'a -> bool (requires equality)

Full name: Script.isElem
val x : seq<'a> (requires equality)
val e : 'a (requires equality)
module Seq

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

Full name: Microsoft.FSharp.Collections.Seq.exists
val isPrime : (int -> bool)

Full name: Script.isPrime
val sumOf : f:(int -> bool) -> (seq<int> -> int)

Full name: Script.sumOf
val f : (int -> bool)
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<_>
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<_>
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.sum
val getSumProduct : xs:seq<char> -> int

Full name: Script.getSumProduct
val xs : seq<char>
val extractNumbers : (seq<char> -> seq<int>)
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
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 intxs : seq<int>
val primes : int
val compos : int
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val toString : (seq<char> -> string)

Full name: Script.toString
Multiple items
val char : value:'T -> char (requires member op_Explicit)

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

--------------------
type char = System.Char

Full name: Microsoft.FSharp.Core.char
module String

from Microsoft.FSharp.Core
val concat : sep:string -> strings:seq<string> -> string

Full name: Microsoft.FSharp.Core.String.concat
val incrChar : x:char -> char

Full name: Script.incrChar
val x : char
val take25NonIntsIncrByOne : xs:seq<char> -> string

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

Full name: Microsoft.FSharp.Collections.Seq.take
val generateAnswer : input:seq<char> -> unit

Full name: Script.generateAnswer
val input : seq<char>
namespace System
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
System.Windows.Forms.Clipboard.SetText(text: string) : unit
System.Windows.Forms.Clipboard.SetText(text: string, format: System.Windows.Forms.TextDataFormat) : unit
val input : string

Full name: Script.input

More information

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