旋转平均


18

给定输入整数n >= 10,输出该整数的所有重复数据消除旋转的平均值。

例如,对于input 123,旋转为123(不旋转),231(一个旋转)和312(两个旋转)。这些平均值为(123 + 231 + 312) / 3222

再举一个例子4928。该旋转是492892842849,和8492。取这四个数字的平均值等于6388.25

对于另一实例,对于输入445445,去重复旋转是445445454454,和544544,所以输出481481

对于输入777,只有一个重复数据消除的旋转,因此输出为777

规则

  • 如果适用,您可以假定输入/输出将适合您语言的本机Integer类型。
  • 输入和输出可以通过任何方便的方法给出。
  • 完整的程序或功能都是可以接受的。如果是函数,则可以返回输出而不是打印输出。
  • 禁止出现标准漏洞
  • 这是因此所有常见的高​​尔夫规则都适用,并且最短的代码(以字节为单位)获胜。

我们可以将输入作为数字列表吗?
丹尼斯

3
@丹尼斯当然可以。去保存一些字节。:p
AdmBorkBork

3
您是否有一个示例,其中重复数据删除实际上更改了输出?在您的445445示例中,每3个唯一的旋转发生两次,因此保留它们不会改变输出。
Kaldo

@Kaldo不,我无法(手动)提出一个,但这并不意味着一个不存在,因此我保留了重复数据删除规则。
AdmBorkBork '18

13
@Kaldo设Ð是数字数量Ñķ最小的正整数,使得旋转Ñ ķ数字向左再现Ñ。取q0≤r <k使得d = qk + r。旋转ÑdQK数字向左必须产生Ñ,所以R = 0。这意味着每个唯一的旋转都会发生q次,因此不需要对旋转进行重复数据消除以计算平均值。
丹尼斯

Answers:



9

APL(Dyalog),9字节

10⊥≢⍴+/÷≢

以数字向量为参数的单子函数。

在线尝试!

我取数字的平均值+/÷≢,然后重复输入的长度≢⍴,最后以10为底进行转换。

从概念上讲,我将轮换的总和(不携带):

 4  2  9  8
 2  9  8  4
 9  8  4  2
+8  4  2  9
 -----------
 23 23 23 23

这只是4+2+9+8重复4次。然后从基数10(对我来说是携带)转换并除以长度。尽管我之前用长度除以它是等效的并节省了字节。


1
那是和ial的:D
Leo

@Leo FWIW将平均值乘以代表位数的答案实际上是在做同样的事情
H.PWiz

3

爪哇10,163个 137 76 72 71字节

n->(Math.pow(10,n.size())-1)/9*n.stream().mapToInt(i->i).sum()/n.size()

-36个字节,感谢@Nevay@OlivierGrégoire通过创建@Dennis的Python 3 Answer端口
来提供-61个字节。 通过将输入作为数字列表而不是字符串作为-1字节。

说明:

在线尝试。

n->                                 // Method with String parameter and double return-type
  (Math.pow(10,n.size())-1)/9       //  Repunits the same length as the input-size
  *n.stream().mapToInt(i->i).sum()  //  multiplied by the sum of digits
  /n.size()                         //  divided by the input-size

1
151字节:n->{var s=new java.util.HashSet();var r=0d;for(int l=n.length(),x;l-->0;)if(s.add(x=new Integer(n=n.substring(1)+n.charAt(0))))r+=x;return r/s.size();},具有137字节的流方法:n->java.util.stream.IntStream.range(0,n.length()).map(i->new Integer(n.substring(i)+n.substring(0,i))).distinct().average().getAsDouble()
Nevay

1
使用orElse(0)代替getAsDouble()
奥利维尔·格雷戈雷

69个字节,基于其他人的解决方案。回合使用(int)5微尘字节,如果需要的话。
OlivierGrégoire'18

您无需强制转换为双:Math.pow已经解决了这一问题。这将为您节省3个字节。
奥利维尔·格雷戈尔(OlivierGrégoire),

@OlivierGrégoire如果这样做,将会给出错误的结果。使用强制转换为int的值,因此我们可以将- 整数除以9并乘以数字总和。只有这样,它才能加倍以获得平均值。如果我将两者都删除(int)*.1则它将例如输出6388.888...而不是6388.25输入4928。如果我将整个对象或仅将对象转换.pow为一个对象int,它将输出6388
凯文·克鲁伊森

