프로그래머스/Lv.1
프로그래머스 코딩테스트 연습 Lv.1 Swift
-
import Foundation func solution(_ n:Int) -> Int { n == 0 ? 0 : (1...n).filter{ n % $0 == 0 }.reduce(0, +) }
[Swift] 약수의 합import Foundation func solution(_ n:Int) -> Int { n == 0 ? 0 : (1...n).filter{ n % $0 == 0 }.reduce(0, +) }
2023.02.15 -
import Foundation func solution(_ left:Int, _ right:Int) -> Int { return (left...right).map{ number in (1...number).filter { number % $0 == 0 }.count % 2 == 0 ? number : -number }.reduce(0, +) }
[Swift] 약수의 개수와 덧셈import Foundation func solution(_ left:Int, _ right:Int) -> Int { return (left...right).map{ number in (1...number).filter { number % $0 == 0 }.count % 2 == 0 ? number : -number }.reduce(0, +) }
2023.02.15 -
import Foundation func solution(_ N:Int, _ stages:[Int]) -> [Int] { var countArray = Array.init(repeating: 0, count: N) // 각 스테이지 당 같혀 있는 사람들의 수 var probabilityArray = Array.init(repeating: (Int(0), Double(0.0)), count: N) // 스테이지, 실패율 var total = stages.count // 각 스테이지를 도전한 사람 for element in stages { guard element != N+1 else { // 모두 통과 했다면 배열에 넣지 않음 continue } countArray[element-1] += 1 } for ..
[Swift] 실패율import Foundation func solution(_ N:Int, _ stages:[Int]) -> [Int] { var countArray = Array.init(repeating: 0, count: N) // 각 스테이지 당 같혀 있는 사람들의 수 var probabilityArray = Array.init(repeating: (Int(0), Double(0.0)), count: N) // 스테이지, 실패율 var total = stages.count // 각 스테이지를 도전한 사람 for element in stages { guard element != N+1 else { // 모두 통과 했다면 배열에 넣지 않음 continue } countArray[element-1] += 1 } for ..
2023.02.15 -
import Foundation func solution(_ new_id:String) -> String { var result = new_id.lowercased().components(separatedBy: ["~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "=", "+", "[", "]", "{", "}", ":", "?", ",", "", "/"]).joined().components(separatedBy: ".").filter { $0 != "" }.joined(separator: ".").trimmingCharacters(in: ["."]) if result.isEmpty { result = "a" } if 15
[Swift] 신규 아이디 추천import Foundation func solution(_ new_id:String) -> String { var result = new_id.lowercased().components(separatedBy: ["~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "=", "+", "[", "]", "{", "}", ":", "?", ",", "", "/"]).joined().components(separatedBy: ".").filter { $0 != "" }.joined(separator: ".").trimmingCharacters(in: ["."]) if result.isEmpty { result = "a" } if 15
2023.02.15 -
import Foundation func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] { var reported: [String: Int] = [:] var user: [String: [String]] = [:] for r in Set(report) { let splited = r.split(separator: " ").map { String($0) } user[splited[0]] = (user[splited[0]] ?? []) + [splited[1]] reported[splited[1]] = (reported[splited[1]] ?? 0) + 1 } return id_list.map { id in return (user[id..
[Swift] 신고 결과 받기import Foundation func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] { var reported: [String: Int] = [:] var user: [String: [String]] = [:] for r in Set(report) { let splited = r.split(separator: " ").map { String($0) } user[splited[0]] = (user[splited[0]] ?? []) + [splited[1]] reported[splited[1]] = (reported[splited[1]] ?? 0) + 1 } return id_list.map { id in return (user[id..
2023.02.15 -
import Foundation func solution(_ s:String, _ n:Int) -> String { return s.utf8.map { var asciiCode = Int($0) switch asciiCode { case 65...90: asciiCode = (asciiCode + n - 65) % 26 + 65 case 97...122: asciiCode = (asciiCode + n - 97) % 26 + 97 default: break } return String(UnicodeScalar(asciiCode)!) }.joined() }
[Swift] 시저 암호import Foundation func solution(_ s:String, _ n:Int) -> String { return s.utf8.map { var asciiCode = Int($0) switch asciiCode { case 65...90: asciiCode = (asciiCode + n - 65) % 26 + 65 case 97...122: asciiCode = (asciiCode + n - 97) % 26 + 97 default: break } return String(UnicodeScalar(asciiCode)!) }.joined() }
2023.02.15 -
import Foundation func solution(_ X:String, _ Y:String) -> String { var result = [String]() for i in 0...9 { result += Array(repeating: String(i), count: min(X.filter{String($0) == String(i)}.count, Y.filter {String($0) == String(i)}.count)) } return result.isEmpty ? "-1" : result.filter{$0 == "0"}.count == result.count ? "0" : result.sorted(by: >).joined() }
[Swift] 숫자 짝궁import Foundation func solution(_ X:String, _ Y:String) -> String { var result = [String]() for i in 0...9 { result += Array(repeating: String(i), count: min(X.filter{String($0) == String(i)}.count, Y.filter {String($0) == String(i)}.count)) } return result.isEmpty ? "-1" : result.filter{$0 == "0"}.count == result.count ? "0" : result.sorted(by: >).joined() }
2023.02.15 -
import Foundation func solution(_ s:String) -> Int { let answer = s.replacingOccurrences(of: "zero", with: "0") .replacingOccurrences(of: "one", with: "1") .replacingOccurrences(of: "two", with: "2") .replacingOccurrences(of: "three", with: "3") .replacingOccurrences(of: "four", with: "4") .replacingOccurrences(of: "five", with: "5") .replacingOccurrences(of: "six", with: "6") .replacingOccurren..
[Swift] 숫자 문자열과 영단어import Foundation func solution(_ s:String) -> Int { let answer = s.replacingOccurrences(of: "zero", with: "0") .replacingOccurrences(of: "one", with: "1") .replacingOccurrences(of: "two", with: "2") .replacingOccurrences(of: "three", with: "3") .replacingOccurrences(of: "four", with: "4") .replacingOccurrences(of: "five", with: "5") .replacingOccurrences(of: "six", with: "6") .replacingOccurren..
2023.02.15