Arrays and Strings

Similar documents
More on Strings & Arrays

Issue with Implementing PrimeSieve() in Go

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

Arrays/Slices Store Lists of Variables

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

Functions & Variables !

A Slice of Life

Last Time: Rolling a Weighted Die

Overview of List Syntax

More Examples /

Homework 6: Spatial Games Due: 11:59pm on Friday, October 30

Conditionals !

// initialize array to true. for (i = 0; i < s; i++) f[i] = true; // get rid of known non-primes f[0] = f[1] = false;

Summary of Go Syntax /

Conditionals & Loops /

Variables, Constants, and Data Types

Review 4. Lists and Sequences

The type of all data used in a C (or C++) program must be specified

Lecture 10: Lindenmayer Systems

Homework 5: Spatial Games : Programming for Scientists Due: Thursday, March 3, 2016 at 11:59 PM

Introduction to Programming, Aug-Dec 2006

Exercise: Using Numbers

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

Basic Operations jgrasp debugger Writing Programs & Checkstyle

COMP-520 GoLite Tutorial

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

Basics of Java: Expressions & Statements. Nathaniel Osgood CMPT 858 February 15, 2011

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

The type of all data used in a C++ program must be specified

CS 61A Discussion 8: Scheme. March 23, 2017

The current topic: Python. Announcements. Python. Python

Problem Solving for Intro to Computer Science

Getting started with Java

CS 115 Lecture 4. More Python; testing software. Neil Moore

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

The Big Python Guide

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

Values and Variables 1 / 30

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives


Computer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

Intro. Scheme Basics. scm> 5 5. scm>

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Bits. Binary Digits. 0 or 1

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Lecture 2: Variables & Assignments

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

Programming to Python

Review of Important Topics in CS1600. Functions Arrays C-strings

Variables and Data Representation

Math 15 - Spring Homework 5.2 Solutions

(Refer Slide Time: 00:23)

Pace University. Fundamental Concepts of CS121 1

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

GO IDIOMATIC CONVENTIONS EXPLAINED IN COLOR

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Perl for Biologists. Session 2 March 19, Constants, variables and functions. Jaroslaw Pillardy

ENGR 102 Engineering Lab I - Computation

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

Lecture 16: Object-oriented Programming

Python for Non-programmers

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

2.1 Indefinite Loops. while <condition>: <body> rabbits = 3 while rabbits > 0: print rabbits rabbits -= 1

GO SHORT INTERVIEW QUESTIONS EXPLAINED IN COLOR

Go Tutorial. To do. A brief, gentle intro to Go. Next Networking. q Today

Python Class-Lesson1 Instructor: Yao

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

VARIABLES AND TYPES CITS1001

Programming Languages 3. Definition and Proof by Induction

Java+- Language Reference Manual

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Go Cheat Sheet. Operators. Go in a Nutshell. Declarations. Basic Syntax. Hello World. Functions. Comparison. Arithmetic. Credits

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

CMPT 125: Lecture 3 Data and Expressions

Python Working with files. May 4, 2017

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Algorithmic Thinking: Computing with Lists

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

Python and Bioinformatics. Pierre Parutto

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

Slicing. Open pizza_slicer.py

Python Intro GIS Week 1. Jake K. Carr

cs1114 REVIEW of details test closed laptop period

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

1 Dynamic Programming

Teaching London Computing

Basic data types. Building blocks of computation

Language Reference Manual

Transcription:

Arrays and Strings 02-201

Arrays

Recall: Fibonacci() and Arrays 1 1 2 3 5 8 13 21 34 55 a Fibonacci(n) a ß array of length n a[1] ß 1 a[2] ß 1 for i ß 3 to n a[i] ß a[i-1] + a[i-2] return a

Declaring Arrays var a [10]int var b [100]string var c [20]float64 Declare arrays of the given type and length. array elements:! index of array:! 13 18-2 10 11 10-22 8 8 7-30 -33-22 0 1 2 3 4 5 6 7 8 9 10 11 12

Accessing/Changing Array Elements in Go Array elements can be accessed by putting their index between [ ] following the array name: fmt.println(a[7],a[8]) a[0] = 10 b[30] = hi there i := 12 + 2 c[i] = 3.1 c[2*i] = c[i] x[i] can appear on left side of assignment to set a value.

