Sort by

recency

|

60 Discussions

|

  • + 0 comments

    My Python3 solution

    def solve(a, b, c):
        yes, no = "YES", "NO"
        a, b = max(a, b), min(a, b)
    
        b_states = {0, b}
        a_states = {0, a}
        b_state = b_states.copy()
        a_state = a_states.copy()
        if c in a_states or c in b_states:
            return yes
        search = True
        while search:
            for jug_a in a_state:
                for jug_b in b_state:
                    
                    a1, a2 = min(a, jug_a + jug_b), max(0, jug_a - (b - jug_b))
                    b1, b2 = min(b, jug_b+jug_a), max(0, jug_b - (a - jug_a))
                    
                    if c in {a1, a2, b1, b2}:
                        return yes
            
                    a_states.add(a1)
                    a_states.add(a2)
                    
                    b_states.add(b1)
                    b_states.add(b2)
        
            if a_states == a_state and b_states == b_state:
                break
        
    
            b_state = b_states.copy()
            a_state = a_states.copy()
    
        # print(a_states, b_states)
        return no
    
  • + 0 comments

    I first solved this problem with backtracking (which works great for games, or procedures with limited next steps available), but using the gcd was a lot simpler - I didn't think about it for a while, though. So there are a few ways to solve this problem!

  • + 1 comment
    def solve(a, b, c):
        hcf=math.gcd(a,b)
        if c<=max(a,b) and c%hcf==0:
              return "YES"
        return "NO"
    
  • + 1 comment

    I cannot understand the question,

    Bruce can do the following, fill jug a with 5 gallons. a = 5, b = 0 Now, he can fill jug b with 3 gallons from jug a. a = 2, b = 3 He can empty jug b and empty 2 gallons from jug a to jug b. a = 0, b = 2 Now, he can fill jug a with 5 gallons and fill jug b with 1 gallon from jug a. This results in jug a containing exactly 4 gallons of water. a = 5, b = 2

    why he made it so complex, why not just pour 4 galon at the begin in a, then it is done, a = 4, b =

  • + 0 comments

    Two line solution. If you want know how read this