#! /usr/bin/env python
# -*- coding: utf-8 -*- 

#===================================================================
# Ejercicio en: http://www.pythonchallenge.com/pc/def/linkedlist.php
# Solución: peak.html
# Obtenemos: http://www.pythonchallenge.com/pc/def/peak.html
#-------------------------------------------------------------------
# FJA - neocipres@telefonica.net            Junio de 2007
#===================================================================

import  sys, re 
import urllib

nothing =  '12345' # sale antes con '19337'
resultadoA = []

def main( ):
    resultado = []
    try:
        sock = urllib.urlopen( url )
    except IOError, (errno, strerror):          
        print '\n Error de E/S (%s): %s' % (errno, strerror), "\n" 
    except:
        print "Error no comtemplado: ", sys.exc_info() [0], '\n'  
    else:
        texto = sock.read()
        sock.close()
        print texto 
        resultado = re.findall('[0-9]{1,5}$', texto )
        #print resultado
        if not resultado:
            resultado.append( int(resultadoA[0]) /2 )    
            
            
        return resultado[0] # con [] devuelve el contenido (str) no la lista.
        
for i in range( 400 ):
    url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='+nothing
    print i, " - ",
    nothing = str (main( ))
    del resultadoA
    resultadoA = []
    resultadoA.append( nothing )
    
