code: def nhs_check_digit(ninedigits: Union[str, List[Union[str, int]]]) -> int:
      
      if len(ninedigits) != 9 or not all(str(x).isdigit() for x in ninedigits):
          raise ValueError("bad string to nhs_check_digit")
      check_digit = 11 - (sum([
          int(d) * f
          for (d, f) in zip(ninedigits, NHS_DIGIT_WEIGHTINGS)
      ]) % 11)
      # ... % 11 yields something in the range 0-10
      # ... 11 - that yields something in the range 1-11
      if check_digit == 11:
          check_digit = 0
      return check_digit
docs: Calculates an NHS number check digit.
  
      Args:
          ninedigits: string or list
  
      Returns:
          check digit
  
      Method:
  
      1. Multiply each of the first nine digits by the corresponding
         digit weighting (see :const:`NHS_DIGIT_WEIGHTINGS`).
      2. Sum the results.
      3. Take remainder after division by 11.
      4. Subtract the remainder from 11
      5. If this is 11, use 0 instead
         If it's 10, the number is invalid
         If it doesn't match the actual check digit, the number is invalid