Tuesday, May 2, 2023

Word Wrap Question

 /*

We are building a word processor and we would like to implement a "word-wrap" functionality.

Given a list of words followed by a maximum number of characters in a line, return a collection of strings where each string element represents a line that contains as many words as possible, with the words in each line being concatenated with a single '-' (representing a space, but easier to see for testing). The length of each string must not exceed the maximum character length per line.

Your function should take in the maximum characters per line and return a data structure representing all lines in the indicated max length.

Examples:

words1 = [ "The", "day", "began", "as", "still", "as", "the",
          "night", "abruptly", "lighted", "with", "brilliant",
          "flame" ]

wrapLines(words1, 13) "wrap words1 to line length 13" =>

  [ "The-day-began",
    "as-still-as",
    "the-night",
    "abruptly",
    "lighted-with",
    "brilliant",
    "flame" ]

wrapLines(words1, 12) "wrap words1 to line length 12" =>

  [ "The-day",
    "began-as",
    "still-as-the",
    "night",
    "abruptly",
    "lighted-with",
    "brilliant",
    "flame" ]    


wrapLines(words1, 20) "wrap words1 to line length 20" =>

  [ "The-day-began-as",
    "still-as-the-night",
    "abruptly-lighted",
    "with-brilliant-flame" ]

words2 = [ "Hello" ]

wrapLines(words2, 5) "wrap words2 to line length 5" =>

  [ "Hello" ]


wrapLines(words2, 30) "wrap words2 to line length 30" =>

  [ "Hello" ]  

words3 = [ "Hello", "Hello" ]

wrapLines(words3, 5) "wrap words3 to line length 5" =>

  [ "Hello",
  "Hello" ]

words4 = ["Well", "Hello", "world" ]

wrapLines(words4, 5) "wrap words4 to line length 5" =>

  [ "Well",
  "Hello",
  "world" ]

words5 = ["Hello", "HelloWorld", "Hello", "Hello"]

wrapLines(words5, 20) "wrap words 5 to line length 20 =>

  [ "Hello-HelloWorld",
    "Hello-Hello" ]


words6 = [ "a", "b", "c", "d" ]
wrapLines(words6, 20) "wrap words 6 to line length 20 =>

  [ "a-b-c-d" ]

wrapLines(words6, 4) "wrap words 6 to line length 4 =>

  [ "a-b",
    "c-d" ]

wrapLines(words6, 1) "wrap words 6 to line length 1 =>

  [ "a",
    "b",
    "c",
    "d" ]

All Test Cases:
          words,  max line length
wrapLines(words1, 13)
wrapLines(words1, 12)
wrapLines(words1, 20)
wrapLines(words2, 5)
wrapLines(words2, 30)
wrapLines(words3, 5)
wrapLines(words4, 5)
wrapLines(words5, 20)
wrapLines(words6, 20)
wrapLines(words6, 4)
wrapLines(words6, 1)

n = number of words OR total characters
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Solution {
    static void Main(String[] args) {
        String[] words1 = new String[] {"The","day","began","as","still","as","the","night","abruptly","lighted","with","brilliant","flame"};
        String[] words2 = new String[] {"Hello"};
        String[] words3 = new String[] {"Hello""Hello"};
        String[] words4 = new String[] {"Well""Hello""world"};
        String[] words5 = new String[] {"Hello""HelloWorld""Hello""Hello"};
        String[] words6 = new String[] {"a""b""c""d"};
        printOutput(wrapLines(words1, 13));        
        printOutput(wrapLines(words1, 12));
        printOutput(wrapLines(words1, 20));
        printOutput(wrapLines(words2, 5));
        printOutput(wrapLines(words2, 30));
        printOutput(wrapLines(words3, 5));
        printOutput(wrapLines(words4, 5));
        printOutput(wrapLines(words5, 20));
        printOutput(wrapLines(words6, 20));
        printOutput(wrapLines(words6, 4));
        printOutput(wrapLines(words6, 1));
        
    }
    
    static void printOutput(String[] result)
    {
        for(int i= 0 ;i< result.Count(); i++) 
        {
            Console.WriteLine(result[i]);
        }
        Console.WriteLine();
    }
    
    static String[] wrapLines(String[] words, int expectedLength)
    {
        List<String> result = new List<String>();
        int count = 0;
        StringBuilder str = new StringBuilder();
       
        for(int i=0;i < words.Count(); i++)
        {            
            int lenOfEachWord = words[i].Count();
            if((count+lenOfEachWord) <= expectedLength)
            {
                str.Append(words[i]);
                str.Append("-");
                count += lenOfEachWord +1// 1 for hyphen
                //Console.WriteLine(count + " " + str.ToString());
            }
            else {
                    count = 0;
                    //Console.WriteLine(str.ToString());
                    result.Add(str.ToString().TrimEnd('-'));
                    str = new StringBuilder();
                    --i;
            }
        
        }
        result.Add(str.ToString().TrimEnd('-'));
        return result.ToArray();
    }
}

No comments:

Post a Comment

Comment!!

Victoria Canada Trip

 Victoria city to do and must have  1. Parliament building  2. Waterfront  3. Mini sea-bus at waterfront  4. Fisherman wharf   5. Museum (op...