Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

Hot answers tagged

59

Perl 6, 36 30 bytes {<Spock Lizard Rock>[.comb%4]} Try it online! Gets each output based on the length of the input modulo 4. I think this is probably the optimal strategy.


24

Python, 28 bytes lambda s:s[:(s+s).find(s,1)] Try it online! The length of the output is the first nonzero position starting from which s can be found in the doubled s+s. For example: s = abcabcabc s+s = abcabcabcabcabcabc abcabcabc ^ s starting at position 3 (zero-indexed) 46 bytes f=lambda s,p='':p*(s+p==p+s)or f(s[1:],...


22

JavaScript (ES6), 49 bytes s=>s.replace(/[beis]/gi,c=>parseInt(c+1,31)%9||8) Try it online! How? By using parseInt(), we don't have to worry about the case at all since lowercase and uppercase letters are parsed the same way. The downside of this formula is that we have to explicitly turn \$0\$ into \$8\$. It's still 1 byte shorter than the Node ...


19

Python 3, 21 bytes lambda a,b:{*a}>={*b} Try it online!


18

Perl 5 -p, 28 bytes $_=/k/?Paper:/i/?Rock:Lizard Try it online! Tried @JoKing's length mod 4 method, but this turned out to be even shorter.


15

Python 2, 77 76 bytes lambda s:'VFULJSGEalmeoylspaballaporrftvc'[hash(s)%3695%8::8]+'reon'[s<'V':] Try it online! In Python 2, the hash function consistently (across runs and also on different platforms) converts any immutable value into a 64-bit integer. By taking that integer first mod 3695, then mod 8, we get an integer 0<=i<8 (3695 was found ...


15

APL (Dyalog Unicode), 20 12 bytesSBCS -6 bytes through dzaima Full program. Prompts stdin for: texture as a list of strings number of rows in shape matrix number of columns in shape matrix indices of falsies (to be blanks) in the shape matrix All the ⎕s are supposed to be rectangles (they're not tofu). They symbolise a computer console which here means ...


14

Perl 6, 72 67 65 64 bytes {<Sylv Esp Glac Jolt Leaf Umbr Flar Vapor>[:36($_)%537%8]~'eon'} Try it online! A golf to all unique indexes by parsing the string as base 36 and moduloing it. I've left a version that just uses the sum below so other answer that may not have the same short base converting code that Perl 6/Raku does. There is also base 35 ...


14

JavaScript (ES6), 98 bytes n=>` _/|2 | |\\0 |_ |/1 \\|3`.replace(/\d/g,k=>n?(k&1?' /':' \\').repeat(n/33^n>98):['/\\'[k]]) Try it online! How? We use the following template: _/|2 | |\0 |_ |/1 \|3 If \$n=0\$ (volume muted): \$0\$ is replaced with / and \$1\$ is replaced with \ the other digits are removed If \$n\neq 0\$: odd ...


13

PHP, 39 bytes Shorter md5 variants by Christoph and Benoit Esnard. <?=[Lizard,Spock,Rock][md5(y.$argn)%3]; Try it online! <?=[Spock,Rock,Lizard][md5($argn.m)%7]; Try it online! PHP, 40 bytes <?=[Rock,Spock,Lizard][md5($argn)[5]%3]; Try it online! Takes sixth character of md5 of input, which gives unique number of 1 for "Rock" and "Scissors",...


12

R, 28 bytes split(w<-scan(,""),nchar(w)) Try it online! split does the trick; results in a list() where the element names are the lengths and the elements are vectors containing the words.


11

J, 23 21 20 bytes >&#*]e.1}:@}.-&#]\.[ Try it online! -1 byte thanks to Bubbler Return true if: >&# left arg is strictly longer than right (see the "friend"/"friend" test case) * and... ] the right arg e. is an element of the list formed by... 1 }:@}. removing the first and last elements of... -&# ]\. [ all the outfixes \. of the ...


10

Perl 6, 28 bytes {($^a,*.ords.join...*)[$^b]} Try it online! Explanation: { } # Anonymous codeblock ( ...*)[$^b] # Index into an infinite list $^a, # Starting from the given number * # Where each element is .ords.join # The ordinal values ...


10

Retina, 78 73 bytes (0(8|[569].?.?|..?0?)(?=[^0]....))?(...?)?(...?)(..)$ $1$#1*-$3$#3* $4 $5 Inspired by both @mazzy's PowerShell answer and @NahuelFouilleul's Perl 5 answer, so make sure to upvote them both as well! -5 bytes thanks to @Neil. Try it online. Explanation: Match the digit groups like this: ( )? # An ...


10

QuadR with i flag, 15 bytes B E I S 8 3 1 5 Try it online! I don't need to explain this, do I?


9

Haskell, 13 bytes all.flip elem Try it online! Haskell doesn't have built-in set or subset functions, so we need to do it ourselves. This is a pointfree version of 17 bytes a%b=all(`elem`a)b Try it online! which it itself shortened from 22 bytes a%b=and[elem c a|c<-b] Try it online!


9

Canvas, 9 bytes m⤢m⤢;n← ╋ Try it here! Abusing that the input will be ASCII and so won't contain ←, but the shape is made of spaces and ← for easy overlapping and replacement. Alternative 12 bytes taking shape as space and #.


8

05AB1E, 3 bytes FÇJ Takes N as first input and the digit as second. Try it online or verify all test cases. Explanation: F # Loop the (implicit) first input (N) amount of times Ç # Convert the characters in the string at the top of the stack to its unicode values # (which will take the second input implicitly in the first iteration) J # ...


8

Python3, 86 84 83 80 77 76 bytes lambda a,b:len(a)<len(b)*any(a==b[:i]+b[i-len(a):]for i in range(1,len(a))) Try it online! -2 bytes thanks to @Value Ink whitespace before and after == -1 byte remove whitespace. -3 bytes thanks to @ovs using any to write for loop in one line. -3 bytes by replacing and with * -1 byte by replacing def with lambda ...


8

Perl 5 (-p), 92, 84 bytes s/(0(8|[569].?.?|..?0?)(?=.{5})(?!0))?(...?)?(...?)(..)$/$1-$3 $4 $5/;s/^\D*|-\K //g Try it online!


8

05AB1E, 14 13 12 bytes „ÛãbÂu«ŽKcº‡ -1 byte thanks to @Grimy by using the dictionary word sie. Try it online. 12 bytes alternative (from @Grimy): 2F.š„ÛãbŽKc‡ Try it online. Explanation: „Ûãb # Push dictionary string "sieb"  # Bifurcate it (short for Duplicate & Reverse copy) u # Uppercase the copy « #...


7

APL (Dyalog Unicode), 28 bytes 'Spock' 'Lizard' 'Rock'⊃⍨4|≢ Try it online! Uses the simple strategy based on input length modulo 4 (idea from Jo King's answer). ⎕IO←0. APL (Dyalog Unicode), 34 bytes 'Lizard' 'Rock' 'Paper'⊃⍨'Xbj'⍸2∘⊃ Try it online! Uses the shortest choice for all cases. ⎕IO←1. How it works 'Lizard' 'Rock' 'Paper'⊃⍨'Xbj'⍸2∘⊃ 2∘⊃ ...


7

JavaScript (ES6),  498 ... 447  445 bytes Outputs in lowercase, except for 'I'. s=>(s.replace(/\w+/g,w=>(x='2want||2eat/5food||5you|2see/5eye|5thing|1I/4me|5person|8many|8bad|5fruit|1he/4him|5tool||2fix/8good||2have||8one'.split`|`[n=parseInt(w,35)%781%21])&&(x=(g=_=>x.split`/`.find(x=>x[0]>>t&1,t++)||g())(),o[t]=x....


7

05AB1E, 3 bytes η¢Ï Try it online! η # prefixes of the input ¢ # count the number of occurences of each one within the input Ï # filter the input, keeping only indices where the above is 1 Prefixes that stop short of the last repetition of the pattern can be found multiple times in the input, offset by a pattern-length. Thus, this ends up ...


7

Ruby -p, 89 67 bytes Direct port of Jo King's Perl answer. By sheer coincidence, the byte count is even the same. The byte counts are no longer the same as Jo King has switched to using a technique that cannot be tersely replicated in Ruby. $_=%w"Leaf Esp Umbr Vapor 0 Sylv Flar Jolt Glac"[$_.sum%64%9]+'eon' Try it online! Original Version, Ruby -p, 89 ...


7

05AB1E, 65 64 bytes „ /SõD‚„\ ‚D4δ∍D)T•4a֙镓| 0\ 1/“ÅвJrI33÷IĀ+©è‡∊'_1•uтÄ•22в®è‚ǝ When I started I thought I would be able to make this pretty short with the approach I had in mind, but it had some annoying edge cases to fix.. Can definitely be golfed, though. Will take another look later on. Try it online or verify some more test cases. Explanation: ...


7

C# (Visual C# Interactive Compiler), 20 bytes a=>b=>(char)(a+b>>1) Try it online!


7

PowerShell, 135 127 118 107 105 bytes -1 byte thanks @Kevin Cruijssen $args-replace'(0(8|[569].?.?|..?0?)(?=[^0].{4}))?(...?)?(...?)(..)$','$1-$3 $4 $5'-replace'^\D+|(?<=\D) ' Try it online!


7

Zsh, 83 79 78 bytes -4 bytes by changing to a recursive solution, (-0 from bugfix,) -1 by abusing unquoted empty parameters expanding to 0 words <<<${1+"${(#)$((x=#1,x>126?x+9122:(x<9)^(x<11)^(x<32)?x+9216:x))}`$0 ${1:1}`"} Try it online! Try it online! Try it online! In arithmetic contexts, #1 is the code of the first character of $...


7

JavaScript (Node.js),  67  66 bytes Saved 1 byte thanks to @Grimmy s=>s.replace(/[^ -~ \n]/g,c=>(B=Buffer)([226,144,B(c)[0]%94+128])) includes a literal TAB Try it online! How? We match all characters that are: +-------> neither printable | +----> nor a tabulation | | +--> nor a linefeed / \ | | [^ -~\t\n] and ...


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