Author Archive

Python Dynamic Array Implementation

Posted by guptaradhesh on July 30, 2017

import ctypes

class DynamicArray(object):
    DYNAMIC ARRAY CLASS (Similar to Python List)
    def __init__(self):
        self.n = 0 # Count actual elements (Default is 0)
        self.capacity = 1 # Default Capacity
        self.A = self.make_array(self.capacity)
    def __len__(self):
        Return number of elements sorted in array
        return self.n
    def __getitem__(self,k):
        Return element at index k
        if not 0 <= k <self.n:
            return IndexError('K is out of bounds!') # Check it k index is in bounds of array
        return self.A[k] #Retrieve from array at index k
    def append(self, ele):
        Add element to end of the array
        if self.n == self.capacity:
            self._resize(2*self.capacity) #Double capacity if not enough room
        self.A[self.n] = ele #Set self.n index to element
        self.n += 1
    def _resize(self,new_cap):
        Resize internal array to capacity new_cap
        B = self.make_array(new_cap) # New bigger array
        for k in range(self.n): # Reference all existing values
            B[k] = self.A[k]
        self.A = B # Call A the new bigger array
        self.capacity = new_cap # Reset the capacity
    def make_array(self,new_cap):
        Returns a new array with new_cap capacity
        return (new_cap * ctypes.py_object)()

Posted in uncategorized | Leave a Comment »

Insert only if not exists – mysql

Posted by guptaradhesh on April 3, 2014



insert into mytable
(my_string, created_at, removed, flag)
SELECT ‘this string should be unique’,curdate(), 0, ‘new’ FROM temp
Where not exists
(select * from mytable where my_string = ‘this string should be unique’);

// make sure there is no parenthesis in select
// it seems, using a temp table is a requirement

Posted in tech | Tagged: , , | Leave a Comment »

Remove first word using sed

Posted by guptaradhesh on April 2, 2014

cat filename | sed -e 's/^ *[^ ]* //'

Rating: 4.5

Posted in uncategorized | Tagged: , | Leave a Comment »

Maximum subarray in an array

Posted by guptaradhesh on March 26, 2014

	 * Returns a maximum sum value in an array of integers
	public int maxScan(int[] array)
		if(array.length == 0) return -1;
		else if(array.length == 1) 
			return array[0];
			int maxSum = array[0];
			int current_max = array[0];
			for(int i = 1; i < array.length; i++)
				current_max = Math.max(array[i], current_max + array[i]); 
				maxSum = Math.max(maxSum, current_max);
			return maxSum;

Posted in uncategorized | Tagged: , | Leave a Comment »

Which linux distribution version and distribution are you using?

Posted by guptaradhesh on February 3, 2014

To find which linux version and distribution you are using:

# uname -a

# cat /proc/version

Posted in uncategorized | Tagged: , , , | Leave a Comment »

Copy files from one ec2 instance to other ec2 instance

Posted by guptaradhesh on January 7, 2014

Doing an scp gave the following error message:

Permission denied (publickey)

The reason for the above is that it asks for ec2 instance password while we use .ppk file now days.

Here are the quick steps one should follow:

  1. server 1
    1. generate the id_rsa.pub
      ssh-keygen -t rsa
    2. /root/.ssh/id_rsa or /home/ec2-user/.ssh/id_rsa
    3. No need to enter any passphrase
  2. server 2
    1. Open /etc/ssh/sshd_config/sshd_config
    2. If logged as ec2-user; susu su
      Uncomment the lines below if they are not already
      RSAAuthentication yes
      PubkeyAuthentication yes
    3.  Open /root/.ssh/authorized_keys
  3. append contents of id_rsa.pub file of server 1 to authorized_keys of server 2

Posted in tech, unix | Tagged: , , | Leave a Comment »

Designing a distributed scalable web application

Posted by guptaradhesh on August 30, 2013

Here is a very interesting and useful read: here

Posted in markets, tech | Tagged: , , , , | Leave a Comment »

Crowdsourcing vs Crowdfunding

Posted by guptaradhesh on August 30, 2013

Crowsdsourcing is when you have many people to help you with a project with ideas, testing, labor to build a better product. Open-source is a big example of crowd-sourcing. Leveraging the collective coding power of freelance programmers to deliver a great product. Another great examples being Wikipedia. Reddit, Digg being more examples of the same.

Crowdfunding is about the financing of projects and people by (large) crowds. Crowdfunding is a DIWO (do it with others) approaching to contributing to a cause they believe in. In a nutshell, it involves soliciting an audience of potential fans and convincing those fans that your film project is worthy of a donation from them to help you make the film. Or raising money for education or medical treatments.
Now they’re bringing these concepts home. Instead of just giving money away because it feels good or is the right thing to do, now you can do so and own shares of stock in that business!
Some of the top crowdfunding sites are: Kickstarter, Indiegogo, Crowdfunder, Angelist, Quirky ..

Posted in markets, tech | Tagged: , , , , , , | Leave a Comment »

Finding longest palindrome in a string

Posted by guptaradhesh on August 26, 2013

Here is an elegant code I found.

string expandAroundCenter(string s, int c1, int c2) {

  int l = c1, r = c2;
  int n = s.length();
  while (l >= 0 && r <= n-1 && s[l] == s[r]) {
  return s.substr(l+1, r-l-1);
string longestPalindromeSimple(string s) {
  int n = s.length();
  if (n == 0) return “”;
  string longest = s.substr(0, 1);  // a single char itself is a palindrome
  for (int i = 0; i < n-1; i++) {
    string p1 = expandAroundCenter(s, i, i);
    if (p1.length() > longest.length())
      longest = p1;
    string p2 = expandAroundCenter(s, i, i+1);
    if (p2.length() > longest.length())
      longest = p2;
  return longest;
source: here

Posted in java, puzzles/ algorithms, tech | Tagged: , , , | Leave a Comment »

Why char array is preferred over String for storing password?

Posted by guptaradhesh on August 23, 2013

String is immutable in java and stored in String pool. Once it’s created it stays in the pool until unless garbage collected, so even though we are done with password it’s available in memory for longer duration and there is no way to avoid it. It’s a security risk because anyone having access to memory dump can find the password as clear text.

If we use char array to store password, we can set it to blank once we are done with it. So we can control for how long it’s available in memory that avoids the security threat with String.

Posted in java, tech | Tagged: , , , | Leave a Comment »