0

The goal is to check if it is one off. If, for instance, the winning ticket is 1234, and my ticket is 1233, or 2234, or 1334 that should pass as true, as the ticket is one off. Non regex involved please as this was for beginners.

If it is exact, then it fails. If it is more than one number off, it fails. This is what I have so far.

Here is my function:

require "minitest/autorun"
require_relative "offbyone.rb"

class TestLottoFunction < Minitest::Test

def test_a_matching_ticket_returns_false
        my_ticket = "1234"
        winning_tickets = "1234"
        assert_equal(false, off(my_ticket, winning_tickets))
    end

def test_a_one_off_ticket
    my_ticket = "1234"
    winning_tickets = "1235"
    assert_equal(true, off(my_ticket, winning_tickets)) 
end

end

I tried to spell it out in a more complicated way of what my goal was exactly, seeing if that would work, and it didn't. I got the same error message, telling me it is line 9 and 15, telling me that the function is expecting either true or false, and it is receiving nil. This is the code that I have.

def off(my_ticket, winning_tickets)
    i = 0
    c = 0

4.times do

    if winning_tickets[0][i] == my_ticket[0][i]
        c+=1
    end

    i+=1

    end
if c==3
    true
end
end

I just changed

 if winning_tickets[i] == my_tickets [i]

to include

winning_tickets[0][i] == my_tickets [0][i] 

and it got rid of one error, but created a new one.

If I don't include the [0][i], I get an error message saying that I have 2 errors, both on the assert_equal lines, both saying they are receiving nil instead of true or false.

If I add the [0][i] to both lines, I get 1 error, saying my error was in the matching ticket, saying it was expecting true and received false.

Any help with this?

I am actually curious as to why I am receiving nil. Where in the array am I going wrong to get nil. Please point out my mistakes.

1
  • Your question is unclear. You repeatedly ask about arrays, but there isn't a single array in your entire code. Commented Sep 7, 2016 at 7:34

2 Answers 2

1

I understand we are given two non-negative numbers, n and m, and wish to determine if n and m have the same number of digits, and among the pairs of correspond digits of the two numbers, whether exactly one pair of digits differs by exactly one and all other pairs are equal. For example, 124 and 134 are "one off", but 124 and 12 and 124 and 135 are not.

You could do the following.

def one_off?(n,m)
  nstr, mstr = n.to_s, m.to_s
  return false unless nstr+mstr =~ /\A\d+\z/ && nstr.size == mstr.size
  nstr.each_char.zip(mstr.chars).reduce(0) { |tot, (ns, ms)|
    tot + (ns.to_i - ms.to_i).abs } == 1
end

one_off?(1234, 1233) #=> true 
one_off?(124, 134)   #=> true 
one_off?(124, 124)   #=> false 
one_off?(124, 135)   #=> false 
one_off?(124, 1245)  #=> false 
one_off?(124, -12)   #=> false 

See Enumerable#zip and Enumerable#reduce (aka inject).

Sign up to request clarification or add additional context in comments.

Comments

1

Using Integer Comparisons

There's some ambiguity in your fixture data and in the definition of what you mean by "off by one." Assuming that you mean an integer that can be +1 or -1 from the target integer, then you may want to do a comparison of Integers rather than indexing into a String. For example:

def off_by_one? winner, mine
  winner.succ == mine or winner.pred == mine ? true : false
end

off_by_one? 1234, 1234
#=> false

off_by_one? 1234, 1233
#=> true

off_by_one? 1234, 1235 
#=> true

off_by_one? 1234, 1254
#=> false

This will work even in boundary conditions where the length of the integers don't match. For example:

off_by_one? 1000, 999
#=> true

Of course, if your business rules are more complex then you may need a different solution.

1 Comment

sorry, i meant any number. So 1234 could be 2234 or 1224 and it would hopefully return true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.