Number of transformations until repeat
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
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
Nov 18 '18 at 20:04
Are you sure such a "closed formula" exists?
– Todd Sewell
Nov 18 '18 at 23:54
"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
Nov 19 '18 at 14:24
Is the 3rd example correct? I see3,0,1,2
should transform to2,3,0,1
– FireCubez
Nov 19 '18 at 17:02
It's the number of transformations before a repeat.
– mroman
Nov 19 '18 at 20:14
|
show 1 more comment
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 Nov 18 '18 at 20:11
asked Nov 18 '18 at 19:38
mroman
1,102613
1,102613
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
Nov 18 '18 at 20:04
Are you sure such a "closed formula" exists?
– Todd Sewell
Nov 18 '18 at 23:54
"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
Nov 19 '18 at 14:24
Is the 3rd example correct? I see3,0,1,2
should transform to2,3,0,1
– FireCubez
Nov 19 '18 at 17:02
It's the number of transformations before a repeat.
– mroman
Nov 19 '18 at 20:14
|
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
Nov 18 '18 at 20:04
Are you sure such a "closed formula" exists?
– Todd Sewell
Nov 18 '18 at 23:54
"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
Nov 19 '18 at 14:24
Is the 3rd example correct? I see3,0,1,2
should transform to2,3,0,1
– FireCubez
Nov 19 '18 at 17:02
It's the number of transformations before a repeat.
– mroman
Nov 19 '18 at 20:14
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
Nov 18 '18 at 20:04
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
Nov 18 '18 at 20:04
Are you sure such a "closed formula" exists?
– Todd Sewell
Nov 18 '18 at 23:54
Are you sure such a "closed formula" exists?
– Todd Sewell
Nov 18 '18 at 23:54
"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
Nov 19 '18 at 14:24
"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
Nov 19 '18 at 14:24
Is the 3rd example correct? I see
3,0,1,2
should transform to 2,3,0,1
– FireCubez
Nov 19 '18 at 17:02
Is the 3rd example correct? I see
3,0,1,2
should transform to 2,3,0,1
– FireCubez
Nov 19 '18 at 17:02
It's the number of transformations before a repeat.
– mroman
Nov 19 '18 at 20:14
It's the number of transformations before a repeat.
– mroman
Nov 19 '18 at 20:14
|
show 1 more comment
9 Answers
9
active
oldest
votes
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
Nov 18 '18 at 20:24
A function is an object. So,g[a]
can be used on it to access the propertya
.
– Arnauld
Nov 18 '18 at 20:27
Ah I see. You're usingg
to store the state in.
– mroman
Nov 18 '18 at 20:29
add a comment |
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 |
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 |
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 |
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 |
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 |
Jelly, 6 bytes
Ṛị$ƬL’
Try it online!
1-indexed.
add a comment |
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 |
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Nov 18 '18 at 20:24
A function is an object. So,g[a]
can be used on it to access the propertya
.
– Arnauld
Nov 18 '18 at 20:27
Ah I see. You're usingg
to store the state in.
– mroman
Nov 18 '18 at 20:29
add a comment |
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
Nov 18 '18 at 20:24
A function is an object. So,g[a]
can be used on it to access the propertya
.
– Arnauld
Nov 18 '18 at 20:27
Ah I see. You're usingg
to store the state in.
– mroman
Nov 18 '18 at 20:29
add a comment |
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 Nov 18 '18 at 20:19
Arnauld
72.4k689305
72.4k689305
What doesdo on a function?
– mroman
Nov 18 '18 at 20:24
A function is an object. So,g[a]
can be used on it to access the propertya
.
– Arnauld
Nov 18 '18 at 20:27
Ah I see. You're usingg
to store the state in.
– mroman
Nov 18 '18 at 20:29
add a comment |
What doesdo on a function?
– mroman
Nov 18 '18 at 20:24
A function is an object. So,g[a]
can be used on it to access the propertya
.
– Arnauld
Nov 18 '18 at 20:27
Ah I see. You're usingg
to store the state in.
– mroman
Nov 18 '18 at 20:29
What does
do on a function?– mroman
Nov 18 '18 at 20:24
What does
do on a function?– mroman
Nov 18 '18 at 20:24
A function is an object. So,
g[a]
can be used on it to access the property a
.– Arnauld
Nov 18 '18 at 20:27
A function is an object. So,
g[a]
can be used on it to access the property a
.– Arnauld
Nov 18 '18 at 20:27
Ah I see. You're using
g
to store the state in.– mroman
Nov 18 '18 at 20:29
Ah I see. You're using
g
to store the state in.– mroman
Nov 18 '18 at 20:29
add a comment |
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 |
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 |
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 Nov 18 '18 at 20:37
Erik the Outgolfer
31.4k429103
31.4k429103
add a comment |
add a comment |
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 |
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 |
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 Nov 18 '18 at 20:46
answered Nov 18 '18 at 20:38
lirtosiast
15.7k436107
15.7k436107
add a comment |
add a comment |
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 |
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 |
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 Nov 18 '18 at 23:09
answered Nov 18 '18 at 22:33
nimi
31.3k32085
31.3k32085
add a comment |
add a comment |
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 |
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 |
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 Nov 19 '18 at 12:18
answered Nov 18 '18 at 22:02
Jo King
20.7k247109
20.7k247109
add a comment |
add a comment |
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 |
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 |
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 Nov 19 '18 at 4:34
answered Nov 19 '18 at 0:25
Jonah
2,011816
2,011816
add a comment |
add a comment |
Jelly, 6 bytes
Ṛị$ƬL’
Try it online!
1-indexed.
add a comment |
Jelly, 6 bytes
Ṛị$ƬL’
Try it online!
1-indexed.
add a comment |
Jelly, 6 bytes
Ṛị$ƬL’
Try it online!
1-indexed.
Jelly, 6 bytes
Ṛị$ƬL’
Try it online!
1-indexed.
edited Nov 18 '18 at 20:10
answered Nov 18 '18 at 20:04
Erik the Outgolfer
31.4k429103
31.4k429103
add a comment |
add a comment |
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 |
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 |
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 Nov 19 '18 at 6:58
answered Nov 19 '18 at 1:38
lirtosiast
15.7k436107
15.7k436107
add a comment |
add a comment |
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 |
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 |
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 Nov 18 '18 at 22:20
Οurous
6,44311033
6,44311033
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Nov 18 '18 at 20:04
Are you sure such a "closed formula" exists?
– Todd Sewell
Nov 18 '18 at 23:54
"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
Nov 19 '18 at 14:24
Is the 3rd example correct? I see
3,0,1,2
should transform to2,3,0,1
– FireCubez
Nov 19 '18 at 17:02
It's the number of transformations before a repeat.
– mroman
Nov 19 '18 at 20:14