6
\$\begingroup\$

United States senators are sorted into three classes, based on what year their six-year term starts. Since each state has two senators, each state has a senator in two of the three classes.

Given a full state name and a class number (1, 2, or 3), output truthy if that state has a senator of that class, and falsey if that state does not have a senator of that class.

States with Class 1 senators:

Arizona, California, Connecticut, Delaware, Florida, Hawaii, Indiana, Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, New Jersey, New Mexico, New York, North Dakota, Ohio, Pennsylvania, Rhode Island, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia, Wisconsin, Wyoming

States with Class 2 senators:

Alabama, Alaska, Arkansas, Colorado, Delaware, Georgia, Idaho, Illinois, Iowa, Kansas, Kentucky, Louisiana, Maine, Massachusetts, Michigan, Minnesota, Mississippi, Montana, Nebraska, New Hampshire, New Jersey, New Mexico, North Carolina, Oklahoma, Oregon, Rhode Island, South Carolina, South Dakota, Tennessee, Texas, Virginia, West Virginia, Wyoming

States with Class 3 senators:

Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maryland, Missouri, Nevada, New Hampshire, New York, North Carolina, North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, South Carolina, South Dakota, Utah, Vermont, Washington, Wisconsin

Test Cases

"Rhode Island", 1 -> true
"Rhode Island", 2 -> true
"Rhode Island", 3 -> false
"California", 1 -> true
"California", 2 -> false
"California", 3 -> true
"South Dakota", 1 -> false
"South Dakota", 2 -> true
"South Dakota", 3 -> true
\$\endgroup\$
13
\$\begingroup\$

Python 2, 67 bytes

lambda s,l:0x169562f9e6307fcdb9e/3**(hash(s)%337%294%137%46)%3+1!=l

Try it online!

The states can be sorted into three disjoint categories, based on what class of senators they don't have. hash(s)%337%294%137%46 generates a number \$0 \le k \le 45 \$ without mixing up the categories. This number is then used to get a single base-3 digit out of 0x169562f9e6307fcdb9e.

If the data is stored in base-4 the digit extraction and comparison are shorter, so we end up with the same byte count:

lambda s,l:0xccf73afa747a5cb6295df6e>>hash(s)%337%294%137%46*2&3!=l

Try it online!

| improve this answer | |
\$\endgroup\$
6
\$\begingroup\$

JavaScript (ES6),  98 ... 83  79 bytes

A regex solution.

Returns a non-zero value if the state has a senator of the given class.

s=>n=>n^=/Oh|gt|N.*D|t$|yl|[UYfv]|[Wdir]i/.test(s)?2:/^I|[ACGHKLOS]/.test(s)||3

Try it online!

How?

The first regular expression matches the states with class 1 and class 3 senators:

 pattern    | states
------------+---------------------------------------------------------------------------
 Oh         | Ohio
 gt         | Washington
 N.*D       | North Dakota
 t$         | Connecticut, Vermont
 yl         | Maryland, Pennsylvania
 [UYfv]     | California, Nevada, New York, (Pennsylvania), Utah
 [Wdir]i    | Arizona, Florida, Hawaii, Indiana, Missouri, Wisconsin

The second regular expression matches the states with class 2 and class 3 senators:

 pattern    | states
------------+---------------------------------------------------------------------------
 ^I         | Idaho, Illinois, (Indiana1), Iowa
 [ACGHKLOS] | Alabama, Alaska, (Arizona1), Arkansas, (California1), Colorado,
            | (Connecticut1), Georgia, (Hawaii1), Kansas, Kentucky, Louisiana,
            | New Hampshire, North Carolina, (Ohio1), Oklahoma, Oregon, South Carolina,
            | South Dakota

1: These states belong to the first group. But if they've already been identified as such by the first regular expression, this one will not be executed anyway.

The states with class 1 and class 2 senators are matched by neither of them:

Maine, Nebraska, Massachusetts, Michigan, Delaware, Minnesota, New Jersey, Tennessee,
West Virginia, Mississippi, Virginia, Wyoming, Montana, Rhode Island, New Mexico, Texas
| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

JavaScript (Node.js), 79 bytes

Returns a non-zero value if the state has a senator of the given class.

s=>n=>0x3A27C2823007F077n.toString(3)[parseInt(s[8]+5+s,35)%462%255%113%56]^~-n

Try it online!

The method used to transform the input string is similar to the one described in my answer to this related challenge. Its purpose is to solve the 3 problems we have in JS:

  • no built-in hash function
  • parseInt() breaks on non-alphanumeric characters
  • only 52 bits of mantissa for Numbers (i.e. 2 distinct strings sharing the same prefix may be parsed the same way)
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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