Skip to main content
added 3 characters in body
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139

The algorithm might make more sense with a sample implementation to look at (here, in Python):

S = raw_input()
L = len(S)
T = 0
for i in range(L/2):
  A = ord(S[i])
  B = ord(S[L-1-i])
  T += abs(A-B)
print T

Let's review the problem description. Each character decrement (e.g. --'b' == 'a') counts as a single operation. The problem is to determine the number of operations required to transform the input string into a palindrome.

The algorithm iterates over half the string, and compares the ordinal value of the character at the current index with that of the character across from it (in its mirrored position). If they are not the same, the one that is larger needs to be decremented, but it doesn't actually need to be checked which is which. Taking the absolute value of the difference will produce the correct number of operations needed regardless.

I think a source of confusion might be the wording, "Let's assume that one such set of indices can be denoted by A' and B'." I believe that the author intended A' and B' to refer to the characters at each of these indices, and not the indices themselves. If they had actually meant to refer to the indices, the algorithm would return the same value for any given string length, which is obviously incorrect.

A golfed version might look like this:

s=map(ord,raw_input())
print sum(abs(s[i]-s[~i])for i in range(len(s)/2))

I suspect Ruby might be a bit shorter, because indexing a string returns the ordinal value, rather than the individual character.

The algorithm might make more sense with a sample implementation to look at (here, in Python):

S = raw_input()
L = len(S)
T = 0
for i in range(L/2):
  A = ord(S[i])
  B = ord(S[L-1-i])
  T += abs(A-B)
print T

Let's review the problem description. Each character decrement (e.g. --'b' == 'a') counts as a single operation. The problem is determine the number of operations required to transform the input string into a palindrome.

The algorithm iterates over half the string, and compares the ordinal value of the character at the current index with that of the character across from it (in its mirrored position). If they are not the same, the one that is larger needs to be decremented, but it doesn't actually need to be checked which is which. Taking the absolute value of the difference will produce the correct number of operations needed regardless.

I think a source of confusion might be the wording, "Let's assume that one such set of indices can be denoted by A' and B'." I believe that the author intended A' and B' to refer to the characters at each of these indices, and not the indices themselves. If they had actually meant to refer to the indices, the algorithm would return the same value for any given string length, which is obviously incorrect.

A golfed version might look like this:

s=map(ord,raw_input())
print sum(abs(s[i]-s[~i])for i in range(len(s)/2))

I suspect Ruby might be a bit shorter, because indexing a string returns the ordinal value, rather than the individual character.

The algorithm might make more sense with a sample implementation to look at (here, in Python):

S = raw_input()
L = len(S)
T = 0
for i in range(L/2):
  A = ord(S[i])
  B = ord(S[L-1-i])
  T += abs(A-B)
print T

Let's review the problem description. Each character decrement (e.g. --'b' == 'a') counts as a single operation. The problem is to determine the number of operations required to transform the input string into a palindrome.

The algorithm iterates over half the string, and compares the ordinal value of the character at the current index with that of the character across from it (in its mirrored position). If they are not the same, the one that is larger needs to be decremented, but it doesn't actually need to be checked which is which. Taking the absolute value of the difference will produce the correct number of operations needed regardless.

I think a source of confusion might be the wording, "Let's assume that one such set of indices can be denoted by A' and B'." I believe that the author intended A' and B' to refer to the characters at each of these indices, and not the indices themselves. If they had actually meant to refer to the indices, the algorithm would return the same value for any given string length, which is obviously incorrect.

A golfed version might look like this:

s=map(ord,raw_input())
print sum(abs(s[i]-s[~i])for i in range(len(s)/2))

I suspect Ruby might be a bit shorter, because indexing a string returns the ordinal value, rather than the individual character.

added 186 characters in body
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139

The algorithm might make more sense with a sample implementation to look at (here, in Python):

S = raw_input()
L = len(S)
T = 0
for i in range(L/2):
  A = ord(S[i])
  B = ord(S[L-1-i])
  T += abs(A-B)
print T

Let's review the problem description. Each character decrement (e.g. --'b' == 'a') counts as a single operation. The problem is determine the number of operations required to transform the input string into a palindrome.

The algorithm iterates over half the string, and compares the ordinal value of the character at the current index with that of the character across from it (in its mirrored position). If they are not the same, the one that is larger needs to be decremented, but it doesn't actually need to be checked which is which. Taking the absolute value of the difference will produce the correct number of operations needed regardless.

InI think a source of confusion might be the above implementationwording, if"Let's assume that one such set of indices can be denoted by A' and B'." I believe that the string is odd in length,author intended A' and B' to refer to the center most character ischaracters at each of these indices, and not checkedthe indices themselves. It doesn't matter if it is or not thoughIf they had actually meant to refer to the indices, because the number of operations required to 'become itself' will be zeroalgorithm would return the same value for any given string length, which is obviously incorrect.

A golfed version might look like this:

s=map(ord,raw_input())
print sum(abs(s[i]-s[~i])for i in range(len(s)/2))

I suspect Ruby might be a bit shorter, because indexing a string returns the ordinal value, rather than the individual character.

The algorithm might make more sense with a sample implementation to look at (here, in Python):

S = raw_input()
L = len(S)
T = 0
for i in range(L/2):
  A = ord(S[i])
  B = ord(S[L-1-i])
  T += abs(A-B)
print T

