-1

This code takes an array of numbers (only 2 different numbers) and returns the number that repeats itself the most:

def majorityElement(self, nums: List[int]) -> int:
    first_num = 0
    second_num = 0
    target = None
    for num in nums:
        if num == nums[0]:
            first_num += 1
        else:
          if target is None:
              target = num
              second_num += 1
    if first_num > second_num:
        return nums[0]
    else:
        return target

Then I saw that the if target is None: was running for every number inside the array, even when I already have a target (I added that code because the first number is easy to catch (nums[0]) but the second one can first appear on index 1, 20, 40000 or anything in between).

I thought it could be cool if you could give "lives" to some pieces of code, like....

def majorityElement(self, nums: List[int]) -> int:
    first_num = 0
    second_num = 0
    target = None
    for num in nums:
        if num == nums[0]:
            first_num += 1
        else:
          %(while True) if target is None:
                # I just created that syntax, it doesn't have to be written like that
                target = num
            second_num += 1
    if first_num > second_num:
        return nums[0]
    else:
        return target

That would mean that piece of code would only be active while that statement is true (I guess it should be true from the beginning, and even if it becomes false and true again, it wouldn't "revive").

Is there any way to accomplish something like that? Would it even work? Because the computer should still check (and therefore, spend time) if it's true, rendering the whole idea useless, right?

Maybe instead of using booleans, just int as lives (%(5) would mean it has 5 lives)

By that I don't mean a for i in range(5) loop, here's the difference:

Code with for loop:

for i in range(10, 13):
    for j in range(2):
        try:
            print(i)
        except:
            pass
    try:
        print(j)
    except:
        pass

0 1 10 0 1 11 0 1 12

Code with lives:

for i in range(10, 13):
    %(3)for j in range(2):
        try:
            print(j)
        except:
            pass
    try:
        print(i)
    except:
        pass

0 1 10 0 #pass 11 #pass #pass 12

MIKIBURGOS
  • 47
  • 9

2 Answers2

1

To be honest it's not a problem if your test runs for every iteration of the loop.

If it really bothers you, you can use set(List) which returns every different element of your list.

Also a more pythonic way of achieving what you're trying to do it to use Counter

from collections import Counter
def majorityElement(nums: List[int]) -> int:
    counter = Counter(nums)
    return counter.most_common(1)[0][0]

On a more general note, if you want code to run only once in a loop, you either use a flag - and yes, it will test for the flag every time the loop runs - or you design your code to put the code you want to execute only once outside the loop.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Maxime
  • 818
  • 6
  • 24
  • It doesn't really bother me, I was just wondering whether or not it was possible to do, seems like a non-dangerous way and easy to use (maybe not to implement) to make some code marginally faster. – MIKIBURGOS Apr 06 '23 at 08:50
0

Basically, you want to do this:

lives = 3
for i in range(10, 13):
    if lives := lives - 1:
        for j in range(2):
            try:
                print(j)
            except:
                pass
    try:
        print(i)
    except:
        pass

but you want the syntax to be as concise as possible.

The only way I can think of achieving this relatively concisely is by wrapping the block of code you want to run in a function with a decorator around it. However, in this case you won't be able to enjoy the benefit of all of these different blocks sharing the same scope.

As such you want to enable a form of "goto", which is considered bad practice almost universally.

rudolfovic
  • 3,163
  • 2
  • 14
  • 38
  • The point was to avoid the check (`if lives := lives - 1:` is a check) but there doesn't seem to be a way to do it. Thanks! :) – MIKIBURGOS Apr 06 '23 at 08:48