Programming Articles - Page 2171 of 3363

Multiplication of two Matrices in Single line using Numpy in Python

Pradeep Elance
Updated on 07-Jan-2020 06:41:16

762 Views

Matrix multiplication is a lengthy process where each element from each row and column of the matrixes are to be multiplied and added in a certain way. For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. The result matrix has the number of rows of the first and the number of columns of the second matrix.For smaller matrices we may design nested for loops and find the result. For bigger matrices we need some built in functionality in python to tackle this. We will see both ... Read More

All possible permutations of N lists in Python

Pradeep Elance
Updated on 07-Jan-2020 06:34:45

1K+ Views

If we have two lists and we need to combine each element of the first element with each element of the second list, then we have the below approaches.Using For LoopIn this straight forward approach we create a list of lists containing the permutation of elements from each list. we design a for loop within another for loop. The inner for loop refers to the second list and Outer follow refers to the first list.Example Live DemoA = [5, 8] B = [10, 15, 20] print ("The given lists : ", A, B) permutations = [[m, n] for m in ... Read More

Add leading Zeros to Python string

Pradeep Elance
Updated on 18-Feb-2020 11:22:50

669 Views

We may sometimes need to append zeros as string to various data elements in python. There may the reason for formatting and nice representation or there may be the reason for some calculations where these values will act as input. Below are the methods which we will use for this purpose.Using format()Here we take a DataFrame and apply the format function to the column wher we need to append the zeros as strings. The lambda method is used to apply the function repeatedly.Example Live Demoimport pandas as pd string = {'Column' : ['HOPE', 'FOR', 'THE', 'BEST']} dataframe=pd.DataFrame(string) print("given column is ") ... Read More

How to populate a Map using a lambda expression in Java?

raja
Updated on 13-Jul-2020 09:11:10

2K+ Views

A Map is a collection object that maps keys to values in Java. The data can be stored in key/value pairs and each key is unique. These key/value pairs are also called map entries.In the below example, we can populate a Map using a lambda expression. We have passed Character and Runnable arguments to Map object and pass a lambda expression as the second argument in the put() method of Map class. We need to pass command-line arguments whether the user enters 'h' for Help and 'q' for quit with the help of Scanner class.Exampleimport java.util.*; public class PopulateUsingMapLambdaTest {    public static void main(String[] args) {       Map map = new ... Read More

Convert all lowercase characters to uppercase whose ASCII value is co-prime with k in C++

Ayush Gupta
Updated on 06-Jan-2020 12:24:57

155 Views

In this tutorial, we will be discussing a program to convert all lowercase characters to uppercase whose ASCII value is co-prime with k.For this we will be provided with a string and an integer value k. Our task is to traverse through the given string and change to uppercase all those characters whose ASCII value is co-prime with the given integer k.Example Live Demo#include using namespace std; //modifying the given string void convert_string(string s, int k){    int l = s.length();    for (int i = 0; i < l; i++) {       int ascii = (int)s[i];   ... Read More

Convert a tree to forest of even nodes in C++

Ayush Gupta
Updated on 06-Jan-2020 12:14:35

259 Views

In this tutorial, we will be discussing a program to convert a tree to a forest of even nodes.For this we will be provided with a binary tree of say N nodes. Our task is to calculate the maximum number of edges that can be removed to get forest of even nodes.Example Live Demo#include #define N 12 using namespace std; //returning the number of nodes of subtree //having the root node int depth_search(vector tree[N], int visit[N], int *ans, int node){    int num = 0, temp = 0;    //marking nodes as visited    visit[node] = 1;    for (int i ... Read More

Convert a string to hexadecimal ASCII values in C++

Ayush Gupta
Updated on 06-Jan-2020 12:10:54

2K+ Views

In this tutorial, we will be discussing a program to convert a string to hexadecimal ASCII values.For this we will be provided with a string of characters. Our task is to print that particular given string into its hexadecimal equivalent.Example Live Demo#include #include //converting string to hexadecimal void convert_hexa(char* input, char* output){    int loop=0;    int i=0;    while(input[loop] != '\0'){       sprintf((char*)(output+i), "%02X", input[loop]);       loop+=1;       i+=2;    }    //marking the end of the string    output[i++] = '\0'; } int main(){    char ascii_str[] = "tutorials point";   ... Read More

Convert a String into a square matrix grid of characters in C++

Ayush Gupta
Updated on 06-Jan-2020 12:06:36

412 Views

In this tutorial, we will be discussing a program to convert a string into a square matrix grid of characters.For this we will be provided with a string of characters. Our task is to print that particular string in the format of a matrix grid having a certain number of rows and columns.Example Live Demo#include using namespace std; //converting the string in grid format void convert_grid(string str){    int l = str.length();    int k = 0, row, column;    row = floor(sqrt(l));    column = ceil(sqrt(l));    if (row * column < l)       row = column; ... Read More

Convert a sentence into its equivalent mobile numeric keypad sequence in C++

Ayush Gupta
Updated on 06-Jan-2020 12:02:35

740 Views

In this tutorial, we will be discussing a program to convert a sentence into its equivalent mobile numeric keypad sequence.For this we will be provided with a string of alphabetical characters. Our task is to print the numeric equivalent of the string i.e the numerical sequence of the keys to type that particular string.Example Live Demo#include using namespace std; //computing the numerical sequence string calc_sequence(string arr[], string input){    string output = "";    //length of input string    int n = input.length();    for (int i=0; i

Convert a number of length N such that it contains any one digit at least 'K' times in C++

Ayush Gupta
Updated on 06-Jan-2020 11:57:57

140 Views

In this tutorial, we will be discussing a program to convert a number of length N such that it contains any one digit at least ‘K’ times.For this we will be provided with a number of given length N. Our task is to convert the digits in the given number such that any one digit gets repeated at least ‘K’ times. Also, you have to calculate the cost of this operation which is the absolute difference between the two and finally print the minimum cost.Example Live Demo#include using namespace std; //calculating the minimum value and final number int get_final(int n, ... Read More

Advertisements