Commit b6875680 authored by Ilya Prokhorov's avatar Ilya Prokhorov

Swift fixed

parent 43aa0478
This source diff could not be displayed because it is too large. You can view the blob instead.
import Foundation
let pipe = FileHandle.standardInput
let data = pipe.readDataToEndOfFile()
let inputs = String(data: data, encoding: .utf8)?.components(separatedBy: "\n")
let url = URL(fileURLWithPath: "../../resources/whitelist.json")
let data = try? Data(contentsOf: url)
let json = try? JSONDecoder().decode([String : [String]].self, from: data!)
let sortedWhitelist =
["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"]
let sortedWhitelist = json!["whiteListSorted"]!
func search(_ word: String, _ sortedWhitelist: [String], _ lowIndex: Int, _ highIndex: Int) {
if (highIndex <= lowIndex) {
return;
}
let middleIndex = lowIndex + (highIndex - lowIndex) / 2;
let middleWhitelistedWord = sortedWhitelist[middleIndex];
let compareResult = middleWhitelistedWord.compare(word);
var highIndex = highIndex
var lowIndex = lowIndex
if (compareResult.rawValue > 0) {
search(word, sortedWhitelist, lowIndex, middleIndex);
}
else if (compareResult.rawValue < 0) {
search(word, sortedWhitelist, middleIndex + 1, highIndex);
while (lowIndex <= highIndex) {
let middleIndex = (lowIndex + highIndex) / 2
let middleWhitelistedWord = sortedWhitelist[middleIndex]
let compareResult = middleWhitelistedWord.compare(word).rawValue
if (compareResult == 0) {
print("Found")
print(word)
return;
}
else if (compareResult < 0) {
lowIndex = middleIndex + 1
}
else {
highIndex = middleIndex - 1
}
}
else {
debugPrint("Found");
debugPrint(word);
}
}
inputs?.forEach {
search($0, sortedWhitelist, 0, sortedWhitelist.count - 1)
}
search(CommandLine.arguments[1], sortedWhitelist, 0, sortedWhitelist.count - 1)
../../generatorTestCase.sh | ./binarySearch > /dev/null
../../generatorTestCase.sh | ./naiveSearch > /dev/null
import Foundation
let url = URL(fileURLWithPath: "../../resources/whitelist.json")
let data = try? Data(contentsOf: url)
let json = try? JSONDecoder().decode([String : [String]].self, from: data!)
let whiteList = json?["whiteList"]
func search(_ word: String) {
whiteList?.forEach {
if $0 == word {
print("Found")
print(word)
return;
}
}
}
search(CommandLine.arguments[1])
import Foundation
let pipe = FileHandle.standardInput
let data = pipe.readDataToEndOfFile()
let inputs = String(data: data, encoding: .utf8)?.components(separatedBy: "\n")
let whitelist = [ "[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
]
func search(_ word: String) {
whitelist.forEach {
if $0 == word {
debugPrint("Found")
debugPrint(word)
return;
}
}
}
inputs?.forEach {
search($0)
}
node sorter.js > /dev/null
./sorter > /dev/null
var fs = require('fs')
fs.readFile("../../resources/whitelist.json", 'utf8', function(error, data) {
if (error) {
throw error;
}
var whiteListContainer = JSON.parse(data);
var whiteList = whiteListContainer.whiteList;
console.log("Original whitelist:");
console.log(whiteList);
var whiteListSorted = [...whiteList].sort(function(lhs, rhs){ return lhs.localeCompare(rhs) });
console.log("After JS sorting whitelist:");
console.log(whiteListSorted);
whiteListContainer.whiteListSorted = whiteListSorted;
fs.writeFile("../../resources/whitelist.json", JSON.stringify(whiteListContainer), 'utf8', function (error) {
if (error) {
throw error;
}
})
});
import Foundation
let url = URL(fileURLWithPath: "../../resources/whitelist.json")
let data = try? Data(contentsOf: url)
var json = try? JSONDecoder().decode([String : [String]].self, from: data!)
let whiteList = json!["whiteList"]!
print(whiteList)
var whiteListSorted = whiteList.sorted(by: { $0.compare($1).rawValue < 0 });
print(whiteListSorted)
json!["whiteListSorted"] = whiteListSorted
let jsonData = try JSONEncoder().encode(json)
print(jsonData)
try? jsonData.write(to: url)
cd NaiveSearch
/home/demensdeum/Apps/swift5/usr/bin/swiftc naiveSearch.swift
cd LinearSearch
/home/demensdeum/Apps/swift5/usr/bin/swiftc linearSearch.swift
cd ..
cd BinarySearch
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment