Arrays/Slices Store Lists of Variables

Similar documents
Issue with Implementing PrimeSieve() in Go

Maps /

Arrays and Strings

From Last Time... Given a bacterial genome (~3 Mbp), where is ori?

A Slice of Life

Functions & Variables !

Lecture 9: Lists. Lists store lists of variables. Declaring variables that hold lists. Carl Kingsford, , Fall 2015

More on Strings & Arrays

Lecture Overview Methods and Interfaces Methods review Interfaces Example: using the sort interface Anonymous fields in structs

Summary of Go Syntax /

Conditionals !

Lecture 10: Lindenmayer Systems

Last Time: Rolling a Weighted Die

Fundamentals: Expressions and Assignment

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

GaE Graphs Ain t Easy. Andrew Jones (adj2129) Kevin Zeng (ksz2109) Samara Nebel (srn2134)

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

COMP520 - GoLite Type Checking Specification

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

GO IDIOMATIC CONVENTIONS EXPLAINED IN COLOR

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

What s the Difference?

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Manufactured Home Production by Product Mix ( )

CS558 Programming Languages

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

COMP520 - GoLite Type Checking Specification

Lecture 16: Object-oriented Programming

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

CS558 Programming Languages

CONTROL AND ENVIRONMENTS 1

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

Introduction to Bioinformatics

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

Information Science 1

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Exercise 6.2 A generic container class

COMP520 - GoLite Type Checking Specification

CSC Web Programming. Introduction to JavaScript

Information Science 1

Go for Java Developers

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

SWIFT - CLOSURES. Global Functions Nested Functions Closure Expressions. Have a name. Capture values from enclosing function

Conditionals & Loops /

CS 137 Part 2. Loops, Functions, Recursion, Arrays. September 22nd, 2017

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

Fun facts about recursion

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Exercise: Using Numbers

User-defined Functions. Conditional Expressions in Scheme

Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F

Lecture 15 CIS 341: COMPILERS

Lecture 22: Java. Overall Structure. Classes & Objects. Every statement must end with ';' Carl Kingsford, , Fall 2015

The Pyth Language. Administrivia

CS112 Lecture: Repetition Statements

. p.1/23. Today. 1. Questions and discussion from lecture. 2. Type-checking Functions Arrays Records (maybe)

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

CIS 110: Introduction to Computer Programming

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

Control Structures in Java if-else and switch

Types, Variables, and Constants

3 The Building Blocks: Data Types, Literals, and Variables

Functions & Variables /

Variables. Data Types.

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Multiple-Subscripted Arrays

1007 Imperative Programming Part II

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Lab 09: Advanced SQL

Example: Monte Carlo Simulation 1

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

CSCI Compiler Design

Guerrilla Section 7: Macros, SQL SQL

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

SFU CMPT Topic: Control Statements

Testing, Debugging, and Verification

CSCE 110: Programming I

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Induction and Semantics in Dafny

Types, Expressions, and States

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Repetition, Looping CS101

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Functional Programming in Haskell Part I : Basics

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Transcription:

Maps 02-201

Arrays/Slices Store Lists of Variables H i T h e r e! 0 1 2 3 4 5 6 7 8 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 ACG TTA GAG CCT TAA GGG CAT 0 1 2 3 4 5 6

What if Indices Aren t Integers? key value California 38,802,500 Texas 26,956,958 Florida 19,893,297 New York 19,746,227 Illinois 12,880,580 Pennsylvania 12,787,209 Ohio 11,594,163 Georgia 10,097,343 North Carolina 9,943,964 Michigan 9,909,877

Maps: The Go Data Structure We Want Recall: slice declaration: var a []int a = make([]int, 10)

Maps: The Go Data Structure We Want Recall: slice declaration: var a []int a = make([]int, 10) Now: map declaration: var statepop map[string]int statepop = make(map[string]int) // note: no need to specify length of map statepop[ Pennsylvania ] = 12787209

Shortcut Declarations Recall: shortcut slice declaration: a := make([]int, 10)

Shortcut Declarations Recall: shortcut slice declaration: a := make([]int, 10) Now: shortcut map declaration: statepop := make(map[string]int)

Adding Items to a Map Recall: appending items to end of a slice b := make([]int, 0) b = append(b, 23)

Adding Items to a Map Recall: appending items to end of a slice b := make([]int, 0) b = append(b, 23) Now: can assign map values directly. statepop := make(map[string]int) statepop[ Pennsylvania ] = 12787209

Number of Items in Map Recall: number of items in slice: a := make([]int, 10) fmt.println(len(a)) // prints 10

Number of Items in Map Recall: number of items in slice: a := make([]int, 10) fmt.println(len(a)) // prints 10 Now: number of items in map: statepop := make(map[string]int) statepop[ Pennsylvania ] = 12,787,209 fmt.println(len(statepop)) // prints 1

Removing an Item from a Map Recall: removing item i from a slice: a = append(a[:i], a[i+1:]...)

Removing an Item from a Map Recall: removing item i from a slice: a = append(a[:i], a[i+1:]...) Now: removing key and value from a map: delete(statepop, Florida ) // deletes both Florida key and the // population value it refers to.

Looping through Maps Recall: looping through slices with two indices: for j, v := range list { fmt.println( The value at, j, is, v)

Looping through Maps Recall: looping through slices with two indices: for j, v := range list { fmt.println( The value at, j, is, v) Now: looping through map proceeds similarly: for j, v := range statepop { fmt.println( The pop of, j, is, v)

Looping through Maps Recall: looping through slices with two indices: for j, v := range list { fmt.println( The value at, j, is, v) Now: looping through map proceeds similarly: for j, v := range statepop { fmt.println( The pop of, j, is, v) Think: In what order do you think the states print?

Looping through Maps Recall: looping through slices with two indices: for j, v := range list { fmt.println( The value at, j, is, v) Now: looping through map proceeds similarly: for j, v := range statepop { fmt.println( The pop of, j, is, v) Note: ordering of map keys doesn t follow clear pattern.

Map Literals Recall: array and slice literals var a = [4]float64{3.2, -30.0, 84.71, 62.3 var prime = []int{2, 3, 5, 7, 11

Map Literals Recall: array and slice literals var a = [4]float64{3.2, -30.0, 84.71, 62.3 var prime = []int{2, 3, 5, 7, 11 Now: map literals charskew := map[byte]int { A : 0, C : -1, G : 1, T : 0, // the last comma is important!

Map Literals func SkewArray(s string) []int{ var a []int a[0] = 0 for i := range s { a = append(a, a[i-1]+charskew(s[i])) return a charskew := map[byte]int { A : 0, C : -1, G : 1, T : 0, // the last comma is important!

Returning to Finding Frequent Words FrequentWords(Text, k) FrequentPatterns ß an empty list c ß empty array of length Text - k for i ß 0 to Text - k Pattern ß Substring(Text, i, k) c[i] ß Count(Text, Pattern) maxcount ß Max(a) for i ß 0 to Text - k if c[i] = maxcount add Substring(Text, i, k) to FrequentPatterns FrequentPatterns ß RemoveDuplicates(FrequentPatterns) return FrequentPatterns Why not rewrite with a map instead of an array?

Rewriting Frequent Words Pseudocode BetterFrequentWords(Text, k) FrequentPatterns ß an empty list Freq ß empty map for i ß 0 to Text - k Pattern ß Substring(Text, i, k) if Freq[Pattern]!exists Freq[Pattern] = 1 else Freq[Pattern]++ maxcount ß Max(Freq) for all patterns Pattern in Freq if Freq[Pattern] = maxcount add Pattern to FrequentPatterns return FrequentPatterns Note: We don t need RemoveDuplicates() or Count()!

Returning to BetterFrequentWords() BetterFrequentWords(Text, k) FrequentPatterns ß an empty list Freq ß empty map for i ß 0 to Text - k Pattern ß Substring(Text, i, k) if Freq[Pattern]!exists Freq[Pattern] = 1 else Freq[Pattern]++ maxcount ß Max(Freq) for all patterns Pattern in Freq if Freq[Pattern] = maxcount add Pattern to FrequentPatterns return FrequentPatterns Exercise: Write a Go function taking a map of strings to ints as input and returning the max value in the map.

Returning to BetterFrequentWords() BetterFrequentWords(Text, k) FrequentPatterns ß an empty list Freq ß empty map for i ß 0 to Text - k Pattern ß Substring(Text, i, k) if Freq[Pattern]!exists Freq[Pattern] = 1 else Freq[Pattern]++ maxcount ß Max(Freq) for all patterns Pattern in Freq if Freq[Pattern] = maxcount add Pattern to FrequentPatterns return FrequentPatterns Next: let s focus on implementing the code in red.

Returning to BetterFrequentWords() BetterFrequentWords(Text, k) FrequentPatterns ß an empty list Freq ß FrequencyMap(Text, k) maxcount ß Max(Freq) for all patterns Pattern in Freq if Freq[Pattern] = maxcount add Pattern to FrequentPatterns return FrequentPatterns

Checking if a Map Contains a Key: Method 1 _, exists := Freq[Pattern] // exists is a boolean value that is equal // to false if Freq[Pattern] doesn t exist if!exists { Freq[Pattern] = 1 else { Freq[Pattern]++

Checking if a Map Contains a Key: Method 2 Freq[Pattern]++ // this will automatically tell Go that we // need a key = Pattern (with default value // 0), and then immediately increment it

Returning to BetterFrequentWords() BetterFrequentWords(Text, k) FrequentPatterns ß an empty list Freq ß empty map of length Text - k for i ß 0 to Text - k Pattern ß Substring(Text, i, k) if Freq[Pattern]!exists Freq[Pattern] = 1 else Freq[Pattern]++ maxcount ß Max(Freq) for all patterns Pattern in Freq if Freq[Pattern] = maxcount add Pattern to FrequentPatterns return FrequentPatterns

Returning to BetterFrequentWords() BetterFrequentWords(Text, k) FrequentPatterns ß an empty list Freq ß FrequencyMap(Text, k) maxcount ß Max(Freq) for all patterns Pattern in Freq if Freq[Pattern] = maxcount add Pattern to FrequentPatterns return FrequentPatterns Exercise: Write a Go function implementing FrequencyMap(Text, k).

Implementing FrequentWords() in Go func FrequentWords(Text string, k int) []string { freqpatterns := make([]string, 0) freq := FrequencyMap(Text, k) m := Max(freq) for pattern, val := range freq { if val == m { freqpatterns = append(freqpatterns, pattern) return freqpatterns

Cataloguing Multiple Genomes Think: Say that you would like to store frequency maps for the replication origins of 1,000 different bacterial genomes. One way is to have a separate frequency map for each genome. How could we consolidate all of this information into a single data structure?

Map of Maps: Mental Image Key Bacterium A! Bacterium B! Bacterium C! Bacterium D! Value!!! Key Value ATGCACGCT 8! GGACGTACG 1! GTACGACAG 2! ATAAATTGC 3! GATACCAGA 2! ATAGGATCC 6! GGATATCCC 3! Bacterium E!! Bacterium F!! Bacterium G!!

Recall: Creating a 2-D Slice 2-D slices are also slices of slices we must define the outer slice first. var field [][]bool = make([][]bool, m) To initialize the slices in field, write an explicit loop. for row := range field { field[row] = make([]bool, n) m rows 0 1 2 3 4 5 6 n columns 0 1 2 3

Map of Maps in Go // say we have a slice of strings genomes // containing the bacterial genomes. database := make(map[string]map[string]int) // create initial 2-D map for bact := range genomes { // create map for each bacterium database[bact] = make(map[string]int) // map bacterium to its frequency map database[bact] = FrequentWords(bact, k)