计算数组中的更改


20

今天的任务是编写一个程序或函数,该程序或函数采用整数数组,并计算从左到右的次数,即值发生变化的次数。通过示例更容易显示:[1 1 1 2 2 5 5 5 5 17 3] => [1 1 1 **2** 2 **5** 5 5 5 **17** **3**] => 4

测试用例:

Input           |   Output
[]              |   0
[0]             |   0
[0 1]           |   1
[0 0]           |   0
[1 2 3 17]      |   3
[1 1 1 2 2 3]   |   2
[-3 3 3 -3 0]   |   3

这是,最少字节获胜!


如果总是正确地计算结果,但如果False打印为0,我的答案是否有效?
FlipTack

1
@FlipTack这取决于语言。总的来说,如果我可以说2+False并且出错了,那不是很好,但是如果我得到了2,那很好。
帕维尔

@FlipTack默认情况下,是共识。
完全人类

空输出是否0可以接受?
泰特斯

@Titus是的。
Pavel

Answers:



9

Python 3,38个字节

f=lambda x=0,*y:y>()and(x!=y[0])+f(*y)

在线尝试!


2
呵呵,确实知道您可以使用默认的arg,不错的发现。
xnor


@Dennis当数组为空时,函数如何退出递归循环?我看不出这不会以结尾maximum recursion depth exceeded
爱奥尼斯(Ioannes)

@Ioannes一旦仅剩一个元素(x),其结果y>()将为False,因此and不会执行以下代码。
丹尼斯

7

Haskell,33个字节

f(a:b:r)=sum[1|a/=b]+f(b:r)
f _=0

在线尝试!


奖励:有点奇怪的无点算术版本(44字节)

sum.(tail>>=zipWith((((0^).(0^).abs).).(-)))

在线尝试!

给定输入[1,1,4,3,3,3],我们首先取相邻条目([0,3,-1,0,0])的差,然后取absolute值:[0,3,1,0,0]。以零到每个元素在第一时间率的功率[1,0,0,1,1],和第二次反转列表:[0,1,1,0,0](1-)也将代替在这里工作(0^))。最后,我们sum从清单中获得2



5

50字节的Brain-Flak

([][()]){{}({}[({})]){{}<>({}())(<>)}{}([][()])}<>

在线尝试!

对于0,什么也不输出,这在脑中是等效的。如果这是不可接受的,则将其附加为+4字节:({})

说明:

#Push stack-height-1
([][()])

#While true:
{

    #Pop the stack-height-1 off
    {}

    #If 'a' is the element on top of the stack, and 'b' is the element underneath it, then
    #Pop 'a' off, and push (a - b)
    ({}[({})])

    #If (a-b) is not 0...
    {
        #Pop (a-b) off
        {}

        #Switch stacks
        <>

        #Increment the value on the other stack
        ({}())

        #Push a 0 back to the main stack
        (<>)

    #Endif
    }

    #Pop either (a-b) or the 0 we pushed
    {}

    #Push stack-height-1
    ([][()])

#Endwhile
}

#Toggle to the alternate stack and display the counter
<>


@Riley做得好!:)
DJMcMayhem


1
@WheatWizard我也尝试过,但是它在空输入时会永远循环。-0+1 = 1
H.PWiz

5

50字节的Brain-Flak

(([][()]){[{}]<({}[({})])>{(<{}>)()}{}([][()])}<>)

在线尝试!

# Get ready to push the answer
(

# Push stack height - 1
([][()])

# Loop until 0 (until the stack has a height of 1)
{

  # Pop the old stack height and subtract it 
  #(cancels the loop counter from the final answer)
  [{}]

  # Pop the top of the stack and subtract the next element from that
  # Don't include this in the final answer
  <({}[({})])>

  # If not 0
  {

    # Pop the difference between the last two numbers
    # Don't include this in the final answer
    (<{}>)

    # Add 1 to the final answer
    ()

  # End if
  }{}

  # Push stack height - 1
  ([][()])

# End while
}

# Switch to the off stack so we don't print anything extra
<>

# Push the total sum. This is the number of times the if was true
)

