The Stack Overflow podcast is back! Listen to an interview with our new CEO.

New answers tagged

0

Forth (gforth), 61 bytes : f 8 * over + f@ 0e swap 0 do dup f@ f+ 8 + loop f/ 1e2 f* ; Try it online! It's way too painful to work with arrays in Forth... A function that takes three items (array length, array start address, index; rightmost being the top) from the main stack, and gives the answer on the FP stack. The array contains floating-point values....


0

Canvas, 8 bytes @;∑÷AA×× Try it here! Uses 1-indexing. The last half just multiplies by 100, so 4 bytes can be saved if percentage over interval [0,1] is allowed as opposed to percentage over interval [0,100]. Explanation of example run: list = [1, 2, 3, 4, 5], index = 5. Stack Visualization | Executed Instruction | Explanation ---------...


1

Forth (gforth), 37 bytes : f begin dup while tuck mod repeat ; Try it online! Input is two single-cell integers, output is one double-cell integer. How it works A "cell" means a space for one item on the stack. A double-cell integer in Forth takes two cells, where the most significant cell is on the top. For this challenge, the GCD of two single-cell ...


0

Forth (gforth), 52 bytes : f begin 2dup max -rot min tuck mod ?dup 0= until ; Try it online! Uses Euclidean Algorithm [repeatedly call larger % smaller until result is 0] Code Explanation : f \ start a new word definition begin \ start and indefinite loop 2dup \ duplicate arguments max -rot min \ reorder ...


2

Add++, 26 bytes D,g,@@#~,%A$p%+ L,MRBCþgbM Try it online! Generates the range \$1, 2, 3, ..., \max(a, b)\$, then filters out elements that don't divide either \$a\$ or \$b\$, Finally, we return the maximum value of the filtered elements.


1

k4, 14 bytes {%+/x%100*x y} can save 1 byte from @Galen Ivanov's solution (in oK) as % is inverse operation in k4 as opposed to square root


1

