Number of transformations until repeat
up vote
12
down vote
favorite
Given a sequence of integers or to be more specific a permutation of 0..N
transform this sequence as following:
- output[x] = reverse(input[input[x]])
- repeat
For example: [2,1,0]
becomes [0,1,2]
and reversed is [2,1,0]
. [0,2,1]
becomes [0,1,2]
and reversed [2,1,0]
.
Example 1
In: 0 1 2
S#1: 2 1 0
S#2: 2 1 0
Output: 1
Example 2
In: 2 1 0
S#1: 2 1 0
Output: 0
Example 3
In: 3 0 1 2
S#1: 1 0 3 2
S#2: 3 2 1 0
S#3: 3 2 1 0
Output: 2
Example 4
In: 3 0 2 1
S#1: 0 2 3 1
S#2: 2 1 3 0
S#3: 2 0 1 3
S#4: 3 0 2 1
Output: 3
Your task is to define a function (or program) that takes a permutation of
integers 0..N
and returns (or outputs) the number of steps until a permutation occurs that has already occured. If X
transforms to X
then the output should be zero, If X
transforms to Y
and Y
to X
(or Y
) then the output should be 1.
Y -> Y: 0 steps
Y -> X -> X: 1 step
Y -> X -> Y: 1 step
A -> B -> C -> D -> C: 3 steps
A -> B -> C -> D -> A: 3 steps
A -> B -> C -> A: 2 steps
A -> B -> C -> C: 2 steps
A -> B -> C -> B: also 2 steps
Testcases:
4 3 0 1 2 -> 0 3 4 1 2 -> 4 3 2 1 0 -> 4 3 2 1 0: 2 steps
4 3 2 1 0 -> 4 3 2 1 0: 0 steps
4 3 1 2 0 -> 4 1 3 2 0 -> 4 3 2 1 0 -> 4 3 2 1 0: 2 steps
1 2 3 0 4 -> 4 1 0 3 2 -> 0 3 4 1 2 -> 4 3 2 1 0 -> 4 3 2 1 0: 3 steps
5 1 2 3 0 4 -> 0 5 3 2 1 4 -> 1 5 3 2 4 0 -> 1 4 3 2 0 5 ->
5 1 3 2 0 4 -> 0 5 3 2 1 4: 4 steps
If your language doesn't support "functions" you may assume that the sequence is given as whitespace seperated list of integers such as 0 1 2
or 3 1 0 2
on a single line.
Fun facts:
- the sequence 0,1,2,3,..,N will always transform to N,...,3,2,1,0
- the sequence N,..,3,2,1,0 will always transform to N,..,3,2,1,0
- the sequence 0,1,3,2,...,N+1,N will always transform to N,...,3,2,1,0
Bonus task:
Figure out a mathematical formula.
Optional rules:
- If your language's first index is 1 instead of 0 you can use permutations
1..N
(you can just add one to every integer in the example and testcases).
code-golf
|
show 1 more comment
up vote
12
down vote
favorite
Given a sequence of integers or to be more specific a permutation of 0..N
transform this sequence as following:
- output[x] = reverse(input[input[x]])
- repeat
For example: [2,1,0]
becomes [0,1,2]
and reversed is [2,1,0]
. [0,2,1]
becomes [0,1,2]
and reversed [2,1,0]
.
Example 1
In: 0 1 2
S#1: 2 1 0
S#2: 2 1 0
Output: 1
Example 2
In: 2 1 0
S#1: 2 1 0
Output: 0
Example 3
In: 3 0 1 2
S#1: 1 0 3 2
S#2: 3 2 1 0
S#3: 3 2 1 0
Output: 2
Example 4
In: 3 0 2 1
S#1: 0 2 3 1
S#2: 2 1 3 0
S#3: 2 0 1 3
S#4: 3 0 2 1
Output: 3
Your task is to define a function (or program) that takes a permutation of
integers 0..N
and returns (or outputs) the number of steps until a permutation occurs that has already occured. If X
transforms to X
then the output should be zero, If X
transforms to Y
and Y
to X
(or Y
) then the output should be 1.
Y -> Y: 0 steps
Y -> X -> X: 1 step
Y -> X -> Y: 1 step
A -> B -> C -> D -> C: 3 steps
A -> B -> C -> D -> A: 3 steps
A -> B -> C -> A: 2 steps
A -> B -> C -> C: 2 steps
A -> B -> C -> B: also 2 steps
Testcases:
4 3 0 1 2 -> 0 3 4 1 2 -> 4 3 2 1 0 -> 4 3 2 1 0: 2 steps
4 3 2 1 0 -> 4 3 2 1 0: 0 steps
4 3 1 2 0 -> 4 1 3 2 0 -> 4 3 2 1 0 -> 4 3 2 1 0: 2 steps
1 2 3 0 4 -> 4 1 0 3 2 -> 0 3 4 1 2 -> 4 3 2 1 0 -> 4 3 2 1 0: 3 steps
5 1 2 3 0 4 -> 0 5 3 2 1 4 -> 1 5 3 2 4 0 -> 1 4 3 2 0 5 ->
5 1 3 2 0 4 -> 0 5 3 2 1 4: 4 steps
If your language doesn't support "functions" you may assume that the sequence is given as whitespace seperated list of integers such as 0 1 2
or 3 1 0 2
on a single line.
Fun facts:
- the sequence 0,1,2,3,..,N will always transform to N,...,3,2,1,0
- the sequence N,..,3,2,1,0 will always transform to N,..,3,2,1,0
- the sequence 0,1,3,2,...,N+1,N will always transform to N,...,3,2,1,0
Bonus task:
Figure out a mathematical formula.
Optional rules:
- If your language's first index is 1 instead of 0 you can use permutations
1..N
(you can just add one to every integer in the example and testcases).
code-golf
I meant more like a "closed formula" such as $f(a_{0},a_{1},a_{...}} = a_{0}^a_{1}+...$ where $a_{i}$ is the i-th element in the given sequence.
– mroman
yesterday
Are you sure such a "closed formula" exists?
– Todd Sewell
20 hours ago
"returns (or outputs) the number of steps until a permutation occurs that has already occured." This is inconsistent with just about everything that follows it. For a start, it makes a return value of 0 impossible...
– Peter Taylor
6 hours ago
Is the 3rd example correct? I see3,0,1,2
should transform to2,3,0,1
– FireCubez
3 hours ago
It's the number of transformations before a repeat.
– mroman
26 mins ago
|
show 1 more comment
up vote
12
down vote
favorite
up vote
12
down vote
favorite
Given a sequence of integers or to be more specific a permutation of 0..N
transform this sequence as following:
- output[x] = reverse(input[input[x]])
- repeat
For example: [2,1,0]
becomes [0,1,2]
and reversed is [2,1,0]
. [0,2,1]
becomes [0,1,2]
and reversed [2,1,0]
.
Example 1
In: 0 1 2
S#1: 2 1 0
S#2: 2 1 0
Output: 1
Example 2
In: 2 1 0
S#1: 2 1 0
Output: 0
Example 3
In: 3 0 1 2
S#1: 1 0 3 2
S#2: 3 2 1 0
S#3: 3 2 1 0
Output: 2
Example 4
In: 3 0 2 1
S#1: 0 2 3 1
S#2: 2 1 3 0
S#3: 2 0 1 3
S#4: 3 0 2 1
Output: 3
Your task is to define a function (or program) that takes a permutation of
integers 0..N
and returns (or outputs) the number of steps until a permutation occurs that has already occured. If X
transforms to X
then the output should be zero, If X
transforms to Y
and Y
to X
(or Y
) then the output should be 1.
Y -> Y: 0 steps
Y -> X -> X: 1 step
Y -> X -> Y: 1 step
A -> B -> C -> D -> C: 3 steps
A -> B -> C -> D -> A: 3 steps
A -> B -> C -> A: 2 steps
A -> B -> C -> C: 2 steps
A -> B -> C -> B: also 2 steps
Testcases:
4 3 0 1 2 -> 0 3 4 1 2 -> 4 3 2 1 0 -> 4 3 2 1 0: 2 steps
4 3 2 1 0 -> 4 3 2 1 0: 0 steps
4 3 1 2 0 -> 4 1 3 2 0 -> 4 3 2 1 0 -> 4 3 2 1 0: 2 steps
1 2 3 0 4 -> 4 1 0 3 2 -> 0 3 4 1 2 -> 4 3 2 1 0 -> 4 3 2 1 0: 3 steps
5 1 2 3 0 4 -> 0 5 3 2 1 4 -> 1 5 3 2 4 0 -> 1 4 3 2 0 5 ->
5 1 3 2 0 4 -> 0 5 3 2 1 4: 4 steps
If your language doesn't support "functions" you may assume that the sequence is given as whitespace seperated list of integers such as 0 1 2
or 3 1 0 2
on a single line.
Fun facts:
- the sequence 0,1,2,3,..,N will always transform to N,...,3,2,1,0
- the sequence N,..,3,2,1,0 will always transform to N,..,3,2,1,0
- the sequence 0,1,3,2,...,N+1,N will always transform to N,...,3,2,1,0
Bonus task:
Figure out a mathematical formula.
Optional rules:
- If your language's first index is 1 instead of 0 you can use permutations
1..N
(you can just add one to every integer in the example and testcases).
code-golf
Given a sequence of integers or to be more specific a permutation of 0..N
transform this sequence as following:
- output[x] = reverse(input[input[x]])
- repeat
For example: [2,1,0]
becomes [0,1,2]
and reversed is [2,1,0]
. [0,2,1]
becomes [0,1,2]
and reversed [2,1,0]
.
Example 1
In: 0 1 2
S#1: 2 1 0
S#2: 2 1 0
Output: 1
Example 2
In: 2 1 0
S#1: 2 1 0
Output: 0
Example 3
In: 3 0 1 2
S#1: 1 0 3 2
S#2: 3 2 1 0
S#3: 3 2 1 0
Output: 2
Example 4
In: 3 0 2 1
S#1: 0 2 3 1
S#2: 2 1 3 0
S#3: 2 0 1 3
S#4: 3 0 2 1
Output: 3
Your task is to define a function (or program) that takes a permutation of
integers 0..N
and returns (or outputs) the number of steps until a permutation occurs that has already occured. If X
transforms to X
then the output should be zero, If X
transforms to Y
and Y
to X
(or Y
) then the output should be 1.
Y -> Y: 0 steps
Y -> X -> X: 1 step
Y -> X -> Y: 1 step
A -> B -> C -> D -> C: 3 steps
A -> B -> C -> D -> A: 3 steps
A -> B -> C -> A: 2 steps
A -> B -> C -> C: 2 steps
A -> B -> C -> B: also 2 steps
Testcases:
4 3 0 1 2 -> 0 3 4 1 2 -> 4 3 2 1 0 -> 4 3 2 1 0: 2 steps
4 3 2 1 0 -> 4 3 2 1 0: 0 steps
4 3 1 2 0 -> 4 1 3 2 0 -> 4 3 2 1 0 -> 4 3 2 1 0: 2 steps
1 2 3 0 4 -> 4 1 0 3 2 -> 0 3 4 1 2 -> 4 3 2 1 0 -> 4 3 2 1 0: 3 steps
5 1 2 3 0 4 -> 0 5 3 2 1 4 -> 1 5 3 2 4 0 -> 1 4 3 2 0 5 ->
5 1 3 2 0 4 -> 0 5 3 2 1 4: 4 steps
If your language doesn't support "functions" you may assume that the sequence is given as whitespace seperated list of integers such as 0 1 2
or 3 1 0 2
on a single line.
Fun facts:
- the sequence 0,1,2,3,..,N will always transform to N,...,3,2,1,0
- the sequence N,..,3,2,1,0 will always transform to N,..,3,2,1,0
- the sequence 0,1,3,2,...,N+1,N will always transform to N,...,3,2,1,0
Bonus task:
Figure out a mathematical formula.
Optional rules:
- If your language's first index is 1 instead of 0 you can use permutations
1..N
(you can just add one to every integer in the example and testcases).
code-golf
code-golf
edited yesterday
asked yesterday
mroman
977511
977511
I meant more like a "closed formula" such as $f(a_{0},a_{1},a_{...}} = a_{0}^a_{1}+...$ where $a_{i}$ is the i-th element in the given sequence.
– mroman
yesterday
Are you sure such a "closed formula" exists?
– Todd Sewell
20 hours ago
"returns (or outputs) the number of steps until a permutation occurs that has already occured." This is inconsistent with just about everything that follows it. For a start, it makes a return value of 0 impossible...
– Peter Taylor
6 hours ago
Is the 3rd example correct? I see3,0,1,2
should transform to2,3,0,1
– FireCubez
3 hours ago
It's the number of transformations before a repeat.
– mroman
26 mins ago
|
show 1 more comment
I meant more like a "closed formula" such as $f(a_{0},a_{1},a_{...}} = a_{0}^a_{1}+...$ where $a_{i}$ is the i-th element in the given sequence.
– mroman
yesterday
Are you sure such a "closed formula" exists?
– Todd Sewell
20 hours ago
"returns (or outputs) the number of steps until a permutation occurs that has already occured." This is inconsistent with just about everything that follows it. For a start, it makes a return value of 0 impossible...
– Peter Taylor
6 hours ago
Is the 3rd example correct? I see3,0,1,2
should transform to2,3,0,1
– FireCubez
3 hours ago
It's the number of transformations before a repeat.
– mroman
26 mins ago
I meant more like a "closed formula" such as $f(a_{0},a_{1},a_{...}} = a_{0}^a_{1}+...$ where $a_{i}$ is the i-th element in the given sequence.
– mroman
yesterday
I meant more like a "closed formula" such as $f(a_{0},a_{1},a_{...}} = a_{0}^a_{1}+...$ where $a_{i}$ is the i-th element in the given sequence.
– mroman
yesterday
Are you sure such a "closed formula" exists?
– Todd Sewell
20 hours ago
Are you sure such a "closed formula" exists?
– Todd Sewell
20 hours ago
"returns (or outputs) the number of steps until a permutation occurs that has already occured." This is inconsistent with just about everything that follows it. For a start, it makes a return value of 0 impossible...
– Peter Taylor
6 hours ago
"returns (or outputs) the number of steps until a permutation occurs that has already occured." This is inconsistent with just about everything that follows it. For a start, it makes a return value of 0 impossible...
– Peter Taylor
6 hours ago
Is the 3rd example correct? I see
3,0,1,2
should transform to 2,3,0,1
– FireCubez
3 hours ago
Is the 3rd example correct? I see
3,0,1,2
should transform to 2,3,0,1
– FireCubez
3 hours ago
It's the number of transformations before a repeat.
– mroman
26 mins ago
It's the number of transformations before a repeat.
– mroman
26 mins ago
|
show 1 more comment
9 Answers
9
active
oldest
votes
up vote
4
down vote
JavaScript (ES6), 54 bytes
a=>~(g=a=>g[a]||~-g(g[a]=a.map(i=>a[i]).reverse()))(a)
Try it online!
What doesdo on a function?
– mroman
yesterday
A function is an object. So,g[a]
can be used on it to access the propertya
.
– Arnauld
yesterday
Ah I see. You're usingg
to store the state in.
– mroman
yesterday
add a comment |
up vote
4
down vote
Python 2, 67 bytes
f=lambda l,*h:len(h)-1if l in h else f([l[i]for i in l][::-1],l,*h)
Try it online!
add a comment |
up vote
3
down vote
Pyth, 10 9 8 bytes
tl.u@LN_
Explanation:
t One less than
l the number of values achieved by
.u repeating the following lambda N until already seen value:
@LN_N composing permutation N with its reverse
Q starting with the input.
Test suite.
add a comment |
up vote
3
down vote
Haskell, 52 bytes
(#)
a#x|elem x a= -1|n<-x:a=1+n#reverse(map(x!!)x)
Try it online!
a # x -- function '#' takes a list of all permutations
-- seen so far (-> 'a') and the current p. (-> 'x')
| elem x a = -1 -- if x has been seen before, return -1
| n<-x:a = -- else let 'n' be the new list of seen p.s and return
1 + -- 1 plus
n # -- a recursive call of '#' with the 'n' and
reverse ... -- the new p.
(#) -- start with an empty list of seen p.s
add a comment |
up vote
3
down vote
Perl 6, 44 35 bytes
-9 bytes thanks to nwellnhof
{($_,{.[[R,] |$_]}...{%.{$_}++})-2}
Try it online!
Explanation:
{ } # Anonymous code block
... # Create a sequence where:
$_, # The first element is the input list
{.[[R,] |$_]} # Subsequent elements are the previous element reverse indexed into itself
{ } # Until
%.{$_} # The index of the listt in an anonymous hash is non-zero
++ # Then post increment it
( )-2 # Return the length of the sequence minus 2
add a comment |
up vote
2
down vote
J, 33 27 26 bytes
-7 thanks to bubbler
_1(+~:i.0:)|.@C.~^:(<@!@#)
Try it online!
how
original explanation. my last improvement only changes the piece which finds "the index of the first element we've seen already". it now uses the "nub sieve" to do it in fewer bytes.
1 <:@i.~ [: ({: e. }:) |.@C.~^:(<@!@#)
|.@C.~ NB. self-apply permutation and reverse
^: NB. this many times:
(<@!@#) NB. the box of the factorial of the
NB. the list len. this guarantees
NB. we terminate, and the box means
NB. we collect all the results
[: ({: e. }:) NB. apply this to those results:
NB. for each prefix
{: e. }: NB. is the last item contained in
NB. the list of previous items?
1 <:@i.~ NB. in that result find:
1 i.~ NB. the index of the first 1
<:@ NB. and subtract 1
Note the entire final phrase 1<:@i.~[:({:e.}:)
is devoted to finding "the index of the first element which has already been seen." This seems awfully long for obtaining that, but I wasn't able to golf it more. Suggestions welcome.
add a comment |
up vote
1
down vote
Jelly, 6 bytes
Ṛị$ƬL’
Try it online!
1-indexed.
add a comment |
up vote
1
down vote
Dyalog APL, 29 28 27 bytes
¯2∘+∘≢{⍵,⍨⊂⌽(⍋⍳⊢)⊃⍵}⍣{⍺≢∪⍺}
Takes boxed arrays. Will trainify and explain later.
Try it here as a test suite.
add a comment |
up vote
0
down vote
Clean, 90 bytes
import StdEnv
$l=length(until([h:t]=any((==)h)t)([h:t]=[reverse(map((!!)h)h),h:t])[l])-2
Try it online!
add a comment |
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
JavaScript (ES6), 54 bytes
a=>~(g=a=>g[a]||~-g(g[a]=a.map(i=>a[i]).reverse()))(a)
Try it online!
What doesdo on a function?
– mroman
yesterday
A function is an object. So,g[a]
can be used on it to access the propertya
.
– Arnauld
yesterday
Ah I see. You're usingg
to store the state in.
– mroman
yesterday
add a comment |
up vote
4
down vote
JavaScript (ES6), 54 bytes
a=>~(g=a=>g[a]||~-g(g[a]=a.map(i=>a[i]).reverse()))(a)
Try it online!
What doesdo on a function?
– mroman
yesterday
A function is an object. So,g[a]
can be used on it to access the propertya
.
– Arnauld
yesterday
Ah I see. You're usingg
to store the state in.
– mroman
yesterday
add a comment |
up vote
4
down vote
up vote
4
down vote
JavaScript (ES6), 54 bytes
a=>~(g=a=>g[a]||~-g(g[a]=a.map(i=>a[i]).reverse()))(a)
Try it online!
JavaScript (ES6), 54 bytes
a=>~(g=a=>g[a]||~-g(g[a]=a.map(i=>a[i]).reverse()))(a)
Try it online!
answered yesterday
Arnauld
69k584291
69k584291
What doesdo on a function?
– mroman
yesterday
A function is an object. So,g[a]
can be used on it to access the propertya
.
– Arnauld
yesterday
Ah I see. You're usingg
to store the state in.
– mroman
yesterday
add a comment |
What doesdo on a function?
– mroman
yesterday
A function is an object. So,g[a]
can be used on it to access the propertya
.
– Arnauld
yesterday
Ah I see. You're usingg
to store the state in.
– mroman
yesterday
What does
do on a function?– mroman
yesterday
What does
do on a function?– mroman
yesterday
A function is an object. So,
g[a]
can be used on it to access the property a
.– Arnauld
yesterday
A function is an object. So,
g[a]
can be used on it to access the property a
.– Arnauld
yesterday
Ah I see. You're using
g
to store the state in.– mroman
yesterday
Ah I see. You're using
g
to store the state in.– mroman
yesterday
add a comment |
up vote
4
down vote
Python 2, 67 bytes
f=lambda l,*h:len(h)-1if l in h else f([l[i]for i in l][::-1],l,*h)
Try it online!
add a comment |
up vote
4
down vote
Python 2, 67 bytes
f=lambda l,*h:len(h)-1if l in h else f([l[i]for i in l][::-1],l,*h)
Try it online!
add a comment |
up vote
4
down vote
up vote
4
down vote
Python 2, 67 bytes
f=lambda l,*h:len(h)-1if l in h else f([l[i]for i in l][::-1],l,*h)
Try it online!
Python 2, 67 bytes
f=lambda l,*h:len(h)-1if l in h else f([l[i]for i in l][::-1],l,*h)
Try it online!
answered yesterday
Erik the Outgolfer
30.7k429102
30.7k429102
add a comment |
add a comment |
up vote
3
down vote
Pyth, 10 9 8 bytes
tl.u@LN_
Explanation:
t One less than
l the number of values achieved by
.u repeating the following lambda N until already seen value:
@LN_N composing permutation N with its reverse
Q starting with the input.
Test suite.
add a comment |
up vote
3
down vote
Pyth, 10 9 8 bytes
tl.u@LN_
Explanation:
t One less than
l the number of values achieved by
.u repeating the following lambda N until already seen value:
@LN_N composing permutation N with its reverse
Q starting with the input.
Test suite.
add a comment |
up vote
3
down vote
up vote
3
down vote
Pyth, 10 9 8 bytes
tl.u@LN_
Explanation:
t One less than
l the number of values achieved by
.u repeating the following lambda N until already seen value:
@LN_N composing permutation N with its reverse
Q starting with the input.
Test suite.
Pyth, 10 9 8 bytes
tl.u@LN_
Explanation:
t One less than
l the number of values achieved by
.u repeating the following lambda N until already seen value:
@LN_N composing permutation N with its reverse
Q starting with the input.
Test suite.
edited 23 hours ago
answered yesterday
lirtosiast
15.4k436104
15.4k436104
add a comment |
add a comment |
up vote
3
down vote
Haskell, 52 bytes
(#)
a#x|elem x a= -1|n<-x:a=1+n#reverse(map(x!!)x)
Try it online!
a # x -- function '#' takes a list of all permutations
-- seen so far (-> 'a') and the current p. (-> 'x')
| elem x a = -1 -- if x has been seen before, return -1
| n<-x:a = -- else let 'n' be the new list of seen p.s and return
1 + -- 1 plus
n # -- a recursive call of '#' with the 'n' and
reverse ... -- the new p.
(#) -- start with an empty list of seen p.s
add a comment |
up vote
3
down vote
Haskell, 52 bytes
(#)
a#x|elem x a= -1|n<-x:a=1+n#reverse(map(x!!)x)
Try it online!
a # x -- function '#' takes a list of all permutations
-- seen so far (-> 'a') and the current p. (-> 'x')
| elem x a = -1 -- if x has been seen before, return -1
| n<-x:a = -- else let 'n' be the new list of seen p.s and return
1 + -- 1 plus
n # -- a recursive call of '#' with the 'n' and
reverse ... -- the new p.
(#) -- start with an empty list of seen p.s
add a comment |
up vote
3
down vote
up vote
3
down vote
Haskell, 52 bytes
(#)
a#x|elem x a= -1|n<-x:a=1+n#reverse(map(x!!)x)
Try it online!
a # x -- function '#' takes a list of all permutations
-- seen so far (-> 'a') and the current p. (-> 'x')
| elem x a = -1 -- if x has been seen before, return -1
| n<-x:a = -- else let 'n' be the new list of seen p.s and return
1 + -- 1 plus
n # -- a recursive call of '#' with the 'n' and
reverse ... -- the new p.
(#) -- start with an empty list of seen p.s
Haskell, 52 bytes
(#)
a#x|elem x a= -1|n<-x:a=1+n#reverse(map(x!!)x)
Try it online!
a # x -- function '#' takes a list of all permutations
-- seen so far (-> 'a') and the current p. (-> 'x')
| elem x a = -1 -- if x has been seen before, return -1
| n<-x:a = -- else let 'n' be the new list of seen p.s and return
1 + -- 1 plus
n # -- a recursive call of '#' with the 'n' and
reverse ... -- the new p.
(#) -- start with an empty list of seen p.s
edited 21 hours ago
answered 22 hours ago
nimi
30.8k31985
30.8k31985
add a comment |
add a comment |
up vote
3
down vote
Perl 6, 44 35 bytes
-9 bytes thanks to nwellnhof
{($_,{.[[R,] |$_]}...{%.{$_}++})-2}
Try it online!
Explanation:
{ } # Anonymous code block
... # Create a sequence where:
$_, # The first element is the input list
{.[[R,] |$_]} # Subsequent elements are the previous element reverse indexed into itself
{ } # Until
%.{$_} # The index of the listt in an anonymous hash is non-zero
++ # Then post increment it
( )-2 # Return the length of the sequence minus 2
add a comment |
up vote
3
down vote
Perl 6, 44 35 bytes
-9 bytes thanks to nwellnhof
{($_,{.[[R,] |$_]}...{%.{$_}++})-2}
Try it online!
Explanation:
{ } # Anonymous code block
... # Create a sequence where:
$_, # The first element is the input list
{.[[R,] |$_]} # Subsequent elements are the previous element reverse indexed into itself
{ } # Until
%.{$_} # The index of the listt in an anonymous hash is non-zero
++ # Then post increment it
( )-2 # Return the length of the sequence minus 2
add a comment |
up vote
3
down vote
up vote
3
down vote
Perl 6, 44 35 bytes
-9 bytes thanks to nwellnhof
{($_,{.[[R,] |$_]}...{%.{$_}++})-2}
Try it online!
Explanation:
{ } # Anonymous code block
... # Create a sequence where:
$_, # The first element is the input list
{.[[R,] |$_]} # Subsequent elements are the previous element reverse indexed into itself
{ } # Until
%.{$_} # The index of the listt in an anonymous hash is non-zero
++ # Then post increment it
( )-2 # Return the length of the sequence minus 2
Perl 6, 44 35 bytes
-9 bytes thanks to nwellnhof
{($_,{.[[R,] |$_]}...{%.{$_}++})-2}
Try it online!
Explanation:
{ } # Anonymous code block
... # Create a sequence where:
$_, # The first element is the input list
{.[[R,] |$_]} # Subsequent elements are the previous element reverse indexed into itself
{ } # Until
%.{$_} # The index of the listt in an anonymous hash is non-zero
++ # Then post increment it
( )-2 # Return the length of the sequence minus 2
edited 8 hours ago
answered 22 hours ago
Jo King
19.2k244102
19.2k244102
add a comment |
add a comment |
up vote
2
down vote
J, 33 27 26 bytes
-7 thanks to bubbler
_1(+~:i.0:)|.@C.~^:(<@!@#)
Try it online!
how
original explanation. my last improvement only changes the piece which finds "the index of the first element we've seen already". it now uses the "nub sieve" to do it in fewer bytes.
1 <:@i.~ [: ({: e. }:) |.@C.~^:(<@!@#)
|.@C.~ NB. self-apply permutation and reverse
^: NB. this many times:
(<@!@#) NB. the box of the factorial of the
NB. the list len. this guarantees
NB. we terminate, and the box means
NB. we collect all the results
[: ({: e. }:) NB. apply this to those results:
NB. for each prefix
{: e. }: NB. is the last item contained in
NB. the list of previous items?
1 <:@i.~ NB. in that result find:
1 i.~ NB. the index of the first 1
<:@ NB. and subtract 1
Note the entire final phrase 1<:@i.~[:({:e.}:)
is devoted to finding "the index of the first element which has already been seen." This seems awfully long for obtaining that, but I wasn't able to golf it more. Suggestions welcome.
add a comment |
up vote
2
down vote
J, 33 27 26 bytes
-7 thanks to bubbler
_1(+~:i.0:)|.@C.~^:(<@!@#)
Try it online!
how
original explanation. my last improvement only changes the piece which finds "the index of the first element we've seen already". it now uses the "nub sieve" to do it in fewer bytes.
1 <:@i.~ [: ({: e. }:) |.@C.~^:(<@!@#)
|.@C.~ NB. self-apply permutation and reverse
^: NB. this many times:
(<@!@#) NB. the box of the factorial of the
NB. the list len. this guarantees
NB. we terminate, and the box means
NB. we collect all the results
[: ({: e. }:) NB. apply this to those results:
NB. for each prefix
{: e. }: NB. is the last item contained in
NB. the list of previous items?
1 <:@i.~ NB. in that result find:
1 i.~ NB. the index of the first 1
<:@ NB. and subtract 1
Note the entire final phrase 1<:@i.~[:({:e.}:)
is devoted to finding "the index of the first element which has already been seen." This seems awfully long for obtaining that, but I wasn't able to golf it more. Suggestions welcome.
add a comment |
up vote
2
down vote
up vote
2
down vote
J, 33 27 26 bytes
-7 thanks to bubbler
_1(+~:i.0:)|.@C.~^:(<@!@#)
Try it online!
how
original explanation. my last improvement only changes the piece which finds "the index of the first element we've seen already". it now uses the "nub sieve" to do it in fewer bytes.
1 <:@i.~ [: ({: e. }:) |.@C.~^:(<@!@#)
|.@C.~ NB. self-apply permutation and reverse
^: NB. this many times:
(<@!@#) NB. the box of the factorial of the
NB. the list len. this guarantees
NB. we terminate, and the box means
NB. we collect all the results
[: ({: e. }:) NB. apply this to those results:
NB. for each prefix
{: e. }: NB. is the last item contained in
NB. the list of previous items?
1 <:@i.~ NB. in that result find:
1 i.~ NB. the index of the first 1
<:@ NB. and subtract 1
Note the entire final phrase 1<:@i.~[:({:e.}:)
is devoted to finding "the index of the first element which has already been seen." This seems awfully long for obtaining that, but I wasn't able to golf it more. Suggestions welcome.
J, 33 27 26 bytes
-7 thanks to bubbler
_1(+~:i.0:)|.@C.~^:(<@!@#)
Try it online!
how
original explanation. my last improvement only changes the piece which finds "the index of the first element we've seen already". it now uses the "nub sieve" to do it in fewer bytes.
1 <:@i.~ [: ({: e. }:) |.@C.~^:(<@!@#)
|.@C.~ NB. self-apply permutation and reverse
^: NB. this many times:
(<@!@#) NB. the box of the factorial of the
NB. the list len. this guarantees
NB. we terminate, and the box means
NB. we collect all the results
[: ({: e. }:) NB. apply this to those results:
NB. for each prefix
{: e. }: NB. is the last item contained in
NB. the list of previous items?
1 <:@i.~ NB. in that result find:
1 i.~ NB. the index of the first 1
<:@ NB. and subtract 1
Note the entire final phrase 1<:@i.~[:({:e.}:)
is devoted to finding "the index of the first element which has already been seen." This seems awfully long for obtaining that, but I wasn't able to golf it more. Suggestions welcome.
edited 16 hours ago
answered 20 hours ago
Jonah
1,921816
1,921816
add a comment |
add a comment |
up vote
1
down vote
Jelly, 6 bytes
Ṛị$ƬL’
Try it online!
1-indexed.
add a comment |
up vote
1
down vote
Jelly, 6 bytes
Ṛị$ƬL’
Try it online!
1-indexed.
add a comment |
up vote
1
down vote
up vote
1
down vote
Jelly, 6 bytes
Ṛị$ƬL’
Try it online!
1-indexed.
Jelly, 6 bytes
Ṛị$ƬL’
Try it online!
1-indexed.
edited yesterday
answered yesterday
Erik the Outgolfer
30.7k429102
30.7k429102
add a comment |
add a comment |
up vote
1
down vote
Dyalog APL, 29 28 27 bytes
¯2∘+∘≢{⍵,⍨⊂⌽(⍋⍳⊢)⊃⍵}⍣{⍺≢∪⍺}
Takes boxed arrays. Will trainify and explain later.
Try it here as a test suite.
add a comment |
up vote
1
down vote
Dyalog APL, 29 28 27 bytes
¯2∘+∘≢{⍵,⍨⊂⌽(⍋⍳⊢)⊃⍵}⍣{⍺≢∪⍺}
Takes boxed arrays. Will trainify and explain later.
Try it here as a test suite.
add a comment |
up vote
1
down vote
up vote
1
down vote
Dyalog APL, 29 28 27 bytes
¯2∘+∘≢{⍵,⍨⊂⌽(⍋⍳⊢)⊃⍵}⍣{⍺≢∪⍺}
Takes boxed arrays. Will trainify and explain later.
Try it here as a test suite.
Dyalog APL, 29 28 27 bytes
¯2∘+∘≢{⍵,⍨⊂⌽(⍋⍳⊢)⊃⍵}⍣{⍺≢∪⍺}
Takes boxed arrays. Will trainify and explain later.
Try it here as a test suite.
edited 13 hours ago
answered 19 hours ago
lirtosiast
15.4k436104
15.4k436104
add a comment |
add a comment |
up vote
0
down vote
Clean, 90 bytes
import StdEnv
$l=length(until([h:t]=any((==)h)t)([h:t]=[reverse(map((!!)h)h),h:t])[l])-2
Try it online!
add a comment |
up vote
0
down vote
Clean, 90 bytes
import StdEnv
$l=length(until([h:t]=any((==)h)t)([h:t]=[reverse(map((!!)h)h),h:t])[l])-2
Try it online!
add a comment |
up vote
0
down vote
up vote
0
down vote
Clean, 90 bytes
import StdEnv
$l=length(until([h:t]=any((==)h)t)([h:t]=[reverse(map((!!)h)h),h:t])[l])-2
Try it online!
Clean, 90 bytes
import StdEnv
$l=length(until([h:t]=any((==)h)t)([h:t]=[reverse(map((!!)h)h),h:t])[l])-2
Try it online!
answered 22 hours ago
Οurous
5,77311031
5,77311031
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f176196%2fnumber-of-transformations-until-repeat%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I meant more like a "closed formula" such as $f(a_{0},a_{1},a_{...}} = a_{0}^a_{1}+...$ where $a_{i}$ is the i-th element in the given sequence.
– mroman
yesterday
Are you sure such a "closed formula" exists?
– Todd Sewell
20 hours ago
"returns (or outputs) the number of steps until a permutation occurs that has already occured." This is inconsistent with just about everything that follows it. For a start, it makes a return value of 0 impossible...
– Peter Taylor
6 hours ago
Is the 3rd example correct? I see
3,0,1,2
should transform to2,3,0,1
– FireCubez
3 hours ago
It's the number of transformations before a repeat.
– mroman
26 mins ago