לדלג לתוכן

6.10 typing תרגול

נחש אותי נחש

  • בתרגיל הזה אתה מקבל קטע קוד, תשתמש במודול typing כדי להוסיף type-hinting לפרויקט.
  • זכור: type-hinting גורם לקוד להראות טוב יותר - תמיד נרצה לעשות type-hinting לכל קוד שאנחנו כותבים :)
    def process_data(data):
        """
        Process a list of tuples containing a string and an integer.
        Returns a dictionary where the keys are strings and the values are integers.
        """
        result = {}
        for item in data:
            key, value = item
            if key not in result:
                result[key] = value
            else:
                result[key] += value
        return result
    
    def display_results(results):
        """
        Display the results from processing the data.
        """
        print("Processed Results:")
        for key, value in results.items():
            print(f"{key}: {value}")
    
    def main():
        """
        Main function to orchestrate the processing of data.
        """
        data = [("apple", 5), ("banana", 3), ("apple", 2), ("orange", 4)]
        processed_data = process_data(data)
        display_results(processed_data)
    
    if __name__ == "__main__":
        main()