Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving.

Similar presentations


Presentation on theme: "Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving."— Presentation transcript:

1 Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving

2 Arrays / Lists Python lists represent sequence of [similar] elements - between [ ], separated by, which are indexed. [ ]  represents lists / arrays. A list l with n elements has the elements l[0], l[1], l[2],…, l[n-1]. l represents the list while l[i] is an element of the list. the first element of the list is indexed with 0. it is possible to have negative indexing.

3 Arrays / Lists Operations List operations: list1 + list2  concatenates the lists.list1 * nr  list1 clones nr times. elem in list  tests if elem is in list. List functions len(l)  length of l max(l)  maximum element of a list min(l)  minimum element list(sequence)  converts the sequence to a list. l.append(elem)  appends elem to the list l.insert(index, elem)  inserts elem to the list at the specified index l.count(elem)  counts how many times elem is in the list l.index(elem)  returns the first index of elem in the list

4 Arrays / Lists Traversal ANY COMPUTATION WITH LIST MUST USE THE LOOP FOR. Ways to traverse the list 1) Traverse the list as iterator 2) Traverse the list elements for elem in l : # process elem … # endfor n = len(l) for i in range(n) : # process the element l[i] … # endfor

5 Input a List Suppose that we need a list with n int elements. - read the number of elements in the list - read n elements using a for loop This scheme will work for any programming language. n = int(input(‘’n=)) l = [] for i in range(n) : elem = int(input()) l.append(elem) # endfor

6 Input a List Suppose that we need a list with int elements. A Python approach: - read some elements and make an array of strings from them - convert them to a sequence of int elements - make a list from this sequence. l = input(‘type elements: ’).split() l = list(map(int, l))

7 Print a List Suppose that we have a list with n int elements. - use a for loop to print each element This scheme will work for any programming language. print(l[i], end = ‘ ‘) printing on the same line. print(l)  the whole list is printed between [] n = len(l) for i in range(n) : print(l[i], end = ‘ ‘) # endfor

8 def listSum(a): n = len(a) # initialise sum sum = 0 # traverse the list for i in range(n) : sum = sum + a[i] # endfor return sum # end listSum def listProduct(a): n = len(a) # initialise prod prod = 1 # traverse the list for i in range(n) : prod = prod * a[i] # endfor return prod # end listProduct

9 def listMax(a): n = len(a) # initialise max, pos max = -10000000 pos = -1 # traverse the list for i in range(n) : if max < a[i] : max = a[i] pos = i # endfor return max, pos # end listProduct def listMin(a): n = len(a) # initialise max, pos min = 10000000 pos = -1 # traverse the list for i in range(n) : if min > a[i] : min = a[i] pos = i # endfor return min, pos # end listProduct

10 Working with Python Files File definition? - external storage of some data. - sequence of characters or bytes stored externally. - the simplest way to persistently store data. - a file is identified by a name and an extension. Types of files: - text files with content as sequence of characters e.g. input.txt, program.py, etc - byte files with content as sequence of bytes e.g. python.exe, etc Operation with files: - reading from a file -writing to a file or appending to a file

11 Working with Python Files How to work with Files in Python? - open the file for the needed operation. f = open(’filename.txt’, ‘how’) filename.txt  the name of the file how is a combination of r/w/a, t/b - do the reading and writing as it is required f.read()  read the whole file f.readline()  read a single line as a string + \n f.write(str)  write a str to a file - close the file. f.close()

12 Working with Python Files Example: 1)Read one integer from a file ‘input.txt’ f = open(‘input.txt’, ‘rt’) a = int(f.readline().strip()) f.close() 2) Read three integers from a single line of the file ‘input.txt’ f = open(‘input.txt’, ‘rt’) a, b, c = map(int, f.readline().strip().split(‘ ‘)) f.close()

13 Working with Python Files Example: 3) Read an array from the file. The file contains the number of elements on the first line and then the array elements on each line. f = open(‘input.txt’, ‘rt’) n = int(f.readline().strip()) array = [] for I in range(n): elem = int(f.readline().strip()) array.append(elem) f.close()

14 Working with Python Files Example: 4) Writing one integer to a file ‘output.txt’ f = open(‘output.txt’, ‘wt’) f.write(str(a)) f.close() 5) Write three integers to a file ‘output.txt’ on the same line f = open(‘output.txt’, ‘wt’) f.write(str(a) + ‘ ‘+ str(b) +’ ‘ + str(c)) f.write(“%d %d %d” %(a, b, c)) f.close()

15 Working with Python Files Example: 6) Write n elements of a list to a file ‘output.txt’ each element of a different line f = open(‘output.txt’, ‘wt’) for i in range(n): f.write(str(array[i])+’\n’) f.close()

16 Working with Python Files AIPO PROBLEMS MAY REQUIRE I/O FROM FILES

17 AIPO Q5 – Bitcoin Investment Given a series of int numbers representing the costs of 1 bitcoin then find the best profit from buying and selling 1 bitcoin. Input: n – int representing price – an array with n numbers written on same line Output: profit – int representing the maximum profit to get Questions: What problem is that? Maximise the profit How to calculate the profit if we buy in day i? It is the maximum value for the bitcoin prices in the future minus the current price on day i. Then calculate the maximum of this profit.

18 AIPO Q5 – Bitcoin Investment How to do it: 1) read n 2) read price – n numbers from the same line 3) initialise profitMax 4) for each day calculate the profit of buying a bitcoin this is a max of price[j]-price[i] for j in range i, i+1, … text this profit against profitMax

19 AIPO Q6 – Combinatorial Numbers This problem is to calculate the combinatorial numbers C(n, k) for some large values of n and k. For example C(123, 54) = 307481607132479763736986522890815830 When we work with large numbers we try to do modulo calculation to keep the values of the results with a certain range; in our case this is modulo 1000000007 so any arithmetical operation must be followed by % 1000000007.

20 AIPO Q6 – Combinatorial Numbers Steps to solve it: 1)Read the input t 2)For each i in range (t) 1)Read the values n, k 2)Calculate the binomial number C(n,k) 3)Print the result

21 To do List 1.Solve ALL AIPO problems. 2.Read about lists


Download ppt "Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving."

Similar presentations


Ads by Google