Skip to content

Instantly share code, notes, and snippets.

@vishw33
Last active April 10, 2018 09:09
Show Gist options
  • Select an option

  • Save vishw33/51ea1fe9d82735330fe36886642804bd to your computer and use it in GitHub Desktop.

Select an option

Save vishw33/51ea1fe9d82735330fe36886642804bd to your computer and use it in GitHub Desktop.

Revisions

  1. vishw33 revised this gist Apr 10, 2018. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions StringArray_full.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,23 @@
    extension Array where Element == String
    {
    func totalCharacterCount() -> Int
    {
    return reduce(0, { $0 + $1.count})
    }

    func wordCount() -> Int
    {
    return reduce(0, {$0 + $1.components(separatedBy: " ").count})
    }

    func elementCharacterCount() -> [Int]
    {
    var countArray:[Int] = [Int]()
    countArray = map{$0.count}
    return countArray
    }
    }

    let arrayString:[String] = ["The Godfather" , "The Shawshank Redemption" , "Schindler's List" , "Raging Bull" , "Gone with the Wind" , "The Wizard of Oz" , "Lawrence of Arabia" , " Forrest Gump"]

    let characterCount = arrayString.totalCharacterCount()
  2. vishw33 created this gist Apr 10, 2018.
    5 changes: 5 additions & 0 deletions StringArray_full.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    let arrayString:[String] = ["The Godfather" , "The Shawshank Redemption" , "Schindler's List" , "Raging Bull" , "Gone with the Wind" , "The Wizard of Oz" , "Lawrence of Arabia" , " Forrest Gump"]

    let characterCount = arrayString.totalCharacterCount()
    let wordCount = arrayString.wordCount()
    let eachCharacterCount = arrayString.elementCharacterCount()