

'''
The module functions print a (relatively) short integer array
or a list with a visually highlighted subarray (sublist).

Highlighting is achived by adding extra symbols around
the items of the subarray, see examples below.
The printed arrray is visualy divided into cells,
each cell is occupied by one item.

Examples of the look of the output::

Display3 # because of 3 printed lines
+----################----+----+----+----+----+----+----+
| 23 # 11 | 44 | 33 # 88 | 62 |  9 | 21 | 17 | 84 |  2 |
+----################----+----+----+----+----+----+----+

Display1 # whole array on a single line
 23 <11>~<44>~<33>~<88>  62    9   21   17   84    2
22[11_44_33]66 18 44
'''

#------------------------------------------#
#                                          #
#   Display changeable parameters          #
#                                          #
#------------------------------------------#
cellWidth = 7 # including its L and R borders

#..........................................#
#                                          #
#   Auxilliary functions                   #
#                                          #
#..........................................#

def itemMaxWidth( arr ):
    maxval = max( arr )
    minval = min( arr )
    valueMaxWidth = len(str(maxval))
    # print( valueMaxWidth )
    if minval < 0:
         valueMaxWidth =  max( valueMaxWidth, len(str(minval)) )
    return valueMaxWidth

#..........................................#
#                                          #
#   display3( arr, beg, end )              #
#                                          #
#..........................................#

def display3( arr, beg, end ):
    # Complies with Python notation: end points *past* the range [beg, end).

    global cellWidth  # default minimum width

    valueMaxWidth = 0       # to be computed

    # adjust possibly 'wild' beg to end  relation
    if beg > end:
        end = beg

    # ------------------------------------------
    # establish max width of an item
    valueMaxWidth = itemMaxWidth( arr )

    # redefine cell width if neccessary
    if cellWidth < valueMaxWidth+2:
        cellWidth = valueMaxWidth+2

    # ------------------------------------------
    # define fill symbols and sequences

    # define fill primitives
    horLinePlainSymbol = '-'
    horLineBoldSymbol  = '#'
    horLinePlainCorner = '+'
    horLineBoldCorner  = '#'

    cellTopPlain   =  horLinePlainSymbol*(cellWidth-2)
    cellTopPlainL  =  horLinePlainCorner + cellTopPlain
    cellTopPlainR  =  cellTopPlain + horLinePlainCorner
    cellTopPlainLR =  horLinePlainCorner + cellTopPlain + horLinePlainCorner
    cellTopBold    =  horLineBoldSymbol*(cellWidth-2)
    cellTopBoldL   =  horLineBoldCorner + cellTopBold
    cellTopBoldR   =  cellTopBold + horLineBoldCorner
    cellTopBoldLR  =  horLineBoldCorner + cellTopBold + horLineBoldCorner

    cellFillPlainSpace   = ' '
    cellFillBoldSpace    = ' '
    cellPlainBorder    = '|'
    cellBoldOutBorder  = '#'
    cellBoldInBorder   = '|'

    # cell fill before and after the value
    cellFillLeftWidth  = (( cellWidth - 2 - valueMaxWidth ) + 1) // 2
    cellFillRightWidth =  cellWidth - 2 - valueMaxWidth - cellFillLeftWidth


    cellFillPlainSpaceL =  cellFillPlainSpace * cellFillLeftWidth
    cellFillPlainSpaceR =  cellFillPlainSpace * cellFillRightWidth
    cellFillBoldSpaceL  =  cellFillBoldSpace  * cellFillLeftWidth
    cellFillBoldSpaceR  =  cellFillBoldSpace  * cellFillRightWidth

    # shorter values left leading spaces
    valueLeadingPlainSpace = ' '
    valueLeadingBoldSpace  = ' '

    # ----------------------------------------------
    # construct horizontal line out of primitives

    boldStart = beg*(cellWidth-1)
    boldEnd   = end*(cellWidth-1)

    horLine = cellTopPlainL * beg
    if end > beg:
       horLine = horLine + cellTopBoldL * (end-beg-1)
       horLine = horLine + cellTopBoldLR
    if end <= beg:
       horLine += horLinePlainCorner
    horLine = horLine + cellTopPlainR * ( len(arr) - end )

    # ----------------------------------------------
    # construct contents representation

    # CONstruct REPresentation
    conrep = ''
    #  left plain segment
    for i in range ( 0, beg ):
        conrep += cellPlainBorder + cellFillPlainSpaceL
        conrep += (valueLeadingPlainSpace*valueMaxWidth + str(arr[i])) [-valueMaxWidth:]
        conrep += cellFillPlainSpaceR

    #  bold segment
    if end > beg:
        conrep += cellBoldOutBorder
        for i in range ( beg, end-1):
            conrep += cellFillBoldSpaceL
            conrep += (valueLeadingBoldSpace*valueMaxWidth + str(arr[i])) [-valueMaxWidth:]
            conrep += cellFillBoldSpaceR + cellBoldInBorder
        conrep += cellFillBoldSpaceL
        conrep += (valueLeadingBoldSpace*valueMaxWidth + str(arr[end-1])) [-valueMaxWidth:]
        conrep += cellFillBoldSpaceR + cellBoldOutBorder

    #  right plain segment
    if end <= beg:
       conrep += cellPlainBorder
    for i in range ( end, len(arr) ):
        conrep += cellFillPlainSpaceL
        conrep += (valueLeadingPlainSpace*valueMaxWidth + str(arr[i])) [-valueMaxWidth:]
        conrep += cellFillPlainSpaceR + cellPlainBorder

    print( horLine )
    print( conrep )
    print( horLine )
    # ...........................................
    # end of Display 3


