# Exercícios by Nick Parlante (CodingBat)
# Papagaio
# temos um papagaio que fala alto
# hora é um parâmetro entre 0 e 23
# temos problemas se o papagaio estiver falando
# antes da 7 ou depois das 20
from Testes import teste, msg_sucesso, msg_inicio


def papagaio(falando, hora):
    return falando and (hora < 7 or hora > 20)


msg_inicio('Papagaio')
teste(papagaio(True, 6), True)
teste(papagaio(True, 7), False)
teste(papagaio(False, 6), False)
teste(papagaio(True, 21), True)
teste(papagaio(False, 21), False)
teste(papagaio(True, 23), True)
teste(papagaio(True, 20), False)
msg_sucesso()
# Exercícios by Nick Parlante (CodingBat)
# Dormir
# dia_semana é True para dias na semana
# feriado é True nos feriados
# você pode ficar dormindo quando é feriado ou não é dia semana
# retorne True ou False conforme você vá dormir ou não
from Testes import teste, msg_sucesso, msg_inicio


def dormir(dia_semana, feriado):
    pass


msg_inicio('Dormir')

teste(dormir(False, False), True)
teste(dormir(True, False), False)
teste(dormir(False, True), True)
teste(dormir(True, True), True)
msg_sucesso()

# Exercícios by Nick Parlante (CodingBat)
# Dez
# dados dois inteiros a e b
# retorna True se um dos dois é 10 ou a soma é 10
from Testes import teste, msg_sucesso, msg_inicio


def dez(a, b):
    pass


msg_inicio('Dez')
teste(dez(9, 10), True)
teste(dez(9, 9), False)
teste(dez(1, 9), True)
teste(dez(10, 1), True)
teste(dez(10, 10), True)
teste(dez(8, 2), True)
teste(dez(8, 3), False)
teste(dez(10, 42), True)
teste(dez(12, -2), True)
msg_sucesso()
# retorna True se a diferença absoluta entre n e 100 ou n e 200
# for menor ou igual a 10
# dista10(93) -> True
# dista10(90) -> True
# dista10(89) -> False
from Testes import teste, msg_sucesso, msg_inicio


def dista10(n):
    diff_100 = n - 100 if n > 100 else 100 - n
    diff_200 = n - 200 if n > 200 else 200 - n
    return diff_100 <= 10 or diff_200 <= 10


msg_inicio('Dista 10')
teste(dista10(93), True)
teste(dista10(90), True)
teste(dista10(89), False)
teste(dista10(110), True)
teste(dista10(111), False)
teste(dista10(121), False)
teste(dista10(0), False)
teste(dista10(5), False)
teste(dista10(191), True)
teste(dista10(189), False)
teste(dista10(190), True)
teste(dista10(200), True)
teste(dista10(210), True)
teste(dista10(211), False)
teste(dista10(290), False)
msg_sucesso()
# Exercícios by Nick Parlante (CodingBat)
# Troca Letras
# seja uma string s
# se s tiver tamanho <= 1 retorna ela mesma
# caso contrário troca a primeira e última letra
# troca('code') -> 'eodc'
# troca('a') -> 'a'
# troca('ab') -> 'ba'
from Testes import teste, msg_sucesso, msg_inicio


def troca(s):
    if len(s) <= 1:
        return s
    else:
        return s[-1] + s[1:-1] + s[0]


msg_inicio('Troca Letras')
teste(troca('code'), 'eodc')
teste(troca('a'), 'a')
teste(troca('ab'), 'ba')
teste(troca('abc'), 'cba')
teste(troca(''), '')
teste(troca('Chocolate'), 'ehocolatC')
teste(troca('nythoP'), 'Python')
teste(troca('hello'), 'oellh')
msg_sucesso()
# Exercícios by Nick Parlante (CodingBat)
# Alunos Problema
# temos dois alunos a e b
# a_sorri e b_sorri indicam se a e b sorriem
# temos problemas quando ambos estão sorrindo ou ambos não estão sorrindo
# retorne True quando houver problemas
from Testes import teste, msg_sucesso, msg_inicio


