Programming Articles - Page 912 of 3363

Rearrange characters in a string such that no two adjacent are same in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:13:17

1K+ Views

We are given a string, let's say, str of any given length. The task is to rearrange the given string in such a manner that there won't be the same adjacent characters arranged together in the resultant string.Let us see various input output scenarios for this −Input − string str = "itinn"Output − Rearrangement of characters in a string such that no two adjacent are same is: initn.Explanation − We are given a string type variable let’s say, str. Now we will rearrange the characters of an input string in such a manner that no two same characters occur at the same position ... Read More

How to find the frequency with subsetting in base R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:36:30

422 Views

The easiest way to find the frequency is to create a table for the provided vector or data frame column and it can be done with table function. But, if the frequency needs to be found with subsetting then subset function and transform function will also be required as shown in the below examples.Example 1Following snippet creates a sample data frame −head(ChickWeight, 20)OutputThe following dataframe is created − weight Time Chick Diet 1   42   0   1     1 2   51  2    1     1 3   59  4    1     1 4 ... Read More

Rectangle with minimum possible difference between the length and the width in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:09:52

309 Views

Given an area of rectangle as input. The goal is to find the sides of a rectangle such that the difference between the length and breadth is minimum.Area of Rectangle = length * breadth.ExamplesInput − Area = 100Output −Side of Rectangle with minimum difference:Length = 10, Breadth = 10Explanation − Sides with area = 100.2 - 50, 4 - 25, 5 - 20, 10 - 10. Sides with minimum difference are 10-10 with difference = 0. As we know tha square is a rectangle with equal length on all sides.Input − Area = 254Output − Side of Rectangle with minimum difference :Length = 127, Breadth ... Read More

How to create a block diagonal matrix using a matrix in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:24:46

2K+ Views

To create a block diagonal matrix using a matrix in R, we can use bdiag function of Matrix package.For Example, if we have a matrix called M and we want to create block diagonal using M 4 times by using the below command −bdiag(replicate(4,M,simplify=FALSE))Check out the Examples given below to understand how it can be done.Example 1Following snippet creates a sample matrix −M1

How to create a table with sub totals for every row and column in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:07:25

3K+ Views

The sub totals for every row and column are actually called marginal sums. Therefore, we can use addmargins function to our table to get the sub totals for every row and column.For example, if we have table called TABLE then we can add the sub totals by using the command given below −TABLE

Recursive program to insert a star between pair of identical characters in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:06:09

736 Views

Given a string str1 as input. The goal is to insert a ‘*’ between a pair of identical characters in the input string and return the resultant string using a recursive approach.If the input string is str1= "wellness" then output will be "wel*lnes*s"ExamplesInput − str1="happiness"Output − String after adding * : hap*pines*sExplanation − Adding * between pairs pp and ss will gives us resultant string hap*pines*sInput − str1=”swimmmmingggg pooool”Output − String after adding * : swim*m*m*ming*g*g*g po*o*o*olExplanation − Adding * between pairs mm, gg and oo will gives us resultant string swim*m*m*ming*g*g*g po*o*o*olApproach used in the below program is as followsIn this approach take string ... Read More

How to find the sample size for two sample proportion tests with given power in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:01:21

1K+ Views

To find the sample size for two sample proportion tests with given power, we can use the function power.prop.test where we need to at least pass the two proportions and power.By default the significance level will be taken as 0.05 and if we want to change it then sig.level argument will be used.Given below are some examples with the display of significance levels.Example 1Use the code given below to find the sample size for two sample proportion tests −power.prop.test(p1=8/20, p2=6/20, power=0.90, sig.level=0.05)OutputIf you execute the above given snippet, it generates the following Output for the two-sample comparison of proportions power ... Read More

Recursive Approach to find nth node from the end in the linked list in C++

Sunidhi Bansal
Updated on 03-Nov-2021 06:59:53

636 Views

Given a Singly linked list and positive integer N as input. The goal is to find the Nth node from the end in the given list using recursion. If the input list has nodes a → b → c → d → e → f and N is 4 then the 4th node from the end will be c.We will first traverse till the last node in the list and while returning from recursion (backtracking) increment count. When count is equal to N then return pointer to current node as result.Let us see various input output scenarios for this −Input − ... Read More

Recursive approach for alternating split of Linked List in C++

Sunidhi Bansal
Updated on 03-Nov-2021 06:56:48

413 Views

Given a Singly linked list as input. The goal is to split the list into two singly linked lists that have alternate nodes of the original list. If the input list has nodes a → b → c → d → e → f then after the split, two sub-lists will be a → c → e and b → d → f.We will take two pointers N1 and N2 one pointing to the head of the original list and another pointing to the head → next. Now move both pointers to the next of next node and create sublists.ExamplesInput − ... Read More

How to remove rows that have NA in R data frames stored in a list?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:58:36

1K+ Views

To remove rows that have NA in R data frames stored in a list, we can use lapply function along with na.omit function.For example, if we have a list called LIST that contains some data frames each containing few missing values then the removal of rows having missing values from these data frames can be done by using the command given below −lapply(LIST,na.omit)Check out the below given example to understand how it works.ExampleFollowing snippet creates a sample data frame −df1

Advertisements