#..........................................#
#                                          #
#   display1( arr, beg, end )              #
#                                          #
#..........................................#

# display1 is maximally economical, it does not define cell width.
# Adjacent items are visually separated by only one symbol.
# The symbols support visual highlighting of the selected interval
# However, as the highlighted part may be a prefix or a suffix,
# additional position for one symbol before and after the items used.

def display1( arr, beg, end ):
    # ------------------------------------------
    # define fill symbols and sequences

    plainSeparator = ' '
    boldSeparator  = '_'
    boldBorderL    = '<'
    boldBorderR    = '>'

    if end <= beg:
       separator = plainSeparator
       conrep = ''
       for item in arr:
           conrep += separator + str(item)
       conrep += separator
       print( conrep )
       return

    # CONstruct REPresentation
    conrep = ''
    for i in range( len(arr) ):
        # at position i, print always its preceding separator,
        separator = ''
        if i < beg:  separator = plainSeparator
        if i == beg: separator = boldBorderL
        if i > beg:
           if i < end:  separator = boldSeparator
           if i == end: separator = boldBorderR
           if i > end:  separator = plainSeparator
        conrep += separator + str(arr[i])
    # final symbol
    if end == len(arr): separator = boldBorderR
    else:               separator = plainSeparator
    conrep += separator

    print( conrep )



# ----------------- END OF MODULE ----------------------------------------------


# ==============================================================================
# anything to run when NOT used as a library module


if __name__ == '__main__':
   a = [24, 11, 6, 188, 3, 22, 12345, 92 ]
   beg, end = len(a)-5, len(a)-2
   display3( a, beg, end )
   display3( a, 0, 0 )
   display3( a, len(a)-1, len(a)-0 )

   display1( a, 2, 4 )
   display1( a, 0, 1 )
   display1( a, len(a)-1, len(a)-0 )






# --------------------------------------------------------------

# 2.
# Guarded Subarray: A[beg:end]
#   A[beg] == A[end-1]
#   A[k] < A[beg],
#           for all k in range from beg+1 up to and not including end-1.
#
# 3.
# Loosely Guarded Subarray: A[beg:end]
#   A[k] < A[beg] and A[k] < A[end-1],
#           for all k in range from beg+1 up to and not including end-1.
#
# 4.
# Up-cliff subarray A[beg:end]
#   len( A[beg:end] ) is even
#   A[j] < A[k],  for all index pairs (j, k)
#                 where beg <= j < mid <= k < end
#                        and  where mid = beg + (end-beg) // 2
#                ( mid is the index of the first item in the second half of the subarray A[beg:end] )
#
# 5.
# X-balanced subarray: A[beg:end]
#    the number of items in A[beg:end] which are smaller than X
#    is equal to the number of items in A[beg:end] which are bigger than X
#    and any number  items in A[beg:end] may be equal to X.
#
# 6.
#  Dense subarray: A[beg:end]
#    the sum of A[beg:end] is at least half of the sum of A.
#    Find the shortest Dense subarray









'''
pp = []
f = open( 'primes0.txt', "r" )
while True:
    line = f.readline().strip()
    if line == None or line.strip(" ") == "":
        break
    pp.extend( [int(t) for t in list(line.split(' ')) if t != '' ]  )
f.close()

print( len(pp), pp[-1] )

pp6 = [ t for t  in pp if t < 10**6 ]

print( len(pp6) )

g = open ( 'primesto106.txt', 'w' )
for t in pp6:
    g.write( str(t) + '\n' )
g.close()

'''




