21
\$\begingroup\$

Introduction

Write a program or function that adds a drop capital to a paragraph. The program will input the text to format, the column width, and the number of lines to drop the capital. The drop capitals look like this:

Lines:        2     3      4 
Drop capital: A|    A.|    A..|
              ~'    ..|    ...|    etc.
                    ~~'    ...|
                           ~~~'

This is a related challenge.

Input

  • Input a string of printable ASCII characters (no tabs or newlines) and 2 integers greater than one.
  • One integer is the number of columns that the output should have.
  • The other is the number of lines spanned by the drop capital.
  • The text string contains words separated by single spaces.
  • Assume none of the words will be longer than the column width. That is, column width > longest word + drop capital height
  • All lines will have at least one word.
  • For this challenge, a word consists of any character other than a space.
  • Input may be in any convenient format following the rules above.

Output

  • A left-justified block of text containing a drop capital with the specified number of lines.
  • A line should contain as many words as possible without being longer than the column width.
  • There is a space between the drop capital and the rest of each line.
  • One trailing space or newline is allowed.

Examples

Lines: 2 Columns: 10 Text: The quick brown fox jumped over the lazy dog.

T| he
~' quick
brown fox
jumped
over the
lazy dog.

Lines: 3 Columns: 10 Text: (Same as above)

T.| he
..| quick
~~' brown
fox jumped
over the
lazy dog.

Lines: 4 Columns: 10 Text: (Same as above)

T..| he
...| quick
...| brown
~~~' fox
jumped
over the
lazy dog.

Lines: 2 Columns: 80 Text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor.

L| orem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
~' Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec
consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero
egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem
lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor.

Lines: 3 Columns: 80 Text: (Same as above)

L.| orem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
..| Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec
~~' consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget
libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta
lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non
tortor.

Lines: 4 Columns: 80 Text: (Same as above)

L..| orem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam
...| lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra
...| nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam
~~~' eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim,
ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies
a non tortor.

This is and standard loopholes apply.

\$\endgroup\$
6
  • 3
    \$\begingroup\$ Yay, another typography challenge! We need more of these. \$\endgroup\$ Nov 4, 2015 at 2:49
  • 2
    \$\begingroup\$ What happens when a word is longer than a column width, ie The quick brown fox jumped over the unenthusiastic dog for any of the first three examples? \$\endgroup\$
    – MickyT
    Nov 4, 2015 at 19:07
  • \$\begingroup\$ @MickyT Assume none of the words will be longer than the column width. That is, column width > longest word + drop capital height \$\endgroup\$ Nov 5, 2015 at 2:37
  • \$\begingroup\$ What if the capital lines are greater than the line of text? Ie. L:4,C:100,T:'Stuff' \$\endgroup\$
    – TFeld
    Nov 5, 2015 at 8:45
  • \$\begingroup\$ Also, can Lines = 1? \$\endgroup\$
    – TFeld
    Nov 5, 2015 at 9:30

2 Answers 2

1
\$\begingroup\$

Python 2, 202 bytes

def f(l,c,t):
 l-=1;s=['.'*l+'|']*l+['~'*l+"'"]
 s[0]=t[0]+s[0][1:];t=t[1:].split();j=0
 while t:
  w=t.pop(0)
  if len(s[j]+w)>=c:j+=1
  if j>=len(s):s.append(w)
  else:s[j]+=' '+w
 return '\n'.join(s)

Call as f(Lines, Columns, Text)

f(4,100,'Stuff') gives

S..| tuff
...|
...|
~~~'
\$\endgroup\$
1
\$\begingroup\$

C#, 244 bytes

string F(int d,int c,string t){var w=t.Substring(1).Split(' ');t=""+t[0];for(int i=0,x=c,y=0;i<w.Length;x+=w[i++].Length+1)t+=(1>(x=x+w[i].Length>c?0:x)?"\n"+(y++<d?(y<d?"| ":"' ").PadLeft(x=d+1,y<d?'.':'~'):""):" ")+w[i];return t.Remove(1,2);}

Indentation, new lines and comments for clarity:

string F(int d,int c,string t){
    var w=t.Substring(1).Split(' ');
    t=""+t[0];
    for(int i=0,x=c,y=0;i<w.Length;x+=w[i++].Length+1)
        t+=(1>(x=x+w[i].Length>c?0:x)?"\n"+(y++<d?(y<d?"| ":"' ").PadLeft(x=d+1,y<d?'.':'~'):""):" ")+w[i];
    return t.Remove(1,2);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.