KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Five easy pieces #3: Mark this

  • Five easy pieces #3: Mark this

    Posted by SJT on March 15, 2022 at 12:00 am

    First stage: file smile.txt

     

    The All-American Smile In a way he was like the land itself. Everything came to him too easily. But at least he knew it, and from time to time he worried about being a phoney.

     

    Write a single expression that reads the file, follows Markdown rules, and returns an HTML document as a string, e.g.

     

    <html>
      <body>
         <p>The All-American Smile</p>
         <p>In a way he was like the land itself. Everything came to him too easily.</p>
         <p>But at least he knew it, and from time to time he worried about being a phoney.</p>
      </body>
    </html>

     

    Hints: The .h namespace and File Binary; however .h.ht is not a solution.

     

    SJT replied 2 months, 2 weeks ago 2 Members · 4 Replies
  • 4 Replies
  • SJT

    Member
    March 19, 2024 at 10:55 am

    Not tempted yet? Lets make it easier and harder.

    • Easier: file lines have no trailing spaces.
    • Harder: let your expression contain no more than one reference to .h.htc.
  • SJT

    Member
    March 19, 2024 at 10:55 am

    Oh yes.  Now were having fun.

     

    {.h.htc[y;x]}/[.h.text"nn"vs"c"$ read1 `:smile.md;`body`html]

     

  • SJT

    Member
    March 19, 2024 at 10:55 am

    Second stage: smile2.md

     

    # The All-American Smile In a way he was like the land itself. Everything came to him too easily. But at least he knew it, and from time to time he worried about being a phoney.

     

    Output:

     

    <html>
      <body>
        <h1>The All-American Smile</h1>
         <p>In a way he was like the land itself. Everything came to him too easily.</p>
         <p>But at least he knew it, and from time to time he worried about being a phoney.</p>
      </body>
    </html>

     

     

  • rocuinneagain

    Member
    March 19, 2024 at 11:01 am

    A few different variations:

    -1 {i:where ""~/:x;raze "<html><body><p>",@[x;i;:;count[i]#enlist "</p><p>"],"</p></body></html>"}
    read0 `:smile.txt 
    -1"<html><body><p>",,[;"</p></body></html>"]
    ssr[;"n";""]
    "</p><p>" sv "nn" vs`char$read1`:smile.txt 
    -1"<html><body><p>",ssr["</p><p>"sv"nn"vs`char$read1`:smile.txt;"n";""],"</p></body></html>" 
    -1{.h.htc[y;x]}over(ssr["</p><p>"sv"nn"vs`char$read1`:smile.txt;"n";""];`p;`body;`html) 
    -1({.h.htc[y;x]})(ssr["</p><p>"sv"nn"vs`char$read1`:smile.txt;"n";""];`p;`body;`html)

     

Log in to reply.