1
恭喜10k代表!
帕维尔

@Pavel谢谢!我花了永远的时间才获得最后几百个。我已经忙于其他事情了:(
Riley

我有这个
H.PWiz

@ H.PWiz我曾经有过这一点,但是我喜欢弹出框如何取消堆栈高度推动。
莱利

5

Haskell,35个字节

-8个字节感谢H.PWiz。

Out-golfed by a recursive version. Haskell is pretty much the best at recursion and I missed it. >_<

f l=sum[1|x<-zipWith(/=)l$tail l,x]

Try it online!

It'd be awesome if anybody figured out how to employ this tip.

Alternate solution, 36 bytes

f l=sum[1|True<-zipWith(/=)l$tail l]

Try it online!



That tip misses the crucial fact that you'd need to uncurry the function f to get it to work. This sum.map fromEnum.(zipWith(/=)=<<tail) is probably the closest you get, but it won't work with [] and is 37 bytes..
ბიმო

5

Java (OpenJDK 8), 65 bytes

Not as short as I'd like, but that's just Java for you.

Test by passing the array as a comma delimited list.

a->{int s=0,i=1;for(;i<a.length;s+=a[i-1]!=a[i++]?1:0);return s;}

Try it online!


2
If the empty array wasn't a test case (and I don't find it really relevant, actually), one could have used: a->{int s=0,p=a[0];for(int n:a)s+=p==(p=n)?0:1;return s;} (57 bytes).
Olivier Grégoire

@OlivierGrégoire I know! I wrote that out and thought I'd managed to cut down the bytes but it failed on that first case.
Luke Stevens

3
56 bytes: a->{int s=0;for(int i:a)s+=a[0]!=(a[0]=i)?1:0;return s;}
Nevay

4

Husk, 3 bytes

Ltg

Try it online!

Explanation

Ltg    Input: [1,1,1,2,2,3]
  g    Group equal elements together: [[1,1,1],[2,2],[3]]
 t     Drop the first group (if any): [[2,2],[3]]
L      Return the length of the list: 2


4

Wolfram Language (Mathematica), 23 24 26 29 bytes

Length@Split@#~Max~1-1&

Try it online!

  • -1 byte thanks to Martin Ender!
  • -2 bytes thanks to JungHwan Min! nice use of Split[].
  • -3 bytes thanks to totallyhuman!

a little explanation:

Split will divide an array into a list of lists (of same elements), that is, turning {1, 2, 2, 3, 1, 1} into {{1}, {2, 2}, {3}, {1, 1}} . So, Length@Split@# is the quantity of consecutive segements. Max[*****-1, 0] is used to deal with {} input.



