Hot answers tagged

31

String compression If you're looking for a more optimized/automatic string compressor, try this one. Try it online! A compressed string looks like “...», where the dots are a chunk of base-250-encoded data. The decompression algorithm is a bit complicated: the chunk is interpreted as a “mixed-base” integer, with divmod breaking off various parts of this ...


21

This is part of what became the Jelly wiki tutorial. Chains (This is sort of a follow-up to Tacit programming.) How does Jelly evaluate a chain? As explained before, there are three cases to consider: whether this chain was called niladically, monadically, or dyadically. 1. Niladic chains These are the easiest of the bunch. To evaluate a niladic chain ...


18

Special-cased numeric values Here are some special cases for Jelly's numerics parser: - evaluates to -1 . evaluates to 0.5 ȷ evaluates to 1000 (ȷ is for scientific notation, e.g. 2ȷ6 is 2000000) ı evalulates to 1j (ı is for complex numbers, e.g. 2ı3 is 2+3j) It's also worth noting that something like 4ı is actually 4+1j, rather than 4. You can mix and ...


15

Optimal string compressor Recently I asked Erik the Outgolfer to add the optimized string compressor to JHT references page, but they said that sorry, but that compressor doesn't seem to be fully implemented it says “ugtestslug” is the shortest possible for ugtestslug, while “#ṀȮụḄPƇ» also does the job So I decide to implement the optimal string ...


15

Optimised string compressor Try it online! Lynn's post details what compressed strings are exactly, along with having a compressor that will produce these compressed strings. However, while tinkering around with a program in Jelly, I found it tiring to have to combine .dictionary and .string along with the correct placements of spaces and so on and so forth, ...


13

Jelly, 3 bytes R¡Ṡ Try it online! Takes input in reverse order. -2 bytes thanks to fireflame. -1 byte thanks to Dennis Explanation R¡Ṡ Main link ¡ Repeat <first argument> times R Range (generates range, and vectorizes on lists) Ṡ Sign (converts all numbers to 1 because they are all positive)


13

APL (Dyalog Extended), 3 bytes ׯ⍸ Try it online! A tacit function. Banning only the exact built-in actually gives APL a massive advantage! How it works ׯ⍸ ⍸ ⍝ Takes a vector v and gives another vector containing v[i] copies of i ⍝ for each index i ¯ ⍝ Inverse of the above, which counts occurrences of i which becomes v[i] × ⍝ Signum of each ...


11

You can use superscript three to nine (³⁴⁵⁶⁷⁸⁹) to golf some usually used values, but this depends on the amount of command line arguments, and in case of links, on the arguments of the links. ³ returns 100, and works only if there's no input. ⁴ returns 16, and works only if there's at most one input. ⁵ returns 10, and works only if there's at most two ...


11

