Check If a String Can Be Obtained by Rotating Another String by 2 Places in Java

Dev Prakash Sharma
Updated on 05-Feb-2021 11:56:59

1K+ Views

Suppose we’ve two strings ‘a’ and ‘b’, the task is to find whether we can obtain string ‘b’ by rotating string ‘a’ exactly by 2 places in an anticlockwise or clockwise direction. For example, Input-1 −a = google b = legoogOutput −TrueExplanation − String ‘google’ can be rotated in an anticlockwise direction by two places, which results in the string ‘legoog’. Thus, we return True.Input-2 −a = tuorialst b = tutorialsOutput −FalseExplanation − String ‘tuorialst’ cannot be rotated by two places in any direction to get another string ‘tutorials’. Thus, we return False.The approach used to solve this problemFor the ... Read More

Delete First Node in a Singly Linked List in C++

Dev Prakash Sharma
Updated on 05-Feb-2021 11:54:11

10K+ Views

A linked list is a linear data structure that has multiple nodes that are connected with each other. Each node consists of two fields – Data Field and the address of the next node.Let us assume we have a singly linked list and we need to delete the first node from this linked list. For example, Input 1 − 4 → 3 → 2 → 1Output − 3 → 2 → 1 →Explanation − ‘4’ is the first node in the given singly linked list. After deleting the first node, the linked list will be 3→2→1.Input 2 − 1 → ... Read More

Bubble Sort in Go Language

Dev Prakash Sharma
Updated on 05-Feb-2021 11:53:00

10K+ Views

Bubble Sort is a sorting algorithm that works by swapping the elements that are in the wrong order. In multiple passes, it checks if the adjacent elements are in the right order (increasing) or not.The Time Complexity of the Bubble Sort is O(n^2) since it takes two nested loops to check the adjacent element.For example, let’s take the following unsorted array −22 15 11 45 13Bubble Sort Algorithm first traverses the whole array and then in another loop checks if the adjacent elements are in order or not.Thus, after sorting the elements will be, 11 13 15 22 45AlgorithmIn two ... Read More

Birthday Paradox in Python

Dev Prakash Sharma
Updated on 05-Feb-2021 11:50:17

1K+ Views

The birthday paradox is a very famous problem in the section of probability.Problem Statement − There are several people at a birthday party, some are having the same birthday collision. We need to find the approximate number of people at a birthday party on the basis of having the same birthday.In the probability, we know that the chance of getting ahead is 1/2, same as if we have some coins, the chance of getting 10 heads is 1/100 or 0.001.Let us understand the concept.The chance of two people having the different birthday is $$\frac{364}{365}$$ which is $$\lgroup1-\frac{1}{365}\rgroup$$ in a Non-leap ... Read More

Birthday Paradox in C++

Dev Prakash Sharma
Updated on 05-Feb-2021 11:47:09

794 Views

The birthday paradox is a very famous problem in the section of probability. The problem statement of this problem is stated as, There are several people at a birthday party, some are having the same birthday collision. We need to find the approximate no. of people at a birthday party on the basis of having the same birthday.In the probability we know that the chance of getting ahead is 1/2 same as if we have some coins, the chance of getting 10 heads is 1/100 or 0.001.Let us understand the concept, The chance of two people having the different birthday ... Read More

Change the Name of a Data Frame in R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:57:44

9K+ Views

To change the name of a data frame, we can set the original name to the new name. Now both of the names can be used. Most of the times the purpose behind changing the name of the data frame is that, the original name does not seem to be a valid name based on the characteristics of the data. For example, if we have normally distributed columns in the data frame then we can name it as normal_distribution. This will help everyone to understand the data belongs to normal distribution.Example1 Live Demoset.seed(24) x

Check If Variable Contains Number Greater Than 1 in R Data Frame

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:55:19

1K+ Views

The variables in an R data frame are referred to as the columns of the data frame. Sometimes we have a threshold value for a particular column and we need to check whether all the values in that column are greater than or less than the threshold. For this purpose, we can make use of ifelse function as shown in the below examples.Example1 Live DemoConsider the below data frame −set.seed(24) x

Create Random Sample of Week Days in R

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:52:32

456 Views

To create a vector of weekdays we can use the command weekdays(Sys.Date()+0:6) and if we want to create a random sample of week days then sample function can be used along with the weekdays command. For example, if we want to create a random sample of 20 days then it can be done as sample(weekdays(Sys.Date()+0:6),20,replace=TRUE).Examples Live DemoExample1

Replace Missing Values with Row Means in an R Data Frame

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:49:08

849 Views

If we have similar characteristics in each column of an R data frame then we can replace the missing values with row means. To replace the missing values with row means we can use the na.aggregate function of zoo package but we would need to use the transposed version of the data frame as na.aggregate works for column means.Example1Consider the below data frame − Live Demox1

Get Date from Year, Month, and Day in Java

Maruthi Krishna
Updated on 05-Feb-2021 10:47:28

7K+ Views

Using the of() methodThe of() method of the java.time.LocalDate class accepts the values of the year, month, and day of month as parameters, creates and returns an object of the LocalDate.ExampleLive Demoimport java.time.LocalDate; public class Test {    public static void main(String[] args) {       LocalDate date = LocalDate.of(2014, 9, 11);       System.out.println("Date Value: "+date);    } }OutputDate Value: 2014-09-11Using the GregorianCalendar classOne of the constructors of the java.util.GregorianCalendar class accepts the values of year, month and day of month as values and creates a Calendar object representing it.Exampleimport java.util.*; class Test {      public static void main(String args[]){ ... Read More

Advertisements