Found 10476 Articles for Python

Write a program in Python to replace all the 0’s with 5 in a given number

Dev Prakash Sharma
Updated on 05-Feb-2021 10:20:10

662 Views

Given an Integer N the task is to replace all the 0’s that appear in the number with ‘5’. However, the number with leading ‘0’ cannot be replaced with ‘5’ as it remains unchanged. For example, Input-1 −N = 1007Output −1557Explanation − The given number has 2 zeros which when replaced by ‘5’ results in the output as 1557.Input-2 −N = 00105Output −155Explanation − Since the given number starts with the leading ‘0’ which can be ignored and the output after replacing the 0 in the middle with ‘5’ results the output as 155.Approach to solve this problemTo replace all ... Read More

Majority Element in Python

Dev Prakash Sharma
Updated on 05-Feb-2021 08:10:17

3K+ Views

Let’s suppose we have an array of integers. The task is to find the index of a particular element in the given array. For example, Input-1 −N = 8 A[ ] = { 1, 2, 4, 3, 3, 1, 1, 5}Output −1Explanation − In the given array of integers, the most appearing number is ‘1’. Thus the output is ‘1’.Input-2 −N = 6 A[ ] = {1, 5, 4, 4, 1, 1}Output −1Explanation − In the given array of integers, the most appearing number is ‘1’. Thus we can return the output ‘1’.Approach to solve this problemThe given array contains ... Read More

Write a program in Python to count the number of digits in a given number N

Dev Prakash Sharma
Updated on 05-Feb-2021 11:58:55

8K+ Views

Let's suppose we have given a number N. the task is to find the total number of digits present in the number. For example, Input-1 −N = 891452Output −6Explanation − Since the given number 891452 contains 6 digits, we will return ‘6’ in this case.Input-2 −N = 0074515Output −5Explanation − Since the given number 0074515 contains 5 digits, we will print the output as 5.The approach used to solve this problemWe can solve this problem in the following way, Take input ‘n’ as the number.A function countDigits(n) takes input ‘n’ and returns the count of the digit as output.Iterate over all ... Read More

Count Good Meals in Python

Dev Prakash Sharma
Updated on 05-Feb-2021 11:58:15

338 Views

A good meal contains exactly two different food items with a sum of deliciousness equal to a power of two. You can pick any two different foods to make a good meal.Let us suppose we have given an array of integers arr where arr[i] is the deliciousness of the ith item of food, return the number of different good meals you can make from this list.For example, Input-1 −arr[ ] = {1, 3, 5, 7, 9}Output −4Explanation − The good meals are (1, 3), (1, 7), (3, 5) and, (7, 9). Their respective sums are 4, 8, 8, and 16, ... 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

How to set a cookie to a specific domain in selenium webdriver with Python?

Debomita Bhattacharjee
Updated on 01-Feb-2021 11:53:26

1K+ Views

We can set a cookie to a specific domain in Selenium webdriver with Python. A cookie is used to hold information sent by the browser. A key−value pair format is utilized and it is like a message provided to the browser by the server.For cookie addition, the method add_cookie is used. The key and value are passed as parameters to the method. To get back all the cookies, get_cookies method is used. To get a specific cookie, the method get_cookie is used.To delete cookies, the method delete_all_cookies is used.Syntaxdriver.add_cookie({"Automation": "QA"}); c= driver.get_cookies(); driver.get_cookie({"Automation"); driver.delete_all_cookies();Examplefrom selenium import webdriver #set geckodriver.exe path ... Read More

How to set window size using phantomjs and selenium webdriver in Python?

Debomita Bhattacharjee
Updated on 01-Feb-2021 11:46:54

332 Views

We can set window size using PhantomJS and Selenium webdriver in Python. To work with the PhantomJS, we should create a driver object of the webdriver.PhantomJS class.Then pass the path of the phantomjs.exe driver file as a parameter to the Class. Next, to set the window size, we shall use the set_window_size method and pass the dimensions as parameters to the method.To obtain the window size of the browser, we can use the get_window_size method.Syntaxdriver.set_window_size(800, 1000) print(driver.get_window_size())Examplefrom selenium import webdriver #set phantomjs.exe path driver = webdriver.PhantomJS(executable_path="C:\phantomjs.exe") driver.maximize_window() #launch URL driver.get("https://www.tutorialspoint.com/index.htm") #set new window size driver.set_window_size(800, 880) #obtain window size print(driver.get_window_size()) ... Read More

XMLRPC server and client modules in Python

Pradeep Elance
Updated on 25-Jan-2021 07:51:19

4K+ Views

We can create our own cross-platform, language-independent server using the XML-RPC protocol.We use the SimpleXMLRPCServer to create the SimpleXMLRPCServer instance and tell it to listen for incoming requests. Next we define some functions to be part of the service and register those functions so that the server knows how to call it.Running the ServerIn the below example we create a server using the SimpleXMLRPCServer instance and register some pre-defined as well as custom functions. Finally, we put the server into an infinite loop receiving and responding to requests.Examplefrom xmlrpc.server import SimpleXMLRPCServer from xmlrpc.server import SimpleXMLRPCRequestHandler class RequestHandler(SimpleXMLRPCRequestHandler):    rpc_paths = ... Read More

XML Processing Modules in Python

Pradeep Elance
Updated on 25-Jan-2021 07:51:34

1K+ Views

XML stands for "Extensible Markup Language". It is mainly used in webpages, where the data has a specific structure. It has elements, defined by a beginning and an ending tag. A tag is a markup construct that begins with < and ends with >. The characters between the start-tag and end-tag, are the element's content. Elements can contain other elements, which are called "child elements".ExampleBelow is the example of an XML file we are going to use in this tutorial.           Vicky, Matthew       Geo-Spatial Data Analysis       Python     ... Read More

The Null Object in Python

Pradeep Elance
Updated on 25-Jan-2021 07:46:49

3K+ Views

Python does not have a null object. But the most closely related similar object is none. In this article, we will see how how None behaves in Python.Checking the type of Null and None we see that there is no Null Type and the None object is of type NoneType.Exampleprint(type(None)) print(type(Null))OutputRunning the above code gives us the following result −Traceback (most recent call last):    File "C:\Users\xxx\scratch.py", line 4, in       print(type(Null)) NameError: name 'Null' is not definedKey facts about NoneNone is the same as False.None is the same as False.None is the same as False.None is an ... Read More

Advertisements