# Introduction


Fish are an integral part of any ecosystem. Unfortunately, fish are often seen as high maintenance. Contrary to popular belief, fish actually reduce pond maintenance as they graze on string algae and bottom feed from the pond floor. They also make very enjoyable pets, providing hours of natural entertainment.



# Task


In this Kata you are fish in a pond that needs to survive by eating other fish. You can only eat fish that are the same size or smaller than yourself. 
You must create a function called fish that takes a shoal of fish as an input string. From this you must work out how many fish you can eat and ultimately the size you will grow to.


# Rules

1.  Your size starts at 1

2.  The shoal string will contain fish integers between 0-9

3.  0 = algae and wont help you feed.

4.  The fish integer represents the size of the fish (1-9).

5.  You can only eat fish the same size or less than yourself.

6.  You can eat the fish in any order you choose to maximize your size.

7   You can and only eat each fish once.

8.  The bigger fish you eat, the faster you grow. A size 2 fish equals two size 1 fish, size 3 fish equals three size 1 fish, and so on.

9.  Your size increments by one each time you reach the amounts below.


# Increase your size
Your size will increase depending how many fish you eat and on the size of the fish.
This chart shows the amount of size 1 fish you have to eat in order to increase your size.




Current size
Amount extra needed for next size
Total size 1 fish
Increase to size


1
4
4
2


2
8
12
3


3
12
24
4


4
16
40
5


5
20
60
6


6
24
84
7


 



Please note: The chart represents fish of size 1

# Returns

Return an integer of the maximum size you could be.


# Example 1
You eat the 4 fish of size 1 (4 * 1 = 4) which increases your size to 2
Now that you are size 2 you can eat the fish that are sized 1 or 2.
You then eat the 4 fish of size 2 (4 * 2 = 8) which increases your size to 3



```python
fish("11112222") => 3
```

# Example 2

You eat the 4 fish of size 1 (4 * 1 = 4) which increases your size to 2
You then eat the remainding 8 fish of size 1 (8 * 1 = 8) which increases your size to 3

```python
fish("111111111111") => 3
```

Good luck and enjoy!

# Kata Series
If you enjoyed this, then please try one of my other Katas. Any feedback, translations and grading of beta Katas are greatly appreciated. Thank you.

 Maze Runner
 Scooby Doo Puzzle
 Driving License
 Connect 4
 Vending Machine
 Snakes and Ladders
 Mastermind
 Guess Who?
 Am I safe to drive?
 Mexican Wave
 Pigs in a Pen
 Hungry Hippos
 Plenty of Fish in the Pond
 Fruit Machine
 Car Park Escape

Difficulty: introductory

Starter code:

def fish(shoal):
	

Solutions:
def fish(shoal):
    eaten, size, target = 0, 1, 4
    for f in sorted(map(int, shoal)):
        if f > size: break
        eaten += f
        if eaten >= target:
            size += 1
            target += 4 * size
    return size

from collections import Counter

def fish(shoal):
    size, eaten = 1, 0
    for fishesSize,n in sorted( Counter(map(int, shoal)).items() ):
        eaten += fishesSize * n if fishesSize <= size else 0
        size = 1 + int( ((1+2*eaten)**.5 - 1)/2 )
    return size

def fish(shoal, size = 1, protein_earned = 0):
    fishes = [int(fish) for fish in shoal if fish != '0']
    grow = True
    while grow:
        edible_fishes = list(filter(lambda x: x <= size, fishes))
        ate_protein = sum(edible_fishes)
        protein_earned += ate_protein
        if protein_earned >= protein_to_grow(size):
            size += 1
            fishes = [fish for fish in fishes if fish not in edible_fishes]
        else:
            grow = False
    return size

def protein_to_grow(actual_size):
    protein_to_accumulate = 0
    while actual_size > 0:
        protein_to_accumulate += actual_size * 4
        actual_size -= 1
    return protein_to_accumulate

def fish(shoal):
    size = 1
    while(count_lesser_fish(shoal, size) >= 2*size*(size + 1)):
        size += 1
    return size
    
def count_lesser_fish(shoal, size):
    return sum(int(f) for f in shoal if int(f) <= size)

def fish(shoal):
    fishes = sorted(shoal)
    my_size = 1
    amount_for_next_size = 4
    
    for i in range(len(fishes)):
        if int(fishes[i]) > my_size: # break condition
            break
        amount_for_next_size -= int(fishes[i])
        if amount_for_next_size <= 0:
            my_size += 1
            amount_for_next_size += my_size * 4        
    return my_size        



def fish(shoal):
    size, eaten = 1, 0
    for i in range(1, 10):
        if size < i:
            break
        eaten += i * shoal.count(str(i))
        size = max(size, int(((2 * eaten + 1) ** 0.5 - 1) / 2) + 1)
    return size


#All data associated to the fish 
class Fish:
    size = 1        #Determines what the fish can eat
    intake = 0      #determines when fish grows in size
#---end class Fish


def attemptDigestion(myFish, menuItem):
    if menuItem <= myFish.size:    #Scenario: Eat the current menue item
        myFish.intake += menuItem
#---end function attemptDigestion

#Based on rules of the game, we grow (in size by 1) when what we have intaked is larger than or equal to the 
# size of our fish.  The cost to increase in size is equal to four times the current size of our fish
# e.g. intake ==14, size is 3.  The req to size 4 is 12 units, so our size increases by 1 and our
# intake is reduced as: 14 - 4*3 = 2 (intake is now 2; we burnt 12 units to inc in size :)
def assessGrowth(myFish):
    if myFish.intake >= 4*myFish.size:  #Situation: the fish is ready to grow in size 1
        myFish.intake =  myFish.intake - 4*myFish.size  #deplete the amount it cost us to inc. in size 
        myFish.size+=1
#---end fuction assessGrowth

def fish(str):
    #Convert the string of fish-sizes to a list
    foodPool = [] 
    foodPool[:0] = str
    foodPool.sort()     #ascending order

    myFish = Fish()

    #Begin the eating and evolving game
    while foodPool and int(foodPool[0]) <= myFish.size:
        menuItem = int(foodPool.pop(0))
        attemptDigestion(myFish, menuItem)  
        #determine if we need to level up based on mySize and the intake
        assessGrowth(myFish)

    #----end while

    return myFish.size

#---end function fishGame


from collections import Counter

def fish(shoal):
    count = Counter(map(int, shoal))
    
    size, eaten, step = 1, 0, 4
    for n in range(1, 10):
        if size >= n:
            eaten += count[n] * n
            while eaten >= step:
                eaten -= step
                size += 1
                step += 4
        else:
            break
    
    return size

from collections import Counter

def fish(shoal):
    c = Counter(map(int, shoal.replace('0', '')))
    consumed = 0
    for size in range(1, 99999):
        consumed += size * c[size]
        if consumed < size * (size+1) * 2:
            return size

def fish(shoal):
    shoal_l = []
    for s in shoal:
      shoal_l.append(int(s))
      shoal_l.sort()
      
    max_size = 1
    food_count = 0
    amount_needed = 4
    
    for fishy in shoal_l:
      if max_size >= fishy:
        food_count += int(fishy)     
      if food_count >= amount_needed:
        max_size += 1 
        amount_needed += (max_size*4)
        
    return max_size