Example of PHP file operations and Algorithm Implementation-interview question _ php instance

Source: Internet
Author: User
This article mainly introduces the example of PHP file operations and Algorithm Implementation, and summarizes some of the most representative and basic questions. For more information, see Operation File

1. Get the extension of a file in more than five ways

Requirement: dir/upload.image.jpg, find .jpg or jpg

<? Php/*** get the file extension of the specified path in five ways */$ str = "dir/upload.image.jpg"; function one ($ str) {$ arr = explode ('. ', $ str); $ count = count ($ arr); return $ arr [$ count-1];} function two ($ str) {$ len = strlen ($ str); for ($ I = $ len-1, $ name = ''; $ str [$ I]! = '. '; $ I --) {$ name. = $ str [$ I] ;}$ name = strrev ($ name); return $ name;} function three ($ str) {$ path = pathinfo ($ str ); return $ path ['extension'];} function four ($ str) {$ arr = explode ('. ', $ str); return array_pop ($ arr);} function five ($ str) {$ start = strrpos ($ str ,'. '); return substr ($ str, $ start + 1);} echo one ($ str); echo"
"; Echo two ($ str); echo"
"; Echo three ($ str); echo"
"; Echo four ($ str); echo"
"; Echo five ($ str); echo"
";

2. Write a php function to calculate the relative paths of the two files. For example, $ a = "/a/B/c/d/e. php "; $ B ="/a/B/12/34/c. php ", what is the relative path of B to?

This question can be regarded as the first public node question. Most of the Code circulating on the internet is wrong, and it is not comprehensive. Of course, I only use ".. /"to indicate, useless ". /"

<? Php/*** calculates the relative path of $ B relative to $ a * @ param string $ B * @ return string */function getRelativePath ($, $ B) {$ patha = explode ('/', $ a); $ pathb = explode ('/', $ B); $ counta = count ($ patha) -1; $ countb = count ($ pathb)-1; $ path = ".. /"; if ($ countb> $ counta) {while ($ countb> $ counta) {$ path. = ".. /"; $ countb -- ;}/// find the first public node for ($ I = $ countb-1; $ I >=0 ;) {if ($ patha [$ I]! = $ Pathb [$ I]) {$ path. = ".. /"; $ I --;} else {// determine whether it is the real first public node to prevent the occurrence of sub-directory name duplication. for ($ j = $ I-1, $ flag = 1; $ j> = 0; $ j --) {if ($ patha [$ j] = $ pathb [$ j]) {continue ;} else {$ flag = 0; break ;}} if ($ flag) break; else $ I ++ ;}}for ($ I + = 1; $ I <= $ counta; $ I ++) {$ path. = $ patha [$ I]. "/";} return $ path;} $ a = "/a/c/d/e. php "; $ B ="/a/c. php "; $ path = getRelativePath ($ a, $ B); echo $ path;


Algorithm

1. Use PHP to describe Bubble sorting and quick sorting. The object can be an array.

<? Php/*** Bubble Sorting Algorithm Implementation (from small to large) */function maopaoSort (& $ array) {$ count = count ($ array); for ($ I = 0; $ I <$ count-1; $ I ++) {for ($ j = 0; $ j <$ count-$ I-1; $ j ++) {if ($ array [$ j]> $ array [$ j + 1]) {$ tmp = $ array [$ j]; $ array [$ j] = $ array [$ j + 1]; $ array [$ j + 1] = $ tmp ;}}}} /*** Quick Sort */function merge tparation (& $ array, $ start, $ end) {$ stand = $ array [$ start]; while ($ start <$ end) {while ($ start <$ end & $ array [$ end] >=$ stand) {$ end --;} if ($ start <$ end) {$ array [$ start ++] = $ array [$ end];} while ($ start <$ end & $ array [$ start] <= $ stand) {$ start ++;} if ($ start <$ end) {$ array [$ end --] = $ array [$ start] ;}} $ array [$ start] = $ stand; return $ start;} function quickSort (& $ array, $ begin, $ end) {if ($ begin <$ end) {$ topology = maid ($ array, $ begin, $ end); quickSort ($ array, $ begin, $ sort-1); quickSort ($ array, $ sort + 1, $ end) ;}$ arr = array (5, 1, 3, 2, 19, 11, 25, 12,100,100 00, 12 ); // bubble sort maopaoSort ($ arr); print_r ($ arr); echo"
"; // Quick sorting $ count = count ($ arr); quickSort ($ arr, 0, $ count-1); print_r ($ arr );

2. Use php to describe sequential search and Binary Search

<? Php/*** sequential search */function seqSearch ($ arr, $ needle) {for ($ I = 0, $ len = count ($ arr ); $ I <$ len; $ I ++) {if ($ arr [$ I] ==$ needle) {return $ I ;}} return-1 ;} /*** Binary Search */function midSearch ($ arr, $ start, $ end, $ needle) {while ($ start <= $ end) {$ mid = (int) ($ start + ($ end-$ start)/2); // prevents if ($ arr [$ mid] = $ needle) from exceeding the integer representation range) {return $ mid;} else if ($ arr [$ mid]> $ needle) {$ end = $ mid-1 ;} else {$ start = $ mid + 1 ;}} return-1 ;}$ arr = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $ needle = 5; echo seqSearch ($ arr, $ needle); echo"
"; Echo midSearch ($ arr, 0, count ($ arr)-1, $ needle );

3. Write a two-dimensional array Sorting Algorithm function that is universal and can call php built-in functions.

/*** Description: obtain the location of the central vertex ** @ param array $ array * @ param int $ left * @ param int $ right * @ param string $ field * @ return int */function fetcharrayray( & $ array, $ left, $ right, $ field) {// baseline Definition $ stand = $ array [$ left]; // traverses the array while ($ left <$ right) {while ($ left <$ right & $ array [$ right] [$ field] >=$ stand [$ field]) {$ right --;} if ($ left <$ right) {$ array [$ left ++] = $ array [$ right];} while ($ left <$ right & $ array [$ left] [$ field] <= $ stand [$ field]) {$ left ++ ;} if ($ left <$ right) {$ array [$ right --] = $ array [$ left];} // obtain the central position $ array [$ left] = $ stand; return $ left;}/*** Description: quick sorting main program ** @ param array $ array * @ param int $ begin * @ param int $ end * @ param string $ field */function quickSort (& $ array, $ begin, $ end, $ field) {// variable definition $ begin = null; if ($ begin <$ end) {$ begin = fetcharrayray( $ array, $ begin, $ end, $ field); quickSort ($ array, $ begin, $ fields-1, $ field); quickSort ($ array, $ struct + 1, $ end, $ field );}}

Add a field parameter using the concept of "Quick Sort"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.