Last active
January 1, 2025 11:21
-
-
Save Schockarum/812da6951dc2830326615f269b5ed006 to your computer and use it in GitHub Desktop.
Hacker Rank [Swift] 1 Month Preparation Kit - Week 1 Algorithms
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 | |
struct PlusMinusRatio{ | |
var numberOfValues: [Double] | |
var elements: Double | |
var precision: Int | |
init(numberOfElements: Int, decimals: Int = 6) { | |
elements = Double(numberOfElements) | |
numberOfValues = [0.0, 0.0, 0.0] //Positives, Negatives, Zeros | |
precision = decimals | |
} | |
func printRatios(){ | |
for element in numberOfValues{ | |
print(String(format:"%.\(precision)f",element/elements)) | |
} | |
} | |
} | |
func plusMinus(arr: [Int]) -> Void { | |
// Write your code here | |
var resultingRatios: PlusMinusRatio = PlusMinusRatio.init(numberOfElements: arr.count) | |
for number in arr{ | |
resultingRatios.numberOfValues[0] += number > 0 ? 1 : 0 | |
resultingRatios.numberOfValues[1] += number < 0 ? 1 : 0 | |
resultingRatios.numberOfValues[2] += number == 0 ? 1 : 0 | |
} | |
resultingRatios.printRatios() | |
} | |
plusMinus(arr: [10,20,-50,0,0,0,0,0,235,-459,10,-25,0,67]) |
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 miniMaxSum(arr: [Int]) -> Void { | |
var sortedArray = arr | |
sortedArray.sort() | |
var minResult: Int = sortedArray[0] | |
var maxResult: Int = sortedArray[(sortedArray.count-1)] | |
for number in 1..<(sortedArray.count-1) | |
{ | |
minResult += sortedArray[number] | |
maxResult += sortedArray[number] | |
} | |
print("\(minResult) \(maxResult)") | |
} |
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 timeConversion(s: String) -> String { | |
// Write your code here | |
let hhmmssXM: [String] = s.components(separatedBy: ":") | |
let hour: Int = militaryHour(hour: hhmmssXM[0], meridiem: hhmmssXM[2]) | |
let militarFormattedHour: String | |
militarFormattedHour = hour < 10 ? "0\(hour):\(hhmmssXM[1]):\(hhmmssXM[2].prefix(2))" : "\(hour):\(hhmmssXM[1]):\(hhmmssXM[2].prefix(2))" | |
return militarFormattedHour | |
} | |
func militaryHour(hour: String, meridiem: String) -> Int{ | |
var intHour = Int(hour) ?? 0 //Como podemos insertar un string raro que no devuelva numero, ponemos valor default | |
intHour = (meridiem.contains("PM") && intHour < 12) ? (intHour + 12) : intHour | |
intHour = (meridiem.contains("AM") && intHour == 12) ? (intHour - 12) : intHour | |
return intHour | |
} | |
// | |
//for hour in 0...12{ | |
// print(timeConversion(s: "\(hour):15:46AM")) | |
// print(timeConversion(s: "\(hour):15:46PM")) | |
//} | |
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 matchingStrings(strings: [String], queries: [String]) -> [Int] { | |
var results: [Int] = [] | |
for index in 0..<queries.count{ | |
results += [0] | |
for string in strings{ | |
results[index] += string.elementsEqual(queries[index]) ? 1 : 0 | |
} | |
} | |
return results | |
} | |
print(matchingStrings(strings: ["ab","ab","abc"], queries: ["ab","abc","bc"])) |
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 | |
// Encuentra el número entero en un set donde todos los números, excepto uno, están repetidos una vez. | |
func lonelyinteger(a: [Int]) -> Int { | |
var singleIntSet = Set<Int>() | |
for num in a{ | |
if singleIntSet.contains(num){ | |
singleIntSet.remove(num) | |
} else { | |
singleIntSet.insert(num) | |
} | |
} | |
return singleIntSet.removeFirst() | |
} | |
let resultLoneInt = lonelyinteger(a: [1,2,3,4,3,2,1]) | |
print(resultLoneInt) |
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 | |
import Darwin | |
import CoreFoundation | |
func flippingBits(n: Int) -> UInt32 { | |
// Write your code here | |
let flippedInt: UInt32 = ~UInt32(n) | |
return flippedInt | |
} | |
print(flippingBits(n: 1)) |
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
//: [Previous](@previous) | |
import Foundation | |
/// Calcula la diferencia absoluta de las diagonáles hacia abajo y hacia la derecha VS hacia arriba y hacia la izquierda | |
func diagonalDifference(arr: [[Int]]) -> Int { | |
let squareSize = arr[0].count | |
var absoluteDifference: Int = 0 | |
var toRightDiagonal: Int = 0, toLeftDiagonal: Int = 0 | |
for i in 0..<squareSize{ | |
toRightDiagonal += arr[i][i] | |
toLeftDiagonal += arr[i][squareSize-1-i] | |
// print("Iteración \(i)- TRD: \(arr[i][i]) - TLD: \(arr[i][squareSize-1-i])") | |
} | |
absoluteDifference = abs((toRightDiagonal-toLeftDiagonal)) | |
return absoluteDifference | |
} | |
print(diagonalDifference(arr: [[1,2,3],[4,5,6],[9,8,9]])) |
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 | |
// Counting Sort. | |
// recorre una vez el arreglo y anota en una array el nùmero de repeticiones de cada número en el arreglo a ordenar | |
// Ejemplo: Si arreglo[20] = 87, suma un 1 a la posiciòn 87 del arreglo de resultados | |
// De éste modo, podemos calcular el arreglo ordenado resultante si creamos a partir del arreglo de repeticiones resultante | |
func countingSort(arr: [Int]) -> [Int] { | |
var resultArray: [Int] = Array.init(repeating: 0, count: 100) | |
for number in arr{ | |
resultArray[number] += 1 | |
} | |
return resultArray | |
} |
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 pangrams(s: String) -> String { | |
let pattern = "[^a-z]+" | |
var filteredString: String = s.lowercased() | |
filteredString = filteredString.replacingOccurrences(of: pattern, with: "", options: [.regularExpression]) | |
var lettersInString: Set<Character> = [] | |
for c in filteredString{ | |
if !lettersInString.contains(c){ | |
lettersInString.insert(c) | |
} | |
} | |
let isPanagram = lettersInString.count == 26 ? "pangram" : "not pangram" | |
return isPanagram | |
} | |
print(pangrams(s: "We promptly judged antique ivory buckles for the next prize. ,")) | |
print(pangrams(s: "We promptly judged antique ivory buckles for the prize")) |
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 twoArrays(k: Int, A: [Int], B: [Int]) -> String { | |
// Copiamos arreglos para poderlos trabajar | |
var minFirstSortedA: [Int] = A | |
var maxFirstSortedB: [Int] = B | |
var result: String = "YES" | |
// Ordenamos el arreglo A con más pequeños primero | |
minFirstSortedA.sort() | |
print(minFirstSortedA) | |
// Ordenamos el arreglo B con más grandes primero | |
maxFirstSortedB.sort(by: >) | |
print(maxFirstSortedB) | |
for i in 0..<(A.count){ | |
if (minFirstSortedA[i] + maxFirstSortedB[i]) < k{ | |
result = "NO" | |
break | |
} | |
} | |
return result | |
} | |
print(twoArrays(k: 10, A: [2, 1, 2], B: [7, 8, 9])) |
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 birthday(s: [Int], d: Int, m: Int) -> Int { | |
var result: Int = 0 | |
var chocolateSum: Int = 0 | |
for i in 0...(s.count-m){ | |
chocolateSum = 0 | |
for j in i...(i+m-1){ | |
chocolateSum += s[j] | |
} | |
result += chocolateSum == d ? 1 : 0; | |
} | |
return result | |
} | |
print(birthday(s: [1,2,1,3,2], d: 3, m: 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
#Ejercicio no disponible para resolver en Swift. Se hizo en Python :p | |
#include <cmath> | |
#include <cstdio> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
string strings_xor(string s, string t) { | |
string res = ""; | |
for(int i = 0; i < s.size(); i++) { | |
if(s[i] = t[i]) | |
res = '0'; | |
else | |
res = '1'; | |
} | |
return res; | |
} | |
int main() { | |
string s, t; | |
cin >> s >> t; | |
cout << strings_xor(s, t) << endl; | |
return 0; | |
} | |
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 findMedian(arr: [Int]) -> Int { | |
var sortedArray: [Int] = arr | |
sortedArray.sort() | |
let medianIdx: Int = sortedArray.count/2 | |
let median: Int = sortedArray[medianIdx] | |
return median | |
} |
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 flippingMatrix(matrix: [[Int]]) -> Int { | |
let n: Int = matrix[0].count/2 | |
var n1: Int = 0, n2: Int = 0, n3: Int = 0, n4: Int = 0 | |
var sum: Int = 0 | |
for i in 0..<n{ | |
for j in 0..<n{ | |
n1 = matrix[i][j] | |
n2 = matrix[i][(n*2)-1-j] | |
n3 = matrix[(n*2)-1-i][j] | |
n4 = matrix[(n*2)-1-i][(n*2)-1-j] | |
sum += compareMax([n1,n2,n3,n4]) | |
} | |
} | |
return sum | |
} | |
func compareMax(_ numbers: [Int]) -> Int{ | |
var max: Int = 0 | |
var mutableArray: [Int] = numbers | |
mutableArray.sort() | |
max = mutableArray[mutableArray.count-1] | |
print("Max of \(mutableArray) is: \(max)") | |
return max | |
} | |
let myMatrix: [[Int]] = [ [1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] ] | |
print(flippingMatrix(matrix: myMatrix)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment