Optimal Alphabet Stepping











up vote
6
down vote

favorite
1












Given an input string consisting of only letters, return the step-size that results in the minimum amount of steps that are needed to visit all the letters in order over a wrapping alphabet, starting at any letter.



For example, take the word, dog. If we use a step-size of 1, we end up with:



defghijklmnopqrstuvwxyzabcdefg   Alphabet
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
defghijklmnopqrstuvwxyzabcdefg Visited letters
d o g Needed letters


For a total of 30 steps.



However, if we use a step-size of 11, we get:



defghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg
^ ^ ^ ^ ^ ^
d o z k v g Visited letters
d o g Needed letters


For a total of 6 steps. This is the minimum amount of steps, so the return result for dog is the step-size; 11.



Test cases:



"dog"      -> 11
"age" -> 6
"apple" -> 19
"alphabet" -> 9
"aaaaaaa" -> 0 for 0 indexed, 26 for 1 indexed
"abcdefga" -> 1 or 9
"aba" -> Any odd number except for 13
"ppcg" -> 15
"codegolf" -> 15
"testcase" -> 9
"z" -> Any number
"joking" -> 19


Rules




  • Input will be a non-empty string consisting only of the letters a to z (you can choose between uppercase or lowercase)

  • Output can be 0 indexed (i.e. the range 0-25) or 1 indexed (1-26)

  • If there's a tie, you can output any step-size or all of them

  • This is code-golf, so the lowest amount of bytes for each language wins!










share|improve this question
























  • Do we need to handle empty input?
    – pizzapants184
    2 hours ago






  • 1




    @pizzapants184 No. I've updated the question to specify the input will be non-empty
    – Jo King
    2 hours ago








  • 1




    @pizzapants184 Thanks, updating the question. Please let me know if any more of my test cases are wrong. I think that was left over from a previous iteration of the question
    – Jo King
    1 hour ago










  • Can we take input as an array of characters?
    – Shaggy
    12 mins ago















up vote
6
down vote

favorite
1












Given an input string consisting of only letters, return the step-size that results in the minimum amount of steps that are needed to visit all the letters in order over a wrapping alphabet, starting at any letter.



For example, take the word, dog. If we use a step-size of 1, we end up with:



defghijklmnopqrstuvwxyzabcdefg   Alphabet
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
defghijklmnopqrstuvwxyzabcdefg Visited letters
d o g Needed letters


For a total of 30 steps.



However, if we use a step-size of 11, we get:



defghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg
^ ^ ^ ^ ^ ^
d o z k v g Visited letters
d o g Needed letters


For a total of 6 steps. This is the minimum amount of steps, so the return result for dog is the step-size; 11.



Test cases:



"dog"      -> 11
"age" -> 6
"apple" -> 19
"alphabet" -> 9
"aaaaaaa" -> 0 for 0 indexed, 26 for 1 indexed
"abcdefga" -> 1 or 9
"aba" -> Any odd number except for 13
"ppcg" -> 15
"codegolf" -> 15
"testcase" -> 9
"z" -> Any number
"joking" -> 19


Rules




  • Input will be a non-empty string consisting only of the letters a to z (you can choose between uppercase or lowercase)

  • Output can be 0 indexed (i.e. the range 0-25) or 1 indexed (1-26)

  • If there's a tie, you can output any step-size or all of them

  • This is code-golf, so the lowest amount of bytes for each language wins!










share|improve this question
























  • Do we need to handle empty input?
    – pizzapants184
    2 hours ago






  • 1




    @pizzapants184 No. I've updated the question to specify the input will be non-empty
    – Jo King
    2 hours ago








  • 1




    @pizzapants184 Thanks, updating the question. Please let me know if any more of my test cases are wrong. I think that was left over from a previous iteration of the question
    – Jo King
    1 hour ago










  • Can we take input as an array of characters?
    – Shaggy
    12 mins ago













up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





Given an input string consisting of only letters, return the step-size that results in the minimum amount of steps that are needed to visit all the letters in order over a wrapping alphabet, starting at any letter.



For example, take the word, dog. If we use a step-size of 1, we end up with:



defghijklmnopqrstuvwxyzabcdefg   Alphabet
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
defghijklmnopqrstuvwxyzabcdefg Visited letters
d o g Needed letters


For a total of 30 steps.



However, if we use a step-size of 11, we get:



defghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg
^ ^ ^ ^ ^ ^
d o z k v g Visited letters
d o g Needed letters


For a total of 6 steps. This is the minimum amount of steps, so the return result for dog is the step-size; 11.



Test cases:



"dog"      -> 11
"age" -> 6
"apple" -> 19
"alphabet" -> 9
"aaaaaaa" -> 0 for 0 indexed, 26 for 1 indexed
"abcdefga" -> 1 or 9
"aba" -> Any odd number except for 13
"ppcg" -> 15
"codegolf" -> 15
"testcase" -> 9
"z" -> Any number
"joking" -> 19


Rules




  • Input will be a non-empty string consisting only of the letters a to z (you can choose between uppercase or lowercase)

  • Output can be 0 indexed (i.e. the range 0-25) or 1 indexed (1-26)

  • If there's a tie, you can output any step-size or all of them

  • This is code-golf, so the lowest amount of bytes for each language wins!










share|improve this question















Given an input string consisting of only letters, return the step-size that results in the minimum amount of steps that are needed to visit all the letters in order over a wrapping alphabet, starting at any letter.



For example, take the word, dog. If we use a step-size of 1, we end up with:



defghijklmnopqrstuvwxyzabcdefg   Alphabet
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
defghijklmnopqrstuvwxyzabcdefg Visited letters
d o g Needed letters


For a total of 30 steps.



However, if we use a step-size of 11, we get:



defghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg
^ ^ ^ ^ ^ ^
d o z k v g Visited letters
d o g Needed letters


For a total of 6 steps. This is the minimum amount of steps, so the return result for dog is the step-size; 11.



Test cases:



"dog"      -> 11
"age" -> 6
"apple" -> 19
"alphabet" -> 9
"aaaaaaa" -> 0 for 0 indexed, 26 for 1 indexed
"abcdefga" -> 1 or 9
"aba" -> Any odd number except for 13
"ppcg" -> 15
"codegolf" -> 15
"testcase" -> 9
"z" -> Any number
"joking" -> 19


Rules




  • Input will be a non-empty string consisting only of the letters a to z (you can choose between uppercase or lowercase)

  • Output can be 0 indexed (i.e. the range 0-25) or 1 indexed (1-26)

  • If there's a tie, you can output any step-size or all of them

  • This is code-golf, so the lowest amount of bytes for each language wins!







code-golf alphabet






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked 3 hours ago









Jo King

20.2k245106




20.2k245106












  • Do we need to handle empty input?
    – pizzapants184
    2 hours ago






  • 1




    @pizzapants184 No. I've updated the question to specify the input will be non-empty
    – Jo King
    2 hours ago








  • 1




    @pizzapants184 Thanks, updating the question. Please let me know if any more of my test cases are wrong. I think that was left over from a previous iteration of the question
    – Jo King
    1 hour ago










  • Can we take input as an array of characters?
    – Shaggy
    12 mins ago


















  • Do we need to handle empty input?
    – pizzapants184
    2 hours ago






  • 1




    @pizzapants184 No. I've updated the question to specify the input will be non-empty
    – Jo King
    2 hours ago








  • 1




    @pizzapants184 Thanks, updating the question. Please let me know if any more of my test cases are wrong. I think that was left over from a previous iteration of the question
    – Jo King
    1 hour ago










  • Can we take input as an array of characters?
    – Shaggy
    12 mins ago
















Do we need to handle empty input?
– pizzapants184
2 hours ago




Do we need to handle empty input?
– pizzapants184
2 hours ago




1




1




@pizzapants184 No. I've updated the question to specify the input will be non-empty
– Jo King
2 hours ago






@pizzapants184 No. I've updated the question to specify the input will be non-empty
– Jo King
2 hours ago






1




1




@pizzapants184 Thanks, updating the question. Please let me know if any more of my test cases are wrong. I think that was left over from a previous iteration of the question
– Jo King
1 hour ago




@pizzapants184 Thanks, updating the question. Please let me know if any more of my test cases are wrong. I think that was left over from a previous iteration of the question
– Jo King
1 hour ago












Can we take input as an array of characters?
– Shaggy
12 mins ago




Can we take input as an array of characters?
– Shaggy
12 mins ago










2 Answers
2






active

oldest

votes

















up vote
1
down vote














Python 2, 222 bytes





