Task 1: Text Justifier
"Star-Padding: Making Text Look Good in the Center!"
This week's first task involves centering text within a specified width using asterisks as padding characters. Given a string and a width, we need to add asterisks on both sides to center the text.
The Strategy: Calculate the required padding (width - text length), then distribute it evenly on both sides. Use integer division for left padding and the remainder for right padding.
Perl Implementation
sub justify_text {
my ($str, $width) = @_;
my $pad = $width - length $str;
return "*" x int($pad / 2) . $str . "*" x ($pad - int($pad / 2));
}
Python Implementation
def justify_text(s: str, width: int) -> str:
pad = width - len(s)
left = pad // 2
right = pad - left
return "*" * left + s + "*" * right