28
\$\begingroup\$

Background

Hello is a language "written" by Anne Veling, which errors if the program does not contain only h, and will print Hello World for every h

Task

You are to write an interpreter/compiler for Hello.

Example in Python

import os
i = input("Program: ")
for a in i:
    if a != 'h':
        print("err")
        quit()
    else:
        print("Hello World")

Details

  • If the program is not only hs, it must print err or error (case-insensitive)
  • It's okay if you print "Hello World"s before your program discovers a non-h character and errors, however, the program must halt if a non-h
  • You may throw an error, as long as you print err/error before throwing, or by using a custom error builtin, like raise SyntaxError('err') in python. (basically, you have to purposefully error with err/error
  • Assume the program will not be empty
  • Output can have a trailing newline, space, or nothing to separate Hello Worlds
  • If a program has multiple lines, it should error (due to \n not being an h)
  • You can assume that input will always be ASCII 33-126 and 10 (decimal)
  • The hs are case sensitive (so H is not a valid program) instruction is found
  • This is , shortest answer wins

Test cases

Input:

h

Output:

Hello World

Input:

hhhhhh

Output:

Hello WorldHello WorldHello WorldHello WorldHello WorldHello World

(again, it doesn't matter whether it's spaces, newlines, or nothing to separate `Hello World`s

Input:

rubbish

Output:

err

Input:

huh

Output:

Hello Worlderr

Or

err

First challenge so please have mercy

\$\endgroup\$
27
  • 11
    \$\begingroup\$ Thank you for using the sandbox. I'd recommend leaving challenges there for at least a week, and even to advertise their imminent publishing in TNB, to lessen the risk of having to make changes after posting. \$\endgroup\$
    – Adám
    Commented Sep 3, 2020 at 4:46
  • 4
    \$\begingroup\$ Can we return a list of Hello Worlds? \$\endgroup\$
    – Adám
    Commented Sep 3, 2020 at 5:23
  • 4
    \$\begingroup\$ Currently there are 2 kind of solutions: those which reproduce the sample code (for hhxhh outputs “Hello WorldHello Worlderr”) and those which only implement the written rules (for hhxhh outputs “err”). This is not good. Please clarify and add a relevant test case. \$\endgroup\$
    – manatwork
    Commented Sep 3, 2020 at 12:48
  • 3
    \$\begingroup\$ @RossPresser "It's okay if you print "Hello World"s before your program discovers a non-h character and errors" \$\endgroup\$
    – Adám
    Commented Sep 3, 2020 at 13:25
  • 3
    \$\begingroup\$ I never said it should print err, I said it could print Hello Worlderr or err \$\endgroup\$
    – the-cobalt
    Commented Sep 3, 2020 at 13:26

63 Answers 63

1 2
3
0
\$\begingroup\$

Rattle, 33 bytes

|II^P[gn[^104/0]b"Hello World">]`

Try it Online!

Explanation:

|                 take input
 I                split input to characters and store characters in consecutive memory slots
  I^              get length of input (put on top of the stack)
    P             set pointer to 0
     [...]`       repeat n times, where n is the top of the stack

g                 get the value at the pointer
 n                get the ASCII int value of the character
  [^104  ]        if not 104 (the ASCII value for 'h')
       /0         divide by 0 (shortest way to throw an error)

b"Hello World"    add "Hello World" to print buffer
              >   move pointer right
\$\endgroup\$
0
\$\begingroup\$

TypeScript's type system, 73 bytes

type F<S>=S extends`h${infer S}`?`Hello World${F<S>}`:S extends""?S:"err"

Try it at the TS playground

TypeScript's type system is well suited for tasks like this.

This version hits the recursion limit for strings longer than 50 characters. Here's an alternate version, using tail-recursion to increase the limit from 50 to 999:

TypeScript's type system, 93 bytes

//@ts-ignore
type F<S,O="">=S extends`h${infer S}`?F<S,`${O}Hello World`>:S extends""?O:"err"

Try it at the TS playground

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

YASEPL, 60 bytes

=b=1'=c®1`1!d¥b,1}7,"h",8!+}2,c;"Hello World",c<|9`8>"err"`9

explanation

=b=1'=c®1`1!d¥b,1}7,"h",8!+}2,c;"Hello World",c<|9`8>"err"`9      packed
=b                                                                declare increment variable "b"
  =1'                                                             get user input and set it to variable "1"
     =c®1                                                         get length of variable 1 and set it to "c"
         `1                }2,c                                   while b < c...
           !d¥b,1                                                 get a character from variable 1 at index b and set it to "d"
                 }7,"h",8                         `8              if d is equal to "h"...
                         !+                                       add one to b.
                                                    >"err"        ...else, escape loop, error and quit
                               ;"Hello World",c<                  repeat "Hello World" c times once done checking if everything is "h" and print it
                                                |9                go to the end of the program
                                                          `9      end of the program

(im trying my best to format the explanation like how other people are. if it doesnt make sense tell me)

\$\endgroup\$
1 2
3

Your Answer

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

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