Program that checks if number can be divided to three numbers - python

I need to make a program that checks if a number can be divided to three(or more) numbers. for example 8=2*2*2 and 153=3*3*17 and so on. And it has to work for all positive real numbers. I just can't wrap my head around it :(
def loytyyko_kolme_tekijaa(luku):
tekija = 2
kaikki = 0
while luku > tekija:
if luku % tekija == 0:
kaikki = kaikki + 1
tekija = tekija + 1
if kaikki >= 3:
return True
else:
return False

Ok now that I see that you tried. Is this what you want?
Copied answer from here:
Python - Integer Factorization into Primes
def factorize(num):
for possible_factor in range(2, num):
if num % possible_factor == 0:
return [possible_factor] + factorize(num // possible_factor)
return [num]
nums = [8,153]
for num in nums:
print("{}: {}".format(num, factorize(num)))
Returns:
8: [2, 2, 2]
153: [3, 3, 17]

Related

The code is for returning the outlying even or odd number, everything works except for my numbers are returning with brackets, thanks

example of a problem would be [2,4,6,8,9]
the code should return 9, instead it returns [9]
def find_outlier(integers):
even, odd = 0, 0
outlier = []
for num in integers:
if num % 2 == 0:
even += 1
else:
odd +=1
if even > odd:
for num in integers:
if num % 2 != 0:
outlier.append(num)
return outlier
else:
if odd > even:
for num in integers:
if num % 2 == 0:
outlier.append(num)
return outlier
You are returning the list and printing the list. That's the reason
you can returning the num instead of the outlier list
def find_outlier(integers):
even, odd = 0, 0
outlier = []
for num in integers:
if num % 2 == 0:
even += 1
else:
odd +=1
if even > odd:
for num in integers:
if num % 2 != 0:
outlier.append(num)
return num
else:
if odd > even:
for num in integers:
if num % 2 == 0:
outlier.append(num)
return num
print(find_outlier([2,4,6,8,9]))
But still your code is fully mess. It's not gonna return more than one odd or even. I exactly don't know what you wanted to mean by this code. But this shortened code may help
def CheckEvenOrOdd(integers):
even, odd= 0,0
Evenlist=[]
OddList=[]
for num in integers:
if num % 2 == 0:
Evenlist.append(num)
even += 1
else:
OddList.append(num)
odd +=1
if even > odd:
return OddList
else:
return Evenlist
print(CheckEvenOrOdd([2,4,6,8,9,11]))
It's gonna return list so there is no problem with that

Largest number formed from some or all elements in an array divisible by 3

I am trying solve the following question:
Given an array with a length of 1-9 elements consisting of digits 0-9, what is the largest number divisble by 3 that can be formed using some / all the elements in the array?
The question only accepts Java and Python, and I chose Python despite being completely inexperienced with it.
I looked around and it seems like the general idea was to kick off the smallest element to get the digits total divisible by 3, and wrote the following "subtractive" approach:
def maxdiv3(l):
l.sort()
tot = 0
for i in l:
tot += i
if tot % 3 == 0:
l.sort(reverse=True)
return int(''.join(str(e) for e in l))
elif tot % 3 == 1:
cl = [] # A copy of the list but only for elements % 3 != 0
acl = [] # Anti copy of the list, only for elements % 3 = 0
for i in l:
if i % 3 == 0:
acl.append(i)
else:
cl.append(i)
removed = False
nl = [] # A new list for the final results
for i in cl:
if not removed:
if i % 3 == 1:
removed = True
else:
nl.append(i)
else:
nl.append(i)
if removed:
nl.extend(acl)
nl.sort(reverse=True)
if len(nl) > 0:
return int(''.join(str(e) for e in nl))
else:
return 0
else:
if len(acl) > 0:
acl.sort(reverse=True)
return int(''.join(str(e) for e in acl))
return 0
elif tot % 3 == 2:
cl = []
acl = []
for i in l:
if i % 3 == 0:
acl.append(i)
else:
cl.append(i)
removed2 = False
nl = []
for i in cl:
if not removed2:
if i % 3 == 2:
removed2 = True
else:
nl.append(i)
else:
nl.append(i)
if removed2:
nl.extend(acl)
nl.sort(reverse=True)
if len(nl) > 0:
return int(''.join(str(e) for e in nl))
removed1 = 0
nl = []
for i in cl:
if removed1 < 2:
if i % 3 == 1:
removed1 += 1
else:
nl.append(i)
else:
nl.append(i)
if removed1 == 2:
nl.extend(acl)
nl.sort(reverse=True)
if len(nl) > 0:
return int(''.join(str(e) for e in nl))
if len(acl) > 0:
acl.sort(reverse=True)
return int(''.join(str(e) for e in acl))
else:
return 0
This approach kept gets stuck on a hidden test case, which means I can't work out what or why.
Based on this, I wrote up a new one:
def maxdiv3(l):
l.sort()
l0 = []
l1 = []
l2 = []
for i in l:
if i % 3 == 0:
l0.append(i)
elif i % 3 == 1:
l1.append(i)
elif i % 3 == 2:
l2.append(i)
tot = sum(l)
nl = []
if tot % 3 == 0:
nl = l
nl.sort(reverse=True)
if len(nl) > 0:
return int(''.join(str(e) for e in nl))
return 0
elif tot % 3 == 1:
if len(l1) > 0:
l1.remove(l1[0])
nl.extend(l0)
nl.extend(l1)
nl.extend(l2)
nl.sort(reverse=True)
if len(nl) > 0:
return int(''.join(str(e) for e in nl))
return 0
elif len(l2) > 1:
l2.remove(l2[0])
l2.remove(l2[0])
nl.extend(l0)
nl.extend(l1)
nl.extend(l2)
nl.sort(reverse=True)
if len(nl) > 0:
return int(''.join(str(e) for e in nl))
return 0
else:
return 0
elif tot % 3 == 2:
if len(l2) > 0:
l2.remove(l2[0])
nl.extend(l0)
nl.extend(l1)
nl.extend(l2)
nl.sort(reverse=True)
if len(nl) > 0:
return int(''.join(str(e) for e in nl))
return 0
elif len(l1) > 1:
l1.remove(l1[0])
l1.remove(l1[0])
nl.extend(l0)
nl.extend(l1)
nl.extend(l2)
nl.sort(reverse=True)
if len(nl) > 0:
return int(''.join(str(e) for e in nl))
return 0
else:
return 0
And this one does pass all the test cases, including the hidden ones.
Here are some of the test cases that I ran my attempt through:
[3, 9, 5, 2] -> 93
[1, 5, 0, 6, 3, 5, 6] -> 665310
[5, 2] -> 0
[1] -> 0
[2] -> 0
[1, 1] -> 0
[9, 5, 5] -> 9
It seems to me that my attempt and the SO solution had the same idea in mind, so what did I neglect to consider? How is the SO solution different from mine and how does that catch whatever it is that my attempt didn't?
Thank you for your time.
A slightly off topic additional question: How do I find edge cases when dealing with blind test cases? I built a random input generator for this question but that didn't help anywhere near I wished it would and is likely not a good general solution.
How do I find edge cases when dealing with blind test cases?
Here is what I did as tester:
digits = list(range(10))
for k in range(1, 10): # Try different sizes
for _ in range(100): # Repeat many times
lst = random.choices(digits, k=k) # Produce random digits
a = maxdiv3(lst) # Run working solution
b = maxdiv3b(lst) # Run solution with a problem
if a != b: # Found a deviation!
print(lst)
break
This was one of the lists I got:
[2, 2, 5, 5, 8]
Then I retraced your code with that input and came into this block:
else:
if len(acl) > 0:
acl.sort(reverse=True)
return int(''.join(str(e) for e in acl))
We are here in the case where the total has a remainder of 1 when divided by 3. We get in this else block when there is no individual digit with such a remainder. It then just outputs all the digits that are multiples of 3. But this is not always right. When there are at least two digits with a remainder of 2 (i.e. 2, 5, 8), such a pair represents a total that has a remainder of 1, i.e. you only have to remove two digits, not more.
The correction is to remove two of those digits (the smallest) and then join the two lists as you did elsewhere in the code:
else:
del nl[0:2]
nl.extend(acl)
nl.sort(reverse=True)
if len(nl) > 0:
return int(''.join(str(e) for e in nl))
else:
return 0
NB: It didn't help that the chosen names are not very descriptive. I have no clue what acl, nl, or cl are abbreviations of.
You can use recursivity to "improve" the digit selection, stopping as soon as the sum of digits is a multiple of 3 (all numbers divisible by 3 have a sum of digits that is a multiple of 3). Only removing digits that are not multiples of 3 can improve the result
The largest number is the one that has digits in decreasing order.
def div3(D):
if sum(D)%3 == 0:
return sum(d*10**i for i,d in enumerate(sorted(D)))
return max(div3(D[:i]+D[i+1:]) for i,d in enumerate(D) if d%3)
d = [1,2,3,4,5]
print(div3(d))
# 54321
d = [2, 2, 5, 5, 8]
print(div3(d))
# 855
print(div3([4,1]))
# 0

How to take list of numbers from a recursive function and return it as a list?

I made a recursive function that gives me a certain number if it's odd or even. So, if I call the function with a 5, it'll output 5, 16, 8, 4, 2, 1. How do I have the output return this sequence of numbers as a list? So, [5, 16, 8, 4, 2, 1].
This is my code:
def recursion_list(number):
ls = []
print(number)
if number == 1:
return 1
if number % 2 == 0:
first = recursion_list(number // 2)
return first
elif number % 2 == 1:
second = recursion_list(3 * number + 1)
return second
There are two ways to do this.
First, return a variable of list type and receive the return value and appending it.
But this method has a large overhead
Second, make variable ls to global variable of list type and instead of returning a value, attach it to the ls variable.
Try to return a list of the printed variables and recursively append them.
def recursion_list(number):
ls = [number]
print(number)
if number == 1:
return ls
if number % 2 == 0:
ls += recursion_list(number // 2)
return ls
elif number % 2 == 1:
ls += recursion_list(3 * number + 1)
return ls
print(recursion_list(5))
output
5
16
8
4
2
1
[5, 16, 8, 4, 2, 1]
The task can be done by defining a list globally.
ls = []
def recursion_list(number):
ls.append(number)
if number == 1:
return ls
if number % 2 == 0:
first = recursion_list(number // 2)
return first
elif number % 2 == 1:
second = recursion_list(3 * number + 1)
return second
print(recursion_list(5))
def recursion_list(number):
ls = [number]
if number == 1:
return ls
elif number % 2 == 0:
return ls + recursion_list(number // 2)
elif number % 2 == 1:
return ls + recursion_list(3 * number + 1)
You could convert you recursive function into a generator and make a list of the output only when needed.
def recursion_gen(number):
yield number
if number == 1: return
if number % 2 == 0:
for n in recursion_gen(number // 2):
yield n
else:
for n in recursion_gen(3 * number + 1):
yield n
print(*recursion_gen(5)) # 5 16 8 4 2 1
ls = list(recursion_gen(5))
print(ls) # [5, 16, 8, 4, 2, 1]
To return a list directly, you don't need an intermediate variable:
def recursion_list(number):
if number == 1:
return [number]
if number % 2 == 0:
return [number] + recursion_list(number // 2)
else:
return [number] + recursion_list(3 * number + 1)

getting only the odd digits in a number with recursion [duplicate]

This question already has answers here:
getting only the odd digits in a number with recursion
(3 answers)
Closed 2 years ago.
So my problem is this i have an number like 123 and as the tilte sugests i want the result to be 13.
The problem is that firstly with the method im using im going to get the invert result (31 for example), secondly im getting a zero at the end that shouldn't be there and instead of joining the digits its summing them and i dont understand why.
So to clarify:
My output:
>>> apenas_digitos_impares(123)
40
Correct output:
>>> apenas_digitos_impares(123)
13
program:
def apenas_digitos_impares(n):
if n == 0:
return 0
elif (n%10)%2 == 0:
return apenas_digitos_impares(n//10)
elif (n%10)%2 == 1:
return 10*(n%10) + apenas_digitos_impares(n//10)
You could do it as -
def apenas_digitos_impares(n):
if n == 0:
return 0
elif (n%10)%2 == 0:
return apenas_digitos_impares(n//10)
elif (n%10)%2 == 1:
return (n%10) + 10*apenas_digitos_impares(n//10)
print(apenas_digitos_impares(123))
OUTPUT :
13
Solution#1: You can do the operation with a single method
import math
def convert_int_to_list(n):
result = []
while n > 0:
if (n % 10) % 2 == 1:
result.append(n % 10)
n = math.floor(n / 10)
return result
print(convert_int_to_list(1345986))
Output:
[9, 5, 3, 1]
Solution#2 You don't have to change your method, just declare an array before the method
result = []
You want odd numbers, then place the array into the correct place.
elif (n%10)%2 == 1:
res.append(n%10)
return 10*(n%10) + apenas_digitos_impares(n//10)
print res
[3, 1]
For various examples:
apenas_digitos_impares(1235)
apenas_digitos_impares(12356789)
[5, 3, 1]
[9, 7, 5, 3, 1]

All prime numbers within a range

I am trying to write a program that will print all the primes within a given range. I have written it, the output is almost okay, it prints primes, but for some reason it prints 4, which is not a prime...
Any assistant will be most appreciated !
def primes():
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
num = 0
i = 0
ctr = 0
for num in range(start,end+1,1):
ctr = 0
for i in range(2,num//2,1):
if num % i == 0 :
ctr = ctr + 1
break
if (ctr==0 and num != 1):
print(num)
for i in range(2,num//2,1):
Lets check this snippet of code when num = 4,it becomes
for i in range(2,2,1):
Now we see the problem.
Solution..?
for i in range(2,(num//2)+1,1):
The following methods are all possible prime checkers you might use to check within your range:
def isPrime(Number): # slow
return 2 in [Number, 2 ** Number % Number]
def isprime(n): # out of memory errors with big numbers
"""check if integer n is a prime"""
# make sure n is a positive integer
n = abs(int(n))
# 0 and 1 are not primes
if n < 2:
return False
# 2 is the only even prime number
if n == 2:
return True
# all other even numbers are not primes
if not n & 1:
return False
# range starts with 3 and only needs to go up the squareroot of n
# for all odd numbers
for x in range(3, int(n ** 0.5) + 1, 2):
if n % x == 0:
return False
return True
def is_prime(n): # Best until now
if n == 2 or n == 3:
return True
if n < 2 or n % 2 == 0:
return False
if n < 9:
return True
if n % 3 == 0:
return False
r = int(n ** 0.5)
f = 5
while f <= r:
# print '\t', f
if n % f == 0:
return False
if n % (f + 2) == 0:
return False
f += 6
return True
for i in range(2,num//2,1):
Above line is wrong. You are iterating from 2 to num / 2 - 1.
You should iterate from 2 to sqrt(num). (range(2, int(math.sqrt(n)) + 1))
Alternatively you can do a special check for 2 and modify your range to range(3, int(math.sqrt(n) + 1, 2)

Categories

Resources