[Tutor] (no subject)
Alan Gauld
alan.gauld at yahoo.co.uk
Wed Mar 24 20:54:47 EDT 2021
On 24/03/2021 18:39, ੧ wrote:
This is obviously homework of some sort so we wont solve it
for you but we can provide hints...
First, let's see if we can express this more clearly:
> You have created an application that encrypts the password. To
> decrypt, you need to perform an operation between two integers, say A and
> B, as described below -
> 1) Let X and Y be the numbers obtained by REVERSING A and B.
> (leading zeroes are discarded)> 2) If both X and Y are PRIME, then the answer is X + Y
> 3) If EXACTLY ONE of X and Y is PRIME, then the answer is A + B
> 4) Otherwise, the answer is A * B
> Your task is to write a program to help you perform this operation.
> Input: Two COMMA-separated integers A and
> B. There can be any number of white-spaces in the input.
> Output : A single integer that is the result of applying the operation
> described above on the input integers.
> Constraints: 1≤A, B≤100001≤A, B≤10000
> Example: Input: 142,123
Output: 265
> Explanation : The reverse of 142 is 241 (prime) and that
> of 123 is 321 (not prime). Hence the answer is A + B = 142 + 123 = 265
> My code is not working. How to solve it
>
> A, B = int(input())
This does not work because input() returns a string and int() converts
that to a number. A single number.
You are expecting a string like
142, 123
You need to split it and strip it of white space before converting
to integers.
It's good to try things out in the interpreter before putting
them in code:
>>> A,B = int(input('?> '))
?> 142, 123
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
A,B = int(input('?> '))
ValueError: invalid literal for int() with base 10: '142, 123'
>>>
So it doesn't work. Do you know how to split a string?
Do you know how to strip whitespace?
Maybe you could write a function that reads a string,
splits and strips it and reverses the characters.
> rev1 = A[::-1]
> rev2 = B[::-1]
If your code above had succeeded you would be trying to
reverse integers. That wouldn't work either. You need
to keep the string form until after you reverse them.
Then convert to int before testing for prime.
> X = rev1
> Y = rev2
This is pointless. Just assign X and Y instead of rev1,rev2.
> qw = min(X, Y)
> for i in range(1, qw+1):
> if X&i == 0 and Y%i == 0:
Do you really mean to use bitwise AND(&) in that first test?
> hcf = i
and is the indentation correct here?
> if hcf == 1:
> print(X+Y)
>
> elif:
> for j in range(2, X):
> if X%j == 0 or Y%j == 0:
> print(A+B)
>
> else:
> print(A*B)
Maybe you should write a function that tests if
a number is prime?
def is_prime(n):
# returns True or false
You can then test that in the interpreter to
be sure it works before copying it into your code.
The interpreter is a powerful tool, be sure to use it.
Then you can use the function to apply the rules you
were given rather than trying to mix the prime test
and rules test into a single set of code. That might
also avoid you losing marks as mentioned in the
assignment.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list