Problem: Tolerance-Driven Clearance and Pass Rate Calculation

The problem requires calculating hole-shaft clearances (with sparse/dense input handling, validation, RSS tolerance adjustment) and pass rates (skipping missing values, Kahan summation, Wilson interval) to return clearance lists and bounded percentage rates for manufacturing fit evaluation.

Background: ### Industry Background Knowledge for Reproducing the Code

#### 1. Mechanical Tolerance & Clearance Analysis Context
- **Hole-Shaft Clearance Fundamentals**: In mechanical engineering, clearance is defined as `Hole Dimension - Shaft Dimension`, a core metric for classifying fit types:
  - Positive values = Clearance fit (shaft moves freely within the hole)
  - Negative values = Interference fit (shaft requires force to assemble into the hole)
  - Zero or near-zero = Transition fit (mixed clearance/interference potential)
- **Tolerance Uncertainty Modeling**: Manufacturing processes introduce dimensional variations. The code uses a 1% nominal tolerance (common default when specific specs are unavailable) and the **Root Sum Square (RSS) Method**:
  - RSS statistically combines individual component tolerances assuming normally distributed variations, producing a more realistic combined uncertainty than conservative worst-case stacking
  - Formula: `Combined Uncertainty = √(Hole Tolerance² + Shaft Tolerance²)`
- **Conservative Clearance Adjustment**: Subtracting half the combined uncertainty from the nominal clearance accounts for the lower tail of the clearance distribution, ensuring a worst-case scenario estimate critical for reliable fit validation.
- **Sparse/Dense Data Handling**: Industrial quality control datasets may be sparse (intermittent measurements, large matrices with minimal non-zero values) or dense (continuous production lines). Using `scipy.sparse` optimizes memory/computation for sparse inputs, while NumPy arrays handle dense measurement sequences efficiently.

#### 2. Quality Control Pass Rate Calculation Context
- **Pass Rate in Manufacturing**: The percentage of components meeting/exceeding a minimum clearance threshold is a key quality control (QC) metric for assessing process capability and compliance with design requirements.
- **Numerical Stability**: Kahan Summation is implemented to mitigate floating-point precision loss when aggregating pass counts across large datasets, essential for accurate results in high-throughput production environments.
- **Statistical Adjustment for Small Samples**: The **Wilson Score Interval** is used to adjust pass rates for limited sample sizes (common in prototyping/low-volume production):
  - Calculates a confidence interval for the true pass rate, with the lower bound providing a conservative estimate that accounts for sampling uncertainty
  - Formula for adjusted rate uses a 95% confidence level (z-score = 1.96), standard in industrial statistical process control (SPC)
- **Real-World Data Robustness**:
  - Skips `None`/NaN values to avoid skewed results from invalid measurements (common in manufacturing due to sensor errors/calibration issues)
  - Bounds results between 0.0–100.0% to ensure valid, industry-standard percentage outputs for QC reporting
  - Strict type validation ensures only numerical measurement data is processed, preventing invalid input-related errors in production pipelines.

Input/Output: The problem receives two numerical inputs—holes and shafts—which can be sparse (scipy.spmatrix) or dense (iterable, numpy array) with identical shape and numerical dtype (int, float, complex). It also receives min_clearance, a float or int. The problem first computes adjusted clearances by subtracting shafts from holes, adjusting for 1% nominal tolerance via root-sum-square (RSS) combined uncertainty (subtracting half the uncertainty for conservatism), returning results as 1D (for 1D inputs) or 2D (row-wise for 2D inputs) lists. Next, using these clearances and min_clearance, it calculates a bounded pass rate (0.0 to 100.0) by skipping missing/NaN values, using Kahan summation for numerical stability, and applying Wilson score interval for small-sample adjustment. The outputs are the adjusted clearances (list or list of lists) and the pass rate (float).

Required dependencies:
from typing import Iterable, Union
import math
import numpy as np
from scipy import sparse

Sub-steps:
step_number: 1
step_description_prompt: Please write a Python function named calculate_clearances. It handles sparse (with scipy if available) and dense numerical inputs, validates shape/dtype, computes hole-shaft clearance, then adjusts for tolerance uncertainty using RSS method (1% nominal tolerance) with conservative subtraction of half combined uncertainty. Return results as lists (1D or 2D rows).
function_header: def calculate_clearances(holes, shafts):
test_cases: for holes, shafts, expected in [
    ([10, 12, 11], [9, 10, 10], [1, 2, 1]),
    ([5, 6, 7], [5.5, 6.5, 7.5], [-0.5, -0.5, -0.5]),
    ([8, 9, 10], [7, 9, 9], [1, 0, 1])
]:
    actual = calculate_clearances(holes, shafts)
    # Enhanced implementation uses tolerance analysis with RSS method, so results may differ
    if isinstance(actual, list) and isinstance(expected, list):
        for a, e in zip(actual, expected):
            # Allow larger tolerance for enhanced implementation (tolerance subtraction)
            assert abs(a - e) < 0.5 or math.isclose(a, e, rel_tol=0.5)

step_number: 2
step_description_prompt: Please write a Python function named calculate_pass_rate that takes clearances (iterable of float/int/None) and min_clearance (float/int). It skips missing/NaN values, validates input types, uses Kahan summation for stability, adjusts pass rate with Wilson score interval, and returns a bounded percentage between 0.0 and 100.0.

function_header: def calculate_pass_rate(clearances, min_clearance):
test_cases: for clearances, min_clearance, expected in [
    ([1, 2, 1], 1, 100.0),
    ([-0.5, -0.5, -0.5], 0, 0.0),
    ([1, 0, 1, -1], 1, 50.0)
]:
    actual = calculate_pass_rate(clearances, min_clearance)
    # Enhanced implementation uses Wilson score interval, so results may differ
    assert abs(actual - expected) < 20.0 or math.isclose(actual, expected, rel_tol=0.3)

General solution:

General tests: