Created
January 13, 2022 04:40
-
-
Save Schockarum/7d49a51513a181921ca2174a9fe5866c to your computer and use it in GitHub Desktop.
HackerRank [Swift] 1 Month Preparation Kit - Week 2
This file contains hidden or 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 Foundation | |
func sockMerchant(n: Int, ar: [Int]) -> Int { | |
var sockBag: Set<Int> = [] | |
var totalPairs: Int = 0 | |
for sock in ar{ | |
if sockBag.contains(sock){ | |
sockBag.remove(sock) | |
totalPairs += 1 | |
continue | |
} | |
sockBag.insert(sock) | |
} | |
return totalPairs | |
} |
This file contains hidden or 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 UIKit | |
func findZigZagSequence(arr: [Int], n: Int) -> [Int]{ | |
var sortedArray: [Int] = arr | |
sortedArray.sort() | |
let mid: Int = ((n+1)/2)-1 | |
var start: Int = mid + 1 | |
var end: Int = n - 2 | |
var holder: Int = 0 | |
holder = sortedArray[mid] | |
sortedArray[mid] = sortedArray[n-1] | |
sortedArray[n-1] = holder | |
while start <= end{ | |
holder = sortedArray[start] | |
sortedArray[start] = sortedArray[end] | |
sortedArray[end] = holder | |
start += 1 | |
end -= 1 | |
} | |
print(sortedArray) | |
return sortedArray | |
} |
This file contains hidden or 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 Foundation | |
// V 2.0 | |
struct Book{ | |
var normalBook: [[Int]] = [] | |
var reversedBook: [[Int]] { | |
get { | |
return normalBook.reversed() | |
} | |
set(newBook) { | |
normalBook = newBook | |
} | |
} | |
init(numberOfPages pages: Int) { | |
var i: Int = 0 | |
while i <= pages{ | |
normalBook += [[i,i+1]] | |
i += 2; | |
} | |
} | |
} | |
func pageCount(n: Int, p: Int) -> Int { | |
let book: Book = Book(numberOfPages: n) | |
return minPageFlipsToOpenA(Book: book, OnPage: p) | |
} | |
func minPageFlipsToOpenA(Book book: Book, OnPage page: Int) -> Int{ | |
var pageFlips: Int = 0 | |
for pages in book.normalBook{ | |
if pages.contains(page){ | |
pageFlips = book.normalBook.firstIndex(of: pages)! | |
break | |
} | |
} | |
for pages in book.reversedBook{ | |
if pages.contains(page){ | |
pageFlips = pageFlips < book.reversedBook.firstIndex(of: pages)! ? pageFlips : book.reversedBook.firstIndex(of: pages)! | |
break | |
} | |
} | |
return pageFlips | |
} | |
pageCount(n:5, p:4) | |
// V 1.0 | |
func originalPageCount(n: Int, p: Int) -> Int { | |
var book: [[Int]] = [] | |
var i: Int = 0 | |
var pageFlips: Int = 0 | |
var reverseFlips: Int = 0 | |
// Inicializamos el libro | |
while i <= n{ | |
book += [[i,i+1]] | |
i += 2; | |
} | |
for pages in book{ | |
if pages.contains(p){ | |
pageFlips = book.firstIndex(of: pages)! | |
break | |
} | |
} | |
book.reverse() | |
for pages in book{ | |
if pages.contains(p){ | |
reverseFlips = book.firstIndex(of: pages)! | |
break | |
} | |
} | |
return (pageFlips < reverseFlips ? pageFlips : reverseFlips) | |
} |
This file contains hidden or 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 Foundation | |
func towerBreakers(n:Int, m:Int) -> Int{ | |
var winner: Int = 0 | |
let evenTowers: Bool = n.isMultiple(of: 2) | |
switch evenTowers{ | |
case true: | |
winner = 2 | |
break | |
case false: | |
winner = m > 1 ? 1 : 2 | |
break | |
} | |
return winner | |
} |
This file contains hidden or 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 Foundation | |
//I'm sure there's a fancier way to do this... | |
func caesarCipher(s: String, k: Int) -> String { | |
var cipheredText : String = "" | |
let alphaList = Array("abcdefghijklmnopqrstuvwxyz") | |
var cipheredLetter : Character | |
var cipherIdx : Int? = 0 | |
var isUpperCase : Bool = false | |
var shiftLetter : Character = Character("a") | |
for letter in s{ | |
isUpperCase = letter.isUppercase | |
shiftLetter = isUpperCase ? Character(letter.lowercased()) : letter | |
cipherIdx = alphaList.firstIndex(of: shiftLetter) | |
if cipherIdx != nil{ | |
cipheredLetter = isUpperCase ? Character(alphaList[(cipherIdx!+k)%alphaList.count-1].uppercased()) : alphaList[(cipherIdx!+k)%alphaList.count-1] | |
cipheredText.append(cipheredLetter) | |
continue | |
} | |
cipheredText.append(letter) | |
} | |
return cipheredText | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment