Desafio da semana #2

40 views
Skip to first unread message

Pinguim Azul

unread,
Aug 12, 2012, 11:38:06 AM8/12/12
to pyth...@googlegroups.com
def prime(n):
if n < 2:
return False
limit = int((n + 1) ** .5)
return all(n % i > 0 for i in xrange(2, limit + 1))

def conv(x):
return ord(x) - ord('a') + 1 if x >= 'a' else ord(x) - ord('A') + 27

def eh_prima(word):
if not word:
return False
if not word.isalpha():
raise Exception
return prime(sum(conv(i) for i in word))

--
Ricardo Bittencourt
http://www.ricbit.com

Henrique Bastos

unread,
Aug 12, 2012, 12:47:09 PM8/12/12
to pyth...@googlegroups.com
Galera, onde estão os enunciados dos problemas?

[]'s,
--
Henrique Bastos
+55 21 9618-6180
http://henriquebastos.net
Twitter: henriquebastos
Gtalk: henr...@bastos.net
Skype: henriquebastos.net
> --
>
>
>

Genilson Israel

unread,
Aug 12, 2012, 5:59:00 PM8/12/12
to pyth...@googlegroups.com
def num_primo(numero):
    raiz = numero ** 0.5    
    if int(raiz) == raiz:
        return False
    for x in xrange(2, int(raiz) + 1):
        if numero % x == 0:
            return False
    return True

def eh_prima(palavra):
    if not palavra:
        return False
    try:
        if not palavra.isalpha():
            raise NameError('ForaIntervalo')
    except NameError:
        print 'Exception #Presença de caracter fora do intervalo'
        return
    soma = 0
    for letra in palavra:
        if letra.isupper():
            soma += (ord(letra) - 38)
        else:
            soma += (ord(letra) - 96)        
    return num_primo(soma)
Message has been deleted

Arthur Assuncao

unread,
Aug 12, 2012, 10:23:23 PM8/12/12
to pyth...@googlegroups.com
def isNumeroPrimo(numero, j=1, i=0):
if (j > numero / 2) or (i > 1):
if i == 1:
return True
else:
return False
else:
if (numero % j) == 0:
i = j
return isNumeroPrimo(numero, j+1, i)

def eh_prima(palavra):
soma = 0
for letra in palavra:
if letra.islower():
soma += ord(letra) - 96
elif letra.isupper():
soma += ord(letra) - 64 + 26
else:
raise Exception('Presença de caracter fora do intervalo')
#print 'Exception'
return False
return isNumeroPrimo(soma)
Message has been deleted
Message has been deleted

Fabrício Kelmer

unread,
Aug 21, 2012, 1:45:31 PM8/21/12
to pyth...@googlegroups.com
Segue minha solução para o desafio devidamente corrigida.

def eh_primo(x):
    y = d = 0
    for i in xrange(len(x)):
        if (ord(x[i]) > 64 and ord(x[i]) < 91) or (ord(x[i]) > 96 and ord(x[i]) < 123):
            if x[i].islower():
                y += (ord(x[i]) - 96)
            else:
                y += (ord(x[i]) - 38)
        else:
            d = 1
            raise Exception('Presença de caracter fora do intervalo')
            return False
    for j in xrange(1, y + 1):
        if y % j == 0:
            d += 1
    if d == 2:

Genilson Israel

unread,
Aug 21, 2012, 6:51:04 PM8/21/12
to pyth...@googlegroups.com
Correção:
- Retirada do tratamento de exceção

def num_primo(numero):
    raiz = numero ** 0.5    
    if int(raiz) == raiz:
        return False
    for x in xrange(2, int(raiz) + 1):
        if numero % x == 0:
            return False
    return True

def eh_prima(palavra):
    if not palavra:
        return False
    if not palavra.isalpha():
        raise Exception
    soma = 0
    for letra in palavra:
        if letra.isupper():
            soma += (ord(letra) - 38)
        else:
            soma += (ord(letra) - 96)        
    return num_primo(soma)

Em domingo, 12 de agosto de 2012 12h38min06s UTC-3, Ricardo Bittencourt escreveu:

André Costa

unread,
Aug 22, 2012, 2:29:48 PM8/22/12
to pyth...@googlegroups.com
CORREÇÃO

def build_structure():
    import string
    alphabet = string.letters
    d = {}
    for i in xrange(0,len(alphabet)):
        d[alphabet[i]] = i+1
    return d
def is_prime(n):
    if n == 2: return True
    if n % 2 == 0 or n == 1: return False
    i = 3
    while i < n:
        if n % i == 0: return False
        i += 2
    return True
def eh_prima(word):
    if not word: return False
    d = build_structure()
    try:
        count = 0
        for letter in word: count += d[letter]
        return is_prime(count)
    except KeyError: raise Exception

Em domingo, 12 de agosto de 2012 21h15min58s UTC-3, André Costa escreveu:
def build_structure():
    import string
    alphabet = string.letters
    d = {}
    for i in xrange(0,len(alphabet)):
        d[alphabet[i]] = i+1
    return d
def is_prime(n):
    if n == 2: return True
    if n % 2 == 0 or n == 1: return False
    i = 3
    while i < n:
        if n % i == 0: return False
        i += 2
    return True
def eh_prima(word):
    if not word: return False
    d = build_structure()
    try:
        count = 0
        for letter in word: count += d[letter]
        return is_prime(count)
    except KeyError: return 'Exception'
Reply all
Reply to author
Forward
0 new messages