Hot answers tagged

20

Ruby, 128 .. 51 49 bytes ->w{%w(o ~).map{|c|w.scan(/_*._*(.)/).count [c]}} Try it online! No error checking. How: Ignore all underscores All mice going towards the piper have the head in an odd-numbered (0-based) position. All mice heading away from the piper have the tail in an odd-numbered position. Take every 2nd character (skipping underscores), ...


16

sed, 12 s/.(.)/&\1/g Try it online!


13

Python, 45 43 38 27 bytes For inputs up until 239999: lambda n:n/100%100<60>n%100 You can try it online! Thanks @Jitse and @Scurpulose for saving me several bytes ;) For inputs above 239999 go with 36 bytes: lambda n:n/100%100<60>n%100<60>n/4e3


12

brainfuck, 13 or 11 bytes >,[.,[..,>]<] Assumes EOF->0. :( You can try it online! (Assuming input also continues to give zeroes indefinitely after the first EOF, we can do it in 11 bytes: ,[.,[..>],] though this eats memory.)


11

Jelly, 3 bytes ḤÐe A full program accepting a string which prints the result Try it online! How? Utilises the fact that the implementation of the double atom, Ḥ, is multiplication by two, and that strings are lists of Python characters. In Python when a character is multiplied by two it becomes a Python string of length two (e.g. 'x'*2=='xx'). Lastly ...


10

Python, 38 bytes f=lambda s:s and s[:2]+s[1:2]+f(s[2:]) Try it online! Takes the first two characters, then the second character, then removes the first two characters and recurses. 39 bytes f=lambda s,c=2:s and s[:c]+f(s[1:],3-c) Try it online! Flips between taking the first two or first one character.


10

x86-16 machine code, IBM PC DOS, 14 bytes Binary: 00000000: b401 cd21 f7d9 78f8 b40e cd10 ebf2 ...!..x....... Unassembled: START: B4 01 MOV AH, 1 ; DOS read char from STDIN CD 21 INT 21H ; put char in AL F7 D9 NEG CX ; toggle CX sign 78 F8 JS START ; if negative, loop to next char B4 0E MOV AH, 0EH ...


10

Python 3.8, 75 68 97 63 bytes lambda s,n:max(l:=[s[j:j+n]for j in range(len(s))],key=l.count) You can try it online! Increased byte count because, as @xnor kindly pointed out, Python's str.count doesn't count overlapping substrings... But then @xnor's reformulation of what I was doing allowed to slice a third of the bytes! How: l:=[s[j:j+n]for j in range(...


8

J, 13 bytes [:;_2|.\_3<\] Try it online! Example input: 'flybuys' _3<\] Box every group of 3 ┌───┬───┬─┐ │fly│buy│s│ └───┴───┴─┘ _2|. Split that into groups of 2, and reverse each one: ┌───┬───┐ │buy│fly│ ├───┼───┤ │s │ │ └───┴───┘ [:; Raze the result: buyflys


8

05AB1E, 13 11 10 bytes '_KāÈÏaTS¢ Port of @GB's Ruby answer, so make sure to upvote him!! -1 byte thanks to @Grimmy by taking the input as character-list. Try it online or verify all test cases. Explanation: '_K '# Remove all "_" from the (implicit) input-list ā # Push a list in the range [1, length] (without popping the list) È ...


8

Perl 6, 33 25 bytes -7 bytes thanks to Kevin Cruijssen 60>*.polymod(100,100).max Try it online!


7

JavaScript (Node.js),  608 597 586  551 bytes Takes input as (source)(input), where input is an array containing single characters and/or integers. s=>I=>eval([..."@CDEJKLMNQTUWYZ^_bkmqw"].reduce((s,c)=>(a=s.split(c)).join(a.pop()),"(R=X=>(H={},o=P=[],S=[],z=x=p=i=0,gUs[p]?~(j=` \n`.indexOf(s[p++]N?j:gK:3,hUgK<2?Mn*2+j):V=n,GUx=...


7

Haskell, 33 24 bytes By noticing that obviously f [] = [] also fits the pattern f s = s and removing one extra space, we get to f(a:b:s)=a:b:b:f s f s=s from my starting point of f []=[] f (a:b:s)=a:b:b:f s f s=s Now my answer matches xnor's Haskell answer. You can try it online!.


7

Perl 6, 24 bytes {S:g/(...)(..?.?)/$1$0/} Try it online! Simple regex substitution that swaps every pair of three and up to three.


7

JavaScript (Node.js),  80 66 65 58  57 bytes Returns [towards, away]. s=>Buffer(s).map(c=>c%3?i^=~c:a[c&1]+=++i&1,a=[i=0,0])&&a Try it online! How? An efficient way to determine whether the character is a mouse body part is to take the ASCII code modulo \$3\$. Quite conveniently, we can also distinguish between P and _ ...


7

Ruby, 98 95 94 bytes Really simple string interpolation. I don't think there's all that much to optimize here that will save a lot of bytes. Takes the byte count as a parameter in order to save 3 bytes, as per Kevin Cruijssen's suggestion. ->n,b,c,l{"# #{n}, #{b} bytes #{c.gsub /^/,' '*4} You can [try it online][tio]! [tio]: "+l} Try it online!


7

05AB1E, 5 bytes ŒIù.M Try it online or verify the smaller test cases or verify the larger test case (which is a bit slow). Explanation: Œ # Push all substrings of the (implicit) input-string Iù # Only keep substrings of a length equal to the second input-integer .M # Only keep the most frequent item of the remaining substrings # (...


7

JavaScript (ES9),  88 85  82 bytes Takes input as (string)(n). s=>g=n=>n?s.replace(eval(`/\\b${s.match(/(?<=^| )./g).join``}/g`),`(${g(n-1)})`):s Try it online! Commented s => // s = string g = n => // g is a recursive function taking the counter n n ? // if n is not ...


6

J, 8 bytes #~1 2$~# Try it online! 1 2$~# Shape $~ the list 1 2 (repeating it cyclically as needed) until it matches the length # of the input. #~ Use this 1 2 1 2... "mask" to Copy #~ the implicit input elementwise. That is, make 1 copy of the first char, 2 copies of the 2nd char, 1 copy of the third char, etc.


6

Haskell, 24 bytes f(a:b:t)=a:b:b:f t f s=s Try it online!


6

Octave / MATLAB, 16 bytes @(s)s(1:2/3:end) Try it online! Explanation Non-integer values are automatically rounded (with a warning) in colon index expressions.


6

APL (Dyalog), 41 38 bytes -3 bytes thanks to Bubbler 2⌽'mehttp://',∊'.',¨⍨⎕A⍴¨⍨⌊1+62×.7*⍒⎕A Try it online! Outputs the URL with the letters capitalised. Uses the 0 indexed formula \$ \lfloor 1 + 62 \times 0.7^{25-x} \rfloor \$, since \$ (\frac{10}{7})^{x-25} = ((\frac{7}{10})^{-1})^{x-25} = (\frac{7}{10})^{25-x}\$ Explanation: 2⌽ ...


6

05AB1E, 25 24 23 21 bytes žXAS.7Ƶ-t∞-m>×….me¬ýJ -1 byte thanks to @Neil's analysis that *(10/7)** is the same as /.7**. -3 bytes thanks to @Grimmy using a different formula and ingenious use of ý! Try it online. Explanation: The formula used to get the correct amount of characters of the alphabet, where \$n\$ is the 1-based index of the alphabetic ...


6

Python 2, 47 bytes Port of G B's Ruby answer. lambda s:map(s.replace('_','')[::2].count,'~o') Try it online!


6

05AB1E (legacy), 56 53 52 51 50 bytes “# ÿ, ÿ¡Ï“I4ú»"[tio]"D“You€© [try€•€Ø]ÿ!“s¶·ý„: IJ Inputs in the order name, byte-count, [code-lines], link, where the [code-lines] is a list of lines. Try it online. 50 bytes alternative (credit to @Grimmy): “:# ÿ, ÿ¡Ï“I4ú»“You€© [try€•€Ø][tio]!“Â6£R¨¶·ýÀ$ú« Try it online. Explanation: “# ÿ, ÿ¡Ï“ # Push ...


6

APL (Dyalog Extended), 33 32 bytes -1 byte thanks to Bubbler. Anonymous infix lambda. Takes count as left argument and string as right argument. {('\b',⊃¨⍵⊆⍨≠⍵)⎕R(1⌽')(',⍵)⍣⍺⊢⍵} Try it online! {…} "dfn"; number is ⍺ and text is ⍵:  ⊢⍵ on the text:  ⍣⍺ repeat given number of times  (…)⎕R(…) PCRE Replace:   ')(',⍵ the text prefixed by ")("   1⌽ ...


6

Gema, 8 characters <J>="$0" Copy of Adám's QuadR solution, just adjusted the syntax and the explanation: Replace  <J> one or more lowercase letters with  "$0" the quoted match. Sample run: bash-5.0$ gema '<J>="$0"' <<< '[abc,def,ghi,jkl]' ["abc","def","ghi","jkl"] Try it online!


6

APL (Dyalog Extended), 19 bytesSBCS -10 bytes thanks to Kevin Cruijssen. Anonymous tacit prefix function. Takes argument as integer. ⍱59<100∘⊤ Try it online! 100∘⊤ convert To base-100 59< are they, each, greater than 59? ⍱ are none of them true?


5

Python 2, 34 bytes (xnor's improvement) lambda s:`[c*11for c in s]`[7::10] Try it online! Python 2, 36 bytes lambda s:`sum(zip(s,s,s),())`[7::10] Try it online! Slices characters 7, 17, 27… out of a string like this: ('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'e', 'e', 'e') ...


5

Gema, 13 characters <u3><u3>=$2$1 Sample run: bash-5.0$ echo -n 'flybuys' | gema '<u3><u3>=$2$1' buyflys Try it online! / Try all test cases online!


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