Skip to content
Snippets Groups Projects

Übung2

Open omah03 requested to merge omah03/alp4-repo-template:Übung2 into main
1 file
+ 111
0
Compare changes
  • Side-by-side
  • Inline
oop-Ubungen.py 0 → 100644
+ 111
0
#Bsp. für ein Algorithmus, welches die Endlichkeit eines Algorithmus widerlegt:
def infinite_loop():
while True:
print("Programmzusammenstoß, This Loop never endes! ")
'''
This function creates an infinite loop using a while statement with the condition True, and prints a message to the console each time the loop executes.
The loop will continue running indefinitely, since the condition True can never be false.
While this algorithm is not useful in most situations, it can be used as a placeholder or a stand-in for more complex code that hasn't been written yet.
For example, you might use an infinite loop like this in a program that you're developing iteratively, where you want to test how the program behaves in the absence of some functionality that you haven't implemented yet.
However, it's important to be careful when using infinite loops like this, since they can easily cause your program to become unresponsive or crash if left running for too long.
It's a good practice to always have some way to break out of an infinite loop, such as a user input or a condition that becomes true after a certain amount of time has elapsed.
'''
#Bsp. für ein Algorithmus, welches das Deterministisches eines Algorithmus widerlegt:
import random
def get_random_item(list):
if len(list) > 0:
index = random.randint(0, len(list)-1)
return list[index]
else:
None
'''
This function takes a list as input, and returns a random item from the list, or None if the list is empty.
The randomness comes from the use of the random.randint function, which generates a random integer between the two provided arguments.
However, the determinism of this function is not clear. While the use of random.randint suggests that the function is non-deterministic,
it's possible that the same result could be produced repeatedly if the function is called with the same input list multiple times.
For example, if get_random_item is called with the same list twice in a row, it's possible that it could return the same random item both times.
This would make the function deterministic in a sense,
but the deterministic behavior would be a side effect of the input rather than an intrinsic property of the function itself.
Therefore, while the function is intended to be non-deterministic,
its actual determinism is not clear and may depend on factors outside the function itself.
'''
#Bsp. für ein Code, welches die Ausfühbarkeit eines Algorithmus widerlegt:
def count_to_n(n):
for i in range(1, n+1):
print(i)
'''
This function takes a positive integer n as input and outputs the numbers from 1 to n to the console using the print function.
However, it doesn't return anything explicitly, so if you were to call it and try to assign its output to a variable or use it in another part of your program,
it wouldn't provide any useful output.
'''
output = count_to_n(5)
print(output)
'''
The output would be None, because the count_to_n function doesn't explicitly return anything.
Again, this is not necessarily a bad thing, as sometimes you might want a function to perform a side effect like printing to the console without returning any output.
However, if you need the output of a function for use elsewhere in your program, make sure it actually returns something!
'''
#algorithm foe cooking an well done egg:
import time
def cook_well_done_egg():
# Fill a small pot with water and place it on the stove over high heat
pot = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # dummy pot variable
heat_level = 'high' # set heat level to high
print(f"Placing pot on {heat_level} heat...")
# While the water is heating up, take an egg from the refrigerator and let it come to room temperature.
egg = 'cold egg' # dummy egg variable
print("Taking egg from the refrigerator...")
# Once the water is boiling, carefully place the egg into the pot using a slotted spoon.
print("Waiting for water to boil...")
while len(pot) < 20: # dummy condition for boiling water
time.sleep(1)
print("Water is boiling!")
print("Carefully placing egg into the pot...")
# Set a timer for 12 minutes and let the egg boil for the full duration.
timer = 12 # in minutes
print(f"Starting {timer} minute timer...")
for i in range(timer):
print(f"{timer-i} minutes remaining...")
time.sleep(60)
print("Timer complete!")
# While the egg is boiling, prepare a bowl of ice water.
bowl = 'bowl of ice water' # dummy bowl variable
print("Preparing bowl of ice water...")
# After 12 minutes, turn off the heat and carefully remove the egg from the pot using a slotted spoon.
print("Turning off heat...")
print("Removing egg from pot...")
cooked_egg = 'cooked egg' # dummy cooked egg variable
# Immediately place the egg into the ice water to stop the cooking process.
print("Placing egg into ice water to cool...")
time.sleep(5) # let egg cool for 5 minutes
# After the egg has cooled, crack it open and peel off the shell.
print("Removing egg from ice water...")
print("Peeling off shell...")
peeled_egg = 'peeled egg' # dummy peeled egg variable
# Cut the egg in half and check the yolk for the desired level of doneness.
print("Cutting egg in half...")
yolk = 'fully cooked' # set yolk to fully cooked
print(f"Yolk is {yolk}. Enjoy your well-done egg!")
Loading