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
Virtual – circuit network is a category of packet switching network, where a virtual path is established between the source and the destination systems for data communication to occur. This path appears to the user as if it is a dedicated physical path, but actually is a logical circuit allocated from a managed pool of circuit resources as per traffic requirements. The network resources forming parts of this path can be shared by other communications, however, is not visible to this user.Phases of Virtual - Circuit TransmissionThere are three phases of transmission by virtual circuits, set up, data transfer and ... Read More
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
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
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
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
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
Dictionaries are most extensively used data structures in python. They contain data in form of keys and values. In this example we will see how to get the items form a dictionary specific to a given set of keys.With dictionary comprehensionIn this approach we simply loop through the dictionary using a for loop with in operator. But along with the in operator we also mention the values of the keys when referring to the dictionary keys.ExampledictA = {'Sun': '2 PM', "Tue": '5 PM', 'Wed': '3 PM', 'Fri': '9 PM'} # Given dictionary print("Given dictionary : ", dictA) res ... Read More
A given positive number when converted to binary, has a number of setbits. Set bits in a binary number is represented by 1. In this article we will see how to get the number of setbits in a given number after it is converted to binary value.Using bin and slicingIn the below example we take a number and apply the bin function to get the binary value. Then we slice it to remove the prefixes added to a binary number and then apply the range function to get the coutn of setbits.Example Live Demodef SetBits_cnt(n, l, r): bin_val = bin(n) ... Read More
As the world embraces more unstructured data, we come across many formats of data where the data structure can be deeply nested like nested JSONS. Python has the ability to deal with nested data structure by concatenating the inner keys with outer keys to flatten the data. In this article we will take a nested dictionary and flattened it.Using a recursive approachIn this approach we design a function to recursively process each item in the dictionary. We pass the dictionary, design a place holder for the output dictionary, the key and separator as parameters. We use the isinstance to check ... Read More