10
2
This is a fairly simple code golf challenge. Your program, given an ASCII string, is to parse that string into two strings, which it will evaluate. If the second string is "later" than the first one, it will return a 1, if it is "earlier" than the first one, it will return a -1, and if they are the same, it will return 0. To clarify what "later" and "earlier" mean, let's take a look at ASCII character codes. You need to compare each character of the string, treating each of them as digits of a number. Later refers to a larger number, occurring after a smaller number. Strings will be formatted with a hyphen character to separate the two input groups.
Take a look at this example:
7-9
as an input should return1
.
7
converts to ASCII code55
, and9
converts to ASCII code57
.As
57
occurs numerically after55
,9
is later than7
.
Another example:
LKzb-LKaj
as an input should return-1
The ASCII code sequences for this are
76-75-122-98
and76-75-97-106
This is a code golf challenge, and byte count is how entries will be scored.
Any input from the 95 printable ASCII characters is accepted, excluding spaces, and hyphens for anything but separating the input. In addition, strings are not guaranteed to be the same length.
Good luck!
EDIT: To be more clear, each character is to be treated like a digit in a number. In the example LKzb-LKaj
, though j
is later than b
, z
is later than a
, and since it is a more significant digit, it takes precedence. A string supplied will always be at minimum 3 characters, eliminating empty strings from the scope of this problem.
EDIT: Here are some more test cases, for your help:
A-9
->-1
11-Z
->-1
3h~J*-3h~J*
->0
Xv-Y0
->1
1Are the two strings guaranteed to be the same length? – es1024 – 2015-07-30T19:01:21.647
No, they are not. A reasonable question. Will edit the question. – Sam Weaver – 2015-07-30T19:01:57.203
Are the numbers you're comparing in the second example
767512298
and767597106
, or are you doing character-by-character comparisons? If the former, shouldn't the second be "later" and return1
? If the latter, this looks a lot like a regularcompareTo()
function. – Geobits – 2015-07-30T19:04:17.540The conversion is a bit confusing. If I understand correctly, are you essentially stringing together the character value of a string into one bigger integer? So "ABC" becomes the integer 979899? This would just mean to compare the two numbers then with a simple
a>b
. – Jwosty – 2015-07-30T19:05:46.950You do not string together the character values, you treat them as one big base-127 (or however many character codes there are) number. The base isn't particularly important, you are doing character by character comparisons. – Sam Weaver – 2015-07-30T19:08:12.970
Can I create a function? – kirbyfan64sos – 2015-07-30T22:00:40.677
5Test case
11-Z
->-1
makes no sense given the current wording of the question.Z
(90) is greater than1
(49) and is the most significant letter. Please clarify how strings of different lengths are compared. – George Reith – 2015-07-30T22:40:05.837The most significant, in this case, refers to the letter with the most value. In binary, decimal, and most number systems, the most significant digit is the left most digit. – Sam Weaver – 2015-07-31T00:41:08.363
Yes @kirbyfan64sos, you can use a function. – Sam Weaver – 2015-07-31T00:41:28.013
2And what about
A-AA
? – None – 2015-07-31T04:32:12.3672@SamWeaver I know leftmost is the most significant digit hence my confusion as to why
11>Z
in your examples when1<Z
. There must be some undefined behaviour to do with strings of differing lengths or the example is wrong. – George Reith – 2015-07-31T08:23:16.1003As previously explained: each string is to be treated as a digit in a base-127 number. If you were to count in this system, you would begin with a character, increment it up to the barrier of printable characters,
~
at 126, then would increment the next digit by one, returning the initial digit to!
. Each increase in the most significant digit is equivalent to increment the second-most-significant digit by 127. – Sam Weaver – 2015-07-31T13:15:58.2931So any string of length n is later than all strings of length less than n? – trichoplax – 2015-07-31T14:46:37.417
Correct. Excellent observation. – Sam Weaver – 2015-07-31T14:50:55.043