-
Notifications
You must be signed in to change notification settings - Fork 0
/
pancake.py
47 lines (38 loc) · 1.62 KB
/
pancake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import random
class Pancake(object):
def __init__(self, value):
self.value = value
self.burnt_side_down = bool(random.getrandbits(1))
class PancakeStack(object):
def __init__(self, pancakes):
self.pancakes = pancakes
def flip(self, position):
# Determine the last position to which we will be swapping items
items_to_swap = ((len(self.pancakes) - position) / 2)
# Swap the pancakes effectively "flipping" the stack from the position
# provided
for i in range(0, items_to_swap):
temp = self.pancakes[len(self.pancakes) - i - 1]
self.pancakes[len(self.pancakes) - i - 1] = self.pancakes[position + i]
self.pancakes[position + i] = temp
def sort(self):
last_sorted_position = 0
# Start at the bottom and find the largest out of order pancake.
# Flip that pancake to the top, and then back down to the last
# un-sorted position.
while last_sorted_position < len(self.pancakes) - 1:
to_flip = self.largest_out_of_order(last_sorted_position)
self.flip(to_flip)
self.flip(last_sorted_position)
last_sorted_position += 1
def largest_out_of_order(self, start):
largest = start
for i in range(start, len(self.pancakes)):
if self.pancakes[i].value > self.pancakes[largest].value:
largest = i
return largest
def ordered(self):
for i in range(0, len(self.pancakes) - 1):
if self.pancakes[i].value >= self.pancakes[i + 1].value:
return False
return True