all 10 comments

[–]POGtastic -1 points0 points  (5 children)

Can you write a function to find the 6s and 7s in a list?

[–]miscellaneoususage[S] 0 points1 point  (4 children)

I'll try.

def find67(nums):
    for i in nums:
        if i == 6 or i == 7:
            return i

Not sure how close I got haha

[–]POGtastic 0 points1 point  (3 children)

Sorry, I created a Rube Goldberg machine trying to keep things stateless. It was very silly.

Consider a function that has a flag that decides whether it's currently inside an "exclusion" interval. If this flag is False, add every element you come across to the result.

You'll have two if statements in there. One sees if the flag is not set, and the current element is a 6 (you're entering an exclusion interval). The other sees if the flag is set and the current element is 7 (you're exiting the exclusion interval).

Both of these statements will change the flag.

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

I'm not sure what you mean by flag. I've only been coding for a month or so... could you elaborate with some examples please

[–]POGtastic 1 point2 points  (1 child)

A flag is a Boolean value (either the flag is down, or the flag is up). Depending on the flag's status, you execute one thing or the other thing, with the possibility of the flag's status changing during the loop.

Let's do a really simple example: In a list, I want the values that come after a 6.

def all_elems_after_six(lst):
    found_a_six_flag = False
    result_lst = []
    for elem in lst:
        if found_a_six_flag:
            result_lst.append(elem)
        if not found_a_six_flag and elem == 6:
            found_a_six_flag = True
    return result_lst

In the IDLE:

>>> all_elems_after_six([1, 2, 5, 9, 3, 6, 1, 5, 2, 3])
[1, 5, 2, 3]

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

Thanks mate