[Tutor] (no subject)
dn
PyTutor at DancesWithMice.info
Wed Mar 24 21:22:11 EDT 2021
On 25/03/2021 13.54, Alan Gauld via Tutor wrote:
> 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...
and response from you afterwards...
> 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.
Because options 1 thru/through 4 are mutually-exclusive, I foresee an
(improved) if...else...elif 'ladder' in your future!
For the same reason (@Alan +1), 'lazy execution' is to be recommended,
ie that the prime? calculation only be considered if/when it becomes
relevant (and no sooner).
Additionally, (maybe) the order of execution is not necessarily the best
sequence of coding tasks, as ...
>> 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?
+1 on the 'use the interpreter' advice.
and I thought +1 on the two string functions advised. However, taking
the 'interpreter' advice myself, revealed/reminded that it is not
necessary to strip white-space before passing a string to int().
>>> int( '23' )
23
>>> int( ' 23 ' )
23
>>> int( '023 ' )
23
That said, remembering that condition (1) requires strings not integers,
combined with the 'lazy execution' advice, means that the int() is only
relevant should the logic call for consideration of condition (2)!
> 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.
All excellent advice!
Consider each of the steps involved and coding (and testing) a separate
function for each, ie each function executes a single task.
Once the sub-problems have all been solved, then it becomes a matter of
writing code which calls them, if/as necessary - like making a pipeline
or constructing a building one floor/tier at a time.
--
Regards,
=dn
More information about the Tutor
mailing list