Skip to content

Instantly share code, notes, and snippets.

View tanmay27vats's full-sized avatar

Tanmay Vats tanmay27vats

View GitHub Profile
@tanmay27vats
tanmay27vats / mini-max-sum.php
Created August 10, 2018 06:26
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 …
function miniMaxSum($arr) {
$sum = array_sum($arr);
$min = $sum;
$max = 0;
foreach($arr as $key => $val) {
$excluded_sum = $sum - $val;
if($max < $excluded_sum) {
$max = $excluded_sum;
}
if($min > $excluded_sum) {
@tanmay27vats
tanmay27vats / array-diagonal-values.php
Created August 10, 2018 05:17
Get 2D array's diagonal values in PHP.
function diagonalValues($arr) {
$d_ar = [];
$main_len = count($arr);
foreach($arr as $key => $ar) {
$sub_len = count($ar);
if($main_len != $sub_len) {
throw new Exception('Array index count mismatched. Hence this is not square array.');
}
$d_ar[0][] = $ar[$key];
@tanmay27vats
tanmay27vats / diagonal-difference.php
Created August 9, 2018 13:29
Diagonal difference of 2D array - PHP. Given a square matrix, calculate the absolute difference between the sums of its diagonals... Note: |x| is the absolute value of x
// Complete the diagonalDifference function below.
function diagonalDifference($arr) {
$d_ar = [];
$main_len = count($arr);
foreach($arr as $key => $ar) {
$sub_len = count($ar);
if($main_len != $sub_len) {
throw new Exception('Array index count mismatched. Hence this is not square array.');
}
@tanmay27vats
tanmay27vats / twins.php
Created April 29, 2018 12:34
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…
function Twins($a, $b) {
$flag = false;
$a_len = count($a);
$b_len = count($b);
if($a_len != $b_len) {
throw new Exception('array index count mismatched!');
}
$result = [];
for($i = 0; $i<$a_len; $i++) {
@tanmay27vats
tanmay27vats / PushNotifications.php
Created March 6, 2018 08:23
Apple Push Notifications Service (APNS) in PHP
//$vHost = 'gateway.sandbox.push.apple.com';
$vHost = 'gateway.push.apple.com';
$vPort = 2195;
$vCert = APPPATH.'../assets/certificates/Certificates-Production.pem';
$vPassphrase = 'password to your pem file';
$vToken = $data['token'];
$vAlert = array(
@tanmay27vats
tanmay27vats / function.php
Created January 21, 2018 16:07
How to upload local file on the FTP/SFTP server - PHP
// connect and login to FTP server
$ftp_server = "ftp.domain.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
//$ftp_conn = ftp_ssl_connect($ftp_server);// for SSL-FTP connection instead of ftp_connect
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "local-file.jpg";
// upload file
if (ftp_put($ftp_conn, "server-file.jpg", $file, FTP_ASCII)) {
@tanmay27vats
tanmay27vats / wizard.php
Created January 21, 2018 15:20
Change collation to utf8 of all tables of database
$dbname=’db’; //your database name
$username=’userrname’; // your database user name
$password=’password’; //your database user password
$db = mysql_connect($dbname,$username,$password);
if (!$db) {
echo "Cannot connect to the database – incorrect details";
} else {
mysql_select_db(‘pm’);
$result = mysql_query(‘show tables’);
@tanmay27vats
tanmay27vats / function.php
Created January 7, 2018 17:43
Woocommerce: Remove Cross Sell From Cart Page
remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' );
@tanmay27vats
tanmay27vats / .htaccess
Created January 3, 2018 08:01
Wordpress: How to use live images on localhost?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# custom rules for loading server images or any other uploaded media files locally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^.*/uploads/(.*)$ http://livesiteurl.com/wp-content/uploads/$1 [L,R=301,NC]
# default WordPress rules
@tanmay27vats
tanmay27vats / function.php
Created December 28, 2017 13:32
WordPress: Rename the default label “Posts” to “News” OR something else...
function tv_change_post_label()
{
global $menu;
global $submenu;
$menu[5][0] = 'News';
$submenu['edit.php'][5][0] = 'News';
$submenu['edit.php'][10][0] = 'Add News';
$submenu['edit.php'][16][0] = 'News Tags';
}
function tv_change_post_object()