Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
3 added 59 characters in body
source | link

Haskell, 6161 58 bytes

n!a=[]#a where c#a|let(a:b)|elem#c|elem a c=c#b|1<2=1+takec=b#c|1<2=1+b#take n(a:c)#b;_#_=0;_#_=0=a#[]

Try it online!Try it online!

n!a        a|      =a#[]     -- take input 'n' and a list 'a'
   =[]#a                 -- and call # with the initial list and an empty cache 
 andlet the initial list

   c#          -- bind function '#':
  (a:b) #c           -- if there's at least one element 'a' left in the list
       |elem a c=c#bc=b#c  --  and it's in the cache, go on with the same cache
                      --  and the remainder of the list
       |1<2=          -- else (i.e. cache miss)
            1+        --  add one to the recursive call of
         takeb# n(a:c)  --  the first n elements of 'a' prepended-- to the cache
 remainder of the list and  
       take n(a:c)  -- #b the first --n elements andof the'a' remainderprepended ofto the listcach
   _#_=0              -- if there's no element in the list, return 0

Edit: -3 bytes thanks to @Lynn.

Haskell, 61 bytes

n!a=[]#a where c#(a:b)|elem a c=c#b|1<2=1+take n(a:c)#b;_#_=0

Try it online!

n!a                   -- take input 'n' and a list 'a'
   =[]#a              -- and call # with an empty cache and the initial list

   c#(a:b)            -- if there's at least one element 'a' left in the list
       |elem a c=c#b  --  and it's in the cache, go on with the same cache
                      --  and the remainder of the list
       |1<2=          -- else (i.e. cache miss)
            1+        --  add one to the recursive call of
         take n(a:c)  --  the first n elements of 'a' prepended to the cache
                 #b   --  and the remainder of the list
   _#_=0              -- if there's no element in the list, return 0

Haskell, 61 58 bytes

n!a|let(a:b)#c|elem a c=b#c|1<2=1+b#take n(a:c);_#_=0=a#[]

Try it online!

n!a|      =a#[]     -- take input 'n' and a list 'a'
                    -- and call # with the initial list and an empty cache 
 let                -- bind function '#':
  (a:b)#c           -- if there's at least one element 'a' left in the list
     |elem a c=b#c  --  and it's in the cache, go on with the same cache
                    --  and the remainder of the list
     |1<2=          -- else (i.e. cache miss)
          1+        --  add one to the recursive call of
       b#           --  the remainder of the list and  
       take n(a:c)  --  the first n elements of 'a' prepended to the cach
 _#_=0              -- if there's no element in the list, return 0

Edit: -3 bytes thanks to @Lynn.

2 added 708 characters in body
source | link

Haskell, 61 bytes

n!a=[]#a where c#(a:b)|elem a c=c#b|1<2=1+take n(a:c)#b;_#_=0

Try it online!

n!a                   -- take input 'n' and a list 'a'
   =[]#a              -- and call # with an empty cache and the initial list

   c#(a:b)            -- if there's at least one element 'a' left in the list
       |elem a c=c#b  --  and it's in the cache, go on with the same cache
                      --  and the remainder of the list
       |1<2=          -- else (i.e. cache miss)
            1+        --  add one to the recursive call of
         take n(a:c)  --  the first n elements of 'a' prepended to the cache
                 #b   --  and the remainder of the list
   _#_=0              -- if there's no element in the list, return 0

Haskell, 61 bytes

n!a=[]#a where c#(a:b)|elem a c=c#b|1<2=1+take n(a:c)#b;_#_=0

Try it online!

Haskell, 61 bytes

n!a=[]#a where c#(a:b)|elem a c=c#b|1<2=1+take n(a:c)#b;_#_=0

Try it online!

n!a                   -- take input 'n' and a list 'a'
   =[]#a              -- and call # with an empty cache and the initial list

   c#(a:b)            -- if there's at least one element 'a' left in the list
       |elem a c=c#b  --  and it's in the cache, go on with the same cache
                      --  and the remainder of the list
       |1<2=          -- else (i.e. cache miss)
            1+        --  add one to the recursive call of
         take n(a:c)  --  the first n elements of 'a' prepended to the cache
                 #b   --  and the remainder of the list
   _#_=0              -- if there's no element in the list, return 0
1
source | link

Haskell, 61 bytes

n!a=[]#a where c#(a:b)|elem a c=c#b|1<2=1+take n(a:c)#b;_#_=0

Try it online!