Answer:
def milk_pattern():
global cows
cows = sorted(['Bessie', 'Buttercup', 'Belinda', 'Beatrice', 'Bella', 'Blue', 'Betsy', 'Sue'])
is_end = False
for _ in iter(dict, 0):
if is_end == True:
break
constraint = input("How would you order the cow, X before Y: ")
names = constraint.split(" before ")
is_end = bool(input("Do not want to add more constraint? True/Fale or any key for true: "))
if names[0] in cows and names[1] in cows:
lay_back = cows.index(names[0])
mv_fwd = cows.index(names[1])
if mv_fwd < lay_back:
count =0
for _ in range(lay_back - mv_fwd):
count += 1
hold = cows[mv_fwd]
cows[mv_fwd] = cows[mv_fwd + count]
cows[mv_fwd + count] = hold
else:
count1 = 0
for _ in range(mv_fwd - lay_back):
count1 += 1
hold = cows[mv_fwd]
cows[mv_fwd] = cows[mv_fwd - count1]
cows[mv_fwd - count1] = hold
milk_pattern()
print(cows)
Step-by-step explanation:
The python code defines a function called "milk_pattern" that defines a global list called "cows" and sorts the cow names in the list and with constraints given from the user prompts the list of milking the cows is sorted accordingly.