Pages

Showing posts with label Ad Hoc. Show all posts
Showing posts with label Ad Hoc. Show all posts

Sunday, 13 December 2015

CodeEval - Black card - Easy

import sys

def black_card(test):
    arr = test.split(" | ")
    n = int(arr[1])
    valz = arr[0].split()
    while len(valz) > 1:
        valz.pop((n%len(valz))-1)
    return valz.pop()

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    print black_card(test)
test_cases.close()

Wednesday, 21 October 2015

CodeEval - One zero, two zeros... - Easy

import sys

def have_zeroes(num,val):
    val_bin = bin(val)[2:]
    counter = 0
    for c in val_bin:
        if c == '0':
            counter += 1
    return counter == num

def range_vals(num,val):
    counter = 0
    for i in range(1,val+1):
        if have_zeroes(num,i):
            counter += 1
    return counter

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
   arr = map(int, test.split())
   print range_vals(arr[0],arr[1])

test_cases.close()

Sunday, 4 October 2015

CodeEval - Matrix Rotation - Easy

import sys
import math

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    arr = test.split()
    values = []
    n = int(math.sqrt(len(arr)))
    matrix = [0]*n
    for i in range(n):
        array = [0]*n
        for j in range(n):
            array[j] = arr[i*n + j]
        matrix[i] = array
    sol = [0]*n
    for i in range(n):
        array = [0]*n
        for j in range(n):
            array[j] = matrix[n-j-1][i]
        sol[i] = array
    ans = ""
    for i in range(n):
        for j in range(n):
            if i > 0 or j > 0 :
                ans += " "
            ans += sol[i][j]
    print ans

test_cases.close()

Saturday, 26 September 2015

CodeEval - Time to eat - Easy


import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

class Time implements Comparable<Time>{
    int hour;
    int minute;
    int second;

    public Time(int hour, int minute, int second) {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    @Override
    public String toString() {
        return String.format("%02d:%02d:%02d", this.hour, this.minute, this.second);
    }

    @Override
    public int compareTo(Time o) {
         if(this.hour<o.hour){
            return 1;
        }
        if(this.hour>o.hour){
            return -1;
        }
        if(this.minute<o.minute){
            return 1;
        }
        if(this.minute>o.minute){
            return -1;
        }
        if(this.second<o.second){
            return 1;
        }
        return 1;
    }
}

public class Main {
    static String parser(String str){
        StringTokenizer st=new StringTokenizer(str);
        Time[] time=new Time[st.countTokens()];
        int i=0;
        while (st.hasMoreTokens()) {
            StringTokenizer st2=new StringTokenizer(st.nextToken(),":");
            time[i]=new Time(Integer.valueOf(st2.nextToken()),
                    Integer.valueOf(st2.nextToken()),
                    Integer.valueOf(st2.nextToken()));
            i++;
        }
        Arrays.sort(time);
        StringBuilder sb=new StringBuilder();
        for (int j = 0; j < time.length ; j++) {
            if(j > 0){
                sb.append(" ");
            }
            sb.append(time[j].toString());
        }
        sb.append("\n");
        return sb.toString();
    }
   