Accessing/Changing Array Elements in Go The length of an array can be found with len(x), where x is an array. Array indices start at 0. The first element is x[0]; last element is x[len(x) - 1]. It s an error to try to access elements past the end (or before the beginning) of the array: var d [100]int d[len(d)-1] = 3 d[len(d)] = 3 d[-60] = 7 // OK // ERROR! // ERROR!

Command Line Arguments with os.args HW2: enter command line information that is input as parameters into a function directly. Package os provides access to these parameters: go run myprogram.go a 3 77 another param os.args is an array of strings. os.args[1] is a os.args[2] is 3 os.args[3] is 77 os.args[4] is another param os.args[0] is myprogram

Exiting a Program with os.exit() if terminating because of an error, use os.exit(1). if terminating normally, use os.exit(0). func RandomWalk(n, s int) { if n <= 0 { fmt.println( n must be positive! ) os.exit(1) } /* Rest of program here */ } Think: How can we make sure that we have the right number of input parameters to a program?

Multidimensional Arrays as 2-D Tables var a [7][4]int a[1][2] = 19 y := a[3][0] // gives 0 x := len(a) // gives 7 Think of as an array of length 7, where each element contains an array of length 4 7 rows 0 1 2 3 4 5 6 4 columns 0 1 2 3

Multidimensional Arrays as 2-D Tables var a [7][4]int a[1][2] = 19 y := a[3][0] // gives 0 x := len(a) // gives 7 Think of as an array of length 7, where each element contains an array of length 4 7 rows 0 1 2 3 4 5 6 4 columns 0 1 2 3 Think: How can we determine the number of columns?

Multidimensional Arrays as 2-D Tables var a [7][4]int a[1][2] = 19 y := a[3][0] // gives 0 x := len(a) // gives 7 Think of as an array of length 7, where each element contains an array of length 4 7 rows 0 1 2 3 4 5 6 4 columns 0 1 2 3 z := len(a[0]) // gives 4

Strings

String Conversion Pain Exercise: What should be the value of MJordan? MJordan := string(45)

String Conversion Pain Exercise: What should be the value of MJordan? MJordan := string(45) Answer: It seems like it should be 45. But the answer is -.

How Strings are Represented Internally Logical interpretation Internal representation H e l l o, W o r l d! 72 101 108 108 111 44 32 87 111 114 108 100 33 Each item in a string is called a character. A character in Go is represented using a byte variable type that is a number.

