|
| 1 | +""" |
| 2 | +
|
| 3 | +2026 Winter Games Day 14: Ski Mountaineering |
| 4 | +Given the snow depth and slope of a mountain, determine if there's an avalanche risk. |
| 5 | +
|
| 6 | +The snow depth values are "Shallow", "Moderate", or "Deep". |
| 7 | +Slope values are "Gentle", "Steep", or "Very Steep". |
| 8 | +Return "Safe" or "Risky" based on this table: |
| 9 | +
|
| 10 | +"Shallow" "Moderate" "Deep" |
| 11 | +"Gentle" "Safe" "Safe" "Safe" |
| 12 | +"Steep" "Safe" "Risky" "Risky" |
| 13 | +"Very Steep" "Safe" "Risky" "Risky" |
| 14 | +""" |
| 15 | + |
| 16 | + |
| 17 | +import unittest |
| 18 | + |
| 19 | +class SkiMountaineeringTest(unittest.TestCase): |
| 20 | + |
| 21 | + def test1(self): |
| 22 | + self.assertEqual(avalanche_risk("Shallow", "Gentle"), "Safe") |
| 23 | + |
| 24 | + def test2(self): |
| 25 | + self.assertEqual(avalanche_risk("Shallow", "Steep"), "Safe") |
| 26 | + |
| 27 | + def test3(self): |
| 28 | + self.assertEqual(avalanche_risk("Shallow", "Very Steep"), "Safe") |
| 29 | + |
| 30 | + def test4(self): |
| 31 | + self.assertEqual(avalanche_risk("Moderate", "Gentle"), "Safe") |
| 32 | + |
| 33 | + def test5(self): |
| 34 | + self.assertEqual(avalanche_risk("Moderate", "Steep"), "Risky") |
| 35 | + |
| 36 | + def test6(self): |
| 37 | + self.assertEqual(avalanche_risk("Moderate", "Very Steep"), "Risky") |
| 38 | + |
| 39 | + def test7(self): |
| 40 | + self.assertEqual(avalanche_risk("Deep", "Gentle"), "Safe") |
| 41 | + |
| 42 | + def test8(self): |
| 43 | + self.assertEqual(avalanche_risk("Deep", "Steep"), "Risky") |
| 44 | + |
| 45 | + def test9(self): |
| 46 | + self.assertEqual(avalanche_risk("Deep", "Very Steep"), "Risky") |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +def avalanche_risk(snow_depth, slope): |
| 52 | + |
| 53 | + if slope == "Gentle" and snow_depth in ("Shallow", "Moderate", "Deep"): |
| 54 | + return "Safe" |
| 55 | + elif slope == "Steep": |
| 56 | + if snow_depth in ("Moderate", "Deep"): |
| 57 | + return "Risky" |
| 58 | + else: |
| 59 | + return "Safe" |
| 60 | + |
| 61 | + elif slope == "Very Steep": |
| 62 | + if snow_depth in ("Moderate", "Deep"): |
| 63 | + return "Risky" |
| 64 | + else: |
| 65 | + return "Safe" |
| 66 | + |
| 67 | +""" |
| 68 | +The above code can be refined => Cleaner, less Repetition |
| 69 | +
|
| 70 | +""" |
| 71 | +def avalanche_risk(snow_depth, slope): |
| 72 | + if slope == "Gentle": |
| 73 | + return "Safe" |
| 74 | + elif slope in ("Steep", "Very Steep"): |
| 75 | + if snow_depth in ("Moderate", "Deep"): |
| 76 | + return "Risky" |
| 77 | + return "Safe" |
| 78 | + else: |
| 79 | + return "Safe" |
| 80 | + |
| 81 | +""" |
| 82 | +=> Removed duplication: "Steep" and "Very Steep" share the same logic, so we group them. |
| 83 | +=> Added a fallback else to handle unexpected slope values gracefully. |
| 84 | +=> Cleaner structure, easier to maintain. |
| 85 | +""" |
| 86 | + |
| 87 | +def avalanche_risk(snow_depth, slope): |
| 88 | + risk_table = { |
| 89 | + "Gentle": {"Shallow":"Safe", "Moderate":"Safe","Deep":"Safe"}, |
| 90 | + "Steep": {"Shallow":"Safe","Moderate":"Risky","Deep":"Risky"}, |
| 91 | + "Very Steep":{"Shallow":"Safe", "Moderate":"Risky", "Deep":"Risky"} |
| 92 | + } |
| 93 | + |
| 94 | + return risk_table[slope][snow_depth] |
| 95 | + |
| 96 | +""" |
| 97 | +
|
| 98 | +=> The solution use a lookup table( nested dictionar) to directly map slope and snow_depth combinations to "Safe" or "Risky". |
| 99 | +=> This avoids the long chaiins of if/elif conditions and makes the code easy to read and extend. |
| 100 | +
|
| 101 | +""" |
| 102 | + |
| 103 | +def test_avalanche_risk(): |
| 104 | + cases = [ |
| 105 | + ("Shallow", "Gentle", "Safe"), |
| 106 | + ("Moderate", "Gentle", "Safe"), |
| 107 | + ("Deep", "Gentle", "Safe"), |
| 108 | + ("Shallow", "Steep", "Safe"), |
| 109 | + ("Moderate", "Steep", "Risky"), |
| 110 | + ("Deep", "Steep", "Risky"), |
| 111 | + ("Shallow", "Very Steep", "Safe"), |
| 112 | + ("Moderate", "Very Steep", "Risky"), |
| 113 | + ("Deep", "Very Steep", "Risky"), |
| 114 | + ] |
| 115 | + |
| 116 | + |
| 117 | + for snow, slope, expected in cases: |
| 118 | + result = avalanche_risk(snow, slope) |
| 119 | + assert result == expected, f"Failed for {snow}, {slope}: got {result}, expected {expected}" |
| 120 | + |
| 121 | + print("All test cases passed!") |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + unittest.main() |
| 128 | + test_avalanche_risk() |
0 commit comments