5 kyu

Precise fractions pt. 1 - basics

78 of 231user6626213

Description:

This kata is part one of precise fractions series (see pt. 2: http://www.codewars.com/kata/precise-fractions-pt-2-conversion).

When dealing with fractional values, there's always a problem with the precision of arithmetical operations. So lets fix it!

Your task is to implement class Fraction that takes care of simple fraction arithmetics. Requirements:

  • class must have two-parameter constructor Fraction(numerator, denominator); passed values will be non-zero integers, and may be positive or negative.

  • two conversion methods must be supported:

  • toDecimal() returns decimal representation of fraction

  • toString() returns string with fractional representation of stored value in format:

    [ SIGN ] [ WHOLES ] [ NUMERATOR / DENOMINATOR ]

    • Note: each part is returned only if it is available and non-zero, with the only possible space character going between WHOLES and fraction. Examples: '-1/2', '3', '-5 3/4'
  • The fractional part must always be normalized (ie. the numerator and denominators must not have any common divisors).

  • Four operations need to be implemented: add, subtract, multiply and divide. Each of them may take integers as well as another Fraction instance as an argument, and must return a new Fraction instance.

  • Instances must be immutable, hence none of the operations may modify either of the objects it is called upon, nor the passed argument.

Python Notes

  • If one integer is passed into the initialiser, then the fraction should be assumed to represent an integer not a fraction.
  • You must implement the standard operator overrides __add__, __sub__, __mul__, __div__, in each case you should support other being an int or another instance of Fraction.
  • Implement __str__ and to_decimal in place of toString and toDecimal as described above.
Algorithms
Mathematics
Puzzles

More By Author:

Check out these other kata created by user6626213

Stats:

CreatedFeb 2, 2015
PublishedFeb 3, 2015
Warriors Trained985
Total Skips512
Total Code Submissions4241
Total Times Completed231
JavaScript Completions57
Java Completions113
Python Completions78
Total Stars26
% of votes with a positive feedback rating93% of 75
Total "Very Satisfied" Votes66
Total "Somewhat Satisfied" Votes7
Total "Not Satisfied" Votes2
Total Rank Assessments24
Average Assessed Rank
5 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • user6626213 Avatar
  • jhoffner Avatar
  • ChristianECooper Avatar
  • bidouille Avatar
Ad