0
\$\begingroup\$

This question already has an answer here:

Definition

A rational is a number that can be expressed as A/B where A and B are integers, B is positive, and A and B are co-prime.

Task

You are to write two programs/functions, one which takes a rational and output an integer, another which takes an integer and outputs a rational.

Denote the two functions as f and g respectively.

They are meant to be inverses of each other, meaning that f(g(n)) = n and g(f(r)) = r for all integers n and all rationals r.

f(r) must be defined for all rationals r and g(n) for all integers n.

f(a) == f(b) if and only if a == b; g(p) == g(q) if and only if p == q.

Specs

  • You can take the rational and the integer in any sensible format.
  • You can choose different format for f and g, but the format must be consistent within f and within g.
  • You can assume that the rational input is simplified.
  • You do not need to simplify the rational output.
  • The two functions/programs must be stand-alone.

Testcases

Below are valid rational inputs:

0/1
1/1
5/7
-1/3

Below are invalid rational inputs:

2/4 (can be output)
0/0
3/0
3/-4
-1/-5

Note

1/2 and 2/4 must map to the same integer, although only the former will be supplied.

As always, if my problem is not clear enough, please address in the comments.

\$\endgroup\$

marked as duplicate by Leaky Nun code-golf Aug 11 '16 at 15:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Related, related, related, related, related. \$\endgroup\$ – Leaky Nun Aug 11 '16 at 15:22
  • \$\begingroup\$ @Dada No, I just stated it there for convenience. \$\endgroup\$ – Leaky Nun Aug 11 '16 at 15:27
  • 1
    \$\begingroup\$ Half of the challenge here is just this challenge worded slightly differently ... \$\endgroup\$ – AdmBorkBork Aug 11 '16 at 15:32
  • \$\begingroup\$ @Dada My definition is still valid. Expressible as ratio of two integers <=> expressible as ratio of two co-prime integers \$\endgroup\$ – Leaky Nun Aug 11 '16 at 15:34
  • \$\begingroup\$ @TimmyD Alright, I'll hammer it myself. \$\endgroup\$ – Leaky Nun Aug 11 '16 at 15:47

Browse other questions tagged or ask your own question.