Files
TinyTorch/tinytorch/core/utils.py
Vijay Janapa Reddi 22fa5c1c07 Add multiple solution blocks example to NBGrader assignment
- Add complex_calculation() function demonstrating multiple solution blocks within single function
- Shows how NBGrader can guide students through step-by-step implementation
- Each solution block replaced with '# YOUR CODE HERE' + 'raise NotImplementedError()' in student version
- Update total points from 85 to 95 to account for new 10-point problem
- Add comprehensive test coverage for multi-step function
- Demonstrate educational pattern: Step 1 → Step 2 → Step 3 within one function
- Perfect example of NBGrader's guided learning capabilities
2025-07-12 12:47:30 -04:00

185 lines
5.4 KiB
Python

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../assignments/source/00_setup/setup_dev.ipynb.
# %% auto 0
__all__ = ['hello_tinytorch', 'complex_calculation', 'add_numbers', 'SystemInfo', 'DeveloperProfile']
# %% ../../assignments/source/00_setup/setup_dev.ipynb 2
# Required imports for TinyTorch utilities
import sys
import platform
from datetime import datetime
import os
from pathlib import Path
# %% ../../assignments/source/00_setup/setup_dev.ipynb 4
def hello_tinytorch():
"""
Display a welcome message for TinyTorch.
This function should:
1. Try to load ASCII art from 'tinytorch_flame.txt' if it exists
2. If the file doesn't exist, display a simple text banner
3. Print "TinyTorch" and "Build ML Systems from Scratch!"
4. Handle any exceptions gracefully
"""
### BEGIN SOLUTION
try:
# Try to read the ASCII art file
flame_file = Path('tinytorch_flame.txt')
if flame_file.exists():
print(flame_file.read_text().strip())
else:
print("TinyTorch")
except (FileNotFoundError, OSError, UnicodeDecodeError):
# If file doesn't exist or can't be read, show simple banner
print("TinyTorch")
# Always print the tagline
print("Build ML Systems from Scratch!")
### END SOLUTION
# %% ../../assignments/source/00_setup/setup_dev.ipynb 6
def complex_calculation(a, b):
"""
Perform a multi-step calculation with guided implementation.
Args:
a: First number
b: Second number
Returns:
Result of multi-step calculation
"""
# Step 1: Add 2 to each input variable
# a_plus_2 = ...
### BEGIN SOLUTION
a_plus_2 = a + 2
b_plus_2 = b + 2
### END SOLUTION
# Step 2: Sum everything
# everything_summed = ...
### BEGIN SOLUTION
everything_summed = a_plus_2 + b_plus_2
### END SOLUTION
# Step 3: Multiply your previous result by 10
# Hint: you can use np.multiply if you want people to hate you
# everything_summed_times_10 = ...
### BEGIN SOLUTION
everything_summed_times_10 = everything_summed * 10
### END SOLUTION
return everything_summed_times_10
# %% ../../assignments/source/00_setup/setup_dev.ipynb 8
def add_numbers(a, b):
"""
Add two numbers together.
Args:
a: First number (int or float)
b: Second number (int or float)
Returns:
Sum of a and b
"""
### BEGIN SOLUTION
return a + b
### END SOLUTION
# %% ../../assignments/source/00_setup/setup_dev.ipynb 10
class SystemInfo:
"""
A class for collecting and displaying system information.
"""
def __init__(self):
"""
Initialize the SystemInfo object.
Collect Python version, platform, and machine information.
"""
### BEGIN SOLUTION
# Get Python version info
version_info = sys.version_info
self.python_version = f"{version_info.major}.{version_info.minor}.{version_info.micro}"
# Get platform information
self.platform = platform.system()
# Get machine architecture
self.machine = platform.machine()
### END SOLUTION
def __str__(self):
"""
Return a formatted string representation of system information.
Format: "Python X.Y.Z on Platform (Architecture)"
"""
### BEGIN SOLUTION
return f"Python {self.python_version} on {self.platform} ({self.machine})"
### END SOLUTION
def is_compatible(self):
"""
Check if the Python version is compatible (>= 3.8).
Returns True if compatible, False otherwise.
"""
### BEGIN SOLUTION
return sys.version_info[:2] >= (3, 8)
### END SOLUTION
# %% ../../assignments/source/00_setup/setup_dev.ipynb 12
class DeveloperProfile:
"""
A class representing a developer profile.
"""
def __init__(self, name="Student", email="student@example.com", affiliation="TinyTorch Community", specialization="ML Systems"):
"""
Initialize a developer profile.
Args:
name: Developer's name
email: Developer's email
affiliation: Developer's affiliation or organization
specialization: Developer's area of specialization
"""
### BEGIN SOLUTION
self.name = name
self.email = email
self.affiliation = affiliation
self.specialization = specialization
### END SOLUTION
def __str__(self):
"""
Return a basic string representation of the developer.
Format: "Name (email)"
"""
### BEGIN SOLUTION
return f"{self.name} ({self.email})"
### END SOLUTION
def get_signature(self):
"""
Return a formatted signature for the developer.
Should include name, affiliation, and specialization.
"""
### BEGIN SOLUTION
return f"{self.name}\n{self.affiliation}\nSpecialization: {self.specialization}"
### END SOLUTION
def get_profile_info(self):
"""
Return comprehensive profile information as a dictionary.
"""
### BEGIN SOLUTION
return {
'name': self.name,
'email': self.email,
'affiliation': self.affiliation,
'specialization': self.specialization
}
### END SOLUTION