Created
July 10, 2017 08:07
-
-
Save JuneBuug/59b1a70b3f78d7728b982a3ff23cef86 to your computer and use it in GitHub Desktop.
Swift playground
This file contains 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
//: Playground - noun: a place where people can play | |
import UIKit | |
import Foundation | |
// 첫번째 | |
func distance(ax : Int, ay: Int, bx: Int, by: Int) -> Float{ | |
let yDistance = Float(abs(ay - by)) | |
let xDistance = Float(abs(ax - bx)) | |
return sqrt(pow(xDistance,2) + pow(yDistance,2)) | |
} | |
var result1 = distance(ax : 1 , ay : 1, bx : 5, by: 6) | |
print(result1) | |
// 두번째 | |
typealias Point = ( x: Int, y: Int) | |
func distance(a : Point, b: Point) -> Double{ | |
let yDistance = Double(abs(a.y - b.y)) | |
let xDistance = Double(abs(a.x - b.x)) | |
return sqrt(pow(xDistance,2) + pow(yDistance,2)) | |
} | |
distance(a:(1,1) , b:(5,6)) | |
// 세번째 | |
func age(_ from: String) -> Int { | |
let index = from.index(from.startIndex, offsetBy:2) | |
var year = Int(from[Range(from.startIndex ..< index)]) | |
let monthIndex = from.index(from.startIndex, offsetBy: 4) | |
var month = Int(from[Range( index ..< monthIndex)]) | |
var day = Int(from.substring(from: monthIndex)) | |
if(year != nil && month != nil && day != nil){ | |
year = year! + 1900 | |
if( month! >= 7 ){ | |
return 2017 - year! - 1 | |
} else { | |
return 2017 - year! | |
} | |
} | |
return -1; | |
} | |
print(age("940613")) | |
// 네번째 | |
func enumerateList(_ A: [Int]){ | |
var length = 0 | |
var index = 0 | |
while index != -1 { | |
print(A[index]); | |
index = A[index] | |
length += 1 | |
} | |
} | |
func countOfList(_ A: [Int]) -> Int{ | |
var length = 0 | |
var index = 0 | |
while index != -1 { | |
index = A[index] | |
length += 1 | |
} | |
return length | |
} | |
func isFullList(_ A: [Int]) -> Bool{ | |
if countOfList(A) < A.count { | |
return false | |
} | |
else { | |
var aSet : Set = [0] | |
var index = 0 | |
while index != -1 { | |
index = A[index] | |
aSet.insert(index) | |
} | |
for index in 0..<A.count { | |
if( !aSet.contains(index)){ | |
return false | |
} | |
} | |
return true | |
} | |
} | |
enumerateList([1,4,-1,3,2]) | |
print(countOfList([1,4,-1,3,2])) | |
print(isFullList([1,4,3,-1,2])) | |
// 5번째 | |
func complex(from array: [Int]) -> Int{ | |
let selected = array.filter({ $0 % 2 == 0 || $0 % 3 == 0 }) | |
let multiply5 = selected.map({ $0 * 5 }) | |
return multiply5.reduce(0,{ $0 + $1 } ); | |
} | |
complex(from: [1,3,4,6]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment