Wednesday, April 8, 2015

Google CodeJam in python templates

I recently started learning python and designed these programs.

Following program is for http://code.google.com/codejam/contest/351101/dashboard#s=p2


 # -*- coding: utf-8 -*-
"""
Created on Tue Apr  7 15:40:09 2015

@author: hpatil
"""

import sys

def read_file( input_file, output_file):
    sys.stdin = open(input_file)
    if(output_file != '' ):
        sys.stdout = open(output_file,"w+")
    read_input()

def solver(line):
    last_digit=10
    for i in line:
        #print(i)
        #print(len(line))
        digit, mystring = digit2string(i)
        if(last_digit==digit):
            print(' ',end='')
        print(mystring,end='')
        last_digit=digit
    print()
    return

def digit2string(char):
    dial = [
        [' '],
        [],
        ['a','b','c'],
        ['d','e','f'],
        ['g','h','i'],
        ['j','k','l'],
        ['m','n','o'],
        ['p','q','r','s'],
        ['t','u','v'],
        ['w','x','y','z'],
    ]
    mystring = ""  
    for digit in range(len(dial)):
        for repeat in range(len(dial[digit])):
            if(char==dial[digit][repeat]):
                for times in range(repeat+1):
                    mystring+=str(digit)
                return digit,mystring
  
  
def read_input():  
    with sys.stdin as file:
        testCases = int(file.readline())
        for test in range(testCases):
            print("Case #{}: ".format(test+1), end="")
            line = file.readline().strip('\n')
            solver(line)

#read_file('input','')
read_input()


This one is for with integer reading for http://code.google.com/codejam/contest/351101/dashboard


# -*- coding: utf-8 -*-
__author__ = 'hpatil'

import sys

def show_graph(x,y):
    import matplotlib.pyplot as plt

    plt.plot (x, y)
    plt.grid(True)
    plt.show()

def solver(credit, nItems, items):
    for i in range(nItems):
        for j in range(i+1,nItems):
            sum = items[i] + items[j]
            if( sum == credit):
                print(i+1,j+1)
                return

def set_io( input_file, output_file):
    sys.stdin = open(input_file)
    sys.stdout = open(output_file,"w")
   
def read_input():   
    with sys.stdin as file:
        testCases = int(file.readline())
        for test in range(testCases):
            print("Case #{}: ".format(test+1), end="")
            credit = int(file.readline())
            nItems = int(file.readline())
            items = list(map(int,file.readline().split()))
            show_graph(range(len(items)), items)
            solver(credit, nItems, items)

set_io('input','out')
read_input()

Following program is for http://code.google.com/codejam/contest/3214486/dashboard which took me a loooooong time

# -*- coding: utf-8 -*-
"""
Created on Wed Apr  8 13:08:15 2015

@author: hpatil
Solution to problem : http://code.google.com/codejam/contest/3214486/dashboard

"""

lit = [
        #['abcdefg']
        '1111110',
        '0110000',
        '1101101',
        '1111001',
        '0110011',
        '1011011',
        '1011111',
        '1110000',
        '1111111',
        '1111011'
    ]


def display_calci(string):
    if(string=="ERROR!"):
        print(string)
    else:
        display = list("         ")
        print(''.join(display))
        # line 1
        if(string[5] =='1'):
            display[0]='|'
        if(string[0] =='1'):
            display[1]='^'
        if(string[6] =='1'):
            display[2]='_'
        if(string[1] =='1'):
            display[3]='|'
        display[4]='\n'
        if(string[4] =='1'):
            display[5]='|'
        if(string[3] =='1'):
            display[6]='_'
            display[7]='_'
        if(string[2] =='1'):
            display[8]='|'
        print(''.join(display))

def isDigitFeasible(active, disp_digit, digit):
    for led in range(7):
        if(active[led]=='1'):
            if( disp_digit[led] != lit[digit][led]):
                return 0
    return 1

def get_active_leds(active, disp_digits):
    active= list(active)
    count = int(disp_digits[0])
    for digit in range(1,count+1):
        for led in range(len(disp_digits[digit])):
            if(disp_digits[digit][led]=='1' ):
                active[led]='1'
    return ''.join(active)

def led_unused(active, disp_digits, possible_digit):
    not_used = list('1111111')
    active= list(active)
    count = int(disp_digits[0])
    for digit in range(1,count+1):
        possible_digit= (possible_digit+1)%10
        for led in range(len(lit[possible_digit])):
            if(lit[possible_digit][led]=='1' ):
                not_used[led]='0'
    return ''.join(not_used)

def print_digit(active, disp_digits, digit):
    result_string = ""
    unused = led_unused(active, disp_digits, digit)
    for led in range(len(active)):
        if(active[led]=='1'):
            result_string += lit[digit][led]
        else:
            if(lit[digit][led] == '1'):
                if(unused[led]=='1'):
                    return "ERROR!"
                else:
                    result_string += '0'
            else:
                result_string += '0'

    return result_string

def get_possible_digits(active, disp_digit, possible):
    possible_return = [ ]
    for digit in possible:
        if(isDigitFeasible(active, disp_digit, digit)):
            possible_return.append(digit)
    return possible_return

def solver(disp_digits):
    active= list('0000000')
    active = get_active_leds(active,disp_digits)
    if(debug):
        display_calci(active)
    count = int(disp_digits[0])
    possible = [9,8,7,6,5,4,3,2,1,0]
    for digit in range(1,count+1):
        possible = get_possible_digits(active,disp_digits[digit], possible)
        possible = list(map(lambda x: (x-1) % 10, possible))
    if(debug):
        print(possible)
    possible_soln = list(map(lambda x: print_digit(active,disp_digits,x), possible))
    if(debug):
        print(possible_soln)
    soln_set = set(possible_soln)
    if(len(soln_set) != 1):
        return "ERROR!"
    else:
        return list(soln_set)[0]

def read_input():
    with sys.stdin as file:
        nTests = int(file.readline())
        for tests in range (1, nTests+1):
            print("Case #" + str(tests)+": ", end="")
            disp_digits = file.readline().split('\n')[0].split(' ')
            if(debug):
                print(disp_digits[1:])
                list(map(lambda x: display_calci(x), disp_digits[1:]))
                print("-----------")
            answer_string = solver(disp_digits)
            if(debug):
                display_calci(answer_string)
            print(answer_string)

import sys
debug = int(sys.argv[1])
sys.stdin = open (sys.argv[2])
#sys.stdin = open ('input2')
#sys.stdin = open ('A-small-practice.in')
#sys.stdin = open ('A-large-practice.in')

read_input()