#!/usr/bin/env python
# coding: utf-8

import sys

# Символы Юникод

# Autogenerated with DRAKON Editor 1.31

def alt_select_test():
    #item 197
    alternative_select(-100, -1)
    alternative_select(100, 1)
    alternative_select(0, 0)


def alternative_select(value, expected):
    #item 2060001
    if value < 0:
        #item 214
        actual = -1
    else:
        #item 2060002
        if value == 0:
            #item 215
            actual = 0
        else:
            #item 216
            actual = 1
    #item 217
    if actual == expected:
        pass
    else:
        #item 223
        raise Exception("alt select failure!")
    #item 2270001
    if value < 0:
        #item 235
        actual = -1
    else:
        #item 2270002
        if value == 0:
            #item 236
            actual = 0
        else:
            #item 2270003
            if value > 0:
                pass
            else:
                #item 2270004
                raise Exception("Condition was not detected.")
            #item 237
            actual = 1
    #item 238
    if actual == expected:
        pass
    else:
        #item 241
        raise Exception("alt select failure!")


def fibonacci(n):
    #item 1530001
    if n == 0:
        #item 157
        result = [0]
    else:
        #item 1530002
        if n == 1:
            #item 158
            result = [0, 1]
        else:
            #item 159
            result = [0, 1]
            #item 1630001
            i = 2
            while True:
                #item 1630002
                if i <= n:
                    pass
                else:
                    break
                #item 164
                f2 = result[i - 2]
                f1 = result[i - 1]
                fib = f1 + f2
                #item 165
                result.append(fib)
                #item 1630003
                i += 1
    #item 168
    return result


def foreach_demo():
    #item 178
    print("iteration demo")
    #item 169
    sequence = fibonacci(15)
    #item 170
    print_list_arrow(sequence)
    print_list_for(sequence)
    print_list_foreach(sequence)
    #item 179
    print("")
    #item 283
    test_early(20, 60)
    #item 284
    test_early(35, 70)
    #item 285
    test_early(4, 0)


def foreach_early_exit(value):
    
    normal_255 = 1
    for x in [10, 20, 30]:
        #item 259
        if x == value:
            #item 263
            found = x
            normal_255 = 0
            break
        else:
            pass
    if normal_255 == 1:
        #item 262
        found = 0
    for x in [40, 20, 70]:
        #item 271
        m = x / 2
        #item 266
        if m == value:
            #item 270
            return x + found
        else:
            pass
    #item 269
    return 0


def main():
    #item 182
    print("DRAKON-Python demo")
    print("==================")
    #item 242
    alt_select_test()
    #item 174
    foreach_demo()
    #item 175
    quick_sort_demo()
    #item 193
    result = "success"
    #item 192
    return result


def print_list_arrow(collection):
    #item 134
    print("using if and arrow:")
    #item 120
    length = len(collection)
    i = 0
    while True:
        #item 121
        if i < length:
            pass
        else:
            break
        #item 119
        item = collection[i]
        write(item)
        #item 123
        i += 1
    #item 135
    print("")


def print_list_for(collection):
    #item 176
    print("using for:")
    #item 130
    length = len(collection)
    #item 1310001
    i = 0
    while True:
        #item 1310002
        if i < length:
            pass
        else:
            break
        #item 133
        item = collection[i]
        write(item)
        #item 1310003
        i += 1
    #item 177
    print("")


def print_list_foreach(collection):
    #item 137
    print("using foreach:")
    for item in collection:
        #item 112
        write(item)
    #item 136
    print("")


def quick_sort_demo():
    #item 181
    print("quick sort demo")
    #item 17
    unsorted = [ "the", "sooner", "we", "start", "this", "the", "better" ]
    sorted = [ "aa", "bb", "cc", "dd", "ee", "ff" ]
    reverse = [ "ff", "ee", "dd", "cc", "bb", "aa" ]
    empty = []
    flat = [ "flat", "flat", "flat", "flat", "flat" ]
    #item 18
    sorter = Sorter(string_comparer)
    unsorted2 = sorter.quick_sort(unsorted)
    sorted2 = sorter.quick_sort(sorted)
    reverse2 = sorter.quick_sort(reverse)
    empty2 = sorter.quick_sort(empty)
    flat2 = sorter.quick_sort(flat)
    #item 19
    print(str(unsorted2))
    print(str(sorted2))
    print(str(reverse2))
    print(str(empty2))
    print(str(flat2))
    #item 20
    strings_are_sorted(unsorted2)
    strings_are_sorted(sorted2)
    strings_are_sorted(reverse2)
    strings_are_sorted(empty2)
    strings_are_sorted(flat2)
    #item 180
    print("")


