# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Pre-Exercises

# importing matplotlib.pyplot allows us to generate graphs
import matplotlib.pyplot as plt

# Create a program that simulates a population of rabbits over a total of 100 time steps. At the beginning there are 1000 rabbits and in each time step 10% are added.

# The growth should stop when there are more than 5000 rabbits.

# If there are more than 5000 rabbits, every day another fox should enter their habitat. For the foxes we won't need a list, we just remember their amount.

# If there are 10 foxes, those 10 leave the habitat. In this case also 3000 rabbits disappear.

# +
TIMESTEPS = 100
rabbit_list = list()
rabbits_now = 1000
rabbit_growth = 0.1 # 10 %
foxes_now = 0

for it in range(TIMESTEPS):
    if rabbits_now > 5000:
        foxes_now += 1
    else:
        rabbits_now = rabbits_now + rabbits_now * rabbit_growth
        
    if foxes_now >= 10:
        foxes_now = 0
        rabbits_now -= 3000
    
    rabbit_list.append(rabbits_now)
    
plt.plot(rabbit_list)
plt.title("Rabbit population chart")
plt.xlabel("Time")
plt.ylabel("Rabbits")

# -

# # Exercise

# Create a program that simulates a deer population over a total of 100 time steps. At the beginning there are 1000 deer and normally the deer population increases by 10% per time step. However, when there are more than 5000 deer, the deer are hunted. This hunt lasts 10 time steps and in each time step, instead of population growth, there is a reduction of the population by 15%. In the plot you should not see the number of deer, but the number of deer / 1000.

# +
TIMESTEPS = 100
DEER_GROWTH = 0.1 # 10 %
HUNT_LIMIT = 5000
HUNT_EFFECT = 0.15 # 15 %
deer_pop = 1000

hunt = 0

deer_list = list()
for it in range(TIMESTEPS):
    if deer_pop > HUNT_LIMIT:
        hunt = 10
        
    if hunt > 0:
        deer_pop -= deer_pop * HUNT_EFFECT
        hunt -= 1
    else:
        deer_pop += deer_pop * DEER_GROWTH
        
    deer_list.append(deer_pop)

result = list()
for value in deer_list:
    result.append(value/1000)

plt.plot(result)
plt.title("Deer population chart")
plt.xlabel("Time")
plt.ylabel("Number of deer")

# +
TIMESTEPS = 100
DEER_GROWTH = 0.1 # 10 %
HUNT_LIMIT = 5000
HUNT_EFFECT = 0.15 # 15 %
deer_pop = 1000

# We can use a variable `hunt` to make sure we follow through
# the required 10 steps of hunting:
hunt = 0

for it in range(TIMESTEPS):
    if deer_pop > HUNT_LIMIT:
        hunt = 10

    if hunt > 0:
        print(hunt) # we print out the value of the hunt variable
        deer_pop -= deer_pop * HUNT_EFFECT
        hunt -= 1
    else:
        deer_pop += deer_pop * DEER_GROWTH
# -

# # Helpful stuff
#
# Recap of the **ternary operator**.
#
# The *arity* of an operator tells us on how many operands it operates.
#
# The `+` operator takes **two** numbers and adds them up. It is a **binary** operator: `a + b`

3+5

# We use the hyphen (`-`) both as **unary** and **binary** operator.
# As unary operator it negates a number: `- a`
# But as binary operator it can perform substraction: `a - b`

2-3

# What *arity* do the following operators have? (*Tipp:* Try them out in Python!)
#
# + `*` (multiplication)
# + `/` (division)
# + `not` (operates on True/False values)

not (3 < 5)

# The **ternary** operator is called **the** *ternary operator* because it is the only one in Python that has three operands.
#
# ~~~python
# a if b else c
# ~~~
#
# It decides between `a` and `c` by checking whether `b` is `True`.

# +
a = 1
b = False
c = 2

(a if b else c)
# -

# The same can be accomplished with some effort using an `if`-expression:
if b:
    r = a
else:
    r = c
r

# The ternary operator is especially useful if we have a variable
# for which the value depends one of two cases
gr = HUNT_EFFECT if hunt > 0 else DEER_GROWTH
gr # apparently currently we are hunting

StarcoderdataJupyterScriptsDedupFiltered