Skip to content

Instantly share code, notes, and snippets.

@chadparker
Last active February 11, 2020 04:21
Show Gist options
  • Select an option

  • Save chadparker/202e06bbdc92ef2d3d58751166feeef3 to your computer and use it in GitHub Desktop.

Select an option

Save chadparker/202e06bbdc92ef2d3d58751166feeef3 to your computer and use it in GitHub Desktop.

Revisions

  1. chadparker revised this gist Feb 11, 2020. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions palindrome.swift
    Original file line number Diff line number Diff line change
    @@ -39,3 +39,16 @@ canBePalindrome("A man, a plan, a can ... na canal panama.")
    //} else {
    // //odd
    //}


    // trying my own string reversing function:

    func reversedString(_ string: String) -> String {
    var reversedString = ""
    for character in string {
    reversedString.insert(character, at: reversedString.startIndex)
    }
    return reversedString
    }

    reversedString("reversed string")
  2. chadparker revised this gist Feb 11, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions palindrome.swift
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,7 @@ canBePalindrome("a") // should return true
    canBePalindrome("aa") // should return true
    canBePalindrome("hannah") // should return true
    canBePalindrome("abc") // should return false
    canBePalindrome("Racecar")
    canBePalindrome("A man, a plan, a can ... na canal panama.")


  3. chadparker revised this gist Feb 11, 2020. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions palindrome.swift
    Original file line number Diff line number Diff line change
    @@ -5,12 +5,14 @@ import Foundation
    // check from each end of the string
    // deal with even or odd amount of characters

    // amanaplan

    func canBePalindrome(_ string: String) -> Bool {

    // prepare the string
    let preparedString = string.lowercased()
    var preparedString = string.lowercased()
    // remove spaces, common punctuation
    preparedString = preparedString.filter { $0 != " " }
    preparedString = preparedString.filter { $0 != "." }
    preparedString = preparedString.filter { $0 != "," }
    print(preparedString)

    return preparedString == String(preparedString.reversed())
    }
    @@ -23,7 +25,10 @@ canBePalindrome("a") // should return true
    canBePalindrome("aa") // should return true
    canBePalindrome("hannah") // should return true
    canBePalindrome("abc") // should return false
    canBePalindrome("A man, a plan, a can ... na canal panama.")


    // I was starting with something like this, until I realized I could just reverse the whole string:

    //let stringBeginning = ""
    //let stringEnd = ""
  4. chadparker created this gist Feb 11, 2020.
    35 changes: 35 additions & 0 deletions palindrome.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import Foundation

    // Requirements:
    // ignore case, whitespace, and punctuation
    // check from each end of the string
    // deal with even or odd amount of characters

    // amanaplan

    func canBePalindrome(_ string: String) -> Bool {

    // prepare the string
    let preparedString = string.lowercased()

    return preparedString == String(preparedString.reversed())
    }


    // Test Cases
    canBePalindrome("tacocat") // should return true
    canBePalindrome("daily") // should return false
    canBePalindrome("a") // should return true
    canBePalindrome("aa") // should return true
    canBePalindrome("hannah") // should return true
    canBePalindrome("abc") // should return false


    //let stringBeginning = ""
    //let stringEnd = ""
    //if preparedString.count % 2 == 0 {
    // //even
    // for letter in
    //} else {
    // //odd
    //}