-2

Problem Statement: An array is accepted if and only if it has the following structure:

  • First a1 elements equal 1.
  • Next a2 elements equal 2.
  • Next a3 elements equal 3.
  • Next a4 elements equal 4.
  • Next a5 elements equal 5.
  • Next a6 elements equal 6.
  • Next a7 elements equal 7.

Where:

  • a(i) can be any non-zero positive integer.
  • There are no other elements in array.

Even though the algorithm for this problem seems pretty easy to implement, I am having some difficulty with the code. Here is what I have written.

print("Enter list: ")
arr = [int(x) for x in input().split()]
print(arr)
i = 0

if arr[0] == 1:  # to check whether input starts with a 1. 
    if arr[i] == 1:  #If next number is also 1, then
        while arr[i] == 1:  #while numbers are equal to 1, increment i
            i = i + 1
            if arr[i] == 2: #If next number is 2, then
                while arr[i] == 2: #same procedure as above ... and so on
                    i = i + 1
                    if arr[i] == 3:
                        while arr[i] == 3:
                            i = i + 1
                            if arr[i] == 4:
                                while arr[i] == 4:
                                    i = i + 1
                                    if arr[i] == 5:
                                        while arr[i] == 5:
                                            i = i + 1
                                            if arr[i] == 6:
                                                while arr[i] == 6:
                                                    i = i + 1
                                                    if arr[i] == 7:
                                                        while arr[i] == 7:
                                                            i = i + 1
                                                        if arr[-1] == 7: #to check if last number is a 7
                                                            print("Accepted")
                                                        else:
                                                            print("not")
                                                    else:
                                                        print("not")
                                            else:
                                                print("not")
                                    else:
                                        print("not")
                            else:
                                print("not")
                    else:
                        print("not")
            else:
                print("not")
    else:
        print("not")
else:
    print("not")

I seem to be getting some kind of indexing error where it says:

   while arr[i] == 7:
IndexError: list index out of range

I don't understand why I am encountering this error. As far as I can tell, I am not exceeding the list index.

Anders
  • 8,307
  • 9
  • 56
  • 88
Sakshi Jain
  • 37
  • 1
  • 1
  • 4
  • Welcome to Stack Overflow - nice to have you. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) to help keeping Stack Overflows content on the highest possible level and increase your chances getting an appropriate answer. – Axel Oct 11 '17 at 00:51
  • I'd have to say that this looks like highly redundant code. I am pretty sure you can simplify all of this in just a few lines. To give you a hint, the number you are expecting to find correlates very close to the position in the array you expect it to be. –  Oct 11 '17 at 10:02
  • Possible duplicate of [How to prevent ArrayIndexOutOfBoundsException in Java?](https://stackoverflow.com/questions/5554734/how-to-prevent-arrayindexoutofboundsexception-in-java) – EJoshuaS - Stand with Ukraine Jan 03 '19 at 04:31
  • Dupe target is for Java, but the solution isn't all that different in Python. – EJoshuaS - Stand with Ukraine Jan 03 '19 at 04:32

2 Answers2

1

You can try this:

def is_accepted(arr):
    return arr[0] == 1 and all(arr[i] -1 == arr[i-1] or arr[i] == arr[i-1] for i in range(1, len(arr)))

print(is_accepted([1, 2, 3, 4, 5, 6, 7]))

Output:

True

Second List:

s = [1, 2, 3, 4, 5, 5, 4, 3, 5, 6, 7]
print(is_accepted(s))

Output:

False

Last input:

s = [1, 1, 2, 2, 3, 3, 3, 4, 5, 6, 6, 7]
print(is_accepted(s))

Output:

True
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • `Enter list: 1 2 3 4 5 5 4 3 5 6 7 [1, 2, 3, 4, 5, 5, 4, 3, 5, 6, 7] True` It shows True for the wrong pattern too. – Sakshi Jain Oct 10 '17 at 19:37
  • @SakshiJain When I run that list you just posted, the output is False. – Ajax1234 Oct 10 '17 at 19:38
  • It doesn't work on redundant numbers. Say if I input a string `s = [1, 1, 2, 2, 3, 3, 3, 4, 5, 6, 6, 7]` according to the problem statement, it is a valid input. But the program says otherwise. – Sakshi Jain Oct 10 '17 at 19:52
  • @SakshiJain So you are saying that redundant numbers are acceptable in this problem? – Ajax1234 Oct 10 '17 at 19:53
  • I think the solution to that would be adding an if statement like `if arr[i] == arr[i-1] or arr[i] == arr[i-1]+1` instead of your original condition. – Sakshi Jain Oct 10 '17 at 19:53
  • Yes. It says in the problem statement that a(i) elements equal to number. Where a(i) could be any non-zero number. – Sakshi Jain Oct 10 '17 at 19:55
0

A fun exercise in recursion.

Write cases the following conditions:

  • list is empty

  • first item of list less than last_checked as a short-circuit

  • match recursively.

def match(lst, last_checked=1):
  if lst == []: return True # base case

  if lst[0] < last_checked:
    return False

  return True and match(lst[1:], lst[0])

Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81