Suppose we are playing the Guess Game. The properties of this game is as follows −Player 1 will pick a number from 1 to n. player2 have to guess which number I picked. Every time player2 guess wrong, player1 will tell player2 whether the number is higher or lower.We can use the function guess(num) which will return 3 possible results as follows −-1 − Player1's number is lower1 − Player1's number is higher0 − Number is matchedSo, if the input is like n = 10, pick = 5, then the output will be 5.To solve this, we will follow these ... Read More
Selenium extracts the text of a webelement with the help of getText() method.Code Implementation with getText().Exampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class ExtractText { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); //Using id with # for css expression driver.findElement(By.cssSelector("#gsc-i-id1")).sendKeys("Selenium"); // extracting the text entered in console with getText() System.out.println(“The entered text is:” ... Read More
Suppose we have two arrays; we have to find their intersections.So, if the input is like [1, 5, 3, 6, 9], [2, 8, 9, 6, 7], then the output will be [9, 6]To solve this, we will follow these steps −Define two maps mp1, mp2Define an array resfor x in nums1(increase mp1[x] by 1)for x in nums2(increase mp2[x] by 1)for each key-value pair x in mp1cnt := 0cnt := minimum of value of x and mp2[key of x]if cnt > 0, then −insert key of x at the end of resreturn resExample Let us see the following implementation to get a ... Read More
Suppose we have an integer; we have to check whether that is a power of 4 or not.So, if the input is like 16, then the output will be True.To solve this, we will follow these steps −if num < 0, then −return falseif num & (num - 1) is non-zero, then −return falseif (num & 01010101010101010101010101010101) is zero, then −return falsereturn trueExample Let us see the following implementation to get better understanding − Live Demo#include using namespace std; class Solution { public: bool isPowerOfFour(int num){ if (num < 0) return false; ... Read More
Suppose we are playing a game called, Nim Game with another player. There is a heap of stones, each time one player takes turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. Player1 will take the first turn to remove the stones. Both of the players are very clever and have optimal strategies for the game. We have to devise an algorithm to determine whether player1 can win the game given the number of stones in the heap.So, if the input is like 5, then the output will be true, as ... Read More
We can reset or clear an edit box in Selenium with the help of clear() method.Code Implementation with clear().Exampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class ResetText { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); //entering text in the edit box driver.findElement(By.cssSelector("#gsc-i- id1")).sendKeys("Selenium"); Thread.sleep(1000); // resetting text from the edit box with ... Read More
Suppose we have a pattern and a string str, find if str follows the same pattern. Here follow means there is a bijection between a letter in pattern and a non-empty word in str.So, if the input is like pattern = "cbbc", str = "word pattern pattern word", then the output will be True.To solve this, we will follow these steps −strcin := strDefine an array of wordsfor each word in strcininsert word at the end of wordsDefine one map p2ii := 0pat := empty stringfor c in pattern −if c is not member of p2i, then −(increase i by ... Read More
The relative or friendly locators in Selenium 4.0 are available with the tagname attribute of the element.above() - Webelement located above with respect to the specified element.Syntax −driver.findElement(withTagName(“”).above(element));below() - Webelement located below with respect to the specified element.Syntax −driver.findElement(withTagName(“”).below(element));toLeftof() - Webelement located to the left of the specified element.Syntax −driver.findElement(withTagName(“”).toLeftOf(element));toRightOf() - Webelement located to the right of the specified element.Syntax −driver.findElement(withTagName(“”).toRightOf(element));Code Implementation with relative Locators.Exampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import static org.openqa.selenium.support.locators.RelativeLocator .withTagName; public class RelLocator { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver ... Read More
Suppose we want to implement one stack using a queue. We have to define these methods for the stack.push(x) − Push x onto stack.pop() − Delete and return top element from stacktop() − Return the top element from stack.empty() − Return whether the stack is empty or not.So, if we call the functions push(10), push(20), then call pop(), pop(), then the output will be 20, 10To solve this, we will follow these steps −Define one deque qDefine a function push(), this will take x, insert x at the beginning of qDefine a function pop()k := first element of qdelete front ... Read More
The various important exceptions in Selenium are listed below −TimeOutException − This exception is thrown if an action does not complete in a particular duration. If the page element does not load even after the waits.driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS) ; driver.get(“https://www.tutorialspoint.com/index.htm” );In the above program, an implicit wait of 15 seconds is added. If the page https://www.tutorialspoint.com/index.htm doesn’t load in 15 seconds, then TimeoutException will be thrown.NoSuchElementException − This exception happens if the web element with particular attributes does not exist on the page. This exception class is a subclass of NotFoundException and is thrown if the driver is not successful in ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP