More on Strings & Arrays

Similar documents
Arrays and Strings

Issue with Implementing PrimeSieve() in Go

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

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

Conditionals !

Methods. Every Java application must have a main method.

Arrays/Slices Store Lists of Variables

A Slice of Life

Topic 8: Lazy Evaluation

CSE 131 Introduction to Computer Science Fall Exam I

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

Maps /

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

Last Time: Rolling a Weighted Die

from scratch A primer for scientists working with Next-Generation- Sequencing data Chapter 1 Text output and manipulation

Fundamentals: Expressions and Assignment

Lecture 10: Lindenmayer Systems

Overview of List Syntax

Python Intro GIS Week 1. Jake K. Carr

Lecture 16: Object-oriented Programming

Repetition Structures

Python and Bioinformatics. Pierre Parutto

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

7 Control Structures, Logical Statements

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

Practice Question. Omar Khan / Atif Alvi. October 10, 2016

Python Class-Lesson1 Instructor: Yao

Expressions and Variables

Python 1: Intro! Max Dougherty Andrew Schmitt

CONTROL AND ENVIRONMENTS 1

UCT Algorithm Circle: Number Theory

CMSC201 Computer Science I for Majors

COMPSCI 230 Discrete Math Prime Numbers January 24, / 15

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

Sequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.

Introduction to Python

C ONTROL AND H IGHER O RDER F UNCTIONS

Prime Factorization. Jane Alam Jan. 1 P a g e Document prepared by Jane Alam Jan

Hello World. It is traditional to start with a Hello World program, though this version is a bit different:

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

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

CS 360 Programming Languages Interpreters

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

Strings. Genome 373 Genomic Informatics Elhanan Borenstein

Types, lists & functions

Algorithms Activity 6: Applications of BFS

Data Structures. Lists, Tuples, Sets, Dictionaries

Announcements For This Lecture

Functions & Variables !

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

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

CSc 120. Introduc/on to Computer Programing II. Adapted from slides by Dr. Saumya Debray. 01- a: Python review

1. Represent each of these relations on {1, 2, 3} with a matrix (with the elements of this set listed in increasing order).

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

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

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

Math 302 Introduction to Proofs via Number Theory. Robert Jewett (with small modifications by B. Ćurgus)

Lectures 5-6: Introduction to C

COMPUTER SCIENCE PAPER 1

Section Handout #5: Arrays

Variable initialization and assignment

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

Slicing. Open pizza_slicer.py

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Algorithmic Thinking: Computing with Lists

Exercise: Using Numbers

Lecture 5: Strings

Computing with Strings. Learning Outcomes. Python s String Type 9/23/2012

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

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

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

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

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Lesson 13: Exploring Factored Form

Beyond Blocks: Python Session #1

Conditionals & Loops /

Getting started with Java

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

Review 4. Lists and Sequences

Python. Karin Lagesen.

Structure and Interpretation of Computer Programs

Values and Variables 1 / 30

CS 253. January 14, 2017

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

Object-Oriented Design Lecture 13 CSU 370 Fall 2008 (Pucella) Friday, Oct 31, 2008

Algorithm Discovery and Design. Why are Algorithms Important? Representing Algorithms. Chapter 2 Topics: What language to use?

Fundamentals of Python: First Programs. Chapter 4: Strings (Indexing, Slicing, and Methods)

Intermediate/Advanced Python. Michael Weinstein (Day 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

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Logic Gates and Boolean Algebra ENT263

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

COMPUTER SCIENCE Paper 1

Scripting Languages. Python basics

Control Structure: Loop

COMP1730/COMP6730 Programming for Scientists. Strings

IT 1033: Fundamentals of Programming Loops

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

Transcription:

More on Strings & Arrays 02-201

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() // Count number of times that Pattern appears // in Text as a substring. func PatternCount(Text, Pattern string) int { count := 0 n := len(text) k := len(pattern) for i:=0; i<n-k+1; i++ { if Text[i:i+k] == Pattern { count++ return count

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 func ReverseComplement(s string) string { return Reverse(Complement(s)) 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

A Note on Range If s is an array or a string, then the for loop syntax for i:=0; i<len(s); i++ { //... is equivalent to for i:=range s { //...

Slices

Who First Computed Earth s Circumference?

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

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

Pseudocode for Sieve of Eratosthenes PrimeSieve(n) iscomposite ß array of n false boolean values biggestprime ß 2 while biggestprime < n for i ß 1 to n/biggestprime iscomposite[i*biggestprime] = true biggestprime++ while biggestprime < n and iscomposite[biggestprime] biggestprime++ return iscomposite Think: Is there anything that we can t do here in Go?

Issue with Implementing PrimeSieve() in Go func PrimeSieve(n int) [n+1]bool { var iscomposite [n+1]bool //ERROR! biggestprime := 2 for biggestprime < n for i:=1; i<=n/biggestprime; i++ { iscomposite[i+biggestprime] = true biggestprime++ for biggestprime<n &&iscomposite[biggestprime]{ biggestprime++ return iscomposite Go only allows us to create an array of constant size.