Skip to content

Instantly share code, notes, and snippets.

View tanmay27vats's full-sized avatar

Tanmay Vats tanmay27vats

View GitHub Profile
@tanmay27vats
tanmay27vats / function.php
Created June 26, 2017 11:46
Show "Stock Out" if all variations are stock out.
function wc_show_variable_product_stock_out()
{
global $product;
if( $product->is_type( 'variable' ) )
{
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
@tanmay27vats
tanmay27vats / yahoo_weather.php
Last active July 23, 2018 12:03
Yahoo weather YQL API without authentication / No OAuth required - PHP script
<?php
class Yahoo_Weather
{
protected $base_url = "http://query.yahooapis.com/v1/public/yql";
protected $location = false;
protected $units = false;
protected $current = false;
protected $future = false;
protected $data = false;
@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 / 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 / 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 / script.js
Created June 14, 2019 09:24
How to check if all inputs are not empty with jQuery
$("input:empty").length == 0;
/* Above statement will bypass the empty spaces. */
/* If you want to search for all empty inputs including blank/empty spaces. Try below one */
$("input").filter(function () {
return $.trim($(this).val()).length == 0
}).length == 0;
@tanmay27vats
tanmay27vats / kill-pid.sh
Last active June 14, 2019 12:21
Delete Process by Process ID (PID) Ubuntu Linux Command
# This will print you PID of process bound on that port.
fuser 3000/tcp
# And this will kill that process
fuser -k 3000/tcp
@tanmay27vats
tanmay27vats / shopify-variant-filters.txt
Created March 9, 2017 13:07
Showing Shopify product's variants (Size / Length / Color) as filters on collection page.
/*****
Here, I am assuming that you have already assigned all variants as tags of a product. Because Shopify filter does not work with variant options.
And, if you assigns all variants as tags you can not differentiate them from tags.
Here I code that is working for 2 filters Size & Length (with mulitple options. ie mulitple sizes, multiple lengths, etc
*****/
<div class="filters-toolbar">
<div class="filters-toolbar__item size-filter">
{% assign variant_sizes = "" %}
{% assign variant_lengths = "" %}
@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 / solution.py
Created January 31, 2022 13:56
[Python] Rotate Array - Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2: Input: nu…
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
count = 0
start = 0
while (count < len(nums)):
current = start
prev = nums[start]