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

Python 2, 67 63 bytes

lambda s,l:0x764d5443a33628366/3**(hash(s)%4560%1502%43)%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)%4560%1502%43 generates a number \$0 \le k \le 42 \$ without mixing up the categories. This number is then used to get a single base-3 digit out of 0x764d5443a33628366.


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

A regex solution.

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


Try it online!


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

JavaScript (Node.js), 79 bytes

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


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)

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.