2

I'm writing a function "most_of" that takes a list of numbers as a argument. The objective of the function is to take the list, iterate over it and find out if the majority of the list integers are divisible by 10.

So for example, if I had passed the argument:

[1,10,10,50,5]

The output would be:

True

Because 3/5 of the integers are divisible by 10. However, if I had passed:

 [1,2,55,77,6]

The output would be:

 False

Because 4/5 of the list integers are not divisible by 10.

Here is what I have tried:

def most_of(lst):
    for i in lst:
        if lst[i] % 10 == 0:
           lst == True
        else:
           lst == False

I'm basically stuck at this point because this doesn't check if the majority of the numbers are divisible by ten, it just divides.

Thanks for the help!

Landon G
  • 819
  • 2
  • 12
  • 31
  • `==` is for checking if things are equal, not for assigning values. But then you don't want to reassign the `lst` variable anyway. You need to keep a count of how many items are and are not divisible by 10, then return `True` or `False` depending on which is greater. – Robin Zigmond Nov 21 '18 at 16:16

4 Answers4

4

Count how many integers are divisible by ten, and test whether that number is "the majority" - that is, if it's greater than or equal to half the lists' length. Like this:

def most_of(lst):
    num = sum(1 for n in lst if n % 10 == 0)
    return num >= len(lst) / 2.0

For example:

>>> most_of([1,10,10,50,5])
True
>>> most_of([1,2,55,77,6])
False
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Possibly better: `sum(not n % 10 for n in lst)`, since `0` is Falsy and `bool` is a subclass of `int`. – jpp Nov 21 '18 at 16:27
  • As influenced by this [answer](https://stackoverflow.com/a/5384590/1435475) I would replace the middle line by `num = len(list(filter(lambda x: x%10 ==0, lst)))`. Its somewhat clumsy to sum something if you are just interested at the number of elements. For huge lists memory efficiency might become an issue, but we should be factor ten below input list size. – guidot Nov 21 '18 at 16:50
  • @guidot but with your proposal we would be creating a temporary list, which is even worse from the memory-consumption perspective. – Óscar López Nov 21 '18 at 16:54
  • @ÓscarLópez I admitted inferior memory-efficiency already (which is unlikely to cause serious problems for reasonably-sized lists), but increasingly welcome plain code and the question does not mention *sum* anywhere. – guidot Nov 21 '18 at 20:42
  • @guidot it's not clumsy, it's the [idiomatic way](https://stackoverflow.com/q/15375093/201359) to count items that meet a condition in Python. In an old-fashioned loop, we would have a statement like `count++;` somewhere. Here we're doing the same, and efficiently - we process one item at a time, and keep adding`1` for each item that meets the condition. – Óscar López Nov 21 '18 at 20:47
  • @guidot even in the post you [linked](https://stackoverflow.com/a/5384573/201359) it's the most-voted, accepted and "usual way" to do this in Python. – Óscar López Nov 21 '18 at 20:50
  • @ÓscarLópez Thanks for the answer, what condition would I have to add to make it so if less than half (or equal too) of them were divisible by 10, then it would return False automatically? So for example, if I passed [10,20,2,3], it would return False, because only 2/4 are divisible – Landon G Nov 21 '18 at 21:39
  • Change the comparison in the last line, to suit your needs. If it's not `>=`, then what could it be? think about it! – Óscar López Nov 21 '18 at 21:50
1

The objective of the function is to take the list, iterate over it and find out if the majority of the list integers are divisible by 10.

Your list will contain two kind of integers: those that are divisible by 10 and those that aren't. You need to find the number of integers in each of the two categories, compare those numbers and return True or False accordingly. So, your function would look like this:

def most_of(lst):
    divisible_counter = 0
    non_divisible_counter = 0
    for element in lst:
        if element % 10 == 0:
            divisible_counter +=1
        else:
            non_divisible_counter += 1
    if divisible_counter > non_divisible_counter:
        return True
    else:
        return False

Of course, all the above code could be reduced a lot. But I wanted to show an algorithm that would be easier to understand for Python beginners.

Ciprian Stoica
  • 2,309
  • 5
  • 22
  • 36
0

A slight modification of the answer by Oscar:

def most_of(lst):
    return sum(1 if n % 10 == 0 else -1 for n in lst) >= 0

with the same results of course

lst1 = [1,10,10,50,5]
lst2 = [1,2,55,77,6]

print(most_of(lst1))  # True
print(most_of(lst2))  # False
Ma0
  • 15,057
  • 4
  • 35
  • 65
-1

you assign your list a bool after you test your first number, but you have to count all numbers which can divide by ten without rest and all other numbers and then compare this counters:

def most_of(lst):
    divideByTen = 0
    otherNumbers = 0
    for i in lst:
        if i % 10 == 0:
           divideByTen+=1
        else:
           otherNumbers+=1
    if(divideByTen > otherNumbers):
        return True
    else:
        return False


a = [1,10,10,50,5]
b = [1,2,55,77,6]

print(most_of(a))
print(most_of(b))
nerd100
  • 97
  • 1
  • 8
  • This is inferior in respect to elegance and does not really look like idiomatic Python (if expresssion requires no parentheses, strange white space omissions...). – guidot Nov 21 '18 at 16:28