    public static void main (String[] args) throws IOException {
        File file = new File(args[0]);
        BufferedReader buffer = new BufferedReader(new FileReader(file));
        String line;
        while ((line = buffer.readLine()) != null) {
            line = line.trim();
            System.out.print(parser(line));
        }
    }
}

Wednesday, 16 September 2015

CodeEval - Query Board - Easy

import sys

test_cases = open(sys.argv[1], 'r')
matrix = [[0 for x in range(256)] for x in range(256)]
for test in test_cases:
    query = test.split()
    command = query[0]
    index = int(query[1])
    if command == "SetRow":
        value=int(query[2])
        for i in range(256):
            matrix[index][i] = value
    elif command == "SetCol":
        value=int(query[2])
        for i in range(256):
            matrix[i][index] = value
    elif command == "QueryRow":
        sum=0
        for i in range(256):
            sum += matrix[index][i]
        print sum
    elif command == "QueryCol":
        sum=0
        for i in range(256):
            sum += matrix[i][index]
        print sum
   

test_cases.close()

Tuesday, 15 September 2015

CodeEval - Delta Time - Easy

import sys

def get_bigger(time1,time2):
    for i in range(3):
        if time1[i] > time2[i]:
            return time1,time2
        elif time1[i] < time2[i]:
            return time2,time1
    return time1,time2

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    timestamp=test.split()
    time1=map(int,timestamp[0].split(":"))
    time2=map(int,timestamp[1].split(":"))
    time1,time2=get_bigger(time1,time2)
    val=[0]*3
    for i in range(2,-1,-1):
        val[i]=time1[i]-time2[i]
        if val[i] < 0:
            if i > 0 :
                val[i] += 60
                time1[i-1] -= 1
            else:
                val[i] += 24
    sol=""
    for i in range(3):
        if i > 0:
            sol += ":"
        sol += str(val[i]).zfill(2)
    print sol

test_cases.close()

CodeEval - Compare Points - Easy

import sys

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    cord = map(int, test.split())
    sol = ""
    if cord[1] > cord[3]:
        sol += "S"
    elif cord[1] < cord[3]:
        sol += "N"
    if cord[0] > cord[2]:
        sol += "W"
    elif cord[0] < cord[2]:
        sol += "E"
    if cord[0] == cord[2] and cord[1] == cord[3]:
        sol += "here"
    print sol

test_cases.close()

Sunday, 13 September 2015

CodeEval - Details - Easy

import sys

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    rows=test.split(',')
    min=10
    for row in rows:
        counter=0
        for c in row:
            if c == 'Y':
                break
            if c == '.':
                counter += 1
        if counter < min:
            min = counter
            if min == 0 :
                break
        if min == 0 :
            break
    print min
test_cases.close()

Wednesday, 9 September 2015

CodeEval - Clean up the words - Easy

import sys

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    sol=""
    for char in test:
        if char.isalpha():
            sol += char
        else :
            sol += " "
    sol = sol.strip()
    words = sol.split()
    sol = ""
    for i in range(len(words)):
        if i > 0 :
            sol += " "
        sol += words[i].lower()
    print sol

test_cases.close()

Monday, 7 September 2015

CodeEval - Stepwise word - Easy

import sys

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    words=test.split()
    max_word=""
    max_length=0
    for i in range(len(words)):
        word_length=len(words[i])
        if word_length > max_length:
            max_length = word_length
            max_word = words[i]
    sol=""
    for i in range(max_length):
        if i> 0 :
            sol += " "
        for j in range(i):
            sol += "*"
        sol += max_word[i]
    print sol

test_cases.close()

CodeEval - Find the highest score - Easy

import sys

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    rows=test.split(" | ")
    maxVals=rows[0].split(" ")
    for i in range(1,len(rows)):
        cols=rows[i].split()
        for j in range(len(cols)):
            if int(maxVals[j]) < int(cols[j]):
                maxVals[j] = cols[j]
    sol=""
    for i in range(len(maxVals)):
        if i > 0 :
            sol += " "
        sol+=maxVals[i]
    print sol

test_cases.close()

Sunday, 6 September 2015

CodeEval - Strings and arrows - Easy

import sys

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    window=""
    for i in range(4):
        window+=test[i]
    counter=0
    for i in range(4,len(test)):
        window+=test[i]
        if(window==">>-->" or window=="<--<<"):
            counter+=1
        window=window[1:]
    print counter

test_cases.close()

Sunday, 16 August 2015

CodeEval - Max Range Sum - Easy

import sys

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    arr=test.split(";")
    valz=map(int,arr[1].split(" "))
    sum=0
    for i in range(int(arr[0])):
        sum = sum + valz[i]
    max=sum
    if max<0:
        max=0
    for i in range(int(arr[0]),len(valz)):
        sum = sum + valz[i]
        sum = sum - valz[i-int(arr[0])]
        if (max<sum):
            max=sum
    print max
test_cases.close()

Monday, 25 May 2015

CodeEval - Swap Numbers - Easy

import java.io.*;
import java.util.StringTokenizer;

public class Main {
    public static void main (String[] args) throws IOException {
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        String line;
        StringBuffer out=new StringBuffer();
        while ((line = buffer.readLine()) != null) {
            if(line.equals("#")){
                break;
            }
            StringTokenizer st=new StringTokenizer(line);
            int n=st.countTokens();
            for(int i=0;i<n;i++){
                if(i>0){
                    out.append(' ');
                }
                String nextToken=st.nextToken();
                out.append(nextToken.charAt(nextToken.length()-1))
                   .append(nextToken.substring(1,nextToken.length()-1))
                   .append(nextToken.charAt(0));
            }
            out.append('\n');
        }
        System.out.print(out);
    }
}

CodeEval - Read More - Easy

import java.io.*;

public class Main {
    public static void main (String[] args) throws IOException {
       
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        String line;
        StringBuffer out=new StringBuffer();
        while ((line = buffer.readLine()) != null) {
            line = line.trim();
            if(line.length()>55){
                if(line.charAt(39)==' '){
                   out.append(line.substring(0, 39));
                }
                else{
                   boolean flag=true;
                   for(int i=39;i>0;i--){
                       if(line.charAt(i)==' '){
                           out.append(line.substring(0, i));
                           flag=false;
                           break;
                       }
                   }
                   if(flag){
                       out.append(line.substring(0, 40));
                   }
                }
                out.append("... <Read More>");
            }else{
                out.append(line);
            }
            out.append("\n");
        }
        System.out.print(out);
    }
}

Sunday, 7 September 2014

CodeEval - Lettercase Percentage Ratio - Easy

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws FileNotFoundException, IOException {

    File file = new File(args[0]);
    BufferedReader in = new BufferedReader(new FileReader(file));
        StringBuffer sb=new StringBuffer();
        String line;
        while ((line = in.readLine()) != null) {
            int counter=0;
            int n=0;
            for(int i=0;i<line.length();i++){
                char c=line.charAt(i);
                if(c>='a'&&c<='z'){
                    counter++;
                    n++;
                }else if(c>='A'&&c<='Z'){
                    n++;
                }
            }
            sb.append(String.format("lowercase: %.2f uppercase: %.2f", (counter*100.0)/n,((n-counter)*100.0)/n)).append('\n');
        }
        System.out.print(sb);
    }
   
}

