Found 10784 Articles for Python

Explain difference between == and is operator in Python.

Mahesh Parahar
Updated on 15-Apr-2020 08:24:35

201 Views

== operator== operator compares the operands by checking the equality of values of objects.is operatoris operator compares the operands by checking the objects to be the same or not.ExampleFollowing is the program in Python to showcase the difference. Live Demolist1 = [1] list2 = [1] list3 = list1 print(id(list1)) print(id(list2)) if (list1 == list2):    print("True") else:    print("False") if (list1 is list2):    print("True") else:    print("False") if (list1 is list3):    print("True") else:    print("False")Output140380664377096 140380664376904 True False True

Run a Python program from PHP

AmitDiwan
Updated on 06-Apr-2020 08:21:43

4K+ Views

In PHP, the ‘shell_exec’ function can be used. It can be executed via the shell and the result can be returned as a string. It returns an error if NULL is passed from the command line or returns no output at all.Below is a code demonstration of the same −The right privileges need to be given so that the python script is successfully executed.Note − While working on a Unix type of platform, PHP code is executed as a web user. Hence, the web user should be given the necessary rights to the directories and sub-directories.

Validate IP Address in Python

Arnab Chakraborty
Updated on 29-Apr-2020 16:08:17

2K+ Views

Suppose we have a string; we have to check whether the given input is a valid IPv4 address or IPv6 address or neither.The IPv4 addresses are canonically represented in dotted-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), For example, 192.168.254.1; Besides, leading zeros in the IPv4 address is invalid. For example, the address 192.168.254.01 is invalid.The IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, suppose the address is 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid address. ... Read More

Print Words Vertically in Python

Arnab Chakraborty
Updated on 29-Apr-2020 09:50:19

4K+ Views

Suppose we have a string s. We have to find all the words vertically in the same order in which they appear in s. Here words are returned as a list of strings, we have to complete with spaces when is necessary. (Trailing spaces are not allowed). Each word would be put on only one column and that in one column there will be only one word. So if the input string is “HOW ARE YOU”, then the output will be [“HAY”, “ORO”, “WEU”]To solve this, we will follow these steps −s := make a list of strings split by ... Read More

Can Make Palindrome from Substring in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:32:37

100 Views

Suppose we have a string s, we have to make queries on substrings of s. For each query queries[i], there are three parts [left, right, k], we may rearrange the substring s[left], ..., s[right], and then choose up to k of them to replace with any lowercase English letter. If the substring is possible to be a palindrome after the operations mentioned above, the result of the query is true. Otherwise false. We have to find an array answer[], where answer[i] is the result of the i-th query queries[i].For example, if the input is “abcda”, queries is like [[3, 3, ... Read More

Design File System in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:22:05

2K+ Views

Suppose we have to design a file system which provides these two functions −createPath(path, value) − This creates a new path and associates a value to it if possible and returns True. It returns False if the path already exists or its parent path doesn't exist.get(path) − This finds the value associated with a path or returns -1 if the path doesn't exist.The format of a path is one or more concatenated strings of the form − (forward slash) / followed by one or more lowercase English letters. For example, /programming and /programming/problems are valid paths while an empty string ... Read More

Number of Dice Rolls With Target Sum in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:06:31

711 Views

Suppose we have d dice, and each die has f number of faces numbered 1, 2, ..., f. We have to find the number of possible ways (out of fd total ways) modulo 10^9 + 7 to roll the dice so the sum of the face up numbers equal to the target. So if the input is like d = 2, f = 6, target = 7, then the output will be 6. So if we throw each dice with 6 faces, then there are 6 ways to get sum 6, as 1 + 6, 2 + 5, 3 + ... Read More

Minimum Swaps to Group All 1's Together in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:02:01

233 Views

Suppose we have a binary array data, we have to find the minimum number of swaps required to group all 1’s stored in the array together in any place in the array. So if the array is like [1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1], then the output will be 3, as possible solution is [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]To solve this, we will follow these steps −set one := 0, n:= length of data arraymake an array summ of size n, and fill this with 0, set summ[0] := ... Read More

Snapshot Array in Python

Arnab Chakraborty
Updated on 30-Apr-2020 05:56:46

481 Views

Suppose we have to Implement a SnapshotArray that supports the following interfaces −SnapshotArray(int length) this will initialize the array-like data structure with the given length. Initially, each element equals 0.set(index, val) this will sets the element at the given index to be equal to val.snap() will take a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.get(index, snap_id) this will return the value at the given index, at the time we took the snapshot with the given snap_idSo if the array size is 2, it is set using [0, 5], after ... Read More

Binary Tree Coloring Game in Python

Arnab Chakraborty
Updated on 30-Apr-2020 11:47:13

325 Views

Suppose there are two players play a turn-based game on a binary tree. We have the root of this binary tree and the number of nodes n in the tree. Here n is odd, and each node has a distinct value from 1 to n. At first, the first player names a value x with 1

Advertisements