查找百分比


15

一段时间以来,我们没有遇到任何轻松,轻松的挑战,所以我们开始吧。

给定一个整数列表,每个整数均大于并有一个索引作为输入,请输出该项目在列表总和给定索引处的百分比。0

输出应该是您的语言(浮点数/整数)的自然表示形式(一元,十进制,教堂数字等)。如果选择轮以任何方式输出,它必须有最低2位小数(合理时,并不需要是圆形的,但也完全可以接受的)。1.21.201.20

索引可以是1索引或0索引,并且将始终在数组的范围内。

这是,因此以字节为单位的最短代码胜出!

例子

使用1索引并四舍五入为2 dp

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

或者,作为三个列表:

[[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]


3
非整数如何精确地输出为一元/教堂数字?
门把手

1
@Doorknob也许是一元数,点和另一个一元数?
高度放射性

由于输出可以四舍五入到小数点后两位,因此也可以输出四舍五入到100?
不相关的字符串

1
测试案例4应该为20/2410
attinat

Answers:





5

果冻,7 个字节

ị÷S}ȷ2×

双向链接在左侧接受一个整数,从一开始的索引,在右侧接受一个数字列表,以产生百分比。

在线尝试!

怎么样?

ị÷S}ȷ2× - Link: integer, i; list, L
ị       - (i) index into (L)
   }    - use right argument:
  S     -   sum (L)
 ÷      - divide
    ȷ2  - literal 10^2 = 100
      × - multiply

1
真好!那是我拥有的一个字节一个字节:P
caird coinheringaahing

5

05AB1E,6 个字节

è²O/т*

一个完整的程序,使用索引然后是列表。使用0索引。

在线尝试!

怎么样?

è²O/т*
è      - index (input 1) into (input 2)
 ²     - push 2nd input
  O    - sum
   /   - divide
    т  - push 100
     * - multiply
       - print top of stack

4

R 28字节

function(n,l)100*l[n]/sum(l)

在线尝试!


我不知道R,但这看起来不起作用(不确定如何在TIO上使用任意数组对其进行测试),因为您应该检索lat索引的元素n,而不仅仅是除以n(请参见[7, 3, 19], 1测试用例) )
caird coinheringaahing

@cairdcoinheringaahing我不好,有错别字(忘了l[]周围的地方n
niko

TIO链接页面上有东西可以为您设置格式。
SS安妮


4

Java(JDK),47个字节

a->i->1e2*a[i]/java.util.Arrays.stream(a).sum()

在线尝试!


你为什么写1e2而不是100?是否因为100是整数并且1e2被视为浮点数?
伊斯梅尔·米格尔

1
@IsmaelMiguel是:1e2携带双精度类型,而a[i]和没有。由于挑战要求返回浮点数,因此可以在其中使用它。
奥利维尔·格雷戈尔(OlivierGrégoire),

将其更改为BiFunction <int [],Integer,Double>,您可以使用以下命令保存10个字节(a,i)->1e2*a[i]/IntStream.of(a).sum()。编辑:> :(我可怜的lambda箭头
Avi的

@Avi仍然需要导入,因此我必须写:(a,i)->1e2*a[i]/java.util.stream.IntStream.of(a).sum(),它是54个字节长。我当前的答案只有47个字节长。此外,a->i->比短一个字节(a,i)->
奥利维尔·格雷戈尔

1
@Avi 是的,它们是必填项,通常用完整的类名而不是导入来编写,所以这就是我在这里所做的
OlivierGrégoire19年




3

JavaScript(ES6),30个字节

将输入作为(array)(index),其中索引从0开始。

a=>n=>a[n]*100/eval(a.join`+`)

在线尝试!


3

MATL,9字节

)1Gs/100*

在线尝试!

说明

          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

在线尝试!






2

临时3.0 24 23块/ 239228字节

-11 bytes thanks to @JoKing

或者以SB语法

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

@JoKing节省了11个字节

从头开始尝试

回答历史

Oh Scratchblocks, why so long?

或者以SB语法

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

从头开始尝试

输入形式为:

item1
item2
...
itemN
index

我真的应该停止对自己这样做。但它非常有趣!


你能不能改repeat length of mlength of m-1并保存自己的delete n
乔·金

不,因为如果我这样做了,它就不会合计最后一个项目。
Lyxal

好吧,如果您删除delete n of m我建议的内容,那么它将
Jo King

2

Pyth,13个字节

c*100@hQeQshQ

在线尝试!

第一次使用Pyth,所以这里可能会有一些相当大的优化,但是我不知道它们在哪里...

0索引,将输入作为 list, index




1

TI-Basic12个字节(12个令牌)

Prompt X
Ans(X)E2/sum(Ans

1索引

进入列表Ans并提示用户输入索引

运行示例

说明:

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

1

视网膜0.8.2,102字节

\d+
$*
^(1)+((?<-1>.(1+))+)
$3$2
,

\G1
10000$*
;(1+)\1
$1;$1$1
r`.*(\2)*;(1+)
$#1
+`^..?$
0$&
..$
.$&

在线尝试!链接包括测试用例。将输入作为index;list,...。说明:

\d+
$*

转换为一元。

^(1)+((?<-1>.(1+))+)
$3$2

索引到列表中。

,

汇总列表。

\G1
10000$*
;(1+)\1
$1;$1$1
r`.*(\2)*;(1+)
$#1

将期望值乘以10000,然后将其除以四舍五入,方法是先将总和的一半相加。

+`^..?$
0$&

确保结果至少包含三位数。

..$
.$&

在倒数第二个位置插入小数点。



1

Perl 6,21个字节

{100*@^a[$^b]/@a.sum}

在线尝试!

简单的解决方案,因为我无法将咖喱参数与$b要建立索引的参数一起使用。一个有趣的解决方案,不必通过使用rotate函数来处理两个参数:

{100*.[0]/.sum}o&rotate

在线尝试!

不幸的是它长了两个字节



1

MathGolf7 6 字节

§\Σ/♀*

基于0的索引。

在线尝试。

说明:

§       # 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 this sum
    ♀*  # Multiply it by 100
        # (after which the entire stack joined together is output implicitly as result)

1

图标,53字节

procedure f(L,i)
s:=0;s+:=!L&\z
return 1e2*L[i]/s
end

在线尝试!

唯一有趣的事情是求和。Icon是最早具有生成器的语言之一。!生成L累加到的列表的所有值s。通常,我们需要编写every s+:=!L,但是我使用了回溯&\z,它检查不存在的z变量是否为non-null,不是,然后从列表中提取下一个值,直到耗尽为止。



1

批处理,111字节

@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%

将输入作为索引,将列表作为命令行参数。注意:由于Batch的限制,仅适用于从1到的索引值9;可以编写一个0索引的版本,该版本将能够索引前10个元素。说明:

@shift

将索引移至%0,列表移至%1... %9(或更少)。但是请注意,批次shift不会影响%*

@set s=%*

获取所有参数,以空格分隔。

@call set/as=%s: =+%-%0,s=(%%%0*10000+s/2)/s,h=s%%%%10,t=s/10%%%%10,s/=100

将空格更改为+s并进行算术运算,从而求和,但减去索引。然后索引到列表中,乘以10000,将总和的一半除以总和。最后将divmod乘以10两次以生成小数位。(%算术运算符在批处理中具有特殊含义,通常需要加倍,但call随后需要再加倍。)

@echo %s%.%t%%h%

输出结果和小数位。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.