Skip to content

Instantly share code, notes, and snippets.

# Manually secure port 6379
sudo iptables -A INPUT -p tcp --dport 6379 -s xxx.xxx.xxx.xxx -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 6379 -j DROP
sudo iptables -L
# Save current firewall config
sudo iptables-save > /etc/iptables.conf
# Load iptables.conf on startup
sudo nano /etc/rc.local
@cfc1020
cfc1020 / benchmark.rb
Last active June 22, 2020 09:48
Finding the k-th largest element from the array effectively and simply as well
require 'benchmark'
examples = []
res1 = []
res2 = []
def f1(arr, k)
arr.sort[arr.size - k]
end
@cfc1020
cfc1020 / max_set.go
Last active May 13, 2020 09:31
MaxSubNonNegativesArray
// Given an array of integers, A of length N, find out the maximum sum sub-array of non negative numbers from A.
/**
* @input A : Integer array
*
* @Output Integer array.
*/
package main
import "fmt"
@cfc1020
cfc1020 / compare_versions.rb
Last active June 3, 2020 18:51
Compare Versions
class MySolution
# @param a : string
# @param b : string
# @returns an integer
def compare_versions(a, b)
aa = a.split('.')
bb = b.split('.')
i = 0
@cfc1020
cfc1020 / convert_to_title.rb
Created June 5, 2020 17:18
excel-sheet-column-title
# @param {Integer} n
# @return {String}
def convert_to_title(n)
res = ''
begin
rem = n % 26
n /= 26
rem = if rem.zero?
@cfc1020
cfc1020 / binary_gap.rb
Created June 5, 2020 17:25
Return the longest run of 1s for a given integer n's binary representation.
# @param {Integer} n
# @return {Integer}
def binary_gap(n)
res = 0
while n.nonzero? do
n &= n << 1
res += 1
end
@cfc1020
cfc1020 / defang_i_paddr.rb
Last active June 6, 2020 17:41
Defanging an IP Address
# @param {String} address
# @return {String}
def defang_i_paddr(address)
address.split('.').join('[.]')
end
@cfc1020
cfc1020 / level_order.rb
Last active June 8, 2020 16:39
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level)..
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end
# @param {TreeNode} root
@cfc1020
cfc1020 / min_add_to_make_valid.rb
Created June 12, 2020 18:22
Minimum Add to Make Parentheses Valid
# @param {String} s
# @return {Integer}
def min_add_to_make_valid(s)
res = 0
balance = 0
s.each_char do |c|
balance += c == '(' ? 1 : -1
if balance < 0
@cfc1020
cfc1020 / is_palindrome.rb
Created June 12, 2020 18:36
Palindrome Number
# @param {Integer} x
# @return {Boolean}
def is_palindrome(x)
return false if x.negative?
s = x.abs.to_s
i = 0
j = s.length - 1