Found 26504 Articles for Server Side Programming

Python Program to solve Maximum Subarray Problem using Divide and Conquer

AmitDiwan
Updated on 19-Apr-2021 11:13:29

401 Views

When it is required solve the maximum subarray problem using the divide and conquer method, Below is the demonstration of the same −Example Live Demodef max_crossing_sum(my_array, low, mid, high):    sum_elements = 0    sum_left_elements = -10000    for i in range(mid, low-1, -1):    sum_elements = sum_elements + my_array[i]    if (sum_elements > sum_left_elements):       sum_left_elements = sum_elements    sum_elements = 0    sum_right_elements = -1000    for i in range(mid + 1, high + 1):       sum_elements = sum_elements + my_array[i]       if (sum_elements > sum_right_elements):     ... Read More

Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program

AmitDiwan
Updated on 19-Apr-2021 11:12:41

876 Views

When it is required to sort a list of tuples in increasing order based on last element of every tuple, a method is defined, that iterates over the tuple and performs a simple swap to achieve the same.Below is the demonstration of the same −Example Live Demodef sort_tuple(my_tup):    my_len = len(my_tup)    for i in range(0, my_len):       for j in range(0, my_len-i-1):          if (my_tup[j][-1] > my_tup[j + 1][-1]):             temp = my_tup[j]             my_tup[j]= my_tup[j + 1]         ... Read More

Python Program to Generate Random Numbers from 1 to 20 and Append Them to the List

AmitDiwan
Updated on 19-Apr-2021 11:12:22

568 Views

When it is required to generate random numbers within a given range and append them to a list, a method is defined, that generates random numbers and ‘append’s them to an empty list.Below is the demonstration of the same −Example Live Demoimport random def random_gen(beg, end, my_num):    my_result = []    for j in range(my_num):       my_result.append(random.randint(beg, end))    return my_result my_num = 19 beg = 1 end = 20 print("The number is :") print(my_num) print("The start and end values are :") print(beg, end) print("The elements are : ") print(random_gen(beg, end, my_num))OutputThe number is : 19 The start ... Read More

Python Program to Find the Cumulative Sum of a List where the ith Element is the Sum of the First i+1 Elements From The Original List

AmitDiwan
Updated on 19-Apr-2021 11:11:58

420 Views

When it is required to find the sum of a list where the specific element is sum of first few elements, a method is defined, that takes list as parameter. It uses list comprehension to find the cumulative sum.Below is the demonstration of the same −Example Live Demodef cumulative_sum(my_list):    cumulative_list = []    my_length = len(my_list)    cumulative_list = [sum(my_list[0:x:1]) for x in range(0, my_length+1)]    return cumulative_list[1:] my_list = [10, 20, 25, 30, 40, 50] print("The list is :") print(my_list) print("The cumulative sum is :") print (cumulative_sum(my_list))OutputThe list is : [10, 20, 25, 30, 40, 50] The cumulative ... Read More

Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10

AmitDiwan
Updated on 19-Apr-2021 11:11:40

1K+ Views

When it is required to find all numbers in a range where there are perfect square, and sum of digits in the number is less than 10, list comprehension is used.Below is the demonstration of the same −Example Live Demolower_limit = int(input(“Enter the lower range: “)) upper_limit = int(input(“Enter the upper range: “)) my_list = [] my_list = [x for x in range(lower_limit,upper_limit+1) if (int(x**0.5))**2==x and sum(list(map(int,str(x))))

Python Program to Create a List of Tuples with the First Element as the Number and Second Element as the Square of the Number

AmitDiwan
Updated on 19-Apr-2021 11:04:09

3K+ Views

When it is required to create a list of tuple, and have the first element as the number, and the second element as the square of the element, list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_list = [23, 42, 67, 89, 11, 32] print(“The list is “) print(my_list) print(“The resultant tuple is :”) my_result = [(val, pow(val, 2)) for val in my_list] print(my_result)OutputThe list is [23, 42, 67, 89, 11, 32] The resultant tuple is : [(23, 529), (42, 1764), (67, 4489), (89, 7921), (11, 121), (32, 1024)]ExplanationA list is defined, and is displayed on ... Read More

Python Program to Find the Second Largest Number in a List Using Bubble Sort

AmitDiwan
Updated on 19-Apr-2021 11:03:51

929 Views

When it is required to find the second largest number in a list using bubble sort, a method named ‘bubble_sort’ is defined, that sorts the elements of the list. Once this is done, another method named ‘get_second_largest’ is defined that returns the second element from the end as output.Below is the demonstration of the same −Example Live Demomy_list = [] my_input = int(input("Enter the number of elements...")) for i in range(1, my_input+1):    b=int(input("Enter the element..."))    my_list.append(b) for i in range(0, len(my_list)):    for j in range(0, len(my_list)-i-1):       if(my_list[j]>my_list[j+1]):          temp=my_list[j]       ... Read More

Python Program to Merge Two Lists and Sort it

AmitDiwan
Updated on 19-Apr-2021 11:03:33

2K+ Views

When it is required to merge two lists and sort them, a method can be defined that sorts the list using ‘sort’ method.Below is the demonstration of the same −Example Live Demodef merge_list(list_1, list_2):    merged_list = list_1 + list_2    merged_list.sort()    return(merged_list) list_1 = [20, 18, 9, 51, 48, 31] list_2 = [28, 33, 3, 22, 15, 20] print("The first list is :") print(list_1) print("The second list is :") print(list_2) print(merge_list(list_1, list_2))OutputThe first list is : [20, 18, 9, 51, 48, 31] The second list is : [28, 33, 3, 22, 15, 20] [3, 9, 15, 18, 20, ... Read More

Python Program to Find the Gravitational Force Acting Between Two Objects

AmitDiwan
Updated on 19-Apr-2021 11:02:59

457 Views

When it is required to find the gravitational force that acts between the two objects, a method named ‘find_gravity’ is used, and three parameters are passed to it.Below is the demonstration of the same −Example Live Demodef find_gravity(m_1, m_2, r):    G_val = 6.673*(10**-11)    F_val = (G_val*m_1*m_2)/(r**2)    return round(F_val, 2) m_1 = 6000000 m_2 = 1000000 r = 45 print("The gravitational force is: ") print(find_gravity(m_1, m_2, r), "N")OutputThe gravitational force is: 0.2 NExplanationA method named ‘find_gravity’ is defined, that takes three parameters.The gravitational force, and the gravitational constant are determined, and the force value is returned as ... Read More

Python Program to Check If Two Numbers are Amicable Numbers

AmitDiwan
Updated on 19-Apr-2021 10:53:57

822 Views

Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. When it is required to check if two numbers are amicable numbers, a method can be defined that iterates over the number, and uses the modulus operator. Another method is defined that calls the previously defined function to determine if two numbers are amicable or not.Below is the demonstration of the same −Example Live Demoimport math def divided_sum_val(my_val) :    res = 0    for i in range(2, int(math.sqrt(my_val)) + 1) :       ... Read More

Advertisements