ASCII Chart (You don t need to know these numbers) Binary Dec Glyph 110 0000 96 ` 110 0001 97 a 110 0010 98 b 110 0011 99 c 110 0100 100 d 110 0101 101 e 110 0110 102 f 110 0111 103 g 110 1000 104 h 110 1001 105 i 110 1010 106 j 110 1011 107 k 110 1100 108 l 110 1101 109 m 110 1110 110 n 110 1111 111 o 111 0000 112 p 111 0001 113 q 111 0010 114 r 111 0011 115 s 111 0100 116 t 111 0101 117 u 111 0110 118 v 111 0111 119 w 111 1000 120 x 111 1001 121 y 111 1010 122 z 111 1011 123 { 111 1100 124 111 1101 125 } 111 1110 126 ~ Binary Dec Glyph 100 0000 64 @ 100 0001 65 A 100 0010 66 B 100 0011 67 C 100 0100 68 D 100 0101 69 E 100 0110 70 F 100 0111 71 G 100 1000 72 H 100 1001 73 I 100 1010 74 J 100 1011 75 K 100 1100 76 L 100 1101 77 M 100 1110 78 N 100 1111 79 O 101 0000 80 P 101 0001 81 Q 101 0010 82 R 101 0011 83 S 101 0100 84 T 101 0101 85 U 101 0110 86 V 101 0111 87 W 101 1000 88 X 101 1001 89 Y 101 1010 90 Z 101 1011 91 [ 101 1100 92 \ 101 1101 93 ] 101 1110 94 ^ 101 1111 95 _ Binary Dec Glyph 010 0000 32 (space) 010 0001 33! 010 0010 34 " 010 0011 35 # 010 0100 36 $ 010 0101 37 % 010 0110 38 & 010 0111 39 ' 010 1000 40 ( 010 1001 41 ) 010 1010 42 * 010 1011 43 + 010 1100 44, 010 1101 45-010 1110 46. 010 1111 47 / 011 0000 48 0 011 0001 49 1 011 0010 50 2 011 0011 51 3 011 0100 52 4 011 0101 53 5 011 0110 54 6 011 0111 55 7 011 1000 56 8 011 1001 57 9 011 1010 58 : 011 1011 59 ; 011 1100 60 < 011 1101 61 = 011 1110 62 > 011 1111 63?

Restoring Sanity: strconv Package

Restoring Sanity: strconv Package MJordan := strconv.itoa(45) // gives 45 x, err := strconv.atoi( 107") //gives 107 if err!= nil { os.exit(1) } y, err := strconv.parsefloat( 3.1, 64) if err!= nil { os.exit(1) } Check out FormatFloat() for float to string conversion.

strings Package Contains many built-in string algorithms. Example: testing whether a string contains another: var a = hi, there var b = the if strings.contains(a, b) { fmt.println( String a contains string b! ) }

Recall: String + Operator s1 + s2 concatenate strings s1 and s2! hi + there == hithere what s + up + doc? == what s up doc?

Indexing Strings Strings work like arrays in some ways: Strings have fixed length. You can find the length of string s with len(s). You can access elements of string s with s[i]. s := Hi There! fmt.println(s[0]) // prints H fmt.println(s[len(s)-1]) // prints! if s[0] == H // single quotes for single symbols H i T h e r e! 0 1 2 3 4 5 6 7 8

Substrings in Go s := Hi There! fmt.println(s[3:5]) // prints Th fmt.println(s[1:]) // prints i There! fmt.println(s[:4]) // prints Hi T var str string = s[3:6] fmt.println(str) fmt.println(str[0]) // prints The // prints T H i T h e r e! 0 1 2 3 4 5 6 7 8

Recall: Counting Pattern in Text 1. Start from the first position of Text and check whether Pattern appears in Text starting at its first position. 2. If yes, draw a dot on a piece of paper. 3. Move to the second position of Text and check whether Pattern appears in Text starting at its second position. 4. If yes, draw another dot on the same piece of paper. 5. Continue until you reach the end of Text. 6. Count the number of dots on the piece of paper.

Example: Implement PatternCount() Pattern Counting Problem: Count the number of times that a pattern appears in a longer string. Input: Strings Text and Pattern. Output: The number of times that Pattern appears in Text. fmt.println(strings.count( ATATA, ATA )) // prints 1, not 2. Exercise: Write a PatternCount() function handling overlapping instances of Pattern solving this problem.

Example: Implement PatternCount() func PatternCount(Pattern, Text string) int { //fill in details here }

Example: Reverse Complementing a String Reverse Complement Problem: Input: A DNA string Text. Output: The reverse complement of Text. 5 3 A G T C G C A T A G T T C A G C G T A T C A 3 5 Think: How could we solve this problem top-down?

Example: Reverse Complementing a String ReverseComplement() Complement() Reverse()

(Unlike Arrays) Strings are Not Editable! func Complement(s string) string { for i:=0; i<len(s); i++ { switch s[i] { case 'A': s[i] = 'T //ERROR! case 'T': s[i] = 'A //ERROR! case 'C': s[i] = 'G //ERROR! case 'G': s[i] = 'C //ERROR! } } return s }

Reverse Complementing a String func Complement(s string) string { s2 := "" for i:=0; i<len(s); i++ { switch s[i] { case 'A': s2 += 'T // type! case 'T': s2 += 'A // type! case 'C': s2 += 'G // type! case 'G': s2 += 'C // type! } } return s2 }

Reverse Complementing a String func Complement(s string) string { s2 := "" for i:=0; i<len(s); i++ { switch s[i] { case 'A': s2 += T case 'T': s2 += A case 'C': s2 += G case 'G': s2 += C } } return s2 } Exercise: Write a Reverse() function reversing a string.

Reverse Complementing a String func Reverse(s string) string { s2 := n := len(s) for i:=0; i<n; i++ { s2 += string(s[n-i-1]) } return s2 }

Slices

Last Time: Random Walk Input: Integers n and s. Output: Print each step of a random walk with s steps in an n x n chessboard.

Self-Avoiding Random Walks Self-avoiding walk: that never visits the same square of the checkerboard twice. Input: Integers n and s. Output: Print each step of a random self-avoiding walk with s steps in an n x n chessboard (start in the middle). Exercise: Write pseudocode for a function that prints a random self-avoiding walk.

Pseudocode for Random Self-Avoiding Walk SelfAvoidingRandomWalk(n, s) field ß n x n boolean array holding all false (x, y) ß (n/2, n/2) field[x][y] ß true Print(x, y) for i ß 0 to (steps 1) (xnext, ynext) = (x, y) while field[xnext][ynext] (xnext, ynext) = RandomStep(x, y, 10) (x, y) = (xnext, ynext) field[x][y] = true Print(x,y) Note the code reuse of RandomStep()...

Just Two Little Problems 1. Corner case: If the path works itself into a corner with nowhere to go, the algorithm will have an infinite loop! 2. In Go, arrays cannot have length equal to a variable. func SelfAvoidingRandomWalk(n, s int) { var field [n][n]bool //throws error! //... blah }

Who First Computed Earth s Circumference?

Eratosthenes of Cyrene (276 195 BC) Also: first algorithm for identifying prime numbers

Finding Primes Prime Number Problem: Input: An integer n. Output: All integers p < n such that p is prime.

Sieve of Eratosthenes https://en.wikipedia.org/wiki/sieve_of_eratosthenes#/media/file:sieve_of_eratosthenes_animation.gif!

Sieve of Eratosthenes Prime Number Problem: Input: An integer n. Output: All integers p < n such that p is prime. Exercise: Write a program in pseudocode that takes n as input and returns an array iscomposite such that: iscomposite[i] = true if i is composite iscomposite[i] = false if i is prime

Fill in Details Later PrimeSieve(n) iscomposite ß array of n false boolean values biggestprime ß 2 while biggestprime < n func primesieve(iscomposite []bool) { var biggestprime = 2 for biggestprime < len(iscomposite) { for i := 2*biggestPrime; i < len(iscomposite); i += biggestprime { iscomposite[i] = true } biggestprime++ for biggestprime < len(iscomposite) && iscomposite[biggestprime] { biggestprime++ } } }

Strings

Need to Implement: Substring() and Count() 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 a[i] = maxcount add Substring(Text, i, k) to FrequentPatterns FrequentPatterns ß RemoveDuplicates(FrequentPatterns) return FrequentPatterns

String + Operator s1 + s2 concatenate strings s1 and s2! hi + there == hithere what s + up + doc? == what s up doc?

Indexing Strings Strings work like arrays in some ways: Strings have fixed length. You can find the length of string s with len(s). You can access elements of string s with s[i]. You cannot modify a string once it has been created. s := Hi There! fmt.println(s[0]) // prints H fmt.println(s[len(s)-1]) // prints! s[3] = t // ERROR! Can t assign to strings if s[0] == H //note the single quotes for individual symbols.

Substrings in Go s := Hi There! fmt.println(s[3:5]) // prints Th fmt.println(s[1:]) // prints i There! fmt.println(s[:4]) // prints Hi T var str string = s[3:6] fmt.println(str) fmt.println(str[0]) // prints The // prints T H i T h e r e! 0 1 2 3 4 5 6 7 8

Recall: Counting Pattern in Text 1. Start from the first position of Text and check whether Pattern appears in Text starting at its first position. 2. If yes, draw a dot on a piece of paper. 3. Move to the second position of Text and check whether Pattern appears in Text starting at its second position. 4. If yes, draw another dot on the same piece of paper. 5. Continue until you reach the end of Text. 6. Count the number of dots on the piece of paper.

Example: Implement Count() Pattern Counting Problem: Count the number of times that a pattern appears in a longer string. Input: Strings Text and Pattern. Output: The number of times that Pattern appears in Text. Note: our Count() function should handle overlaps (unlike strings.count()). Think: do you see why strings is before Count()? Exercise: Write a Count() function in Go solving this problem.

Example: Reverse Complementing a String Reverse Complement Problem: Input: A DNA string Text. Output: The reverse complement of Text. 5 3 A G T C G C A T A G T T C A G C G T A T C A 3 5 Exercise: How could we solve this? What are the barriers?

Strings are Not Editable! func Complement(s string) string { for i:=0; i<len(s); i++ { switch s[i] { case 'A': s[i] = 'T //ERROR! case 'T': s[i] = 'A //ERROR! case 'C': s[i] = 'G //ERROR! case 'G': s[i] = 'C //ERROR! } } return s }

Self-Avoiding Random Walks Self-avoiding walk: a walk that never visits the same square of the checkerboard twice. Input: Integers n and s. Output: Print each step of a random self-avoiding walk with s steps in an n x n chessboard (start in the middle).

Pseudocode for Random Self-Avoiding Walk SelfAvoidingRandomWalk(n, s) field ß n x n boolean array holding all false (x, y) ß (n/2, n/2) field[x][y] ß true Print(x,y) for i ß 0 to (steps 1) (xnext, ynext) = (x, y) while field[xnext][ynext] (xnext, ynext) = RandomStep(x, y, 10) (x, y) = (xnext, ynext) field[x][y] = true Print(x,y) Note the code reuse of RandomStep()...

Just Two Little Problems 1. Corner case: If the path works itself into a corner with nowhere to go, the algorithm will have an infinite loop! 2. In Go, arrays cannot have length equal to a variable. func SelfAvoidingRandomWalk(n, s int) { var field [n][n]bool //throws error! //... blah }

Let s Next Look at Max() 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 a[i] = maxcount add Substring(Text, i, k) to FrequentPatterns FrequentPatterns ß RemoveDuplicates(FrequentPatterns) return FrequentPatterns

Issue #2: Arrays Copied in Function Calls func Max(A [10000000]int) int { m := 0 for i := range A { if A[i] > m { m = A[i] } } return m } func main() { var numbers [10000000]int } // fill numbers with random integers for i := range numbers { numbers[i] = rand.int() } fmt.println(max(numbers)) A new array A is created and the contents from numbers is copied over (wasteful of memory).

(See Burrows-Wheeler Transform Slides)

Slices

Slices: Everything Arrays in Go Should Be A slice variable is declared by not specifying a size in [] var a []int // at this point a has the special value nil // and can t be used as an array a = make([]int, 10, 20) Creates an array of size 20 with a slice of size 10 inside it. 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 1! 2! 3! 4! 5! 6! 7! 8! 9! 10! 11! 12! 13! 14! 15! 16! 17! 18! 19! Length of this slice is 10! Underlying array has size 20!

Omitting the Third Parameter var a []int a = make([]int, 10) // this is the same as make([]int, 10, 10)

We Can Still Change Items var a []int a = make([]int, 10) a[3] = -18

Passing a Variable as Slice Length var n int = 5050 var a []string a = make([]string, n) // this is OK!

Defining a Slice in One Line var n int = 100 b := make([]string, n) // this is OK!

Recall: Literals... The Other Side of = Integer literals: a sequence of digits 0 9 72 6402 000734 String literals: a sequence of characters between Hi there! 1+ =4 3.14159 Unicode strings are supported. bool (Boolean) literals: either true or false true false

Array and Slice Literals Can also write slice literals (useful if you have a short list of data that may change):! var a = []float64{3.2, -30, 84, 62} var prime = []int{2,3,5,7,11}

Array and Slice Literals Can also write slice literals (useful if you have a short list of data that may change):! var a = []float64{3.2, -30, 84, 62} var prime = []int{2,3,5,7,11} We can also use array literals (useful for a short list of data that is fixed):! var a = [3]float64{3.2, -30.0, 84.134} var prime = [5]int{2,3,5,7,11}

The append() Operation If we want to make a slice bigger by adding something to the end of it... s := make([]int, 10) 0! 0! 0! 0! 0 0! 0! 0! 0 0! 0! 1! 2! 3! 4! 5! 6! 7! 8! 9!

The append() Operation If we want to make a slice bigger by adding something to the end of it... s := make([]int, 10) s = append(s, 5) 0! 0! 0! 0! 0 0! 0! 0! 0 0! 5 0! 1! 2! 3! 4! 5! 6! 7! 8! 9! 10