1

I'm new to coding and am using Codingbat to learn python basics. I'm wondering about the difference between the two methods of sequence matching in python arrays below. I get that the difference might be arbitrary but just wanted to know if their solution is superior for any reasons.

Thanks

Given an array of ints, return True if the sequence of numbers 1, 2, 3 appears in the array somewhere.

my solution

  for i in range(len(nums)):
    if nums[i:i+3] == seq:
      return True

their solution:

for i in range(len(nums)-2):
    if nums[i]==1 and nums[i+1]==2 and nums[i+2]==3:
      return True

3 Answers 3

2

First difference is that in your solution you are comparing a list slice(nums[i:i+3]) and the target list (seq) whereas the given solution is integer comparison.

Secondly, your solution has the for loop that iterates through all the elements of the "nums" array but for example if till last 3rd element, no '1'(first element of the sequence) is found, checking the last two elements would always be waste. The given solution on the other hand iterates two times less than entire sequence by giving range(len(nums)-2). This is a bit optimized solution than the one you have given.

nums=[5,6,7,2,3,1]
seq=[1,2,3]
def sequence(nums):
    for i in range(len(nums)):
        print(nums[i])
        if nums[i:i+3]==seq:
            return True
print(sequence(nums))

Output: 5 6 7 2 3 1 None

Given solution:

nums=[5,6,7,2,3,1]
seq=[1,2,3]

def sequence2(nums):
    for i in range(len(nums)-2):
        print(nums[i])
        if nums[i]==1 and nums[i+1]==2 and nums[i+2]==3:
            return True

print(sequence2(nums))

Output: 5 6 7 2 None

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

1 Comment

This is a good answer. I suggest describing nums[i:i+3] as a list slice rather than a list, and removing the backticks from the examples.
0

If using your solution, when checking the last int in the list. It may have an error of index out of range. Be careful of these minor mistakes.

2 Comments

When I ran my solution in their console it passed all test cases, which is why I'm wondering if there's a meaningful difference between the two solutions. Thanks
Slices don't give index errors. For example: [1,2][0:10] just returns [1, 2].
0

There's no such big difference. One difference is that your loop, in the last values, iterates over an out of range item, and their solution iterates until the last three elements: For example with this nums=[1,4,6,1,2,3] and seq=[1,2,3], i is going to take values between 0 and len(nums)-1, that is to say in this case i=[0,1,2,3,4,5], and your solution iterations will be something like this:

i=0
nums[i:i+3]=nums[0:3]->[1,4,6]
nums[0:3] == seq ->False
...
i=1
nums[i:i+3]=nums[1:4] ->[4,6,1]
nums[i:i+3] == seq ->False
....
#and so on

But after the last three element (i=len(nums)-3), it doesn't make sense you continue comparing because lists are going to be with different length:

i=len(nums)-3=3
nums[i:i+3]=nums[3:6] ->[1,2,3]
nums[i:i+3] == seq ->True
return True
...
i=len(nums)-2=4
nums[i:i+3]=nums[4:7] ->[2,3]
nums[i:i+3] == seq ->False
...
i=len(nums)-1=5
nums[i:i+3]=nums[5:8] ->[3]
nums[i:i+3] == seq ->False
...
#End loop

So maybe you could do it with range(len(nums)-2), the last iteration i=len(nums)-3. The other solution, as I explain you, iterates until i=len(nums)-3, and compare item by item:

i=0
nums[i]=nums[0]-> 1
nums[i+1]=nums[1]-> 4
nums[i+1]=nums[2]-> 6
nums[i]==1 and nums[i+1]==2 and nums[i+2]==3 --->False
...
#and so on until i i=len(nums)-3

So in short, the differences are loops and that you compare slice by slice, and their solution compares item by item.

Comments

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.