|
Post by Josh on Apr 28, 2017 18:15:16 GMT
Random Integer to EnglishIn this code golf exercise, you must generate a uniform random integer from 1 to 10,000. You should then output each integer in that random number in English separated by spaces.
Example:
Random Number:
4603 Output:
four six zero three You must try to achieve this in the smallest amount of characters possible in a language of your choice.
Please format your solution as the language name followed by the number of characters that your solution contains before writing out the solution in a code block.
Good luck!
This is a replacement for "Convert the integers" so that it can be fair for all users to attempt.
|
|
omgitspenguins
Active Member
Veteran
OmgStillAPenguin
Posts: 53
ROBLOX Username: penguins113311
|
Post by omgitspenguins on Apr 28, 2017 18:17:17 GMT
2 hard 4 m3
|
|
zelo101
Active Member
Veteran
Posts: 26
ROBLOX Username: Umadekid
|
Post by zelo101 on Apr 28, 2017 18:21:09 GMT
4 hard 8 m6
|
|
|
Post by Josh on Apr 28, 2017 18:33:01 GMT
Have a go! See what you can do!
|
|
|
Post by 128Gigabytes on Apr 28, 2017 19:01:44 GMT
I'll start us off with an easy one, just the first way I thought to do it shortened.
Lua, 167 characters.
a,b={"zero";"one";"two";"three";"four";"five";"six";"seven";"eight";"nine"},''for d in tostring(math.random(1,10^4)):gmatch"."do b=b..a[tonumber(d)+1].." "end print(b)
|
|
|
Post by Josh on Apr 28, 2017 19:25:49 GMT
I'll start us off with an easy one, just the first way I thought to do it shortened. Lua, 167 characters. a,b={"zero";"one";"two";"three";"four";"five";"six";"seven";"eight";"nine"},''for d in tostring(math.random(1,10^4)):gmatch"."do b=b..a[tonumber(d)+1].." "end print(b)
Looks good, 128Gigabytes! We'll have to see if anyone can come up with a shorter solution!
|
|
|
Post by 128Gigabytes on Apr 28, 2017 19:36:41 GMT
Lua, 161 characters.
print(({tostring(math.random(1, 10^4)):gsub(".", function(c) return ({"zero";"one";"two";"three";"four";"five";"six";"seven";"eight";"nine"})[c+1]..' 'end)})[1])
Or this, if it is okay to have it also print an extra number at the end (Since gsub returns 2 values.)
Lua, 154 characters.
print(tostring(math.random(1, 10^4)):gsub(".", function(c) return ({"zero";"one";"two";"three";"four";"five";"six";"seven";"eight";"nine"})[c+1]..' 'end))
|
|
|
Post by Xzeni on Apr 29, 2017 15:28:41 GMT
math.randomseed(tick())
local a={"zero","one","two","three","four","five","six","seven","eight","nine"}
local r=tostring(math.random(1,10^4))
local c=""
for i=1,#r do
c=c.." "..a[tonumber(r:sub(i,i))+1]
end
print(c)
In terms of shortness, I lost. I just like keeping my code clean, organized, and readable. Sorry.
|
|
vha
New Member
Veteran
Posts: 5
|
Post by vha on Apr 30, 2017 17:12:16 GMT
I think I've the smallest solution so far here:
import random
d=("zero","one","two","three","four","five","six","seven","eight","nine")
for c in str(random.randint(1,1e4)):
print(d[int(c)]) Written in python with 142 characters.
|
|
|
Post by Josh on Apr 30, 2017 17:14:01 GMT
I think I've the smallest solution so far here: import random
d=["zero","one","two","three","four","five","six","seven","eight","nine"]
for c in str(random.randint(1,1e4)):
print(d[int(c)]) Written in python with 142 characters. Looks good!
|
|
zmart
New Member
Veteran
Posts: 8
|
Post by zmart on May 2, 2017 17:15:05 GMT
vha's 142 char solution forced me to look for ways to shorten my first-off solution that was 149 chars, and thus I'm only ousting him by 1 char! Awesome solution, vha.
JavaScript* ,141 characters
(Math.floor(Math.random()*9999+1)+'').split('').map((d)=>{return ['zero','one','two','three','four','five','six','seven','eight','nine'][d]}) Shortcuts used: +'' to typecast string vs String() and .toString() ()=> anonymous function shorthand in ECMA6 string:number array reference behavior (See ["zero", "one", "two"]["1"] : one)
* ECMAScript 2015 / modern web console
|
|
|
Post by Josh on May 2, 2017 17:18:59 GMT
|
|
zmart
New Member
Veteran
Posts: 8
|
Post by zmart on May 2, 2017 17:40:50 GMT
Thanks Josh! Apologies for missing formatting specification on the first go around.
|
|
|
Post by cntkillme on May 4, 2017 6:48:13 GMT
warn(s:gsub('.',function(v)return({'zero','one','two','three','four','five','six','seven','eight','nine'})[v+1]..' 'end)) 121 characters
Edit: some people didn't include a way of outputting the string itself, so getting rid of the warn call makes this 115 characters.
|
|
zmart
New Member
Veteran
Posts: 8
|
Post by zmart on May 4, 2017 13:35:55 GMT
warn(s:gsub('.',function(v)return({'zero','one','two','three','four','five','six','seven','eight','nine'})[v+1]..' 'end)) 121 characters Specification for this run says "you must generate a uniform random integer from 1 to 10,000", but from what I can tell, you're substituting 's' for this operation. I'm honestly a complete newb at code golf - only been coding for a few years, golfing for about 2 weeks - so if this is a conventional standard that has made it over from some other communities that can be automatically assumed while golfing, I'd be very curious to know more. My problem with it is that such a convention would give a ton of task interpretation to the individual golfers themselves. Someone used 's' to skip an entire chunk of the Golf Course, so why not use 's' to skip even more? This could eventually devolve until solutions to potentially "open" courses are some version of the following: print(s)
|
|