
# Create an empty list.
aList = []
print( aList )

# Create a list with small number of values.
aList = [4, 3, 2, 1]
print( aList )

# -----------------------------------------------------------
# Create a list with generally more values,
# the list is filled in various systematic ways.

# Create a list with 7 copies of value 11
aList = [11]*7
print( aList )

# Create lists containing ranges of values.
aList1 = list( range(6) )
aList2 = list( range(0, 6) )
aList3 = list( range(4, 10) )
aList4 = list( range(-20, 100, 10) )
aList5 = list( range( 20, 10, -1 ) )
aList6 = list( range( 20, 0, -4 ) )

print( aList1 )
print( aList2 )
print( aList3 )
print( aList4 )
print( aList5 )
print( aList6 )

# -----------------------------------------------------------
# Hard copy of a list
aList1 = [6,5,4,3,2]
aList2 = aList1.copy()
print(aList1)
print(aList2)


# -----------------------------------------------------------
#   LIST COMPREHENSION

# List of 3rd powers
aList = [n*n*n for n in range(0,10) ]
print( aList )

# List of 3rd powers, only divisible by 3
aList = [n*n*n for n in range(0,10) if n % 3 == 0]
print( aList )

# List of values taken from another list, multiplied by 100,
# but only of those which are less then 10
aList1 = [18, 7, 16,  3, 12, 1  ]
aList2 = [ x*100 for x in aList1 if x < 10]
print(aList1)
print(aList2)

# -----------------------------------------------------------
# Various solutions of the self-check task:
#    Print the number of such elements in list L which value
#    differs from the given x by less than 10.


# Classical solution 1
def ff_classic( L, x ):
    counter = 0
    for t in L:
        if abs(t-x) < 10: counter += 1
    print( counter )

# Classical solution 1
def ff_superClassic( L, x ):
    counter = 0
    for i in range(0, len(L)):
        if abs(L[i]-x) < 10: counter += 1
    print( counter )


# List comprehension and indicator vector
def ff1( L, x ):
    # create an indicator vector of chosen values
    indic = [ int(abs(val-x) < 10) for val in L  ]
    print( indic )
    print( sum(indic) )
# Note that int(True) == 1, int(False) == 0.
# Indicator vector is sometimes a handy tool, typically
# more on conceptual level that on implementation level


# List comprehension and list of ones for each chosen value
def ff2( L, x ):
    # create a vector containg only 1s,
    # each 1 stands for one chosen value
    ones = [1  for val in L if abs(val-x) < 10  ]
    print( ones )
    print( sum(ones) )


# List comprehension
def ff3( L, x ):
    print( sum(1 for t in L if abs(t-x) < 10 ) )

L = [ 48, 10, 20, 51, 90, 80, 53, 70 ]
x = 50
print()
print( L )

ff_classic( L, x )
ff_superClassic( L, x )
ff1( L, x )
ff2( L, x )
ff3( L, x )