Let's review the problem description. Each character decrement (e.g. --'b' == 'a') counts as a single operation. The problem is determine the number of operations required to transform the input string into a palindrome.

The algorithm iterates over half the string, and compares the ordinal value of the character at the current index with that of the character across from it (in its mirrored position). If they are not the same, the one that is larger needs to be decremented, but it doesn't actually need to be checked which is which. Taking the absolute value of the difference will produce the correct number of operations needed regardless.

In the above implementation, if the string is odd in length, the center most character is not checked. It doesn't matter if it is or not though, because the number of operations required to 'become itself' will be zero.

A golfed version might look like this:

s=map(ord,raw_input())
print sum(abs(s[i]-s[~i])for i in range(len(s)/2))

I suspect Ruby might be a bit shorter, because indexing a string returns the ordinal value, rather than the individual character.

The algorithm might make more sense with a sample implementation to look at (here, in Python):

S = raw_input()
L = len(S)
T = 0
for i in range(L/2):
  A = ord(S[i])
  B = ord(S[L-1-i])
  T += abs(A-B)
print T

Let's review the problem description. Each character decrement (e.g. --'b' == 'a') counts as a single operation. The problem is determine the number of operations required to transform the input string into a palindrome.

The algorithm iterates over half the string, and compares the ordinal value of the character at the current index with that of the character across from it (in its mirrored position). If they are not the same, the one that is larger needs to be decremented, but it doesn't actually need to be checked which is which. Taking the absolute value of the difference will produce the correct number of operations needed regardless.

I think a source of confusion might be the wording, "Let's assume that one such set of indices can be denoted by A' and B'." I believe that the author intended A' and B' to refer to the characters at each of these indices, and not the indices themselves. If they had actually meant to refer to the indices, the algorithm would return the same value for any given string length, which is obviously incorrect.

A golfed version might look like this:

s=map(ord,raw_input())
print sum(abs(s[i]-s[~i])for i in range(len(s)/2))

I suspect Ruby might be a bit shorter, because indexing a string returns the ordinal value, rather than the individual character.

added 15 characters in body
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139

The algorithm might make more sense with a sample implementation to look at (here, in Python):

sS = raw_input()
slenL = len(sS)
tT = 0
for i in range(slenL/2):
  tA +== abs(ord(s[i]S[i]) 
 - B = ord(s[slenS[L-i1-1]i])
  T += abs(A-B)
print tT

Let's review the problem description. Each character decrement (e.g. --'b' == 'a') counts as a single operation. The problem is determine the number of operations required to transform the input string into a palindrome.

The algorithm iterates over half the string, and compares the ordinal value of the character at the current index with that of the character across from it (in its mirrored position). If they are not the same, the one that is larger needs to be decremented, but it doesn't actually need to be checked which is which. Taking the absolute value of the difference will produce the correct number of operations needed regardless.

In the above implementation, if the string is odd in length, the center most character is not checked. It doesn't matter if it is or not though, because the number of operations required to 'become itself' will be zero.

A golfed version might look like this:

s=map(ord,raw_input())
print sum(abs(s[i]-s[~i])for i in range(len(s)/2))

I suspect Ruby might be a bit shorter, because indexing a string returns the ordinal value, rather than the individual character.

The algorithm might make more sense with a sample implementation to look at (here, in Python):

s = raw_input()
slen = len(s)
t = 0
for i in range(slen/2):
  t += abs(ord(s[i]) - ord(s[slen-i-1]))
print t

Let's review the problem description. Each character decrement (e.g. --'b' == 'a') counts as a single operation. The problem is determine the number of operations required to transform the input string into a palindrome.

The algorithm iterates over half the string, and compares the ordinal value of the character at the current index with that of the character across from it (in its mirrored position). If they are not the same, the one that is larger needs to be decremented, but it doesn't actually need to be checked which is which. Taking the absolute value of the difference will produce the correct number of operations needed regardless.

In the above implementation, if the string is odd in length, the center most character is not checked. It doesn't matter if it is or not though, because the number of operations required to 'become itself' will be zero.

A golfed version might look like this:

s=map(ord,raw_input())
print sum(abs(s[i]-s[~i])for i in range(len(s)/2))

I suspect Ruby might be a bit shorter, because indexing a string returns the ordinal value, rather than the individual character.

The algorithm might make more sense with a sample implementation to look at (here, in Python):

S = raw_input()
L = len(S)
T = 0
for i in range(L/2):
  A = ord(S[i]) 
  B = ord(S[L-1-i])
  T += abs(A-B)
print T

Let's review the problem description. Each character decrement (e.g. --'b' == 'a') counts as a single operation. The problem is determine the number of operations required to transform the input string into a palindrome.

The algorithm iterates over half the string, and compares the ordinal value of the character at the current index with that of the character across from it (in its mirrored position). If they are not the same, the one that is larger needs to be decremented, but it doesn't actually need to be checked which is which. Taking the absolute value of the difference will produce the correct number of operations needed regardless.

In the above implementation, if the string is odd in length, the center most character is not checked. It doesn't matter if it is or not though, because the number of operations required to 'become itself' will be zero.

A golfed version might look like this:

s=map(ord,raw_input())
print sum(abs(s[i]-s[~i])for i in range(len(s)/2))

I suspect Ruby might be a bit shorter, because indexing a string returns the ordinal value, rather than the individual character.

Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
Loading
Post Made Community Wiki by primo