static LinkedList <String> GenerateBinaryRepresentationList(int n) { LinkedQueue <StringBuilder> q = new LinkedQueue <StringBuilder>(); LinkedList <String> output = new System.Collections.Generic.LinkedList <String>(); if (n < 1) { return(output); } q.Push(new StringBuilder("1")); while (n-- > 0) { StringBuilder sb = q.Pop(); //add to output list "not sure Addafter or Addlast" output.AddLast(sb.ToString()); //copy StringBuilder sbc = new StringBuilder(sb.ToString()); //left sb.Append('0'); q.Push(sb); //right sbc.Append('1'); q.Push(sbc); } return(output); }
static LinkedList <string> GenerateBinaryRepresentationList(int n) { LinkedQueue <StringBuilder> q = new LinkedQueue <StringBuilder>(); LinkedList <String> output = new LinkedList <String>(); if (n < 1) { return(output); } q.Push(new StringBuilder("1")); while (n-- > 0) { StringBuilder sb = q.Pop(); output.AddLast(sb.ToString()); StringBuilder sbc = new StringBuilder(sb.ToString()); sb.Append('0'); q.Push(sb); sbc.Append('1'); q.Push(sbc); } return(output); }
/// <summary> /// This will print the binary repesentation of all numbers from 1 up to n /// By doing this it will design a virtual binary tree that will perform /// a level order traversal using the FIFO queue. /// Result should be like this: /// 1 /// / \ /// 10 11 /// / \ / \ /// 100 101 110 111 /// /// etc. /// which will store each value that was visited in the list. /// </summary> static LinkedList <string> generateBinaryRepresentationList(int n) { /// <summary> /// Creates an empty queue of strings /// </summary> LinkedQueue <StringBuilder> x = new LinkedQueue <StringBuilder>(); /// <summary> /// The list is returning the binary values /// </summary> LinkedList <string> output = new LinkedList <string>(); if (n < 1) { /// <summary> /// binary values and representation do not support negative values /// return an empty list /// </summary> return(output); } /// <summary> /// This will add the first binary number. This uses a dynamic string /// to avoid string concat. /// </summary> x.push(new StringBuilder("1")); while (n > 0) { /// <summary> /// print the first part of the queue /// </summary> StringBuilder sb = x.pop(); output.AddLast(sb.ToString()); /// < summary > /// Make a copy /// </summary> StringBuilder sbc = new StringBuilder(sb.ToString()); /// <summary> /// Left Child /// </summary> sb.Append("0"); x.push(sb); /// <summary> /// Right Child /// </summary> sbc.Append("1"); x.push(sbc); n--; } return(output); }
static void Main(string[] args) { int C = 72; // Column length to wrap to string inputFilename = "../../WarOfTheWorlds.txt"; string outputFilename = "../../output.txt"; StreamReader sr = null; if (args.Length != 3) { printUsage(); Environment.Exit(1); } try { C = int.Parse(args[0]); inputFilename = args[1]; outputFilename = args[2]; sr = File.OpenText(inputFilename); } catch (FileNotFoundException e) { Console.WriteLine("Could not find the input file."); Environment.Exit(1); } catch (Exception e) { Console.WriteLine("Something is wrong with the input."); printUsage(); Environment.Exit(1); } IQueueInterface <string> words = new LinkedQueue <string>(); while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] tmp = line.Split(' '); foreach (var word in tmp) { words.push(word); } } sr.Close(); int spacesRemaining = wrapSimply(words, C, outputFilename); Console.WriteLine("Total spaces remaining (Greedy): " + spacesRemaining); return; }
static LinkedList <string> GenerateBinaryRepresentationList(int n) { ///<value> ///Create an empty queue of strings with which to perform the traversal ///</value> LinkedQueue <StringBuilder> q = new LinkedQueue <StringBuilder>(); ///<value> ///A list for returning the binary values ///</value> LinkedList <String> output = new LinkedList <String>(); if (n < 1) { ///<return> /// binary representation of negative values is not supported return an empty list ///</return> return(output); } /// Enqueue the first binary number. Use a dynamic string to avoid string concat q.Push(new StringBuilder("1")); /// BFS while (n-- > 0) { /// print the front of queue StringBuilder sb = q.Pop(); output.AddLast(sb.ToString()); /// Make a copy StringBuilder sbc = new StringBuilder(sb.ToString()); /// Left child sb.Append('0'); q.Push(sb); /// Right child sbc.Append('1'); q.Push(sbc); } return(output); }
static void Main(string[] args) { Console.WriteLine("Please enter wrap width: "); string C = Console.ReadLine(); string directory = Directory.GetCurrentDirectory(); Console.WriteLine("Please enter the input file name: "); Console.WriteLine("Automatically uses current directory of exe for path"); string inputFilename = Console.ReadLine(); inputFilename = Path.Combine(directory, inputFilename); Console.WriteLine("Automatically uses current directory of exe for path"); Console.WriteLine("Please enter the output file name: "); string outputFilename = Console.ReadLine(); outputFilename = Path.Combine(directory, outputFilename); //Console.WriteLine(C); //string inputFilename = Path.Combine(Directory.GetCurrentDirectory(), "WarOfTheWorlds.txt"); //Console.WriteLine(inputFilename); //string outputFilename = Path.Combine(Directory.GetCurrentDirectory(), "output.txt"); //Console.WriteLine(outputFilename); int C2 = int.Parse(C); args = new string[3]; args[0] = C; args[1] = inputFilename; args[2] = outputFilename; LinkedQueue <string> words = new LinkedQueue <string>(); if (args.Length != 3) { PrintUsage(); Environment.Exit(1); } try { using (StreamReader reads = new StreamReader(inputFilename)) { string book = reads.ReadToEnd(); foreach (string word in book.Split(' ')) { words.Push(word); } reads.Close(); } } catch (FileNotFoundException) { Console.WriteLine("Could not find the input file."); PrintUsage(); Environment.Exit(1); } catch (Exception) { Console.WriteLine("Something is wrong with the input."); PrintUsage(); Environment.Exit(1); } int spacesRemaining = WrapSimply(words, C2, outputFilename); Console.WriteLine("Total spaces remaining (Greedy): " + spacesRemaining); Console.WriteLine("Press enter to exit"); Console.ReadLine(); }