Menu

Weekly Challenge: Mixed fractions
📰

Weekly Challenge: Mixed fractions

DEV Community: perl·Simon Green·6 days ago
#tIkfGjjr
#devtoperl#devto#dev#number#word
Long article (1280 words) — no AI summary yet.
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.