New answers tagged

2

GNU-C-Preprocessor, 90 + O(48*n) Bytes There should be code golf challenges for C preprocessor only, my idea: #define T(x,...) x #define D(x,y...) y #define A(f,x,y) f(x,y) #define Z5(f,x,y) A(f,T x,T y),Z4(f,(D x),(D y)) #define Z4(f,x,y) A(f,T x,T y),Z3(f,(D x),(D y)) #define Z3(f,x,y) A(f,T x,T y),Z2(f,(D x),(D y)) #define Z2(f,x,y) A(f,T x,T y),Z1(f,(D x)...


2

GNU sed, 55 + 1(r flag) = 56 bytes GNU sed doesn't even have integers, not to mention functions, lists / arrays, etc. In order to participate, I used the following assumptions for the I/O methods, which hopefully respect the consensus regarding sed: each input list / array is given on a separate input line, with the integers separated by space. the black-...


3

Idris - 77 bytes z:(a->b->c)->List a->List b->List c z f(x::v)(y::w)=f x y::z f v w z _ _ _=[]


1

Batch, 99 bytes @set/pf= @set/ps= @call:c %s% @exit/b :c @set/ps= @for %%i in (%s%)do @call %f% %%1 %%i&shift Takes three lines of input, the command or batch function to execute, followed by two space or comma separated lists. Works fine with numbers but anything needing quoting is likely to crash and burn. Explanation: @set/pf= Input the command or ...


4

x86 machine code (8086), 15 bytes 00000000: AD 50 87 F3 AD 50 87 F3-FF D2 58 AB E2 F2 C3 .P...P....X.... Callable function. Expects CX = lengths of both lists, [SI] = address of first list, [BX] = address of second list, [DX] = address of function. Outputs towards [DI] = address of output list (buffer needs to be large enough). Disassembly: L: ...


1

