/// <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);
        }
示例#2
0
        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);
        }
示例#3
0
        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;
        }