def alunos_problema(a_sorri, b_sorri):
    pass


msg_inicio('Alunos Problema')
teste(alunos_problema(True, True), True)
teste(alunos_problema(False, False), True)
teste(alunos_problema(True, False), False)
teste(alunos_problema(False, True), False)
msg_sucesso()
# Fizz-buzz
# Esta função responsável por converter múltiplos
# de 3 em 'fizz', múltiplos de 5 em 'buzz' e
# múltiplos de ambos em 'fizz-buzz'.
# Números não divisíveis, são retornados normalmente.
from Testes import teste, msg_sucesso, msg_inicio


def fizzbuzz(n):
    if n % 3 == 0 and n % 5 == 0:
        return 'fizz-buzz'
    elif n % 3 == 0:
        return 'fizz'
    elif n % 5 == 0:
        return 'buzz'
    else:
        return n


msg_inicio('FizzBuzz')
teste(fizzbuzz(1), 1)
teste(fizzbuzz(2), 2)
teste(fizzbuzz(3), 'fizz')
teste(fizzbuzz(4), 4)
teste(fizzbuzz(5), 'buzz')
teste(fizzbuzz(15), 'fizz-buzz')
teste(fizzbuzz(30), 'fizz-buzz')
teste(fizzbuzz(148), 148)
msg_sucesso()
예제 #8
0
# Exercícios by Nick Parlante (CodingBat)
# Apaga
# seja uma string s e um inteiro n
# retorna uma nova string sem a posição n
# apaga('kitten', 1) -> 'ktten'
# apaga('kitten', 4) -> 'kittn'
from Testes import teste, msg_sucesso, msg_inicio


def apaga(s, n):
    pass


msg_inicio('Apaga')
teste(apaga('kitten', 1), 'ktten')
teste(apaga('kitten', 0), 'itten')
teste(apaga('kitten', 4), 'kittn')
teste(apaga('Hi', 0), 'i')
teste(apaga('Hi', 1), 'H')
teste(apaga('code', 0), 'ode')
teste(apaga('code', 1), 'cde')
teste(apaga('code', 2), 'coe')
teste(apaga('code', 3), 'cod')
teste(apaga('chocolate', 8), 'chocolat')

teste(apaga('chocolate', 8), 'chocola')  # Erro proposital

msg_sucesso()
# Exercícios by Nick Parlante (CodingBat)
# Diff 21
# dado um inteiro n retorna a diferença absoluta entre n e 21
# porém se o número for maior que 21 retorna dobro da diferença absoluta
# diff21(19) -> 2
# diff21(25) -> 8
# dica: abs(x) retorna o valor absoluto de x
from Testes import teste, msg_sucesso, msg_inicio


def diff21(n):
    pass


msg_inicio('Diff 21')
teste(diff21(19), 2)
teste(diff21(10), 11)
teste(diff21(21), 0)
teste(diff21(22), 2)
teste(diff21(25), 8)
teste(diff21(30), 18)
msg_sucesso()
예제 #10
0
# Exercícios by Nick Parlante (CodingBat)
# Soma Dobro
# dados dois números inteiros retorna sua soma
# porém se os números forem iguais retorna o dobro da soma
# soma_dobro(1, 2) -> 3
# soma_dobro(2, 2) -> 8
from Testes import teste, msg_sucesso, msg_inicio


def soma_dobro(a, b):
    a = a
    if a != b:
        return a + b
    else:
        return (a + b) * 2


msg_inicio('Soma Dobro')
teste(soma_dobro(1, 2), 3)
teste(soma_dobro(3, 2), 5)
teste(soma_dobro(2, 2), 8)
teste(soma_dobro(-1, 0), -1)
teste(soma_dobro(0, 0), 0)
teste(soma_dobro(0, 1), 1)
msg_sucesso()