Pages

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()

Monday, 5 October 2015

CodeEval - Magic Numbers - Moderate

import sys

def is_magic(x):
    arr_char = list(str(x))
    arr_sorted = ''.join(sorted(arr_char))
    n = len (arr_char)
    for i in range(n-1):
        if arr_sorted[i] == arr_sorted[i+1]:
            return False
    arr_checked = [False]*n
    pos = 0
    while True:
        if  arr_checked[pos]:
            break
        arr_checked[pos] = True
        index = int(arr_char[pos])
        pos = (pos + index) % n
    if not pos == 0:
        return False
    for checked in arr_checked:
        if not checked:
            return False
    return True

test_cases = open(sys.argv[1], 'r')
arr = []
for i in range(1,10001):
    if is_magic(i):
        arr.append(i)
for test in test_cases:
    bound = test.split()
    a = int (bound[0])
    b = int (bound[1])
    sol = ""
    for x in arr:
        if a > x :
            continue
        if b < x :
            break
        if not sol == "" :
            sol += " "
        sol += str(x)
    if sol ==  "":
        print -1
    else :
        print sol
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));
        }
    }
}

Thursday, 17 September 2015

CodeEval - Filename Pattern - Moderate

import sys
import re

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    arr=test.split()
    regex=""
    for c in arr[0]:
        if c == '.':
            regex += '\.'
        elif c == '?':
            regex +='.'
        elif c == '*':
            regex +='.*'
        else:
            regex +=c
    sol=[]
    for x in range(1,len(arr)):
        if re.match('^'+regex+'$', arr[x]):
            sol.append(arr[x])
    value=""
    for i in range(len(sol)):
        if i>0 :
            value += " "
        value += sol[i]
    if not value:
        value = "-"
    print value

test_cases.close()

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()