Hot answers tagged

17

JavaScript (ES6), 130 102 bytes Takes input as an array of strings in "Rs" format, where R is the rank and s is the suit. Expects "T" for 10's. Aces are low. a=>(s='A23456789TJQK')[([[R,[,S]],B,C,D]=a.map(c=>[s.search(c[0])+14,c]),R+=D<C|2*((D<B)+(C<B)))%13]+S Try it online! How? We first convert each card into an array [rank, card] where ...


15

Python 2, 143 140 138 136 127 125 124 123 121 bytes lambda(S,V),*l:S+N[F(V)+int(`map(sorted(l,key=lambda(s,v):(F(v),s)).index,l)`[1::3],3)*3/10] N='23456789TJQKA'*2;F=N.find Try it online! Aces are high Encodes the three cards by finding their position in a sorted list of the cards (0=smallest, 1=middle, 2=largest): cards: [SK, C4, H4] sorted: [C4, ...


13

Ruby class Dakota PRIVATE_KEY = 8411088 def self.encrypt(str) str.gsub(/[A-Z]/){|c|"0#{c.downcase}"}.gsub(/[a-z]+/){|s|xor(s.to_i(36),$')} end def self.decrypt(str) str.gsub(/\d+/){|s|out = s.to_i.to_s(36);out[0] = out[0].upcase if s[0]==?0; out} end def self.xor(n, config) n^=PRIVATE_KEY if private_env?(config) n end ...


12

CJam, 18 bytes UXri{_@+}*;26b'af+ Try it online in the CJam interpreter. How it works UX e# Push 0 and 1. ri{ e# Read an integer and execute the loop that many times. _ e# Push a copy the topmost integer. @ e# Rotate the bottom-most integer on top of the stack. + e# Pop the two topmost integers and push their sum. }* e# ; e# ...


12

Jelly, (17 bytes + 18 bytes) × length 6 = 210 points b36µỤỤ+×3µŒ¿b3U+Ṣ Ṣ:3_J Ṣ%3Uḅ3œ?Çḅ36 Try it online! (or with extra debug info) Having had a go at solving this challenge aiming at the stated victory condition, I thought it'd be interesting to go for a hypothetical alternative victory condition: code-golf given a minimum possible maximum length for ...


10

Jelly, 33 bytes ØDḊḊ;“TJQKA”p“CDHS” ¢iⱮµḊŒ¿×4+Ḣị¢ Try it online! Explanation The first line is niladic. It yields a list of the 52 cards ØDḊḊ;“TJQKA”p“CDHS” ØD Digits: '0123456789' ḊḊ Dequeue twice: '23456789' ; Append with... “TJQKA” ...the string 'TJQKA': '23456789TJQKA'. These are ...


9

Jelly, (4 3 bytes + 6 5 bytes) × length 8 = 80 64 points b⁴ÄṢŻIḅ⁴ Try it online! Jelly, (2 1 byte + 4 3 bytes) × length 10 = 60 40 points ÄṢŻI Try it online! Explanation Solution 1 This is using a different algorithm from most of the other answers. We start by encoding the value in hexadecimal (b⁴), as with the other answers, then take a cumulative ...


8

TeaScript, 34 bytes 37 51 54 TeaScript is JavaScript for golfing. It also brings ES2015 features to the average browser. F(x)b(26)l(#C(lc()+(l<'a'?49:10))) Try it online Explanation // x is the input F(x) // Fibonacci from input .b(26) // To Base-26 string but with 0-9, a-p // instead of a-z, to fix this... .l(# ...


8

CJam This is the encoder: 232375064392749269032321519657657089927649992440902190178063558812627752920796248165803740235420850037801568815744960725761679066919872746899310628404239458 128b:c~ and this is the decoder: ...


8

05AB1E, 13 bytes Encoder, 8 bytes 0ì¥ĀηOØP Try it online! Explanation 0ì # prepend 0 to input ¥ # calculate deltas Ā # truthify each η # calculate prefixes O # sum each Ø # get the prime at that index P # product Decoder, 5 bytes Ò.ØÉJ Try it online! Explanation Ò # get ...


7

Java I discovered that division by zero does not cause errors in this program. This program fully encodes the Strings into a form that cannot be traced to the North Dakotan government. Due to the strange behaviour mentioned above, encoding and decoding might not work correctly in all cases. class Program{ public static void main(String[] args){ ...


7

Jelly, 17 bytes Encoder (10 bytes): 0;IA+\‘ÆNP Try it online! Decoder (7 bytes): ÆEĖŒṙḂ¬ Try it online! How? Encoder: 0;IA+\‘ÆNP - Link: list of integers (1s and 0s) e.g. [1,1,1,1,0] 0; - prepend a zero [0,1,1,1,1,0] I - incremental differences [1,0,0,0,-1] A - absolute values ...


7

Shakespeare Programming Language, 10 * (264 + 494) = 8650 7910 7580 Encoder: 264 bytes ,.Ajax,.Ford,.Act I:.Scene I:.[Exeunt][Enter Ajax and Ford]Ajax:Open mind.Be you nicer zero?You be the sum ofyou the product ofthe sum ofI a big big pig the sum ofa big big big big cat a big big pig.If soSpeak thy.Ford:You are the sum ofyou a cat.If soLet usAct I. Try ...


6

Ostrich, 46 45 chars Don't have a version number in the header because this is actually just the latest commit. I added the O (ascii code to string) operator after releasing the latest version (but still before this challenge was posted). {a95,{32+O}%:d3@{:x\.3@?3@\+\x-x\+}/;{d=}%s*} Explanation: a this is the "r" array (a is short for [], ...


6

Mathematica, 67 61 bytes Print[""<>Alphabet[][[Fibonacci@Input[]~IntegerDigits~26+1]]] Calculates f(1000000) in about 51 milliseconds.


6

CJam, 20 '¡,q{_C#c' ,C+@|}fC; Try it online Explanation: '¡, make a string of characters with codes from 0 to 160 (a modified "d") could have been to 126 but stackexchange doesn't like the DEL character q read the input (s) {…}fC for each character C in s _ duplicate the d string C# find the index of C in d c ...


5

Simplex v.0.6, 35 bytes Sometimes I sigh and think, "Is this even worth submitting? It doesn't win, so why bother?" In response, I think, "Heck. It was fun. Besides, this is really fancied-up brainf*** anyhow. Not too shabby." 5_*Ij1~SRpRi@T[Uj&ERp]pSR5_Vj26@pWo 5_ ~~ sqrt(5) * ~~ copy ...


5

JavaScript (ES6), 130 bytes I/O: array of bits ↔ integer Encoder, 71 bytes a=>a.map(p=k=>r*=(g=i=>n%--i?g(i):i<2?n:g(++n))(n+=p^(p=k)),r=1,n=2)&&r Try it online! Decoder, 59 bytes D=(n,k=2,x=b=0)=>k>n?[]:n%k?D(n,k+1,1):[b^=x,...D(n/k,k,0)] Try it online!


5

APL (Dyalog Unicode), 49 bytesSBCS ⊃x⌽⍨⊃i-4×2-⌊1.8⊥⍋1↓i←⎕⍳⍨x←,⍉'CDHS'∘.,2↓⎕D,'TJQKA' Try it online! Overview: 'CDHS'∘.,2↓⎕D,'TJQKA' generates the outer product, so a 2d matrix with (C2 C3 C4 ...), (D2 D3 D4 ...), .... We then transpose this matrix to get (C2 D2 H2 ...), ... and then flatten that. Thanks to @ngn for the 2-⌊1.8⊥, which takes the order of ...


5

Python 2.7, 31 * (52 + 37) = 2759 Encoder (69 52 bytes): lambda n:[chr(i)if n&(1<<i)else""for i in range(32)] Decoder (41 37 bytes): lambda s:sum([1<<(ord(c))for c in s]) Stores the all the non-zero bits in the input number as ascii values. The value of the ascii character stores the position of the set bit. For example the value 'a' ...


5

Python 3, 8 * (45 + 38) = 664 Encoder (45 bytes): lambda n:[16*i+(n>>4*i)%16 for i in range(8)] Decoder (38 bytes): lambda l:sum(x%16<<x//16*4 for x in l) Try it online!


5

Stax, score 8 × (10+9) = 152 Encoder, 10 bytes Ç·MÉJ'♀τ│½ Run and debug it 16|E{i16*+m Full program, implicit input 16|E Get hexadecimal digits { m Map: i16*+ Add 16 * loop index Implicit output as string The encoder outputs the string in an increasing order. Decoder, 9 bytes üL∟n╫k∞‼9 Run and debug it o{16%...


5

05AB1E, 8 maximum length * (8 + 7) bytes = 120 Encoder (8) hεHN16*+ Try it online! Decoder (7) {16%hJH Try it online! Uses the same technique as wastl and Jonathan.


4

CJam, 50 48 bytes l_{}`:T&1${_T#)_(@a?)+}%{(\2b)*}%@e`{(2b1>Tf=}%? Thanks for Dennis for saving 2 bytes. Try it online. Explanation l_ e# Read and duplicate input. {}`:T e# T = "{}" & e# If the input has '{ or '}: 1$ e# Input. { e# For each character: _T#) e# If it is '...


4

Pyth, 17 bytes s@LGjhu,eGsGQU2lG Try it online.


4

Python 2, 299 bytes Here's my attempt at an answer. The Huffman codes are different from the examples given, but should still be optimal. i=raw_input();m=n=[(c,i.count(c))for c in set(i)] while n[1:]:n.sort(key=lambda x:(x[1]));(a,b),(c,d)=n[:2];n=[((a,c),b+d)]+n[2:] n=n[0][0] r=[] def a(b,s): if b[1:]:a(b[0],s+'0');a(b[1],s+'1') else:r.append(b+(s if s[...


4

Stax, 11 bytes, 0 penalty, Score 11 This program uses [0-9A-P] for input and [01] for output. ö■▄←·ï↨≡⌐╠H Run and debug it online - click the run button to start. The first four test cases run in milliseconds. The fifth in seconds. The sixth in millenia. The corresponding ascii representation of this program is this. A$21*,26|bD|N It leans heavily ...


4

Retina, 218 208 bytes [JQK] 1$& T`AJQK`1123 *' G0` \d+ 5** ' G, 1,` T`CD\HS`d \d * /^(_+)¶\1/(/¶(_+)¶\1/(K`1 /^(_+)¶_+¶\1/(K`2 ))K`4 /^(_+)¶_+¶\1/(K`3 /¶(_+)¶\1/(K`5 )))K`6 \d $+3-$& (\d+)-(\d+) $1*_$2* _{13}(_+)|(_{1,13}) $.($1$2 Try it online! Explanation: [JQK] 1$& T`AJQK`1123 Replaces Aces, Jacks, Queens and Kings with 1, 11, 12 and 13. ...


4

JavaScript (ES6), 8 * (40 + 32) = 576 The encoder outputs an array of \$0\$ to \$8\$ integers. The decoder takes the same format as input. Encoder (40 bytes) E=(n,k=0)=>n?[k|n&15,...E(n>>4,k+16)]:[] Decoder (32 bytes) s=>s.map(c=>s|=c%16<<(c/4&~3))|s Demo Try it online! How? The input is divided into 8 blocks of 4 bits ...


4

Jelly, (8 + 9) bytes * 8 max-length = 136 b⁴+J’Ɗ⁴¡ Encoder (footer formats the list as Python would for clarity) Ṣ_J‘Ɗ⁴¡ḅ⁴ Decoder It's theoretically possible to have a max-length of six, can that be done in 22 bytes or fewer? It's impossible with a max-length of five since \$\sum_{i=0}^{i=5}\binom{127+i}{127}=321402081< 2^{31}-1\$ How? Since \$2^{...


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