Weekly Challenge 401
Each week Mohammad S. Anwar sends out The Weekly Challenge , a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. Unless otherwise stated, Copilot (and other AI tools) have NOT been used to generate the solution. It's a great way for us all to practice some coding.
Challenge , My solutions
Task 1: Fractional Addition Subtraction
You are given a string representing an expression of fraction addition and subtraction.
Write a script that returns the calculation result of given expression in string format. If final result is an integer, change it to the format of fraction that has a denominator 1.
My solution
Thankfully, Python has the fractions module, while Perl has the Number::Fraction module, written by Dave Cross.
from fractions import Fraction import re
def fractional_addition_subtraction ( expr ) -> str : fractions = re .…