/// <summary> /// 第一种方案。 /// </summary> /// <param name="a">大楼。</param> /// <returns></returns> static testResult PlanA(int[] a) { var lo = 0; var hi = a.Length - 1; var mid = 0; var eggs = 0; var result = new testResult(); while (lo <= hi) { mid = lo + (hi - lo) / 2; if (ThrowEgg(mid)) { lo = mid + 1; } else { eggs++; hi = mid - 1; } } result.BrokenEggs = eggs; result.F = hi; return(result); }
static List<testResult> parseXML(XElement mainFile) { List<testResult> ret = new List<testResult>(); List<XElement> assembly = getElements("assembly", mainFile); List<XElement> collection = getElements("collection", assembly); List<XElement> test = getElements("test", collection); foreach (var testRun in test) { testResult testResult = new testResult(); testResult.testName = testRun.Attribute("name").Value; testResult.timeElapsed = testRun.Attribute("time").Value; testResult.result = testRun.Attribute("result").Value; XElement performance = testRun.Element("performance"); testResult.runID = performance.Attribute("runid").Value; testResult.etlPath = performance.Attribute("etl").Value; XElement metrics = performance.Element("metrics"); XElement iterations = performance.Element("iterations"); if(iterations == null) { continue; } foreach(var iteration in iterations.Elements()) { string it = iteration.Attribute("index").Value; if (it == "0") continue; foreach (var metric in getMetrics(metrics)) { if (metric.unit != "list") { metric.value = iteration.Attribute(metric.name).Value; metric.iteration = it; testResult.metrics.Add(metric); } else { foreach (var ListResult in iteration.Elements("ListResult")) { if(ListResult.Attribute("Name").Value == metric.name) { testResult.ListResults.Add(ListResult); } } } } } ret.Add(testResult); } return ret; }
static void Main(string[] args) { int[] building = new int[100000]; for (int i = 0; i < 100000; i++) { building[i] = i; } // 第一问:二分查找即可 testResult A = PlanA(building); Console.WriteLine($"Plan A: F={A.F}, Broken Eggs={A.BrokenEggs}"); // 第二问:按照第 1, 2, 4, 8,..., 2^k 层顺序查找,一直到 2^k > F, // 随后在 [2^(k - 1), 2^k] 范围中二分查找。 testResult B = PlanB(building); Console.WriteLine($"Plan B: F={B.F}, Broken Eggs={B.BrokenEggs}"); }
static void Main(string[] args) { int[] building = new int[100000]; for (int i = 0; i < 100000; i++) { building[i] = i; } // 第一问:第一个蛋按照 √(N), 2√(N), 3√(N), 4√(N),..., √(N) * √(N) 顺序查找直至碎掉。这里扔了 k 次,k <= √(N) // k-1√(N) ~ k√(N) 顺序查找直至碎掉,F 值就找到了。这里最多扔 √(N) 次。 testResult A = PlanA(building); Console.WriteLine($"Plan A: F={A.F}, Broken Eggs={A.BrokenEggs}, Throw Times={A.ThrowTimes}"); // 第二问:按照第 1, 3, 6, 10,..., 1/2k^2 层顺序查找,一直到 1/2k^2 > F, // 随后在 [1/2k^2 - k, 1/2k^2] 范围中顺序查找。 testResult B = PlanB(building); Console.WriteLine($"Plan B: F={B.F}, Broken Eggs={B.BrokenEggs}, Throw Times={B.ThrowTimes}"); }
/// <summary> /// 第二种方案。 /// </summary> /// <param name="a">大楼。</param> /// <returns></returns> static testResult PlanB(int[] a) { int lo = 0; int hi = 1; int mid = 0; int eggs = 0; testResult result = new testResult(); while (ThrowEgg(hi)) { lo = hi; hi *= 2; } eggs++; if (hi > a.Length - 1) { hi = a.Length - 1; } while (lo <= hi) { mid = lo + (hi - lo) / 2; if (ThrowEgg(mid)) { lo = mid + 1; } else { eggs++; hi = mid - 1; } } result.BrokenEggs = eggs; result.F = hi; return(result); }
/// <summary> /// 第二种方案。 /// </summary> /// <param name="a">大楼。</param> /// <returns></returns> static testResult PlanB(int[] a) { var lo = 0; var hi = 0; var eggs = 0; var throwTimes = 0; var result = new testResult(); for (var i = 0; ThrowEgg(hi); i++) { throwTimes++; lo = hi; hi += i; } eggs++; if (hi > a.Length - 1) { hi = a.Length - 1; } while (lo <= hi) { if (!ThrowEgg(lo)) { eggs++; break; } lo++; throwTimes++; } result.BrokenEggs = eggs; result.F = lo - 1; result.ThrowTimes = throwTimes; return(result); }
/// <summary> /// 第一种方案。 /// </summary> /// <param name="a">大楼。</param> /// <returns></returns> static testResult PlanA(int[] a) { var lo = 0; var hi = 0; var eggs = 0; var throwTimes = 0; var result = new testResult(); while (ThrowEgg(hi)) { throwTimes++; lo = hi; hi += (int)Math.Sqrt(a.Length); } eggs++; if (hi > a.Length - 1) { hi = a.Length - 1; } while (lo <= hi) { if (!ThrowEgg(lo)) { eggs++; break; } throwTimes++; lo++; } result.BrokenEggs = eggs; result.F = lo - 1; result.ThrowTimes = throwTimes; return(result); }
public static List<testResult> RunTest(string path,Hashtable dictionary ,int dicSize, Hashtable idfTable, KNearestNeighbors knn) { List<testResult> result = new List<testResult>(); //int[] trainingAnswer = new int[17998]; int count = 0; string[] categories = Directory.GetDirectories(path); for (int i = 0; i < categories.Count(); i++) //traverse Categories { Console.WriteLine(Path.GetFileName(categories[i])); string[] file_names = Directory.GetFiles(categories[i]); for (int j = 0; j < file_names.Count(); j++) //file in Cagetory { Console.WriteLine(Path.GetFileName(file_names[j])); System.IO.StreamReader file = new System.IO.StreamReader(file_names[j]); double[] featureV = new double[dicSize]; for(int k = 0;k<dicSize;k++) //initial featureV[k] = 0; String line; int counter = 0; Hashtable docWord = new Hashtable(); Stemmer stemmer = new Stemmer(); int sumWordCount = 0; stemmer.stem(); //Console.WriteLine(stemmer.stem("running")); //String word; /******Structured Column*****/ while ((line = file.ReadLine()) != null) { //Console.WriteLine(line); if (line.Contains(": ")) { string[] splitPart = line.Split(new string[] { ": " }, StringSplitOptions.None); string columnName = splitPart[0].Trim(); string content = splitPart[splitPart.Length - 1]; if (columnName.ToLower() == "subject") { foreach (string iter_word in Regex.Split(content, @"[^A-Za-z0-9_-]")) { String word = iter_word.ToLower().Trim(new Char[] { '_', '-' }); double Num; bool isNum = double.TryParse(word, out Num); if (isNum) { continue; } stemmer.add(word.ToCharArray(), word.Length); stemmer.stem(); word = stemmer.ToString(); if (word.Length == 0) { continue; } if (stopWordTable.ContainsKey(word)) { continue; } sumWordCount += 1 * subjectWeight; // word preprocess done if (docWord.ContainsKey(word)) { int temp = (int)docWord[word]; temp += 1 * subjectWeight; docWord[word] = temp; } else { docWord[word] = 1 * subjectWeight; } } } /*else if (columnName.ToLower() == "keywords") { foreach (string iter_word in Regex.Split(content, @"[^A-Za-z0-9_-]")) { String word = iter_word.ToLower().Trim(new Char[] { '_', '-' }); double Num; bool isNum = double.TryParse(word, out Num); if (isNum) { continue; } stemmer.add(word.ToCharArray(), word.Length); stemmer.stem(); word = stemmer.ToString(); if (word.Length == 0) { continue; } if (stopWordTable.ContainsKey(word)) { continue; } sumWordCount += 1 * keywordsWeight; // word preprocess done if (docWord.ContainsKey(word)) { int temp = (int)docWord[word]; temp += 1 * keywordsWeight; docWord[word] = temp; } else { docWord[word] = 1 * keywordsWeight; } } } if (columnName.ToLower() == "newsgroups") { foreach (string iter_word in content.Split(new char[] { ',' })) { String word = iter_word.ToLower().Trim(); sumWordCount += 1 * newsgroupsWeight; // word preprocess done if (docWord.ContainsKey(word)) { int temp = (int)docWord[word]; temp += 1 * newsgroupsWeight; docWord[word] = temp; } else { docWord[word] = 1 * newsgroupsWeight; } } }*/ /*else if (columnName.ToLower() == "from") { Regex emailRegex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase); //find items that matches with our pattern MatchCollection emailMatches = emailRegex.Matches(content); foreach (Match emailMatch in emailMatches) { String word = emailMatch.Value; // word preprocess done if (docWord.ContainsKey(word)) { int temp = (int)docWord[word]; temp += 1 * fromWeight; docWord[word] = temp; } else { docWord[word] = 1 * fromWeight; } } }*/ } else { break; } } /******Text******/ while ((line = file.ReadLine()) != null) { if (line.StartsWith(">") || line.StartsWith("|>")) { continue; } //foreach(string iter_word in line.Split(new Char [] {' ', ',', '.', ':', '\t', '\n' })) foreach (string iter_word in Regex.Split(line, @"[^A-Za-z0-9_-]")) { String word = iter_word.ToLower().Trim(new Char[] { '_', '-' }); double Num; bool isNum = double.TryParse(word, out Num); if (isNum) { continue; } stemmer.add(word.ToCharArray(), word.Length); stemmer.stem(); word = stemmer.ToString(); if (word.Length == 0) { continue; } if (stopWordTable.ContainsKey(word)) { continue; } sumWordCount++; // word preprocess done if (docWord.ContainsKey(word)) { int temp = (int)docWord[word]; temp++; docWord[word] = temp; } else { docWord[word] = 1; } } }// line end foreach (string word in docWord.Keys) { if (dictionary.ContainsKey(word)) { int indexOfDic = (int)dictionary[word]; double TF = System.Convert.ToDouble((int)docWord[word])/System.Convert.ToDouble(sumWordCount); double IDF = (double)idfTable[word]; featureV[indexOfDic] = TF * IDF; } } testResult resultTemp = new testResult(); resultTemp.docName = Path.GetFileName(file_names[j]); resultTemp.oriClass = i; resultTemp.resultClass = knn.Compute(featureV); result.Add(resultTemp); Console.WriteLine(resultTemp.resultClass); }//file end //Console.ReadLine(); }//category end return result; }