..MindWrite..

Author Archive

Insert only if not exists – mysql

Posted by guptaradhesh on April 3, 2014

 

Example:

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];
		else
		{	
			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]) {
    l–;
    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 »

Print matrix in a sprial order

Posted by guptaradhesh on August 22, 2013

 

Recursive Solution (Java):

public class PrintMatrixSpiral
{
 public static void main(String[] args)
 {
  int[][] matrix =
  {
  { 2, 4, 6, 2, 5, 7 },
  { 2, 5, 7, 8, 9, 3 },
  { 6, 4, 7, 3, 5, 7 } };

  printSpiral(matrix);
 }

 public static void printSpiral(int[][] matrix)
 {
  printSpiral(matrix, 0);
 }

 private static void printSpiral(int[][] matrix, int depth)
 {
  if (matrix == null && matrix.length == 0)
   return;
  int rows = matrix.length;
  int cols = matrix[0].length;
  if (2 * depth > Math.min(rows, cols))
   return;
  for (int i = depth; i < cols - depth - 1; ++i)
  {
   System.out.print(matrix[depth][i] + ",");
  }
  for (int i = depth; i < rows - depth - 1; ++i)
  {
   System.out.print(matrix[i][cols - depth - 1] + ",");
  }
  for (int i = rows - depth; i > depth; --i)
  {
   System.out.print(matrix[rows - depth - 1][i] + ",");
  }
  for (int i = rows - depth - 1; i > depth; --i)
  {
   System.out.print(matrix[i][depth] + ",");
  }
  printSpiral(matrix, ++depth);
 }
}


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