CodeEval - Juggling With Zeros - Easy

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws FileNotFoundException, IOException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer();
        String line;
        while ((line = in.readLine()) != null) {
            if(line.equals("#")){
                break;
            }
            StringTokenizer st=new StringTokenizer(line);
            StringBuilder strBin=new StringBuilder();
            while(st.hasMoreTokens()){
                String rule=st.nextToken();
                if(rule.equals("0")){
                    strBin.append(st.nextToken());
                }else{
                    int n=st.nextToken().length();
                    for(int i=0;i<n;i++){
                       strBin.append(1);
                    }
                }
            }
            sb.append(Long.parseLong(strBin.toString(), 2)).append('\n');
        }
        System.out.print(sb);
    }
   
}

CodeEval - Roller Coaster - Easy

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws FileNotFoundException, IOException {

    File file = new File(args[0]);
    BufferedReader in = new BufferedReader(new FileReader(file));
        StringBuffer sb=new StringBuffer();
        String line;
        while ((line = in.readLine()) != null) {
            boolean upperCase=true;
            for(int i=0;i<line.length();i++){
               char c=line.charAt(i);
               if(c>='a' &&c<='z'){
                   if(upperCase){
                      sb.append((char)(c-32));
                   }else{
                       sb.append(c);
                   }
                   upperCase=!upperCase;
               }else if(c>='A'&&c<='Z'){
                   if(!upperCase){
                      sb.append((char)(c+32));
                   }else{
                       sb.append(c);
                   }
                   upperCase=!upperCase;
               }else{
                   sb.append(c);
               }
            }
            sb.append('\n');
        }
        System.out.print(sb);
    }
   
}

Friday, 2 May 2014

CodeEval - Minesweeper - Hard

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class Main {
    public static void main (String[] args) throws FileNotFoundException, IOException {

    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    StringBuffer sb=new StringBuffer();
    String line;
    while ((line = in.readLine()) != null) {
        StringTokenizer st=new StringTokenizer(line,";");
        StringTokenizer st2=new StringTokenizer(st.nextToken(),",");
        int n=Integer.parseInt(st2.nextToken());
        int m=Integer.parseInt(st2.nextToken());
        StringBuilder str=new StringBuilder(st.nextToken());
        int[][]arr=new int[n+2][m+2];
        char[][]val=new char[n+2][m+2];
        for(int i=1;i<n+1;i++){
            for(int j=1;j<m+1;j++){
                val[i][j]=str.charAt(0);
                if(val[i][j]=='*'){
                    inc(arr, i, j);
                }
                str.deleteCharAt(0);
            }
        }
        for(int i=1;i<n+1;i++){
            for(int j=1;j<m+1;j++){
                if(val[i][j]=='*'){
                    sb.append(val[i][j]);
                }else{
                   sb.append(arr[i][j]);
                }
            }
        }
        sb.append('\n');
    }
    System.out.print(sb);
  }
   
    static void inc(int[][]arr,int x,int y){
        for(int i=x-1;i<x+2;i++){
            arr[i][y-1]++;
            if(i!=x)
                arr[i][y]++;
            arr[i][y+1]++;
        }
    }
}

Tuesday, 1 April 2014

Facebook Hacker Cup 2013 - Qualification - Problem A - Beautiful Strings

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;


   
public class Main {
    public static void main (String[] args) throws IOException {
   
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    StringBuffer sb=new StringBuffer();
    String line;
    while ((line = in.readLine()) != null) {
       String val=line.toLowerCase();
       char[]hist=new char[26];
       for(int i=0;i<val.length();i++){
           if(val.charAt(i)>='a' && val.charAt(i)<='z'){
            hist[val.charAt(i)-'a']++;
           }
       }
       Arrays.sort(hist);
       int sum=0;
       for(int i=hist.length-1;i>-1;i--){
           sum+=hist[i]*(i+1);
       }
       sb.append(sum).append('\n');
    }
    System.out.print(sb);
  }
}