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 findLongestWord(word: String) -> String { | |
let wordArray = word.components(separatedBy: " ") | |
var maxLength = 0 | |
for index in 0..<(wordArray.count) { | |
if wordArray[index].count > maxLength { | |
maxLength = wordArray[index].count | |
} | |
} | |
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
// | |
// Compare elements in one array | |
// | |
// Check if all elements in array are the same | |
let input = "aaaa" | |
let areSame = input.allSatisfy { $0 == input.last } |
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
// | |
// Restrict certain characters | |
// | |
// Make certain characters impossible to use | |
// | |
import Foundation | |
let input = "abcd@e" |
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
// | |
// Remove certain characters | |
// | |
let input = "1 234 567".filter {!$0.isWhitespace} | |
let input2 = "aAbBcCdD".filter {!$0.isLowercase} | |
let input3 = "!@A#$BC%DE^FG&HI".filter {!$0.isLetter} |
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
// | |
// Convert Int array to String array and vice versa | |
// | |
// Int array -> String array | |
let intArray = [1, 2, 3, 4, 5] | |
let stringArray = intArray.map { String($0) } |
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
// | |
// Convert String array into String and vice versa | |
// | |
// Also convert Int array into Int and vice versa | |
// | |
import Foundation | |
// String array -> String | |
let stringArray = ["Kevin", "Bob", "Steve"] |
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
// | |
// Find random element in array | |
// | |
let array = "abcdefghijklmnopqrstuvwxyz" | |
let random = (0...3).compactMap { _ in array.randomElement() } // (0...3) - it means amount of characters | |
print(random) // ["f", "u", "y", "m"] |
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
// | |
// Rounding numbers | |
// | |
import Foundation | |
let number = 15.4444444445 | |
let round = String(format: "%.2f", number) // 2f stands for two decimal places |
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
// | |
// Error handling | |
// | |
// Create error | |
enum InputError: Error { | |
case EmptyName | |
} | |
// Create function |
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
// | |
// Check the current date | |
// | |
import Foundation | |
// Create current date | |
var currentDate = Date() | |
// Create a DateFormatter object |
OlderNewer