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
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
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
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
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
Suppose we have a string s and an integer n, we have to split the s into n-sized pieces.So, if the input is like s = "abcdefghijklmn", n = 4, then the output will be ['abcd', 'efgh', 'ijkl', 'mn']To solve this, we will follow these steps −i:= 0f:= a new listwhile i < size of s, doinsert s[from index i to i+n-1] at the end of fi := i + nreturn fLet us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, s, n): i=0 f=[] while(i
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
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
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
IntroductionThe php://wrapper enableaccess to various I/O streams. This includes standard input, output and error streams. In-memory, disk backed and filtered streams are lso accessed with php:// protocol.Standard streamsphp://stdin, php://stdout and php://stderr allow direct access to standard input stream device, standard output stream and error stream to a PHP process respectively. Predefined constants STDIN, STDOUT and STDERR respectively represent these streams.php://inputphp://input allows read-only acess to raw data contained in HTTP request body. Note that same data is available in $HTTP_POST_RAW-DATA variable (which is now deprecated). However, php://input is not available for enctype attribute is set to multipart/form-dataphp://outputThis wrapper represents write-only tream, allowing buffer ... Read More