Coders Den

Get Mime Type of file without reading file metas

What is MIME Type? MIME stands for Multipurpose Internet Mail Extension. It is the standardized way to identify the nature and format of a document. It is defined and standardized in IETF RFC 6838. The Internet Assigned Numbers Authority (IANA) is the official body who is responsible for keeping track of all official MIME types, and you can find the most up-to-date and complete list at the Media Types page. Syntax of MIME Type type/subtype MIME type string is a combination of 2 strings that are Type & SubType separated by / symbol. There should not be any space between Type & SubType. Type is a category of multiple MIME types of common groups that are closely related to each other. SubType …

Read More
PHP Function

Get the Minimum & Maximum Sum of an array value in PHP

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. For example,  arr = [1,3,5,7,9]. Our minimum sum is 1 + 3 + 5 + 7 = 16 and our maximum sum is 3 + 5 + 7 + 9 = 24 . We would print.

Function Description Complete the miniMaxSum function in the editor below. It should print two space-separated integers on one line: the minimum sum and the maximum sum of 4 of 5 elements. miniMaxSum has the following parameter(s): arr: an array of 5 integers Input Format …

Read More
PHP Function

Get 2D array’s diagonal values in PHP

Sometimes is programming line you may need diagonal values of a 2D array. Today in this article I will help you how you can get 2D array’s diagonal values in PHP programming language. Before the start, we should know the logic behind the function. Let suppose we have an array i.e.

Here, diagonal values will be. ar1 = 1,5,9 and ar2 = 3,5,7. We should check that array must be a square. If the input array is not a square we should throw an exception because error handling is the best practice. Here is the code that will fetch diagonal values from a 2D array in PHP. View the code on Gist.

Read More
PHP Function

Diagonal difference of 2D array – PHP

Given a square matrix, calculate the absolute difference between the sums of its diagonals. For example, the square matrix arr is shown below:

The left-to-right diagonal = 1 + 5 + 9 = 15. The right to left diagonal = 3 + 5 + 9 = 17 . Their absolute difference is  |15 – 17| = 2. Function description Complete the diagnoalDifference  function in the editor below. It must return an integer representing the absolute diagonal difference. diagonalDifference takes the following parameter: arr: an array of integers . Input Format The first line contains a single integer, , the number of rows and columns in the matrix . Each of the next lines describes a row arr[I] , and consists of n space-separated …

Read More

Two strings, a and b, are said to be twins only if

Two strings, a and b, are said to be twins only if they can be made equivalent by performing some number of operations on one or both strings. There are two possible operations: SwapEven: Swap a character at an even-numbered index with a character at another even-numbered index. SwapOdd: Swap a character at an odd-numbered index with a character at another odd-numbered index. For example, a = “abcd” and b = “cdab” are twins because we can make them equivalent by performing operations. Alternatively, a = “abcd” and b = “bcda” are not twins (operations do not move characters between odd and even indices), and neither are a = “abc” and b = “ab” (no amount of operations will insert a ‘c’ into string b). Complete the twins function in the editor below. It has two parameters: An array of n strings named a. An …

Read More

Replace French letters with their English letters

In this Article I’ll explain you how you can replace French letters with their English letters. In some cases you may need to change French special characters with their English letters for your target audience. Like Š => S š => s Ð => Dj Ž => Z You can replace these letters with too many methods. But here I’ll highlight 2 methods: strtr — Translate characters or replace substrings str_replace — Replace all occurrences of the search string with the replacement string Replace French letters with ‘strtr’ function Translating the letters:

In above example strtr function will search the key (array-key of second parameter) and will replace with its respective value. Because we have passed second parameter as …

Read More

Yahoo weather YQL API without authentication

Sometime you may need to show weather status on your own/client’s website. There are lots of web-services that do that for you but most of them are paid services. After frantic search I found Yahoo weather YQL API that uses YQL (Yahoo Query Language) this API doesn’t require any authentication for weather report (No OAuth required). Yahoo weather YQL API PHP Script To create your own Yahoo weather API you need to Get your API Keys first.

In above Yahoo example it’ll show weather report of “chicago, il“. For for clarity or flexibility I have added above function in PHP Class:

By default Yahoo weather API sends temperature in fahrenheit. If you want to show temperature in celsius then you …

Read More