Python 2, 49 bytes -1 thanks to Jonathan Allan f=lambda a,b:1>>a or[0]*b and f(a,b-1)+[f(a-1,b)] Try it online! This problem seemed like it could be written in the form of a doubly recursive function, subtracting one from each parameter. The key idea is that the sequence can be related as follows: f(0,b) = 1 f(a,0) = [] f(a,b) = f(a,b-1) + [f(a-1,...


11

JavaScript (Node.js),  173 170  163 bytes Expects ([ long_dic, short_dic ])(n), where the dictionaries are arrays of words and n is a BigInt. d=>F=(n,C)=>n?(B=Buffer,(g=k=>x=+[n%(n/=k=BigInt(k),k)])(3)?(q=x^2||g(3),b=B(d[g(2)][g(d[x].length)]),b[-q%2]^=32,q%3>0^C?' '+b:b):B([g(96)^95?x+32:10]))+F(n,1):'' Try it online! Commented d => ...


10

Abuse string bugs Credits go to Adnan for taking advantage of this first in Write a program to elasticize strings. Jelly is supposed to get character arithmetic one day, but until that happens, we can take advantage of the fact that Python overloads most arithmetic operators and that Jelly does no type checking. For example “abcd”Ḥ isn't supposed to do ...


10

JavaScript (ES6),  306 ... 227  226 bytes Expects (program)(ω). s=>g=(v,w=v,n=0,h=_=>(O={a:w,'+':"x+y",_:"x-y",S:"x*x",D:"x-1",'+~':"x*2",'_~':"0"}[c=(s+'a').match(/.~?/g)[n++]]||+c)[2]>O?2:O>g)=>(I=h(i=h(E=(o,x,y)=>eval(o)),o=O),o)?g(i?E(o,v,I?I-2?E(O,w):w:O):I?E(O,o,v):v,w,n-=i==...


9

Mathematica, 17 bytes 1^Range~Nest~##& Takes b then a. Try it on Wolfram Sandbox How? 1^Range~Nest~##& Range (* Range function; generates {1..<input>} *) ~Nest~## (* Apply it on <input 1> <input 2> times *) (* Mathematica automatically maps Range onto integers *) 1^ ...


9

21 bytes, solves Shortest code to throw SIGILL (safe) “A{gœỴ¹⁻ėe⁾÷ỤḞḂ4ḷ$»ŒV Try it online! This decompresses to the following Python 3 code (which is 22 bytes long, only one byte longer than the compressed version): import os os.kill(0,4) Try it online! The basic idea is to pick a task that's completely out of scope for Jelly; in this case, we're ...


9

Jelly, 15 bytes ⁾D’y“S²a⁸~`”yKv Try it online! ⁾D’y Substitute D for Jelly's decrement builtin. (This is done alone because ’ is a string terminator.) “ ”y Substitute S² S for square, a⁸ a for left argument, ~` and ~ for reuse argument. (+ and _ ...


8

APL, 44 bytes {1=≡⍺⍵:⍺+⍵⋄=/∆←|≡¨⍺⍵:⊃∇¨/↓↑⍺⍵⋄</∆:⍺∘∇¨⍵⋄⍵∇⍺} APL's + distributes over arrays as well, but in a different enough manner that this can't really be used. However, there is a built-in depth function (≡). Explanation: 1=≡⍺⍵:⍺+⍵: if the depths of ⍺ ⍵ are both zero (and therefore the depth of ⍺ ⍵ is 1), add them. ∆←|≡¨⍺⍵: take the absolute of ...


8

This is part of what became the Jelly wiki tutorial. Multi-chain links Remember when I wrote that you define a link by making a chain of other links? I wasn’t telling the whole truth: in reality, it’s a two-layer process. A link is a chain of chains, and by default, the outer chain simply has unit length. Consider this program: C+H That’s complement ...


7

This is part of what became the Jelly wiki tutorial. Tacit programming Jelly is a tacit programming language. This means you define links (functions) by composing existing links into a chain, without explicitly talking about the arguments involved. Which way the arguments “flow” through this composition is defined by the pattern the links are arranged in. ...


7

Jelly, 24 22 bytes ß"+ŒḊ?çⱮç€</ɼ?,ŒḊ€©Eɗ? Try it online! or see all test cases This technically violates To prevent boring and unbeatable solutions, if a language has this exact operation as a built-in, you may not use that language. However, this is by no means a boring or unbeatable solution. The only time + is used here is to add two flat ...


7

Python 3, 42 41 bytes -1 thanks to @ovs lambda a:[i+1in a for i in range(max(a))] Try it online!


7

25 bytes, solves We had a question once which only failed on Sundays - Cracked (Razetime) “ÇṄṚ,Ḣ¢;Xv^wME ṖƘżẓ⁸9!»ŒV Try it online! The compressed code is heavily based on Mr XCoder's submission: exit('Su'in time.ctime()<_) Notes: There's no need to import time since Jelly's interpreter does that, but not much is exposed, just sleeping and simple time ...


6

If TMTOWTDI, pick the one that fits your chain. One of the advantages of a tacit language is that you usually can get away without using variable references. However, this works only if the links in your chain have the right arities. For example, the straightforward way of taking the sum of all arrays in a 2D array is S€ which maps the sum atom over all ...


6

This is part of what became the Jelly wiki tutorial. Program structure Each line in a Jelly program is a link definition. Links are basically functions. The bottom line represents “main”: it's the link that gets evaluated using the arguments passed on the command line. All links but the last one, then, are function definitions: you can refer to them using ...


6

Charcoal, 147 bytes Nθ≔θζF⁺⪫⪪⪫⪪S+~¦d¦_~¦z¦i«≡№DSdiz++__鲫F⁼²Lυ⊞υθF²⊞υ⎇⊖Lυζι»¹«≔⎇υθζε≡ιD≦⊖εS≦×εεd≦⊗εz≦⁻εε⎚¿υ⊞υε≔εζ»⊞υ⎇⁼aιθIι¿‹²Lυ«≔⎇⁼+§υ¹⁺§υ⁰§υ²⁻§υ⁰§υ²ζ≔…⟦ζι⟧⁻Lυ³υ»»Iζ Try it online! Link is to verbose version of code. Takes the argument as the first input and the script as the second input. Explanation: Nθ≔θζ Input the argument and copy it to the flow-...


5

Haskell, 150 bytes data L=S Int|V{v::[L]} d(V z)=1+maximum(d<$>S 0:z);d _=0 S x!S y=S$x+y x!y|d x<d y=V$(x!)<$>v y|d x>d y=y!x|1<2=V$v x#v y (x:a)#(y:b)=x!y:a#b;a#b=a++b Explanation The first line defines an algebraic data type L, which is either a Scalar (containing an Int) or a Vector (containing a list of Ls, accessed using a ...


5

Mathematica, 122 bytes d=Depth x_~f~y_/;d@x>d@y:=y~f~x x_~f~y_/;d@x<d@y:=x~f~#&/@y x_List~f~y_:=MapThread[f,{x,y}~PadRight~Automatic] x_~f~y_=x+y Defines a recursive function f which computes the sum. Making use of Mathematica's pattern matching, this function is made up of four separate definitions: x_~f~y_/;d@x>d@y:=y~f~x If the depth of x ...


5

Consider using i rather than e e: If x occurs in y, then 1, else 0 i: Find the first index of element y in list x, or 0 The key thing here is that e and i take arguments in opposite orders. This can be exploited in two main cases: The question allows you to return an inconsistent truthy value, and your answer works by generating a list and checking if the ...


5

Use outer products to create useful integer matrices Here is a gist I have created of this post with slightly nicer HTML table formatting. The outer product quick þ can be attached to dyads and causes the dyad to act on each pair of elements in its left and right arguments. It is shorthand for €Ð€. For example if we had the code [1,2]+€Ð€[0,10] we could ...


5

Java, 802 794 754 746 bytes I decided to take up @Dennis♦ for the challenge to operate on strings "as a last resort" because it was probably "too complicated". Also, in the worst language to golf on. Arrays in the input are comma-separated, surrounded with square brackets, and without whitespace. Full program with functions wrapped into a ...


Only top voted, non community-wiki answers of a minimum length are eligible