Skip to main content

Microsoft Azure Notebooks - Live code - F#, R, and Python

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

Project Euler - Problem 17



Problem


If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.


If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?


NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.


Solution


//this is a very ugly solution, which I will return to to properly factor to smaller recursive methods




let GetThousandsLetters num = 
    match num with
        | 1 -> "one thousand"
        | 2 -> "two thousand"
        | 3 -> "three thousand"
        | 4 -> "four thousand"
        | 5 -> "five thousand"
        | 6 -> "six thousand"
        | 7 -> "seven thousand"
        | 8 -> "eight thousand"
        | 9 -> "nine thousand"


let GetHundredsLetters num = 
    match num with
        | 1 -> "one hundred"
        | 2 -> "two hundred"
        | 3 -> "three hundred"
        | 4 -> "four hundred"
        | 5 -> "five hundred"
        | 6 -> "six hundred"
        | 7 -> "seven hundred"
        | 8 -> "eight hundred"
        | 9 -> "nine hundred"


let GetTeensLetters num = 
    match num with
        | 1 -> "eleven"
        | 2 -> "twelve"
        | 3 -> "thirteen"
        | 4 -> "fourteen"
        | 5 -> "fifteen"
        | 6 -> "sixteen"
        | 7 -> "seventeen"
        | 8 -> "eighteen"
        | 9 -> "nineteen"


let GetTensLetters num = 
    match num with
        | 1 -> "ten"
        | 2 -> "twenty"
        | 3 -> "thirty"
        | 4 -> "forty"
        | 5 -> "fifty"
        | 6 -> "sixty"
        | 7 -> "seventy"
        | 8 -> "eighty"
        | 9 -> "ninety"


let GetSecondDigitLetters num = 
    match num with
        | 1 -> "ten"
        | 2 -> "twenty"
        | 3 -> "thirty"
        | 4 -> "forty"
        | 5 -> "fifty"
        | 6 -> "sixty"
        | 7 -> "seventy"
        | 8 -> "eighty"
        | 9 -> "ninety"


let GetFirstDigitLetters num = 
    match num with
        | 1 -> "one"
        | 2 -> "two"
        | 3 -> "three"
        | 4 -> "four"
        | 5 -> "five"
        | 6 -> "six"
        | 7 -> "seven"
        | 8 -> "eight"
        | 9 -> "nine"





let GetNumber num =
    let numAsString = num.ToString()
    let numCharArray = numAsString.ToCharArray()
    let numIntArray = numCharArray |> Array.map (fun x -> System.Int32.Parse(x.ToString()))
    let ln = numIntArray.Length
    if ln = 4 then


        let fourth = GetThousandsLetters numIntArray.[ln-4]
        fourth
             
    elif ln = 3 then


        let third = GetHundredsLetters numIntArray.[ln-3]
        
        if numIntArray.[ln-2] = 0 then
            if numIntArray.[ln-1] = 0 then
                third
            else
                let first = GetFirstDigitLetters numIntArray.[ln-1]
                third + " and " + first
        elif numIntArray.[ln-2] = 1 then
            if numIntArray.[ln-1] = 0 then
                let second = GetTensLetters numIntArray.[ln-2]
                third + " and " + second
            else
                let second = GetTeensLetters numIntArray.[ln-1]
                third + " and " + second
        else
            if numIntArray.[ln-1] = 0 then
                let second = GetTensLetters numIntArray.[ln-2]
                third + " and " + second
            else
                let first = GetFirstDigitLetters numIntArray.[ln-1]    
                let second = GetSecondDigitLetters numIntArray.[ln-2]
                third + " and " + second + "-" + first
            
    elif ln = 2 then


        if numAsString.EndsWith("0") then
            let second = GetTensLetters numIntArray.[ln-2]
            second
        elif numIntArray.[ln-2] = 1 then
            let second = GetTeensLetters numIntArray.[ln-1]
            second
        else
            let first = GetFirstDigitLetters numIntArray.[ln-1]    
            let second = GetSecondDigitLetters numIntArray.[ln-2]
            second + "-" + first
            
    elif ln = 1 then


        let first = GetFirstDigitLetters numIntArray.[ln-1]    
        first
    
    else
        //nothing
        ""


