Mistake in Count Even Number List? - python

Can someone point out to me why the list is not returning the list I input with the amount of Even numbers in the list?
import sys
import random
def count_even(num_list):
num_list = [7, 65, 1337, 8, -2, 24, 6, 67, 54, 36, 25, 1, 42, 9, 138, 4356, 6]
count_even = 0
for num in num_list:
if num % 2 == 0:
count_even += 1
return num_list
def main(argv):
error_code = 0
num_list = [7, 65, 1337, 8, -2, 24, 6, 67, 54, 36, 25, 1, 42, 9, 138, 4356, 6]
print(count_even(num_list))
return error_code
if __name__ == '__main__':
error_code = main(sys.argv[1:])
print('[+] Terminated with code: ' + str(error_code))
sys.exit(error_code)

in def count_even, you're returning the array, instead of count of even numbers which is count_even. Like #alaniwi mentioned, replace 'return num_list' with 'return count_even'.

Related

how to remove composite numbers from a list in python 3?

I am not able to remove composite numbers from a list in python 3 .Can you help?
Example input:
list1 = [2, 3, 6, 7, 14, 21, 23, 42, 46, 69, 138, 161, 322, 483]
Expected output:
list1 = [2, 3, 7, 23]
Thanks in advance.
You can use a list comprehension with all:
list1 = [2, 3, 6, 7, 14, 21, 23, 42, 46, 69, 138, 161, 322, 483]
new_result = [i for i in list1 if all(i%c != 0 for c in range(2, i))]
Output:
[2, 3, 7, 23]
Ajax1234's solution is correct, but instead of using range(2, i), I would add the modification that range(2, i) becomes range(2, 1+math.ceil(math.sqrt(i))), where the math module has been imported. For very large lists, this reduces the execution time since all composite numbers have factors less than or equal to 1+math.ceil(math.sqrt(i)).

Python: How to efficiently count the number of "1"s in the binary representation of 1 to n numbers?

E.g. For the input 5, the output should be 7.
(bin(1) = 1, bin(2) = 10 ... bin(5) = 101) --> 1 + 1 + 2 + 1 + 2 = 7
Here's what I've tried, but it isn't a very efficient algorithm, considering that I iterate the loop once for each integer. My code (Python 3):
i = int(input())
a = 0
for b in range(i+1):
a = a + bin(b).count("1")
print(a)
Thank you!
Here's a solution based on the recurrence relation from OEIS:
def onecount(n):
if n == 0:
return 0
if n % 2 == 0:
m = n/2
return onecount(m) + onecount(m-1) + m
m = (n-1)/2
return 2*onecount(m)+m+1
>>> [onecount(i) for i in range(30)]
[0, 1, 2, 4, 5, 7, 9, 12, 13, 15, 17, 20, 22, 25, 28, 32, 33, 35, 37, 40, 42, 45, 48, 52, 54, 57, 60, 64, 67, 71]
gmpy2, due to Alex Martella et al, seems to perform better, at least on my Win10 machine.
from time import time
import gmpy2
def onecount(n):
if n == 0:
return 0
if n % 2 == 0:
m = n/2
return onecount(m) + onecount(m-1) + m
m = (n-1)/2
return 2*onecount(m)+m+1
N = 10000
initial = time()
for _ in range(N):
for i in range(30):
onecount(i)
print (time()-initial)
initial = time()
for _ in range(N):
total = 0
for i in range(30):
total+=gmpy2.popcount(i)
print (time()-initial)
Here's the output:
1.7816979885101318
0.07404899597167969
If you want a list, and you're using >Py3.2:
>>> from itertools import accumulate
>>> result = list(accumulate([gmpy2.popcount(_) for _ in range(30)]))
>>> result
[0, 1, 2, 4, 5, 7, 9, 12, 13, 15, 17, 20, 22, 25, 28, 32, 33, 35, 37, 40, 42, 45, 48, 52, 54, 57, 60, 64, 67, 71]

Is there any way to condense a for-else loop in Python?

