-
Notifications
You must be signed in to change notification settings - Fork 0
/
HackerRank-ProductTop3.swift
62 lines (48 loc) · 1.64 KB
/
HackerRank-ProductTop3.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// main.swift
// ProductTop3
//
// Created by Jamshed on 17/12/19.
// Copyright © 2019 Jamshed. All rights reserved.
//
import Foundation
import Cocoa
let firstLine = readLine()
let secondLine = readLine()
let whitespace = NSCharacterSet.whitespaces
let phrase = firstLine
if (firstLine?.contains(" "))!{ // if user entry space with limit or something
print("-1")
} else { // for valid entry
let fullInputArr = secondLine!.split{$0 == " "}.map(String.init)
var fullInputArrInt = [Int]()
var count = 0
for currentInput in fullInputArr {
if let myInt = Int(currentInput) {
fullInputArrInt.append(myInt)
count = count + 1
} else {
break
}
}
if fullInputArr.count < 3 || count != Int(firstLine!) {
print("-1")
} else {
var numberArray: [Any] = fullInputArr.sorted()
/// - Parameters:
/// - numbers: array of anything
/// - Returns: sorted _Double_ array
func sortAnyNumbers(_ numbers: [Any]) -> [Int] {
var _numbers: [Int] = []
numbers.forEach { number in
if let numb = Int("\(number)".components(separatedBy: CharacterSet(charactersIn: "-01234567890.").inverted).joined()) {
_numbers.append(Int(numb))
}
}
return _numbers.sorted()
}
let sortedArray = sortAnyNumbers(numberArray)
let totalProduct = Int(sortedArray[sortedArray.count-1]) * Int(sortedArray[sortedArray.count-2]) * Int(sortedArray[sortedArray.count-3])
print(totalProduct)
}
}