formatting

This commit is contained in:
Andreas Koepf 2025-01-24 10:34:07 +01:00
parent 0e9250bce0
commit aaabc05ace
37 changed files with 504 additions and 666 deletions

View file

@ -1,17 +1,20 @@
"""Base conversion task generator"""
from dataclasses import dataclass
from random import Random
from typing import Optional, Tuple
@dataclass
class BaseConversionConfig:
"""Configuration for base conversion task generation"""
min_base: int = 2 # Minimum base (2=binary)
max_base: int = 16 # Maximum base (16=hex)
min_value: int = 0 # Minimum decimal value to convert
max_value: int = 1000 # Maximum decimal value to convert
min_base: int = 2 # Minimum base (2=binary)
max_base: int = 16 # Maximum base (16=hex)
min_value: int = 0 # Minimum decimal value to convert
max_value: int = 1000 # Maximum decimal value to convert
seed: Optional[int] = None
size: int = 500 # Virtual dataset size
size: int = 500 # Virtual dataset size
def validate(self):
"""Validate configuration parameters"""
@ -55,37 +58,37 @@ class BaseConversionDataset:
def _generate_conversion(self, rng: Random) -> Tuple[int, int, int]:
"""Generate random value and source/target bases"""
value = rng.randint(self.config.min_value, self.config.max_value)
# Choose source and target bases
source_base = rng.randint(self.config.min_base, self.config.max_base)
target_base = rng.randint(self.config.min_base, self.config.max_base)
while target_base == source_base: # Ensure different bases
target_base = rng.randint(self.config.min_base, self.config.max_base)
return value, source_base, target_base
def __getitem__(self, idx: int) -> dict:
"""Generate a single base conversion task"""
rng = Random(self.seed + idx)
value, source_base, target_base = self._generate_conversion(rng)
# Convert decimal to source base representation
source_repr = format(value, f'x' if source_base == 16 else f'b' if source_base == 2 else '').strip()
source_repr = format(value, f"x" if source_base == 16 else f"b" if source_base == 2 else "").strip()
if source_base not in (2, 16):
source_repr = format(value, f'{source_base}x').lower().strip()
source_repr = format(value, f"{source_base}x").lower().strip()
# Convert decimal to target base for answer
target_repr = format(value, f'x' if target_base == 16 else f'b' if target_base == 2 else '').strip()
target_repr = format(value, f"x" if target_base == 16 else f"b" if target_base == 2 else "").strip()
if target_base not in (2, 16):
target_repr = format(value, f'{target_base}x').lower().strip()
target_repr = format(value, f"{target_base}x").lower().strip()
source_name = self._format_base_name(source_base)
target_name = self._format_base_name(target_base)
# Add hint for bases > 10 about using lowercase letters
hint = " (use lowercase letters a-z for digits above 9)" if target_base > 10 else ""
return {
"question": f"Convert the {source_name} number {source_repr} to {target_name}{hint}",
"answer": target_repr,
@ -94,8 +97,8 @@ class BaseConversionDataset:
"source_base": source_base,
"target_base": target_base,
"source_repr": source_repr,
"target_repr": target_repr
}
"target_repr": target_repr,
},
}