Skip to content

Instantly share code, notes, and snippets.

View tanmay27vats's full-sized avatar

Tanmay Vats tanmay27vats

View GitHub Profile
@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 / 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 / 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]
@tanmay27vats
tanmay27vats / solution.php
Created January 31, 2022 14:04
[PHP] 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: nums …
class Solution {
/**
* @param Integer[] $nums
* @param Integer $k
* @return NULL
*/
function rotate(&$nums, $k) {
$count = 0;
$start = 0;
@tanmay27vats
tanmay27vats / solution.rb
Created January 31, 2022 14:21
[Ruby] 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: nums…
# @param {Integer[]} nums
# @param {Integer} k
# @return {Void} Do not return anything, modify nums in-place instead.
def rotate(nums, k)
count = 0
start = 0
while (count < nums.size)
current = start
prev = nums[start]
loop do
@tanmay27vats
tanmay27vats / solution.py
Last active February 1, 2022 07:24
[Python] Roman to Integer - Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
class Solution:
def romanToInt(self, s: str) -> int:
roman_dict = { 'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000, 'IV':4, 'IX':9, 'XL':40, 'XC':90, 'CD':400, 'CM':900 }
i = 0
sum = 0
while i < len(s):
if s[i:i+2] in roman_dict and i+1<len(s):
sum+=roman_dict[s[i:i+2]]
i+=2
else:
@tanmay27vats
tanmay27vats / solution.py
Created February 1, 2022 07:10
[Python] Longest Common Prefix - Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: strs = ["flower","flow","flight"] Output: "fl" Example 2: Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among …
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs) == 0:
return ""
common_prefix = strs[0]
for i in range(1,len(strs)):
temp = ""
if len(common_prefix) == 0:
break
for j in range(len(strs[i])):
@tanmay27vats
tanmay27vats / solution.py
Created February 1, 2022 08:13
[Python] Valid Parentheses - Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Example 1: Input: s = "()" Output: true Example 2: Input: …
class Solution:
def isValid(self, s: str) -> bool:
s = s.replace(' ','')
if len(s)%2 != 0:
return False
dict = {'(' : ')', '[' : ']', '{' : '}'}
ar = []
for i in s:
if i in dict:
ar.append(i)