3

外壳,5个字节

d´MKA

在线尝试!

说明

d´MKA
    A  Take the average of the digits
 ´MK   Replace each element of the original list with the average
d      Join the list to get a number

外壳,7个字节

A§modṙŀ

在线尝试!

说明

A§modṙŀ
      ŀ  Take the range [1..length(input)]
 §m  ṙ   Rotate the input by each element of the range
   od    Convert each list of digits to a number
A        Take the average of the list

1
令人困惑的是,至少有一个5字节的解决方案
H.PWiz

@ H.PWiz我无法弄清楚,您能给个提示吗?:P
Leo

@Leo没有ŀ,并且第一个字符(在左侧)不是A
H.PWiz

3

R84 73 64字节

function(D,K=sum(D|1))mean(array(D,K+1:0)[1:K,1:K]%*%10^(K:1-1))

在线尝试!

输入为数字列表。

感谢MickyT削减了11个字节!Dennis证明重复数据消除是不必要的,因此减少了8个字节。


以稍微不同的方式旋转数字为73
MickyT

@MickyT aaahhh巧妙利用回收!
朱塞佩

@MickyT array(D,K+1:0)matrix(D,K+1,K)一个字节短。
朱塞佩

2

05AB1E,9个字节

vÀD}\OIg/

在线尝试!


v没有y,有趣。
魔术章鱼缸

gFÀD})¨Osg/是我在想的地方。
魔术章鱼缸

您知道如何使用该.æ = pop a compute permutations by function, usage: .æ<FUNC>}命令吗?我也不是,但这似乎很合适。
魔术章鱼缸

不带y的@MagicOctopusUrn v是我发现执行g(input)次旋转的最短解决方案。我正在检查.æ,似乎没有在注册<FUNC>}
Kaldo

2

Stax,6 个字节

ñJä⌠╤►

运行并调试

该程序将用引号分隔的字符串作为输入,并将平均值表示为缩减的分数。例如777/1 ,不必重复旋转。它永远不会改变结果。

拆开包装,松开包装并进行评论,看起来像这样。

:)  get all character rotations
{em convert strings back to integers
:V  mean - integer inputs means result will be rational

运行这个


2

Perl 6、15个字节

{.sum/$_*1 x$_}

在线尝试!

平均值是应用于每个小数位的数字平均值,因此数字平均值乘以111 .... 1 x $_会生成一个1s的字符串,并通过乘法将其强制转换为字符串。

将数字列表作为输入。序列在总和之前需要.cache,而数字或字符串输入则需要.comb。



1

JavaScript(Node.js),43字节

x=>eval(x.join`+`)*'1'.repeat(n=x.length)/n

在线尝试!

我们可以将输入作为数字列表吗?–丹尼斯♦7分钟前

@丹尼斯当然,那很好。去保存一些字节。:p – AdmBorkBork 3分钟前


1

果冻6 5字节

ṙJḌÆm

在线尝试!

怎么运行的

ṙJḌÆm  Main link. Argument: A (digit array)

 J     Yield the indices of A, i.e., [1, ..., len(A)].
ṙ      Rotate A 1, ..., and len(A) units to the left, yielding a 2D array.
  Ḍ    Convert each rotation from decimal to integer.
   Æm  Take the arithmetic mean.

1

Japt,8字节

将输入作为单个数字字符串的数组。

xpUÊ)÷UÊ

尝试一下


说明

             :Implicit input of array U
 pUÊ         :Repeat each string length of U times
x   )        :Reduce by addition
     ÷UÊ     :Divide by the length of U

1

APL(Dyalog Unicode)21个 14字节SBCS

+/≢÷⍨⍳∘≢(⍎⌽)¨⊂

在线尝试!

隐式前缀功能。将输入作为字符串。

感谢Adám 节省了7个字节

怎么样?

