20
3
In the language Nim, the rules for differentiating identifiers are slightly more relaxed than most other languages. Two identifiers are equivalent or address the same variable if they follow these rules:
- the first character of both are the same (case sensitive)
- both strings are the same (case insensitive) after removing all instances of the characters
-
and_
Challenge
Write a program/function that takes two strings that represent Nim identifiers and output a truthy or falsey value based on whether or not they are equivalent by the rules above.
Specifications
- Standard I/O rules apply.
- Standard loopholes are forbidden.
- The strings will only contain ASCII printables. You do not need to check if it's a valid identifier.
- The strings may be taken as two separate inputs, list of strings, etc. (you know the drill)
- Empty strings need not be handled.
- The output must be consistent for both truthy and falsey values.
- This challenge is not about finding the shortest approach in all languages, rather, it is about finding the shortest approach in each language.
- Your code will be scored in bytes, usually in the encoding UTF-8, unless specified otherwise.
- Built-in functions that perform this task are allowed but including a solution that doesn't rely on a built-in is encouraged.
- Explanations, even for "practical" languages, are encouraged.
Test cases
Input Output
count, Count falsey
lookMaNoSeparator, answer falsey
_test, test falsey
test, tset falsey
aVariableName, a_variable_name truthy
numbers_are_cool123, numbersAreCool123 truthy
symbolsAre_too>_>, symbols_areTOO>> truthy
Ungolfed reference implementation
This is written in Nim, itself.
import strutils, re
proc sameIdentifier(a, b: string): bool =
a[0] == b[0] and
a.replace(re"_|–", "").toLower == b.replace(re"_|–", "").toLower
3I suggest a test case of
f("_test", "test")
. – Doorknob – 2017-07-28T02:51:34.817@Doorknob Added. – totallyhuman – 2017-07-28T03:14:19.470
1I suggest adding
f("test", "tset")
, as I think one answer gives an unexpected result for it. – Ørjan Johansen – 2017-07-29T00:11:31.527@ØrjanJohansen Done. – totallyhuman – 2017-07-29T18:36:22.640
Wait, so input is strings that "represent Nim identifiers", and we "do not need to check if it's a valid identifier", but then one of the examples contains
>
? – aschepler – 2017-07-29T21:54:23.567It looks like if we can assume actual Nim identifiers, even
-
is disallowed. – Ørjan Johansen – 2017-07-29T23:06:20.210To be honest, I don't know Nim much. To be fair, I didn't say they were valid Nim identifiers. :P However much they might be wrong, the challenge is still the same. Nim is just an inspiration. ;) – totallyhuman – 2017-07-30T00:21:41.147