Tag Info

Hot answers tagged subsequence

46

Word VBA, 199 147 126 116 102 100 87 85 Bytes Why emulate when you can do?! Declared function in the ThisDocument module that takes input n in the form of Array(true,true,false,true) and outputs to the Word font size selector :P Golfed: Sub a(n):Set f=Content.Font:For Each i In n If i Then f.Grow Else f.Shrink Next:End Sub Ungolfed: Sub a(n) Set f=...

44

Brainfuck, 46 45 (63 with printable characters in input) Compatible with Alex Pankratov's bff (brainfuck interpreter used on SPOJ and ideone) and Thomas Cort's BFI (used on Anarchy Golf). The printable version takes the array first as a string, followed by a tab, followed by the starting string with no trailing newline. Demonstration on ideone. -[+>,--...

30

Python 2, 46 45 bytes f=lambda n,k=1:kin bin(n^n/2)and-~f(n,k*10) Try it online! How it works By XORing n and n/2 (dividing by 2 essentially chops off the last bit), we get a new integer m whose unset bits indicate matching adjacent bits in n. For example, if n = 1337371, we have the following. n = 1337371 = 101000110100000011011₂ n/2 = 668685 = ...

29

convey, 59 47 bytes { ?;\,&:1< v^<^ ,<$1 "=>^}"@"} >">>#=" '*'>:<=0 Try it online! Run with tas and tsase (output is t*as*): Split the input into two streams ?;\… ^<, then always take one letter :1, compare it with the head =, and either @ put the letter back into the queue , and output a '*'$…}, ...

28

Python 2, 49 bytes Takes as input a 2D binary matrix $a$, and its size $n$. lambda a,n:sorted(map(sum,a+zip(*a)))[-2:]==[1,n] Try it online! There may be shorter approaches, but this is what I could find for now. Please let me know if the algorithm is incorrect. Explanation Since there is exactly one line, there should be exactly one row/column ...

24

Ruby, 50 44 43 bytes FGITW answer. Gotta go fast! Thanks to @Neil for saving 6 bytes. Oh right, crossed out 44 is still 44 ->m,t{m.gsub(/\d+/){eval$&+t.sub(?^,'**')}} 24 Wolfram Language (Mathematica), 34 bytes 0~Range~19~Binomial~i~Sum~{i,0,#}& Try it online! The tier $n$ metasequence is the sum of the first $n+1$ elements of each row of the Pascal triangle. 18 Haskell, 34 bytes (iterate(init.scanl(+)1)[1..20]!!) Uses 0-indexed inputs (f 4 returns tier 5.) Haskell, 36 bytes f 1=[1..20] f n=init$scanl(+)1$f$n-1 Try it online! Uses 1-indexed inputs (f 5 returns tier 5.) Explanation scanl (+) 1 is a function that takes partial sums of a list, starting from (and prepending) 1. For example: scanl (+) 1 [20,300,...

17

Python 2, 46 bytes f=lambda n,r=1:max(r,n and f(n/2,1+~-n/2%2*r)) Try it online Extracts binary digits from n in reverse by repeatedly taking n/2 and n%2. Tracks the length of the current run r of equal digits by resetting it to 0 if the last two digits are unequal, then adding 1. The expression ~-n/2%2 is an indicator of whether the last two digits are ...

17

JavaScript (Node.js),  819  779 bytes Returns 0 for undefined TIO uniqueness. s=>(B=Buffer)('J .!> . 8 " . . 0 L"0"c$E"~~~="8"~1 Y$^"~~~j S$x"P S$;"7 @ Z G"/$8$A :%X!T%A G M$c*h"d"L!Q$8"~F$.$>"s$@"`"E 3 1$r"U">"8 F&~Y"="z"~Q"F&...

17

Python 3, 47 bytes f=lambda x,*a:f(*a,x[1:],x[:-1])if sum(x)else x Try it online! It's simple breadth-first search, implemented recursively. The if...else is slightly bothering me; it feels like there is an improvement somewhere...

16

JavaScript (ES6), 35 bytes Expects (haystack)(needle), where haystack is a list of characters and needle is either a list of characters or a string. Returns a list of characters. The blank-out character is 0. s=>w=>s.map(c=>c==w[s|=0]?++s&&c:0) Try it online! Commented s => // s[] = sentence as an array of characters w => ...

15

Perl, 36 34 bytes s/\d+/"0|$&$^I"=~s#\^#**#r/gee The source code is 30 bytes long and it requires the switches -pi (+4 bytes). It takes the first input from STDIN, the second input as an argument to -i. Thanks to @DenisIbaev for golfing off 2 bytes! Test it on Ideone.

15

Jelly, 1 byte ṡ Jelly has a single byte dyadic atom for this very operation Try it online! (the footer splits the resulting list with newlines, to avoid a mushed representation being printed.)

14

CJam, 15 bytes rr{_C#)/(C@s}fC Try it online. How it works rr e# Read two whitespace-separated tokens from STDIN. { }fC e# For each character C in the second string. _ e# Duplicate the first string. C# e# Compute the index of the character in the string. )/ e# Add 1 and split the string ...

14

05AB1E, 6 bytes b.¡€gM Try it online! Explanation b # convert to binary .¡ # split at difference €g # map length on each M # take max

13

JavaScript (V8), 48 bytes a=>f=b=>b[2]?b[[x,...r]=f(b.slice(1)),1]<f?x:r:a Try it online! -2 bytes thanks to @Arnauld!

13

Brachylog, 8 bytes {z|}≡ᵛ+1 Try it online! {z|} Either the rows or the columns ≡ᵛ are all the same list +1 which sums to 1.

12

Perl, 52 bytes The solution is provided as function that takes the string as argument and returns a list of positions. One-based positions, case-sensitive search, without newlines: 52 bytes sub l{pop=~/(l).*?(l).*?(a).*?(m).*?(a)/;@+[1..\$#+]} The case-sensitive search returns an empty array in the example of the question, because after matching the ...

12

sed, 299+1 Yes, sed can find a llama. No, sed can't do math. This is the longest answer so far, at 299+1 characters, because I had to teach sed to count. This answer requires a sed with extended regular expressions (sed -E or sed -r). I used OpenBSD sed(1). Input is one string per line. (Therefore, the string may not contain a newline.) Output is a line of ...

12

C, 62 bytes f(char*s,char*c){while(*s-*c||putchar(*c++),*s)putchar(*s++);} Well, this is surprisingly competetive. We define a function f(char*, char*) that takes the string as its first input and the array of characters to duplicate as its second input. Some testing code: int main (int argc, char** argv) { f("onomatopeia", "oao"); ...

12

Python 2, 95 bytes l=[];n=input() exec"a=min(set(range(n))-{2*b-c for b,c in zip(l,l[1::2])});print-~a;l=[a]+l;"*n The main trick is in generating the numbers the new value must avoid. Keeping the reversed sequence so far in l, let's look at what elements might form a three-term arithmetic progression with the value we're about to add. ? 4 2 2 1 ...

12

Jelly, 10 7 bytes BṡRḄFS_ Try it online! How it works BṡRḄFS_ Main link. Input: n B Convert n to base 2. R Yield [1, ..., n]. ṡ Get all overlapping slices of lengths 1 to n. This yields empty arrays if the slice size is longer than the binary list. Ḅ Convert each binary list to integer. F Flatten the ...

12

Python 2, 88 bytes def f(a,b,c,o=""): for q in a:x=q==b[:1];o+=c[:x]or q;b=b[x:];c=c[x:] print[o,a][c>''] A function that takes in the three strings and outputs the result to STDOUT. The function simply does one pass over the string, taking the appropriate char and updating b,c as we go. For testing (after replacing the print with return): S = """ &...

12

JavaScript (ES6), 103 101 bytes Takes input as an array of -1 / 1. a=>a.map(k=>[1,12,28,36,48,72,80,1630,1638].map((v,i)=>n+=n>v&&k*[1,1,6,4,12,-16,2,-2,-8][i]),n=11)|n Test let f = a=>a.map(k=>[1,12,28,36,48,72,80,1630,1638].map((v,i)=>n+=n>v&&k*[1,1,6,4,12,-16,2,-2,-8][i]),n=11)|n console.log(f([])); ...

12

J, 11, 9 8 bytes -1 byte thanks to miles! [:+//.]\ How it works? The left argument is s, the right one - L ]\ - splits L into sublists with length s /. - extracts the oblique diagonals (anti-diagonals) +/ - adds them up [: - makes a fork from the above verbs Here's an example J session for the first test case: a =. 1 2 3 4 5 6 7 8 9 ] 3 ]\ a ...

12

Java 8, 48 39 33 bytes s->"ROYGBRO BGYORBG".indexOf(s)|7 -6 bytes thanks to @RickHitchcock, so make sure to upvote him as well! Takes uppercase color as input-String. Outputs -1 for none, 7 for clockwise, and 15 for counterclockwise. Try it online. Explanation: s-> // Method with String parameter and integer return-type "...

12

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 ...

12

Brachylog, 7 bytes {|∧Ṣ}ᵐ⊇ Try it online! I should not be surprised to learn that it's considerably faster if I put the ⊇ at the end. Takes the original marquee through the input and the target through the output. { }ᵐ The input variable, with each element | possibly ∧Ṣ replaced by a space, ⊇ has the output variable as a ...

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