Last active
September 22, 2017 05:34
-
-
Save hcn1519/5c3b5c8d98daa216cc9d6effe7fdad88 to your computer and use it in GitHub Desktop.
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
// p1 | |
import Foundation | |
func problem1(n: Int, arr1: [Int], arr2: [Int]) { | |
let binaryArr1 = generateBinaryArray(arr: arr1, length: n) | |
let binaryArr2 = generateBinaryArray(arr: arr2, length: n) | |
var finalArr: [[Int]] = [] | |
var finalResult: [String] = [] | |
for i in 0..<n { | |
var row: [Int] = [] | |
var item: String = "" | |
for j in 0..<n { | |
if binaryArr1[i][j] == 1 || binaryArr2[i][j] == 1 { | |
item.append("#") | |
row.append(1) | |
} else { | |
item.append(" ") | |
row.append(0) | |
} | |
} | |
finalArr.append(row) | |
finalResult.append(item) | |
} | |
print(finalResult) | |
} | |
// 십진수를 이진수로 | |
func decimalToBinary(input: Int) -> [Int] { | |
var input = input | |
var result: [Int] = [] | |
while input > 1 { | |
let q = input / 2 | |
let remainder = input % 2 | |
result.append(remainder) | |
input = q | |
} | |
result.append(input) | |
return result | |
} | |
// 이진수 배열 만들기 | |
func generateBinaryArray(arr: [Int], length: Int) -> [[Int]] { | |
var binaryArr: [[Int]] = [] | |
for item in arr { | |
var row = decimalToBinary(input: item) | |
// 부족한 0 채워주기 | |
if row.count < length { | |
let shouldAddZero = length - row.count | |
for _ in 1...shouldAddZero { | |
row.append(0) | |
} | |
} | |
// 거꾸로 들어가서 반전 | |
row = row.reversed() | |
binaryArr.append(row) | |
} | |
return binaryArr | |
} | |
let n = 5 | |
let arr1 = [9, 20, 28, 18, 11] | |
let arr2 = [30, 1, 21, 17, 28] | |
problem1(n: n, arr1: arr1, arr2: arr2) | |
let m = 6 | |
let arr3 = [46, 33, 33 ,22, 31, 50] | |
let arr4 = [27 ,56, 19, 14, 14, 10] | |
problem1(n: m, arr1: arr3, arr2: arr4) | |
// p2 | |
func readString(input: String) -> [String] { | |
var set: String = "" | |
var setOfInput: [String] = [] | |
for (index, c) in input.characters.enumerated() { | |
if c != "#" && c != "*" { | |
set += String(c) | |
} | |
switch c { | |
case "S", "D", "T": | |
if index+1 < input.characters.count { | |
let additionalOption = input[input.index(input.startIndex, offsetBy: index+1)] | |
if additionalOption == "#" || additionalOption == "*" { | |
set += String(additionalOption) | |
} | |
setOfInput.append(set) | |
set = "" | |
} else { | |
setOfInput.append(set) | |
} | |
default: | |
break | |
} | |
} | |
return setOfInput | |
} | |
func pointCalculator(oneSet: String) -> (Int, Bool) { | |
var oneSet = oneSet | |
var points = 0 | |
var multiplier = false | |
if oneSet.contains("*") { | |
oneSet.remove(at: oneSet.index(before: oneSet.endIndex)) | |
multiplier = true | |
} | |
var seperatedSet: [String] = [] | |
var bonus = -1 | |
if oneSet.contains("S") { | |
seperatedSet = oneSet.components(separatedBy: "S") | |
bonus = 0 | |
} else if oneSet.contains("D") { | |
seperatedSet = oneSet.components(separatedBy: "D") | |
bonus = 1 | |
} else { | |
seperatedSet = oneSet.components(separatedBy: "T") | |
bonus = 2 | |
} | |
points = Int(seperatedSet[0])! | |
let p = points | |
switch bonus { | |
case 1: | |
points *= p | |
case 2: | |
points *= (p * p) | |
default: | |
break | |
} | |
if oneSet.contains("#") { | |
points *= -1 | |
} | |
return (points, multiplier) | |
} | |
func adjustOptionsAndPrintAnswer(setOfInput: [String]){ | |
var answer = 0 | |
var defaultPoints: [Int] = [] | |
var prevPoint: Int = 0 | |
for index in setOfInput.indices { | |
let eachPoint = pointCalculator(oneSet: setOfInput[index]) | |
if eachPoint.1 { | |
// 스타상 | |
if index == 0 { | |
// 첫 번째 경우 | |
answer = eachPoint.0 * 2 | |
prevPoint = eachPoint.0 * 2 | |
} else { | |
// 이외 | |
answer += prevPoint | |
answer += eachPoint.0 * 2 | |
prevPoint = eachPoint.0 | |
} | |
} else { | |
answer += eachPoint.0 | |
prevPoint = eachPoint.0 | |
} | |
} | |
print(answer) | |
} | |
let input1 = "1S2D*3T" | |
let input2 = "1D2S#10S" | |
let input3 = "1D2S0T" | |
let input4 = "1S*2T*3S" | |
let input5 = "1D#2S*3S" | |
let input6 = "1T2D3D#" | |
let input7 = "1D2S3T*" | |
adjustOptionsAndPrintAnswer(setOfInput: readString(input: input1)) | |
adjustOptionsAndPrintAnswer(setOfInput: readString(input: input2)) | |
adjustOptionsAndPrintAnswer(setOfInput: readString(input: input3)) | |
adjustOptionsAndPrintAnswer(setOfInput: readString(input: input4)) | |
adjustOptionsAndPrintAnswer(setOfInput: readString(input: input5)) | |
adjustOptionsAndPrintAnswer(setOfInput: readString(input: input6)) | |
adjustOptionsAndPrintAnswer(setOfInput: readString(input: input7)) | |
// p3 | |
func runtimeCalculator(cacheSize: Int, cities: [String]) -> Int { | |
var runTime = 0 | |
var caches: [String] = [] | |
if !(cacheSize < 1) { | |
caches = Array(repeatElement("", count: cacheSize)) | |
} else { | |
// 사이즈가 0일 경우 | |
return cities.count * 5 | |
} | |
var index = 0 | |
for city in cities { | |
let lowerCity = city.lowercased() | |
if !caches.contains(lowerCity) { | |
// 없음 | |
runTime += 5 | |
if index < cacheSize { | |
caches[index] = lowerCity | |
index += 1 | |
} else { | |
// index = 배열 길이 | |
index = 0 | |
caches[index] = lowerCity | |
index += 1 | |
} | |
} else { | |
// 있음 | |
runTime += 1 | |
} | |
} | |
return runTime | |
} | |
let cacheSize = [3, 3, 2, 5, 2, 0] | |
let cities = [ ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA"], | |
["Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul"], | |
["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"], | |
["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"], | |
["Jeju", "Pangyo", "NewYork", "newyork"], | |
["Jeju", "Pangyo", "Seoul", "NewYork", "LA"] | |
] | |
let result = [50, 21, 60, 52, 16, 25] | |
for i in 0..<6 { | |
if result[i] == runtimeCalculator(cacheSize: cacheSize[i], cities: cities[i]) { | |
print("\(i+1)번 예시 O") | |
} else { | |
print("\(i+1)번 예시 X") | |
} | |
} | |
// p4 | |
func generateSet(str: String) -> [String: Int] { | |
var set: [String: Int] = [:] | |
for (i, c) in str.enumerated() { | |
if i + 1 < str.count { | |
let element = String(c) + String(str[str.index(str.startIndex, offsetBy: i+1)]) | |
// 특수문자 제외, 알파벳만 | |
if NSPredicate(format: "SELF MATCHES %@", "^[a-zA-Z]+$").evaluate(with: element) { | |
if set[element] == nil { | |
set[element] = 1 | |
} else { | |
set[element]! += 1 | |
} | |
} | |
} | |
} | |
return set | |
} | |
// 교집합 길이 만들기 | |
func generateIntersection(str1: [String: Int], str2: [String: Int]) -> Double { | |
var result: [String: Int] = [:] | |
for (key, value) in str1 { | |
if let value2 = str2[key] { | |
if value2 <= value { | |
result.updateValue(value2, forKey: key) | |
} else { | |
result.updateValue(value, forKey: key) | |
} | |
} | |
} | |
var intersectionLength = 0 | |
for (_, value) in result { | |
intersectionLength += value | |
} | |
return Double(intersectionLength) | |
} | |
// 합집합 길이 만들기 | |
func generateUnion(str1: [String: Int], str2: [String: Int]) -> Double { | |
var result: [String: Int] = str1 | |
for (key, value) in str2 { | |
if result[key] == nil { | |
result.updateValue(value, forKey: key) | |
} else { | |
if result[key]! <= value { | |
result.updateValue(value, forKey: key) | |
} | |
} | |
} | |
var unionLength = 0 | |
for (_, value) in result { | |
unionLength += value | |
} | |
return Double(unionLength) | |
} | |
func jakad(str1: String, str2: String) { | |
let lowerStr1 = str1.lowercased() | |
let lowerStr2 = str2.lowercased() | |
let set1 = generateSet(str: lowerStr1) | |
let set2 = generateSet(str: lowerStr2) | |
let interSectionCount = generateIntersection(str1: set1, str2: set2) | |
let unionCount = generateUnion(str1: set1, str2: set2) | |
if unionCount == 0 { | |
print(65536) | |
} else { | |
let similarity: Double = interSectionCount / unionCount | |
let resultSimilarity = Int(similarity * 65536) | |
print(resultSimilarity) | |
} | |
} | |
jakad(str1: "FRANCE", str2: "french") | |
jakad(str1: "handshake", str2: "shake hands") | |
jakad(str1: "aa1+aa2", str2: "AAAA12") | |
jakad(str1: "E=M*C^2", str2: "e=m*c^2") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment