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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to Programming
1 Module 2: Fundamental Concepts Problems Programs –Programming languages.
2/28/2008. >>> Overview Arrays in Python – a.k.a. Lists Ranges are Lists Strings vs. Lists Tuples vs. Lists Map-Reduce Lambda Review: Printing to a file.
LAB-10 1-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
Group practice in problem design and problem solving
 1 String and Data Processing. Sequential Processing Processing each element in a sequence for e in [1,2,3,4]: print e for c in “hello”: print c for.
Programming Training Main Points: - Working with Functions in Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Lecture 16 – Open, read, write and close files.  At the end of this lecture, students should be able to:  understand file structure  open and close.
Lists in Python.
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Munster Programming Training
Computer Science 111 Fundamentals of Programming I Number Systems.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/ Objective : 1.To understand primitive storage structures and types 2.To.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
Lists Computers and Programming. Agenda What is a list? How to access elements in the list? The for statement Operations on lists Looping with.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Computer Science 210 Computer Organization Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Strings CS303E: Elements of Computers and Programming.
Homework #5 New York University Computer Science Department Data Structures Fall 2008 Eugene Weinstein.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8.
Programming Training Main Points: - Sorting and searching algorithms on Arrays. - Problem Solving.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Mr. Fowler Computer Science 14 Feb 2011 Strings in Python.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
Programming Constructs Notes Software Design & Development: Computational Constructs, Data Types & Structures, Algorithm Specification.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Computer Science 18 Feb 2011 Strings in Python. Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson.
Chapter 9: Data types and data structures OCR Computing for A Level © Hodder Education 2009.
Topic: Python Lists – Part 1
COMPSCI 107 Computer Science Fundamentals
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Main Points: - Working with strings. - Problem Solving.
Python’s input and output
Main Points: - More Fundamental Algorithms on Arrays.
Determinate Loops with the
Creation, Traversal, Insertion and Removal
Programming Training Main Points:
String and Lists Dr. José M. Reyes Álamo.
Programming Training Main Points: - Working with Functions in Python
Programming Training Main Points: - Problems with repetitions.
Programming Training Main Points: - Working with strings.
Hmjngj jxhngh.
Files Handling In today’s lesson we will look at:
Topics Introduction to File Input and Output
Programming Dr. Jalal Kawash.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
More Basics of Python Common types of data we will work with
Introduction to Computer Science
Presentation transcript:

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

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.

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

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

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

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))

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

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

def listMax(a): n = len(a) # initialise max, pos max = 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 = 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

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

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()

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()

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()

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()

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()

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

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.

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

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) = 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 so any arithmetical operation must be followed by %

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

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