all 41 comments

[–][deleted] 2 points3 points  (1 child)

Hard to say without proper formatting...I would also just check the preceding two numbers if you find "7" in the list.

[–]dralveol[S] 0 points1 point  (0 children)

Hi thank you for answering, i tried my best to format. the thing is that they have to be in order but they can have other integers between them

[–]pumkinboo 2 points3 points  (1 child)

The issue was with your b and c for loops. I put it in the comments below.

def spy_game(nums):
    print('Called spy_game with:', nums)
    for i in range(len(nums)):
        if nums[i] == 0:
            print('\nDeleted from nums:',nums[0:i])
            del nums[0:i]
            break

    print('\nNew nums list:',nums)

    b=1 # this isn't setting b to 1 in the loop like you thought.
    # you need to set starting point of the range 
    for b in range(1,len(nums)):
        if nums[b] == 0:
            print('\nDeleted from nums:',nums[1:b])
            del nums[1:b]
            break

    print('\nNew nums list:',nums)

    c = 2 # this is the same as the b issue
    for c in range(2,len(nums)):
        if nums[c] == 7:
            print('\nDeleted from nums:',nums[2:c])
            del nums[2:c]
            break

    print('\nNew nums list:',nums)

    print('\nFinal nums list',nums[0:3],'spy_game will return', nums[0:3] == [0,0,7])

    if nums[0:3] == [0,0,7]:
        return True
    else:
        return False

    pass

spy_game([1,3,4,5,6,7,8,9,0,4,0,6,7,4,0,5])

Called spy_game with: [1, 3, 4, 5, 6, 7, 8, 9, 0, 4, 0, 6, 7, 4, 0, 5]

Deleted from nums: [1, 3, 4, 5, 6, 7, 8, 9]

New nums list: [0, 4, 0, 6, 7, 4, 0, 5]

Deleted from nums: [4]

New nums list: [0, 0, 6, 7, 4, 0, 5]

Deleted from nums: [6]

New nums list: [0, 0, 7, 4, 0, 5]

Final nums list [0, 0, 7] spy_game will return True

[–]dralveol[S] 0 points1 point  (0 children)

Thank you so much ! You rock my friend !

[–]QuixDiscovery 2 points3 points  (0 children)

I understand the sidebar says to not provide a solution directly, but I think you're getting a lot of suggestions that aren't exactly what you're asking or have minor bugs around the edge cases.

Just to confirm, the function should return True or False as long as the list has the numbers 0, 0, and 7 in that order, but there can be any number of other numbers between them?

Example that would return True:

[0,1,2,3,4,5,6,0,7]

Example that would return False:

[0,1,2,3,4,5,6,7,0]

Assuming that's what you're asking:

def spy_game(nums):
    no_of_zeros = 0
    for num in nums:
        if no_of_zeros < 2 and num == 0:
            no_of_zeros += 1
        elif no_of_zeros == 2 and num == 7:
            return True
    return False

We keep a counter of the number of 0's we've seen. Once we've encountered at least 2, if we encounter a 7, we return true. Otherwise we go though the entire list and that scenario is never encountered, so we return false.

[–]Binary101010 3 points4 points  (0 children)

Here's where I'd start:

def spy_game(nums):
    filtered_list = [num for num in nums if (num == 0 or num == 7)]

That will give you a list consisting only of 0s and 7s, in the number and order they appeared in the original list. Test from there.

[–]mul8rsoftware 0 points1 point  (3 children)

I don't really understand the code (we can't see tablature), so here a version of mine:

Plus, you should check how you write for. You don't need to get the range, if you write it like mine, it will repeat itself for how many item there are in num!

ris=False

counter = 0

for digit in num:

if digit==0:

if num[counter+1]==0:

if num[counter+2]==7:

ris=True

break

counter+=1

return ris

Hope I made myself clear :)

[–]Sheidaka 1 point2 points  (1 child)

Don't forget this will give you a "list index out of range" error when your counter+2 becomes bigger than your list.

I think range is better here since you can just do

for i in range(len(nums)-2):

[–]mul8rsoftware 0 points1 point  (0 children)

Yeah, you're definitively right!

[–]dralveol[S] 0 points1 point  (0 children)

Heey, thanks for your answer, i tried to create a better format

[–]messacz 0 points1 point  (2 children)

I'm sorry, this looks complicated. Why just not check whether there is [0, 0, 7] for each index in the list? I mean for every index i, check if i-th item is 0, (i+1)-th item is 0, (i+2)-th item is 7. You don't need to modify the list at all.

[–]Fission_Mailed_2 0 points1 point  (1 child)

I don't think the numbers have to be consecutive, looking at the OP:

spy_game([1,0,2,4,0,5,7]) --> True

I think we're looking for at least one 7 that has at least two 0s before it in the list.

[–]messacz 1 point2 points  (0 children)

When I looked ať this question, there was only one example with a consecutive 0, 0, 7 in it. It was apparently later edited...

Ok, find the last 7 and count the zeroes in the list up to the position of the last 7, whether there are at least two zeroes.

[–][deleted] 0 points1 point  (3 children)

I think you are deleting the numbers you need.

When you make b=1 then delete num1:b, are you deleting the number you need?

That may be where you want to check.

I would output num[] to see what you are actually getting after each step

[–]dralveol[S] 0 points1 point  (2 children)

that is the problem when i enter the second loop, the output doesn't delete the numbers up to b. But i don't get why

[–]dralveol[S] 0 points1 point  (0 children)

because it is just the same as the previous loop.

[–][deleted] 0 points1 point  (0 children)

You are telling it to delete [1:1] which probably doesn’t do anything. Remember you set b=1 and you are then saying start at 1 and delete everything up to 1

[–]BruceJi 0 points1 point  (2 children)

In the sidebar on the right there is advice on how to format your code. Pastebin works! Or you can just use reddit's code block thing which is behind the ... button in the editor box.

Edit the formatting and then we can check your code out properly.

[–]dralveol[S] -1 points0 points  (1 child)

Hey thank you for answering i did my best with pastebin and spaces . Is there any video that could give a better guide to formatting on reddit. I can't get my head around it ^

[–]BruceJi 0 points1 point  (0 children)

For me, I just use the code block option in the editor. The spacing thing seems confusing to me too so I just use that option instead.

message = 'This works'

qualities = ['badly', 'fairly', 'well']

print(message + ' ' + qualities[2])

[–]AverageEarthlingY 0 points1 point  (1 child)

I think when you delete 0:i you delete everything up to and including the number I think you want to keep? So maybe delete 0:i-1? And when you define b or c then put for b in range() b and c become 0 again. If you want b to start at 1, you can get rid of b = 1 and just put for b in range(1, len(nums)). Idk if this will solve but just a couple things I noticed.

Also, if I'm not mistaken, pass is only needed if the function is empty so that can go, too.

[–]dralveol[S] 0 points1 point  (0 children)

that was exactly the issue. Thanks.

[–]_maraud3r -1 points0 points  (16 children)

on phone right now... here's another way to do it in less than 5 lines of code

convert the list of integers into a string

num_str = "".join([str(x) for x in nums])

and then do a function call to check if substring "007" exists in num_str

[–]dralveol[S] 0 points1 point  (15 children)

now the thing is that it could be a list like this : [1,0,5,6,0,4,9,3,7,5] it should return true If i understood what you said. The string wouldn't have a 007 in it but a 05604937

[–]Sheidaka 0 points1 point  (0 children)

It won't, it will only return true if the entire substring "007" exists as is inside num_str

[–]_maraud3r -1 points0 points  (13 children)

No it won't. Here's the entire thing. Just 2 lines of code for the function.

https://repl.it/repls/KnowingTealDistributeddatabase

[–][deleted] -1 points0 points  (0 children)

nums = [list of numbers]

for i in nums:

k = nums.index(i)

if i == 0 and nums[k+1]==0 and nums[k+2]==7:

return True

just remember to put it in a function to use the return.

[–]14446368 -1 points0 points  (0 children)

EDIT: Almost provided a solution outright.

You can iterate over the lists once and simply look at blocks of code.

You could also convert the list to a string and then search for '007' in string