A=map(chr,range(65,91))
def t(s,l,S=0):
a=i=A.index(s[0])
for c in s[1:]:
while a!=A.index(c):
S+=1;a+=l;a%=26
if a==i:return float('inf')
i=a
return S
def f(s):T=[t(s,l)for l in range(26)];return T.index(min(T))


Try it online!



-8 bytes from Jo King



This would be shorter in a language with a prime number of letters (wouldn't need the float('inf') handling of infinite loops). Actually, this submission would still need that for handling strings like "aaa".



This submission is 0-indexed (returns values from 0 to 25 inclusive).



f takes a(n uppercase) string and returns the Optimal Alphabet Stepping



t is a helper function that takes the string and an alphabet stepping and returns the number of hops needed to finish the string (or float('inf') if impossible).






share|improve this answer



















  • 1




    216 bytes
    – Jo King
    1 hour ago












  • Use while a!=A(c)and S<len(s)*26: and you may remove if a==i:return float('inf'), since len(s)*26 is upper bound of any answer.
    – tsh
    26 mins ago




















up vote
1
down vote













JavaScript, 153 bytes





w=>[...Array(26)].map((_,i)=>i).map((_,s,a)=>~[...w].map(c=>(t+=a.find(v=>!p|(u(c,36)+~v*s-u(p,36))%26==0),p=c),p=t=0,u=parseInt)+t<m&&(m=t,n=s),m=1/0)|n


Try it online!






share|improve this answer





















  • 144 bytes
    – Shaggy
    11 mins ago











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f177404%2foptimal-alphabet-stepping%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote














Python 2, 222 bytes





A=map(chr,range(65,91))
def t(s,l,S=0):
a=i=A.index(s[0])
for c in s[1:]:
while a!=A.index(c):
S+=1;a+=l;a%=26
if a==i:return float('inf')
i=a
return S
def f(s):T=[t(s,l)for l in range(26)];return T.index(min(T))


Try it online!



-8 bytes from Jo King



This would be shorter in a language with a prime number of letters (wouldn't need the float('inf') handling of infinite loops). Actually, this submission would still need that for handling strings like "aaa".



This submission is 0-indexed (returns values from 0 to 25 inclusive).



f takes a(n uppercase) string and returns the Optimal Alphabet Stepping



t is a helper function that takes the string and an alphabet stepping and returns the number of hops needed to finish the string (or float('inf') if impossible).






share|improve this answer



















  • 1




    216 bytes
    – Jo King
    1 hour ago












  • Use while a!=A(c)and S<len(s)*26: and you may remove if a==i:return float('inf'), since len(s)*26 is upper bound of any answer.
    – tsh
    26 mins ago

















up vote
1
down vote














Python 2, 222 bytes





A=map(chr,range(65,91))
def t(s,l,S=0):
a=i=A.index(s[0])
for c in s[1:]:
while a!=A.index(c):
S+=1;a+=l;a%=26
if a==i:return float('inf')
i=a
return S
def f(s):T=[t(s,l)for l in range(26)];return T.index(min(T))


Try it online!



-8 bytes from Jo King



This would be shorter in a language with a prime number of letters (wouldn't need the float('inf') handling of infinite loops). Actually, this submission would still need that for handling strings like "aaa".



This submission is 0-indexed (returns values from 0 to 25 inclusive).



f takes a(n uppercase) string and returns the Optimal Alphabet Stepping



t is a helper function that takes the string and an alphabet stepping and returns the number of hops needed to finish the string (or float('inf') if impossible).






share|improve this answer



















  • 1




    216 bytes
    – Jo King
    1 hour ago












  • Use while a!=A(c)and S<len(s)*26: and you may remove if a==i:return float('inf'), since len(s)*26 is upper bound of any answer.
    – tsh
    26 mins ago















up vote
1
down vote










up vote
1
down vote










Python 2, 222 bytes





A=map(chr,range(65,91))
def t(s,l,S=0):
a=i=A.index(s[0])
for c in s[1:]:
while a!=A.index(c):
S+=1;a+=l;a%=26
if a==i:return float('inf')
i=a
return S
def f(s):T=[t(s,l)for l in range(26)];return T.index(min(T))


Try it online!



-8 bytes from Jo King



This would be shorter in a language with a prime number of letters (wouldn't need the float('inf') handling of infinite loops). Actually, this submission would still need that for handling strings like "aaa".



This submission is 0-indexed (returns values from 0 to 25 inclusive).



f takes a(n uppercase) string and returns the Optimal Alphabet Stepping



t is a helper function that takes the string and an alphabet stepping and returns the number of hops needed to finish the string (or float('inf') if impossible).






share|improve this answer















Python 2, 222 bytes





A=map(chr,range(65,91))
def t(s,l,S=0):
a=i=A.index(s[0])
for c in s[1:]:
while a!=A.index(c):
S+=1;a+=l;a%=26
if a==i:return float('inf')
i=a
return S
def f(s):T=[t(s,l)for l in range(26)];return T.index(min(T))


Try it online!



-8 bytes from Jo King



This would be shorter in a language with a prime number of letters (wouldn't need the float('inf') handling of infinite loops). Actually, this submission would still need that for handling strings like "aaa".



This submission is 0-indexed (returns values from 0 to 25 inclusive).



f takes a(n uppercase) string and returns the Optimal Alphabet Stepping



t is a helper function that takes the string and an alphabet stepping and returns the number of hops needed to finish the string (or float('inf') if impossible).







share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 1 hour ago









pizzapants184

2,594716




2,594716








  • 1




    216 bytes
    – Jo King
    1 hour ago












  • Use while a!=A(c)and S<len(s)*26: and you may remove if a==i:return float('inf'), since len(s)*26 is upper bound of any answer.
    – tsh
    26 mins ago
















  • 1




    216 bytes
    – Jo King
    1 hour ago












  • Use while a!=A(c)and S<len(s)*26: and you may remove if a==i:return float('inf'), since len(s)*26 is upper bound of any answer.
    – tsh
    26 mins ago










1




1




216 bytes
– Jo King
1 hour ago






216 bytes
– Jo King
1 hour ago














Use while a!=A(c)and S<len(s)*26: and you may remove if a==i:return float('inf'), since len(s)*26 is upper bound of any answer.
– tsh
26 mins ago






Use while a!=A(c)and S<len(s)*26: and you may remove if a==i:return float('inf'), since len(s)*26 is upper bound of any answer.
– tsh
26 mins ago












up vote
1
down vote













JavaScript, 153 bytes





w=>[...Array(26)].map((_,i)=>i).map((_,s,a)=>~[...w].map(c=>(t+=a.find(v=>!p|(u(c,36)+~v*s-u(p,36))%26==0),p=c),p=t=0,u=parseInt)+t<m&&(m=t,n=s),m=1/0)|n


Try it online!






share|improve this answer





















  • 144 bytes
    – Shaggy
    11 mins ago















up vote
1
down vote













JavaScript, 153 bytes





w=>[...Array(26)].map((_,i)=>i).map((_,s,a)=>~[...w].map(c=>(t+=a.find(v=>!p|(u(c,36)+~v*s-u(p,36))%26==0),p=c),p=t=0,u=parseInt)+t<m&&(m=t,n=s),m=1/0)|n


Try it online!






share|improve this answer





















  • 144 bytes
    – Shaggy
    11 mins ago













up vote
1
down vote










up vote
1
down vote









JavaScript, 153 bytes





w=>[...Array(26)].map((_,i)=>i).map((_,s,a)=>~[...w].map(c=>(t+=a.find(v=>!p|(u(c,36)+~v*s-u(p,36))%26==0),p=c),p=t=0,u=parseInt)+t<m&&(m=t,n=s),m=1/0)|n


Try it online!






share|improve this answer












JavaScript, 153 bytes





w=>[...Array(26)].map((_,i)=>i).map((_,s,a)=>~[...w].map(c=>(t+=a.find(v=>!p|(u(c,36)+~v*s-u(p,36))%26==0),p=c),p=t=0,u=parseInt)+t<m&&(m=t,n=s),m=1/0)|n


Try it online!







share|improve this answer












share|improve this answer



share|improve this answer










answered 41 mins ago









tsh

8,15511546




8,15511546












  • 144 bytes
    – Shaggy
    11 mins ago


















  • 144 bytes
    – Shaggy
    11 mins ago
















144 bytes
– Shaggy
11 mins ago




144 bytes
– Shaggy
11 mins ago


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f177404%2foptimal-alphabet-stepping%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

數位音樂下載

格利澤436b

When can things happen in Etherscan, such as the picture below?