Strings: Strings? concatenate (join) + make multiple copies using *

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Def find_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count +=1 return(largest); print(find_largest(7,50))
Lists Introduction to Computing Science and Programming I.
Def f(x): counter = 0 y = 0; while counter
Def f(x): if (x == 1): return x else: return(x + f(x-1)) assertEqual(f(4),______) def f2(x): if (x == 1): return str(x) else: return(str(x) + f2(x-1))
Def f(x): counter = 0 y = 0; while counter
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Guide to Programming with Python
PPT 2. Other things: functions  Can you have a function with no inputs?  Yes: def f( ): return (3 + 4 )  Can you have a function with no outputs? 
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lecture 20 – Test revision COMPSCI 101 Principles of Programming.
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
PPT3. Quick function:  Write a function that checks to see if a number is even or not.  What TYPE does this return?
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
PPT 4. Variables: (assignment)  A variable: holds a value.  A space in RAM we give a name to  A lot like parameters, only we create them within the.
Strings:  Strings?  concatenate (join) +  make multiple copies using *  compare: > =
Largest of x random numbers def find_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count.
Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors in transcription or understanding. If there.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
EECS 110: Lec 10: Definite Loops and User Input Aleksandar Kuzmanovic Northwestern University
Chapter 10 Loops: while and for CSC1310 Fall 2009.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
LISTS: A NEW TYPE! We've got: ints floats booleans strings and now… (drum roll) LISTS! sets of a thing (e.g., sets of ints)
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
Input, Output and Variables GCSE Computer Science – Python.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
For loop: our last loop type
GCSE COMPUTER SCIENCE Practical Programming using Python
Python Basics.
String and Lists Dr. José M. Reyes Álamo.
Intro to CS Nov 21, 2016.
EECS 110: Lec 10: Definite Loops and User Input
4. Java language basics: Function
Python unit_4 review Tue/Wed, Dec 1-2
CSc 120 Introduction to Computer Programing II Adapted from slides by
Introduction to Python
Tuples and Lists.
Python - Lists.
CS-104 Final Exam Review Victor Norman.
Foundations of Programming: Arrays
Other things: functions
5. Function (2) and Exercises
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CS 115 Lecture 8 Structured Programming; for loops
CSC 108H: Introduction to Computer Programming
Structured Programming; for loops Taken from notes by Dr. Neil Moore
The CS 5 Black Post Penguins Invade Dormitory
Adapted from slides by Marty Stepp and Stuart Reges
Arithmetic operations, decisions and looping
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Chapter (3) - Looping Questions.
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Looping Topic 4.
Flowcharts and Pseudo Code
For loops Taken from notes by Dr. Neil Moore
What is printed out? def f(x): print(x+3) return(x + 7) def g(k): return(f(k) + 2) print(g(1)) 4 10 def f2(x): return(x + 7) print(x+3) def g2(k): return(f2(k)
Topics Sequences Lists Copying Lists Processing Lists
Python Basics with Jupyter Notebook
The structure of programming
Strings: What can we do with Strings?
More While Loops.
Lists: a new type! LISTS! sets of a thing (e.g., sets of ints)
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.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Introduction to Strings
More Basics of Python Common types of data we will work with
Presentation transcript:

Strings: Strings? concatenate (join) + make multiple copies using * compare: > < == >= <= != What else can we do?

A lot! Len, in def f(x): if "e" in x: return("e is in your message.") else: return("e is not in your message.") strvar = “puppies are cute” print(f(strvar)) z = len("cat")

Strings Positional Get a character at a particular index within a string stringvar1 = “ binary” Index 012345 stringvar2 = “ computer” Index 012 3 4 5 6 7 So to use a particular item in the list: x=stringvar2[3] #”p” y=stringvar1[1] #”i” z=stringvar2[0] #”c” w=stringvar1[6] #??? Example: def f(par): randnum = randrange(0,6) return(“you get “ + par[randnum]) print(f((stringvar1))

String: stringvar1 = “binary” Index 012345 stringvar2 = “ computer” get the length of a string len(stringvar1) # will give you 6, not 5! len(stringvar2) #will give you 8, not 7 Example: def f(par): y = len(par) randnum = randrange(0,y) #Why does this work? return(“you get “ + par[randnum]) print(f(stringvar2))

We can now do: def f(x,y): f(0,’kluge’) if (x == len(y)): else: return print(y[x]) return(f(x+1,y)) f(0,’kluge’)

Slicing (Different from Indexing) Copying parts of strings: 0 1 2 3 4 5 | p | i | z | z | a | -5 -4 -3 -2 -1 def f(word): return(word[2:5] ) print(f(“pizza”)) def g(): word = “pizza” return(word[1:3]) def h(): return(word[-4:-2]) def i(): return(word[-4:3])

Shortcuts 0 1 2 3 4 5 | p | i | z | z | a | -5 -4 -3 -2 -1 word=“pizza” word[0:4] pizz word[:4] word[2:5] zza word[2:]

# display a slice def g(s,f,wd): return("wd["+str(s)+":"+str(f)+"] is "+wd[s:f]) print(g(3,7,"sesquipedalian"))

Strings are Immutable word of the day Can: x = "catamaran" print(x.count("a")) print(x.index('a')) Can’t (if it changes the string, we can’t do it) x[3] = “y” #can’t do!

What does this do? def f(str1): str2 = “WuGmUmP” if str1.upper() == str2.upper(): return(“You’re one of us!”) else: return(“You’re not one of us!”) newstr = “wuGMuMp” print(f(newstr))

def rec(a,x,y): if (x == len(a)): return 0 elif (a[x] in y): return 1 + rec(a,x+1,y) else: return rec(a, x+1, y)   a = "catamaran" y = "aeiou" print(str(rec(a,0,y)))

def rec(a,x,y): if (x == len(a)): return "" elif (a[x] in y): return rec(a,x+1,y) else: return (a[x] + rec(a, x+1, y)) a = "catamaran" y = "aeiou" print(str(rec(a,0,y)))

def f(x,y,z): if y == len(x): return(z) else: if (x[y] == "p"): return(f(x,y+1,z+"m")) return(f(x,y+1,z+x[y])) print(f("puppy",0,""))

def f(x,a): if (x == len(a)//2): return a[x]==a[len(a)//2] else: if a[x] != a[len(a)-(x+1)]: return a[x] == a[len(a)-(x+1)] return(f(x+1,a)) print(f(0,"mom")) print(f(0,"mommy"))

def g(x,y,z,q): if (x == len(y)): return z else: if (y[x] not in " , def g(x,y,z,q): if (x == len(y)): return z else: if (y[x] not in " ,!.:;?"): if q: return(g(x+1,y,z+1,False)) return(g(x+1,y,z,False)) return(g(x+1,y,z,True)) print(g(0,"Hello there, how are you today?",0,True))

def h(x,y,z,q): if x == len(y): return 0 else: if y[x] in "\"'": if (q == y[x]): return(z) elif q != y[x]: return h(x+1,y,0,y[x]) return h(x+1,y,z+1,q) print(h(0,"He said, 'Houston, we have a problem' in the movie",0,'+'))

While Loop Look at this code: def ThreeYearOld(): x = "" while (x != "Because."): x = input("But why?") return("Oh. Okay.") print(ThreeYearOld()) Look at this code: What is our starting condition? What must be true in order for the loop to start? What will make the loop end? What must become false? Does something INSIDE the loop that changes so that eventually the loop will end? Does this all remind you of something?

Loops: While Loops: What does this code print out? def f(x, counter): while counter < x: print(counter) counter = counter + 1 return(counter) print(f(5, 0)) def f2(x,counter): if (counter >= x): return (counter) else: print(counter) return(f2(x,counter+1)) print(f2(5,0)) While Loops: Continue while condition is true Must initialize to make condition true In recursion we stop when the stopping contion is true – in while loops we stop when the while condition is false. Something inside the loop must change so that the condition becomes false eventually. What does this code print out?

While loops Starting condition (What must be true to enter loop)? def f(total,count): while count >= 1: #This is the part that loops total += count count = count -1 return(total) print(f(0, int(input("Enter a number")))) Starting condition (What must be true to enter loop)? What makes the loop stop? What inside the loop changes so that the loop will stop?

While loops Starting condition? What makes the loop stop? def f(total): while count >= 1: total += count count = count -1 return(total) print(f(0)) Starting condition? What makes the loop stop? What inside the loop changes so that the loop will stop?

While loops def f(total, count): while count >= 1: total += count count = count + 1 return(total) print(f(0, 4)) Starting condition? What makes the loop stop? What inside the loop changes so that the loop will stop?

Loops: Anything that can be done recursively can be done with a while loop (and vice versa) Certain problems work better with certain loops loop must have a True/False condition while count >= 1: While loops starts when the condition is True It continues while the condition is True It stops when the condition is False Something must change inside the loop that will eventually make the True/False condition False total += count count = count -1

Differences (syntactic): We can initialize variables used by the while loop inside the function (BUT BEFORE THE WHILE LOOP!) Why can’t we do this with recursion? def f(): x = 5 counter = 0 while counter < x: print(counter) counter = counter + 1 return(counter) print(f()) def f(x, counter): while counter < x: print(counter) counter = counter + 1 return(counter) print(f(5, 0)) In general, unless you know what you are doing, while loops should not call the function from within the function In general, unless you know what you are doing, recursive function should not use while loops inside the function.

Write a function that takes as an input parameter an integer and uses a while loop to print out "ha" that number of times. Starting condition? What makes the loop stop? (your True/False condition?) What inside the loop changes so that the loop will stop? def func(x): while (x > 0): print("ha ",end= " ") x -= 1 return func(5) def func(x): stringvar = "" while (x > 0): stringvar += "ha ") x -= 1 return(stringvar) print(func(5)) Recursive: def func(x): if x <= 0: return else: print("ha ") return(func(x-1)) func(5) Recursive: def func(x,stringvar): if x <= 0: return(stringvar) else: return(func(x-1,stringvar+"ha ")) print(func(5, ""))

What does this do? def w2(x,y): tot = 0 while (y > 0): tot += x y -= 1 return(tot) print(w2(3,5))

What about this? def g(y,z): x = len(y)-1 while x >= 0: z += y[x] x-=1 return z print(g("nilbog",""))

What does this do? def f(message): newmessage = "" x = 0 while x < len(message): if message[x] == "g": newmessage += "l" else: newmessage += message[x] x += 1 return(newmessage) print(f("pogysyggabicaggy“))

This one? def c2(x): y = 0 k = 0 while (x > 0): y += x%2 * 10**k k += 1 x = x//2 return(y) print(c2(10))

What does this one do? def w4(x, z): y = randrange(0,x) print(y) g = -1 #Why did I do this? while (g != y): g = randrange(0,x) print(g) z+= 1 print(z) return("It took " + str(z)) print(w4(10,0))

Try: exponent: Write a function that takes as input parameters 2 integers and uses a while loop to calculate x to the power of y (don’t use **) def c(x,y): z = 1; while (y > 0): z *= x y -= 1 return z

Write a while loop that converts a binary number to an integer def c3(x): y = 0 k = 0 while (x > 0): y += x%2 * 2 **k k+=1 x = x//10 return(y)

Versus: def z(ct): x = 0 while (ct >= 0): ct = ct - 1 x += ct return(x) print(z(5)) def z(ct): x = 0 while (ct >= 0): x += ct ct = ct - 1 return(x) print(z(5))

def f5(x,y): k = 0 v = "" while k < len(y): if y[k] not in x: v+=y[k] k+=1 print(v) z = True q = len(v) while k < q: if v[k] != v[q-k-1]: z = False return(z)   print(f5("?.,!","udel.edu"))

Largest of x random numbers Write a function that takes 2 integers (x and y) as input parameters , and generates x random numbers between 0 and y. It finds the largest of those random numbers. def find_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count +=1 return(largest); print(find_largest(7,50))

What do we get? def q(x,y): ct = 0 while (ct < x): ct2 = 0 while (ct2 < y): print(ct + ct2) ct2 += 1 ct += 1 return q(4,3) Rules: The contents of the while loop is only what is indented under the while loop We do the contents of the while loop over and over until the while loop’s condition becomes false. Only then do we do what is below the while loop.

What does this print out? def h(x): ct = 1 while (ct <= x): ct2 = ct while (ct2<=x): print(ct2, end=“\t") #prints a tab after every ct2 ct2 += 1 ct += 1 print("\n") #this is a line break (starts a new line) return h(5)

Can you reverse it def h(x): ct = 1 while (ct <= x): ct2 = 1 Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Output: 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5 def h(x): ct = 1 while (ct <= x): ct2 = ct while (ct2<=x): print(ct2, end=" ") ct2 += 1 ct += 1 print("\n") return h(5) def h(x): ct = 1 while (ct <= x): ct2 = 1 while (ct2<=ct): print(ct2, end=" ") ct2 += 1 ct += 1 print("\n") h(5)

What do we get? def g(a,b): while (a != b): while (a > b): a = a-b while(b>a): b = b-a return(a) print(g(6,20))

def f14(x): k = 0 n = -1 v = x while k <= (x def f14(x): k = 0 n = -1 v = x while k <= (x*2): m = 0 while (m < v): print(v, end = "") m += 1 if (v == 0): n = 1 else: print() v += n k+=1 f14(3)