Skip to main content
edited body
Source Link
codez
  • 969
  • 1
  • 9
  • 29

\$21124\$ (Correct according to Project Euler)

\$21124\$ (Correct according to Project Euler)

Time: \$0.0172510147094727\$ seconds

Time: \$0.0172510147094727\$ seconds

\$21124\$ (Correct according to Project Euler)

Time: \$0.0172510147094727\$ seconds

\$21124\$ (Correct according to Project Euler)

Time: \$0.0172510147094727\$ seconds

Added spoilers for question, fixed a bit of wording, added project-Euler tag, added relevant link.
Source Link

Problem:Number letter counts, Problem 17:

NOTE: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.

Result: Result

\$21124\$ (Correct according to Project Euler)

Time: 21124 found in 0.0172510147094727 seconds (Correct according to Project Euler).\$0.0172510147094727\$ seconds

Since this hasn't been solved in Swift, I'd like a code review of this challenge which talks about the efficiency of the code, and how to optimise efficient,correct bad practices or violations of any concepts. Here is the code:

Problem:

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.

Result: Result: 21124 found in 0.0172510147094727 seconds (Correct according to Project Euler).

Since this hasn't been solved in Swift, I'd like a code review of this challenge which talks about the efficiency of the code, and how to optimise efficient, bad practices or violations of any concepts. Here is the code:

Number letter counts, Problem 17:

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.

Result:

\$21124\$ (Correct according to Project Euler)

Time: \$0.0172510147094727\$ seconds

Since this hasn't been solved in Swift, I'd like a code review of this challenge which talks about the efficiency of the code, and how to correct bad practices or violations of any concepts. Here is the code:

added 280 characters in body
Source Link
codez
  • 969
  • 1
  • 9
  • 29

SolutionResult: Result: 21124 found in 0.0172510147094727 seconds (Correct according to Project Euler).

import Foundation

let unitNames:[String] = [
    "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
]

let tensNames:[String] = [
    "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"
]

func convertIntegerToWords(number:Int) -> String {
    var words:String = ""
    
    if number >= 0 && number <= 19 {
        
        words = words + unitNames[number]
        
    } else if number >= 20 && number <= 99  {
        
        var digits = number.array
        
        words = words + tensNames[digits[0]]
        words = words + unitNames[digits[1]]
        
    } else if number >= 100 && number <= 999 {
        
        var digits = number.array
        
        if number % 100 == 0 {
            words = words + unitNames[digits[0]] + "hundred";
        } else {
            words = words + unitNames[digits[0]] + "hundredand";
            var newNumber = number - digits[0] * 100;
            words = words + convertIntegerToWords(newNumber)
        }
        
    } else if number == 1000 {
        
        words = words + "onethousand"
        
    }
    
    return words
}

extension Int {
    var array: [Int] {
        return Array(description).map{String($0).toInt() ?? 0}
    }
}

func eulerSeventeenEulerSeventeen() -> Int {
    
    var length:Int = 0
    
    for iterator in 1...1000 {
        length = length + count(convertIntegerToWords(iterator))
    }
    let end = NSDate();
    
    return length
}

let start = NSDate();
var result = EulerSeventeen()
let end = NSDate();

let timeInterval: Double = end.timeIntervalSinceDate(start);

println(eulerSeventeen"Result: \(result) found in \(timeInterval) seconds")

Solution: 21124 (Correct according to Project Euler).

import Foundation

let unitNames:[String] = [
    "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
]

let tensNames:[String] = [
    "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"
]

func convertIntegerToWords(number:Int) -> String {
    var words:String = ""
    
    if number >= 0 && number <= 19 {
        
        words = words + unitNames[number]
        
    } else if number >= 20 && number <= 99  {
        
        var digits = number.array
        
        words = words + tensNames[digits[0]]
        words = words + unitNames[digits[1]]
        
    } else if number >= 100 && number <= 999 {
        
        var digits = number.array
        
        if number % 100 == 0 {
            words = words + unitNames[digits[0]] + "hundred";
        } else {
            words = words + unitNames[digits[0]] + "hundredand";
            var newNumber = number - digits[0] * 100;
            words = words + convertIntegerToWords(newNumber)
        }
        
    } else if number == 1000 {
        
        words = words + "onethousand"
        
    }
    
    return words
}

extension Int {
    var array: [Int] {
        return Array(description).map{String($0).toInt() ?? 0}
    }
}

func eulerSeventeen() -> Int {
    var length:Int = 0
    
    for iterator in 1...1000 {
        length = length + count(convertIntegerToWords(iterator))
    }
    
    return length
}

println(eulerSeventeen())

Result: Result: 21124 found in 0.0172510147094727 seconds (Correct according to Project Euler).

import Foundation

let unitNames:[String] = [
    "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
]

let tensNames:[String] = [
    "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"
]

func convertIntegerToWords(number:Int) -> String {
    var words:String = ""
    
    if number >= 0 && number <= 19 {
        
        words = words + unitNames[number]
        
    } else if number >= 20 && number <= 99  {
        
        var digits = number.array
        
        words = words + tensNames[digits[0]]
        words = words + unitNames[digits[1]]
        
    } else if number >= 100 && number <= 999 {
        
        var digits = number.array
        
        if number % 100 == 0 {
            words = words + unitNames[digits[0]] + "hundred";
        } else {
            words = words + unitNames[digits[0]] + "hundredand";
            var newNumber = number - digits[0] * 100;
            words = words + convertIntegerToWords(newNumber)
        }
        
    } else if number == 1000 {
        
        words = words + "onethousand"
        
    }
    
    return words
}

extension Int {
    var array: [Int] {
        return Array(description).map{String($0).toInt() ?? 0}
    }
}

func EulerSeventeen() -> Int {
    
    var length:Int = 0
    
    for iterator in 1...1000 {
        length = length + count(convertIntegerToWords(iterator))
    }
    let end = NSDate();
    
    return length
}

let start = NSDate();
var result = EulerSeventeen()
let end = NSDate();

let timeInterval: Double = end.timeIntervalSinceDate(start);

println("Result: \(result) found in \(timeInterval) seconds")
Source Link
codez
  • 969
  • 1
  • 9
  • 29
Loading