Wednesday, September 16, 2015

Comparing Currency Exchanges (CPROM UiTM Arau 2015)

You will need to convert Ringgit Malaysia (RM) to dollar currency. There are two currency exchange booths. Each has a display that shows their conversion rate (CR) as a dollar per RM and their fee as a percentage. The fee is taken before your money is converted. You need to identify which booth will give you the most dollars for your RM, how many dollars and how much is the difference. For example:

Ringgit Malaysia (RM) : 200

Conversion Rate (CR1)
: 0.26 Fee  
: 1%  (amount  51.48 dollars)

Conversion Rate (CR2)
: 0.27 Fee  
: 2% (amount 52.92 dollars)

Answer: 2 is the best; 52.92 dollars; difference is 1.44

Input 
The first line of the input contains the number of test cases, which entered by the user. Following the first line are the test cases. Each line in a test case contains RM to convert, CR1, Fee for CR1, CR2 and Fee for CR2.
The input must be read from standard input.

Output
The output of the program should print the booth with the most dollars, the amount of dollars and the difference amount between booth. . The output must be written to standard output.

Sample Input
3
200 0.26 1 0.27 2
1000 0.3 2 0.28 1
100 0.24 3 0.27 3

Sample Output
2 is the best;52.92 dollars;difference is 1.44
1 is the best;294.00 dollars;difference is 16.80
2 is the best;26.19 dollars;difference is 2.91



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Solved By Afiq
import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.*;

public class Q1{
   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 [] ans = new String[cases];
      
      DecimalFormat df = new DecimalFormat("0.00");
      
      for(int x=0;x<cases;x++){
         String[] get = scan.next().split(" ");
         
         double amount = Integer.parseInt(get[0]);
         double cr1 = Double.parseDouble(get[1]);
         double cr1p = Double.parseDouble(get[2]);
         double cr2 = Double.parseDouble(get[3]);
         double cr2p = Double.parseDouble(get[4]);
         
         double totalc1 = (amount*cr1) - ((amount*cr1)*(cr1p/100));
         double totalc2 = (amount*cr2) - ((amount*cr2)*(cr2p/100));
                  
         double diff = Math.abs(totalc1-totalc2); 
         
         int best=0;double bestc=0;
         if(totalc1<totalc2){
            best=2;
            bestc = totalc2;
         }
         else{
            best=1;
            bestc = totalc1;
         }
         
         ans[x] = best+" is the best;"+df.format(bestc)+" dollars;difference is "+df.format(diff);
         
      }
      
      for(int x=0;x<cases;x++){
         System.out.println(ans[x]);
      }
   }
}

Friday, September 11, 2015

Time Conversion

Problem Statement

You are given time in AM/PM format. Convert this into a 24 hour format.

Note Midnight is 12:00:00AM or 00:00:00 and 12 Noon is 12:00:00PM.

Input Format

Input consists of time in the AM/PM format i.e. hh:mm:ssAM or hh:mm:ssPM
where
01≤hh≤12
00≤mm≤59
00≤ss≤59

Output Format

You need to print the time in 24 hour format i.e. hh:mm:ss
where
00≤hh≤23
00≤mm≤59
00≤ss≤59



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  String s = "07:05:45PM";
  DateFormat inFormat = new SimpleDateFormat( "hh:mm:ssaa");
  DateFormat outFormat = new SimpleDateFormat( "HH:mm:ss");

  Date date = null;
  try {
   date = inFormat.parse(s); 
  }catch (ParseException e ){
   e.printStackTrace();
  }
  if( date != null ){
   String myDate = outFormat.format(date);
   System.out.println(myDate);
  }
 }

}

Monday, September 7, 2015

Triangle Pattern


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public static void main(String []args){
        int num = 6;
        
        for(int x=0;x<num;x++){
            for(int y=0;y<(num-x-1);y++){
                System.out.print(" ");
            }
            for(int z=0;z<=x;z++){
                System.out.print("*");
                for(int a=0;a<z;a = a+z){
                    System.out.print("*");
                }
            }
            System.out.println();
        }
     }

Count Posibble Coin Changing Problem (Dynamic Programming)

