Tutorial A7 – Solutions

1 Create an empty list and fill it iteratively with even numbers from 2 to 12 converted to floats.

[1]:
l = []
for i in range(2, 13, 2):
    l.append(float(i))
print(l)
[2.0, 4.0, 6.0, 8.0, 10.0, 12.0]

2 Consider again the discrete potential function from the lesson. Find out if the function has a potential energy minimum and print out the \(x\)-value of the minimum if you found one.

[2]:
x = list(range(-10, 11))
E = [x**2 - 2 for x in x]
print("x: ", x)
print("E: ", E)
x:  [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
E:  [98, 79, 62, 47, 34, 23, 14, 7, 2, -1, -2, -1, 2, 7, 14, 23, 34, 47, 62, 79, 98]
[3]:
slope_map = {True: "+", False: "-"}

current = 0  # Current index
slope = False  # Negative slope (we just assume this for simplicity)

# Go through the list of energies (starting at second element)
for n, next_ in enumerate(E[1:], 1):
    # What is the slope to the next point?
    if next_ < E[current]:
        next_slope = False  # Negative slope
    elif next_ > E[current]:
        next_slope = True  # Positive slope
    else:
        next_slope = slope  # no change in slope

    print(f"E({x[current]}): {E[current]}, E({x[n]}): {next_}, "
          f"Slope: {slope_map[slope]}/{slope_map[next_slope]}")

    if (next_slope is True) and (slope is False):
        print(f"Minimum around x = {x[current]}")
    elif (next_slope is False) and (slope is True):
        print(f"Maximum around x = {x[current]}")

    slope = next_slope
    current += 1
E(-10): 98, E(-9): 79, Slope: -/-
E(-9): 79, E(-8): 62, Slope: -/-
E(-8): 62, E(-7): 47, Slope: -/-
E(-7): 47, E(-6): 34, Slope: -/-
E(-6): 34, E(-5): 23, Slope: -/-
E(-5): 23, E(-4): 14, Slope: -/-
E(-4): 14, E(-3): 7, Slope: -/-
E(-3): 7, E(-2): 2, Slope: -/-
E(-2): 2, E(-1): -1, Slope: -/-
E(-1): -1, E(0): -2, Slope: -/-
E(0): -2, E(1): -1, Slope: -/+
Minimum around x = 0
E(1): -1, E(2): 2, Slope: +/+
E(2): 2, E(3): 7, Slope: +/+
E(3): 7, E(4): 14, Slope: +/+
E(4): 14, E(5): 23, Slope: +/+
E(5): 23, E(6): 34, Slope: +/+
E(6): 34, E(7): 47, Slope: +/+
E(7): 47, E(8): 62, Slope: +/+
E(8): 62, E(9): 79, Slope: +/+
E(9): 79, E(10): 98, Slope: +/+

3 Given an integer n compute the factorial of this number.

[4]:
n = 5

factorial = 1
while n > 1:
    factorial *= n
    n -= 1

print(factorial)
120

Advanced 1 Take the following dummy text and create a dictionary that has the occurring letters (characters) as keys and the corresponding letter counts as values (example below).

"""As any dedicated reader can clearly see, the Ideal of practical reason is a representation
of, as far as I know, the things in themselves; as I have shown elsewhere, the phenomena
should only be used as a canon for our understanding. The paralogisms of practical
reason are what first give rise to the architectonic of practical reason. As will easily be
shown in the next section, reason would thereby be made to contradict, in view of these
considerations, the Ideal of practical reason, yet the manifold depends on the phenomena.
Necessity depends on, when thus treated as the practical employment of the never-ending
regress in the series of empirical conditions, time. Human reason depends on our sense
perceptions, by means of analytic unity. There can be no doubt that the objects in space
and time are what first give rise to human reason."""
[5]:
# Example for count dict
counts = {
    "a": 12,
    "b": 1
}
[6]:
text = """As any dedicated reader can clearly see, the Ideal of practical reason is a representation
of, as far as I know, the things in themselves; as I have shown elsewhere, the phenomena
should only be used as a canon for our understanding. The paralogisms of practical
reason are what first give rise to the architectonic of practical reason. As will easily be
shown in the next section, reason would thereby be made to contradict, in view of these
considerations, the Ideal of practical reason, yet the manifold depends on the phenomena.
Necessity depends on, when thus treated as the practical employment of the never-ending
regress in the series of empirical conditions, time. Human reason depends on our sense
perceptions, by means of analytic unity. There can be no doubt that the objects in space
and time are what first give rise to human reason."""
[7]:
counts = {}
for letter in text:
    if letter in counts:
        counts[letter] += 1
        continue
    counts[letter] = 1

print(counts)
{'A': 2, 's': 54, ' ': 134, 'a': 61, 'n': 60, 'y': 11, 'd': 26, 'e': 100, 'i': 44, 'c': 29, 't': 55, 'r': 42, 'l': 22, ',': 11, 'h': 32, 'I': 4, 'o': 49, 'f': 14, 'p': 17, '\n': 9, 'k': 1, 'w': 10, 'g': 7, 'm': 14, 'v': 6, ';': 1, 'u': 11, 'b': 8, '.': 6, 'T': 2, 'x': 1, 'N': 1, '-': 1, 'H': 1, 'j': 1}