Found 33676 Articles for Programming

Check if suffix and prefix of a string are palindromes in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:20:34

346 Views

Suppose we have a string s, we have to check whether the string palindromes as its prefix and suffix substrings or not.So, if the input is like s = "levelishighforracecar", then the output will be True as there are palindrome prefix and suffix: "level" and "racecar" respectively.To solve this, we will follow these steps −l := size of sfor i in range 2 to l + 2, doif substring of s up to index i is palindrome, thencome out from loopif i is same as(l + 1) , thenreturn Falsefor i in range 2 to l + 2, doif substring ... Read More

Check if subarray with given product exists in an array in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:18:42

635 Views

Suppose we have an array called nums and this contains positive and negative numbers. We have another value k. We have to check whether any subarray whose product is k is present in the array or not.So, if the input is like nums = [-2, -1, 1, 3, 5, 8], k = 6, then the output will be True as the subarray is [-2, -1, 3]To solve this, we will follow these steps −minimum := nums[0], maximum := nums[0]prod_max := nums[0]for i in range 1 to size of nums - 1, doif nums[i] < 0, thenswap maximum and minimummaximum := ... Read More

Check if strings are rotations of each other or not in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:17:37

800 Views

Suppose we have two strings s and t, we have to check whether t is a rotation of s or not.So, if the input is like s = "hello", t = "llohe", then the output will be True.To solve this, we will follow these steps −if size of s is not same as size of t, thenreturn Falsetemp := s concatenate with s againif count of t in temp > 0, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example CodeLive Demodef solve(s, t):    if len(s) != len(t):       return False     ... Read More

Check if string follows order of characters defined by a pattern or not in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:16:58

658 Views

Suppose we have a string s and another string t as pattern, we have to check whether characters in s follows the same order as determined by characters present in t. Here we have no duplicate characters in the pattern.So, if the input is like s = "hello world" t = "hw", then the output will be True.To solve this, we will follow these steps −if size of s < size of t, thenreturn Falsefor i in range 0 to size of t - 2, dox := t[i], y := t[i + 1]right := last index of x in sleft ... Read More

Check if right triangle possible from given area and hypotenuse in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:15:15

250 Views

Suppose we have the hypotenuse and area of a right angle triangle, we have to find the base and height of this triangle. If it is not possible return False.So, if the input is like hypo = 10, area = 24, then the output will be (6, 8).To solve this, we will follow these steps −hypo_sq := hypo * hypos := square root of (hypo_sq / 2.0)maxArea := calculate area of triangle using base s and hypotenuse hypoif area > maxArea, thenreturn Falseleft := 0.0, right := swhile |right - left| > 0.000001, dobase := (left + right) / 2.0if ... Read More

Check if reversing a sub array make the array sorted in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:13:40

357 Views

Suppose we have an array called nums with unique elements. We have to check whether the array will be sorted or not after reversing one sub-array of it. If the array is already sorted, then also return true.So, if the input is like nums = [4, 6, 27, 25, 15, 9, 37, 42], then the output will be True because if we reverse [9, 15, 25, 27], then the array will be sorted.To solve this, we will follow these steps −n := size of numsif array has only one element then return Truei := 1for i in range 1 to ... Read More

Python Generate QR Code using pyqrcode module?

Pradeep Elance
Updated on 12-Jan-2021 13:46:33

501 Views

A QR code consists of black squares arranged in a square grid on a white background, which can be read by an imaging device such as a camera. It is widely used for many commercial tracking applications and payment and website login etc. for various applications aimed at mobile-phone users. The pyqrcode module is used to generate the qrcocode in python. There are four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji) to store data efficiently.Alphanumeric qrcodeWe use the pyqrcode module. It has the cerate function which will be used to generate the qrcode. Finally we save it as a ... Read More

Python Front and rear range deletion in a list?

Pradeep Elance
Updated on 12-Jan-2021 13:44:05

203 Views

Using list slicingIn this approach we use slicing from both the front and rear of the list. The result is stored into a new list. The number of elements to be sliced can be a variable.Example Live DemolistA = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] # Given list print("Given list : " ,listA) # No of elements to be deleted # from front and rear v = 2 new_list = listA[v:-v] print("New list : ", new_list)OutputRunning the above code gives us the following result −Given list : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] New list ... Read More

Python Float Layout in Kivy?

Pradeep Elance
Updated on 12-Jan-2021 13:41:42

584 Views

Kivy is an open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. It is used to develop the Android application, as well as Desktops applications. In this article we will see how to use the FloatLayout widget to create buttons of at different positions in a window. The position of the button can be absolute or relative with respect to the bigger window.With Absolute positionIn this approach we set the position of the button at a fixed position. So when the resize of the window happens, the size of ... Read More

Python file parameter in print()?

Pradeep Elance
Updated on 12-Jan-2021 13:36:23

985 Views

The regular use of the print() function is to display text either in the command-line or in the interactive interpreter. But the same function can also write into a file or an output stream.Printing to fileIn the example we can open a file with a new filename in write mode then mention that filename in the print function. The value to be written to the file can be passed as arguments into the print function.ExampleNewfile= open("exam_score.txt", "w") # variables exam_name = "Degree" exam_date = "2-Nov" exam_score = 323 print(exam_name, exam_date, exam_score, file=Newfile , sep = ", ") ... Read More

Advertisements