static int GetReverseSwapCount(int[] arr) { int[] arrCopy = new int[arr.Length]; int[] reverseSortedArray = new int[arr.Length]; Stack <int> previousIndexPosition = new Stack <int>(); Dictionary <int, int> sortedIndexCompare = new Dictionary <int, int>(); int swapCount = 0; Array.Copy(arr, arrCopy, arr.Length); Array.Copy(arr, reverseSortedArray, arr.Length); Array.Sort(reverseSortedArray); Array.Reverse(reverseSortedArray); for (int i = 0; i < reverseSortedArray.Length; i++) { sortedIndexCompare.Add(reverseSortedArray[i], i); } for (int i = 0; i < arrCopy.Length; i++) { int sortedIndex = sortedIndexCompare[arrCopy[i]]; if (i != sortedIndex) { int tmp = arrCopy[sortedIndex]; arrCopy[sortedIndex] = arrCopy[i]; arrCopy[i] = tmp; previousIndexPosition.Push(i); if (i != arrCopy.Length) { i = sortedIndex - 1; } swapCount++; } else if (previousIndexPosition.Count() > 0) { if (previousIndexPosition.First() != 0) { i = previousIndexPosition.First() - 1; } else { i = previousIndexPosition.First(); } previousIndexPosition.Clear(); } } return(swapCount); }
public static void task_3(string input) { Dictionary <char, char> bracketPairs = new Dictionary <char, char>() { { '(', ')' }, { '{', '}' }, { '[', ']' }, }; bool notBalanced = false; Stack <char> brackets = new Stack <char>(); foreach (char c in input) { if (bracketPairs.Keys.Contains(c)) { brackets.Push(c); } else { if (bracketPairs.Values.Contains(c)) { if (c == bracketPairs[brackets.First()]) { brackets.Pop(); } else { Console.WriteLine(input + " is not balanced"); notBalanced = true; break; } } } } if (brackets.Count() == 0) { Console.WriteLine(input + " is balanced"); } if (brackets.Count > 0 && notBalanced == false) { Console.WriteLine(input + " is not balanced"); } }
static void far() { //for(int i = 0;i < ) Console.CursorVisible = false; Stack<FileSystemInfo[]> parent = new Stack<FileSystemInfo[]>(); Stack<int> indexhist = new Stack<int>(); string[] drives = Environment.GetLogicalDrives(); FileSystemInfo[] cur = new FileSystemInfo[drives.Length]; for (int i = 0; i < cur.Length; i++) cur[i] = new DirectoryInfo(drives[i]); int index = 0; Show(cur, index); String moveString = "", moveName = "", moveStringTo = "", copyName = "", copyString = "", copyStringTo = ""; int k = 1; ConsoleKeyInfo pressed = Console.ReadKey(true); while (pressed.Key != ConsoleKey.Escape) { switch (pressed.Key) { case ConsoleKey.F2: { String pathString = cur[index].FullName; pathString = pathString.Substring(0, pathString.Length - cur[index].Name.Length); Console.Clear(); Console.WriteLine("Write the new File or Directory name"); string newName = Console.ReadLine(); try { Directory.Move(cur[index].FullName, pathString + newName); } catch { File.Move(cur[index].FullName, pathString + newName); } DirectoryInfo dir = parent.First()[indexhist.First()] as DirectoryInfo; cur = dir.GetFileSystemInfos(); } break; case ConsoleKey.F4: { Console.Clear(); Console.WriteLine("Write Directory Name: "); Console.CursorVisible = true; String newDirectory = Console.ReadLine(); String pathCreate = cur[index].FullName; pathCreate = pathCreate.Substring(0, pathCreate.Length - cur[index].Name.Length); string pathString = Path.Combine(pathCreate, newDirectory); Directory.CreateDirectory(pathString); DirectoryInfo dir = parent.First()[indexhist.First()] as DirectoryInfo; cur = dir.GetFileSystemInfos(); } break; case ConsoleKey.F5: { Console.Clear(); Console.WriteLine("Write File Name: "); Console.CursorVisible = true; String newFile = Console.ReadLine(); String pathCreate = cur[index].FullName; pathCreate = pathCreate.Substring(0, pathCreate.Length - cur[index].Name.Length); string pathString = Path.Combine(pathCreate, newFile); File.Create(pathString); DirectoryInfo dir = parent.First()[indexhist.First()] as DirectoryInfo; cur = dir.GetFileSystemInfos(); } break; case ConsoleKey.M: { moveString = cur[index].FullName; moveName = cur[index].Name; } break; case ConsoleKey.T: if (moveString.Length != 0) { moveStringTo = cur[index].FullName; moveStringTo = moveStringTo.Substring(0, moveStringTo.Length - cur[index].Name.Length); moveStringTo += moveName; try { Directory.Move(moveString, moveStringTo); } catch { File.Move(moveString, moveStringTo); } moveString = ""; moveStringTo = ""; DirectoryInfo dir = parent.First()[indexhist.First()] as DirectoryInfo; cur = dir.GetFileSystemInfos(); } break; case ConsoleKey.C: { copyName = cur[index].Name; copyString = cur[index].FullName; } break; case ConsoleKey.V: { copyStringTo = cur[index].FullName; copyStringTo = copyStringTo.Substring(0, copyStringTo.Length - cur[index].Name.Length); copyStringTo += copyName; if (copyString != copyStringTo) { File.Copy(copyString, copyStringTo); } else { copyStringTo += " (" + k + ")"; k++; File.Copy(copyString, copyStringTo); } //Show(cur, index); DirectoryInfo dir = parent.First()[indexhist.First()] as DirectoryInfo; cur = dir.GetFileSystemInfos(); } break; case ConsoleKey.Delete: { try { File.Delete(cur[index].FullName); } catch { Directory.Delete(cur[index].FullName); } DirectoryInfo dir = parent.First()[indexhist.First()] as DirectoryInfo; cur = dir.GetFileSystemInfos(); //File.Delete(cur[index].FullName) || Directory.Delete(cur[index].FullName); k = 1; } break; case ConsoleKey.DownArrow: if (cur.Length > index + 1) index++; else index = 0; break; case ConsoleKey.UpArrow: if (index > 0) { index--; } else index = cur.Length - 1; break; case ConsoleKey.LeftArrow: if (parent.Count > 0) { index = indexhist.Pop(); cur = parent.Pop(); } break; case ConsoleKey.O: { try { Bitmap m = new Bitmap(cur[index].FullName); ConsoleWriteImage(m); Console.ReadKey(); Console.WriteLine("Graphics in console window!"); Point location = new Point(10, 10); Size imageSize = new Size(20, 10); } catch (Exception e) { } } break; case ConsoleKey.RightArrow: if (cur[index] is DirectoryInfo) { try { DirectoryInfo dire = cur[index] as DirectoryInfo; indexhist.Push(index); parent.Push(cur); index = 0; cur = dire.GetFileSystemInfos(); } catch (Exception e) { } } else Process.Start(cur[index].FullName); break; case ConsoleKey.Enter: Process.Start(cur[index].FullName); break; } Show(cur, index); pressed = Console.ReadKey(true); } }