Friday, 26 December 2014

BASE - SPOJ SOLUTION :-

     This one may be the easy one after the life, universe, everything in the spoj. This one is my 101th classical problem in spoj. In the midst of too much of personal confusions on this night I did this one and got AC in one go which really suppressed all those feelings a bit. Yeah I'm addicted to that green (accepted) you know.

Problem Statement :

         Go go...it's there in spoj's problems archive. I just redirect you there.


Constraints :

      You just need to take care of reading the input. Yeah the numbers given may contain alphabets too...
         
         Don't make this simple problem too tough by assuming anything that is not given in the problem statement. Alphabets only contains Uppercase letters and not any lowercase or other symbols.

         This section doesn't deserve this name,yeah I know but take some moment to read this to prevent yourself from some WA's...

TAKE CARE OF THIS...!!!   
  • It will have a 7-digital display.
  • Its buttons will include the capital letters A through F in addition to the digits 0 through 9.
  • It will support bases 2 through 16.


Hint :

        All you need to do is to convert the given number to some intermediate base. Be wise,while choosing this one. Your intermediate base must act faster than the other one's. I have chosen 10 and yeah it did it's job quite well and passed my python code in 0.00s. Do the rest conversion with this base 10 number converted from the given base.

       Check if the result is greater than 7 in length. Since it is 7-digit display,How can we expect it to hold a number greater than length 7.So just print "Error" to the stdout in this case.

       Right justify your output to 7 places.Yeah really this rjust() function in python helped a lot with formatting.


Source Code : IDEONE


""" AUTHOR :  PVKCSE
    STUDENT AT  :  ACCET,KKDI
    TASK        :  BASE - SPOJ """

import sys

symbol_table=[i.upper() for i in "abcdefg"]

for line in sys.stdin :
    a = line.strip().split()
    num = a[0]
    base_from = int(a[1])
    base_to = int(a[2])
    num_10 = 0
    prog = 1
    inc = 0
    for i in reversed(num) :
        if ord(i) >=65 :
            i = (ord(i) - 65)+10
        num_10+=((int(i))*(base_from**inc))
        inc +=1
    result = ""
    if base_to != 10 :
        while num_10 > 0 :
            temp=(num_10%base_to)
            if temp >= 10 :
                result+=symbol_table[temp-10]
            else :
                result+=str(temp)
            num_10/=base_to
        ans=""
        for i in reversed(result) :
            ans+=i
        result = ans
    else :
        result+=str(num_10)
    if len(result) > 7 :
        result = "ERROR"
    print str(result).rjust(7)

NOTE : IF YOU HAVE ANY PROBLEM IN UNDERSTANDING THE CODE, JUST VISIT AND HAVE SOME TOUCH WITH THE COMMENT BOX.THANKS FOR READING...!!!

0 comments :

Post a Comment