public void Remove(int startIndex, int numCharsToRemove) { LinkedList<char> newLinkedList = new LinkedList<char>(); int total = linkedList.Count; int baseIndexCount = 0; List<char> charList = new List<char>(); foreach (char c in linkedList) { charList.Add(c); } for (int index = 0; index < total; index++) { if (index == startIndex) { for (int count = 0; count < numCharsToRemove; count++) { index++; } } newLinkedList.AddLast(charList[index]); baseIndexCount++; } linkedList = newLinkedList; PrintEachItem(); }
public void Insert(string inputString, int insertPosition) { char[] inputCharArray = inputString.ToCharArray(); LinkedList<char> newLinkedList = new LinkedList<char>(); int total = inputCharArray.Length + linkedList.Count; List<char> charList = new List<char>(); foreach(char c in linkedList) { charList.Add(c); } int baseIndexCounter = 0; for (int index = 0; index < total; index++) { if(index == insertPosition) { foreach(char nextChar in inputCharArray) { newLinkedList.AddLast(nextChar); index++; } } newLinkedList.AddLast(charList[baseIndexCounter]); baseIndexCounter++; } linkedList = newLinkedList; PrintEachItem(); }
// public static void Main(){ genericList(); } private static void genericList() { List<string> strs = new List<string>(); strs.Add("one"); strs.Add("two"); strs.Add("three"); strs.Add("four"); //all in one for(int i=0; i<strs.Count; i++){ con.Write( strs[i] + "." ); } //foreach foreach(string str in strs){ Console.Write(str + ","); } }
static void Main(string[] args) { // TestMoveBit(); // Test1(); // Test List<T> ArrayList myArrayList = new ArrayList(); myArrayList.Add(3); List<int> myInt = new List<int>(); myInt.Add(3); Console.ReadKey(); }
//Cities data public static List<Cities> CityData() { var seattle = new Cities(); List<Cities> cities = new List<Cities>(); seattle.CityName = "Seattle, WA"; seattle.CityPopulation = 652405; var redmond = new Cities(); redmond.CityName = "Redmond, WA"; redmond.CityPopulation = 57530; var spokane = new Cities(); spokane.CityName = "Spokane, WA"; spokane.CityPopulation = 208916; var bellevue = new Cities(); bellevue.CityName = "Bellevue, MO"; bellevue.CityPopulation = 200123; var everette = new Cities(); everette.CityName = "Everette, WA"; everette.CityPopulation = 133992; var olympia = new Cities(); olympia.CityName = "Olympia, WA"; olympia.CityPopulation = 48338; var tacoma = new Cities(); tacoma.CityName = "Tacoma, WA"; tacoma.CityPopulation = 133992; var ellensburge = new Cities(); ellensburge.CityName = "Ellensburge, WA"; ellensburge.CityPopulation = 18111; cities.Add(seattle); cities.Add(redmond); cities.Add(spokane); cities.Add(bellevue); cities.Add(everette); cities.Add(olympia); cities.Add(tacoma); cities.Add(ellensburge); return cities; }
/// <summary> /// this method creates a list of internal filters (without the GUI part) /// </summary> /// <returns>List of simplyfied filters</returns> public List<InternalFilter> ConvertFiltersToInternal() { List<InternalFilter> list = new List<InternalFilter>(); foreach (Filter f in allFilter) { if (f.GetCategory() != null) { list.Add(new InternalFilter(f.GetCategory(), f.GetSearch1(), f.GetSearch2())); } } return list; }
public char[] GetAllChars() { char[] allCharacters = new char[Length()]; List<Node> allNodes = new List<Node>(); Node cur = head; while (cur.next != null) { allNodes.Add(cur); cur = cur.next; } allNodes.Add(cur); for (int i = 0; i < allCharacters.Length; i++) { allCharacters[i] = allNodes[i].data; } return allCharacters; }
static void Main() { var start = new Point(9, 3); var end = new Point(1, 8); Func<Point, char> getCell = GetCell(MAZE); Func<Point, IEnumerable<Point>> getNeighbours = GetNeighbours(getCell); Func<Point, Point, int> getCost = (from, to) => getCell(to) == '@' ? 4:1 ; Func<Point, int> manhattanHeuristic = (to) => Math.Abs(to.X - end.X) + Math.Abs(to.Y - end.Y); Func<Point, double> manhattanHeuristicD = (to) => Math.Abs(to.X - end.X) + Math.Abs(to.Y - end.Y); var millisecondsTimeout = 100; var algorithms = new Dictionary<int, Tuple<string, Func<IEnumerable<Point>>, Func<IEnumerable<Point>>>> { {1, "Depth First Search", () => DepthFirstSearch.Explore(start, getNeighbours),()=>DepthFirstSearch.FindPath(start,getNeighbours,p => p.Equals(end) ) }, {2, "Breadth First Search", () => BreadthFirstSearch.Explore(start, getNeighbours) ,()=>BreadthFirstSearch.FindPath(start,getNeighbours,p => p.Equals(end) )}, {3, "Dijkstra", () => Dijkstra.Explore(start, getNeighbours,getCost ) ,()=>Dijkstra.FindPath(start,getNeighbours,getCost,p => p.Equals(end) )}, {4, "Greedy Best-First Search (with manhattan)", () => GreedyBestFirstSearch.Explore(start, getNeighbours, manhattanHeuristic),()=>GreedyBestFirstSearch.FindPath(start,getNeighbours,manhattanHeuristic,p => p.Equals(end) )}, {5, "A* (with manhattan)", () => AStar.Explore(start, getNeighbours,getCost, manhattanHeuristicD),()=>AStar.FindPath(start,getNeighbours,getCost,manhattanHeuristicD,p => p.Equals(end) )}, }; int choice = 0; while (choice != 10) { Console.Clear(); Console.WriteLine("Choose traversal algorithm :"); foreach (var algorithm in algorithms) { Console.WriteLine($"\t{algorithm.Key} - {algorithm.Value.Item1}"); } while (choice == 0) { int.TryParse(Console.ReadLine(), out choice); } var currentMaze = MAZE; var result = algorithms[choice].Item2(); var path = algorithms[choice].Item3().ToList(); var visited = new List<Point>(); foreach (var item in result) { visited.Add(item); var lines = currentMaze.Split(new[] { Environment.NewLine }, StringSplitOptions.None); var line = lines[item.Y].ToCharArray(); line[item.X] = '*'; lines[item.Y] = new string(line); currentMaze = string.Join(Environment.NewLine, lines); DisplayMaze(currentMaze, visited.Count, 0,0, algorithms[choice].Item1); Thread.Sleep(millisecondsTimeout); if (item == end) break; } foreach (var item in path) { var lines = currentMaze.Split(new[] { Environment.NewLine }, StringSplitOptions.None); var line = lines[item.Y].ToCharArray(); line[item.X] = '+'; lines[item.Y] = new string(line); currentMaze = string.Join(Environment.NewLine, lines); } var pathcost = path.Aggregate(0, (current, point) => current + getCost(Point.Empty, point)); DisplayMaze(currentMaze, visited.Count(), path.Count(),pathcost, algorithms[choice].Item1); choice = 0; Console.WriteLine(); Console.WriteLine($"Press any key to reset"); Console.Read(); } }
private void RestoreUtil(string s, string resultstr, int start, int count, List<string> results) { int maxlen = 3; if (count == 3) { string ip = s.Substring(start, s.Length - start); if (IsValid(ip)) { resultstr += ip; results.Add(resultstr); } return; } for (int i = 0; i < maxlen; i++) { string ip = s.Substring(start, i + 1); if (IsValid(ip)) { //resultstr += ip + "."; RestoreUtil(s, resultstr + ip + ".", start + i + 1, count + 1, results); //resultstr = resultstr.Remove(resultstr.Length - ip.Length); } } }
private void GetParentheses(List<string> results, string oneResult, int number, int leftNumber, int rightNumber) { if (leftNumber == number && rightNumber == number) { results.Add(oneResult); return; } if (leftNumber < number) { oneResult += "("; GetParentheses(results, oneResult, number, leftNumber + 1, rightNumber); } if (rightNumber < number && rightNumber < number) { oneResult += ")"; GetParentheses(results, oneResult, number, leftNumber, rightNumber + 1); } }
private void GenerateSum(List<string> results, int[] input, string s, int sum) { for (int i = 0; i < input.Length; i++) { if (s.Length > 0 && input[i] < (s[s.Length - 1] - '0')) { continue; } if (sum - input[i] == 0) { s += " " + input[i].ToString(); results.Add(s); return; } else if (sum - input[i] > 0) { s += " " + input[i].ToString(); GenerateSum(results, input, s, sum - input[i]); } else { s = String.Empty; return; } } }
private void FindAllSumPathRecursive(TreeNode<int> node, List<string> results, Stack<int> stack, int sum, int currentSum) { if (node == null) { return; } if (node.LeftNode == null && node.RightNode == null) { if (currentSum + node.Value == sum) { int[] array = new int[stack.Count]; stack.CopyTo(array, 0); results.Add(node.Value + " " + string.Join(" ", array)); } return; } stack.Push(node.Value); FindAllSumPathRecursive(node.LeftNode, results, stack, sum, currentSum + node.Value); FindAllSumPathRecursive(node.RightNode, results, stack, sum, currentSum + node.Value); stack.Pop(); }
private List<string> Perm(string a, string c) { if (c.Length == 1) { return new List<string>() {a + c}; } List<string> tempResults = new List<string>(); for (int i = 0; i < c.Length; i++) { string temp = c; tempResults.AddRange(Perm(c.Substring(i, 1), temp.Remove(i, 1))); } List<string> results = new List<string>(); tempResults.ForEach(i => results.Add(a + i)); return results; }
public static List<int> MergeTwoSortedList(List<int> list1, List<int> list2) { if(list1 == null || list1.Count == 0) { return (list2 == null || list2.Count == 0) ? new List<int>() : list2; } else if (list2 == null || list2.Count == 0) { return (list1 == null || list1.Count == 0) ? new List<int>() : list1; } List<int> newList = new List<int>(); int index1 = 0, index2 = 0; while (index1 < list1.Count && index2 < list2.Count) { newList.Add((list1[index1] <= list2[index2]) ? list1[index1++] : list2[index2++]); } if (index1 >= list1.Count) { newList.AddRange(list2.GetRange(index2, list2.Count-index2)); } else { newList.AddRange(list1.GetRange(index1, list1.Count - index1)); } return newList; }