|
| 1 | +import pandas as pd |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import seaborn as sns |
| 4 | + |
| 5 | + |
| 6 | +def f_100(list_of_pairs): |
| 7 | + """ |
| 8 | + Create a Pandas DataFrame from a list of pairs and visualize the data using a bar chart. |
| 9 | + - The title of the barplot should be set to 'Category vs Value'`. |
| 10 | +
|
| 11 | + Parameters: |
| 12 | + list_of_pairs (list of tuple): Each tuple contains: |
| 13 | + - str: Category name. |
| 14 | + - int: Associated value. |
| 15 | +
|
| 16 | + Returns: |
| 17 | + tuple: |
| 18 | + - DataFrame: A pandas DataFrame with columns 'Category' and 'Value'. |
| 19 | + - Axes: A matplotlib Axes displaying a bar chart of categories vs. values. |
| 20 | +
|
| 21 | + Requirements: |
| 22 | + - pandas |
| 23 | + - matplotlib.pyplot |
| 24 | + - seaborn |
| 25 | +
|
| 26 | + Example: |
| 27 | + >>> list_of_pairs = [('Fruits', 5), ('Vegetables', 9)] |
| 28 | + >>> df, ax = f_100(list_of_pairs) |
| 29 | + >>> print(df) |
| 30 | + Category Value |
| 31 | + 0 Fruits 5 |
| 32 | + 1 Vegetables 9 |
| 33 | + """ |
| 34 | + df = pd.DataFrame(list_of_pairs, columns=["Category", "Value"]) |
| 35 | + plt.figure(figsize=(10, 5)) |
| 36 | + sns.barplot(x="Category", y="Value", data=df) |
| 37 | + plt.title("Category vs Value") |
| 38 | + ax = plt.gca() |
| 39 | + return df, ax |
| 40 | + |
| 41 | + |
| 42 | +import unittest |
| 43 | + |
| 44 | + |
| 45 | +class TestCases(unittest.TestCase): |
| 46 | + """Test cases for the f_100 function.""" |
| 47 | + |
| 48 | + @staticmethod |
| 49 | + def is_bar(ax, expected_values, expected_categories): |
| 50 | + extracted_values = [ |
| 51 | + bar.get_height() for bar in ax.patches |
| 52 | + ] # extract bar height |
| 53 | + extracted_categories = [ |
| 54 | + tick.get_text() for tick in ax.get_xticklabels() |
| 55 | + ] # extract category label |
| 56 | + |
| 57 | + for actual_value, expected_value in zip(extracted_values, expected_values): |
| 58 | + assert ( |
| 59 | + actual_value == expected_value |
| 60 | + ), f"Expected value '{expected_value}', but got '{actual_value}'" |
| 61 | + |
| 62 | + for actual_category, expected_category in zip( |
| 63 | + extracted_categories, expected_categories |
| 64 | + ): |
| 65 | + assert ( |
| 66 | + actual_category == expected_category |
| 67 | + ), f"Expected category '{expected_category}', but got '{actual_category}'" |
| 68 | + |
| 69 | + def test_case_1(self): |
| 70 | + df, ax = f_100( |
| 71 | + [ |
| 72 | + ("Allison", 49), |
| 73 | + ("Cassidy", 72), |
| 74 | + ("Jamie", -74), |
| 75 | + ("Randy", -25), |
| 76 | + ("Joshua", -85), |
| 77 | + ] |
| 78 | + ) |
| 79 | + # Testing the DataFrame |
| 80 | + self.assertEqual( |
| 81 | + df["Category"].tolist(), ["Allison", "Cassidy", "Jamie", "Randy", "Joshua"] |
| 82 | + ) |
| 83 | + self.assertEqual(df["Value"].tolist(), [49, 72, -74, -25, -85]) |
| 84 | + # Testing the plot title |
| 85 | + self.assertEqual(ax.get_title(), "Category vs Value") |
| 86 | + self.is_bar( |
| 87 | + ax=ax, |
| 88 | + expected_categories=["Allison", "Cassidy", "Jamie", "Randy", "Joshua"], |
| 89 | + expected_values=[49, 72, -74, -25, -85], |
| 90 | + ) |
| 91 | + |
| 92 | + def test_case_2(self): |
| 93 | + df, ax = f_100( |
| 94 | + [ |
| 95 | + ("Jonathan", 36), |
| 96 | + ("Maureen", 47), |
| 97 | + ("Zachary", -32), |
| 98 | + ("Kristen", 39), |
| 99 | + ("Donna", -23), |
| 100 | + ] |
| 101 | + ) |
| 102 | + # Testing the DataFrame |
| 103 | + self.assertEqual( |
| 104 | + df["Category"].tolist(), |
| 105 | + ["Jonathan", "Maureen", "Zachary", "Kristen", "Donna"], |
| 106 | + ) |
| 107 | + self.assertEqual(df["Value"].tolist(), [36, 47, -32, 39, -23]) |
| 108 | + # Testing the plot title |
| 109 | + self.assertEqual(ax.get_title(), "Category vs Value") |
| 110 | + |
| 111 | + def test_case_3(self): |
| 112 | + df, ax = f_100( |
| 113 | + [ |
| 114 | + ("Eric", -91), |
| 115 | + ("Jennifer", 52), |
| 116 | + ("James", -79), |
| 117 | + ("Matthew", 25), |
| 118 | + ("Veronica", 2), |
| 119 | + ] |
| 120 | + ) |
| 121 | + # Testing the DataFrame |
| 122 | + self.assertEqual( |
| 123 | + df["Category"].tolist(), |
| 124 | + ["Eric", "Jennifer", "James", "Matthew", "Veronica"], |
| 125 | + ) |
| 126 | + self.assertEqual(df["Value"].tolist(), [-91, 52, -79, 25, 2]) |
| 127 | + # Testing the plot title |
| 128 | + self.assertEqual(ax.get_title(), "Category vs Value") |
| 129 | + |
| 130 | + def test_case_4(self): |
| 131 | + df, ax = f_100( |
| 132 | + [ |
| 133 | + ("Caitlin", -82), |
| 134 | + ("Austin", 64), |
| 135 | + ("Scott", -11), |
| 136 | + ("Brian", -16), |
| 137 | + ("Amy", 100), |
| 138 | + ] |
| 139 | + ) |
| 140 | + # Testing the DataFrame |
| 141 | + self.assertEqual( |
| 142 | + df["Category"].tolist(), ["Caitlin", "Austin", "Scott", "Brian", "Amy"] |
| 143 | + ) |
| 144 | + self.assertEqual(df["Value"].tolist(), [-82, 64, -11, -16, 100]) |
| 145 | + # Testing the plot title |
| 146 | + self.assertEqual(ax.get_title(), "Category vs Value") |
| 147 | + |
| 148 | + def test_case_5(self): |
| 149 | + df, ax = f_100( |
| 150 | + [ |
| 151 | + ("Justin", 96), |
| 152 | + ("Ashley", 33), |
| 153 | + ("Daniel", 41), |
| 154 | + ("Connie", 26), |
| 155 | + ("Tracy", 10), |
| 156 | + ] |
| 157 | + ) |
| 158 | + # Testing the DataFrame |
| 159 | + self.assertEqual( |
| 160 | + df["Category"].tolist(), ["Justin", "Ashley", "Daniel", "Connie", "Tracy"] |
| 161 | + ) |
| 162 | + self.assertEqual(df["Value"].tolist(), [96, 33, 41, 26, 10]) |
| 163 | + # Testing the plot title |
| 164 | + self.assertEqual(ax.get_title(), "Category vs Value") |
| 165 | + |
| 166 | + def test_case_6(self): |
| 167 | + df, ax = f_100( |
| 168 | + [ |
| 169 | + ("Vanessa", -115), |
| 170 | + ("Roberto", -267), |
| 171 | + ("Barbara", 592), |
| 172 | + ("Amanda", 472), |
| 173 | + ("Rita", -727), |
| 174 | + ("Christopher", 789), |
| 175 | + ("Brandon", 457), |
| 176 | + ("Kylie", -575), |
| 177 | + ("Christina", 405), |
| 178 | + ("Dylan", 265), |
| 179 | + ] |
| 180 | + ) |
| 181 | + # Testing the DataFrame |
| 182 | + self.assertEqual( |
| 183 | + df["Category"].tolist(), |
| 184 | + [ |
| 185 | + "Vanessa", |
| 186 | + "Roberto", |
| 187 | + "Barbara", |
| 188 | + "Amanda", |
| 189 | + "Rita", |
| 190 | + "Christopher", |
| 191 | + "Brandon", |
| 192 | + "Kylie", |
| 193 | + "Christina", |
| 194 | + "Dylan", |
| 195 | + ], |
| 196 | + ) |
| 197 | + self.assertEqual( |
| 198 | + df["Value"].tolist(), [-115, -267, 592, 472, -727, 789, 457, -575, 405, 265] |
| 199 | + ) |
| 200 | + # Testing the plot title |
| 201 | + self.assertEqual(ax.get_title(), "Category vs Value") |
| 202 | + |
| 203 | + def test_case_7(self): |
| 204 | + df, ax = f_100( |
| 205 | + [ |
| 206 | + ("Kevin", -896), |
| 207 | + ("Kirk", 718), |
| 208 | + ("Cathy", -328), |
| 209 | + ("Ryan", -605), |
| 210 | + ("Peter", -958), |
| 211 | + ("Brenda", -266), |
| 212 | + ("Laura", 117), |
| 213 | + ("Todd", 807), |
| 214 | + ("Ann", 981), |
| 215 | + ("Kimberly", -70), |
| 216 | + ] |
| 217 | + ) |
| 218 | + # Testing the DataFrame |
| 219 | + self.assertEqual( |
| 220 | + df["Category"].tolist(), |
| 221 | + [ |
| 222 | + "Kevin", |
| 223 | + "Kirk", |
| 224 | + "Cathy", |
| 225 | + "Ryan", |
| 226 | + "Peter", |
| 227 | + "Brenda", |
| 228 | + "Laura", |
| 229 | + "Todd", |
| 230 | + "Ann", |
| 231 | + "Kimberly", |
| 232 | + ], |
| 233 | + ) |
| 234 | + self.assertEqual( |
| 235 | + df["Value"].tolist(), |
| 236 | + [-896, 718, -328, -605, -958, -266, 117, 807, 981, -70], |
| 237 | + ) |
| 238 | + # Testing the plot title |
| 239 | + self.assertEqual(ax.get_title(), "Category vs Value") |
| 240 | + |
| 241 | + def test_case_8(self): |
| 242 | + df, ax = f_100( |
| 243 | + [ |
| 244 | + ("Samuel", -366), |
| 245 | + ("Kathy", -267), |
| 246 | + ("Michael", -379), |
| 247 | + ("Teresa", 926), |
| 248 | + ("Stephanie", -73), |
| 249 | + ("Joy", -892), |
| 250 | + ("Robert", 988), |
| 251 | + ("Jenna", -362), |
| 252 | + ("Jodi", 816), |
| 253 | + ("Carlos", 981), |
| 254 | + ] |
| 255 | + ) |
| 256 | + # Testing the DataFrame |
| 257 | + self.assertEqual( |
| 258 | + df["Category"].tolist(), |
| 259 | + [ |
| 260 | + "Samuel", |
| 261 | + "Kathy", |
| 262 | + "Michael", |
| 263 | + "Teresa", |
| 264 | + "Stephanie", |
| 265 | + "Joy", |
| 266 | + "Robert", |
| 267 | + "Jenna", |
| 268 | + "Jodi", |
| 269 | + "Carlos", |
| 270 | + ], |
| 271 | + ) |
| 272 | + self.assertEqual( |
| 273 | + df["Value"].tolist(), |
| 274 | + [-366, -267, -379, 926, -73, -892, 988, -362, 816, 981], |
| 275 | + ) |
| 276 | + # Testing the plot title |
| 277 | + self.assertEqual(ax.get_title(), "Category vs Value") |
| 278 | + |
| 279 | + |
| 280 | +def run_tests(): |
| 281 | + suite = unittest.TestSuite() |
| 282 | + suite.addTest(unittest.makeSuite(TestCases)) |
| 283 | + runner = unittest.TextTestRunner() |
| 284 | + runner.run(suite) |
| 285 | + |
| 286 | + |
| 287 | +if __name__ == "__main__": |
| 288 | + run_tests() |
0 commit comments