I have made a piece of code that spits out prime numbers up to the 10001st number. It currently takes up 4 lines of code, and was wondering if I could condense it further? Here it is;
for i in range(3,104744,2):
for x in range(3,int(i/2),2):
if i % x == 0 and i != x: break
else: print(i)
I am aware that condensing code too much is usually not a good thing, but was wondering if it was possible.
Thanks.
You can use a list comprehension and any to get a one-liner solution:
>>> [p for p in range(2, 100) if not any (p % d == 0 for d in range(2, int(p**0.5) + 1))]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
It uses the fact that a divisor cannot be larger than the square root of the number it divies.
It seems to work fine:
>>> len([p for p in range(2, 104744) if not any (p % d == 0 for d in range(2,int(p**0.5)+1))])
10001
List comprehension
>>> r=range(2,100)
>>> [p for p in r if [p%d for d in r].count(0)<2]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Try this one:
for i in range(3,100,2):
if all( i%x for x in range(3, i//2, 2) ):
print(i)

Pyschools Number Sequence

So i'm doing pyschools topic 6 question 23:
Write a function getNumbers(number) that takes in a number as argument and return a list of numbers as shown in the samples given below.
Examples
>>> getNumbers(10)
[100, 64, 36, 16, 4, 0, 4, 16, 36, 64, 100]
>>> getNumbers(9)
[81, 49, 25, 9, 1, 1, 9, 25, 49, 81]
>>> getNumbers(8)
[64, 36, 16, 4, 0, 4, 16, 36, 64]
>>> getNumbers(0)
[0]
This is my code:
def getNumbers(num):
x = []
y = []
if num % 2 == 0:
x = [i**2 for i in range(0, num+2, 2)]
y = [i**2 for i in range(0, num+2, 2)]
z = sorted(x, reverse=True) + y
if z.count(0) > 1:
z.remove(0)
return z
elif num % 3 == 0:
x = [i**2 for i in range(1, num+2, 2)]
y = [i**2 for i in range(1, num+2, 2)]
return sorted(x, reverse=True) + y
elif num == 1:
x.append(num)
y.append(num)
return sorted(x, reverse=True) + y
Which works but, i'm not passing Private Test Case. Any ideea why?
Private Test Case is something made by them to see if you hard code.
Test Cases Expected Result Returned Result
getNumbers(10) [100, 64, 36, 16, 4, 0, 4, 16, 36, 64, 100] [100, 64, 36, 16, 4, 0, 4, 16, 36, 64, 100]
getNumbers(9) [81, 49, 25, 9, 1, 1, 9, 25, 49, 81] [81, 49, 25, 9, 1, 1, 9, 25, 49, 81]
Private Test Cases Passed Failed
getNumbers(0) [0] [0]
getNumbers(1) [1, 1] [1, 1]
This was the easiest thing to do:
def getNumbers(num):
x = -num
y = list(range(x, num+1, 2))
return [i**2 for i in y]
getNumbers(10)
[100, 64, 36, 16, 4, 0, 4, 16, 36, 64, 100]
Also please find below a version
def getNumbers(num):
n=list(range(num+1))
num1=[]
x=num
for i in n:
x=num**2
num1.append(x)
num-=2
return num1
def getNumbers(num):
return [i * i for i in range(-num, num + 1, 2)]
Square of a number is always positive. You need to end the range on num + 1 to include number in the result. The range is iterable, so the list around range is spurious.

Find number of prime numbers between 2 and n

I am trying to find the number of prime numbers between 2 and n, where n is provided by the user. I can't seem to make it work. here's my code:
>>> def numOfPrime(n):
count = 0
for i in range(2,n):
p = True
for j in range(2,i):
if i % j ==0:
p = False
if p == True:
count += 1
return count
>>> numOfPrime(100)
0
The indentation of your if block is incorrect. It should be in the loop.
def numOfPrime(n):
count = 0
for i in range(2,n):
p = True
for j in range(2,i):
if i % j ==0:
p = False
if p == True:
count += 1
return count
Now numOfPrime(100) returns 25, which correctly accounts for 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, and 97.

Categories