advanced computer programming sinan aydin · 2019. 10. 16. · stack implementation stack...

18
Sinan AYDIN Advanced Computer Programming Stack Implementation; Multidimensional lists

Upload: others

Post on 18-Feb-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

  • Sinan AYDIN

    Advanced Computer Programming

    Stack Implementation; Multidimensional lists

  • Stack Implementation Stack Implementation; Multidimensional lists

    Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).There are many real-life examples of a stack.

  • Stack Implementation Stack Implementation; Multidimensional lists

    #Stack Data Structureclass Stack():

    def __init__(self):self.items = []

    def push(self, item):self.items.append(item)

    def pop(self):return self.items.pop()

    def is_empty(self):return self.items == []

    def peek(self):if not self.is_empty():

    return self.items[-1]def get_stack(self):

    return self.items

    yigin=Stack()yigin.push(1)yigin.push(2)yigin.push(3)

    print(yigin.items)yigin.pop()print(yigin.items)print(yigin.peek())

  • Stack Implementation Stack Implementation; Multidimensional lists

    #Stack Data Structureclass Stack():

    def __init__(self):self.items = []

    def push(self, item):self.items.append(item)

    def pop(self):return self.items.pop()

    def is_empty(self):return self.items == []

    def peek(self):if not self.is_empty():

    return self.items[-1]def get_stack(self):

    return self.items

    yigin=Stack()yigin.push(1)yigin.push(2)yigin.push(3)

    print(yigin.items)yigin.pop()print(yigin.items)print(yigin.peek())

    [1, 2, 3][1, 2]2

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Multiple-Subscripted Sequences

    b = [ [ 1, 2 ], [ 3, 4 ] ]

    c = ( ( 1, 2 ), ( 3, 4, 5 ) )

    from random import *Matrix = [[randint(1, 100) for j in range(0, 10)] for i in range(0, 10)]for x in range(len(Matrix)):

    print(Matrix[x])

  • Multidimensional lists Stack Implementation; Multidimensional lists

    # Fig. 5.20: fig05_20.py# Making tables using lists of lists and tuples of tuples.table1 = [[1, 2, 3], [4, 5, 6]]table2 = ((1, 2), (3,), (4, 5, 6))print ("Values in table1 by row are")for row in table1:

    for item in row:print (item,end=" ")

    print ()print ("\nValues in table2 by row are")

    for row in table2:for item in row:

    print (item,end=" ")print()

    Values in table1 by row are1 2 3 4 5 6

    Values in table2 by row are1 2 3 4 5 6

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Tasks:

    1. Write a function that allows the user to first enter a matrix that defines its size

    2. Write two functions that write and read the entered matrix into the file

    3. Write a function that adds two identical sized matrices that are entered by this method

    Input loopm[i][j]=int(input("input %d. row %d. column value:" % (i+1,j+1)))

    file = open("My_matrix.Txt", "w")

    You can write a list directlyprint(str(m), end=" ", file=file)

    file = open("My_matrix.Txt", "r")

    Generate empty matrixmatrix1 = [[0 for j in range(ys)] for i in range(xs)]

    Read as text and convert as a list with eval() functiona=eval(file.read())

    mn[i][j]=m[i][j]+n[i][j]

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Tasks:

    1. Write a function that allows the user to first enter a matrix that defines its sizeEnter dimensionsDefine matrix as a listFor loop matrix columns

    For loop matrix rowsinput matrix element for [i][j]

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Tasks:

    1. Write a function that allows the user to first enter a matrix that defines its sizeEnter dimensionsDefine matrix as a listFor loop matrix columns

    For loop matrix rowsinput matrix element for [i][j]

    int(input("Enter matrix m for size (m x n) :")

    int(input("Enter matrix n for size (m x n) :"))

    for i in range(len(m)):for j in range(len(m[i])):

    matrix1 = [[0 for j in range(ys)] for i in range(xs)]

    m[i][j]=int(input("input %d. row %d. column value:" % (i+1,j+1)))

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Tasks:

    1. Write a function that allows the user to first enter a matrix that defines its size

    def inputmatrix(m):for i in range(len(m)):

    for j in range(len(m[i])):m[i][j]=int(input("input %d. row %d. column value:" % (i+1,j+1)))

    # enter dimensionsxs=int(input("Enter matrix m for size (m x n) :"))ys=int(input("Enter matrix n for size (m x n) :"))# generate matrixmatrix1 = [[0 for j in range(ys)] for i in range(xs)]inputmatrix(matrix1)

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Tasks:

    1. Write a function that allows the user to first enter a matrix that defines its size

    def inputmatrix(m):for i in range(len(m)):

    for j in range(len(m[i])):while True:

    try:m[i][j]=int(input("input %d. row %d. column value:" % (i+1,j+1)))break

    except ValueError:print ("You must enter integer value")

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Tasks:

    1. Write a function that allows the user to first enter a matrix that defines its size2. Write two functions that write and read the entered matrix into the file

    create a file with w parameterconvert the list to stringwrite string to fileclose file

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Tasks:

    1. Write a function that allows the user to first enter a matrix that defines its size2. Write two functions that write and read the entered matrix into the file

    create a file with w parameterconvert the list to stringwrite string to fileclose file

    file = open("My_matrix.Txt", "w")

    str(m)

    print(str(m), end=" ", file=file)

    file.close()

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Tasks:

    1. Write a function that allows the user to first enter a matrix that defines its size2. Write two functions that write and read the entered matrix into the file

    create a file with w parameterconvert the list to stringwrite string to fileclose file

    open file in read moderead text variableconvert to listreturn matrixclose file

    def writematrixtofile (m):# create a file with w parametertry:

    file = open("My_matrix.Txt", "w") # open file in write modeexcept IOError: # file open failed

    print("File could not be opened:")sys.exit(1)

    print(str(m), end=" ", file=file)file.close()

    file = open("My_matrix.Txt", "r")

    a=eval(file.read())

    for i in range(len(m)):for j in range(len(m[i])):

    m[i][j]=a[i][j]

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Tasks:

    1. Write a function that allows the user to first enter a matrix that defines its size2. Write two functions that write and read the entered matrix into the file

    create a file with w parameterconvert the list to stringwrite string to fileclose file

    open file in read moderead text variableconvert to listreturn matrixclose file

    def writematrixtofile (m):# create a file with w parametertry:

    file = open("My_matrix.Txt", "w") # open file in write modeexcept IOError: # file open failed

    print("File could not be opened:")sys.exit(1)

    print(str(m), end=" ", file=file)file.close()

    def readmatrixfromfile(m):file = open("My_matrix.Txt", "r") # open file in read modetry:

    passexcept IOError: # file open failed

    print("File could not be opened:")sys.exit(1)

    a=eval(file.read())for i in range(len(m)):

    for j in range(len(m[i])):m[i][j]=a[i][j]

    file.close()

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Tasks:

    1. Write a function that allows the user to first enter a matrix that defines its size2. Write two functions that write and read the entered matrix into the file3. Write a function that adds two identical sized matrices that are entered by this method

    def sum_matrix(m,n):mn=mfor i in range(len(m)):

    for j in range(len(m[i])):mn[i][j]=m[i][j]+n[i][j]

    return mn

  • Multidimensional lists Stack Implementation; Multidimensional lists

    Tasks:1. Write a function that allows the user to first enter a matrix that defines its size2. Write two functions that write and read the entered matrix into the file3. Write a function that adds two identical sized matrices that are entered by this method

    #Main Program# enter dimensionsxs=int(input("Enter matrix m for size (m x n) :"))ys=int(input("Enter matrix n for size (m x n) :"))# generate matrixmatrix1 = [[0 for j in range(ys)] for i in range(xs)]matrix2 = [[0 for j in range(ys)] for i in range(xs)]matrix3 = [[0 for j in range(ys)] for i in range(xs)]# print empty generate matrixfor x in range(len(matrix1)):

    print(matrix1[x])# input matrix1print ("enter matrix1")inputmatrix(matrix1)# write matrix to filewritematrixtofile (matrix1)# input matrixprint ("enter matrix2")inputmatrix(matrix2)

    print ("---matrix1---")for x in range(len(matrix1)):

    print(matrix1[x])print ("---matrix2---")for x in range(len(matrix2)):

    print(matrix2[x])readmatrixfromfile(matrix3)print ("---reading..matrix3---")for x in range(len(matrix3)):

    print(matrix3[x])total=sum_matrix(matrix3,matrix2)print("--total---")for x in range(len(total)):

    print(total[x])

    import sysdef inputmatrix(m):

    for i in range(len(m)):for j in range(len(m[i])):

    while True:try:

    m[i][j]=int(input("input %d. row %d. column value:" % (i+1,j+1)))break

    except ValueError:print ("You must enter integer value")

    def writematrixtofile (m):try:

    file = open("My_matrix.Txt", "w") # open file in write modeexcept IOError: # file open failed

    print("File could not be opened:")sys.exit(1)

    print(str(m), end=" ", file=file)file.close()

    def readmatrixfromfile(m):file = open("My_matrix.Txt", "r") # open file in write modetry:

    passexcept IOError: # file open failed

    print("File could not be opened:")sys.exit(1)

    a=eval(file.read())for i in range(len(m)):

    for j in range(len(m[i])):m[i][j]=a[i][j]

    file.close()return a

    def sum_matrix(m,n):mn=mfor i in range(len(m)):

    for j in range(len(m[i])):mn[i][j]=m[i][j]+n[i][j]

    return mn

  • Sinan AYDIN

    Advanced Computer Programming

    Lists, Tuples and Dictionaries