1
> a = [1,2,3,4,5]  
> b = [1,2,3,4,5]  
> answer = [2,6,9,16,25]
>[e for e in a if a in b]  #at this point, i'm lost. sorry i'm new

What do I do after this? I want to multiply the elem in a if it's the same as the elem in b then check if the answer is in answer.

jpp
  • 159,742
  • 34
  • 281
  • 339
  • `res = [a * b if a == b else a for a, b in zip(a, b)]; [x == y for x, y in zip(res, answer)]`. – Christian Dean Apr 04 '18 at 19:52
  • Do you care more about relative positions of the elements, or just that they have the same value? The way I originally read this was looking at the same position in each list. – Ketzak Apr 04 '18 at 20:00

7 Answers7

4

Try using "zip" like this:

>>> [c*d for c,d in zip(a,b)]
[1, 4, 9, 16, 25]

Then you can compare that result list against the answer list you provided. A good way would be to do a truthy comparison like so, again using zip:

>>> res = [c*d for c,d in zip(a,b)]
>>> [x==y for x,y in zip(res, answer)]
[False, False, True, True, True]

if you merely want to check if each answer exists anywhere in the answer list, use "in" instead:

>>> res = [c*d for c,d in zip(a,b)]
>>> [x in answer for x in res]
[False, False, True, True, True]
Ketzak
  • 620
  • 4
  • 14
2

One way to achieve what you are looking for is:

result = filter(lambda x: x in answer, (x**2 for x in a for y in b if x == y ))

print(list(result))

This approach does not consider the order or the index of the elements.

If you also need to consider the position of the elements in a and b and intend to only compare elements with the same index then you can use:

result = filter(lambda x: x in answer, (x**2 for x,y in zip(a,b) if x == y))

Using the generator expression you can compare the elements in a with the elements in b. Since they have to be equal you can use x**2 to get the result of their multiplication. The results can be filtered using the python filter which returns only elements which are in the answer array.

0x51ba
  • 463
  • 3
  • 12
1

I would keep it simple and use something like this:

a =[1,2,3,4,5]
b =[1,2,3,4,5]
answer = [2,6,9,16,25]

computed_aswers = [val_1*val_1 for val_1 in a if val_1 in b]
available_answers = [ans for ans in computed_aswers if ans in answer]

print(available_answers)

Output would be:

[9, 16, 25]
fixatd
  • 1,394
  • 1
  • 11
  • 19
1

If you are happy using a 3rd party library, numpy is convenient for this task.

import numpy as np

a = np.array([1,2,3,4,5])
b = np.array([1,2,3,4,5])
answer = np.array([2,6,9,16,25])

res = a*b
# array([ 1,  4,  9, 16, 25])

### check by position ###
check = res == answer
# array([False, False,  True,  True,  True], dtype=bool)

### check if in answer ###
check2 = np.in1d(res, answer)
# array([False, False,  True,  True,  True], dtype=bool)

Relevant: Why NumPy instead of Python lists?

jpp
  • 159,742
  • 34
  • 281
  • 339
0

There's a fantastic function in python called zip that's a bit easier to understand then a list comprehension.

What zip does is allow you to iterate over all three lists at the same time, take a look at the following example:

A = [1,2,3,4,5]  
B = [1,2,3,4,5]  
answers = [2,6,9,16,25]

for a, b, answer in zip(A, B, answers):
    
    # Check if the element in a is the same as b
    if a == b:
        # Multiply them together
        mult = a * b
        # Check if a*b is answer
        if mult == answer:
            print('{} * {} equals {}.  Hooray!'.format(a, b, answer))
        else:
            print('{} * {} does not equal {} '.format(a, b, answer))
    else:
        print('{} does not equal {}'.format(a, b))
    

Notes:

  • This assumes that A B and answers are all the same length

  • It doesn't check that mult is anywhere in answers, only that it's at the exact same index as the a & b multiplied.

Community
  • 1
  • 1
Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43
  • I get it now, but this is the tricky part. Let's say I got the answer now, and I want to check if the element in a is equal to some part of the answer. let's say 12^2 = 144 but i want to know if 144 has the number 12 in it, like literally number 12. This is the problem I want to solve. Think of a 4 digit whole number "x" when multiplied to itself, you'll get 8 digit whole number who's 4 last digit number is equal to "x" – Renzo Aldridge Quiballo Apr 05 '18 at 01:39
  • 1
    @RenzoAldridgeQuiballo I created a different [question & answer for that question](https://stackoverflow.com/questions/49673030/find-the-a-4-digit-number-whos-square-is-8-digits-and-last-4-digits-are-the-ori/49673031#49673031) – Aaron N. Brock Apr 05 '18 at 12:59
0

Given:

a = [1, 2, 3, 4, 5]  
b = [1, 2, 3, 4, 5]
answer = [2, 6, 9, 16, 25]

Another approach is using numpy.multiply:

import numpy as np
result = [a == b for a, b in zip(answer, np.multiply(a, b))]
print(result)  # [False, False, True, True, True]

Or, instead of list comprehension for element-wise equality testing, using np.equal:

import numpy as np
result = np.equal(np.multiply(a, b), answer)
print(result)  # [False, False, True, True, True]

Or, simply falling back on the == operator for a numpy array and a same-sized list (-> element-wise comparison):

import numpy as np
result = np.multiply(a, b) == answer
print(result)  # [False, False, True, True, True]
dfrib
  • 70,367
  • 12
  • 127
  • 192
  • In my opinion, if you are going to use `numpy`, you should fully vectorise your solution (i.e. no list comprehension), cf my answer. – jpp Apr 05 '18 at 00:08
  • @jpp yes that is indeed probably more appropriate, thanks for the feedback. – dfrib Apr 05 '18 at 06:14
0
Here the program solution is in Perl.

Assume the given 4 digits number is abcd.

Where 

d is in unit position
c is in 10th position
b is in 100th position
a is in 1000th position

abcd * abcd = xxxxabcd. This is what condition placed in the question, where x can be any single digit unsigned integer.

Last 4 digits are formed as given below
1* (d*d) = Unit Position
10 * (2*c*d) + overflow digit from unit position = 10th Position 
100 * ( 2*b*d + c*c )  + overflow digit from 10th position = 100th Position
1000 * ( 2*a*d + 2*b*c ) + overflow digit from 100th position = 1000th Position.

if (d*d mod 10) equals d then d can end with 1,5,6 or 0. (eg. 1 * 1 = 1, 5 * 5 = 25, 6 * 6 = 36, 10 * 10 = 100)

So any number ends with 2,3,4,7,8,9 cannot give the expected result. Hence we can eliminate those numbers. Hence 60% of the numbers need not be checked.




#!perl
for( $str = 1000; $str < 10000; $str++ )
{
    $temp = $str;
    $d = $temp % 10;

    if( $d == 1 || $d == 5 || $d == 6 || $d == 0 )
    {

        $a = int($temp / 1000);
        $temp = $temp % 1000;

        $b = int($temp / 100);
        $temp = $temp % 100;

        $c = int($temp / 10);

        #This step can still be optimized 
        $sum = ( $d*$d + 10 * (2 * $c * $d ) + 100* (2 * $b * $d + $c * $c) + 1000* (2 * ( $a * $d + $b * $c )) ) % 10000;

        if( $sum == $str )
        {
            print "\nAnswer is $str";
            print "\nSquare of $str is " . $str*$str;
        }

    }
}