let testNumber = 
    let arrWords = [1..1000] |> List.map (fun x -> GetNumber x)
    let arrWordsMinusSpaces = arrWords |> List.map (fun x -> x.Replace(" ", ""))
    let arrWordsMinusHyphen = arrWordsMinusSpaces |> List.map (fun x -> x.Replace("-", ""))
    let arrLengths = arrWordsMinusHyphen |> List.map (fun x-> x.Length)
    List.sum arrLengths

let GetNumber num =
    let numAsString = num.ToString()
    let numCharArray = numAsString.ToCharArray()
    let numIntArray = numCharArray |> Array.map (fun x -> System.Int32.Parse(x.ToString()))
    let ln = numIntArray.Length
    if ln = 4 then

        let fourth = GetThousandsLetters numIntArray.[ln-4]
        fourth
             
    elif ln = 3 then

        let third = GetHundredsLetters numIntArray.[ln-3]
        
        if numIntArray.[ln-2] = 0 then
            if numIntArray.[ln-1] = 0 then
                third
            else
                let first = GetFirstDigitLetters numIntArray.[ln-1]
                third + " and " + first
        elif numIntArray.[ln-2] = 1 then
            if numIntArray.[ln-1] = 0 then
                let second = GetTensLetters numIntArray.[ln-2]
                third + " and " + second
            else
                let second = GetTeensLetters numIntArray.[ln-1]
                third + " and " + second
        else
            if numIntArray.[ln-1] = 0 then
                let second = GetTensLetters numIntArray.[ln-2]
                third + " and " + second
            else
                let first = GetFirstDigitLetters numIntArray.[ln-1]    
                let second = GetSecondDigitLetters numIntArray.[ln-2]
                third + " and " + second + "-" + first
            
    elif ln = 2 then

        if numAsString.EndsWith("0") then
            let second = GetTensLetters numIntArray.[ln-2]
            second
        elif numIntArray.[ln-2] = 1 then
            let second = GetTeensLetters numIntArray.[ln-1]
            second
        else
            let first = GetFirstDigitLetters numIntArray.[ln-1]    
            let second = GetSecondDigitLetters numIntArray.[ln-2]
            second + "-" + first
            
    elif ln = 1 then

        let first = GetFirstDigitLetters numIntArray.[ln-1]    
        first
    
    else
        //nothing
        ""

let testNumber = 
    let arrWords = [1..1000] |> List.map (fun x -> GetNumber x)
    let arrWordsMinusSpaces = arrWords |> List.map (fun x -> x.Replace(" ", ""))
    let arrWordsMinusHyphen = arrWordsMinusSpaces |> List.map (fun x -> x.Replace("-", ""))
    let arrLengths = arrWordsMinusHyphen |> List.map (fun x-> x.Length)
    List.sum arrLengths

Comments

Popular posts from this blog

Microsoft Azure Notebooks - Live code - F#, R, and Python

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

Digit cancelling fractions (Project Euler Problem 33)

Problem The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s. We shall consider fractions like, 30/50 = 3/5, to be trivial examples. There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator. If the product of these four fractions is given in its lowest common terms, find the value of the denominator. (link to Problem 33 on the Project Euler site) Note This is a somewhat crude solution, since I am only just getting back into solving these problems, or working with F#, but there are several similar problems for which I can develop properly factored, reusable functions. Solution open System let product xs ys = seq{for x in xs do for y in ys do let a = float x % float 10 ...

Consecutive Prime Sum (Project Euler - Problem 50)

Problem The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953. Which prime, below one-million, can be written as the sum of the most consecutive primes? Note Some libraries used in this code are F# modules I use, but have also published as a  Nuget library , such as EulerLib.GetPrimes() and  EulerLib. isPrime(). You need to reference the NuGetLibrary to use this code as is. Solution #load "Stat.fs" #load "Print.fs" #load "EulerLib.fs" open Stat open Print open EulerLib open System let rec FindLongestPrimeSequenceSum (primeList:list ) (nextItem:int) lessThanValue (primeArray:list ) bestPrime (correctArray:list )...