func solution(_ absolutes:[Int], _ signs:[Bool]) -> Int {
zip(absolutes, signs)
.map { $1 ? $0 : -$0 }
.reduce(0, +)
}
func someMethod(_ arr: [Int]) {}
let arr = [1, 2, 3, 4].prefix(3)
someMethod(Array(arr)) // ✅
let numbers = [1, 2, 3, 4, 5]
print(numbers.prefix(2))
// Prints "[1, 2]"
print(numbers.prefix(10))
// Prints "[1, 2, 3, 4, 5]"
var value = 100
var result = ""
result = String(value, radix: 2)
print(result) // 1100100
var value = "1100100"
var result: Int = Int(value, radix: 2)!
print(result) // 100
var value = "1100100"
var result: Int = Int(value, radix: 2)!
var newResult: String = String(result, radix: 16)
print(newResult) // c9
2진수 -> 10진수 -> 16진수
(index, value) 튜플형식으로 구현된 리스트형 리턴
for (n, c) in "Swift".enumerated() {
print("\(n): '\(c)'")
}
// Prints "0: 'S'"
// Prints "1: 'w'"
// Prints "2: 'i'"
// Prints "3: 'f'"
// Prints "4: 't'"
let arr = ["one", "two", "three"]
print(arr.enumerated()) // EnumeratedSequence<Array<String>>(_base: ["one", "two", "three"])
for (index, number) in arr.enumerated() {
print("\(index), \(number)")
}
/*
0, one
1, two
2, three
*/
let s = "String"
let asciis = Array(s).map { Int($0.asciiValue!) }
let s = "String"
let asciis = s.utf8.map { Int($0) }
let s = "String"
let asciis = Array(s).map { Int($0.asciiValue!) }
let str = asciis.map { String(UnicodeScalar($0)) }.joined()
func solution(_ s:String, _ n:Int) -> String {
var asciis = Array(s).map { Int($0.asciiValue!) }
return asciis.map {
var ascii = $0
switch ascii {
case 65...90:
ascii = (ascii + n - 65) % 26 + 65
case 97...122:
ascii = (ascii + n - 97) % 26 + 97
default:
break
}
return String(UnicodeScalar(ascii)!)
}.joined()
}
func solution(_ n:Int, _ arr1:[Int], _ arr2:[Int]) -> [String] {
return (0..<n).map { String(String(arr1[$0]|arr2[$0]|2<<(n - 1), radix: 2).map { $0 == "1" ? "#" : " " }[1...n]) }
}
func solution(_ n:Int, _ arr1:[Int], _ arr2:[Int]) -> [String] {
return (0..<n).map {
let binary = String(arr1[$0] | arr2[$0], radix: 2)
let padded = String(repeating: "0", count: n - binary.count) + binary
return padded.reduce("") { $0 + ($1 == "0" ? " " : "#") }
}
}
for i in food.indices {
result.append(String(repeating: String(i), count: food[i] / 2))
}
let numberList = dartResult.split(whereSeparator: {$0.isLetter || $0 == "#" || $0 == "*"})
let letterList = dartResult.split(whereSeparator: {$0.isNumber})
let dict = Dictionary(grouping: tangerine) { $0 }
print((dict.mapValues { $0.count } )
let dict = Dictionary(grouping: tangerine) { $0 }
print((dict.mapValues { $0.count } )
func getWeapon(num: Int) -> Int {
var count: Int = 0
var i: Int = 1
while i * i <= num {
if num % i == 0 {
if i * i == num {
count += 1
} else {
count += 2
}
}
i += 1
}