Print Continuous Numbers in the Form of Triangle using PHP

AmitDiwan
Updated on 02-Jul-2020 06:35:25

310 Views

To print continuous numbers in the form of triangle in PHP, the code is as follows −Elements Live DemoOutput1 2 3 4 5 6 7 8 9 10This is similar to generating a star or a number pattern, the only difference being, continuous numbers are generated instead of stars or repeated numbers. The function ‘continuous_pattern’ is defined that takes the limit as a parameter. The limit value is iterated over and the number is printed and increment. The relevant line breaks are also generated in between. The function is called by passing this limit value and the relevant output is generated on the console.

PHP Program to Print the Number Pattern

AmitDiwan
Updated on 02-Jul-2020 06:34:08

2K+ Views

To print the number pattern in PHP, the code is as follows −Example Live DemoOutput1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6This is similar to generating a star pattern, the only difference being, numbers are generated instead of stars. The function ‘num_pattern’ is defined that takes the limit as a parameter. The limit value is iterated over and the number is printed and relevant line breaks are also generated in between. The function is called by passing this limit value and the relevant output is generated on the console.

Print a Pattern of Pyramid in PHP

AmitDiwan
Updated on 02-Jul-2020 06:32:21

2K+ Views

Let us see an example to print a pattern of pyramid in PHP −Example Live DemoOutput         *         * *        * * *       * * * *      * * * * *     * * * * * *    * * * * * * *A function named ‘print_pattern’ is defined that iterates through the number of rows through which the pattern needs to be generated. Relevant line breaks are also echoed using the ‘echo’ attribute. The function is called by passing the number of rows for which the pattern needs to be generated.

MySQL Query to Select Top N Rows Efficiently

AmitDiwan
Updated on 02-Jul-2020 06:31:24

428 Views

Use index to select top n rows efficiently. Let us first create a table −mysql> create table DemoTable (StudentName varchar(100), StudentScore int ); Query OK, 0 rows affected (0.66 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('John', 34); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Carol', 55); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Bob', 58); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Sam', 38); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Mike', 48); Query OK, 1 row ... Read More

Multidimensional Associative Arrays in PHP: How to Retrieve Values

AmitDiwan
Updated on 02-Jul-2020 06:30:47

2K+ Views

Multidimensional arrays store multiple arrays whereas associative arrays store key-value pairs as data. Grouped relation between data can be stored in multidimensional associative arrays.Example Live DemoOutputJoe 20An array is defined that contains various attributes of an employee. The employee array is accessed based on the index names and the data is displayed on the console.

Check Multidimensional Nature of an Array in PHP

AmitDiwan
Updated on 02-Jul-2020 06:28:53

2K+ Views

The ‘rsort’ function can be used to check if an array is multidimensional or not. It takes one parameter, i.e the array that needs to be checked and returns yes or no depending on the nature of the array.Example Live DemoOutputIs the array multi-dimensional? bool(true)An array is defined that contains string elements. A function named ‘multi_dim’ is defined that sorts the elements of the array using ‘rsort’. The ‘isset’ function is then used to perform ‘AND’ operation on the elements of the array. This would help understand if the array has a single dimension or is multi-dimensional.Read More

Different Ways of Checking if an Array is Empty in PHP

AmitDiwan
Updated on 02-Jul-2020 06:27:29

558 Views

Using the ‘sizeof’ functionLet us see an example −Example Live DemoOutputThe array is empty!An array can be checked to see if it is empty or not in multiple ways. One method is to use the ‘sizeof’ function that sees if the array is empty. If yes, the size would be 0, thereby confirming the array being empty.Using the ‘empty’ functionExample Live DemoOutputThe array is non-empty The array is empty!Another method to check if an array is empty or not is to use the ‘empty’ function, that checks to see the contents of the array, and if nothing is present, says it is ... Read More

Removing Empty Array Elements in PHP

AmitDiwan
Updated on 02-Jul-2020 06:23:49

697 Views

To remove empty array elements in PHP, the code is as follows −Example Live DemoOutputAfter removing null values from the array, the array has the below elements -This 91 102 is a sampleAn array is defined, that contains strings, numbers and ‘null’ values. The ‘foreach’ loop is used to iterate over the elements and if a value is empty, i.e. it contains null, then it is deleted from the array. The relevant array is displayed again that wouldn’t contain null values.

Binary Search Bisect in Python

Arnab Chakraborty
Updated on 02-Jul-2020 06:15:22

2K+ Views

Here we will see the bisect in Python. The bisect is used for binary search. The binary search technique is used to find elements in sorted list. The bisect is one library function.We will see three different task using bisect in Python.Finding first occurrence of an elementThe bisect.bisect_left(a, x, lo = 0, hi = len(a)) this function is used to return the left most insertion point of x in a sorted list. Last two parameters are optional in this case. These two are used to search in sublist.Examplefrom bisect import bisect_left def BinSearch(a, x):    i = bisect_left(a, x)   ... Read More

A Number Link Game

sudhir sharma
Updated on 02-Jul-2020 05:37:51

770 Views

Number link is a type of logic puzzle involving finding paths to connect numbers in a grid.A simple example of a Numberlink puzzle The solution to the Numberlink puzzleRules − The player has to pair up all the matching numbers on the grid with single continuous lines (or paths). The lines cannot branch off or cross over each other, and the numbers have to fall at the end of each line (i.e., not in the middle). It is considered that a problem is well-designed only if it has a unique solution and all the cells in the grid are filled, ... Read More

Advertisements