![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find the product of three elements when all are unique in Python
Suppose we have three numbers, x, y, and z, we have to find their product, but if any two numbers are equal, they do not count.
So, if the input is like x = 5, y = 4, z = 2, then the output will be 40, as all three numbers are distinct so their product is 5 * 4 * 2 = 40
To solve this, we will follow these steps −
- temp_set := a new set
- remove := a new set
- for each i in [x,y,z], do
- if i is in temp_set, then
- insert i into the set called remove
- insert i in to the set temp_set
- if i is in temp_set, then
- for each i in remove, do
- delete i from temp_set
- multiplied := 1
- for each i in temp_set, do
- multiplied := multiplied * i
- return multiplied
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, x, y, z): temp_set = set() remove = set() for i in [x, y, z]: if i in temp_set: remove.add(i) temp_set.add(i) for i in remove: temp_set.remove(i) multiplied = 1 for i in temp_set: multiplied *= i return multiplied ob = Solution() print(ob.solve(5, 4, 2))
Input
5, 4, 2
Output
40
Advertisements