fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class Program
  7. {
  8. public static TableClass _MyTable = new TableClass();
  9.  
  10. public class TableRow
  11. {
  12. public int ID;
  13. public string Name;
  14. public string Hair;
  15. public string Eyes;
  16.  
  17. public TableRow(int _ID, string _Name, string _Hair, string _Eyes)
  18. {
  19. ID = _ID;
  20. Name = _Name;
  21. Hair = _Hair;
  22. Eyes = _Eyes;
  23. }
  24. }
  25.  
  26. public class TableClass
  27. {
  28. public List<TableRow> Table = new List<TableRow>();
  29. }
  30.  
  31. static void Main(string[] args)
  32. {
  33. // ID Name Hair Eyes
  34. // 1 John Black Brown
  35. // 2 Paul Brown Green
  36. // 3 Ringo Blond Blue
  37. // 4 George Red Blue
  38. // 5 John * *
  39. // 6 John * Brown
  40. // 7 * Red *
  41. // 8 * * *
  42. // 9 Paul * *
  43.  
  44. CreateTable();
  45.  
  46. ShowTable();
  47.  
  48. FindID("John", "Black", "Brown"); // should return 1, complete match
  49.  
  50. FindID("John", "Red", "Green"); // should return 5 (not 7), matched on "John" and the other two wildcards (*)
  51.  
  52. FindID("John", "Red", "Brown"); // should return 6, matched on "John" and "Brown" and one wildcard
  53.  
  54. FindID("Brian", "Grey", "Grey"); // should return 8, matched on three wildcard
  55.  
  56.  
  57. }
  58.  
  59. static bool match(TableRow row, string _Name, string _Hair, string _Eyes)
  60. {
  61. return (row.Name == _Name || row.Name == "*") && (row.Hair == _Hair || row.Hair == "*") && (row.Eyes == _Eyes || row.Eyes == "*");
  62. }
  63.  
  64. static int score(TableRow row)
  65. {
  66. //can assume these already "match"
  67. var score = 0;
  68. score += row.Name == "*" ? 1000 : 2000;
  69. score += row.Hair == "*" ? 100 : 200;
  70. score += row.Eyes == "*" ? 10 : 20;
  71. return score;
  72. }
  73.  
  74. static int FindID(string _Name, string _Hair, string _Eyes)
  75. {
  76. // needs to be implemented
  77. var s = from row in _MyTable.Table
  78. where match(row, _Name, _Hair, _Eyes)
  79. orderby score(row) descending
  80. select row;
  81.  
  82.  
  83. Console.WriteLine(s.First().ID);
  84. return 0;
  85. }
  86.  
  87. static void CreateTable()
  88. {
  89. _MyTable.Table.Add(new TableRow(1, "John", "Black", "Brown"));
  90. _MyTable.Table.Add(new TableRow(2, "Paul", "Brown", "Green"));
  91. _MyTable.Table.Add(new TableRow(3, "Ringo", "Blond", "Blue"));
  92. _MyTable.Table.Add(new TableRow(4, "George", "Red", "Blue"));
  93. _MyTable.Table.Add(new TableRow(5, "John", "*", "*"));
  94. _MyTable.Table.Add(new TableRow(6, "John", "*", "Brown"));
  95. _MyTable.Table.Add(new TableRow(7, "*", "Red", "*"));
  96. _MyTable.Table.Add(new TableRow(8, "*", "*", "*"));
  97. _MyTable.Table.Add(new TableRow(9, "Paul", "*", "*"));
  98. }
  99.  
  100. static void ShowTable()
  101. {
  102. foreach(TableRow _TableRow in _MyTable.Table)
  103. {
  104. Console.WriteLine("{0} {1} {2} {3}", _TableRow.ID, _TableRow.Name, _TableRow.Hair, _TableRow.Eyes);
  105. }
  106. }
  107. }
Success #stdin #stdout 0.05s 34120KB
stdin
Standard input is empty
stdout
1 John Black Brown
2 Paul Brown Green
3 Ringo Blond Blue
4 George Red Blue
5 John * *
6 John * Brown
7 * Red *
8 * * *
9 Paul * *
1
5
6
8