6
\$\begingroup\$

Not entirely sure if this one or a similar challenge has already been posted, but here it goes anyway.

Write a program that takes a string as input and writes its trucated/summarized output form such that a specific sep (separator) is placed in the middle, removing the necessary characters of the original string so that the length of output equals a given width.

Rules

  • Input is a string, a width and a sep (separator);
  • if len(string) <= width: output = string, meaning that the output is the string itself unchanged;
  • if width <= len(sep): output = sep, meaning that the output is the separator only;
  • len(output) must be equal to width, with the exception of the previous case;
  • The excess character in case of uneven number of remaining characters for each half of the string can be placed on the left or right halve;
  • Standard code-golf challenge with the smallest program to win.

Examples

Format: "string" [length(string)] --> "output" [length(ouput) / width] with sep equal to _(...)_ (_ are whitespaces).

"xtlgxbdwytxcjacu" [16] --> "x (...) " [8 / 8]
"upjptz" [6] --> " (...) " [7 / 3]
"h" [1] --> "h" [1 / 1]
"qsshkvmawcmf" [12] --> " (...) " [7 / 7]
"tvdknroxszhufgck" [16] --> "tv (...) k" [10 / 10]
"ag" [2] --> "ag" [2 / 5]
"vbgdegtgshnzjrudca" [18] --> "vbgd (...) udca" [15 / 15]
"bscwimjjhmgvcwp" [15] --> "bscwimjjhmgvcwp" [15 / 18]
"xwhgbaenornbzwirlp" [18] --> "xwhgbaenornbzwirlp" [18 / 18]
"tbqyrekkzyizc" [13] --> "t (...) " [8 / 8]
"oowdwbeezeh" [11] --> "o (...) h" [9 / 9]
"avlzfzpjazhjc" [13] --> "avlzfzpjazhjc" [13 / 15]
"dngxmebwenfrbidonses" [20] --> "dngxme (...) donses" [19 / 19]
"bqfxyffdemjghipk" [16] --> "bqfxyffdemjghipk" [16 / 16]
"sfwnlkreghzff" [13] --> " (...) " [7 / 5]
"dznlsyaojtferxh" [15] --> "dznlsyaojtferxh" [15 / 17]
"zwkhjdafkzjdgc" [14] --> "zwk (...) dgc" [13 / 13]
"kpzochngmwoulne" [15] --> "kpz (...) lne" [13 / 13]
"vluqvhpcxg" [10] --> "vluqvhpcxg" [10 / 10]
"wuc" [3] --> "wuc" [3 / 8]
\$\endgroup\$
8
  • 1
    \$\begingroup\$ @JonathanAllan The latter is correct. The program should accept a string, the desired width and separator. \$\endgroup\$ – dfernan Feb 3 '17 at 13:13
  • \$\begingroup\$ The two inequality driven rules have a conflict when len(string) = len(sep) = width, for example, what should the output be for string ab, separator 12, width 2. Are we free to output either the string or the separator in such cases? \$\endgroup\$ – Jonathan Allan Feb 3 '17 at 13:46
  • \$\begingroup\$ The input/output need to be strings ? Or they can be list of characters? \$\endgroup\$ – Rod Feb 3 '17 at 14:01
  • \$\begingroup\$ @JonathanAllan The rules are in order. It is preferred to output the string itself if there is room for it. So, in your case, the output should be ab. \$\endgroup\$ – dfernan Feb 3 '17 at 14:24
  • 3
    \$\begingroup\$ The kolmogorov-complexity tag is to be used for constant output. I have replaced it with the string tag, you can revert if you disagree. \$\endgroup\$ – Erik the Outgolfer Feb 3 '17 at 15:00
3
\$\begingroup\$

Python 2, 93 76 72 bytes

s,w,p=input()
l=len(s)
if w<l:w=max(w-len(p),0);s[-~w/2:l-w/2]=p
print s

Try it online!

Input and output are a list of characters

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Your code outputs the wrong value for the "upjptz" example. \$\endgroup\$ – Tom Feb 3 '17 at 14:55
  • \$\begingroup\$ @TomDevs fixed it \$\endgroup\$ – Rod Feb 3 '17 at 15:09
2
\$\begingroup\$

JavaScript (ES6), 78 bytes

(w,s,e,l=s.length,m=e.length)=>l>w?m<w?s.slice(0,w-m>>1)+e+s.slice(m-w>>1):e:s

I was pleasantly surprised to discover that w-m>>w and m-w>>1 give the desired result.

\$\endgroup\$
1
\$\begingroup\$

Python 2, 76 bytes

def f(s,p,w):t=len(s);q=len(p)-w;print[s,[p,s[:~q/2]+p+s[t+q/2:]][q<0]][t>w]

Tests are at TryItOnline!

Note: Places the extra character for odd splits on the right hand side
(question states that either is fine, although the examples show left only).

\$\endgroup\$
0
\$\begingroup\$

JavaScript ES6, 103 99 bytes

(a,b,c,d=a.length,e=c.length,f=(b>e?b:e)-e,g=a.split(""),h=g.splice(-~f/2,d-f,c))=>b<d?g.join(""):a

f=(a,b,c,d=a.length,e=c.length,f=(b>e?b:e)-e,g=a.split(""),h=g.splice(-~f/2,d-f,c))=>b<d?g.join(""):a;

console.log(f("dngxmebwenfrbidonses", 19, " (...) "));

\$\endgroup\$
1
  • \$\begingroup\$ You can save some bytes by changing ("") into ``. You can also shorten f: f=b>e?b-e:0. \$\endgroup\$ – Luke Feb 4 '17 at 8:41
0
\$\begingroup\$

Mathematica, 90 bytes

If[(l=Length)[i=#2]<=#,#2,If[#<=(s=l@#3),#3,Join[(t=i~Take~Floor[#/2]&)[#-s],#3,t[s-#]]]]&

Unnamed function taking an integer (the width) as its first argument and two lists of characters (the string and the separator) as its second/third arguments, and returning a list of characters.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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