Mixed Fraction Equality

15

In elementary school, children learn about proper fractions, where the numerator is less than the denominator, and thus the value of the fraction is less than one. Later, they are taught about fractions where the value of the fraction is greater than one, and two different ways to express these fractions: mixed fractions and improper fractions.

Given a mixed fraction, determine if it is equivalent to the improper fraction where the integer value and the numerator are concatenated together. For example, for input 1 3/4, the improper fraction is 13/4.

Test Cases

1 3/4        -> falsey
1 3/10       -> truthy
6 6/7        -> falsey
55 55/100    -> truthy
4 9/100      -> falsey
40 9/100     -> falsey
7 49/1000    -> falsey
9 1/2        -> falsey
999 999/1000 -> truthy
1 21/200     -> falsey
1 21/101     -> falsey

For input, you may take the integer part and the fraction part as separate inputs, but you may not take the fraction as input in two pieces, and you may not take it as a decimal value. You may drop the integer part (not take it as input) if you do not need to use it.

Stephen

Posted 2018-07-24T22:26:59.847

Reputation: 12 293

Should or can the fraction be simplified? Like the fourth test case would be false as 54/100 simplifies to 27/50 – Jo King – 2018-07-24T22:45:58.687

1Should output be two distinct, consistent values or any, possibly inconsistent, truthy/falsey values? – Luis Mendo – 2018-07-24T22:53:56.253

@JoKing no, and I will fix that one, should both be 55 – Stephen – 2018-07-24T23:15:12.167

@LuisMendo whatever meta defines as truthy falsey – Stephen – 2018-07-24T23:15:37.130

This is probably inherent in the definition of mixed fractions, but just to confirm: can we assume that the fraction part of the input will have its numerator less than the denominator? – sundar - Reinstate Monica – 2018-07-24T23:25:45.827

1By the way, fixing the 4th test case to have 55 wouldn't change the issue would it - 55/100 can also be simplified to 11/20, so the same question @JoKing raised arises there. – sundar - Reinstate Monica – 2018-07-24T23:44:00.243

@sundar I think it was two separate statements: "No simplification involved" and "I made a typo, I'll fix that" – Οurous – 2018-07-24T23:46:48.573

So the second argument must be taken as a string? – dylnan – 2018-07-24T23:49:12.653

3"you may not take the fraction as input in two pieces" - err why? That is exactly what the / does :/ – Jonathan Allan – 2018-07-24T23:54:10.787

11This seems to be equivalent to "given an input that doesn't matter and two numbers as a string separated by a slash, determine whether the second number equals 10 to the power of the length of the first number". – xnor – 2018-07-25T00:15:00.453

@sundar Yes, you can assume that, the fraction part will be proper. As Ourous said, no simplification involved. – Stephen – 2018-07-25T01:07:53.580

@JonathanAllan the idea is that you remove the space and see if it is still equal - yes you can basically do what xnor described in his comment, but I'm not going to just give you two integers – Stephen – 2018-07-25T01:09:56.357

This is probably also inherent in the definition of mixed fractions, but can we assume that the integer part is > 0? If not, most answers are probably invalid. – wastl – 2018-07-25T13:07:49.963

@wastl yes, you can make that assumption, you can also assume the numerator is > 0 – Stephen – 2018-07-25T13:13:41.210

Answers

8

MATL, 7 bytes

UsGXzU=

Input is a string. Output is 1 for truthy, 0 for falsey.

Try it online! Or verify all test cases.

Explanation

U     % Implicit input. Convert to number(s): gives a vector of two numbers
s     % Sum of that vector
G     % Push input again
Xz    % Remove spaces
U     % Convert to number
=     % Equal? Implicit display

Luis Mendo

Posted 2018-07-24T22:26:59.847

Reputation: 87 464

8

Perl 6, 16 12 bytes

{1+$_==1~$_}

Try it online!

Takes input as a string representing the fraction. Turns out Perl 6's dynamic typing can handle strings to rational fractions, who knew? So the string "1/10" when coerced to a number, returns 0.1

The anonymous code block simply checks if the fraction plus one equals one concatenated with the fraction. Thanks to xnor's Python answer for showing me that the integer part doesn't matter.

Old solution, 27 26 bytes

{.nude[0]==.Int~[%] .nude}

Try it online!

Takes input as a rational mixed fraction, and returns true or false. Returns false for the fourth test case because it can be simplified.

Explanation:

.nude returns a list of [numerator, denominator].

{                        } # Anonymous code block
 .nude[0]    # Check if the numerator of the mixed fraction
         ==  # Is equal to
           .Int  # The integer part of the fraction
               ~ # Concatenated to
                [%] .nude  # The numerator modulo the denominator
                           # And return implicitly

Jo King

Posted 2018-07-24T22:26:59.847

Reputation: 38 234

7I'm guessing that .nude is named for numerator + denominator, but someone probably took great pleasure in being able to call it that. – Οurous – 2018-07-24T23:42:03.807

1I was going to go with something that took it as a single string '1 3/10' {S/\s//==.words.sum} – Brad Gilbert b2gills – 2018-07-25T00:20:39.193

6

Retina 0.8.2, 17 16 bytes

(.)+/1(?<-1>0)*$

Try it online! Requires just the fraction part, so the linked test suite removes the integer from the test cases. Explanation: The improper concatenation is equal to the mixed number only if the denominator is a power of 10 and the numerator has one digit for every zero in the denominator. .NET's balancing groups are used to verify that sufficient digits exist. Edit: Saved 1 byte thanks to @sundar.

Neil

Posted 2018-07-24T22:26:59.847

Reputation: 95 035

Doesn't work for 1 11/10. It seems to be a problem with your implementation, not the method – H.PWiz – 2018-07-24T23:14:17.547

1Note that " You may drop the integer part (not take it as input) if you do not need to use it." - so the leading space may be unnecessary if you change the input to have only the fraction. – sundar - Reinstate Monica – 2018-07-24T23:21:34.130

1@H.PWiz I don't think we have to deal with inputs where the numerator is greater than the denominator (since these are supposed to be mixed fractions with only the non-integral decimal part expressed as a fraction). But I'll ask the OP to confirm that. – sundar - Reinstate Monica – 2018-07-24T23:23:59.243

@sundar I'd have to change it to ^ instead, so it doesn't help. – Neil – 2018-07-24T23:54:14.803

The / makes it unambiguous what you're matching, so I don't think you need the anchor there (going by usual regex matching rules, no Retina expertise here). Seems to work anyway: Try it online!.

– sundar - Reinstate Monica – 2018-07-24T23:56:28.413

@sundar Ah, of course, because I want to match the whole numerator anyway, so it's easier if I don't have the integer part getting in my way, thanks! – Neil – 2018-07-25T00:00:05.217

6

Husk, 8 bytes

§·=r¤+r+

Try it online!

Explanation

§(·=r)(¤+r)(+)  -- example arguments: "1" "3/10"
§               -- fork both arguments
      (¤ r)     -- | read both: 1 3/10
      ( + )     -- | and add them: 13/10
           (+)  -- | concatenate: "13/10"
                -- and do
 (· r)          -- | read the second argument: 13/10
 ( = )          -- | and compare: 13/10 == 13/10
                -- : 1

ბიმო

Posted 2018-07-24T22:26:59.847

Reputation: 15 345

5

R, 78 65 bytes

function(n,e=function(a)eval(parse(t=sub(" ",a,n))))e("")==e("+")

Try it online!

-13 bytes thanks to both Giuseppe and JayCe!

Robert S.

Posted 2018-07-24T22:26:59.847

Reputation: 1 253

1Just sub is fine here. Also, you can use t= instead of text= – Giuseppe – 2018-07-25T12:04:28.547

1

What can I say? Brilliant! it simplifies nicely to 65 bytes

– JayCe – 2018-07-25T13:48:56.780

@JayCe Glad to see I was on the right page! Thanks! – Robert S. – 2018-07-25T14:41:21.173

You can try porting xnor's Python 3 answer for probably 20 bytes...

– JayCe – 2018-07-26T15:09:32.637

@JayCe one of your favorite tricks!

– Giuseppe – 2018-08-04T20:16:41.650

@Giuseppe really nice! – JayCe – 2018-08-04T23:24:10.490

5

Python 2, 43 bytes

lambda k:k.split('/')[1]==`10**k.find('/')`

Try it online!

Chas Brown

Posted 2018-07-24T22:26:59.847

Reputation: 8 959

4

Stax, 5 bytes

╡ÄLσ`

Run and debug it

Explanation:

+yj$e= Full program, implicit input
+      Add integer and fraction part
 y     Push unparsed input
  j$   Split on spaces and flatten, i.e. Remove spaces
    e  Evaluate
     = Check for equality

wastl

Posted 2018-07-24T22:26:59.847

Reputation: 3 089

4

Brachylog, 15 bytes

ḍ{lᵛ&ht¬ị&t↔ị1}

Try it online!

Takes the fractional part alone as a string input.

Indirectly uses the same idea as my Julia answer - "the denominator is 10^{length of numerator}" can be said as "the denominator is a power of ten, and the length of the denominator is equal to the length of the numerator + the length of "/" (i.e. 1).

ḍ                   % split the input in half
 {            }     % and verify that
  lᵛ                % each half has the same length (i.e. the string had even length)
    &ht¬ị           % and the last character of the first half is 
                    %  not a number (it should be "/")
         &t↔ị1      % and the second half when reversed is the number 1
                    %  i.e. the denominator should be a power of 10

Older answer:

15 20 bytes

a₀ᶠịˢtl;10↺^.&a₁ᶠịˢh

Try it online!

(-1 byte thanks to @Fatalize, but unfortunately +6 bytes since I discovered bugs in the older method.)

Same idea as my Julia answer.

sundar - Reinstate Monica

Posted 2018-07-24T22:26:59.847

Reputation: 5 296

1You can shorten it by 1 byte by replacing variable A with the output variable . (and thus remove the last A because the output variable is implicitely there at the end) – Fatalize – 2018-07-26T06:52:30.427

@Fatalize Thanks, I forget that the output is pretty much available as a free variable in these decision problems. Unfortunately I found bugs in the code I had: since it only asked for any numeric prefix and any numeric suffix, it was passing things like 61/10 (using just 6 as the numerator/prefix) 2/110 (using just 10 as the denominator/suffix). I've tried to fix it, not sure if this is the best way to do it though. – sundar - Reinstate Monica – 2018-07-26T14:09:11.737

I'm not sure I can help you because I don't understand the specs of this challenge at all, even after reading it 3 times. I don't know what "mixed fractions and improper fractions" are called in my country, or if it's even taught in elementary schools here. – Fatalize – 2018-07-26T14:41:47.460

1@Fatalize Fair enough. Do you have interest in reviving the chat room? I have a bunch of questions to pester you with, if you're interested and have the time. – sundar - Reinstate Monica – 2018-07-27T17:34:32.040

Sure, just ask a mod to revive the room and tag me when you ask questions – Fatalize – 2018-07-30T06:37:22.563

4

Python 3, 26 bytes

lambda k:eval(k+'+1==1'+k)

Try it online!

For example, input 3/4 gives 3/4+1==13/4. Instead of taking the whole part of the fraction, we just set it to 1 to test the mixed fraction equality. Test cases from Chas Brown.

xnor

Posted 2018-07-24T22:26:59.847

Reputation: 115 687

3

Clean, 57 bytes

import StdEnv,Text
$b#[u,v:_]=split"/"b
=v==""<+10^size u

Try it online!

This one is a little shorter but breaks for large numerator/denominators.

Clean, 77 61 60 58 bytes

-1 thanks to OMᗺ's tip on my other answer

import StdEnv,Text
$b#[u,v:_]=split"/"b
=1<+[48\\_<-:u]==v

Try it online!

This uses Neil's method, it's a bit shorter than doing it directly.
There's some trickery with conversion overloading, where 1<+[48\\_<-:u] converts [Int] to [Char] and then to {#Char} (:== String), but Int directly to String.

Clean, 91 89 bytes

import StdEnv,Text
t=toInt
$a b#[b,c:_]=map t(split"/"b)
#d=t(a<+b)
=d/c==t a&&d-d/c*c==b

Try it online!

Defines a function $ :: String String -> Bool which extracts the numerator and denominator, string-concatenates the integer part and the numerator, and checks equivalence.

Οurous

Posted 2018-07-24T22:26:59.847

Reputation: 7 916

3

JavaScript, 26 bytes

Takes input in currying syntax (f(x)(y)) where x is the integer and y is the fraction as a string.

x=>y=>x==eval(x+y)-eval(y)

Try it online

Shaggy

Posted 2018-07-24T22:26:59.847

Reputation: 24 623

3

Julia 0.6, 29 bytes

r->10^ndigits(num(r))==den(r)

Try it online!

Based on the idea that the output should be true only when the denominator is a power of ten with as many zeros as there are digits in the numerator. Takes input as a Rational type, checks that the denominator is equal to 10 raised to the number of digits in the numerator.

sundar - Reinstate Monica

Posted 2018-07-24T22:26:59.847

Reputation: 5 296

3

Java 10, 107 70 67 57 bytes

f->new Long(f.split("/")[1])==Math.pow(10,f.indexOf("/"))

Welcome to the world without eval..

-40 bytes by creating a port of @ChasBrown's Python 2 answer.
-10 bytes thanks to @Shaggy (I should have read @ChasBrown's answer better and his use of find (indexOf)..)

Try it online.

Explanation:

f->                         // Method with String parameter and boolean return-type
  new Long(f.split("/")[1]) //  Take the denominator as integer
  ==Math.pow(10,            //  And check whether it is equal to 10 to the power of:
                f.indexOf("/"))
                            //   the length of the numerator-String

Kevin Cruijssen

Posted 2018-07-24T22:26:59.847

Reputation: 67 575

157 bytes – Shaggy – 2018-07-25T12:26:21.150

@Shaggy Ah, Chas Brown even has the same in the Python 2 answer that I linked.. Not sure why I didn't already use that.. Thanks! – Kevin Cruijssen – 2018-07-25T12:29:22.200

3

05AB1E, 7 bytes

'/¡ćg°Q

Only takes the fractions as input.

Try it online or verify all test cases.

Explanation:

'/¡        # Split the input by '/'
           #  i.e. '3/10' → ['3', '10']
   ć       # Head extracted:
           #  i.e. ['3', '10'] → 10 and 3
    g      # Take the length of the numerator
           #  i.e. '3' → 1
     °     # Take 10 to the power of this length
           #  1 → 10**1 → 10
      Q    # Check if that value equals the denominator
           #  10 and 10 → 1 (truthy)

Or a more general explanation:

We have to validate two things:

  • Is the denominator a factor 10 (1, 10, 100, 1000, etc.)?
  • Does the length of the numerator + 1 equal the length of the denominator?
    • This second part is done by checking if the denominator as is, is equal to 10 to the power of the length of the numerator, which saves 2 bytes

PS: If we could take the numerator and denominator as separated inputs, just 3 bytes would have been enough: g°Q.

Kevin Cruijssen

Posted 2018-07-24T22:26:59.847

Reputation: 67 575

2

Haskell, 47 40 bytes

-7 thanks to OMᗺ

f b|(u,v)<-span('/'<)b="/1"++('0'<$u)==v

Try it online!

A port of my Clean answer using Neil's method.

Οurous

Posted 2018-07-24T22:26:59.847

Reputation: 7 916

2

Perl 5 -p, 23 bytes

$_=eval=~s/..//r eq$_+0

Try it online!

Takes the fractional part alone as input (as allowed by OP), outputs 1 for true and nothing for false.

$_=       # assign to be printed by -p
eval      # evaluate fraction to get 0.something, for eg. 0.54
=~s/..//r # remove the 0. part, giving 54
 eq       # string equality check
$_+0      # after first coercing input to number to remove / and denominator

The decimal part taken by itself would be exactly equal to the numerator only when the denominator is the immediate next power of ten greater than the numerator, which is the condition we need to check for.

sundar - Reinstate Monica

Posted 2018-07-24T22:26:59.847

Reputation: 5 296

2

Noether, 17 bytes

I#I"/"^WL1-%WL_=P

Try it online!

Explanation

So how does this work? Well, if you look at the test cases, the only true cases are when the denominator is a power of ten, \$10^a\$, where \$a\$ is the length of the numerator plus one (\$a = \lfloor \log_{10} n \rfloor + 1\$, where \$n\$ is the numerator and \$\lfloor x \rfloor\$ represents the flooring function).

I#                - Push the first input then pop it off the stack
  I"/"^           - Push the second input and split the string at "/"
       W          - Convert the top (the denominator) of the stack from a string to a number
        L1-       - Take the log 10 of the top of the stack and subtract 1 (a)
           %      - Rotate the stack
            WL_   - Convert the top of the stack (the numerator) to a number, take the log10 and floor the result (b)
               =  - Check if a and b are equal
                P - Print the top of the stack

Beta Decay

Posted 2018-07-24T22:26:59.847

Reputation: 21 478

2

TeaScript, 25 bytes

First input is the fraction, second is the integer.

I just started in TeaScript so it might be golfed down a lot

s"/")▒⌐ep(xs"/")░.n

Try it online!

Luis felipe De jesus Munoz

Posted 2018-07-24T22:26:59.847

Reputation: 9 639

2

R, 53 bytes

function(n,x=el(strsplit(n,"/")))x[2]==10^nchar(x[1])

Try it online!

Takes only the fractional part as input. As mentioned by xnor in a comment:

This seems to be equivalent to "given an input that doesn't matter and two numbers as a string separated by a slash, determine whether the second number equals 10 to the power of the length of the first number".

Robert S.'s answer is less golfy but much more interesting than mine.

JayCe

Posted 2018-07-24T22:26:59.847

Reputation: 2 655

1

Excel, 52 bytes

=10^FIND("/",B1)/10-MID(B1,FIND("/",B1)+1,LEN(B1))=0

Ignores the Integer input. Basically: IS Denominator = 10^LEN(Numerator)


For denominators limited to <10^9: 48 bytes:

=10^FIND("/",B1)/10-MID(B1,FIND("/",B1)+1,1E9)=0

Bulk of logic is splitting on /. If input could be taken separately, 16 bytes:

=10^LEN(B1)-C1=0

Wernisch

Posted 2018-07-24T22:26:59.847

Reputation: 2 534

1

Elixir, 81 bytes

fn b->[n,d]=String.split b,"/";String.to_integer(d)==:math.pow 10,byte_size n end

Try it online!

Might be able to get somewhere with {n,"/"<>d}=Integer.parse b, but not sure how.

Okx

Posted 2018-07-24T22:26:59.847

Reputation: 15 025

1

C (gcc/clang), 59 49 47 bytes

f(a,b){a=atoi(b=strchr(a,47)+1)==pow(10,b+~a);}

Port of Chas Brown's Python 2 answer. Try it online here.

Ignores the integer part of the input. Thanks to Jonathan Frech for golfing 2 bytes.

Ungolfed:

f(a, b) { // function returning an int and taking a string as input; also declaring another string variable
          // this abuses the parameters as much as possible, omitting the type int and implicitly converting it to char *
    a =                             // return a truthy or falsey value based on
        atoi(b = strchr(a, 47) + 1) // the denominator (as integer; 47 is the ASCII code for '/')
        == pow(10, b + ~a);         // is equal to 10 to the power of the length of the numerator-string
}

O.O.Balance

Posted 2018-07-24T22:26:59.847

Reputation: 1 499

'/' can most likely be 47. – Jonathan Frech – 2018-07-25T14:36:37.180

Possible 47 bytes.

– Jonathan Frech – 2018-07-25T14:40:20.807

Nice, thank you! – O.O.Balance – 2018-07-25T15:02:54.037

You are welcome. I think you forgot to update your header to reflect the new byte count. – Jonathan Frech – 2018-07-25T15:35:46.500

1

2DFuck, 86 bytes

..!x..!...,,,,[>,,,,,,,,],,,,,,,,,[v!],[v!],[v!],![v!],,,[,,[v!],[v!],[v!],[v!]<,,,]r.

Try it online!

Takes input without integer part.

wastl

Posted 2018-07-24T22:26:59.847

Reputation: 3 089

1

ForceLang, 86 78 bytes

set a io.readnum()
set b io.readnum()
set d number.parse a+b+""
io.write d=a+b

SuperJedi224

Posted 2018-07-24T22:26:59.847

Reputation: 11 342