Last active
April 14, 2018 19:35
-
-
Save eXhausted/0e13bc0d8f45f2eb8158463bd9e8a046 to your computer and use it in GitHub Desktop.
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 Cocoa | |
func loadHTML() -> String { | |
let bundle = Bundle.main | |
let path = bundle.path(forResource: "File", ofType: nil) | |
let data = try! Data(contentsOf: URL(fileURLWithPath: path!)) | |
return String(data: data, encoding: .utf8)! | |
} | |
var cols = loadHTML() | |
extension String { | |
var en_clean: String { | |
var replaceIt = ["<div class=\"cols-wrapper\">"] | |
replaceIt.append("<p>") | |
replaceIt.append("<div class=\"cols\">") | |
replaceIt.append("</div>") | |
replaceIt.append("</span>") | |
replaceIt.append("</p>") | |
replaceIt.append("<!--end cols-->") | |
replaceIt.append("<span class=\"color_dis\">") | |
replaceIt.append("<span class=\"color_correct\">") | |
return replaceIt.reduce(self) { $0.replacingOccurrences(of: $1, with: "") } | |
} | |
var en_remove_digit: String { | |
if let last = self.last, Int(String(last)) != nil { | |
return String(dropLast()).en_remove_digit | |
} else { | |
return self | |
} | |
} | |
} | |
var clean = cols.en_clean | |
var a: [String] = clean.split(separator: "\n").map(String.init) | |
func mapIt(_ arg :String) -> (String, Bool) { | |
let split = arg.split(separator: ":") | |
return (String(split[0]).en_remove_digit, !split[1].contains("код не введён")) | |
} | |
let b = a.map(mapIt) | |
var sum = [String: Int]() | |
let c = b.reduce(into: sum) { (result, pair) in | |
if pair.1 { | |
if result[pair.0] == nil { | |
result[pair.0] = 1 | |
} else { | |
result[pair.0] = result[pair.0]! + 1 | |
} | |
} | |
} | |
func reduce(_ arg: [String]) -> String { | |
return arg.reduce("") { $0 + "\n" + $1 } | |
} | |
let d = c.map({ (key, value) -> String in | |
return "\(key)" | |
}) | |
let e = c.map({ (key, value) -> String in | |
return "\(value)" | |
}) | |
print(reduce(d)) | |
print(reduce(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment