Swift implementation of five string operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func cleanUp(s: String) -> String { | |
return splitIntoSentences(summary: fixHyphenation(s: fixWhitespace(s: s)))[0] | |
} | |
func fixHyphenation(s: String) -> String { | |
return s.replacingOccurrences(of: "- ", with: "") | |
} | |
func fixWhitespace(s: String) -> String { | |
return s.replacingOccurrences(of: "\n", with: " ") | |
} | |
func splitIntoSentences(summary: String) -> [String] { | |
return summary | |
.components(separatedBy: ". ") | |
.map { ensureEndsWithPeriod(sentence: $0) } | |
} | |
func ensureEndsWithPeriod(sentence: String) -> String { | |
if let lastCharacter = sentence.characters.last { | |
if lastCharacter == "." { | |
return sentence | |
} else { | |
return sentence + "." | |
} | |
} else { | |
return sentence | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
class Tests: XCTestCase { | |
func testFixHyphenation() { | |
XCTAssertEqual( fixHyphenation(s: "auto- mobile"), "automobile" ) | |
} | |
func testFixWhitespace() { | |
XCTAssertEqual( fixWhitespace(s: "and\nthe story"), "and the story" ) | |
} | |
func testCleanUp() { | |
let input = "Relating to the state transient lodging tax; creating\nnew provisions; amending ORS 284.131 and\n320.305; prescribing an effective date; and pro-\nviding for revenue raising that requires approval\nby a three-fifths majority.\nWhereas Enrolled House Bill 2267 (chapter 818," | |
let expected_output = "Relating to the state transient lodging tax; creating new provisions; amending ORS 284.131 and 320.305; prescribing an effective date; and providing for revenue raising that requires approval by a three-fifths majority." | |
XCTAssertEqual( cleanUp(s: input), expected_output ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment