Oh, <PROGRAM NAME HERE>, tell me how many character '<CHARACTER HERE>'/s are there in your body.

I do not think it a dupe to this. That question asks for printing a list directly but this one requires some input that will cause problem.

Your work is to write a quine-like program/function that takes an input of one character, and respond with a number telling how many times that character occurs in your program. Comments, indents, trailing spaces, line feeds all count.

The range of acceptable input MUST cover all characters the language you are using can accept (not as string literals or comments) and all characters with lesser index in Unicode. For exmaple, Javascript and Python 3 accept Unicode characters for variable name, then it MUST allow Unicode input. For some stack processing languages, there are characters like ‘Ȯß, so part of the non-ASCII input MUST also be acceptable.

For example, if your program is in pure ASCII and only LF

foo(bar,baz)#there is a space-> 
    do things

The inputs and outputs should be:

  • \n -> 1
  • \r -> 0
  • \t -> 0
  • -> 9
  • a -> 4
  • b -> 2
  • ( -> 1
  • ) -> 1
  • \x01 through \x09 -> 0
  • (U+FF0B) -> undefined behaviour (you can even destroy everything or delete the program itself furiously)

The program with least characters wins.

  • \$\begingroup\$ closely related \$\endgroup\$ – Okx Mar 9 '17 at 11:08
  • 2
    \$\begingroup\$ @PeterTaylor I don't think it's quite a duplicate, it has a difference and that could affect answers greatly. \$\endgroup\$ – Okx Mar 9 '17 at 11:09
  • \$\begingroup\$ @Okx I can see why that would be considered a duplicate... \$\endgroup\$ – John Dvorak Mar 9 '17 at 11:16
  • 2
    \$\begingroup\$ It's definitely a duplicate. \$\endgroup\$ – Mego Mar 9 '17 at 11:17
  • 1
    \$\begingroup\$ I'm voting to reopen because I think the input taking part is different enough. \$\endgroup\$ – Rɪᴋᴇʀ Mar 17 '17 at 3:53

Python, 172 chars

lambda a:'-mbda#l.fn()+1: \\\'i'.find(a)+1#\\\\\\\\\\\\\\((((((((())))))))))fffffffbd####lllll......nnnnnnnn+1+1+1+1+1+1+1+1+1+1+11:::::::::::::              '''''''''''''''iiiiiiiiiiiiiiiii

Using str.find()+1 to generate 0 for any character not in program. Using comment for complement.


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