├── Algorithms ├── Bit Manipulation │ ├── Lonely Integer │ │ ├── LonelyInteger.swift │ │ └── readme.md │ └── readme.md ├── Implementation │ ├── Apple and Orange │ │ ├── AppleAndOrange.swift │ │ └── readme.md │ ├── Bon Appetit │ │ ├── BonAppetit.swift │ │ └── readme.md │ ├── Breaking the Records │ │ ├── BreakingTheRecords.swift │ │ └── readme.md │ ├── Divisible Sum Pairs │ │ ├── DivisibleSumPairs.swift │ │ └── readme.md │ ├── Grading Students │ │ ├── GradingStudents.swift │ │ └── readme.md │ └── readme.md ├── Strings │ ├── Caesar Cipher │ │ ├── CaeserCipher.swift │ │ └── readme.md │ ├── Camelcase │ │ ├── Camelcase.swift │ │ └── readme.md │ ├── HackerRank in a String │ │ ├── HackerRankInAString.swift │ │ └── readme.md │ ├── Mars Exploration │ │ ├── MarsExploration.swift │ │ └── readme.md │ ├── Pangrams │ │ ├── Pangrams.swift │ │ └── readme.md │ ├── Sherlock and the Valid String │ │ ├── SherlockAndTheValidString.swift │ │ └── readme.md │ └── readme.md └── Warm Up │ ├── A Very Big Sum │ ├── AVeryBigSum.swift │ └── readme.md │ ├── Birthday Cake Candles │ ├── BirthdayCakeCandles.swift │ └── readme.md │ ├── Compare The Triplets │ ├── CompareTheTriplets.swift │ └── readme.md │ ├── Diagonal Difference │ ├── DiagonalDifference.swift │ └── readme.md │ ├── Mini-Max Sum │ ├── MiniMaxSum.swift │ └── readme.md │ ├── Plus Minus │ ├── PlusMinus.swift │ └── readme.md │ ├── Simple Array Sum │ ├── SimpleArraySum.swift │ └── readme.md │ ├── Solve Me First │ ├── SolveMeFirst.swift │ └── readme.md │ ├── Staircase │ ├── Staircase.swift │ └── readme.md │ ├── Time Conversion │ ├── TimeConversion.swift │ └── readme.md │ └── readme.md ├── Cracking the Coding Interview └── Techniques and Concepts │ └── Recursion: Fibonacci Numbers │ └── FibonacciNumbers.swift ├── Data Structures ├── Arrays │ ├── Sparse Arrays │ │ ├── SparseArrays.swift │ │ └── readme.md │ └── readme.md └── Stacks │ ├── Equal Stacks │ ├── EqualStacks.swift │ └── readme.md │ └── readme.md └── readme.md /Algorithms/Bit Manipulation/Lonely Integer/LonelyInteger.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | let _ = readLine()! 4 | let ints = readLine()!.components(separatedBy: " ").map{Int($0)!} 5 | 6 | for int in ints { 7 | if (ints.filter{$0==int}).count == 1 { 8 | print(int) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Algorithms/Bit Manipulation/Lonely Integer/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Bit Manipulation/Lonely Integer/readme.md -------------------------------------------------------------------------------- /Algorithms/Bit Manipulation/readme.md: -------------------------------------------------------------------------------- 1 |

Bit Manipulation Algorithmn Solutions :pencil:

2 |

Table of Algorithms

3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |

Problem

Solution

Explanation

Difficulty

Date Submitted

Lonely IntegerLonelyInteger.swiftReadme.mdEasyJuly 20, 2017
19 | -------------------------------------------------------------------------------- /Algorithms/Implementation/Apple and Orange/AppleAndOrange.swift: -------------------------------------------------------------------------------- 1 | import Foundation; 2 | 3 | // MARK: - Tree Object 4 | struct Tree { 5 | let position: Int 6 | var droppingPositions = [Int]() 7 | init(position: Int, arrayOfDeltas: [Int]) { 8 | self.position = position 9 | for delta in arrayOfDeltas { 10 | droppingPositions.append(position + delta) 11 | } 12 | } 13 | // Returns number of droppings in a given range 14 | func quantityInRange(_ range: ClosedRange) -> Int { 15 | var counter = 0 16 | for position in droppingPositions { 17 | if range.contains(position) { 18 | counter += 1 19 | } 20 | } 21 | return counter 22 | } 23 | } 24 | 25 | // Get Input Values 26 | let housePoints = (readLine()!.components(separatedBy: " ").map{ Int($0)! }) 27 | let treePositions = (readLine()!.components(separatedBy: " ").map{ Int($0)! }) // a = [0]; b = [1] 28 | _ = (readLine()!.components(separatedBy: " ").map{ Int($0)! }) // m = [0]; n = [0] 29 | let appleDeltas = (readLine()!.components(separatedBy: " ").map{ Int($0)! }) // delta (d) of apples (a) * (m) 30 | let orangeDeltas = (readLine()!.components(separatedBy: " ").map{ Int($0)! }) // delta (d) of oranges (b) * (m) 31 | 32 | // Initialize Tree Objects 33 | let appleTree = Tree(position: treePositions[0], arrayOfDeltas: appleDeltas) 34 | let orangeTree = Tree(position: treePositions[1], arrayOfDeltas: orangeDeltas) 35 | 36 | // Call quantity in range methods with house points 37 | print(appleTree.quantityInRange(housePoints[0]...housePoints[1])) 38 | print(orangeTree.quantityInRange(housePoints[0]...housePoints[1])) 39 | -------------------------------------------------------------------------------- /Algorithms/Implementation/Apple and Orange/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Implementation/Apple and Orange/readme.md -------------------------------------------------------------------------------- /Algorithms/Implementation/Bon Appetit/BonAppetit.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | let op = readLine()!.components(separatedBy: " ").map{ Int($0)! } // n = op[0] k = op[1] 4 | let a = readLine()!.components(separatedBy: " ").map{ Int($0)! } 5 | let tCharged = Int(readLine()!)! 6 | 7 | let tActual = ((a.reduce(0,+) - a[op[1]]) / 2) 8 | if tCharged == tActual { 9 | print("Bon Appetit") 10 | } else { 11 | print(abs(tActual - tCharged)) 12 | } 13 | -------------------------------------------------------------------------------- /Algorithms/Implementation/Bon Appetit/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Implementation/Bon Appetit/readme.md -------------------------------------------------------------------------------- /Algorithms/Implementation/Breaking the Records/BreakingTheRecords.swift: -------------------------------------------------------------------------------- 1 | import Foundation; 2 | 3 | // get scores 4 | _ = readLine()! 5 | let arr = readLine()!.components(separatedBy: " ").map{ Int($0)! } 6 | 7 | var upper = arr[0] 8 | var lower = arr[0] 9 | var upperCounter = 0 10 | var lowerCounter = 0 11 | 12 | // compare scores 13 | for score in arr { 14 | if (score > upper) { 15 | upper = score 16 | upperCounter += 1 17 | } else if (score < lower) { 18 | lower = score 19 | lowerCounter += 1 20 | } 21 | } 22 | 23 | print(upperCounter, lowerCounter) 24 | -------------------------------------------------------------------------------- /Algorithms/Implementation/Breaking the Records/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Implementation/Breaking the Records/readme.md -------------------------------------------------------------------------------- /Algorithms/Implementation/Divisible Sum Pairs/DivisibleSumPairs.swift: -------------------------------------------------------------------------------- 1 | import Foundation; 2 | 3 | let op = readLine()!.components(separatedBy: " ").map{ Int($0)! } // n = op[0], j = op[1] 4 | let a = readLine()!.components(separatedBy: " ").map{ Int($0)! } 5 | let k = op[1] 6 | 7 | var counter = 0 8 | for i in 0...a.count - 1 { 9 | for j in 0...a.count - 1 { 10 | if (a[i] < a[j]) { 11 | if (a[i] < a[j]) && ((a[i] + a[j]) % k == 0) { 12 | counter += 1 13 | } 14 | } 15 | } 16 | } 17 | 18 | print(counter) 19 | -------------------------------------------------------------------------------- /Algorithms/Implementation/Divisible Sum Pairs/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Implementation/Divisible Sum Pairs/readme.md -------------------------------------------------------------------------------- /Algorithms/Implementation/Grading Students/GradingStudents.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | // Get input 4 | let quantity = (Int(readLine()!)!) 5 | var grades = [Int]() 6 | for _ in 1...quantity { 7 | grades.append(Int(readLine()!)!) 8 | } 9 | 10 | // MARK: - Rounding Function 11 | func roundGrade(_ grade: Int) -> Int { 12 | var grade = grade 13 | let diff = grade % 5 14 | if diff >= 3 && grade >= 38 { 15 | grade += 5 - diff 16 | } 17 | return grade 18 | } 19 | 20 | // Output rounded grades 21 | for grade in grades { 22 | print(roundGrade(grade)) 23 | } 24 | -------------------------------------------------------------------------------- /Algorithms/Implementation/Grading Students/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Implementation/Grading Students/readme.md -------------------------------------------------------------------------------- /Algorithms/Implementation/readme.md: -------------------------------------------------------------------------------- 1 |

Implementation Algorithm Solutions :pencil:

2 |

Table of Algorithms

3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 |

Problem

Solution

Explanation

Difficulty

Date Submitted

Apple and OrangeAppleAndOrange.swiftReadme.mdEasyMay 28, 2017
Bon AppétitBonAppetit.swiftReadme.mdEasyJun 1, 2017
Breaking the RecordsBreakingTheRecords.swiftReadme.mdEasyJune 8, 2017
Dibisible Sum PairsDivisibleSumPairs.swiftReadme.mdEasyMay 28, 2017
Grading StudentsGradingStudents.swiftReadme.mdEasyMay 28, 2017
47 | -------------------------------------------------------------------------------- /Algorithms/Strings/Caesar Cipher/CaeserCipher.swift: -------------------------------------------------------------------------------- 1 | import Foundation; 2 | 3 | _ = readLine()! // irrelavent 4 | let S = readLine()! 5 | let K = Int(readLine()!)! 6 | 7 | // MARK: - Encryption Function 8 | func encryptString(_ string: String, rotation: Int) -> String { 9 | var encryptedString = String() 10 | for char in string.unicodeScalars { 11 | var code = Int(char.value) 12 | // is lowercased letter 13 | if (97...122).contains(char.value) { 14 | code += rotation 15 | while (code > 122) { 16 | // reduce until in char range 17 | code -= (122 - 96) 18 | } 19 | // is uppercased letter 20 | } else if (65...90).contains(char.value) { 21 | code += rotation 22 | while (code > 90) { 23 | // reduce until in char range 24 | code -= (90 - 64) 25 | } 26 | } 27 | encryptedString += String(Character(UnicodeScalar(code)!)) 28 | } 29 | return encryptedString 30 | } 31 | 32 | // encrypt string 33 | print(encryptString(S, rotation: K)) 34 | -------------------------------------------------------------------------------- /Algorithms/Strings/Caesar Cipher/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Strings/Caesar Cipher/readme.md -------------------------------------------------------------------------------- /Algorithms/Strings/Camelcase/Camelcase.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | let s = readLine()! 4 | 5 | // String method, checks if uppercased at index 6 | fileprivate extension String { 7 | func isUppercased(at: Index) -> Bool { 8 | let range = at.. Bool { 11 | var query = Array(query.characters) 12 | let string = Array(string.characters) 13 | for char in string { 14 | if query[0] == char && query.count == 1 { 15 | return true 16 | } else if query[0] == char { 17 | query.removeFirst() 18 | } 19 | } 20 | return false 21 | } 22 | 23 | 24 | for str in arr { 25 | if (queryString(str, for: query)) { 26 | print("YES") 27 | } else { 28 | print("NO") 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Algorithms/Strings/HackerRank in a String/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Strings/HackerRank in a String/readme.md -------------------------------------------------------------------------------- /Algorithms/Strings/Mars Exploration/MarsExploration.swift: -------------------------------------------------------------------------------- 1 | let s = readLine()! 2 | 3 | fileprivate extension String { 4 | 5 | // MARK: - Triplets Property 6 | // Returns 3 char arrays modeling a given string 7 | var triplets: [String] { 8 | var result: [String] = [] 9 | let characters = Array(self.characters) 10 | stride(from: 0, to: characters.count, by: 3).forEach { 11 | result.append(String(characters[$0.. Character { 19 | return Array(self.characters)[i] 20 | } 21 | } 22 | 23 | var count = 0 24 | 25 | for triplet in s.triplets { 26 | switch(triplet) { 27 | case "SOS" : 28 | continue 29 | default : 30 | if triplet.charAtIndex(0) != "S" { 31 | count += 1 32 | } 33 | if triplet.charAtIndex(1) != "O" { 34 | count += 1 35 | } 36 | if triplet.charAtIndex(2) != "S" { 37 | count += 1 38 | } 39 | } 40 | } 41 | 42 | print(count) 43 | -------------------------------------------------------------------------------- /Algorithms/Strings/Mars Exploration/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Strings/Mars Exploration/readme.md -------------------------------------------------------------------------------- /Algorithms/Strings/Pangrams/Pangrams.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | // Get input 4 | let s = readLine()! 5 | 6 | // MARK: - String Method isPangram 7 | fileprivate extension String { 8 | var isPangram: Bool { 9 | let chars = Array(self.lowercased().characters) 10 | for i in 97...122 { 11 | if !(chars.contains(Character(UnicodeScalar(i)!))) { 12 | return false 13 | } 14 | } 15 | return true 16 | } 17 | } 18 | 19 | // Validate Pangram 20 | if (s.isPangram) { 21 | print ("pangram") 22 | } else { 23 | print ("not pangram") 24 | } 25 | -------------------------------------------------------------------------------- /Algorithms/Strings/Pangrams/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Strings/Pangrams/readme.md -------------------------------------------------------------------------------- /Algorithms/Strings/Sherlock and the Valid String/SherlockAndTheValidString.swift: -------------------------------------------------------------------------------- 1 | let s = readLine()! // s = given string 2 | 3 | fileprivate extension String { 4 | 5 | // MARK: Occurances Method | ⇒ O(n) 6 | // Returns indicies of a given character (c) 7 | func occurances(_ c: Character) -> Int { 8 | var count = 0 9 | let chars = Array(self.characters) 10 | for char in chars { 11 | if char == c { 12 | count += 1 13 | } 14 | } 15 | return count 16 | } 17 | 18 | // MARK: Unique Chars Property | ⇒ O(n) 19 | // Returns unique characters in a given string 20 | var uniqueCharacters: [Character] { 21 | var uniqueChars = [Character]() 22 | let chars = Array(self.characters) 23 | for char in chars { 24 | if !uniqueChars.contains(char) { 25 | uniqueChars.append(char) 26 | } 27 | } 28 | return uniqueChars 29 | } 30 | } 31 | 32 | // exceptionMade serve a purpose in the monitoring of times frequency found invalid (limit 1) 33 | var exceptionMade = false 34 | 35 | // make dictionary of unique characters and their occurances in string 's' 36 | let chars = s.uniqueCharacters.map{($0, s.occurances($0))} // [i].0 : char, [i].1 : occurances 37 | let freq = chars[0].1 // frequency to compare to other elements 38 | 39 | // iterate over characters 40 | evaluation: for (index, char) in chars.enumerated() { 41 | let cFreq = char.1 42 | if !(exceptionMade) { 43 | switch(cFreq) { 44 | case freq: 45 | // frequency is correct 46 | break 47 | case freq + 1: 48 | // frequency is over by one 49 | exceptionMade = true 50 | break 51 | case 1: 52 | // one odd out character 53 | exceptionMade = true 54 | break 55 | default: 56 | // frequency is incorrect 57 | print("NO") 58 | break evaluation 59 | } 60 | } else if (exceptionMade) { 61 | switch(cFreq) { 62 | case freq: 63 | // frequency is correct 64 | break 65 | default: 66 | // frequency is incorrect 67 | print("NO") 68 | break evaluation 69 | } 70 | } 71 | if (index == chars.count - 1) { 72 | // frequency was correct, last char in loop 73 | print("YES") 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Algorithms/Strings/Sherlock and the Valid String/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Strings/Sherlock and the Valid String/readme.md -------------------------------------------------------------------------------- /Algorithms/Strings/readme.md: -------------------------------------------------------------------------------- 1 |

String Algorithm Solutions :pencil:

2 |

Table of Algorithms

3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 |

Problem

Solution

Explanation

Difficulty

Date Submitted

Caesar CipherCaeserCipher.swiftReadme.mdEasyMay 30, 2017
CamelcaseCamelcase.swiftReadme.mdEasyMay 28, 2017
HackerRank in a StringHackerRankInAString.swiftReadme.mdEasyMay 31, 2017
Love Letter StringLoveLetterString.swiftReadme.mdEasyJune 8, 2017
Mars ExplorationMarsExploration.swiftReadme.mdEasyJune 3, 2017
PangramsPangrams.swiftReadme.mdEasyJune 7, 2017
Sherlock and the Valid StringSherlockAndTheValidString.swiftReadme.mdMediumJune 1, 2017
61 | -------------------------------------------------------------------------------- /Algorithms/Warm Up/A Very Big Sum/AVeryBigSum.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | // number of elements 4 | let n = Int(readLine()!)! 5 | 6 | // read array and map the elements to integer 7 | let arr = readLine()!.components(separatedBy: " ").map{ Int($0)! } 8 | 9 | var sum = 0 10 | for int in arr { 11 | sum += int 12 | } 13 | 14 | print(sum) 15 | -------------------------------------------------------------------------------- /Algorithms/Warm Up/A Very Big Sum/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Warm Up/A Very Big Sum/readme.md -------------------------------------------------------------------------------- /Algorithms/Warm Up/Birthday Cake Candles/BirthdayCakeCandles.swift: -------------------------------------------------------------------------------- 1 | import Foundation; 2 | 3 | // Get input 4 | _ = Int(readLine()!)! // irrelavent 5 | let candleHeights = readLine()!.components(separatedBy: " ").map{ Int($0)! } 6 | 7 | // Get max height 8 | let maxHeight: Int = candleHeights.max()! 9 | 10 | var counter = 0 11 | for height in candleHeights { 12 | if maxHeight == height { 13 | counter += 1 14 | } 15 | } 16 | 17 | print(counter) 18 | -------------------------------------------------------------------------------- /Algorithms/Warm Up/Birthday Cake Candles/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Warm Up/Birthday Cake Candles/readme.md -------------------------------------------------------------------------------- /Algorithms/Warm Up/Compare The Triplets/CompareTheTriplets.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | // Get number input 4 | let a = readLine()!.components(separatedBy: " ").map { Int($0)!} 5 | let b = readLine()!.components(separatedBy: " ").map { Int($0)!} 6 | 7 | // Map numbers to dictionary 8 | var numbers = [Int:Int]() 9 | for (index, element) in a.enumerated() { 10 | numbers[element] = b[index] 11 | } 12 | 13 | 14 | // Initialize score counters 15 | var aScore = 0 16 | var bScore = 0 17 | 18 | func compare(_ anumber: Int, _ bnumber: Int) { 19 | if anumber > bnumber { 20 | aScore += 1 21 | } else if bnumber > anumber { 22 | bScore += 1 23 | } 24 | } 25 | 26 | // Compare number set 27 | for (anumber, bnumber) in numbers { 28 | compare(anumber, bnumber) 29 | } 30 | 31 | // Output scores 32 | print(aScore, bScore) 33 | -------------------------------------------------------------------------------- /Algorithms/Warm Up/Compare The Triplets/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Warm Up/Compare The Triplets/readme.md -------------------------------------------------------------------------------- /Algorithms/Warm Up/Diagonal Difference/DiagonalDifference.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | // read the integer n 4 | let n = Int(readLine()!)! 5 | 6 | // declare 2d array 7 | var arr: [[Int]] = [] 8 | 9 | // read array row-by-row 10 | for _ in 0.. O(n^2) 7 | func insertionSort(_ array: [Int]) -> [Int] { 8 | var sort = array 9 | for x in 1.. 0 && sort[y] < sort[y - 1] { 12 | swap(&sort[y - 1], &sort[y]) 13 | y -= 1 14 | } 15 | } 16 | return sort 17 | } 18 | 19 | // Sort ints 20 | let sortedInts = insertionSort(ints) 21 | 22 | // Get min and max via index 23 | let minSum = sortedInts[0] + sortedInts[1] + sortedInts[2] + sortedInts[3] 24 | let maxSum = sortedInts[1] + sortedInts[2] + sortedInts[3] + sortedInts[4] 25 | 26 | print(minSum, maxSum) 27 | -------------------------------------------------------------------------------- /Algorithms/Warm Up/Mini-Max Sum/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Warm Up/Mini-Max Sum/readme.md -------------------------------------------------------------------------------- /Algorithms/Warm Up/Plus Minus/PlusMinus.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | // number of elements 4 | let n = Int(readLine()!)! 5 | 6 | // read array and map the elements to integer 7 | let arr = readLine()!.components(separatedBy: " ").map{ Int($0)! } 8 | 9 | // Initialize counters 10 | var positiveCount = 0.0 11 | var negitiveCount = 0.0 12 | var zeroCount = 0.0 13 | 14 | // Score integers 15 | for int in arr { 16 | if int > 0 { 17 | positiveCount += 1.0 18 | } else if int < 0 { 19 | negitiveCount += 1.0 20 | } else { 21 | zeroCount += 1.0 22 | } 23 | } 24 | 25 | var total = positiveCount + negitiveCount + zeroCount 26 | 27 | // Get fractions 28 | var percentPositive = positiveCount / total 29 | var percentNegitive = negitiveCount / total 30 | var percentZero = zeroCount / total 31 | 32 | // Output fractions 33 | print("\(percentPositive)\n\(percentNegitive)\n\(percentZero)") 34 | -------------------------------------------------------------------------------- /Algorithms/Warm Up/Plus Minus/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Warm Up/Plus Minus/readme.md -------------------------------------------------------------------------------- /Algorithms/Warm Up/Simple Array Sum/SimpleArraySum.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | // number of elements 4 | let n = Int(readLine()!)! 5 | 6 | // read array and map the elements to integer 7 | let arr = readLine()!.components(separatedBy: " ").map{ Int($0)! } 8 | 9 | // find and print the sum of array 10 | var total = Int() 11 | for num in arr { 12 | total += num 13 | } 14 | print(total) 15 | -------------------------------------------------------------------------------- /Algorithms/Warm Up/Simple Array Sum/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Warm Up/Simple Array Sum/readme.md -------------------------------------------------------------------------------- /Algorithms/Warm Up/Solve Me First/SolveMeFirst.swift: -------------------------------------------------------------------------------- 1 | // read integers line by line 2 | var a = Int(readLine()!)! 3 | var b = Int(readLine()!)! 4 | 5 | // Hint: Type print(a + b) below 6 | print(a + b) 7 | -------------------------------------------------------------------------------- /Algorithms/Warm Up/Solve Me First/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Warm Up/Solve Me First/readme.md -------------------------------------------------------------------------------- /Algorithms/Warm Up/Staircase/Staircase.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | // read the integer n 4 | let n = Int(readLine()!)! 5 | 6 | // print the staircase 7 | func drawStaircase(size n: Int) -> String { 8 | var stairs = String() 9 | // iterate over range of 1 and n 10 | for step in (1...n).reversed() { 11 | // repeat \s and # accordingly 12 | stairs += String(repeating: " ", count: step - 1) + String(repeating: "#", count: n + 1 - step) + "\n" 13 | } 14 | return stairs 15 | } 16 | 17 | print(drawStaircase(size: n)) 18 | -------------------------------------------------------------------------------- /Algorithms/Warm Up/Staircase/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Warm Up/Staircase/readme.md -------------------------------------------------------------------------------- /Algorithms/Warm Up/Time Conversion/TimeConversion.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | // MARK: - String ⇒ Date 4 | fileprivate extension String { 5 | func toDate() -> Date { 6 | let dateFormatter = DateFormatter() 7 | // format: 08:10:45AM - Eqv 8 | dateFormatter.dateFormat = "hh:mm:ssa" 9 | dateFormatter.locale = Locale.init(identifier: "en_GB") 10 | let date = dateFormatter.date(from: self) 11 | return date! 12 | } 13 | } 14 | 15 | // MARK: - Date ⇒ String 16 | fileprivate extension Date { 17 | func toString() -> String { 18 | let dateFormatter = DateFormatter() 19 | // format: 20:10:45 - Eqv 20 | dateFormatter.dateFormat = "HH:mm:ss" 21 | dateFormatter.locale = Locale.init(identifier: "en_GB") 22 | let dateString = dateFormatter.string(from: self as Date) 23 | return dateString 24 | } 25 | } 26 | 27 | // Get input 28 | let date = readLine()!.toDate() 29 | 30 | // Output date 31 | let outDate = date.toString() 32 | print(outDate) 33 | -------------------------------------------------------------------------------- /Algorithms/Warm Up/Time Conversion/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Algorithms/Warm Up/Time Conversion/readme.md -------------------------------------------------------------------------------- /Algorithms/Warm Up/readme.md: -------------------------------------------------------------------------------- 1 |

Warm Up Algorithm Solutions :pencil:

2 |

Table of Algorithms

3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 |

Problem

Solution

Explanation

Difficulty

Date Submitted

A Very Big SumAVeryBigSum.swiftReadme.mdEasyMay 28, 2017
Birthday Cake CandlesBirthdayCakeCandles.swiftReadme.mdEasyMay 28, 2017
Compare The TripletsCompareTheTriplets.swiftReadme.mdEasyMay 28, 2017
Diagonal DifferenceDiagonalDifference.swiftReadme.mdEasyMay 28, 2017
Mini-Max SumMiniMaxSum.swiftReadme.mdEasyMay 28, 2017
Plus MinusPlusMinus.swiftReadme.mdEasyMay 28, 2017
Simple Array SumSimpleArraySum.swiftReadme.mdEasyMay 28, 2017
Solve Me FirstSolveMeFirst.swiftReadme.mdEasyMay 28, 2017
StaircaseStaircase.swiftReadme.mdEasyMay 28, 2017
Time ConversionTimeConversion.swiftReadme.mdEasyMay 28, 2017
82 | -------------------------------------------------------------------------------- /Cracking the Coding Interview/Techniques and Concepts/Recursion: Fibonacci Numbers/FibonacciNumbers.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | // Get input 4 | let n = Int(readLine()!)! 5 | 6 | // MARK - Fibonacci Function 7 | var memo = [Int:Int]() 8 | func fibonacci(_ n: Int) -> Int { 9 | if n == 0 { 10 | return 0 11 | } else if n == 1 { 12 | return 1 13 | } else { 14 | if memo[n] == nil { 15 | memo[n] = fibonacci(n - 1) + fibonacci(n - 2) 16 | } 17 | return memo[n]! 18 | } 19 | } 20 | 21 | print(fibonacci(n)) 22 | -------------------------------------------------------------------------------- /Data Structures/Arrays/Sparse Arrays/SparseArrays.swift: -------------------------------------------------------------------------------- 1 | import Foundation; 2 | 3 | // Get Input 4 | let N = Int(readLine()!)! 5 | var strings = [String]() 6 | for _ in 1...N { 7 | strings.append(readLine()!) 8 | } 9 | let Q = Int(readLine()!)! 10 | var queries = [String]() 11 | for _ in 1...Q { 12 | queries.append(readLine()!) 13 | } 14 | 15 | // MARK: - String Query Function 16 | func makeStringQuery(_ query: String, strings: [String]) -> Int { 17 | var counter = 0 18 | for string in strings { 19 | if string == query { 20 | counter += 1 21 | } 22 | } 23 | return counter 24 | } 25 | 26 | // Make Queries 27 | for query in queries { 28 | print(makeStringQuery(query, strings: strings)) 29 | } 30 | -------------------------------------------------------------------------------- /Data Structures/Arrays/Sparse Arrays/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Data Structures/Arrays/Sparse Arrays/readme.md -------------------------------------------------------------------------------- /Data Structures/Arrays/readme.md: -------------------------------------------------------------------------------- 1 |

Stack Data Structure Solutions :bar_chart:

2 |

Table of Algorithms

3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |

Problem

Solution

Explanation

Difficulty

Date Submitted

Sparse ArraysSparseArrays.swiftReadme.mdEasyMay 28, 2017
19 | -------------------------------------------------------------------------------- /Data Structures/Stacks/Equal Stacks/EqualStacks.swift: -------------------------------------------------------------------------------- 1 | iimport Foundation; 2 | 3 | // Get elements; get stack height 4 | _ = readLine()!.components(separatedBy: " ").map{ Int($0)! } // irrelavent 5 | var stack1 = readLine()!.components(separatedBy: " ").map{ Int($0)! }; var stack1Height = stack1.reduce(0, +) 6 | var stack2 = readLine()!.components(separatedBy: " ").map{ Int($0)! }; var stack2Height = stack2.reduce(0, +) 7 | var stack3 = readLine()!.components(separatedBy: " ").map{ Int($0)! }; var stack3Height = stack3.reduce(0, +) 8 | 9 | // Remove elements until stacks are equal in height 10 | while stack1Height != stack2Height || stack1Height != stack3Height || stack2Height != stack3Height { 11 | if (stack1Height >= stack2Height) && (stack1Height >= stack3Height) { 12 | stack1Height -= stack1.removeFirst() 13 | } else if (stack2Height >= stack1Height) && (stack2Height >= stack3Height) { 14 | stack2Height -= stack2.removeFirst() 15 | } else if (stack3Height >= stack1Height) && (stack3Height >= stack2Height) { 16 | stack3Height -= stack3.removeFirst() 17 | } 18 | } 19 | 20 | // Return equal height 21 | print(stack1Height) 22 | -------------------------------------------------------------------------------- /Data Structures/Stacks/Equal Stacks/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xch4z/hackerrank-swift/7d984874a2d13b8de4fc4d5ec006323c5d736060/Data Structures/Stacks/Equal Stacks/readme.md -------------------------------------------------------------------------------- /Data Structures/Stacks/readme.md: -------------------------------------------------------------------------------- 1 |

Stack Data Structure Solutions :bar_chart:

2 |

Table of Algorithms

3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |

Problem

Solution

Explanation

Difficulty

Date Submitted

Equal StacksEqualStacks.swiftReadme.mdEasyMay 28, 2017
19 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 |

My Solutions to HackerRank Problems in Swift

2 |

Table of Contents

3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 |

Algorithms

Data Structures

Mathematics

Regex

Warm Up (10/10)Arrays (1/6)Fundamentals (0/31)Introduction (0/6)
Implementation (5/64)Linked Lists (0/45)Number Theory (0/73)Character Classes (0/3)
Strings (7/45)Trees (0/17)Combinatorics (0/54)Repititions (0/5)
Sorting (0/19)Balanced Trees (0/3)Algebra (0/37)Grouping and Capturing (0/3)
Search (0/25)Stacks (1/9)Geometry (0/30)Back References (0/4)
Graph Theory (0/62)Queues (0/5)Probability (0/28)Assertions (0/4)
Greedy (0/23)Heap (0/4)Linear Algebra (0/28)Applications (0/23)
Dynamic Programming (0/97)Disjoint Set (0/4)
Constructive Algorithms (0/21)Multiple Choice (0/3)
Bit Manipulation (1/26)Trie (0/2)
Recursion (0/21)Advanced (0/48)
Game Theory (0/42)
NP Complete (0/4)
89 |

*only populated subdomains are linked...*

90 |
91 | --------------------------------------------------------------------------------