Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 387 of 2547
Python Program to Merge Two Lists and Sort it
When it is required to merge two lists and sort them, Python provides several approaches. You can use list concatenation with the sort() method, or combine merging and sorting in one step using sorted(). Below is the demonstration of the same − Method 1: Using List Concatenation and sort() Example Define a function that merges two lists and sorts them in-place − def merge_list(list_1, list_2): merged_list = list_1 + list_2 merged_list.sort() return merged_list numbers_1 = [20, 18, 9, 51, 48, 31] ...
Read MorePython Program to Find the Gravitational Force Acting Between Two Objects
When calculating the gravitational force between two objects, we can use Newton's Law of Universal Gravitation. This law states that every particle attracts every other particle with a force proportional to the product of their masses and inversely proportional to the square of the distance between them. The formula is: F = G × (m₁ × m₂) / r², where G is the gravitational constant (6.673 × 10⁻¹¹ N⋅m²/kg²). Example Here's how to calculate gravitational force using a Python function ? def find_gravity(m_1, m_2, r): G_val = 6.673*(10**-11) # Gravitational ...
Read MorePython Program to Check If Two Numbers are Amicable Numbers
Amicable numbers are two different numbers where the sum of proper divisors of each number equals the other number. For example, 220 and 284 are amicable because the proper divisors of 220 sum to 284, and the proper divisors of 284 sum to 220. What are Proper Divisors? Proper divisors are all positive divisors of a number except the number itself. For example, proper divisors of 220 are: 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110. Method 1: Using Mathematical Approach This method finds divisors efficiently by checking only up to the square ...
Read MorePython Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List
When you need to compute a polynomial equation where the coefficients are stored in a list, you can use nested loops to calculate each term. A polynomial like 2x³ + 5x² + 3x + 0 can be evaluated by multiplying each coefficient by the corresponding power of x. Understanding Polynomial Evaluation For a polynomial represented as a list [2, 5, 3, 0], this corresponds to: 2x³ (coefficient 2, power 3) 5x² (coefficient 5, power 2) 3x¹ (coefficient 3, power 1) 0x⁰ (coefficient 0, power 0) Example Here's how to evaluate the polynomial when ...
Read MorePython Program to Check if a Number is a Strong Number
A strong number is a number whose sum of all digits' factorial equals the original number. For example, 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145. We can check this using the modulus operator and loops to extract digits and calculate factorials. Example Let's check if 145 is a strong number ? def factorial(n): if n == 0 or n == 1: return 1 fact = 1 ...
Read MorePython Program to Check if a Number is a Perfect Number
A Perfect Number is a positive integer that equals the sum of its positive divisors, excluding the number itself. For example, 6 is a perfect number because its divisors (1, 2, 3) sum to 6. The first few perfect numbers are 6, 28, 496, and 8128. Basic Method to Check Perfect Number We can iterate through all numbers from 1 to n-1 and check if they divide n evenly ? n = 6 my_sum = 0 for i in range(1, n): if n % i == 0: ...
Read MorePython Program to Convert Binary to Gray Code
When it is required to convert binary code to gray code, a method is defined that performs the XOR operation. Gray code is a binary numeral system where two successive values differ in only one bit, making it useful in digital circuits and error correction. Below is the demonstration of the same − Understanding Gray Code Conversion The conversion from binary to Gray code follows a simple rule: The most significant bit of Gray code is the same as the binary code For other bits, the Gray code bit is the XOR of the current ...
Read MorePython Program to Generate Gray Codes using Recursion
Gray codes are binary codes where consecutive numbers differ by exactly one bit. Python can generate Gray codes using recursion by building sequences iteratively. This approach uses bit manipulation and string operations to create the complete Gray code sequence. What are Gray Codes? Gray codes (also called reflected binary codes) are sequences where adjacent values differ by only one bit position. For example, in 2-bit Gray code: 00 → 01 → 11 → 10, each step changes exactly one bit. Gray Code Generation Algorithm The algorithm starts with the base case (0, 1) and recursively builds ...
Read MorePython Program to Clear the Rightmost Set Bit of a Number
When it is required to clear the rightmost set bit of a number, the bitwise AND (&) operator can be used with a clever mathematical property. This technique involves performing n & (n-1) which effectively removes the rightmost 1 bit from the binary representation. How It Works The operation n & (n-1) works because when you subtract 1 from a number, all bits to the right of the rightmost set bit get flipped, including the rightmost set bit itself. The AND operation then clears that bit. Binary Visualization Let's see how this works with number 6 ...
Read MorePython Program to Search the Number of Times a Particular Number Occurs in a List
When it is required to search the frequency of a number in a list, Python provides multiple approaches. You can define a custom method, use the built-in count() method, or utilize dictionary-based counting with collections.Counter. Method 1: Using Custom Function Define a function that iterates through the list and counts occurrences ? def count_num(my_list, x_val): my_counter = 0 for elem in my_list: if (elem == x_val): my_counter = ...
Read More