i have a task:
got a range on input
[x;y]
then i should check every number from that range and check every numeral in number. if its odd, i should print it
for example: 3, 20
i should print 4,6,8,20
def check(num):
if int(num) % 2 == 0:
return True
x, y = int(input()), int(input())
numbers = []
if x <= y:
while x != y:
for i in str(x):
if check(i):
numbers.append(x)
x += 1
else:
while y != x:
for i in str(y):
i = int(i)
if check(i):
numbers.append(y)
y += 1
if y == x:
for i in str(x):
if check(i):
numbers.append(x)
print(numbers)
it prints
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 21, 22, 22, 23, 24, 24, 25, 26, 26, 27, 28, 28, 29]
instead of 2,4,6,8,20,22,24,26,28
You are checking and approving based on a single digit for each number. You should check all of them before appending the entire number. That's why you get 22 twice: once for the first 2 and once for the second. You get 10, because even though 1 is odd, 0 is even and so you append it to your list.
Move the check for the entire number into the function check and have it return True only if all digits are even. This shortens your code. Also note that you can trivially swap x and y if the user entered them in the wrong order.
The function check immediately returns False as soon as it finds a digit i is odd, and you can see it can only return True if the loop ended and all digits were even.
def check(num):
for i in str(num):
if int(i) % 2 != 0:
return False
return True
x, y = int(input()), int(input())
if x > y:
x,y = y,x
numbers = []
while x <= y:
if check(x):
numbers.append(x)
x += 1
print (numbers)
Result, with 3 and 20 entered (but 20 and 3 would also work):
[4, 6, 8, 20]
Related
I want to make a program as an exercise which prints all prime numbers in range of given limit(parameter). Thanks.
#prime numbers
prime = list()
for x in range(1, 100):
for a in range(2, x):
if x % a == 0:
break
else:
prime.append(x)
print(prime)
You can use this list comprehension to find prime numbers in given range;
def prime_generator(number):
return [x for x in range(2, number) if not any(x % y == 0 for y in range(2, int(x/2)+1))]
In: prime_generator(30)
Out: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Given the list:
n = [3, 6, 12, 24, 36, 48, 60]
I need to turn a random number divisible by 3, after by 6, after by 12, after by 24, after by 36, after by 48 and after by 60. One at each time, not the divisible for the seven numbers simultaneously.
But, to make the number divisible, I need to sum another number to reach the number divisible by 3, 6, 12, 24, 36, 48 or 60.
I don't know the random number neither the number to add to this random number in order to turn it divisible by 3, 6, 12, 24, 36, 48 or 60.
Can someone help me, please?
Using the map function list comprehension:
import random
n = [3, 6, 12, 24, 36, 48, 60]
a = random.randint(start, stop)
result = [a + x - a % x if a % x else a for x in n]
addition = [x - a for x in result]
print(a)
print(result)
print(addition)
start and stop are the limits for your random number.
Output for a = 311:
311
[312, 312, 312, 312, 324, 336, 360]
[1, 1, 1, 1, 13, 25, 49]
Begin of the old answer to the rather unclear question before the first edit by the author.
The following post answers the question: Which is the next number after a, which is a multiple of all numbers of the list n, if it is not a?
First the least common multiple lcm of the numbers from the list must be determined. For this purpose, the method least_common_multiple is executed for the complete list. The modulo operation then checks whether a is not already a multiple of the numbers. If this is the case, a is output. Otherwise, the next multiple of lcm is output.
from math import gcd
from functools import reduce
n = [3, 6, 12, 24, 36, 48, 60]
a = 311
def least_common_multiple(x, y):
return abs(x * y) // gcd(x, y)
lcm = reduce(least_common_multiple, n)
result = a if a > 0 else 1 # can be change if necessary, see edit
mod = result % lcm
if mod:
result += (lcm - mod)
print('Current number: ' + str(a))
print('The next number divisible by any number from the given list: ' + str(result))
print('Necessary addition: ' + str(result - a))
Output:
Current number: 311
The next number divisible by any number from the given list: 720
Necessary addition: 409
Edit: Changed the code so that 0 is no longer a valid result for a non-positive a.
However, if it is valid, you can change the code at the commented part with: result = a.
New answer for the clarified question:
import math
def f(x, a):
return math.ceil(a / x) * x - a
n = [3, 6, 12, 24, 36, 48, 60]
a = 311
result = [f(x, a) for x in n]
print(result)
Old answer:
I think you're looking for the LCM (lowest common multiple) of all numbers in n. We can get the LCM for two numbers using the GCD (greatest common divisor). We can then use reduce to get the LCM for the whole list. Assuming x can be negative, simply subtract a from the LCM to get your answer. The code could look like this:
import math
from functools import reduce
def lcm(a, b): # lowest common multiple
return a * b // math.gcd(a, b)
n = [3, 6, 12, 24, 36, 48, 60]
smallest_divisible_by_all = reduce(lcm, n)
a = 311
x = smallest_divisible_by_all - a
print(x)
which outputs
409
If speed is not important, you could also do a brute force solution like this:
ns = [3, 6, 12, 24, 36, 48, 60]
a = 311
x = 0
while not all([(a + x) % n == 0 for n in ns]):
x += 1
(assuming that x >= 0)
So I can think of 2 solutions:
First if you want a list which show how much you have to add for the random number to get to a divisible number:
def divisible(numbers,random):
l = []
for number in numbers:
if random % number != 0:
l += [number - (random % number)]
else:
l += [0]
return l
a = 311
n = [3, 6, 12, 24, 36, 48, 60]
print(divisible(n,a))
Output:
[1, 1, 1, 1, 13, 25, 49]
Or if you want to know how far is the smallest number from the random number that everyone shares as divisible. For this take a look how you calculate lcm.
from math import gcd
def divisible_all(numbers, random):
lcm = numbers[0]
for i in numbers[1:]:
lcm = lcm*i//gcd(lcm, i)
tempLcm = lcm
while lcm < random: # in case the lcm is smaller than the random number add lcm until it is bigger (I assume you only allow positive numbers)
lcm += tempLcm
return lcm-random
print(divisible_all(n,a))
Output:
409
You can do:
import math
n = [3, 6, 12, 24, 36, 48, 60]
div_n={el: False for el in n}
a=311
a0=math.ceil(a/max(n)) * max(n)
while not all(div_n.values()):
div_n={el: False for el in n}
# print(a0)
for el in n:
if(a0 % el > 0):
a0=math.ceil(a0/el) * el
break
else:
div_n[el]=True
print(a0-a)
Output:
409
When my program is done printing 10 random numbers, it should print something like this. (These numbers 3, 7, and 10 are just used as example.)
List had 3 evens and 7 odds
The 3rd element in sorted nums is 10
For some reason this isn't happening for me.
This is the code I have so far:
import random
nums =[]
for i in range (1,11):
x = random.randint(1,50)
nums.append(x)
print(nums)
nums.sort()
print(nums)
start = nums[0:5]
print(start)
finish = nums[7:10]
print(finish)
def evenOdd(num):
odd = 0
even = 0
for x in num:
if (x % 2 == 0):
even += 1
else:
odd += 1
print('List had', even, 'evens and', odd, 'odds')
print('The 3rd element in sorted nums is', num[2])
When I run it, this is what the program displays:
Traceback (most recent call last):
[7, 21, 6, 14, 22, 8, 24, 20, 26, 27]
line 35, in <module>
[6, 7, 8, 14, 20, 21, 22, 24, 26, 27]
print('List had', even, 'evens and', odd, 'odds')
[6, 7, 8, 14, 20]
NameError: name 'even' is not defined
[24, 26, 27]
Process finished with exit code 1
You have a number of problems in your code. You aren't actually calling your evenOdd function, if you did call it, you have provided no way to get its results, and you have some variable name mismatches. Also, your evenOdd() function wasn't doing the right thing because the else clause needed to be indented to be within the for loop. The way it is in your version, odd will always be 1:
Here's code that fixes all of these issues:
import random
nums =[]
for i in range (1,11):
x = random.randint(1,50)
nums.append(x)
print(nums)
nums.sort()
print(nums)
start = nums[0:5]
print(start)
finish = nums[7:10]
print(finish)
def evenOdd(num):
odd = 0
even = 0
for x in num:
if (x % 2 == 0):
even += 1
else:
odd += 1
return even, odd
even, odd = evenOdd(nums)
print('List had', even, 'evens and', odd, 'odds')
print('The 3rd element in sorted nums is', nums[2])
The main thing here is that the evenOdd function returns a "tuple" that contains the resulting counts of odd and even numbers so that you can use them outside the function.
Sample result:
[2, 36, 20, 32, 6, 40, 39, 27, 24, 47]
[2, 6, 20, 24, 27, 32, 36, 39, 40, 47]
[2, 6, 20, 24, 27]
[39, 40, 47]
('List had', 7, 'evens and', 3, 'odds')
('The 3rd element in sorted nums is', 20)
I am assuming the last print statements ought to be in your evenOdd function, in which case your code isn't properly indented. It should be like this:
def evenOdd(num):
odd = 0
even = 0
for x in num:
if (x % 2 == 0):
even += 1
else:
odd += 1
print('List had', even, 'evens and', odd, 'odds')
print('The 3rd element in sorted nums is', num[2])
After that, you can call the function thus:
evenOdd(nums)
Define the variables even and odd globally - they're only available inside the function.
even = 0
odd = 0
# ...
def evenOdd(num):
global even
global odd
even = 0
odd = 0
# ...
you can use this also, :-
import random
nums =[random.randint(1,51,) for var in range(1,11)] # creating nums list by short method
nums.sort()
print(nums)
def evenOdd(num):
odd = 0
even = 0
for x in num:
if (x % 2 == 0):
even += 1
else:
odd += 1
return even,odd
even,odd = evenOdd(nums)
print('List had', even, 'evens and', odd, 'odds')
print('The 3rd element in sorted nums is', nums[2])
I hope you learn something new.
Am new to python and I have been trying to solve this problem but it does not seem to work as intended. your help is highly appreciated:
Given two numbers X and Y, write a function that:
returns even numbers between X and Y, if X is greater than Y
else returns odd numbers between x and y
.
def number(x,y):
if x > y:
for i in range(x,y):
if i%2 == 0:
list = []
return list.append[i]
else:
for i in range(x,y):
if i%2 == 1:
list = []
return list.append[i]
print(number(10,2))
Try this code it's working as per your need.
def number(x,y):
num= []
if x > y:
for i in range(y,x):
if i%2 == 0:
num.append(i)
else:
for i in range(x,y):
if i%2 == 1:
num.append(i)
return num
print(number(2,10))
print(number(10,2))
The outputs are:
[3, 5, 7, 9]
[2, 4, 6, 8]
Let me know if this doesn't serve your purpose.
And it is done. Basically if x > y, you need to switch the first range. You append the items normally(using () instead of []), and then return the full list, got it?
def number(x,y):
list = []
if x > y:
for i in range(y,x):
if i%2 == 0:
list.append(i)
else:
for i in range(x,y):
if i%2 == 1:
list.append(i)
return list
print(number(10,2))
Working sample: https://py3.codeskulptor.org/#user302_nwBq00w56n_1.py
Instead of testing for oddness/evenness all the time, use range(start,stop[,step]) with a step of 2 starting with a (corrected, known) odd/even number:
def number(x,y):
if x > y:
if y%2 == 1: # y is smaller && odd
y += 1 # make even
return list(range(y,x,2)) # x is > y - start from y to x
else: # this is strictly not needed - but more verbose intention-wise
if x%2 == 0: # is even
x += 1 # make odd
return list(range(x,y,2))
print(number(10,32))
print(number(10,2))
You need to also switch x and y if x > y
you do not need to iterate a range and add its element to a list iteratively - simply stuff the range-sequence into the list(sequence) constructor and return it
Output:
[11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31]
[2, 4, 6, 8]
It's so easy to do, and there are several ways to do what do you want, so i show you two ways to do that, first an understandable way and second an easy way ok let's start:-
First example
def number(x,y):
list = [] #firstly create a list
if x > y: #if x was greater than y
for num in range(y, x): # a loop for searching between them
if(num % 2 == 0): # if the number was even add it to list
list.append(num)
elif y > x: #if y was greater than x
for num in range(x, y): # a loop for searching between them
if(num % 2 != 0): # if the number was not even add it to list
list.append(num)
return list
print(number(10, 20))
print(number(20, 10))
#[11, 13, 15, 17, 19]
#[10, 12, 14, 16, 18]
Second example
number = lambda x, y : [n for n in range(y, x) if n%2 == 0] if x > y else [n for n in range(x, y) if n%2 != 0]
print(number(10, 20))
print(number(20, 10))
#[11, 13, 15, 17, 19]
#[10, 12, 14, 16, 18]
Note : But be sure that in both of my answers the x number is inclusive(exists in searching function) and the y number is exclusive, so if you wanted to make both of them inclusive so make loops ...(x, y+1)... and if you wanted to make both of them exclusive just change loops to ...(x+1, y)....
Knowing that 2 % 2 == 0 we then can just use if not 2 % 2 for evens since not 0 will evaluate to true, here it is with comprehension and in extended form
def something(x, y):
if x > y:
l = [i for i in range(y, x) if not i % 2]
else:
l = [i for i in range(x, y) if i % 2]
return l
print(something(10, 2))
print(something(2, 10))
~/python/stack$ python3.7 sum.py
[2, 4, 6, 8]
[3, 5, 7, 9]
Full loop:
def something(x, y):
l = []
if x > y:
for i in range(y, x):
if not i % 2:
l.append(i)
else:
for i in range(x, y):
if i %2:
l.append(i)
return l
Here in this i use the list comprehensions.list comprehension is a easy and readable technique in python.In this i include both x and y
def fun(x,y):
if x>y:
l=[i for i in range(y,x-1) if i%2==0]
return l.reverse()
else:
l=[i for i in range(x,y+1) if i%2!=0]
return l
I'm trying to check modulo of a number against a tuple of numbers, if the modulo is equals to one of the values in the tuple I want to return True else return False.
This is what I had tried so far:
def check(y):
k = (2, 5, 8, 10, 13, 16, 19, 21, 24, 27, 29)
for i in range(0, len(k)):
if k[i] == y % 30:
return True
else:
return False
def main():
print(check(1439))
main()
It always returns false.
This is always returning false as only first item is checked. If first item is a match then it will return true. For example, if y is 32 then it will return true. You need to return false after checking all values, i.e. outside of for loop. Or a better solution is to use in operator.
def check(y):
k = (2, 5, 8, 10, 13, 16, 19, 21, 24, 27, 29)
return y % 30 in k
It always returns false because this code:
for i in range(0, len(k)):
if k[i] == y % 30:
return True
else:
return False
returns true or false based only on the first item in the array, because it returns in both possible code paths. Either k[0] == y % 30 and it returns true, or k[0] != y % 30 and it returns false.
If you want to use this loop-based solution, you need to check every item in the array, returning true immediately if it matches, otherwise returning false only after the list is exhausted, something like (using the for n in k variant of the loop since the index is irrelevant here):
for n in k:
if n == y % 30:
return True
return False
The full program is thus:
def check(y):
k = (2, 5, 8, 10, 13, 16, 19, 21, 24, 27, 29)
for n in k:
if n == y % 30:
return True
return False
def main():
print(check(1439))
print(check(36))
main()
with the first call producing true as 1439 % 30 == 29 (in the list) but the second giving false because 36 % 30 == 6 (not in the list).
Of course, there's a far more Pythonic way to achieve this:
def check(y):
k = (2, 5, 8, 10, 13, 16, 19, 21, 24, 27, 29)
return any (x == y % 30 for x in k)
That basically finds any element in k for which that element is equal to y % 30. See this link for more information on the Python any operation, and you'll see instantly the equivalent code given is remarkably similar to your loop:
def any(iterable):
for element in iterable:
if element:
return True
return False
But, of course, it turns out to be unnecessary to use any in this particular case as y % 30 is effectively a fixed value in the context of searching through the list. Instead, you can opt for the much simpler:
def check(y):
k = (2, 5, 8, 10, 13, 16, 19, 21, 24, 27, 29)
return (y % 30) in k
leaving the any variant for more complex comparisons not easily doable, such as only checking even numbers from the list:
def check(y):
k = (2, 5, 8, 10, 13, 16, 19, 21, 24, 27, 29)
return any (x == y % 30 for x in k if x % 2 == 0)
You can accomplish this with a generator expression inside any():
def check(y):
return any(n == y % 30 for n in k)
This builds an iterator of booleans that is true for all elements of k that are divisors of y.