Monday, April 7, 2014

pythonchallenge.com Challenege 2

IDEA:
Need to parse the webpage pythonchallenge.com/pc/def/ocr.html and extract the commented scrap text having loads of chars

We are supposed to find the unique characters from that text.



CODE
--------------------------------

import urllib2

r = urllib2.urlopen('http://www.pythonchallenge.com/pc/def/ocr.html')
h = r.read()

import re

o = re.match(r'.*<!--.*-->.*<!--(.*)-->.*', h, re.S)

print

d = ''
if o:
        print o.group(1)
        d = o.group(1)

h1 = {}

for e in d:
        if e in h1:
                h1[e] = h1[e] + 1
        else:
                h1[e] = 1

op = ''

for k in h1.keys():
        if h1[k] == 1:
                op = op + k

print op
---------------------------

equality should be the answer

http://www.pythonchallenge.com/pc/def/equality.html



Project Euler Problem 9 Solution

Question:

To find the pythagoras triplet a, b, c such that a^2 + b^2 = c^2 and a + b + c = 1000


Idea:

as, a + b + c = 1000 and a ^2 + b^2 = c^2, hence a < b < c

s = a + b + c

hence,

a < s/3  because a < b < c
(in worst case, a = b = c = s/3 )

b < s/2 because b > a i.e. b > s/3 and b < c
(in worst case a =1, b ~ c hence, b = c = s/2)


CODE:

-----------------------------------
#!/usr/bin/python

import math


a = b = c = 0
s = 1000
f = False

a = 1
while ( a < s/3 ):
        b = a
        while ( b < (s / 2)):
                c = s - a - b
                if ( a * a + b * b == c * c):
                        f = True
                        break
                b = b + 1

        if f:
                break

        a = a + 1


print a, b, c , (a * b * c)

-----------------------------------------------------



Project Euler Problem 8 Solution

Find the largest product of 5 contiguous digits?

CODE:
-------------------------------------------------- 

n = 493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

print n

print len(str(n))


l =  str(n)

print l[990:995]

i = 0
m = 1

while ( i <= 995):
        l1 = l[i:i+5]
        print l1
        m1 = 1
        for e in l1:
                m1 =  m1 * int(e)

        if ( m1 > m ):

             m = m1
        

        i = i + 1


print "great prod is ", m

---------------------------------------------------------------------------

Answer = 40824
TAKE AWAY:
List SLICING

Project Euler Problem 7 Solution

Solution :
10001th prime = 104743
 
CODE:
-----------------------------------------------
#!/usr/bin/python

#sum of squares of 1st 100 numbers
import math

def isPrime(n):

        i = 2
        #while ( i <= n/2):
        while ( i <= math.sqrt(n)):
                if ( n % i == 0):
                        return False
                else:
                        i = i + 1

        return True


i = 2

n1 = 10001
c = 0

while (True):
        if isPrime(i):
                c = c + 1
                if (c == n1):
                        break
        i = i + 1


print "prime no ", n1, " = ", i
--------------------------------------------------------


Sunday, April 6, 2014

pythonchallenge.com problem 2 solution

We had to translate the relevant characters:
k ->m
o -> q
e -> g


---------------------------------------
#!/usr/bin/python
import string

str1 = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj"


str2 = string.maketrans(string.ascii_lowercase, string.ascii_lowercase[2:] + string.ascii_lowercase[:2])


print str1.translate(str2)





-------------------------------------

Answer = map => ocr

challenge 3:
http://www.pythonchallenge.com/pc/def/ocr.html


 Learning:
string module: import string

string.ascii_lowercase => 'a-z' characters string

mapping = string.maketrans(str1, str2) => creates the mapping for str1 chars to str2 chars

s1.translate(mapping) => returns translated string