Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto #9, #14 and 15 - Swift #6400

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation

func octal(num: Int) -> Int {
func convert(num: Int, rest: [Int] = []) -> [Int] {
guard num != 0 else { return rest }

return convert(num: num / 8, rest: [num % 8] + rest)
}

let rest = convert(num: num)
return rest.reduce(0) {
$0 * 10 + $1
}
}

func hexadecimal(num: Int) -> String {
let hexValues = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
func convert(num: Int, hex:[String] = []) -> [String] {
guard num > 15 else { return [hexValues[num]] + hex }

return convert(num: num / 16, hex: [hexValues[num % 16]] + hex)
}

let hex = convert(num: num)
return hex.reduce("") {
"\($0)\($1)"
}
}


print(octal(num: 456))
print(hexadecimal(num: 456))
46 changes: 46 additions & 0 deletions Retos/Reto #15 - AUREBESH [Fácil]/swift/jcalderita.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Foundation

let latin = [
"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 aurebesh = [
"Aurek", "Besh", "Cresh", "Dorn", "Enth",
"Forn", "Grek", "Herf", "Isk", "Jenth",
"Krill", "Leth", "Mern", "Nern", "Osk",
"Peth", "Qek", "Resh", "Senth", "Trill",
"Usk", "Vev", "Wesk", "Xesh", "Yirt", "Zerek"
]

func translate(sentence: String, latinToAurebesh: Bool) -> String {
if latinToAurebesh {
return sentence.uppercased().map { char in
guard let idx = latin.firstIndex(of: String(char)) else { return String(char) }

return aurebesh[idx]
}.joined()
} else {
let separatedAurebesh = sentence.reduce(into: [String]()) { (result, character) in
if character.isUppercase || character.isWhitespace || character.isNumber {
result.append(String(character))
} else {
result[result.endIndex-1].append(character)
}
}

return separatedAurebesh.map { word in
guard let idx = aurebesh.firstIndex(of: word) else { return word }

return latin[idx]
}.joined()
}
}

let translateAurebesh = translate(sentence: "May the Force be with you", latinToAurebesh: true)
print(translateAurebesh)
let translateLatin = translate(sentence: translateAurebesh, latinToAurebesh: false)
print(translateLatin)
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import Foundation

let characters = Set("abcdefghijklmnopqrstuvwxyz")

func transforSentence(_ sentence: String) -> String {
func transformSentence(_ sentence: String) -> String {
sentence.trimmingCharacters(in: .whitespaces).lowercased()
}

func isHeterogram(_ sentence: String) -> Bool {
let compare = transforSentence(sentence)
let compare = transformSentence(sentence)
return compare.count == Set(compare).count
}

func isIsogram(_ sentence: String) -> Bool {
let compare = transforSentence(sentence)
let compare = transformSentence(sentence)
return compare.count != Set(compare).count
}

func isPangram(_ sentence: String) -> Bool {
let compare = transforSentence(sentence)
let compare = transformSentence(sentence)
return characters.subtracting(compare).isEmpty
}

Expand Down