Thursday, September 29, 2016

Circle of Numbers (CodeFIght)

Consider integer numbers from 0 to n - 1 written down along the circle in such a way that the distance between any two neighbouring numbers is equal (note that 0 and n - 1 are neighbouring, too).

Given n and firstNumber, find the number which is written in the radially opposite position tofirstNumber.


Example


For n = 10 and firstNumber = 2, the output should be
circleOfNumbers(n, firstNumber) = 7.




Change Snippet Background Color
 




int circleOfNumbers(int n, int firstNumber) {
    return (firstNumber+(n/2)) % n;
}
 

Sunday, September 25, 2016

How To Hide Keys in Android


Android apps which make RESTful calls typically need to supply an api key when making HTTP requests. Ideally, you wanna create an Android app that uses Twitter APIs, so yo need an API key and an API secrets only you and your apps know. Because you need these values inside you app, it’s easy and quick to write them down directly in your source code. But when you commit this code to GitHub (or any other public repo), practically you’re telling your secret keys to entire world. Seems uncommon??

For example, if you declare in code

private static final String API_KEY = “123456abc”;
private static final String API_URL = “https://your-domain-api.com/api/”;

and push your changes to a public git repo, your api key is now known to everyone. Other developers could re-use it.

One simple way to avoid this bad practice is to store your values inside an environmental variable, so only your machine knows it, then read this values in some way and inject them in your code at build time. For best way where i can find is hiding your keys inside gradle.properties

Where to find gradle.properties
  1. Show Hidden files
  2. Find .gradle folder in ~/Users and Click it
  3. You will find gradle.properties file inside .gradle folder. If not exist, then create new file name gradle.properties
  4. Then store anything that you want inside it. See below example:

Note (Warning): 
  1. Amend your .gitignore file to exclude gradle.properties from version control /gradle.properties
  2. Remove your gradle.properties file from your remote git repo, if it exists.

How to use the .gradle.properties global variable

Just call the name inside the build.gradle

for example:

signingConfigs {


    release {

        storeFile file('../yourkeystore.keystore')

        storePassword PROJECT_STORE_PASSWORD

        keyAlias PROJECT_KEY_ALIAS

        keyPassword PROJECT_KEY_PASSWORD

    }


    debug{



    }

}

buildConfigField 'String','SECRETKEY',PROJECT_API_SECRET
buildConfigField 'String','URL_API',PROJECT_URL_API_DEBUG

Then if you want to call the buildConfigField inside your code : BuildConfig.URL_API to get the value in gradle.properites



That’s all, then it’s up to you how to create more elaborated configurations. 

Saturday, September 24, 2016

Type of Programmer In This World

In journeys and programming adventures, there are many strange foes, and even stranger allies. They are identified at least five different kinds of code warriors, some make for wonderful comrades in arms, while others seem to foil my every plan. However they all have their place in the pantheon of software development. Without a healthy mix of these different programming styles you’ll probably find your projects either take too long to complete, are not stable enough or are too perfect for humans to look upon.

The OCD Perfectionist Programmer
"You want to do what to my code?"

This guy is the foundation of your company. When something goes wrong he will fix it fast and in a way that won’t break again. Of course he doesn’t care about how it looks, ease of use, or any of those other trivial concerns, but he will make it happen, without a bunch of talk or time-wasting nonsense. The best way to use this person is to point at a problem and walk away.

The Duct Tape Programmer
"The code may not be pretty, but damnit, it works!"

This guy doesn’t care about your deadlines or budgets, those are insignificant when compared to the art form that is programming. When you do finally receive the finished product you will have no option but submit to the stunning glory and radiant beauty of perfectly formatted, no, perfectly beautiful code, that is so efficient that anything you would want to do to it would do nothing but defame a masterpiece. He is the only one qualified to work on his code.

The Theoretical Programmer
"Well, that’s a possibility, but in practice this might be a better alternative."

His world has one simple truth; writing code is bad. If you have to write something then you’re doing it wrong. Someone else has already done the work so just use their code. He will tell you how much faster this development practice is, even though he takes as long or longer than the other programmers. But when you get the project it will only be 20 lines of actual code and will be very easy to read. It may not be very fast, efficient, or forward-compatible, but it will be done with the least effort required.

