Server Side Programming Articles - Page 1575 of 2650

Get last element of each sublist in Python

Pradeep Elance
Updated on 09-Sep-2020 12:33:34

2K+ Views

A list in python can also contain lists inside it as elements. These nested lists are called sublists. In this article we will solve the challenge of retrieving only the last element of each sublist in a given list.Using for loopIt is a very simple approach in which we loop through the sublists fetching the item at index -1 in them. A for loop is used for this purpose as shown below.Example Live DemoAlist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]] print("Given List:", Alist) print("Lastst Items from sublists:") for item in Alist:    print((item[-1]))OutputRunning the above code gives us ... Read More

Get first element of each sublist in Python

Pradeep Elance
Updated on 09-Sep-2020 12:32:15

5K+ Views

A list in python can also contain lists inside it as elements. These nested lists are called sublists. In this article we will solve the challenge of retrieving only the first element of each sublist in a given list.Using for loopIt is a very simple approach in which we loop through the sublists fetching the item at index 0 in them. A for loop is used for this purpose as shown below.Example Live DemoAlist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]] print("Given List:", Alist) print("First Items from sublists:") for item in Alist:    print((item[0]))OutputRunning the above code gives us ... Read More

Get first and last elements of a list in Python

Pradeep Elance
Updated on 09-Sep-2020 12:26:46

15K+ Views

There may be situation when you need to get the first and last element of the list. The tricky part here is you have to keep track of the length of the list while finding out these elements from the lists. Below are the approaches which we can use to achieve this. But of course all the approaches involve using the index of the elements in the list.Using only indexIn any list the first element is assigned index value 0 and the last element can be considered as a value -1. So we apply these index values to the list ... Read More

Maximum possible middle element of the array after deleting exactly k elements in C++

Ayush Gupta
Updated on 09-Sep-2020 12:32:14

218 Views

In this tutorial, we will be discussing a program to find maximum possible middle element of the array after deleting exactly k elementsFor this we will be provided with an array of size N and an integer K. Our task is to reduce K elements from the array such that the middle element of the resulting array is maximum.Example Live Demo#include using namespace std; //calculating maximum value of middle element int maximum_middle_value(int n, int k, int arr[]) {    int ans = -1;    int low = (n + 1 - k) / 2;    int high = (n + ... Read More

Get dictionary keys as a list in Python

Pradeep Elance
Updated on 09-Sep-2020 12:24:32

428 Views

For many programs getting the keys from a dictionary is important input to be used by some other program which rely on this dictionary. In this article we are going to see how to capture the keys as a list.Using dict.keysThis is a very direct method of accessing the keys. This method is available as a in-built method.Example Live DemoAdict = {1:'Sun', 2:'Mon', 3:'Tue', 4:'Wed'} print("The given dictionary is : ", Adict) print(list(Adict.keys()))OutputRunning the above code gives us the following result −The given dictionary is :    {1: 'Sun', 2: 'Mon', 3: 'Tue', 4: 'Wed'} [1, 2, 3, 4]Using *The ... Read More

Maximum possible intersection by moving centers of line segments in C++

Ayush Gupta
Updated on 09-Sep-2020 12:21:51

129 Views

In this tutorial, we will be discussing a program to find maximum possible intersection by moving centers of line segmentsFor this we will be provided with the center of three line segments and their length. Our task is to move their center by K distance to increase the length of intersection region.Example Live Demo#include using namespace std; //finding maximum intersection int max_intersection(int* center, int length, int k) {    sort(center, center + 3);    if (center[2] - center[0] >= 2 * k + length) {       return 0;    }    else if (center[2] - center[0] >= 2 ... Read More

Count unique sublists within list in Python

Pradeep Elance
Updated on 09-Sep-2020 12:21:49

570 Views

A Python list can also contain sublist. A sublist itself is a list nested within a bigger list. In this article we will see how to count the number of unique sublists within a given list.Using CounterCounter is a subclass of Dictionary and used to keep track of elements and their count. It is also considered as an unordered collection where elements are stored as Dict keys and their count as dict value. So in the below example we directly take a list which has sublists.Example Live Demofrom collections import Counter # Given List Alist = [['Mon'], ['Tue', 'Wed'], ['Tue', 'Wed']] ... Read More

Maximum possible difference of two subsets of an array in C++

Ayush Gupta
Updated on 09-Sep-2020 12:20:29

579 Views

In this tutorial, we will be discussing a program to find maximum possible difference of two subsets of an arrayFor this we will be provided with an array containing one or two instances of few random integers. Our task is to create two subsets of that array such that the difference of their sum is maximum and no subset contains repetitive numbers.Example Live Demo#include using namespace std; //finding maximum subset difference int maxDiff(int arr[], int n) {    int SubsetSum_1 = 0, SubsetSum_2 = 0;    for (int i = 0; i

Maximum points of intersection n lines in C++

Ayush Gupta
Updated on 09-Sep-2020 12:18:22

203 Views

In this tutorial, we will be discussing a program to find maximum points of intersection n linesFor this we will be provided with a number of straight lines. Our task is to find the maximum number of intersections the given number of lines meet.Example Live Demo#include using namespace std; #define ll long int //finding maximum intersection points ll countMaxIntersect(ll n) {    return (n) * (n - 1) / 2; } int main() {    ll n = 8;    cout

Maximum points of intersection n circles in C++

Ayush Gupta
Updated on 09-Sep-2020 12:16:37

171 Views

In this tutorial, we will be discussing a program to find maximum points of intersection n circlesFor this we will be provided with the number of circles. Our task is to find the maximum number of intersections the given number of circles meet.Example Live Demo#include using namespace std; //returning maximum intersections int intersection(int n) {    return n * (n - 1); } int main() {    cout

Advertisements