Toi, 6 bytes (r[rr] Try it online! Toi is a language that only knows sets. The memory is a set S that starts as the empty set. The input number can be given as a von Neumann ordinal, constructed using "ua"*n, or alternatively the input number can be represented by the nesting level using "u"*n. u nests the context S, and a changes S to \$S \cup \{t \mid t ...


2

Scratch 3.0 24 23 blocks/239 228 bytes Alternatively in SB syntax when gf clicked set[s v]to(0 ask()and wait repeat until<(answer)=( add(answer)to[m v ask()and wait end set[n v]to(item(length of(n))of(m repeat(length of((m)-(1 change[s v]by(item(1)of[m v delete (1)of[m v end say(((n)/(s))*(100 Saved 11 bytes thanks to @JoKing Try it on scratch Answer ...


1

Stax, 7 bytes â└╡Æå'W Run and debug it at staxlang.xyz! 0-indexed. Requires at least one item in the input list to be of the form N.0 rather than simply N. Replacing / in the unpacked version with :_ gets around this restriction at the cost of one byte. Unpacked (8 bytes) and explanation: @AJ*x|+/ Index atop the stack, with the list beneath @ ...


1

Go, 79 bytes func f(a[]float64,i int)float64{s:=0.;for _,v:=range a{s+=v};return a[i]/s*100} Try it online! This has a nice amount of accuracy. It takes a Go slice and uses range to calculate the sum.


1

Julia 1.0, 21 bytes f(l,i)=100l[i]/sum(l) Try it online!


1

T-SQL 2008, 41 bytes To find the row number equal to the index: A division of index and row number multiplied with division of row number and index, if both results are 1, index and row are the same because both are above 0 and integer division round down. This saves 1 byte compared to using IIF a - value b - row number Using table variable as ...


2

PowerShell, 34 bytes param($l,$n)$l|%{$i+=$_};$l[$n]/$i Try it online! Shame parameters are so dang expensive in Powershell.


1

Swift, 60 bytes {(n:[Int],i)->Float in 1e2*Float(n[i])/Float(n.reduce(0,+))} Try it online! I picked Swift to participate and bumped into its type sensitivity !


1

Batch, 111 bytes @shift @set s=%* @call set/as=%s: =+%-%0,s=(%%%0*10000+s/2)/s,h=s%%%%10,t=s/10%%%%10,s/=100 @echo %s%.%t%%h% Takes input as index and list as command-line arguments. Note: Only works for index values from 1 to 9 due to limitations of Batch; a 0-indexed version could be written which would be able to index the first 10 elements. Explanation:...


4

Java (JDK), 47 bytes a->i->1e2*a[i]/java.util.Arrays.stream(a).sum() Try it online!


1

Factor, 46 bytes : f ( i l -- n ) dup [ nth ] dip sum / 1e2 * ; Try it online! 0-indexed


1

Icon, 53 bytes procedure f(L,i) s:=0;s+:=!L&\z return 1e2*L[i]/s end Try it online! The only interesting thing here is finding the sum. Icon was one of the first languages to have generators. ! generates all the values of the list L that are accumulated to s. Normally we need to write every s+:=!L, but I used backtracking with &\z, which checks if ...


1

MathGolf, 7 6 bytes §\Σ/♀* 0-based indexing. Try it online. Explanation: § # Index the (implicit) second input-integer into the first (implicit) input-list, # which apparently doesn't pop the list \ # Swap so this list is at the top of the stack now Σ # Take the sum of that list / # Divide the earlier number we indexed by ...


1

Octave, 21 bytes @(a,n)a(n)/sum(a)*100 Try it online!


4

C (gcc), 64 bytes 0-indexed. The only fun bit was the realization that 1e2 is a double, saving a byte over 100.! float f(v,n,t)int*v;{n=v[n];for(t=0;*v;t+=*v++);return n*1e2/t;} Try it online!


1

Perl 6, 21 bytes {100*@^a[$^b]/@a.sum} Try it online! The simple solution, since I can't use curried parameters with the $b parameter being indexed. A funner solution that doesn't have to handle two parameters by using the rotate function instead: {100*.[0]/.sum}o&rotate Try it online! But it is unfortunately two bytes longer


1

Racket, 81 43 bytes Accepts a list and index. Outputs rationals (default for division in Racket). Big thanks to Galen Ivanov for finding the apply function. Still learning as I go :) . (λ(l i)(*(/(list-ref l i)(apply + l))100)) Try it online!


1

Zsh, 32 30 bytes -1 thanks to @ErikF, using 1e2 instead of 100.. <<<$[1e2*$@[`<&0`]/(${@/#/+})] Try it online! Try it online! Accepts the list as arguments and the index on stdin. <<<$[1e2*$@[`<&0`]/(${@/#/+})] # $[ arithmetic ] $[ `<&0` ] # capture stdin $[ $@[ ] ] # ...


2

K (oK), 15 14 bytes -1 byte thanks to ngn! {100*x[y]%+/x} Try it online! 0-indexed


1

AWK, 43 40 bytes {s+=a[NR]=$1}END{print 100*a[$1]/(s-$1)} Try it online! -3 bytes thanks to CowsQuack Takes the index as the last line of input


1

Ruby, 25 23 21 bytes ->a,i{1e2*a[i]/a.sum} Try it online!


1

Bash, 77 75 73 72 80 76 75 bytes a=($@) ((b=10000*${a[$1]}/(${@/%/+}-$1),b<10)) echo $[b/100].${?/1}$[b%100] Try it online! +8 bytes thanks to GammaFunction, who found a bug with large numbers -5 bytes thanks to GammaFunction, who golfed his original bug fix


3

MATL, 9 bytes )1Gs/100* Try it online! Explanation implicitly take two inputs ) get the entry within the first input at the index specified by the second 1G push the first input onto the stack again s compute the sum / divide first entry of the stack by this number (the sum) 100* multiply by 100 Try it ...


4

R 28 bytes function(n,l)100*l[n]/sum(l) Try it online!


3

Wolfram Language (Mathematica), 15 bytes 100##[[]]/Tr@#& Try it online! Input as list, index


1

Retina 0.8.2, 102 bytes \d+ $* ^(1)+((?<-1>.(1+))+) $3$2 , \G1 10000$* ;(1+)\1 $1;$1$1 r`.*(\2)*;(1+) $#1 +`^..?$ 0$& ..$ .$& Try it online! Link includes test cases. Takes input as index;list,.... Explanation: \d+ $* Convert to unary. ^(1)+((?<-1>.(1+))+) $3$2 Index into the list. , Sum the list. \G1 10000$* ;(1+)\1 $1;$1$1 r`.*...


3

J, 10 bytes 100*{%1#.] Try it online! 0-indexed


2

Red, 31 29 bytes -2 bytes thanks to ErikF func[b i][1e2 * b/:i / sum b] Try it online!


1

TI-Basic, 12 bytes (12 tokens) Prompt X Ans(X)E2/sum(Ans 1-indexed Takes the list in Ans and prompts the user for the index Example run Explanation: Prompt X # Prompt the user for the index Ans(X)E2/sum(Ans Ans(X) # The value at the Xth index in the list E2 # times 100 /sum(Ans # Divided by the sum of the list ...


3

PHP (7.4), 35 bytes fn($l,$i)=>100/array_sum($l)*$l[$i] Try it online! Input index is 0-based.


3

Haskell,  20  18 bytes i?a=a!!i/sum a*100 A dyadic operator (?) taking the (0-indexed) index on the left and a list on the right which yields the percentage. Try it online!


6

Python 3, 26 bytes lambda i,a:a[i]/sum(a)*100 An unnamed function accepting an integer (0-indexed index) and a list which returns the percentage. Try it online!


6

C# (Visual C# Interactive Compiler), 22 bytes x=>y=>x[y]*100/x.Sum() Try it online!


5

05AB1E, 6 bytes è²O/т* A full program taking the index then the list. Uses 0-indexing. Try it online! How? è²O/т* è - index (input 1) into (input 2) ² - push 2nd input O - sum / - divide т - push 100 * - multiply - print top of stack


1

Japt, 8 bytes gV *L/Vx Try it


6

APL (Dyalog Unicode), 9 bytesSBCS Anonymous tacit infix function. Takes index as left argument and list as right argument. 100×⌷÷1⊥⊢ Try it online! 100 one hundred × times ⌷ the indexed element ÷ divided by 1⊥ the sum (lit. the base-1 evaluation) of ⊢ the right argument


1

Perl 5 -ap -MList::Util=Sum, 19 bytes $_=100*$F[<>]/sum@F Try it online! Take the list, space separated on the first line, the index (0-based) on the second.


5

Jelly, 7 bytes ị÷S}ȷ2× A dyadic Link accepting an integer, one-based index on the left and a list of numbers on the right which yields the percentage. Try it online! How? ị÷S}ȷ2× - Link: integer, i; list, L ị - (i) index into (L) } - use right argument: S - sum (L) ÷ - divide ȷ2 - literal 10^2 = 100 × - multiply


3

JavaScript (ES6), 30 bytes Takes input as (array)(index), where index is 0-based. a=>n=>a[n]*100/eval(a.join`+`) Try it online!


0

1+, 4 bytes ..+: Pretty much trivial. I just want to get 1+ into those catalogue.


1

Keg, 3 bytes ¿¿/ Simply 2 nice inputs and then divides them. TIO


4

Mornington Crescent, 1827 1698 chars I felt like learning a new language today, and this is what I landed on... (Why do I do this to myself?) This entry won't be winning any prizes, but it beats all 0 other answers so far using the same language! Take Northern Line to Bank Take Central Line to Holborn Take Piccadilly Line to Heathrow Terminals 1, 2, 3 Take ...


0

Ruby, 6 bytes n.odd? Try it online! I don't know if this is cheating. There's a method definition in the header. Is there a way to skip doing this and pass in a number differently, perhaps? odd? is a default operation in Ruby.


6

2 weights I was inspired by the other answers to approximate the polarization identity in a different way. For every small \$\epsilon>0\$, it holds that $$ xy \approx \frac{e^{\epsilon x+\epsilon y}+e^{-\epsilon x-\epsilon y}-e^{\epsilon x-\epsilon y}-e^{-\epsilon x+\epsilon y}}{4\epsilon^2}.$$ It suffices to take \$\epsilon=0.01\$ for this challenge. ...


Top 50 recent answers are included