25
\$\begingroup\$

The task is simple. Write an interpreter for the language *.

Here's a bigger link to the wiki.

There are only three valid * programs:

  • * Prints "Hello World"
  •  *  Prints a random number between 0 and 2,147,483,647
  • *+* Runs forever.

The third case must be an infinite loop according to the specifications in this question

Input:

  • The input can be taken via any acceptable input method by our standard I/O rules
  • It will always be one of the above programs

Output:

  • The first case should print exactly Hello World, with or without a trailing line break.
  • For the second case, if your language's integer maximum is smaller than 2,147,483,647, use your language's integer maximum
  • The first and second cases can print to any acceptable output by our standard I/O rules.
  • The third case should not give any output.

Scoring:

As this is , the shortest answer, in bytes, wins.

\$\endgroup\$
8
  • 7
    \$\begingroup\$ When you say 'between 0 and 2,147,483,647', is that inclusive or exclusive? (E.g., is 0 a valid output?) \$\endgroup\$
    – Chas Brown
    Aug 9, 2019 at 21:46
  • 7
    \$\begingroup\$ Changing the spec after posting a challenge and invalidating existing solutions is an automatic -1 from me. \$\endgroup\$
    – Shaggy
    Aug 10, 2019 at 7:00
  • 2
    \$\begingroup\$ For languages that don't have a built-in way of generating a random number, is there an acceptable way to satisfy the "print a random number" requirement? \$\endgroup\$ Aug 10, 2019 at 22:44
  • 2
    \$\begingroup\$ If our language's integers have no, or a higher, maximum may we use a higher upper bound? \$\endgroup\$ Aug 10, 2019 at 23:43
  • 8
    \$\begingroup\$ @Shaggy I'm not seeing any rule changes in the wiki for the question, only a space to a non-breaking space (check the markdown tab), because the SE markdown renderer wasn't rendering it, but looking at the original revisision, it's clear it should be normal spaces, and the "hack' is only done for SE markdown renderer issues \$\endgroup\$
    – Ferrybig
    Aug 11, 2019 at 20:49

35 Answers 35

1
2
1
\$\begingroup\$

Wren, 143 135 bytes

I am not a good golfer... The RNG generates the same value each time because it is a pseudo-random number generator.

Fn.new{|a|
import"random"for Random
if(a=="*+*"){
while(1){}
}else System.write(a[0]==" "?Random.new(9).int((1<<31)-1):"Hello World")
}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Kotlin, 78 bytes

{print(if(it<"*")(0..2147483647).random()else{while(it>"*"){}
"Hello World"})}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

VBScript, 147 bytes

Sub m(s)
randomize:i=0
if s="*" then msgbox "Hello World"
if s=" * " then msgbox int(2147483647*Rnd)
if s="*+*" then
do
loop while 1
end if
end sub
\$\endgroup\$
0
\$\begingroup\$

Crystal, 75 bytes

puts case gets;when"*";"Hello World"when/\+/;loop{}else;rand 2147483647;end

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 21 bytes

['*›_#}gi„Ÿ™‚ï™ëžIL<Ω

Try it online! * times out on TIO as the code generates the list [1, 2, 3, ..., 2147483648], subtracts 1 from each element, and picks a random element from the list. This is required as the only sources of randomness in 05AB1E are Ω/.R (random pick) and .r (random shuffle), but no pure RNGs.

['*›_#}gi„Ÿ™‚ï™ëžIL<Ω  # full program
     #                 # break out of...
[                      # infinite loop...
     #                 # if...
                       # implicit input...
   ›                   # does...
    _                  # not...
   ›                   # come after...
 '*                    # literal...
   ›                   # in lexicographical order
      }                # end infinite loop
        i              # if...
       g               # length of...
                       # implicit input...
        i              # is 1...
         „Ÿ™‚ï         # then push "hello world"...
              ™        # in title case
               ë       # else...
                    Ω  # pick a random element from...
                  L    # [1, 2, 3, ...,
                žI     # ..., 2147483648]...
                       # (implicit) with each element...
                   <   # decremented
                       # implicit output
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.