0

So I am making a simplifying program to find the biggest number that can be divisible by the same number for both lists which I already found a way to partially do... I need a way so that I can find a common value from both the given lists which is also the highest common value in both the lists. Like the biggest number present in both the given lists should be selected and returned, I dont have a code yet Im still in the process of planning what to do

Example: list1 = [2,4,6,12] list2 = [2,4,6,8,14]

The answer I need is 6 as it is the highest common number from both of these lists, it shouldn't be 2 or 4 since they are not the highest value. I hope my explanation is understandable to write a reply. Thank You

5 Answers5

2

Another solution without using for loops, which is a little bit more faster:

list1=[2,4,6,12]
list2=[2,4,6,14]
max_in_both = max(filter(lambda x: x in list2, list1))
alt164
  • 21
  • 3
  • "_without using for loops_" -- but `max()` and `filter()` accept iterables, which they loop over. The `in` operator will repeatedly iterate over `list1` in O(N) time, giving total time complexity of O(N^2). Avoiding the `for` keyword is not a relevant goal. Prefer to pass in `set(list1)`, to avoid quadratic behavior. Consider relying on the [intersection](https://docs.python.org/3/library/stdtypes.html#frozenset.intersection) operator. – J_H May 13 '23 at 15:34
  • Yes, you are totally right. What I meant with "without using for loops" is that almost always is faster to use built-in functions than for loops (with the `for` keyword). But yes, your solution is much better, every day is a learning experience. Thanks! – alt164 May 13 '23 at 16:05
2

Easiest way is to use the intersection of two sets as follows:

list1 = [2,4,6,12]
list2 = [2,4,6,8,14]

print(max(set(list1).intersection(list2)))

...or...

print(max(set(list1) & set(list2)))

Output:

6
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
1

It would have been better for you to post what you had tried, but here's a simple solution.

def intersection(lst1, lst2):
    lst3 = [value for value in lst1 if value in lst2]
    lst3.sort()
    return lst3

lst1 = [2,4,6,12]
lst2 = [2,4,6,8,14]
var = intersection(lst1, lst2)
print(var[-1])
Pragmatic_Lee
  • 473
  • 1
  • 4
  • 10
0

you could have simply traverse and check if tour condition is satisfied the code simply is

list1=[9,1,2]
list2=[1,3,4]
import math
s=-math.inf
if len(list1)>len(list2):
 t=list1.copy() 
 list1=list2.copy() 
 list2=t.copy() 
for i in range(0,len(list1)):
 if list1[i] in list2 and list1[i]>=s:
  s=list1[i]
print(s)
Vishnu Balaji
  • 175
  • 1
  • 11
  • 1
    If the lists are not in sorted order, this can fail - e.g., list1 = [9, 1, 2] list2 = [1,3,4] will give a result of 9 (should be 1) – DarkKnight May 13 '23 at 16:12
  • the problem can be solved by initialising s with a very small number. the solution has been updated – Vishnu Balaji May 14 '23 at 06:50
0

Try this:

  1. A new list but only that contains the common integers of list1 and list2
  2. The highest integer in the new list is printd

It looks like:

#DECLARING LISTS
list1 = [2,4,6,12]
list2 = [2,4,6,8,14]

#CREATING NEW LIST
list3 = []

#CHECKING FOR COMMON INTEGERS
for i in list1:
    if i in list2:
        #ADDING COMMON INTEGERS TO THE NEW LIST
        list3.append(i)
    else:
        pass

#FINDING THE HIGHEST INTEGER & PRINTING IT
max_num = max(list3)
print(max_num)
Neeharika
  • 11
  • 3