[Question]
  [
We haven't had any nice, easy challenges in a while, so here we go.
Given a list of integers each greater than \$0\$ and an index as input, output the percentage of the item at the given index of the total sum of the list.
Output should be whatever the natural representation for floats/integers is in your language (unary, decimal, Church numerals etc.). If you choose to round the output in any way, it must have at minimum 2 decimal places (when reasonable. \$1.2\$ doesn't *need* to be rounded, but \$1.20\$ is also perfectly acceptable).
Indexes can be either 1-indexed or 0-indexed, and will always be within the bounds of the array.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes wins!
## Examples
Using 1-indexed and rounded to 2 d.p
```
list, index                    =>         output
[1, 2, 3, 4, 5], 5             => 5 / 15    => 33.33
[7, 3, 19], 1                  => 7 / 29    => 24.14
[1, 1, 1, 1, 1, 1, 1, 1, 1], 6 => 1 / 9     => 11.11
[20, 176, 194, 2017, 3], 1     => 20 / 2410 => 0.83
[712], 1                       => 712 / 712 => 100
```
Or, as three lists:
```
[[1, 2, 3, 4, 5], [7, 3, 19], [1, 1, 1, 1, 1, 1, 1, 1, 1], [20, 176, 194, 2017, 3], [712]]
[5, 1, 6, 1, 1]
[33.33, 24.14, 11.11, 0.83, 100]
```
  
[Answer]
# [Python 3](https://docs.python.org/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!](https://tio.run/##K6gsycjPM/6fZhvzPycxNyklUSFTJ9EqMTozVr@4NFcjUVPL0MDgf0FRZl6JRpqGiU60oY6RjrGOiY5prKbmfwA "Python 3 – Try It Online")**
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 9 [bytes](https://codegolf.meta.stackexchange.com/a/9429/43319 "When can APL characters be counted as 1 byte each?")[SBCS](https://github.com/abrudz/SBCS ".dyalog files using a single byte character set")
Anonymous tacit infix function. Takes index as left argument and list as right argument.
```
100×⌷÷1⊥⊢
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///39DA4PD0Rz3bD283fNS19FHXov9pj9omPOrte9Q31dP/UVfzofXGj9omAnnBQc5AMsTDM/g/kPYK9vfTgNLq0aY6CoY6CmZg0jBWXTPt0Aq4XDRQzEhHwVhHwURHwTRWRyHaHMwztASxDSF6MBFI0sgAyDAHGWsJ1GtkYAjSCTbB0Cg2Vh0A "APL (Dyalog Unicode) – Try It Online")
`100` one hundred
`×` times
`⌷` the indexed element
`÷` divided by
`1⊥` the sum (lit. the base-1 evaluation) of
`⊢` the right argument
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), 22 bytes
```
x=>y=>x[y]*100/x.Sum()
```
[Try it online!](https://tio.run/##bZBLT4NAFIXX5VeMrAY7xRnoQ62QuFA3mhpdmFhZUDqkUyttYFAI4bfjHV6pj4RkuPd@59wzEySjIBHVbRoFV@t9utrxpUfqSkSSNB3XDZ0qc9zccbNl7p0ySs8y8zn9wEY11z79GInokEqGHBTxL9TZLL3iuCwYQRZBNkFjgiYl@cEWs3rCLn73QfT/9xu0KDRnU@UB/hZlyvHPFmaV5VFkq4kMk0ntOm29GyTmSbqTPWLbpg0ZrbHJYANjJgOWmucqN6WggRdDAnBIslXHXAv3MfeDDVZu2CcrA9a2j2W@igNuQhCckdxw3PowDFRog8cYzLBvzLvf1Yipoo0F7g@@3JhP@zRa4xDAGiBWx/DswAPJ1wA2t1iK4dCDoQgRVgYnTs/UCwcvsZAc6zedsKDweHd7iQpW6qSHifJTWwbb4RCOUhvwXcJRK1@kEm50qdR6j9azexHB/C3SoVNqKsYWuYganRIUSPJEBn4C8UJf7PgaLLaAHy@4Hi3e/RxMqm8 "C# (Visual C# Interactive Compiler) – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
ị÷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!](https://tio.run/##y0rNyan8///h7u7D24NrT2w3Ojz9////pv@jDXUUjHQUjHUUTHQUTGMB "Jelly – 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
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 6 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
è²O/т*
```
A full program taking the index then the list. Uses 0-indexing.
**[Try it online!](https://tio.run/##yy9OTMpM/f//8IpDm/z1LzZp/f9vwhVtqGOkY6xjomMaCwA "05AB1E – Try It Online")**
### How?
```
è²O/т*
è      - index (input 1) into (input 2)
 ²     - push 2nd input
  O    - sum
   /   - divide
    т  - push 100
     * - multiply
       - print top of stack
```
[Answer]
# [Java (JDK)](http://jdk.java.net/), 47 bytes
```
a->i->1e2*a[i]/java.util.Arrays.stream(a).sum()
```
[Try it online!](https://tio.run/##bZFRa4MwFIXf@ysuBUFL6tS1K2tXoTAGe9jT9iY@ZDWWdGokuZYV8bd3SXTt6IoGyfluzvXc7OmBTvfZ14mXtZAIe733G@SFP1mN/ml5U22Ri8rAbUGVgjfKK2hHAHXzWfAtKKSoPwfBMyg1c99R8mqXpEDlTnm2FOBl8HniFSYp@RDPQh9nZ/m1QrZjMo4hh/WJTmM@jUMWTWjC07vL72ykpEflK5SMli71fNWUrnda2R7W2jyATKGC9dAboIU2JBARuCcwIzDviNFm0EFHLiULy8NHTVsIrqA@f/u11fOr6ijQZPFg3HS/KAiN903fRRgZPTirXR8mFxLcIZHNs@xTeedQFuoh64noqAYmQbq6QL0y9v2LwvQPfT8qZKUvGvRrfVmYu2NHEXCyJTjKqcYEhkGj6G/TtW080nsSyH1a18VxkPvNRvV36toaz@ubdSOzutMP "Java (JDK) – Try It Online")
[Answer]
# [J](http://jsoftware.com/), 10 bytes
```
100*{%1#.]
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/DQ0MtKpVDZX1Yv9rcilwpSZn5CuYKKQpGCoYKRgDWaYQIQOgkDlQwNASwjcFK0GDCKVGBgqG5mZA1SZApiFQI5Iphkb/AQ "J – Try It Online")
0-indexed
[Answer]
# [R](https://www.r-project.org/) 28 bytes
```
function(n,l)100*l[n]/sum(l)
```
[Try it online!](https://tio.run/##K/qfVpqnYKMLopJLMvPzNPJ0cjQNDQy0cqLzYvWLS3M1cjT/5wBVKBhamXKBlCqYcgEVA9UpAGW4AA)
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 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!](https://tio.run/##VZDRboMgFIbvfYoTF1sEumm3rNkY3YM0vaAoLYnFRtGLNT47A6wXvYAc/gPf/x/k5iylc6pphQWFRmqozbWxeGR3w8eDOTLVdsjygnnJEo5HQnLW1XboDBhc1ts3yyb3oo1shqqGn95Wun297JMnqdGnoCUeDVehDQqF6M6SgryIDjD2hzGHewIQWngUTU8BawpmuLIkyD6eF7kUTdNKND/u9V/dqkDL8/k1QAis@Qy4DTbw0fqwzlk03GxgtQKENRe21QgTEp1999Z5jEJp1mdVGrwJ4TwG@U3Tb69E0d@MLj5OwC2mEHI@I9mjsXCPFLIK@B4ylcaxKMwjxcPCnZKw/Pb444Ilk3PbwpW7T1d@ffiy3Ll3V7jiHw "C (gcc) – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), ~~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!](https://tio.run/##y0gszk7Nyfn/P9M@0TZRUTFTv7g0VyFRy9DA4H9uYmaegq1CSj6XgoJCQVFmXomCioKJgr1CtKGOgpGOgrGOgomOgmnsfwA "Haskell – Try It Online")**
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 15 bytes
```
100##[[]]/Tr@#&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b739DAQFk5Ojo2Vj@kyEFZ7X9AUWZeSbSyrl2ag4NyrJq@QzVXdbWhjpGOsY6JjmktCAEFzIFcQ8taHUMwz1AHDcIkjAx0DM3NgCpNdIwMDIGaYBLmhkYgJlftfwA "Wolfram Language (Mathematica) – Try It Online")
Input as `list, index`
[Answer]
# 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!](https://tio.run/##dc/fCoIwFMfxe5/iXGrZPGebmRf6IiI4zEKRLTJ8fdvsD6U1ZLALP3x/nRrVUF/by22nzbGZTtmkslxnuSp0uSHEqBlV7yvWmVZX2yqYaqMH0zesN2f/5BcUAg9BhCBDiMvAl0EAXyeKIIYIKHaPLAchmBDeQklmglIL4BJ4KYlVePpUuGQkvXXL78@ysWOtQlaZEacQMaKlwtH@kexdjt3EkVzcZ5hVOLoYSegUZIf1IOJ/t7wHEYfH7VIQpzs "JavaScript (Node.js) – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), 9 bytes
```
)1Gs/100*
```
[Try it online!](https://tio.run/##y00syfn/X9PQvVjf0MBA6///aEMdIx0zIx1jnVguYwA "MATL – 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 online!](https://tio.run/##y00syfn/X9PQvVj///9oQx0jHTMjHWOdWC5jAA "MATL – Try It Online")
[Answer]
# [PHP](https://php.net/) (7.4), 35 bytes
```
fn($l,$i)=>100/array_sum($l)*$l[$i]
```
[Try it online!](https://tio.run/##K8go@G9jX5BRwMWlkmb7Py1PQyVHRyVT09bO0MBAP7GoKLEyvrg0FyiqqaWSE62SGfvfmosrNTkjX0ElTSPaUEfBSEfBWEfBREfBNBZIaVpz/QcA "PHP – Try It Online")
Input index is 0-based.
[Answer]
# [K (oK)](https://github.com/JohnEarnest/ok), ~~15~~ 14 bytes
-1 byte thanks to ngn!
```
{100*x[y]%+/x}
```
[Try it online!](https://tio.run/##y9bNz/7/P82q2tDAQKsiujJWVVu/ovZ/mkK0kYGCobmZgqGliYKRgaG5grG1gkHsfwA "K (oK) – Try It Online")
0-indexed
[Answer]
# [TI-Basic](http://en.wikipedia.org/TI-Basic), [12 bytes (12 tokens)](https://codegolf.meta.stackexchange.com/questions/1541/how-to-score-ti-basic)
```
Prompt X
Ans(X)E2/sum(Ans
```
1-indexed
Takes the list in [`Ans`](https://codegolf.meta.stackexchange.com/a/8580/44694) and prompts the user for the index
[Example run](https://i.stack.imgur.com/ZP69c.jpg)
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
                 # The result of the last expression in a program is implicitly returned
```
[Answer]
# [Red](http://www.red-lang.org), ~~31~~ 29 bytes
-2 bytes thanks to ErikF
```
func[b i][1e2 * b/:i / sum b]
```
[Try it online!](https://tio.run/##XcsxDsIwEETR3qf4NU2ySxKLHIPW2sbEllwQoUDO77hCAU01o3lbWuo9LcFcnql5Xx8hUixIUi7Ebi50vPcn0eprK@uHTBCUKwOjMYL77r6tcjPEna5/MaaT0B7xU0MD2kvzP9iLtl4P "Red – Try It Online")
[Answer]
# [Factor](https://factorcode.org/), 46 bytes
```
: f ( i l -- n ) dup [ nth ] dip sum / 1e2 * ;
```
[Try it online!](https://tio.run/##XYwxDoJAFAV7TjGlmqAsgkQ4gKGhMVbGgsCiRFgQlsIYz46rFBrzm/cmb36RZrrpxsM@TnYhV9kpWVGn@kIvb4NUmexpO6n1ve1KpYmsOAnJmlyem6oYQwpmlFTYNoo5@dByRBn/RF629EPNCiFdFkSjxwOByxoPn6eRl5ZjWGCI2E4Ay/@s/u@7dh1EsDGCZ6J4uz@fhDu18QU "Factor – Try It Online")
0-indexed
[Answer]
# [Zsh](https://www.zsh.org/), ~~32~~ 30 bytes
***-1** thanks to @ErikF, using `1e2` instead of `100.`.*
```
<<<$[1e2*$@[`<&0`]/(${@/#/+})]
```
~~[Try it online!](https://tio.run/##qyrO@J@moVn938bGRiXa0MBAT0vFITrBRs0gIVZfQ6XaQV9ZX7tWM/Z/LVeagqGCkYKxgomCqQJQtYIpUMQcyDe0VAACkIjhfwA "Zsh – Try It Online")~~
[Try it online!](https://tio.run/##qyrO@J@moVn938bGRiXaMNVIS8UhOsFGzSAhVl9DpdpBX1lfu1Yz9n8tV5qCoYKRgrGCiYKpAlCxgilQxBzIN7RUAAKQiOF/AA "Zsh – Try It Online")
Accepts the list as arguments and the index on stdin.
```
<<<$[1e2*$@[`<&0`]/(${@/#/+})]  # $[ arithmetic ]
   $[       `<&0`            ]  # capture stdin
   $[    $@[     ]           ]  # index into the arguments
   $[1e2*$@[`<&0`]           ]  # multiply by 100.0 (casts to float)
   $[              (${@/#/+})]  # prepend each element with + and add
   $[             /          ]  # divide
<<<                              # print to stdout
```
[Alternate **30 byte** solution](https://tio.run/##qyrO@J@moVn9v9hWQ1slzkGTy8bGRiXaMNVIS8UhOsFGzSAhVr849n8tV5qCoYKRgrGCiYKpAlCNgilQxBzIN7RUAAKQiOF/AA "Zsh – Try It Online")
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 34 bytes
```
param($l,$n)$l|%{$i+=$_};$l[$n]/$i
```
[Try it online!](https://tio.run/##bY7dCoJAEEbvfYoBp3Bp0Z3dNZOwF4kIL5SEzcQuujCf3XbzhwKX2YsP5sz5mseraJ@3wpgBy6wbmrzN7wEajjVD8950WO0yvPZHNGesLxFWQ@95WywhIA6Sg@KgOcQMNPjw87ITxBABxVNQKlRqBJMvRSkD8c/MYGJBmU5B6pD0YlwfZl2@3SULpvMVopBoBKWwS8neSW1ZKchVWPROIpxSk3BBhIe5KUkGqy3npiQj@yfh8AE "PowerShell – Try It Online")
Shame parameters are so dang expensive in Powershell.
[Answer]
# Scratch 3.0 ~~24~~ 23 blocks/~~239~~ 228 bytes
[![-11 bytes thanks to @JoKing](https://i.stack.imgur.com/fBloX.png)](https://i.stack.imgur.com/fBloX.png)
*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](https://scratch.mit.edu/projects/333497999/)
## Answer History
[![Oh Scratchblocks, why so long?](https://i.stack.imgur.com/kWVww.png)](https://i.stack.imgur.com/kWVww.png)
*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
delete(n)of[m v
repeat(length of(m
change[s v]by(item(1)of[m v
delete (1)of[m v
end
say(((n)/(s))*(100
```
[Try it on scratch](https://scratch.mit.edu/projects/333497999/)
Input is in the form of:
```
item1
item2
...
itemN
index
```
I really should stop doing this to myself. But it *is* very fun!
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 13 bytes
```
c*100@hQeQshQ
```
[Try it online!](https://tio.run/##K6gsyfj/P1nL0MDAISMwNbA4I/D//2hDHQUjHQVjHQUTHQXTWCALAA "Pyth – Try It Online")
First time using Pyth so theres probably some pretty big optimizations here, but I dont know where they are...
0-index, takes input as `list, index`
[Answer]
# [Go](https://golang.org/), 79 bytes
```
func f(a[]float64,i int)float64{s:=0.
for _,v:=range a{s+=v}
return a[i]/s*100}
```
This has a nice amount of accuracy. It takes a Go slice and uses `range` to calculate the sum.
[Try it online!](https://tio.run/##PY5NCoMwFIT3OcXDlWmDjbYoteQO3YuUhxgJ1URidBNydpv@0FkMDAzfzGD2GbsnDj1MqDQhapqNdZDIySW7XHUHMsWmlaNBV16YAqUd/SW/1IJnRBoLD7bVwqKOHPTLUWyB2N6tVgM2qj0th5zz8OW9d1IKnkAUQi3gj/cFZ5BXZbTrhUHB84rBOXya8VB2t3F91Gm8xIBTeiNhfwE "Go – Try It Online")
[Answer]
# [MathGolf](https://github.com/maxbergmark/mathgolf/blob/master/math_golf.txt), ~~7~~ 6 [bytes](https://github.com/maxbergmark/mathgolf/blob/master/code_page.py)
```
§\Σ/♀*
```
0-based indexing.
[Try it online.](https://tio.run/##y00syUjPz0n7///Q8phzi/UfzWzQ@v8/2lDPQMcIiI2B2ASITfUMYnVMuKLNoWKGliABAy6wQgI4VseUK9rIAMQxNwPrBZloZGAINQxsjrmhEZgFAA)
**Explanation:**
```
§       # Index the (implicit) second input-integer into the first (implicit) input-list
 \      # Swap so the first (implicit) input-list is at the top of the stack
  Σ     # Take the sum of that input-list
   /    # Divide the earlier indexed number by this sum
    ♀*  # Multiply it by 100
        # (after which the entire stack joined together is output implicitly as result)
```
[Answer]
# [Snap!](https://snap.berkeley.edu), 79 bytes (scratchblocks syntax)
```
((L)(i))::hat
report((combine(L)using((()+()@addInput)@addInput::grey ring))/(i
```
This is a "custom block" (function), and it takes a "list" (array) as the first argument and the idnex as the second argument.
Wow, having to manually mark hats/grey rings/grey ring inputs really makes short Snap! code in scratchblocks look relatively ugly.
[Answer]
# Erlang 41 bytes
```
f(X,N)->lists:nth(N,X)/lists:sum(X)*100.
```
[Answer]
# [CJam](https://sourceforge.net/p/cjam), ~~14~~ 12 bytes
*-2 bytes thanks to @luis-mendo*
```
_@=\:+d/1e2*
```
[Try it online!](https://tio.run/##S85KzP1voBBtrmCsYGgZ@z/ewTbGSjtF3zDVSOv/fwA "CJam – Try It Online")
0-indexed. Takes input as `index [list]`
# Explanation
```
code         | explanation
-------------|------------
_@=\:+d/1e2* | whole program
 @=          | get the item at the index
_  \:+       | sum the array
      d      | convert to a *d*ouble
       /     | divide
        1e2* | multiply by 100 
```
[Answer]
# [Perl 5](https://www.perl.org/) `-ap -MList::Util=Sum`, 19 bytes
```
$_=100*$F[<>]/sum@F
```
[Try it online!](https://tio.run/##K0gtyjH9/18l3tbQwEBLxS3axi5Wv7g018Ht/39DBSMFYy7Df/kFJZn5ecX/dRML/uv6@mQWl1hZhZZk5tgC1QEA "Perl 5 – Try It Online")
Take the list, space separated on the first line, the index (0-based) on the second.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 8 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
gV
*L/Vx
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=Z1YKKkwvVng&input=NApbMSwgMiwgMywgNCwgNV0)
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 102 bytes
```
\d+
$*
^(1)+((?<-1>.(1+))+)
$3$2
,
\G1
10000$*
;(1+)\1
$1;$1$1
r`.*(\2)*;(1+)
$#1
+`^..?$
0$&
..$
.$&
```
[Try it online!](https://tio.run/##XYs9DsIwFIN3X4NHlTSPKE7/hILo2EtEqEgwsDBU3D@kjNiL5c/enp/X@16OZllLfjhIi5uhdcbMlxOv3tBZ6yykkwgF8kIwVNVh2mEmhEkoxLb61uRo2x@AHAi33ryfBUEaeC/w0pQyJGrUTnsdwDTVxDPGWv65whiU01h5rzGwTvcD4xc "Retina 0.8.2 – 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`.*(\2)*;(1+)
$#1
```
Multiply the desired value by 10000 and divide by the sum with rounding by adding on half of the sum first.
```
+`^..?$
0$&
```
Ensure that the result has at least three digits.
```
..$
.$&
```
Insert a decimal point at the second last position.
]