Lua, 63 bytes load'o,f,a,b={},...for i=1,#a do o[i]=f(a[i],b[i])end return o' Try it online! Functions are 1st-class values in Lua, so this is pretty simple (if a bit long because of it's need to iterate).


11

Haskell, 22 bytes (.zip).(.).map.uncurry Try it online! Not particularly interesting, just a reimplementation of zipWith converted to pointfree form: \f x y -> map (uncurry f) (zip x y) \f x -> map (uncurry f) . zip x \f x -> (map (uncurry f) .) zip x \f -> (map (uncurry f) .) . zip \f -> (. zip) (map (uncurry f) .) \f -> (. zip) ((.) (...


4

Factor, 24 bytes [ zip swap f assoc>map ] Try it online! Avoids the obvious built-in 2map and its trivial generalizations nmap and 2map-as, and uses zip and assoc>map instead (so it works like e.g. the Rust answer). Takes the input from the stack in the order of func arr1 arr2. Small golfing tip: when using a function that takes an "exemplar"...


5

Husk, 2 bytes δm Try it online! (Implement the function in the footer, the header passes it to the main code) Sure, Husk has a trivial builtin solution for this (z), but here's the shortest interesting solution :) δ is a higher order function that turns something using an unary function on elements of one list into one using a binary function on elements ...


2

Stax, 6 bytes invocable, or 3 bytes toplevel Does not make sense to compress functions in stax so I didn't. The two arrays are passed on the stack, and the black-box function is passed in the x register. |\{x!m Explained: |\ Zip input arrays { x push contents of x register ! invoke top of stack m ...


2

Pure Zsh, 31 bytes b=($=3) for 2 3 (${${=2}:^b})$@ Try it online! Takes parameters function name, space-delimited list, space-delimited list With -o shwordsplit, one byte is saved with b=($3) instead. b=($=3) # =split $3 and save as array $b # ${${=2}:^b} # Zip $=2 and $b together for 2 3 (${${=2}:^b}) # Loop over 2 at a time ...


3

Wolfram Language (Mathematica), 11 bytes Inner[##,]& Try it online! Inner almost does zipwith - except, by default, it returns the sum of the results. 12 bytes #@@@(#2)& Try it online!  is \[Transpose].


2

Scala 3 (compile time), 87 bytes type Z[F[_,_],A<:Tuple,B]<:Tuple=(A,B)match{case(a*:b,c*:d)=>F[a,c]*:Z[F,b,d]case _=>A} Try it online! Takes tuples instead of lists. This isn't particulary interesting or complicated, but here's an explanation: type Z[ //Declare a type Z taking these arguments F[_,_], //A type F ...


2

Racket, 41 bytes (λ(x y f)(for/list([i x][j y])(f i j)))) Try it online! for/list is almost the necessary builtin, just needs some additional syntax.


3

Rust, 34 bytes |f,a,b|a.into_iter().zip(b).map(f) Try it online! Rust doesn't have a zipwith function but it does have a zip and a map function. Returns an iterator over the values.


1

Perl 5, 46 bytes sub{$f=shift;map&$f(@_[$_,$_+@_/2]),0..@_/2-1} Try it online!


4

Nim, 71 70 bytes iterator z[I](f:proc(x,y:I):I;a,b:seq):I= for i,x in a:yield x.f b[i] Try it online!


1

CJam, 8 bytes l'.l++~p Try it online! Or verify the first three cases: 1, 2, 3. Input is a line with (the string representation of) two arrays, and then a line with (the string representation of) the function, defined as a CJam block. How it works l e# Read line containing the two arrays '. e# Push character "." (which will do the actual job) ...


1

Icon, 52 bytes procedure z(a,b,f) write(f(a[i:=1to*a],b[i]))&\y end Try it online!


4

Java 8, 52 bytes A->B->{for(int l=A.length;l-->0;)A[l]=f(A[l],B[l]);} Puts the result in the first argument instead of returning a new integer-array (to save 25 bytes). Try it online. Explanation: A->B->{ // Method with two integer-array parameters & no return-type for(int l=A.length;l-->0;)// Loop `l` in the ...


2

Red, 69 bytes func[a b f][collect[repeat i length? a[keep compose[(f a/:i b/:i)]]]] Try it online! Printing the result instead of returning it: 61 bytes func[a b f][repeat i length? a[print compose[(f a/:i b/:i)]]] Try it online!


6

C (gcc), 49 bytes z(f,a,b,n)int*a,*b,(*f)();{for(;n--;f(a++,b++));} Try it online! Loops for 0-n, applying f on &a[i], &b[i]. Fairly simple. Ungolfed signature: void zipwith(void (*blackbox)(int *, int *), int *a, int *b, int length); This version expects a function like this: void func(int *a, int *b) { *a = *a OP *b; } It overwrites the ...


5

Husk, 5 bytes mF₁Te Try it online! Takes the two arrays as arguments, and the function in the footer. You can use any binary function which operates on two TNums from Husk's Commands page. Explanation mF₁Te Main program: accepts two arrays e two element list from the arrays T transpose m map each pair to F itself folded by ₁ the black ...


3

Zsh, 24 bytes for 1 2 (`paste $@`)f $@ Try it online! Uses a black-box function named f, with input from two files specified in command line arguments. However, paste alone is really just a zip, and this brings up some interesting debate depending on your interpretation of what counts as a "function" under the Unix philosophy of "everything ...


5

R, 32 bytes function(f,x,y)Vectorize(f)(x,y) Try it online! If R didn't already have a built-in (mapply/Map) for zipwidth, here's how one could implement it in 32 bytes... (...and if R didn't have Vectorize, we'd need to implement it the 'hard' way for 54 bytes: function(f,x,y){for(i in seq(a=x))T[i]=f(x[i],y[i]);T})


4

JavaScript (ES6), 32 bytes (F,a,b)=>a.map((x,i)=>F(x,b[i])) Try it online!


6

Trivial builtin answers Edit your answer into this post if it consists of a builtin which does the entire task by itself APL (Dyalog Unicode), 1 byte ¨ Try it online! Husk, 1 byte z Try it online! Jelly, 1 byte " Try it online! Julia, 3 bytes map Try it online! Python 2, 3 bytes map Try it online! R, 3 bytes Map Try it online! The canonical R ...


1

Lua (67 bytes) function x(t,n)return load("return "..table.concat(t,"+",1,n))()end Explanation: table.concat(t,"+",1,n) gives the string formed by concatenation of elements of t from 1 to n using + as connector, i.e. t[1]+...+t[n]. load(<string>)(), as its name suggests, loads a string to be executed as a function. In ...


1

Husk, 4 3 bytes Saved 1 byte thanks to Dominic Van Essen !¡∫ Try it online! !¡∫ ¡ Repeatedly apply the following function to the first argument, making an infinite list ∫ Cumulative sum (one cycle) ! Index into this list with the second argument


0

Wolfram Language (Mathematica), 37 35 bytes Merge[Rule@@@#,#~BinCounts~{1,6}&]& Try it online!


1

Lua, 114 112 103 bytes function(t,o)for i=1,#t do v=t[i]n,r=v[1],v[2]o[n]=o[n]or{n,{0,0,0,0,0}}o[n][2][r]=o[n][2][r]+1 end end Try it online! Explanation function(t,o) -- Anonymous function taking a table and the output table for i=1,#t do -- For every number from 1 to the length of t v = t[i] -- Get the current entree n, r = v[1], v[2] ...


0

Stax, 19 bytes ┬░╩íΩ{\ë╔B_m♦½─Γe╧¼ Run and debug it Input is given directly, no quotes. Explanation L{eL3B{|+mmM{3B{:*mm$|M Input: STDIN, auto split on newlines, pushed to stack L listify the stack { m map each line to: {e m evaluate it L listify the stack 3B ...


1

Jelly, 18 bytes ƈLпḊỴḲ€V+3\€×3\FṀ Try it online! Takes input from STDIN, space and newline separated Jelly, 9 bytes +3\€×3\FṀ Try it online! Takes a 20x20 matrix of integers as input. +4 bytes to input as a space and newline separated grid How it works +3\€×3\FṀ - Main link. Takes a matrix M on the left € - Over each row r: 3\ - Over ...


0

Husk, 18 bytes m§,o←←oλm#⁰ḣ5)m→k← Try it online!


2

C (gcc), 48 46 44 bytes -2 thanks to @ceilingcat f(int*a){*a=*a-a[-1]?a[-(*a==a[1])]:f(a+1);} Try it online!


1

Lua (LuaJIT), 119 96 79 bytes n=t;while 2~=n[2]do n={}for a=1,#t*2,2 do x=a-#t n[#n+1]=t[a]or t[x+x%2]end;t=n Try it online!


1

Husk, 11 bytes L↑S≠ŀ¡ȯΣTC2 Try it online!


1

Stax, 14 bytes ü≈:☻‼Xí┌ùß♦▲▬á Run and debug it


3

K (ngn/k), 78 76 60 bytes -16 bytes from using @ngn's version {(,()){(,y),+|x}/|,/{1_{1_y,*x}':x,,*x}'0N 4#-1_*'(|+1_)\x}/ Try it online! {...}/ use the "do" variant of / to run the function x times, seeded with y and returning the last result. (each iteration rotates each ring of the input matrix one position) (|+1_)\x peel off the first row, ...


1

05AB1E, 12 bytes Uses 0/1 for F/T. Åγ.¥ü2sÏ€εN- Try it online! Åγ run-length encodes the input, pushing the chunk elements and the chunk lengths. .¥ takes the cumulative sum of the chunck lengths and prepends a 0. ü2 creates pairs of all adjacent numbers in this list. s swaps to the list of chunk elements. Ï selects each pair which is at the same index as a ...


0

05AB1E, 8 bytes ÙΣ¢}R.γ¢ Try it online or verify all test cases. Explanation: Ù # Uniquify the (implicit) input-list Σ # Sort this uniquified list by: ¢ # Get its count in the (implicit) input-list }R # After the ascending sort: reverse it to make it descending .γ # Then adjacently group each value by: ¢ # Its count in ...


1

K (ngn/k), 16 bytes {|/&/~^x?x+/:!4} Try it online! Another port of @ngn's APL answer, using ~^x?y as the membership test.


2

Python 2, 63 bytes lambda x:max(sum(a-m^a-n<0for m,n in zip(x,x[1:]))for a in x)+1 Try it online! I'm back 6 years in the future to golf the Python answers by Sp3000 and Jakube! The improvement is shorten the core expression of (a+.5-m)*(a+.5-n)<0 or min(t)<a<=max(t) with t for m,n to: a-m^a-n<0 This is parsed as (a-m)^(a-n)<0. Due the ...


3

K (ngn/k), 17 bytes {|/+/'~~':'x>/:x} Try it online! Modeled after Zgarb's APL answer. x>/:x build matrix storing whether or not each value of x is larger than each other value of x ~~':' for each "row", determine where the values change (i.e. differ each) +/' take the sum of each row |/ take/return the maximum sum


0

Scala, 128 bytes m=>Stream.iterate(_)(a=>a.tail.lazyZip(a)map(_-_)).takeWhile(Nil!=).:\(Seq.fill(m)(0))((a,b)=>b.scanLeft(a(0))(_+_))takeRight(m) Try it online!


1

Perl 5 -pa, 89 bytes map$k{$_}++,@F;$_="@{[sort{$k{$b}-$k{$a}}keys%k]}";s/\S+ /$&.'| 'x($k{1*$&}!=$k{1*$'})/ge Try it online!


2

Husk, 6 5 bytes ↔k#¹u Try it online! -1 byte from Leo. Explanation ↔k#¹u u uniquify the input k#¹ key on frequency in input ↔ reverse


1

Ruby 2.7, 15 bytes p$*.tally.key 1 Takes in the array of chars as input from the arguments $* passed to it. Ruby 2.7, 18 bytes ->a{a.tally.key 1} The following is the screenshot of SoloLearn code playground, which uses Ruby 2.7,


1

jq, 11 bytes .-[sort[1]] Try it online! Explanation . # The input - # With all occurances of ... removed: [sort # The sorted input [1]] # 's second item


0

SmileBASIC 4, 43 bytes 1 RSORT I:IF I[0]-I[1]GOTO@0 2 ?MIN(I)@0?MAX(I) Receives array input through I[]. The first value printed is the answer, other printed values are to be ignored. Works by reverse-sorting the list of integers (from largest to smallest values) and comparing the zeroth and first elements. If their difference is non-zero, then the odd ...


Top 50 recent answers are included