Check If You Can Return to 12 O'Clock by Adding or Subtracting Seconds in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:12:17

95 Views

Suppose we have an array of n different second values. We have to check whether it is possible to start from 12'O clock and go back to 12 by only adding or subtracting the given seconds or not. We can use all given seconds exactly once, we can either add seconds or subtract it.So, if the input is like seconds = [40, 90, 50], then the output will be True as it can add 40 then subtract 90 then again add 50.To solve this, we will follow these steps −size := 2^(length of seconds array)for c in range 0 to ... Read More

Form String B from A Under Given Constraints in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:10:50

131 Views

Suppose we have two strings s and t, and two values p and q. We have to check whether it is possible to get t from s such that s is separated into groups of p number of characters except the last group which will have characters ≤ p and we can pick at most q number of characters from each group, and also order of characters in t must be same as s.So, if the input is like s = "mnonnopeqrst", t = "moprst", p = 5, q = 2, then the output will be True as we can ... Read More

Check If a Straight Line Can Be Drawn with Given Direction Cosines in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:08:58

232 Views

Suppose we have three direction cosines l, m and n in 3-D space, we have to check whether it is possible to draw a straight line with these direction cosines or not.So, if the input is like l = 0.42426 m = 0.56568 n = 0.7071, then the output will be True as this the direction cosines of a vector {3, 4, 5}.To solve this, we will follow some rule asl = cos(a), where a is angle between the straight line and x-axism = cos(b), where b is angle between the straight line and y-axisn = cos(c), where c is ... Read More

Check If It Is Possible to Create a Polygon with Given n Sides in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:06:16

354 Views

Suppose we have an array nums that contain the size of n sides. We have to check whether we can form a polygon with all of the given sides or not.So, if the input is like nums = [3, 4, 5], then the output will be True because there are three sides and sum of any two sides is larger than the 3rd one. To solve this, we will use this property where length of one side is smaller than the sum of all other sides.To solve this, we will follow these steps −sort the list numsif last element of ... Read More

Check If It Is Possible to Create a Polygon with a Given Angle in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:05:11

255 Views

Suppose we have an angle a. We have to check whether we can make a regular polygon where all angles are same as a or not.So, if the input is like a = 120, then the output will be True the pentagon has all angles same as 120°. As we know$$Interior Angle(a)=\frac{180\times(n-2)}{n}\begin{bmatrix} n=number of sides of polygon\end{bmatrix}$$ $$¿n=\frac{360}{180-a}$$So if n is integer then this is forming a regular polygon.To solve this, we will follow these steps −sides := 360 /(180 - a)if sides has no fractional part, thenreturn Truereturn FalseExampleLet us see the following implementation to get better understanding − Live ... Read More

Check Palindrome String Creation from Given N in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:04:06

504 Views

Suppose we have a number n. We have to check whether we can create an alphabetical lowercase string from that number and check whether the string is palindrome or not. Here we will take only characters from a to j, [a = 0, b = 1... j = 9]. So if the number is 42 the substring "ec" will be printed till 6 (4+2) characters "ececec" then check this is palindrome or not.So, if the input is like n = 43, then the output will be True the string is "ededede" and this is palindrome.To solve this, we will follow ... Read More

Convert One String into Another with Given Constraints in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:00:36

211 Views

Suppose we have two strings s and t with only three characters 'A', 'B' and '#'. We have to check whether it is possible to convert s into t by performing these operations on s.'A' can move only to the left hand side'B' can move only to the right hand sideNeither 'A' nor 'B' can cross each otherSo, if the input is like s = "##AB##B" t = "A###B#B", then the output will be True as in s A can move easily to the left most position, and middle B can move one step to the rightTo solve this, we ... Read More

Check If Sum Is Possible from Given Set of Elements in Python

Arnab Chakraborty
Updated on 18-Jan-2021 12:58:12

473 Views

Suppose we have an array called nums and another value sum. We have to check whether it is possible to get sum by adding elements present in nums, we may pick a single element multiple times.So, if the input is like nums = [2, 3, 5] sum = 28, then the output will be True as we can get 26 by using 5 + 5 + 5 + 5 + 3 + 3 + 2To solve this, we will follow these steps −MAX := 1000table := an array of size MAX ad fill with 0Define a function util() . This ... Read More

Check If a String Can Be Split into Four Distinct Strings in Python

Arnab Chakraborty
Updated on 18-Jan-2021 12:56:25

361 Views

Suppose we have a string s, we have to check whether we can split it into four sub-strings such that each of them are non-empty and unique.So, if the input is like s = "helloworld", then the output will be True as one of the possible set of sub-strings are ["hel", "lo", "wor", "ld"]To solve this, we will follow these steps −if size of s >= 10, thenreturn Truefor i in range 1 to size of s - 1, dofor j in range i + 1 to size of s - 1, dofor k in range j + 1 to ... Read More

Extract Href Links from a Website Using BeautifulSoup

AmitDiwan
Updated on 18-Jan-2021 12:53:53

13K+ Views

BeautifulSoup is a third party Python library that is used to parse data from web pages. It helps in web scraping, which is a process of extracting, using, and manipulating the data from different resources.Web scraping can also be used to extract data for research purposes, understand/compare market trends, perform SEO monitoring, and so on.The below line can be run to install BeautifulSoup on Windows −pip install beautifulsoup4Following is an example −Examplefrom bs4 import BeautifulSoup import requests url = "https://en.wikipedia.org/wiki/Algorithm" req = requests.get(url) soup = BeautifulSoup(req.text, "html.parser") print("The href links are :") for link in soup.find_all('a'):    print(link.get('href'))OutputThe href links ... Read More

Advertisements