The Half-Assed Programmer
"What do you want? It works doesn’t it?"

The guy who couldn’t care less about quality, that’s someone elses job. He accomplishes the tasks that he’s asked to do, quickly. You may not like his work, the other programmers hate it, but management and the clients love it. As much pain as he will cause you in the future, he is single-handedly keeping your deadlines so you can’t scoff at it (no matter how much you want to).

The Anti-programming Programmer
"I’m a Programmer, damnit. I don’t write code."

This guy is more interested the options than what should be done. He will spend 80% of his time staring blankly at his computer thinking up ways to accomplish a task, 15% of his time complaining about unreasonable deadlines, 4% of his time refining the options, and 1% of his time writing code. When you receive the final work it will always be accompanied by the phrase “if I had more time I could have done this the right way”.



Sources: Steve Banner

Sunday, September 18, 2016

CPROM 2016 Q10 Adding Hexadecimal Number

Download Question Here

Change Snippet Background Color
 
import java.util.*;

/**
   Author : Hafiq
   Date :
**/

public class HexaDec{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String line = System.getProperty("line.separator");
      scan.useDelimiter(line);
      
      int cases = scan.nextInt();
      
      for(int x=0;x<cases;x++){
         String[] split = scan.next().split(" ");
         
         int one = Integer.parseInt(split[0],16);
         int two = Integer.parseInt(split[1],16);
         
         System.out.println(Integer.toHexString(one+two).toUpperCase());
  
      }
      
   }
} 
 

CPROM 2016 Q9 SERIES OF NUMBERS

Download Question Here

Change Snippet Background Color
 
import java.util.*;


/**
   Author : Hafiq
   Date :
**/

public class NoPlate{

   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      
      String in = scan.next();
      
      String[] split = in.split("(?<=\\D)(?=\\d)");
      
      int num = Integer.parseInt(split[1]);
      boolean tukar = false;
      String cur = split[0];
      
      for(int x=0;x<5;x++){
         num += 1;
         if(num > 9999){
            num = 1;
            tukar = true;
         }
         else
            tukar = false;
         
         int count = 0;   
         if(tukar){
            int z=0;
            while(true){
               if(cur.equals(plateStr(z))){
                  count = z;
                  break;
               }
               z++;
            }
            
            cur = plateStr(count+1);
         }
         
         System.out.println(cur+""+num);        
      }
   }
   
   static String plateStr(int x) {
      return x < 0?"":plateStr((x / 26) - 1) + (char)(65 + x % 26);
   }
   
   
   //ZZ9997
   //ZZ9998
   //ZZ9999
   //AAA1
   //AAA2
} 
 

CPROM 2016 Q8 LEXICOGRAPHIC ORDERING

Download Question Here

Change Snippet Background Color
 
import java.util.*;


/**
   Author : Hafiq
   Date :
**/

public class Lexi{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String line = System.getProperty("line.separator");
      scan.useDelimiter(line);
      
      int cases = scan.nextInt();
      String[] list = new String[cases];
      for(int x=0;x<cases;x++){
         list[x] = scan.next();
      }
      
      Arrays.sort(list);
      
      for(String ls:list)
         System.out.println(ls);
  }
  
} 
 

CPROM 2016 Q7 Simple Average And Median

Download Question Here

Change Snippet Background Color
 
import java.util.*;


/**
   Author : Hafiq
   Date :
**/

public class Aveg{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String line = System.getProperty("line.separator");
      scan.useDelimiter(line);
      
      int cases = scan.nextInt();
      
      for(int x=0;x<cases;x++){
         int count = scan.nextInt();
         String[] split = scan.next().split(" ");
         int [] num = new int[count];
         for(int z=0;z<count;z++){
            num[z] = Integer.parseInt(split[z]);
         }
         Arrays.sort(num);
         
         int median = 0;double mean = 0;
         int mid = count/2;
         
         if(count%2 == 0)
            median = (num[mid-1]+num[mid]) / 2;
         else
            median = num[mid];
                     
         for(int y=0;y<count;y++){
            mean += num[y];
         }
         
         System.out.println("Average:"+mean/count+" Median:"+median);
         
      }
   }
} 
 

