One Liners on BitBangedhttps://bitbanged.com/posts/one-liners/Recent content in One Liners on BitBangedHugo -- gohugo.ioenutkarsh@bitbanged.com (Utkarsh Verma)utkarsh@bitbanged.com (Utkarsh Verma)Thu, 26 Mar 2020 07:40:56 +0530Remove duplicate elements from listshttps://bitbanged.com/posts/one-liners/remove-duplicate-elements-from-lists/Thu, 26 Mar 2020 07:40:56 +0530utkarsh@bitbanged.com (Utkarsh Verma)https://bitbanged.com/posts/one-liners/remove-duplicate-elements-from-lists/Question #With a given list of integers, write a python program to print this list after filtering out the duplicate values.
Input Format: #In one line take the elements of the list L with each element separated by a space.
Output Format: #Print the elements of the modified list in one line with each element separated by a space.
Solution #Cool way (Neat as well): #print(*list(map(int, set(list(input().split()))))) Explanation: #Sets in Python can only store unique elements.Enter, semiprime numbershttps://bitbanged.com/posts/one-liners/enter-semiprime-numbers/Wed, 25 Mar 2020 19:23:06 +0530utkarsh@bitbanged.com (Utkarsh Verma)https://bitbanged.com/posts/one-liners/enter-semiprime-numbers/Question #A semiprime number is an integer which can be expressed as a product of two distinct primes.
For example, $15 = 3 \times 5$ is a semiprime number while $9 = 3 \times 3$ isn’t.
Test if a natural number $n$ is semiprime or not.
Solution #If we’re planning to approach this problem, we should start from the ground up, i.e. by starting with check if an integer $n$ is prime or not.Behold, the factorialhttps://bitbanged.com/posts/one-liners/behold-the-factorial/Wed, 25 Mar 2020 15:34:59 +0530utkarsh@bitbanged.com (Utkarsh Verma)https://bitbanged.com/posts/one-liners/behold-the-factorial/Question #Find the factorial $n!$, of a positive integer $n$.
The factorial $n!$ is defined as: $$n! = \prod_{i=1}^{n}{i} = 1 \times 2 \times …\times n$$
Solution #This is a fairly simple problem which can be trivially solved using loops.
fact = 1 for i in range(1, n+1): fact *= i print(fact) We can approach the same thing using recursion also, by exploiting the fact that $n! = n \times (n-1)!