Input: a = [1, 2, 3, 4, 5] b = [5, 6, 7, 8, 9] Output: True Input: a = [1, 2, 3, 4, 5] b = [6, 7, 8, 9] Output: False
Method 1: bypass list
By using traversal on two lists, we can check if there is at least one common element in them. When traversing two lists, if we find one element in common, we return true. After a full traversal and validation, if the elements don`t match, we return false.
True Fa lse
Method 2. Using Set and Property
Using set and property if there is at least one common element, then set (a) & amp; set (b) returns a positive integer, if it does not contain a positive integer then it returns 0. So we insert a into set_a and b into set_b and then check if set_a & amp; set_b for a positive integer or not.
Method 3: using set intersection
Using built-in function set intersections . a_set.intersection (b_set) returns a positive integer if there is at least one common element, otherwise it returns 0. So we insert a in set_a and b in set_b and then check a_set.intersection (b_set) and return depending on the value .
Python Workout isn’t designed to teach you Python, although I hope and expect that you’ll learn quite a bit along the way. It is meant to help you improve your understand- ing of Python and how to...
We are experiencing a renaissance of artificial intelligence, and everyone and their neighbor wants to be a part of this movement. That’s quite likely why you are browsing through this book. There a...
In the last decade, we have seen the impact of exponential advances in technology on the way we work, shop, communicate, and think. At the heart of this change is our ability to collect and gain insig...
90 SPECIFIC WAYS TO WRITE BETTER PYTHON. The Python programming language has unique strengths and charms that can be hard to grasp. Many programmers familiar with other languages often approach Pyt...