+/≢÷⍨⍳∘≢(⍎⌽)¨⊂  Main fn, example argument '123'
                Enclose the argument (turns it into a scalar)
             ¨   Use each of the left arguments to
         ( ⌽)    Rotate, then
                Convert strings into numbers
      ⍳∘≢        Tally (≢) the argument, then index (⍳) from 1. 
                 Returns 1 2 3 for a 3 digit argument, and rotates the argument 1, 2, then 3 times.
                Use the result as left argument for
    ÷            Divide
                By the number of rotations
+/               And sum the results

:| 仍然无法习惯APL注释


1

木炭,11字节

I∕×ΣθI⭆θ1Lθ

在线尝试!链接是详细版本的代码。说明:

    θ  θ  θ Input as a string
   Σ        Sum of digits
      ⭆ 1   Replace each character with the literal `1`
     I      Cast to integer
  ×         Multiply
         L  Length
 ∕          Divide
I           Cast to string
            Implicitly print

1

J,10个字节

10#.#$+/%#

这是H.PWiz对J.而言的出色APL解决方案的移植。

将数字列表作为参数。

说明:

+/%#平均的数字(划分%的数字的总和+/可以通过数#

#$根据位数创建平均值的副本列表

10#. 转换以10为底的表格

在线尝试!






1

C ++,218208字节

-10字节归功于Zacharý

#include<set>
#include<cmath>
float a(int m){std::set<int>a;int n=m,t,c=0;for(;n>0;n/=10)++c;for(;n<c;++n){a.insert(m);t=m%10;m=m/10+std::pow(10.f,c-1)*t;}int s=0;for(int v:a){s+=v;}return float(s)/a.size();}

并且,要测试:

int main() {
    printf("%f\n%f\n%f\n%f\n",a(123),a(4928),a(445445),a(777));
}

1
你不需要之间的间隔#include<,并且可以去除{}周围都++c;s+=v;。您也许可以将其int s=0与其他变量一起移到开头。
扎卡里

1
另外,我认为您不需要n=0在第二个for循环中使用,因为0届时应该已经到达了。m/=10;m+=std::pow(10.f,c-1)*t;=> m=m/10+std::pow(10.f,c-1)*t。而且会不会使用int代替auto工作?
扎卡里

您仍然可以int s=0;使用其他变量移动,并且是否需要用大括号括起来s+=v;
扎卡里


n>0=> n可能有效。
扎卡里

0

Pyth,12个字节

csms.<zdlzlz

可能是可改进的。

c         lz     Divide by the length of the input:
 s               Reduce by +
  m     lz       Map over d = [0 ... the length of the input]:
   s.<zd         Shift the input d characters to the left and cast to int

在这里尝试!


有一个内置的平均值函数o。如果这样做并且将I / O作为数字列表进行处理,则可以将其缩减为8个字节

嗯,我当时在讲“如果适用,您可以假定输入/输出将适合您语言的本机整数类型。” 表示如果接受则必须是整数Q
RK。

0

J,23字节

(+/%#)".~.(|."0 1~i.@#)

将输入作为字符串

说明

          (|."0 1~i.@#)  | All rotations
        ~.               | Deduplicate
      ".                 | Convert each to int
(+/%#)                   | Average

0

Matlab,65个字节

c=num2str(n);l=nnz(c);mean(str2num(c(mod((1:l)+(0:l-1)'-1,l)+1)))

要做这个,很确定它可以做得更好。


0

Clojure,139个字节

#(let[n(count %)G(set(apply map list(for[i(range n)](take n(drop i(cycle %))))))](/(apply +(for[g G](read-string(apply str g))))(count G)))

相当次优的语言功能,用于将字符序列转换为整数。


0

dc,37个字节

这是一个完整的程序,读取输入并输出输出:

?1sd[O~rzsadO<x+ldO*1+sd]dsxxOkld*la/p

它的工作原理是将数字分成几个数字,然后将数字的均值乘以适当的长度repdigit(随行建立d)。

?                               # read input
 1sd                            # initialize d=1
    [                   ]dsxx   # define and execute recursive macro x:
     O~r                        #   remainder and quotient of /10
        zsa                     #   a = number of digits
           dO<x                 #   recurse if needed
               +ldO*1+sd        #   increment repdigit d
                             Ok         # after executing x, set precision 
                               ld*la/   # multiply by repdigit; divide by a
                                     p  # print the result
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.