Found 33676 Articles for Programming

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

231 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

481 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

347 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

309 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

PHP php://

Malhar Lathkar
Updated on 22-Sep-2020 10:47:26

2K+ Views

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

PHP phar://

Malhar Lathkar
Updated on 22-Sep-2020 10:45:40

1K+ Views

IntroductionThe phar:// stream wrapper is available in all PHP versions after 5.3.0. Phar stands for PHP Archive. It is used to distribute PHP application or library and is executed as a normal PHP file. The phar:// wrapper supports opening file with fopen() for read/write, rename, and directory stream operations opendir() as well as create and remove directories.Phar class allows packaging application resources contained inside a directory in a phar archive. To perform read operations, this archive is put in phar:// wrapperBuilding phar archiveTo begin with, ensure that phar.readonly setting in php.ini is set to 0. Then, create a src folder in which ... Read More

PHP http://

Malhar Lathkar
Updated on 22-Sep-2020 10:44:14

366 Views

IntroductionThe http:// and https:// wrappers enable read only access to resources and files through HTTP procol. When handling virtual name-based host,  host: header is also sent along with user_agent (if configured in php.ini)The http header information is stored in $http_response_header variable. These headers have to be processed to know URL of resource where the document comes from with the help of from: header.HTTPS is supported only if openssl extension is enabled in php.ini settings. Both HTTP and HTTPS connections are read only and don't support writing or copying files.UsageRepresentation of file name in different possible ways is as follows −http://localhost http://example.com http://localhost?name='Ram'&age=20 https://example.com http://username:password@abc.comExampleAbove ... Read More

PHP glob://

Malhar Lathkar
Updated on 22-Sep-2020 10:42:23

438 Views

IntroductionThe glob:// stream wrapper is available in all PHP versions after 5.3.0. It finds pathnames that match given pattern. Similar purpose is fulfilled by PHP's filesystem function glob() which follows libc glob() rules.ParametersSpecial characters* − Matches zero or more characters.? − Matches exactly one character (any character).[...] − Matches one character from a group of characters. If the first character is !, matches any character not in the group.\ − Escapes the following character, except when the GLOB_NOESCAPE flag is used.Valid flagsGLOB_MARK − Adds a slash (a backslash on Windows) to each directory returnedGLOB_NOSORT − Return files as they appear ... Read More

Advertisements