UTSAVS26 / PySnippets

Collection of reusable Python code snippets for all.
https://sites.google.com/view/pysnippets/
MIT License
27 stars 74 forks source link

Bug/cocktail sort #470

Closed k-harpreet closed 4 days ago

k-harpreet commented 5 days ago

issue 469

Description: This pull request addresses several issues with the cocktail_sort function in the sorting/cocktail_sort.py module, which currently fails in certain edge cases. Specifically, the algorithm fails to handle invalid input, does not correctly handle case-sensitive string sorting, and lacks proper key validation. These issues are highlighted by failing test cases, including cases with invalid input types (e.g., passing non-dictionary data) and mixed-case strings that should be sorted case-insensitively.

Fix:

Input Validation: The PR adds a check to ensure that the dict_list is a list of dictionaries and that the key parameter is a valid string. Case Sensitivity: A helper function is introduced to normalize string values for comparison, ensuring case-insensitive sorting when case_sensitive=False. Key Validation: The code now checks if the provided key exists in all dictionaries, raising a KeyError if it's missing from any dictionary. These changes ensure that the algorithm works as expected across all edge cases, including cases with mixed-case strings and invalid input.

Testing: After implementing the fix, I ran all relevant test cases, including:

test_cocktail_sort_case_sensitive_strings: Ensures that strings are sorted correctly based on case sensitivity. test_cocktail_sort_invalid_key: Validates that a KeyError is raised when an invalid key is provided. All test cases now pass successfully, and the algorithm behaves as expected.

github-actions[bot] commented 5 days ago

👋 Thank you for opening this pull request! We're excited to review your contribution. Please give us a moment, and we'll get back to you shortly!

Feel free to join our community on Discord to discuss more!