diff --git a/JOURNAL.md b/JOURNAL.md new file mode 100644 index 0000000..0e1dc2f --- /dev/null +++ b/JOURNAL.md @@ -0,0 +1,11 @@ +# AI-Assisted Software Development: Final Journal +# Treasure Mashabane + +## 1-4: Code Comprehension & Decomposition +I focused on breaking down complex algorithms into smaller, testable functions. I learned that "Functional Decomposition" is the first step to avoiding technical debt. + +## 5-8: Design Patterns & Advanced Features +Implementing the Factory Pattern helped me understand the 'Open/Closed' principle. I also used Decorators to handle logging, which kept my business logic clean. + +## 9-12: Code Quality & AI Collaboration +I used AI to optimize O(n^2) algorithms into O(n) solutions. My main takeaway is that AI is a powerful pair-programmer, but human oversight is required for code safety and style. diff --git a/ai_optimized.py b/ai_optimized.py new file mode 100644 index 0000000..4579ec1 --- /dev/null +++ b/ai_optimized.py @@ -0,0 +1,2 @@ +# Paste your optimized Python code here +print("Optimization logic goes here") diff --git a/use-cases/code-algorithms/python/algorithm_challenge.py b/use-cases/code-algorithms/python/algorithm_challenge.py new file mode 100644 index 0000000..115dbf9 --- /dev/null +++ b/use-cases/code-algorithms/python/algorithm_challenge.py @@ -0,0 +1,10 @@ +def filter_high_value_orders(orders, threshold): + """ + Algorithm: Extract orders above a price threshold + and format the labels for a report. + """ + return [ + {"id": o['id'], "label": f"VALUED-${o['price']}"} + for o in orders + if o.get('price', 0) > threshold + ] diff --git a/use-cases/code-algorithms/python/code_quality.py b/use-cases/code-algorithms/python/code_quality.py new file mode 100644 index 0000000..e8bf7b3 --- /dev/null +++ b/use-cases/code-algorithms/python/code_quality.py @@ -0,0 +1,21 @@ +def format_user_data(name, age, city): + """ + Demonstrates clean string formatting and + proper PEP 8 naming conventions. + """ + formatted_name = name.strip().title() + return f"User: {formatted_name} ({age}) - Location: {city.upper()}" + +cat < ai_optimized.py +def find_duplicates_optimized(items): + """ + Before: O(n^2) using nested loops. + After AI Suggestion: O(n) using a set for constant time lookups. + """ + seen = set() + duplicates = set() + for item in items: + if item in seen: + duplicates.add(item) + seen.add(item) + return list(duplicates) diff --git a/use-cases/code-algorithms/python/decomposition.py b/use-cases/code-algorithms/python/decomposition.py new file mode 100644 index 0000000..e182fec --- /dev/null +++ b/use-cases/code-algorithms/python/decomposition.py @@ -0,0 +1,10 @@ +def calculate_total(price, tax_rate, discount): + """Separating concerns: logic decomposition""" + subtotal = apply_discount(price, discount) + return apply_tax(subtotal, tax_rate) + +def apply_discount(price, discount): + return price * (1 - discount) + +def apply_tax(amount, tax_rate): + return amount * (1 + tax_rate) diff --git a/use-cases/code-algorithms/python/decorators.py b/use-cases/code-algorithms/python/decorators.py new file mode 100644 index 0000000..ba37c30 --- /dev/null +++ b/use-cases/code-algorithms/python/decorators.py @@ -0,0 +1,17 @@ +import functools +import time + +def timer_decorator(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + print(f"Execution time for {func.__name__}: {end_time - start_time:.4f}s") + return result + return wrapper + +@timer_decorator +def complex_data_process(): + time.sleep(1) + return "Process Complete" diff --git a/use-cases/code-algorithms/python/error_handling.py b/use-cases/code-algorithms/python/error_handling.py new file mode 100644 index 0000000..2a62310 --- /dev/null +++ b/use-cases/code-algorithms/python/error_handling.py @@ -0,0 +1,12 @@ +def safe_access(data, key, default=None): + """Refactored to handle KeyErrors and TypeErrors safely""" + try: + return data.get(key, default) + except AttributeError: + return "Error: Input is not a dictionary" + +def divide_numbers(a, b): + try: + return a / b + except ZeroDivisionError: + return "Error: Cannot divide by zero" diff --git a/use-cases/code-algorithms/python/factory_pattern.py b/use-cases/code-algorithms/python/factory_pattern.py new file mode 100644 index 0000000..6bef035 --- /dev/null +++ b/use-cases/code-algorithms/python/factory_pattern.py @@ -0,0 +1,17 @@ +from abc import ABC, abstractmethod + +class Notification(ABC): + @abstractmethod + def send(self, message): pass + +class EmailNotification(Notification): + def send(self, message): return f"Email sent: {message}" + +class SMSNotification(Notification): + def send(self, message): return f"SMS sent: {message}" + +class NotificationFactory: + @staticmethod + def create_notification(method): + methods = {"email": EmailNotification(), "sms": SMSNotification()} + return methods.get(method.lower(), ValueError("Invalid method")) diff --git a/use-cases/code-algorithms/python/readability.py b/use-cases/code-algorithms/python/readability.py new file mode 100644 index 0000000..39d9cb7 --- /dev/null +++ b/use-cases/code-algorithms/python/readability.py @@ -0,0 +1,8 @@ +def get_admin_emails(users): + """Before: Nested loops and manual checks. + After: Clean list comprehension for readability.""" + return [ + user['email'] + for user in users + if user.get('role') == 'admin' and user.get('active') + ] diff --git a/use-cases/code-algorithms/python/resource_management.py b/use-cases/code-algorithms/python/resource_management.py new file mode 100644 index 0000000..7ef2536 --- /dev/null +++ b/use-cases/code-algorithms/python/resource_management.py @@ -0,0 +1,5 @@ +def write_report(filename, content): + """Uses a Context Manager to ensure files are closed properly.""" + with open(filename, 'w') as file: + file.write(f"REPORT START\n{content}\nREPORT END") + return "File written successfully" diff --git a/use-cases/code-algorithms/python/task_manager.py b/use-cases/code-algorithms/python/task_manager.py new file mode 100644 index 0000000..8fbbb9b --- /dev/null +++ b/use-cases/code-algorithms/python/task_manager.py @@ -0,0 +1,7 @@ +def process_tasks(tasks): + """Refactored Task Manager using Idiomatic Python""" + return [t['name'].upper() for t in tasks if t.get('status') == 'pending'] + +def find_task_by_id(tasks, task_id): + """Algorithm Deconstruction: Search optimization""" + return next((t for t in tasks if t['id'] == task_id), None) diff --git a/use-cases/code-algorithms/python/tests_demo.py b/use-cases/code-algorithms/python/tests_demo.py new file mode 100644 index 0000000..4e1ca53 --- /dev/null +++ b/use-cases/code-algorithms/python/tests_demo.py @@ -0,0 +1,13 @@ +import unittest + +def add(a, b): + return a + b + +class TestSimpleMath(unittest.TestCase): + def test_add_positive(self): + self.assertEqual(add(1, 2), 3) + def test_add_negative(self): + self.assertEqual(add(-1, -1), -2) + +if __name__ == "__main__": + unittest.main()