Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Friday, May 8, 2015

Solution to Reverse Words

This is one solution to "Reverse Words" problem in google codejam hosted at https://code.google.com/codejam/contest/351101/dashboard#s=p1

import java.io.BufferedReader;
import java.io.FileReader;

/**
 * @author: Aniruddha Dutta Chowdhury (adchowdhury@gmail.com)
 * @since: May 8, 2015
 */

public class ReverseWords {

    public static void main(String[] args) {
        String filePath = "input.txt";
       
        try {
            BufferedReader br = new BufferedReader(new FileReader(filePath));
            String sCurrentLine;
            StringBuffer sb = new StringBuffer();
            int insertIndex = 0;
            char currentChar = '\b';
            while ((sCurrentLine = br.readLine()) != null) {
                //System.out.println(sCurrentLine);
                for(int iCharCounter = 0; iCharCounter < sCurrentLine.length(); iCharCounter++) {
                    currentChar = sCurrentLine.charAt(iCharCounter);
                    if(currentChar == ' ') {
                        insertIndex = 0;
                        sb.insert(insertIndex, currentChar);
                    }else {
                        sb.insert(insertIndex++, currentChar);
                    }
                }
                System.out.println(sb);
                sb = new StringBuffer();
                insertIndex = 0;
            }
        } catch (Throwable a_th) {
            a_th.printStackTrace();
        }
    }
}

Friday, June 28, 2013

Solution to Store Credit

This is a probable solution to CODE JAM's Store Credit.


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;


public class StoreCredit {
   
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(ReverseWords.class.getClassLoader().getResourceAsStream("A-large-practice.in"));
            int problemCount = Integer.parseInt(scanner.nextLine());
            int caseCounter = 1;
            for(int iProblemCounter = 0; iProblemCounter < problemCount; iProblemCounter++) {
                Set completedIndexes = new HashSet();
               
                int creditAmount = Integer.parseInt(scanner.nextLine());
                int numberOfItems = Integer.parseInt(scanner.nextLine());
                String itemsLine = scanner.nextLine();
                String[] intArray = itemsLine.split(" ");
                String output = "";
                itemLoop:{
                    for (int iArrayCounter = 0; iArrayCounter < numberOfItems; iArrayCounter++) {
                        completedIndexes.add(iArrayCounter);
                        for (int iArrayInnerCounter = 0; iArrayInnerCounter < numberOfItems; iArrayInnerCounter++) {
                            if(completedIndexes.contains(iArrayInnerCounter)) {
                                continue;
                            }
                            int sum = (Integer.parseInt(intArray[iArrayCounter]) + Integer.parseInt(intArray[iArrayInnerCounter]));
                            output = (iArrayCounter + 1) + " " + (iArrayInnerCounter + 1);
                            if(sum == creditAmount) {
                                break itemLoop;
                            }
                        }
                    }
                }
                System.out.println("Case #" + (caseCounter++) + ": " + output);
            }
        } catch (Throwable a_th) {
            a_th.printStackTrace();
        }
    }
}
See you later friends.