x86 machine code, 46 bytes
Hexdump:
57 53 33 c0 33 ff f6 01 0f 75 15 6a 0a 5b 99 f7
f3 6b ff 0a 03 fa 33 c0 38 01 75 0f 97 5b 5f c3
69 c0 26 2b aa 6e 32 01 c1 e8 02 41 eb d8
It's a fastcall
function - receives a pointer to the string in ecx
, and returns the result in eax
.
The hashing function multiplies by a magic number 1856645926
, does a XOR
with input byte, and shifts right by 2 bits.
Saving and restoring noclobber registers (edi
and ebx
) took 4 bytes, but I didn't find a more efficient way to implement this. Storing the constant 10 in ebx
was particularly annoying!
Disassembly with corresponding code bytes:
57 push edi ; edi = result
53 push ebx ; we use ebx to store the constant 10
33 C0 xor eax,eax
33 FF xor edi,edi
myloop:
F6 01 0F test byte ptr [ecx],0Fh ; check for end of word
75 15 jne myhash
6A 0A push 0Ah
5B pop ebx
99 cdq ; prepare 64-bit dividend in edx:eax
F7 F3 div eax,ebx ; find the remainder of division by 10
6B FF 0A imul edi,edi,0Ah
03 FA add edi,edx ; update the result
33 C0 xor eax,eax ; reset the hash temporary variable
38 01 cmp byte ptr [ecx],al ; check for end of input (here al=0)
75 0F jne mycontinue
97 xchg eax,edi ; set the return register
5B pop ebx ; restore registers
5F pop edi ; restore registers
C3 ret
myhash:
69 C0 26 2B AA 6E imul eax,eax,6EAA2B26h ; hashing...
32 01 xor al,byte ptr [ecx] ; hashing...
C1 E8 02 shr eax,2 ; hashing...
mycontinue:
41 inc ecx ; next input byte
EB D8 jmp myloop
Equivalent C code:
int doit(const char* s)
{
int result = 0;
unsigned temp = 0;
while (true)
{
int c = *s++;
if ((c & 15) == 0)
{
temp %= 10;
result = result * 10 + temp;
temp = 0;
if (c == 0)
break;
else
continue;
}
temp *= 1856645926;
temp ^= c;
temp >>= 2;
}
return result;
}
3
Welcome to the site. There are a couple of things that we usually expect from questions that are missing here. The most important would be an objective scoring criterion which all challenges must have.
– Post Rock Garf Hunter – 2019-09-22T21:06:55.9503Aside from that this question is very sparse on specification. You should specify exactly what is required of submissions without ambiguity. One sentence and an example just isn't up to our clarity standards for challenges. – Post Rock Garf Hunter – 2019-09-22T21:08:35.033
@SriotchilismO'Zaic the
code-golf
tag is enough? – Sparkler – 2019-09-22T21:11:25.8733
On top of what has been said already, we have a sandbox where users can post their challenges before posting them to main. That way you will miss less information when making posts. If you look at other recent posts on the site with a reasonably positive reception I think you will see that both your question and solution aren't quite in line with what we do here.
– FryAmTheEggman – 2019-09-22T21:13:19.0431To handle the scoring criterion it would be the absolute minimum. You really should put a sentence in the body of the question though. E.g. "Answers will be scored in bytes with fewer bytes being better". There is technically more than one way to score [tag:code-golf], but bytes is overwhelmingly the most popular. – Post Rock Garf Hunter – 2019-09-22T21:13:50.713
1What behavior is expected on leading zeros for example should
zero zero one
output001
,1
, or are both allowed? Also what should be done with the empty string? Does it count as0
or is it not a valid input? – Post Rock Garf Hunter – 2019-09-22T21:17:33.3833At the risk of being pedantic, I'd like to point out that the
range "zero".."nine"
is not fully specified. – Unrelated String – 2019-09-22T21:22:22.1674Annoyingly, the builtin
Interpreter@"SemanticNumber"
does exactly this in Mathematica—except that it fails on strings starting withzero zero
. – Greg Martin – 2019-09-23T05:45:28.2772I have troubles parsing the title. Shouldn't it be written in the usual way, "I ... the source code, you .. the input!" ? – Eric Duminil – 2019-09-23T08:09:38.907
2What's unclear here? Why the VTCs? The only potential issue I see is the one raised by @UnrelatedString above. – Shaggy – 2019-09-23T11:40:05.633
1Can we assume the input is all uppercase, rather than all lowercase? – Grimmy – 2019-09-23T12:49:46.367
2
Possible duplicate of Convert English to a number without built-ins or libraries
– Olivier Grégoire – 2019-09-23T13:06:55.0433@OlivierGrégoire it's a different challenge to that one - this one is only the digits rather than the ordinal number in English (i.e. "four two" here "fourty two" there) this makes quite a difference. – Jonathan Allan – 2019-09-23T13:51:02.530