Found 26504 Articles for Server Side Programming

Count ways to express ‘n’ as sum of odd integers in C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:12:56

343 Views

Given an integer n as input. The goal is to find the number of ways in which we can represent ‘n’ as the sum of odd integers. For example, if n is 3 it can be represented as sum ( 1+1+1 ) and (3) so total 2 ways.For ExampleInputn=6OutputCount of ways to express ‘n’ as sum of odd integers are: 8ExplanationThe ways in which we can express ‘n’ as sum of odd integers − 1. 1+1+1+1+1+1 2. 3+1+1+1 3. 1+3+1+1 4. 1+1+3+1 5. 1+1+1+3 6. 3+3 7. 1+5 8. 5+1Inputn=9OutputCount of ways to express ‘n’ as sum of odd integers ... Read More

How to remove names from a named vector in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:10:12

7K+ Views

To assign names to the values of vector, we can use names function and the removal of names can be done by using unname function. For example, if we have a vector x that has elements with names and we want to remove the names of those elements then we can use the command unname(x).Example1Live Demo> x1 names(x1) x1OutputG K N V P F F A P D L N K J V H S L F C M F H T I V 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... Read More

Count ways to divide circle using N non-intersecting chords in C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:11:06

442 Views

Given an integer N as input for a number of chords in a circle with 2*N end points. The goal is to count the ways in which we can divide that circle using such chords so that no chord intersects with each other.For N=3, points will be 6, 1 way of getting 3 chords is between 1−2, 3−4, 5−6Other ways −1−6, 2−5, 3−4 1−2, 3−6, 4−5 1−4, 2−3, 5−6 1−6, 2−3, 4−5Total 5 ways.For ExampleInputN=4OutputCount of ways to divide circle using N non-intersecting chords are: 14ExplanationThere will be a total 8 points between which we can draw chords. After drawing ... Read More

How to convert a string vector into title case in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:08:33

667 Views

We cannot be sure about the data characteristics we get for analysis and mostly it is not well organised, thus, the first task would be to make it more organised. The string values not in title case should also be taken care of if it is especially supposed to be in title case. For this purpose, we can str_to_title function of stringr package.Example1Live Demo> x1 x1Output[1] "india" "united kingdom" "indonesia" "canada" [5] "canada" "india" "united kingdom" "canada" [9] "indonesia" "united kingdom" "indonesia" "canada" [13] "russia" "indonesia" "canada" "russia" [17] "united kingdom" "russia" "russia" "india" [21] "united kingdom" "india" "india" "united ... Read More

How to set a specific value for a range in an R vector?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:05:46

353 Views

Suppose we have a vector that contains hundred values starting from 1 to 100 and we want to set values greater than 5 and less than 96 to 5 then it can be done with the help of ifelse function. For example, if such vector is named as x then the command will be as follows −ifelse(x>5 & x x1 x1Output[1] 2 4 1 6 7 4 0 1 6 4 0 7 1 3 3 1 4 6 7 7 0 2 7 3 9 4 4 8 6 3 3 5 4 5 6 5 6 [38] 2 ... Read More

How to find the number of positive values in an R vector?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:04:20

2K+ Views

We know that positive values are greater than 0, therefore, we can use this condition with length function to find the number of positive values in a vector. For example, if we have a vector x that contains some positive and some negative values and we want to find the number of values that are positive then we can use the command length(x[x>0]).Example1Live Demo> x1 x1Output[1] 0.21314126 1.23449384 -1.02721325 -0.23168203 -1.36368881 -0.82416287 [7] 0.31224895 -0.90773340 0.10312288 -0.38914253 0.01196499 0.44875369 [13] 0.40820219 0.70172242 -0.23766272 -0.01023414 1.12403398 0.05837136 [19] -0.67403563 -0.26134292 0.31192384 -1.25116951 0.22115555 0.46544495 [25] 0.76567139 0.76948285 -1.42650924 0.24616899 0.18043015 1.04896235 ... Read More

How to check if all values in a vector are integer or not in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:03:06

2K+ Views

To check whether all values in a vector in R are integer or not, we can round the vector using floor function then subtract the vector values from it and check whether the output is zero or not. If the output will be zero that means the value is integer otherwise it is not. The floor function returns the largest integer that is smaller or equal to the actual value. For example, if we have a vector x then it can be done as x-floor(x)==0.Example1Live Demo> x1 x1Output[1] 4 0 2 8 6 1 3 7 3 4 0 7 ... Read More

How to convert a matrix into a data frame with column names and row names as variables in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:01:36

1K+ Views

To convert a matrix into a data frame with column names and row names as variables, we first need to convert the matrix into a table and then read it as data frame using as.data.frame. For example, if we have a matrix M then it can be done by using the below command −as.data.frame(as.table(M))Example1Live Demo> M1 M1Output[, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [1, ] 1 7 13 19 25 31 [2, ] 2 8 14 20 26 32 [3, ] 3 9 15 21 27 33 [4, ] 4 10 16 22 28 34 ... Read More

How to create an empty data frame with fixed number of rows and without columns in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:00:17

2K+ Views

To create an empty data frame with fixed number of rows but no columns, we can use data.frame function along with the matrix function. That means we need to create a matrix without any column using matrix and save it in a data frame using data.frame function as shown in the below examples.Example1Live Demo> df1 df1Outputdata frame with 0 columns and 10 rows Example2Live Demo> df2 df2Outputdata frame with 0 columns and 100 rows Example3Live Demo> df3 df3Outputdata frame with 0 columns and 39 rows Example4Live Demo> df4 df4Outputdata frame with 0 columns and 20 rows Example5Live Demo> df5 df5Outputdata ... Read More

Count trailing zeros in factorial of a number in C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:09:15

2K+ Views

Given an integer number as input. The goal is to find the number of trailing zeroes in the factorial calculated for that number. A factorial of a number N is a product of all numbers in the range [1, N].We know that we get a trailing zero only if the number is multiple of 10 or has a factor pair (2, 5). In all factorials of any number greater than 5, we have a number of 2s more than 5s in prime factorization of that number. Dividing a number by powers of 5 will give us the count of 5s ... Read More

Advertisements