I was exploring Jupyter notebooks , that combines live code, markdown and data, through Microsoft's implementation, known as MS Azure Notebooks , putting together a small library of R and F# notebooks . As Microsoft's FAQ for the service describes it as : ...a multi-lingual REPL on steroids. This is a free service that provides Jupyter notebooks along with supporting packages for R, Python and F# as a service. This means you can just login and get going since no installation/setup is necessary. Typical usage includes schools/instruction, giving webinars, learning languages, sharing ideas, etc. Feel free to clone and comment... In R Azure Workbook for R - Memoisation and Vectorization Charting Correlation Matrices in R In F# Charnownes Constant in FSharp.ipynb Project Euler - Problems 18 and 67 - FSharp using Dynamic Programming
Description
open System.IO
let alphaArrayValue (ch:char) =
let alphas = [|' ';'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q';'R';'S';'T';'U';'V';'W';'X';'Y';'Z';|]
let sumEachItem (item:string) =
let newArray =
let fileAsString = File.ReadAllText(@"C:\Documents and Settings\...\names.txt")
- Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order.
- Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
- For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list.
- So, COLIN would obtain a score of 938 53 = 49714.
- What is the total of all the name scores in the file?
Solution
let alphas = [|' ';'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q';'R';'S';'T';'U';'V';'W';'X';'Y';'Z';|]
let value = alphas |> Array.findIndex (fun x -> ch = x)
value
let sumEachItem (item:string) =
let charArray = item.ToCharArray()
let value = charArray |> Array.map (fun x -> alphaArrayValue x)
Array.sum value
let start_22 = System.DateTime.UtcNow
let newArray =
let fileAsString = File.ReadAllText(@"C:\Documents and Settings\...\names.txt")
let arr = fileAsString.Replace("\"","").Split(',')
let summedItems = (Array.sort arr) |> Array.map (fun x -> sumEachItem x)
let results = Array.create (Array.length summedItems) 0
let result =
for i=0 to ((Array.length summedItems)-1) do
results.[i] <- summedItems.[i] * (i+1)
Array.sum results
printfn "%f" ((System.DateTime.UtcNow).Subtract(start_22).TotalSeconds)
Comments
Post a Comment