Server Side Programming Articles - Page 1559 of 2650

Count Elements x and x+1 Present in List in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:12:50

180 Views

Suppose we have a list of numbers called nums, we have to find the number of elements x there are such that x + 1 exists as well.So, if the input is like [2, 3, 3, 4, 8], then the output will be 3To solve this, we will follow these steps −s := make a set by inserting elements present in numscount := 0for each i in nums, doif i+1 in s, thencount := count + 1return countLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, nums):       s = set(nums) ... Read More

Cell Count After Removing Corner Diagonals in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:11:25

136 Views

Suppose we have a number n representing the length of an n x n board. We have to delete all cells that are diagonal to one of the four corners and return the number of empty cells.So, if the input is like n = 4,XOOXOXXOOXXOXOOXThen the output will be 8.To solve this, we will follow this formula −n*n - 2 * n +(n mod 2)Let us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, n):       return n*n - 2 * n + (n%2) ob = Solution() print(ob.solve(4))Input4Output8

Connell sequence in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:06:14

548 Views

Suppose we have a number n, we have to find the nth term of Connell sequence. The Connell sequence is as follows: 1. Take first odd integer: 1 2. Take next two even integers 2, 4 3. Then take the next three odd integers 5, 7, 9 4. After that take the next four even integers 10, 12, 14, 16 And so on.So, if the input is like 12, then the output will be 21To solve this, we will follow these steps −i := 1while quotient of (i *(i + 1) / 2) < n + 1, doi := i ... Read More

Common Words in Two Strings in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:04:19

6K+ Views

Suppose we have two strings s0 and s1, they are representing a sentence, we have to find the number of unique words that are shared between these two sentences. We have to keep in mind that, the words are case-insensitive so "tom" and "ToM" are the same word.So, if the input is like s0 = "i love python coding", s1 = "coding in python is easy", then the output will be 2 as there are 2 common words, ['python', 'coding']To solve this, we will follow these steps −convert s0 and s1 into lowercases0List := a list of words in s0s1List ... Read More

Column Sort of a Matrix in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:02:40

2K+ Views

Suppose we have a matrix, we have to sort each of the columns in ascending order.So, if the input is like1121316641118then the output will be1646118112131To solve this, we will follow these steps −R := row count of matrix, C := column count of matrixres := matrix of same size as given matrix and fill with 0for col in range 0 to C, dovalues := take the elements as a vector of matrix[col]for row in range 0 to R, dores[row, col] := delete last element from valuesreturn resLet us see the following implementation to get better understanding −Example Live Democlass Solution:   ... Read More

Collatz sequence in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:00:19

2K+ Views

Suppose we have a positve integer n, we have to find the length of its Collatz sequence. As we know Collatz sequence is generated sequentially where n = n/2 when n is even otherwise n = 3n + 1. And this sequence ends when n = 1.So, if the input is like n = 13, then the output will be 10 as [13, 40, 20, 10, 5, 16, 8, 4, 2, 1] these is the sequence.To solve this, we will follow these steps −if num is same as 0, thenreturn 0length := 1while num is not same as 1, donum ... Read More

Total Distance to Visit City Blocks in Python

Arnab Chakraborty
Updated on 22-Sep-2020 10:58:41

239 Views

Suppose we have a matrix of unique strings representing the city blocks, and another list of strings containing blocks to visit. If we are at block matrix[0][0], then find the total Manhattan distance required to visit every block in order.So, if the input is likeQBCDEZGGiBlock = [H, B, C]Then the output will be 6 as "h" is 2 blocks bottom(south) and 1 block right(east), "b" is 2 blocks up(north), "c" is 1 block right(east).To solve this, we will follow these steps −coords := a map with key 'start' and value (0, 0)for each row in mat, dofor each col in ... Read More

PHP compression Stream Wrappers

Malhar Lathkar
Updated on 22-Sep-2020 10:51:29

493 Views

IntroductionIn PHP, zlib://, bzip2:// and zip:// represent wrappers for respective compression streams.compress:zlib://This works similar to gzopen() function, however, it can be used with filesystem functions like fread() and others.compress://bzip2This is similar to bzopen() function. Both stream wrappers operate even on systems not capable of supporting fopencookie.zip://The ZIP extension registers this wrapper. From PHP 7.2.0 onwards, archives encrypted with passwords are supported. It is possible to set password with password context option.Exampleszlib compression can be applied with following PHP codeTo uncompress, we can use following syntaxWe can also use built-in copy() function to build compressed zlib file and uncompress the samecopy('file.txt', ... Read More

PHP ssh2://

Malhar Lathkar
Updated on 22-Sep-2020 10:50:15

360 Views

IntroductionThe libssh2 library provides access to resources on a remote machine using a secure cryptographic transport. These are shell, remote exec, tunneling, file transfer and SCP. PHP has wrappers for these resources. They are ssh2.shell://, ssh2.exec://, ssh2.tunnel://, ssh2.sftp://, and ssh2.scp:// respectivelyNote that these wrappers are not enabled by default. SSH2 extension available from PECL must be installed.Usagessh2.shell://user:pass@example.com:22/xterm ssh2.exec://user:pass@example.com:22/usr/local/bin/somecmd ssh2.tunnel://user:pass@example.com:22/192.168.0.1:14 ssh2.sftp://user:pass@example.com:22/path/to/filenamessh2.*// context optionssessionPreconnected ssh2 resource to be reusedsftpPreallocated sftp resource to be reusedmethodsKey exchange, hostkey, cipher, compression, and MAC methods to use callbacksusernameUsername to connect aspasswordPassword to use with password authenticationpubkey_fileName of public key file to use for authenticationprivkey_fileName of private key file ... Read More

PHP rar://

Malhar Lathkar
Updated on 22-Sep-2020 10:48:58

313 Views

IntroductionThe RAR (Roshal Archive) is file compression format that supports error recovery and file spanning. PHP supports use of .RAR files as IO stream. The rar:// is a stream wrapper for RAR streams.rar:// wrapper takes the relative or absolute url encoded path to the RAR archive. An optional (*), or (#) and an optional url encoded entry name, as stored in the archive. This wrapper can open both files and directories.If the pound sign and the entry name part are not included, the root of the archive will be displayed. The usage of the wrapper with RecursiveDirectoryIterator requires the number sign to ... Read More

Advertisements