1
24 bytes: Max[Length@Split@#-1,0]&
JungHwan Min

23: Length@Split@#~Max~1-1&
Martin Ender

4

Retina, 24 21 16 bytes

Thanks to @MartinEnder for -3 bytes and noticing a bug
-1 byte thanks to @tsh
-4 bytes thanks to @Leo

m`^(\S+)¶(?!\1$)

Try it online!


4

Symbolic Python, 120 117 bytes

Golfed 3 bytes by removing an explicit cast to integer (using unary +) for the counter variable - this means that if there are no changes in the array the output will be False instead of 0, but this is allowed by meta.

___=-~(_==_)
__('___=~-'+`_>_`[___::___]+`__`[-~___]+'(_)')
__('__=___=_>_'+';___+=_[__]!=_[-~__];__=-~__'*___)
_=___

Try it online!

# LINE 1: Generate value '2' for utility
___=-~(_==_)

# LINE 2: Get len(input) - 1
__('___=~-'+`_>_`[___::___]+`__`[-~___]+'(_)')
   '___=~-'+`_>_`[___::___]+`__`[-~___]+'(_)'     # Generate string '___=~-len(_)'
            `_>_`[___::___]                       #    'le' spliced from 'False'
                           +`__`[-~___]           #    'n' indexed from '<function ...>'
   '___=~-'+                           +'(_)'     #    Remaining characters in plaintext
__(                                          )    # Execute this to get len(input) - 1

# LINE 3: Main calculation loop
__('__=___=_>_'+';___+=_[__]!=_[-~__];__=-~__'*___) 
__(                                               ) # Execute:
   '__=___=_>_'                                     #   Set var1, var2 to 0
               +';                           '*___  #   len(input) - 1 times do:
                       _[__]!=_[-~__]               #   Compare input[var1, var1 + 1]
                  ___+=              ;              #   Add this to var2
                                      __=-~__       #   Increment var1

# LINE 4: Set output variable ('_') to the result calculated.
_=___                                       

2
=_= what is this wizardry?
totallyhuman

3

Jelly, 3 bytes

ITL

Try it online!

How it works

ITL  - Full program.

I    - Increments (deltas).
 T   - Get the indices of truthy values (gets the indexes of non-0 elements).
  L  - Length.

3

K (oK), 8 bytes

Solution:

+/1_~~':

Try it online!

Examples:

+/1_~~':1 1 1 2 2 5 5 5 5 17 3
4
+/1_~~':()
0
+/1_~~':-3 3 3 -3 0
3

Explanation:

Interpreted right-to-left:

+/1_~~': / the solution
     ~': / equal each-previous
    ~    / not (ie differ)
  1_     / 1 drop, remove first as this is different to null
+/       / sum up trues



3

R, 24 bytes

cat(sum(!!diff(scan())))

Try it online!

Same as the MATL answer, just used sum(!!diff)) since there's no nnz.


+1 I thought using rle would be shorter, but nope, length(rle()$v) uses too many characters and is off by one.
Neal Fultz

@NealFultz it's probably still worth posting as an answer! Always good to see another approach. And you should use sum(rle()$v|1) instead of length anyway. :)
Giuseppe

3

Cubix, 24 bytes

UpO@0I>I!^-u>q.uvv$!^;)p

Try it online

Note that Cubix uses 0 to indicate that there are no more inputs, so 0 cannot be in the list.

Explanation

Unfolded:

    U p
    O @
0 I > I ! ^ - u
> q . u v v $ !
    ^ ;
    ) p

We start at the 0, pushing the counter (initialized with 0) and the first input (I) onto the stack.

We then enter the loop. At each iteration of the loop, we get the next input with I. If it's 0, we've run out of inputs, so we rotate the counter to the top (p), Output, and exit (@).

Otherwise, we take the difference of the top two elements. If it's nonzero, we rotate the counter to the top, increment it, and rotate it back to the bottom with p)q. We then pop the difference with ; before moving to the next iteration.

All the characters not mentioned here are just control flow. There tend to be a lot of those in Cubix programs.


@MickyT Good approach, but you seem to be overcounting by 1. You could swap the 0 for a (, but that fails on the empty input.

apologies, will look at it again
MickyT

3

Brain-Flak, 50 bytes

(([][()]){[{}({}[({})])]{{}()(<()>)}{}([][()])}<>)

Try it online!

Since everyone is posting their 50 byte solutions here is mine (I have a 48 byte one but it was a simple modification of DjMcMayhem's so I did feel it worth posting)

Explanation

This answer extensively uses value canceling.

Un-golfed it looks like

([][()])({<{}({}[({})])>{<{}>()(<()>)}{}<([][()])>}<>)

Here we compute the delta's until the stack has one item left, each time we accumulate one value from the inner loop if the delta is non zero.

This is a pretty straight forward way of doing it.

To make this golfy we begin value canceling. The first one and the one that should be obvious to any hardened brain-flak golfer is the stack heights. It is a well known fact that

([])({<{}>...<([])>}{})

is the same as

(([]){[{}]...([])}{})

When the values are modified by one, the same holds. This gives us

(([][()]){[{}]<({}[({})])>{<{}>()(<()>)}{}([][()])}<>)

You may notice this didn't even save us bytes, but don't fret it will become more useful as we go on.

We can perform another reduction, if you see a statement

<(...)>{<{}> ...

you can actually reduce it to

[(...)]{{} ...

This works because if we enter the loop [(...)] and {} will cancel, and if we don't the value of [(...)] already was zero in the first place and doesn't need to be canceled. Since we have an occurrence of this pattern in our code we can reduce it.

(([][()]){[{}][({}[({})])]{{}()(<()>)}{}([][()])}<>)

That saved us 2 bytes but it also put two negs next to each other. These can be combined to save us another 2.

(([][()]){[{}({}[({})])]{{}()(<()>)}{}([][()])}<>)

And that's our code.


3

Perl 6, 18 bytes

{sum $_ Z!= .skip}

Test it

Expanded:

{ # bare block lambda with implicit parameter 「$_」

  sum         # count the number of True values

      $_      # the input
    Z!=       # zip using &infix:«!=»
      .skip   # the input, but starting from the second value
              # (implicit method call on 「$_」
}

3

Gaia, 2 bytes

ėl

Try it online!

This abuses a bug (or feature?) of Gaia, that run-length-encoding doesn't take the last run of elements into account. Note that I have double checked, it works for all test cases.

  • ė - Run length encoding (with the flaw described above).
  • l - Length.

2

JavaScript (ES6), 35 bytes

a=>a.filter((e,i)=>e-a[i+1]).length

I wonder if it could be shortened by using recursion. But my best attempt is 35 as well: f=([a,...b])=>1/a?!!(a-b[0])+f(b):0
Arnauld

@Arnauld I'd tried that too, but miscounted and thought it was 36 bytes, otherwise I'd have added it in as an alternative.
Neil


2

APL (Dyalog), 8 bytes

+/2≠/⊃,⊢

Try it online!

How?

⊃,⊢ - the list, with the first value repeated for the case of single element

2≠/ - changes list, not equal for every 2 elements

+/ - sum



2

J, 10 bytes

[:+/2~:/\]

Infixes of length 2... are they unequal? 2 ~:/\ ]

Sum the resulting list of 0s and 1s: +/

Try it online!


[:+/0=-/\ ought to work for I think 9 bytes.
cole

2

Ruby, 31 bytes

->a{a.chunk{|x|x}.drop(1).size}

Try it online!


Instead of .drop(1) you can do [1..-1]
Cyoce

@Cyoce Unfortunately drop returns an Enumerator, not an Array, so that doesn't work.
Jordan

huh. It returns an Array on my version.
Cyoce

@Cyoce Which version?
Jordan

I'm on 1.9.3 but why can't you take the size of an Array anyway?
Cyoce

2

C (gcc 5.4.0), 61 bytes

f(c,v)int*v;{int*p=v,s=0;for(;p<v+c-1;s+=*p++!=*p);return s;}

Try it Online!

f is a function taking the length of the array and a pointer to the first element of the array, and returning the number of changes in the array;

This submission utilizes undefined behavior (*p++!=*p, p is used twice in an expression in which it is changed), which works on my machine (gcc 5.4.0) and on TIO, but may not work on other implementations or versions.

Explanation:

f(c,v)int*v;{ // old-style declaration for v, and implicit-int for c and return value
    int*p=v,s=0; // p is a pointer to the current item, s is the number of changes
    for(;p<v+c-1;s+=*p++!=*p); // for each consecutive pair of integers, if they are different, add one to the number of changes
    return s; // return the number of changes
}

Could you maybe add a link to an online testing environment?
Jonathan Frech

@JonathanFrech Added
pizzapants184

2

05AB1E, 3 bytes

γ¦g

Try it online!

An alternative to Erik's answer.

γ¦g  ~ Full program.

γ    ~ Group into runs of equal adjacent elements.
 ¦   ~ Remove the first group (if there are any).
  g  ~ Length.

Ah, missed that that was a case.
Magic Octopus Urn
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.