Jump to content

KonaUser

New Members
  • Posts

    0
  • Joined

  • Last visited

Everything posted by KonaUser

  1. Yes I did just make it for this post I just love golfing! You can try it out here! (TIO link)
  2. Ok here is my answer in Kona. For the record I would not characterize the K family (and thus Kona) to be made for code-golfing by the scale at which they are used within the finance sector. In any case, here it is: f:{(y-#a)_'(!#a)!\:a:((.$_ceil y*%#x)*#j)#j:(x," ")} I am going to try and explain, more or less, how it functions. Note: in Kona, functions are evaluated right-to-left. First, we begin with j:(x," ") Which essentially just appends an empty space at the end of the string, and is then assigned to the variable j. Moving on, we have the following: a:((.$_ceil y*%#x)*#j)#j:(x," ") The part (.$_ceil y*%#x)*#j can be broken down in the following way: #x is the number of elements in x, which in our case, since we are applying it to a string, is the number of characters; followed by y*%#x, which just means we divide our desired length by the number of characters in our string. The _ceil is simply the cealing function, and the .$ is used to turn a floating into an integer (if decimal part is zero). All of it is then multiplied by the length of our j (which again is the string with an empty space at the end), and then named "a". Here is a couple of examples of it in use: {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";12] "Hello Hello Hello " {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";6] "Hello Hello " {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";4] "Hello " {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";15] "Hello Hello Hello " Once we have our "a", we consider the following part: (!#a)!\:a First off, !#a is a vector of integers from 0 up to (#a)-1 (the length of a); e.g. if a is "Hello " as our third example above, then !#a is simply 0 1 2 3. The !\: is a tad more difficult; the ! in its dyadic form is a rotate function; e.g. 2!"Hello" is "lloHe"; and since our (!#a) is a vector, we use the \: to apply all to our a; for example, 1 2 3!\:"Hello" ("elloH" "lloHe" "loHel") Finally, (y-#a)_' is simply removing the first (y-#a) characters from each row of our column vector a. Here are a few examples of the whole function in action: f["Hello";8] ("Hello He" "ello Hel" "llo Hell" "lo Hello" "o Hello " " Hello H" "Hello He" "ello Hel" "llo Hell" "lo Hello" "o Hello " " Hello H") f["Hello";2] ("He" "el" "ll" "lo" "o " " H") f["Hello";15] ("Hello Hello Hel" "ello Hello Hell" "llo Hello Hello" "lo Hello Hello " "o Hello Hello H" " Hello Hello He" "Hello Hello Hel" "ello Hello Hell" "llo Hello Hello" "lo Hello Hello " "o Hello Hello H" " Hello Hello He" "Hello Hello Hel" "ello Hello Hell" "llo Hello Hello" "lo Hello Hello " "o Hello Hello H" " Hello Hello He") f["Hello";5] ("Hello" "ello " "llo H" "lo He" "o Hel" " Hell") I hope everything was clear enough. Anyways the character count (without the f: which was added purely for clarity) is 50; and I am sure if I had a bit more time to think about it I could reduce it quite a lot. For more info on Kona here is the GitHub repository: kevinlawler/kona: Open-source implementation of the K programming language
×
×
  • Create New...