Lecture 26: Lists of Lists

Slides:



Advertisements
Similar presentations
RAPTOR Syntax and Semantics By Lt Col Schorsch
Advertisements

2/7/2008. >>> Overview * boolean * while * random * tuples.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
1 Various Methods of Populating Arrays Randomly generated integers.
Computer Science 111 Fundamentals of Programming I More Digital Image Processing.
Twenty high-level operations on pictures. Recap: vector graphics constructors [box width height] Creates a picture with a box [group pictures …] Makes.
Behavioral Experiments in a Network Formation Game Prof. Michael Kearns Networked Life Spring 2010.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Fifteen high-level operations on pictures. Recap: vector graphics constructors [box width height] Creates a picture with a box [group pictures …] Makes.
Lec 5 Feb 10 Goals: analysis of algorithms (continued) O notation summation formulas maximum subsequence sum problem (Chapter 2) three algorithms image.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
INTRODUCTION TO PYTHON PART 5 - GRAPHICS CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Bitmapped Images 27 th November 2014 With Mrs
Vector Graphics Making custom images. Raster vs. Vector Graphics In computer graphics, a raster graphics image, or bitmap, is a dot matrix data structure.
Image Processing & Perception Sec 9-11 Web Design.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Vector vs. Bitmap
1 Chapter 5 Continuous Random Variables. 2 Table of Contents 5.1 Continuous Probability Distributions 5.2 The Uniform Distribution 5.3 The Normal Distribution.
Computer Science 111 Fundamentals of Programming I Introduction to Graphics.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To.
McGraw-Hill/IrwinCopyright © 2009 by The McGraw-Hill Companies, Inc. All Rights Reserved. Continuous Random Variables Chapter 6.
June 14, ‘99 COLORS IN MATLAB.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
PART TWO Electronic Color & RGB values 1. Electronic Color Computer Monitors: Use light in 3 colors to create images on the screen Monitors use RED, GREEN,
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
IS502:M ULTIMEDIA D ESIGN FOR I NFORMATION S YSTEM D IGITAL S TILL I MAGES Presenter Name: Mahmood A.Moneim Supervised By: Prof. Hesham A.Hefny Winter.
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
Images provided by Ohio University’s Witmer Lab for STEM educational aids for K–12 Researchers conducted a CT scan of the head of a 41-year-old male white.
ITEC2110, Digital Media Chapter 2 Fundamentals of Digital Imaging 1 GGC -- ITEC Digital Media.
Test 2 on Wed, 11/9 On image processing
Data Representation Images.
David Meredith Aalborg University
Pixels, Colors and Shapes
Computer Graphics Lecture 3 Computer Graphics Hardware
Adapted from slides by Marty Stepp and Stuart Reges
high-level operations on pictures
Adapted from slides by Marty Stepp and Stuart Reges
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Color Values All colors in computer images are a combination of red, green and blue Each component is encoded as a number means the color is.
6th Lecture – Rectangles and Regions, and intro to Bitmap Images
Topic 8 graphics "What makes the situation worse is that the highest level CS course I've ever taken is cs4, and quotes from the graphics group startup.
Representing Images 2.6 – Data Representation.
Building Java Programs
Flooding © 2018.
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges
CSE 8A Lecture 6 Reading for next class:
Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Fundamentals of Programming I Introduction to Digital Image Processing
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Multidimensional Arrays
CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.
Programming for Art: Images
Lecture 29: Lists of Lists
Non-numeric Data Representation
Lecture 30: Lists of Lists
EECS 110: Lec 12: Mutable Data
Lecture 24: Lists of Lists
Presentation transcript:

Lecture 26: Lists of Lists CSc 110, Spring 2017 Lecture 26: Lists of Lists

List of lists mystery def mystery(data, pos, n): result = [] for i in range(0, n): for j in range(0, n): result.append(data[i + pos][j + pos]) return result Suppose that a variable called grid has been declared as follows: grid = [[8, 2, 7, 8, 2, 1], [1, 5, 1, 7, 4, 7], [5, 9, 6, 7, 3, 2], [7, 8, 7, 7, 7, 9], [4, 2, 6, 9, 2, 3], [2, 2, 8, 1, 1, 3]] which means it will store the following 6-by-6 grid of values: 8 2 7 8 2 1 1 5 1 7 4 7 5 9 6 7 3 2 7 8 7 7 7 9 4 2 6 9 2 3 2 2 8 1 1 3 For each call at right, indicate what value is returned. If the function call results in an error, write error instead. Function Call Contents of List Returned mystery(grid, 2, 2) ___________________________ mystery(grid, 0, 2) ___________________________ mystery(grid, 3, 3) ___________________________

Find Lucky 7 Write a function lucky(m)that takes a rectangular list m and looks for the number 7 in m. If found, lucky returns a list containing the row and column position of 7, and if not found, returns an empty list. Example: z = [[20, 3, 6], [4, 12, 18], [6, 13, 5], [15, 7, 8]] lucky(z) Returns [3,1]

Mountain peak Write a program that reads elevation data from a file, draws it on a DrawingPanel and finds the path from the highest elevation to the edge of the region. Data: 34 76 87 9 34 8 22 33 33 33 45 65 43 22 5 7 88 0 56 76 76 77 4 45 55 55 4 5 …

Mountain peak Consider the data: 34 76 87 9 34 8 22 33 33 33 45 65 43 22 5 7 88 0 56 76 76 77 4 45 55 55 4 5 … Each line is a row of elevations  we will create a list of lists of elevations First steps: 1) create a mapping of the data representation to DrawingPanel components 2) read in the data 3) draw an image of the elevation data using DrawingPanel components

[ [34, 76, 87, 9, 34, 8, 22, 33, 33, 33, 45, 65, 43, 22] [5, 7, 88, 0, 56, 76, 76, 77, 4, 45, 55, 55, 4, 5] … ] Each row becomes a row of rectangles in the DrawingPanel Each rectangle is 1 pixel wide If the mountain data is stored in a list of lists data, how large do we make the DrawingPanel? p = DrawingPanel( width ??, height ??)

[ [34, 76, 87, 9, 34, 8, 22, 33, 33, 33, 45, 65, 43, 22] [5, 7, 88, 0, 56, 76, 76, 77, 4, 45, 55, 55, 4, 5] … ] Each row becomes a row of rectangles in the DrawingPanel Each rectangle is 1 pixel wide The elevation is mapped to a level of black or white, a shade of grey, creating a greyscale image: white is weakest (higher in elevation) black is strongest (lower in elevation) p.canvas.create_rectangle(x, y, x + 1, y + 1, outline=map elevation to a shade of grey)

Mapping indices to arguments data = [ [34, 76, 87, 9, 34, 8, 22, 33, 33, 33, 45, 65, 43, 22] [5, 7, 88, 0, 56, 76, 76, 77, 4, 45, 55, 55, 4, 5]... ] For the first row: p.canvas.create_rectangle(0, 0, 1, 1, outline = color of 34) p.canvas.create_rectangle(1, 0, 2, 1, outline = color of 76) p.canvas.create_rectangle(2, 0, 3, 1, outline = color of 87) p.canvas.create_rectangle(3, 0, 4, 1, outline = color of 9) ....

Mapping to indices to arguments data = [ [34, 76, 87, 9, 34, 8, 22, 33, 33, 33, 45, 65, 43, 22] [5, 7, 88, 0, 56, 76, 76, 77, 4, 45, 55, 55, 4, 5]... ] for row in range(0, len(data)): for col in range(0, len(data[row])): color = get_color(data[row][col]) p.canvas.create_rectangle(col, row, col + 1, row + 1, outline = color) p.canvas.create_rectangle(0, 0, 1, 1, outline = color of 34-- data[0][0]) p.canvas.create_rectangle(1, 0, 2, 1, outline = color of 76-- data[0][1]) p.canvas.create_rectangle(2, 0, 3, 1, outline = color of 87-- data[0][2]) p.canvas.create_rectangle(3, 0, 4, 1, outline = color of 9-- data[0][3]) ...

