# Find the percentage

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 , 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
, 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], ]
[5, 1, 6, 1, 1]
[33.33, 24.14, 11.11, 0.83, 100]

• Sandbox post (now deleted) – caird coinheringaahing Sep 29 '19 at 0:04
• how exactly can non-integers be output as unary / church numerals? – Doorknob Sep 29 '19 at 3:25
• @Doorknob Maybe a unary number, the dot, and another unary number? – HighlyRadioactive Sep 29 '19 at 5:32
• Since the output can be rounded to two decimal places, it might also be permissible to output rounded times 100? – Unrelated String Sep 29 '19 at 6:10
• test case 4 should be 20/2410 – attinat Sep 29 '19 at 8:31

# 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 !

# Zsh, 32 30 bytes

-1 thanks to @ErikF, using 1e2 instead of 100..

<<<$[1e2*$@[<&0]/(${@/#/+})]  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 • (-1 bytes) 1e2 instead of 100. works just like in C to cast to float: Try it online! – ErikF Sep 30 '19 at 4:40 # 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! • To my understand, #lang racket is not counted into bytes if submission is a function. – tsh Sep 30 '19 at 6:33 • I think tsh is right. You can make it work in TIO by assigning the lambda to a variable. Since this is code-golf, you need to remove all the whitespaces like this for 60 bytes – Galen Ivanov Sep 30 '19 at 6:39 • (foldl (λ (a b) (+ a b)) 0 l) -> (apply + l) for 44 bytes – Galen Ivanov Sep 30 '19 at 6:44 • @GalenIvanov The last ) is matched to (define and is not a part of function. So it should not be included too. – tsh Sep 30 '19 at 6:48 • @tsh Yes, of course! I forgot to move it to the footer. So it is 43 bytes – Galen Ivanov Sep 30 '19 at 6:54 # 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

• I saw you mention in chat for more test cases, here's a bash wrapper. – GammaFunction Sep 29 '19 at 20:32
• @GammaFunction Thanks! I had no idea you could write to the file system in TIO. – Jonah Sep 29 '19 at 21:04
• @GammaFunction What's the reason we need the space and single quotes around EOF there to make it work? – Jonah Sep 29 '19 at 21:12
• Single quotes prevent $expansions from happening in the heredoc. The space does nothing either way – GammaFunction Sep 29 '19 at 21:16 • {s+=a[NR]=$1} shaves 3 bytes – Kritixi Lithos Sep 30 '19 at 6:41

# 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 input. 1-indexed

SELECT max(b/@*(@/b)*a*100)/sum(a)FROM @x


Try it online

# Julia 1.0, 21 bytes

f(l,i)=100l[i]/sum(l)


Try it online!

# Bash, 777573728076 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

• Ah, nice catch. Ty! – Jonah Sep 29 '19 at 20:56
• Whoops, saved one more. I don't think bash is going to let me be any more clever than this, though – GammaFunction Sep 29 '19 at 21:28

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

# 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
@           Element at index...
AJ*        Times 100
x|+     Sum of list
/    Divide


# 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

# Clojure, 40 bytes

(fn[a b](* 1e2(/(nth a b)(apply + a))))


Try it online!

It's nice to know about the 1e2 trick. :-)

# Canvas, 8 bytes

＠；∑÷ＡＡ××


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,2,3,4,5], 5, [1,2,3,4,5]    | (program start)      | Canvas starts with the stack populated with the inputs repeating infinitely
..., [1,2,3,4,5], 5                 | ＠, get item         | ＠NA gets the Nth item of A
..., 5, [1,2,3,4,5]                 | ；, swap two ToS     | Swaps the position of the two top items in the stack
..., 5, 15                          | ∑, sum               | Sums all items in an array
..., 0.33333333333333333333         | ÷, divide            | Divides the two top items in the stack
..., 0.33333333333333333333, 10     | Ａ, push 10          | Pushes 10 to the stack
..., 0.33333333333333333333, 10, 10 | Ａ, push 10          | "                    "
..., 0.33333333333333333333, 100    | ×, multiply          | Multiplies the two top items in the stack
..., 33.333333333333333333          | ×, multiply          | "                                       "
...                                 | (end of program)     | Pop and print ToS


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

### How it works

: f ( len addr idx -- F: ans )