Skip to main content
minor update
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
w =>                                // main function: takes width 'w' as input, returns 'g'
  g = (                             // g = recursive function with:
    b,                              //   - b = number of books
    s = `|${'-'.repeat(w * 3)}|\n`, //   - s = top/bottom of shell, filled with '-'
    r = s.replace(                  //   - r = pattern of the current row of books,
      RegExp('---', 'g'),           //     ('b' is updated while buildingusing it)'s' as a template and updating
      _ => b && b-- ? '|X|' : '   ' // NB: 'r' must be defined in the scope of'b' 'g',while building it
    )                               // NB: 'r' must be otherwisedefined itin wouldthe bescope overwrittenof by'g',
  ) =>                              //     subsequentotherwise callsit would be overwritten by
    (                               //     subsequent calls
      b ?                           // if there are remaining books:
        g(b) + s                    //   do a recursive call and append shell top
      :                             // else:
        s                           //   just append shell top
    ) + r + r + r + s               // append book rows and shell bottom
w =>                                // main function: takes width 'w' as input, returns 'g'
  g = (                             // g = recursive function with:
    b,                              //   - b = number of books
    s = `|${'-'.repeat(w * 3)}|\n`, //   - s = top/bottom of shell, filled with '-'
    r = s.replace(                  //   - r = pattern of the current row of books
      RegExp('---', 'g'),           //     ('b' is updated while building it)
      _ => b && b-- ? '|X|' : '   ' // NB: 'r' must be defined in the scope of 'g',
    )                               //     otherwise it would be overwritten by
  ) =>                              //     subsequent calls
    (                               //
      b ?                           // if there are remaining books:
        g(b) + s                    //   do a recursive call and append shell top
      :                             // else
        s                           //   just append shell top
    ) + r + r + r + s               // append book rows and shell bottom
w =>                                // main function: takes width 'w' as input, returns 'g'
  g = (                             // g = recursive function with:
    b,                              //   - b = number of books
    s = `|${'-'.repeat(w * 3)}|\n`, //   - s = top/bottom of shell, filled with '-'
    r = s.replace(                  //   - r = pattern of the current row of books,
      RegExp('---', 'g'),           //         using 's' as a template and updating
      _ => b && b-- ? '|X|' : '   ' //         'b' while building it
    )                               // NB: 'r' must be defined in the scope of 'g',
  ) =>                              //     otherwise it would be overwritten by
    (                               //     subsequent calls
      b ?                           // if there are remaining books:
        g(b) + s                    //   do a recursive call and append shell top
      :                             // else:
        s                           //   just append shell top
    ) + r + r + r + s               // append book rows and shell bottom
added the commented version
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

###Formatted and commented

w =>                                // main function: takes width 'w' as input, returns 'g'
  g = (                             // g = recursive function with:
    b,                              //   - b = number of books
    s = `|${'-'.repeat(w * 3)}|\n`, //   - s = top/bottom of shell, filled with '-'
    r = s.replace(                  //   - r = pattern of the current row of books
      RegExp('---', 'g'),           //     ('b' is updated while building it)
      _ => b && b-- ? '|X|' : '   ' // NB: 'r' must be defined in the scope of 'g',
    )                               //     otherwise it would be overwritten by
  ) =>                              //     subsequent calls
    (                               //
      b ?                           // if there are remaining books:
        g(b) + s                    //   do a recursive call and append shell top
      :                             // else
        s                           //   just append shell top
    ) + r + r + r + s               // append book rows and shell bottom

###Test cases

###Test cases

###Formatted and commented

w =>                                // main function: takes width 'w' as input, returns 'g'
  g = (                             // g = recursive function with:
    b,                              //   - b = number of books
    s = `|${'-'.repeat(w * 3)}|\n`, //   - s = top/bottom of shell, filled with '-'
    r = s.replace(                  //   - r = pattern of the current row of books
      RegExp('---', 'g'),           //     ('b' is updated while building it)
      _ => b && b-- ? '|X|' : '   ' // NB: 'r' must be defined in the scope of 'g',
    )                               //     otherwise it would be overwritten by
  ) =>                              //     subsequent calls
    (                               //
      b ?                           // if there are remaining books:
        g(b) + s                    //   do a recursive call and append shell top
      :                             // else
        s                           //   just append shell top
    ) + r + r + r + s               // append book rows and shell bottom

###Test cases

saved 1 byte
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

##JavaScript (ES6), 100 9999 98 bytes

Takes the width w and the number of books b in currying syntax (w)(b).

f=w=>g=(w,b,s=`|${'-'.repeat(w*3)}|
`,r=s.replace(/---/g,_=>b&&b--?'|X|':'   '))=>(b?fg(w,b)+s:s)+r+r+r+s

###Test cases

f=let f =

w=>g=(w,b,s=`|${'-'.repeat(w*3)}|
`,r=s.replace(/---/g,_=>b&&b--?'|X|':'   '))=>(b?fg(w,b)+s:s)+r+r+r+s

console.log(f(6,)(2))
console.log(f(2,)(6))
console.log(f(4,)(9))

##JavaScript (ES6), 100 99 bytes

f=(w,b,s=`|${'-'.repeat(w*3)}|
`,r=s.replace(/---/g,_=>b&&b--?'|X|':'   '))=>(b?f(w,b)+s:s)+r+r+r+s

###Test cases

f=(w,b,s=`|${'-'.repeat(w*3)}|
`,r=s.replace(/---/g,_=>b&&b--?'|X|':'   '))=>(b?f(w,b)+s:s)+r+r+r+s

console.log(f(6,2))
console.log(f(2,6))
console.log(f(4,9))

##JavaScript (ES6), 100 99 98 bytes

Takes the width w and the number of books b in currying syntax (w)(b).

w=>g=(b,s=`|${'-'.repeat(w*3)}|
`,r=s.replace(/---/g,_=>b&&b--?'|X|':'   '))=>(b?g(b)+s:s)+r+r+r+s

###Test cases

let f =

w=>g=(b,s=`|${'-'.repeat(w*3)}|
`,r=s.replace(/---/g,_=>b&&b--?'|X|':'   '))=>(b?g(b)+s:s)+r+r+r+s

console.log(f(6)(2))
console.log(f(2)(6))
console.log(f(4)(9))

saved 1 byte
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading