15
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Should or can the fraction be simplified? Like the fourth test case would be false as 54/100 simplifies to 27/50 \$\endgroup\$ – Jo King Jul 24 '18 at 22:45
  • 1
    \$\begingroup\$ Should output be two distinct, consistent values or any, possibly inconsistent, truthy/falsey values? \$\endgroup\$ – Luis Mendo Jul 24 '18 at 22:53
  • 1
    \$\begingroup\$ By 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. \$\endgroup\$ – sundar Jul 24 '18 at 23:44
  • 3
    \$\begingroup\$ "you may not take the fraction as input in two pieces" - err why? That is exactly what the / does :/ \$\endgroup\$ – Jonathan Allan Jul 24 '18 at 23:54
  • 11
    \$\begingroup\$ 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". \$\endgroup\$ – xnor Jul 25 '18 at 0:15

25 Answers 25

8
\$\begingroup\$

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
\$\endgroup\$
8
\$\begingroup\$

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
\$\endgroup\$
  • 7
    \$\begingroup\$ I'm guessing that .nude is named for numerator + denominator, but someone probably took great pleasure in being able to call it that. \$\endgroup\$ – Οurous Jul 24 '18 at 23:42
  • 1
    \$\begingroup\$ I was going to go with something that took it as a single string '1 3/10' {S/\s//==.words.sum} \$\endgroup\$ – Brad Gilbert b2gills Jul 25 '18 at 0:20
6
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Doesn't work for 1 11/10. It seems to be a problem with your implementation, not the method \$\endgroup\$ – H.PWiz Jul 24 '18 at 23:14
  • 1
    \$\begingroup\$ Note 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. \$\endgroup\$ – sundar Jul 24 '18 at 23:21
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – sundar Jul 24 '18 at 23:23
  • \$\begingroup\$ @sundar I'd have to change it to ^ instead, so it doesn't help. \$\endgroup\$ – Neil Jul 24 '18 at 23:54
  • \$\begingroup\$ 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!. \$\endgroup\$ – sundar Jul 24 '18 at 23:56
6
\$\begingroup\$

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
\$\endgroup\$
5
\$\begingroup\$

Python 2, 43 bytes

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

Try it online!

\$\endgroup\$
5
\$\begingroup\$

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ Just sub is fine here. Also, you can use t= instead of text= \$\endgroup\$ – Giuseppe Jul 25 '18 at 12:04
  • 1
    \$\begingroup\$ What can I say? Brilliant! it simplifies nicely to 65 bytes \$\endgroup\$ – JayCe Jul 25 '18 at 13:48
  • \$\begingroup\$ @JayCe Glad to see I was on the right page! Thanks! \$\endgroup\$ – Robert S. Jul 25 '18 at 14:41
  • \$\begingroup\$ You can try porting xnor's Python 3 answer for probably 20 bytes... \$\endgroup\$ – JayCe Jul 26 '18 at 15:09
  • \$\begingroup\$ @JayCe one of your favorite tricks! \$\endgroup\$ – Giuseppe Aug 4 '18 at 20:16
4
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ You 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) \$\endgroup\$ – Fatalize Jul 26 '18 at 6:52
  • \$\begingroup\$ @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. \$\endgroup\$ – sundar Jul 26 '18 at 14:09
  • \$\begingroup\$ 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. \$\endgroup\$ – Fatalize Jul 26 '18 at 14:41
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – sundar Jul 27 '18 at 17:34
  • \$\begingroup\$ Sure, just ask a mod to revive the room and tag me when you ask questions \$\endgroup\$ – Fatalize Jul 30 '18 at 6:37
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ 57 bytes \$\endgroup\$ – Shaggy Jul 25 '18 at 12:26
  • \$\begingroup\$ @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! \$\endgroup\$ – Kevin Cruijssen Jul 25 '18 at 12:29
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

C (gcc), 67 56 55 bytes

m;x(char*_){for(m=1;*_++;m*=10)*_*=*_^47;m=m==atoi(_);}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 64 bytes: tio.run/… \$\endgroup\$ – O.O.Balance Jul 25 '18 at 12:49
  • \$\begingroup\$ Actually, do you even need r? tio.run/… \$\endgroup\$ – O.O.Balance Jul 25 '18 at 12:51
  • \$\begingroup\$ @O.O.Balance No I do not. Thank you. \$\endgroup\$ – Jonathan Frech Jul 25 '18 at 14:31
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

2DFuck, 86 bytes

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

Try it online!

Takes input without integer part.

\$\endgroup\$
1
\$\begingroup\$

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
}
\$\endgroup\$
  • \$\begingroup\$ '/' can most likely be 47. \$\endgroup\$ – Jonathan Frech Jul 25 '18 at 14:36
  • \$\begingroup\$ Possible 47 bytes. \$\endgroup\$ – Jonathan Frech Jul 25 '18 at 14:40
  • \$\begingroup\$ Nice, thank you! \$\endgroup\$ – O.O.Balance Jul 25 '18 at 15:02
  • \$\begingroup\$ You are welcome. I think you forgot to update your header to reflect the new byte count. \$\endgroup\$ – Jonathan Frech Jul 25 '18 at 15:35
1
\$\begingroup\$

ForceLang, 86 78 bytes

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.