def string_comparer(left, right):
    #item 6
    if left < right:
        #item 7
        return -1
    else:
        #item 10
        if left > right:
            #item 9
            return 1
        else:
            #item 8
            return 0


def strings_are_sorted(array):
    length = len(array)
    
    _next_item_ = 260001
    while True:
        if _next_item_ == 260001:
            _it26 = iter(range(0, length))
            try:
                i = _it26.next()
                _go26 = True
            except StopIteration:
                _go26 = False
            _next_item_ = 260002
    
        elif _next_item_ == 260002:
            if _go26:
                #item 28
                current = array[i]
                _next_item_ = 290001
            else:
                return None
    
        elif _next_item_ == 290001:
            j = i + 1
            _next_item_ = 290002
    
        elif _next_item_ == 290002:
            if j < length:
                #item 31
                after = array[j]
                _next_item_ = 360000
            else:
                #item 260003
                try:
                    i = _it26.next()
                    _go26 = True
                except StopIteration:
                    _go26 = False
                _next_item_ = 260002
    
        elif _next_item_ == 360000:
            _sw360000_ = string_comparer(current, after)
            _next_item_ = 360001
    
        elif _next_item_ == 360001:
            if _sw360000_ == -1:
                #item 43
                _next_item_ = 290003
            else:
                _next_item_ = 360002
    
        elif _next_item_ == 360002:
            if _sw360000_ == 0:
                #item 44
                _next_item_ = 290003
            else:
                _next_item_ = 360003
    
        elif _next_item_ == 290003:
            j += 1
            _next_item_ = 290002
    
        elif _next_item_ == 360003:
            if _sw360000_ == 1:
                _next_item_ = 45
            else:
                #item 360004
                raise Exception("Not expected:  " + str(_sw360000_))
                _next_item_ = 45
    
        elif _next_item_ == 45:
            _next_item_ = 32
    
        elif _next_item_ == 32:
            raise Exception( "Collection is not sorted:\n" + str(array))
            return None
    


def test_early(value, expected):
    #item 278
    actual = foreach_early_exit(value)
    #item 279
    if actual == expected:
        pass
    else:
        #item 282
        raise Exception("foreach_early_exit failed")


def write(item):
    #item 142
    sys.stdout.write(str(item))
    sys.stdout.write(" ")

class Sorter:
    foo = ""


    def __init__(self, comparer):
        #item 105
        self.comparer = comparer


    def quick_sort(self, collection):
        #item 60
        length = len(collection)
        #item 610001
        if (length == 0) or (length == 1):
            #item 72
            result = collection
        else:
            #item 610003
            if length == 2:
                #item 77
                first = collection[0]
                second = collection[1]
                #item 78
                if self.comparer(first, second) < 0:
                    #item 98
                    result = collection
                else:
                    #item 79
                    result = [ second, first ]
            else:
                #item 82
                half = int(length / 2)
                median = collection[half]
                left = []
                right = []
                #item 830001
                i = 0
                while True:
                    #item 830002
                    if i < length:
                        pass
                    else:
                        break
                    #item 96
                    if i == half:
                        pass
                    else:
                        #item 85
                        current = collection[i]
                        #item 86
                        if self.comparer(current, median) < 0:
                            #item 87
                            left.append(current)
                        else:
                            #item 88
                            right.append(current)
                    #item 830003
                    i += 1
                #item 94
                left_sorted = self.quick_sort(left)
                right_sorted = self.quick_sort(right)
                #item 95
                result = []
                result.extend(left_sorted)
                result.append(median)
                result.extend(right_sorted)
        #item 99
        return result

main()
