Skip to content

Instantly share code, notes, and snippets.

@ethanmpeterson
Created November 15, 2016 18:49
Show Gist options
  • Select an option

  • Save ethanmpeterson/ce7761e57b5002427f3a464e0015b348 to your computer and use it in GitHub Desktop.

Select an option

Save ethanmpeterson/ce7761e57b5002427f3a464e0015b348 to your computer and use it in GitHub Desktop.
import Foundation
/*
MAKING A PLAN
A common problem solving model is known as input-process-output.
1. What are the possible inputs I can expect to receive?
2. How should each type of input be processed?
3. What should the output be in each case?
It can be helpful to organize this into a table. Here's an example:
http://russellgordon.ca/rsgc/2016-17/ics3u/IPO-example.jpg
*/
/*
IMPLEMENT THE PLAN
A good way to orgaize your code is to separate your code into the three sections - input, process, output – as much as possible.
The start of a solution is implemented below. Consider all the possible inputs. Can you finish the solution?
*/
/*
INPUT
Be sure that your implementation of this section discards all the possible bad inputs the user could provide.
*/
var inputToProcess : String = ""
// Loop until valid input is received
while inputToProcess == "" {
// Show the prompt
print("STRING: ", terminator: "")
// Get the user's input
var input : String?
input = readLine()
// Use optional binding to see if the string can be unwrapped (to see if it is not nil)
if let notNilInput = input {
if let inputAsString = String(notNilInput) {
if (inputAsString.characters.count > 1 && inputAsString.characters.count <= 30) {
inputToProcess = inputAsString
}
}
}
}
/*
PROCESS
Here is where you implement the logic that solves the problem at hand.
(e.g.: in past exercises, verify that a number is prime, for example).
Of course, what goes here in your code varies, depending on the problem you are solving.
*/
// Add 'process' code below....
let alphabet : [Character] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
let consonant : [Character] = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"]
let vowels : [Character] = ["a", "e", "i", "o", "u"]
var output : String = ""
func isCons(letter : Character) -> Bool {
if (consonant.contains(letter)) {
return true
} else {
return false
}
}
func nextCons(letter : Character) -> Character {
if (letter == "z" || letter == "y") {
return "z"
} else {
for i in stride(from: alphabet.index(of: letter)! + 1, to: alphabet.count - 1, by: 1) {
if (consonant.contains(alphabet[i])) {
return alphabet[i]
}
}
}
return "!"
}
func nextVowel(letter : Character) -> Character {
var leftVowel : Character = "!"
var rightVowel : Character = "!"
var leftDist = 0
var rightDist = 0
var nextVowel : Character = "!"
for i in stride(from: alphabet.index(of: letter)! + 1, to: alphabet.count, by: 1) {
rightDist += 1
if (vowels.contains(alphabet[i])) {
rightVowel = alphabet[i]
break
}
}
for j in stride(from: alphabet.index(of: letter)! - 1, to: -1, by: -1) {
leftDist += 1
if (vowels.contains(alphabet[j])) {
leftVowel = alphabet[j]
break
}
}
if (rightDist == leftDist) {
if (leftVowel != "!") {
nextVowel = leftVowel
} else {
nextVowel = rightVowel
}
} else if (leftDist < rightDist) {
if (leftVowel != "!") {
nextVowel = leftVowel
} else {
nextVowel = rightVowel
}
} else if (rightDist < leftDist) {
if (rightVowel != "!") {
nextVowel = rightVowel
} else {
nextVowel = leftVowel
}
}
return nextVowel
}
for char in inputToProcess.characters {
if (isCons(letter: char)) {
output.append(char)
output.append(nextVowel(letter: char))
output.append(nextCons(letter: char))
} else {
output.append(char)
}
}
/*
OUTPUT
Here is where you report the results of the 'process' section above.
*/
// Add 'output' code below... replace what is here as needed.
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment