Jump to content

Recommended Posts

Posted (edited)

Boas ando a aprender python, e encontrei um site com desafios. e estou com dificuldades neste:

When I start to feed my pigeon, a minute later two more fly by. And a minute later another 3. Then 4, and so on. One portion of food lasts a pigeon for a minute. In case there's not enough food for all the birds, the pigeons that came first, eat first. Pigeons are hungry animals and eat without stopping. If I have N portions of wheat, how many pigeons will be fed with at least one portion of wheat?

e deveria dar este resultado:

checkio(1) == 1

checkio(2) == 1

checkio(5) == 3

checkio(10) == 6

isto foi o código a que cheguei:

def checkio(number):
 minu=0
 birds=0
   while number>0:
	   minu=minu+1
	   birds=birds+minu
	   number=number-birds
 return birds

obrigado-

Edited by Feijó
Posted (edited)

Boas.

Um problema com a função é que está a retornar o número total de pombos e não quantos é que efectivamente comeram.

Edited by Olos
Posted

Boas.

Um problema com a função é que está a retornar o número total de pombos e não quantos é que efectivamente comeram.

Obrigado 🙂

sempre consegui fazer o exercício, se tiverem alguma crítica que me ajude a melhorar agradecia 🙂

foi a esta resposta que cheguei :

def checkio(number):
   minutos=0
   birdsA=0
   birdsF=0
   while number > 0:
    minutos=minutos+1
    birdsA=birdsA+minutos
    if birdsA<=number:
	    birdsF=birdsA
	    number=number-birdsF
    elif number>birdsF:
	    birdsF=number
    else:
	    break
   return birdsF

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.