2) Read in the data from drawingpanel import * from random import * def main(): file = open("mountaindata.dat") lines = file.readlines() data = [] for line in lines: data.append(line.split()) p = DrawingPanel(len(data[0]), len(data)) draw_image(p, data)

3) Draw the elevation image # draws the passed in data on the passed in drawing panel. # The data is a list of lists of numbers representing # elevation data. def draw_image(p, data): for row in range(0, len(data)): # data[row] -> [3, 5, 76, 3] for col in range(0, len(data[row])): color = get_color(int(data[row][col])) p.canvas.create_rectangle(col, row, col + 1, row + 1, outline=color)

Mountain peak data = [ [34, 76, 87, 9, 34, 8, 22, 33, 33, 33, 45, 65, 43, 22] [5, 7, 88, 0, 56, 76, 76, 77, 4, 45, 55, 55, 4, 5] ... ] Next steps: 4) Find the peak 5) Find the steepest path down 6) Draw the path in yellow

4) Find the peak data = [ [34, 76, 87, 9, 34, 8, 22, 33, 33, 33, 45, 65, 43, 22] [5, 7, 88, 0, 56, 76, 76, 77, 4, 45, 55, 55, 4, 5] ... ] Find the largest elevation in the list of lists. Write find_peak(data) Return a tuple of the location in the 2d list

5) Find the steepest path data = [['2537', '2483', '2475', '2480', '2518', '2532', '2480', '2478', '2431'] ['2541', '2549', '2614', '2700', '2647', '2746', '2690', '2621', '2550'] ['2525', '2525', '2640', '2769', '2802', '2883', '2856', '2694', '2631'] ['2514', '2505', '2526', '2614', '2717', '2715', '2867', '2836', '2771'] ['2506', '2482', '2480', '2528', '2518', '2561', '2586', '2662', '2654'] ['2527', '2477', '2464', '2459', '2452', '2475', '2480', '2500', '2518'] ['2544', '2505', '2488', '2454', '2442', '2445', '2446', '2467', '2470'] ['2528', '2486', '2464', '2446', '2434', '2436', '2442', '2444', '2450'] ['2464', '2505', '2482', '2456', '2433', '2463', '2462', '2489', '2467'] ['2532', '2541', '2519', '2515', '2496', '2502', '2529', '2519', '2553']] How do we determine the steepest path? We would need to compare the peak to each neighbor.

5) Find the steepest path down data = [['2537', '2483', '2475', '2480', '2518', '2532', '2480', '2478', '2431'] ['2541', '2549', '2614', '2700', '2647', '2746', '2690', '2621', '2550'] ['2525', '2525', '2640', '2769', '2802', '2883', '2856', '2694', '2631'] ['2514', '2505', '2526', '2614', '2717', '2715', '2867', '2836', '2771'] ['2506', '2482', '2480', '2528', '2518', '2561', '2586', '2662', '2654']] ... ] We will simplify this problem. Look at only three neighbors: up down front If peak is at location data[r][c], define each above.

5) Find the steepest path down data = [['2537', '2483', '2475', '2480', '2518', '2532', '2480', '2478', '2431'] ['2541', '2549', '2614', '2700', '2647', '2746', '2690', '2621', '2550'] ['2525', '2525', '2640', '2769', '2802', '2883', '2856', '2694', '2631'] ['2514', '2505', '2526', '2614', '2717', '2715', '2867', '2836', '2771'] ...] Compare and find the smallest of the three to create the next path element. What happens if there are ties?

5) Find the steepest path down Rules for ties. If up == down but < front, choose randomly between them. up = 2550 down = 2550 front = 2690 If front ties with up or down, choose front. up = 2690 up = 2550 down = 2550 down = 2690 front = 2550 front = 2550

5) Pseudocode for find_path initialize current location  (this is both a row and column) make an empty list for path while location is still within the list bounds assign up, front and down if (up < down and down < front) append up location to path else if (down < up and down < front) append down location to path else if (down == up and up < front) chose randomly between down and up append one of them to path else append front location to path update current location based on the chosen next location for path return path

6) Pseudocode for draw_path For each tuple in the path Using the column and row given in the tuple, draw a rectangle that is one pixel wide and filled in with yellow