From previous "Count Posibble Coin Changing Problem (Recursive)"


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class HelloWorld {

 public static void main(String[] args) {
   int[] arr = {5,37,8,39,33,17,22,32,13,7,10,35,40,2,43,49,46,19,41,1,12,11,28};
   int m = (int)arr.length;
   int n = 166;
   System.out.print(" " + count(arr, m, n) + " ");
 }
public static int count(int[] s, int m, int n) { long x, y; // We need n+1 rows as the table is constructed in bottom up manner using // the base case 0 value case (n = 0) long[][] coins = new int[n + 1][m]; // Fill the entries for 0 value case (n = 0) for(int i = 0; i < m; i++) { coins[0][i] = 1; } // Fill rest of the table entries in bottom up manner for(int i = 1; i < n + 1; i++) { for(int j = 0; j < m; j++) { // Count of solutions including S[j] x = i - s[j] >= 0 ? coins[i - s[j]][j] : 0; // Count of solutions excluding S[j] y = j >= 1 ? coins[i][j - 1] : 0; // total count coins[i][j] = x + y; } } return coins[n][m - 1]; } }

Count Posibble Coin Changing Problem (Recursive)

Problem Statement

How many different ways can you make change for an amount, given a list of coins? In this problem, your code will need to efficiently compute the answer.

Task

Write a program that, given

An amount N and types of infinite available coins M
A list of M coins - C={C1,C2,C3,..,CM}

The problem can be formally stated:
Given a value N, if we want to make change for N cents, and we have infinite supply of each of C={C1,C2,…,CM} valued coins, how many ways can we make the change? The order of coins doesn’t matter.

Input : first line is the amount and the second line is the list of distinct coins
Output: Total of combination to get the amount

Input
21
1 2 5 10 20 50 100

Output
44



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
public class HelloWorld{

    public static void main(String []args){
        int [] sets = new int[]{1,2,5,10,20,50,100};
        int amount = 21;
        System.out.println(findCount(amount,sets,0));
    }

    public static int findCount(int amount, int coins[], int Index) {
        if (amount == 0)
            return 1;
        else if (amount < 0 || coins.length ==Index)
            return 0;
        else {
            int firstCoin = findCount(amount-coins[Index], coins, Index);
            int nextCoin = findCount(amount, coins, Index+1);
            return firstCoin + nextCoin;
        }
    }
}

Saturday, September 5, 2015

Christmas Tree Pattern

Write code that output a Christmas tree pattern

Output:
     *    
     *    
    ***    
     *     
    ***    
   *****   
     *     
    ***     
   *****    
  *******  


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class HelloWorld{

     public static void main(String []args){
        int max=10; // height
        
        for(int x=0;x<max;x++){
            for(int y=0;y<x+1;y++){
                for(int z=0;z<max-y-1;z++){
                    System.out.print(" ");
                }
                for(int z=0;z<(y*2)+1;z++){
                    System.out.print("*");
                }
                
                System.out.println();
            }
        }
     }
}

Wednesday, August 26, 2015

Manny's password Problem

Danny has a possible list of passwords of Manny's facebook account. All passwords length is odd. But Danny knows that Manny is a big fan of palindromes. So, his password and reverse of his password both should be in the list.

You have to print the length of Manny's password and it's middle character.

Note : The solution will be unique.

INPUT
The first line of input contains the integer N, the number of possible passwords.
Each of the following N lines contains a single word, its length being an odd number greater than 2 and lesser than 14. All characters are lowercase letters of the English alphabet.

OUTPUT
The first and only line of output must contain the length of the correct password and its central letter.

CONSTRAINTS
1 ≤ N ≤ 100

Sample Input(Plaintext Link)
4
abc
def
feg
cba

Sample Output(Plaintext Link)
3 b



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
import java.io.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
  Scanner scan = new Scanner(System.in);
  int cases = scan.nextInt();
  
  String [] data = new String[cases];
  for(int x=0;x<cases;x++){
   data[x] = scan.next();
  }
  String mid="";
  for(int x=0;x<data.length-1;x++){
   for(int y=1;y<data.length;y++){
    if(palin(data[x]).equals(data[y]))
     mid = data[x].length()+" "+data[x].substring(data[x].length() / 2, data[x].length() / 2 + 1 );
     
   }
  }
  
  System.out.print(mid);
    }
    
    public static String palin(String x){
     String value = "";
     
     value = new StringBuffer(x).reverse().toString();
      
     return value;
    }
}