ubuntuusers.de

Python-Rekursion

Status: Gelöst | Ubuntu-Version: Kein Ubuntu
Antworten |

Marc_BlackJack_Rintsch Team-Icon

Ehemalige
Avatar von Marc_BlackJack_Rintsch

Anmeldungsdatum:
16. Juni 2006

Beiträge: 4673

Wohnort: Berlin

Weil das Beispiel im ersten Beitrag so schlecht ist, hier mal wie man das klassisch in der funktionalen Programmierung machen würde einen Stack aus Paaren/Tupeln aufzubauen:

 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
NIL = ()


def create_stack():
    """
    Creates an empty stack.
    
    >>> create_stack()
    ()
    """
    return NIL


def push(stack, value):
    """
    Pushes given `value` to `stack` and returns the stack with that value on
    top.
    
    >>> push(create_stack(), 42)
    ((), 42)
    """
    return (stack, value)


def is_empty(stack):
    """
    Return `True` if given `stack` is empty, `False` otherwise.
    
    >>> is_empty(create_stack())
    True
    >>> is_empty(push(create_stack(), 23))
    False
    """
    return stack == NIL


def pop(stack):
    """
    Pop top of given `stack` and return a tuple with the stack and the popped
    value.
    
    Raises `ValueError` when trying to `pop()` from an empty stack.
    
    >>> pop(create_stack())
    Traceback (most recent call last):
      ...
    ValueError: Stack is empty
    >>> stack, value = pop(push(create_stack(), 4711))
    >>> value
    4711
    """
    if is_empty(stack):
        raise ValueError("Stack is empty")
    return stack


def insert_at_bottom(stack, value):
    """
    Return stack with the given `value` inserted at the bottom.
    
    >>> stack = insert_at_bottom(create_stack(), 42)
    >>> stack
    ((), 42)
    >>> stack = insert_at_bottom(stack, 23)
    >>> stack
    (((), 23), 42)
    >>> stack = insert_at_bottom(stack, 4711)
    >>> stack
    ((((), 4711), 23), 42)
    """
    if is_empty(stack):
        return push(stack, value)
    
    stack, temporary = pop(stack)
    stack = insert_at_bottom(stack, value)
    return push(stack, temporary)


def reverse(stack):
    """
    Returns a reversed stack.
    
    >>> reverse(create_stack())
    ()
    >>> reverse((((), True), False))
    (((), False), True)
    >>> reverse(((((), 4711), 23), 42))
    ((((), 42), 23), 4711)
    """
    if is_empty(stack):
        return stack

    stack, value = pop(stack)
    return insert_at_bottom(reverse(stack), value)

Das würde man so in der Praxis in Python natürlich nicht machen, da würde man sich eine Stack-Klasse schreiben, oder weil das ja eine eher sehr dünne Schicht über den list-Typ ist, gleich eine Liste verwenden.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Stack:
    def __init__(self):
        self.items = []
    
    def __len__(self):
        return len(self.items)
    
    def push(self, item):
        self.items.append(item)
    
    def pop(self):
        self.items.pop()
    
    def insert_at_bottom(self, item):
        self.items.insert(0, item)
    
    def reverse(self):
        self.items.reverse()
Antworten |