CPROM 2016 Q6 Title Case

Download Question Here

Change Snippet Background Color
 
import java.util.*;


/**
   Author : Hafiq
   Date :
**/

public class TitleCase{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String line = System.getProperty("line.separator");
      scan.useDelimiter(line);
      int cases = scan.nextInt();
      
      for(int x=0;x<cases;x++){
         String[] split = scan.next().split(" ");
         
         String temp = "";
         for(int y=0;y<split.length;y++){
            temp += (""+split[y].charAt(0)).toUpperCase() + split[y].substring(1,split[y].length());
            if(y<=split.length-1){
               temp+=" ";
            }
         }
         
         System.out.println(temp);
      }
   
   }
} 
 

CPROM 2016 Q5 Encryption

Download Question Here

Change Snippet Background Color
 
import java.util.*;


/**
   Author : Hafiq
   Date :
**/

public class Encryption{

   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String input = scan.nextLine();
      
      String temp = "";
      int count = 1;
      for(int x=0;x<input.length();x++){
         char a = input.charAt(x);
         
         if(a == ' '){
            temp += (char)(a+count);
            count++;
         }
         else{
            temp += (char)(a+2);
         }
      }
      
      System.out.println(temp);
   
   }
} 
 

CPROM 2016 Q4 Pair Isograms

Download Question Here

Change Snippet Background Color

import java.util.*;

/**
   Author : Hafiq
   Date :
**/

public class PairIsoGram{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String line = System.getProperty("line.separator");
      scan.useDelimiter(line);
      
      int cases = scan.nextInt();
      
      for(int x=0;x<cases;x++){
         String input = scan.next();
         Map<Character, Integer> count = new HashMap<Character, Integer>();
         for(int y=0;y<input.length();y++){
            char c = input.charAt(y);
            if (count.containsKey(c)) {
                int cnt = count.get(c);
                count.put(c, ++cnt);
            } else {
                count.put(c, 1);
            }
         }        
         boolean flag = true;
         for(Map.Entry<Character, Integer> entry : count.entrySet()){
            if(entry.getValue() != 2){
               flag = false;
               break;
            }
         }         
         System.out.println(input+" --- "+ ((flag)?"pair program":"not pair isogram"));
      }       
   }
}

Saturday, September 17, 2016

CPROM 2016 Q3 Find The Winner

Download Question Here

Change Snippet Background Color
 
import java.util.*;


/**
   Author : Hafiq
   Date :
**/

public class Winner{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String line = System.getProperty("line.separator");
      scan.useDelimiter(line);
      
      int cases = scan.nextInt();
      
      int max = 0;
      String current = "";
      Event ev = new Event();
      List event = new ArrayList<>();
      for(int x=0;x(){
         @Override
         public int compare(Event e1,Event e2){
            if (e1.gold > e2.gold) {
               return -1;
            } else if (e1.gold < e2.gold) {
               return 1;
            } else {
               return 0;
            }
         }
      });
      
       
      for(Event e:event){
         System.out.println(e.toString());
      }
   }
}

class Event{
      public String name;
      public int gold;
      public int silver;
      public int bronze;
      
      public Event(){}
      
      public Event(String a,int b,int c, int d){
          name = a;
          gold = b;
          silver = c;
          bronze = d;
      }
      
      public String toString(){
         return name+" "+gold+" "+silver+" "+bronze;
      }
   }
 

CPROM2016 Q2 Split And Count

Download Question Here

Change Snippet Background Color
	
import java.util.*;
/**
   Author : Hafiq
   Date :
**/
public class splitString{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String[] input = scan.nextLine().split("[\\s@&.;$?+-]+");
      System.out.println("Substrings length:"+input.length);
      for(int x=0;x<input.length;x++){
         System.out.println("Str["+x+"]"+input[x]+" Length:"+input[x].length());
      }
   }
}
	

CPROM2016 Q1 Multiplication Table

Download Question Here

Change Snippet Background Color
 
import java.util.*;
/**
   Author : Hafiq
   Date :
**/
public class multiplicationTable{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      int input = scan.nextInt();
      for(int x=1;x<=12;x++){
         System.out.println(x+"*"+input+"="+(x*input));
      }
   }
}