COMPSCI 101 Principles of Programming

1 / 18

COMPSCI 101 Principles of Programming - PowerPoint PPT Presentation

COMPSCI 101 Principles of Programming. Lecture 21 – Complex String Processing. Learning outcomes. At the end of this lecture, students should be able to: Write code using more complex boolean operators Write code transforming strings. Review. Strings and Lists are Sequences

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

PowerPoint Slideshow about 'COMPSCI 101 Principles of Programming' - evers

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
COMPSCI 101Principles of Programming

Lecture 21 – Complex String Processing

Learning outcomes
• At the end of this lecture, students should be able to:
• Write code using more complex boolean operators
• Write code transforming strings

COMPSCI 101 - Principles of Programming

Review
• Strings and Lists are Sequences
• Test Cases are Important
• Boolean Operators:and, or and not

COMPSCI 101 - Principles of Programming

Exercise
• Write a function named who_wins() that accepts the guesses for person1 and person2 as parameters and determines who wins “Rock Paper Scissors” and returns “person1”, “person2” or “tie”

>>> who_wins("rock","scissors")

'person1'

>>> who_wins("rock","paper")

'person2'

>>> who_wins("paper","paper")

'tie'

COMPSCI 101 - Principles of Programming

defwho_wins(person1_choice,person2_choice):

if (person1_choice == "rock" and

person2_choice == "scissors") or

(person1_choice == "scissors" and

person2_choice == "paper") or

(person1_choice == "paper" and

person2_choice == "rock"):

return "person1"

if person1_choice == person2_choice:

return "tie"

return "person2”

COMPSCI 101 - Principles of Programming

Exercise
• Write a program that calls a function named poetry()that returns random poetry of the form Noun Verb Noun

>>> poetry()

'friend loves friend'

>>> poetry()

'dog hates cat'

COMPSCI 101 - Principles of Programming

import random

def poetry():

noun1 = random.randint(0,5)

verb = random.randint(0,3)

noun2 = random.randint(0,5)

nouns = ["friend", "dog", "cat", "rabbit", "girl", "boy"]

verbs=["loves","hates","likes","is afraid of"]

return nouns[noun1] + " " + verbs[verb] + " " +

nouns[noun2]

poetry()

COMPSCI 101 - Principles of Programming

Exercise
• Write a function named remove_punctuation() that accepts a string containing a written fragment and returns the fragment with all the punctuation removed.

>>> remove_punctuation("'I know the answer! The answer lies within the heart of all mankind! The answer is 12? I think I'm in the wrong building.' (Charles Schulz, 'Peanuts')")

'I know the answer The answer lies within the heart of all mankind The answer is 12 I think Im in the wrong building Charles Schulz Peanuts'

COMPSCI 101 - Principles of Programming

defremove_punctuation(sentences):

new_sentences= ""

punctuation = ["'", "!", ".", "(", ")", ",", "?", ":", '"']

for character in sentences:

if character not in punctuation:

new_sentences += character

return new_sentences

COMPSCI 101 - Principles of Programming

Exercise
• Write a function namedisogram() that accepts a word as a parameter and returns True if it is an isogram and False otherwise.
• An isogram, sometimes known as a nonpattern word, is a word or phrase in which every letter occurs the same number of times.

>>> isogram("subdermatoglyphic")

True

>>> isogram("deed")

True

>>> isogram("sara")

False

COMPSCI 101 - Principles of Programming

def isogram(word):

letter = word[0]

num = count(letter,word)

for i in range(1,len(word)):

if num != count(word[i],word):

return False

return True

def count(letter,word):

count = 0

for letter2 in word:

if letter == letter2:

count = count + 1

return count

COMPSCI 101 - Principles of Programming

Exercise
• Write a function named semordinlap() that accepts a word as a parameter and returns True if it is a semordinlap and False otherwise.
• A semordnilapis a word which when reversed is another valid word

>>> semordnilap("dog")

True

>>> semordnilap("dan")

False

COMPSCI 101 - Principles of Programming

defsemordnilap(word):

dictionary_file= open("unixdict.txt", "r")

dictionary_list = dictionary.split()

if word in dictionary_list and

reverse(word) in dictionary_list:

return True

return False

def reverse(word):

new_word = ""

for i in range(len(word)-1,-1,-1):

new_word += word[i]

return new_word

COMPSCI 101 - Principles of Programming

Exercise
• Write a function named change_case() that accepts a sentence and returns the same sentence in all uppercase or all lowercase, without using the methods upper() or lower().

>>> change_case("pApEr","upper")

'PAPER'

>>> change_case("PapeR","lower")

'paper'

>>> change_case("Can we dO a WHoleSentence","upper")

'CAN WE DO A WHOLE SENTENCE'

COMPSCI 101 - Principles of Programming

defchange_case(sentence,my_type):

uppercase = ["A", "B", "C", "D", "E", "F", "G", "H", "I",

"J", "K", "L", "M", "N", "O", "P", "Q", "R",

"S", "T", "U", "V", "W", "X", "Y", "Z"]

lowercase = ["a", "b", "c", "d", "e", "f", "g", "h", "i",

"j", "k", "l", "m", "n", "o", "p", "q", "r",

"s", "t", "u", "v", "w", "x", "y", "z"]

new_sentence = ""

for i in range(0,len(sentence)):

if ((my_type == "upper" and

sentence[i] in lowercase) or

(my_type == "lower" and

sentence[i] in uppercase)):

new_sentence += get_new_letter(sentence[i],my_type)

else:

new_sentence += sentence[i]

return new_sentence

COMPSCI 101 - Principles of Programming

Helper Functions

defget_new_letter(letter, my_type):

uppercase = ["A", "B", "C", "D", "E", "F", "G", "H",

"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

lowercase = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y” ,"z"]

if my_type == "upper":

for i in range(0,len(lowercase)):

if lowercase[i] == letter:

return uppercase[i]

else:

for i in range(0,len(uppercase)):

if uppercase[i] == letter:

return lowercase[i]

COMPSCI 101 - Principles of Programming

Summary
• Boolean Operators can make code easier to understand
• Sequences are easy to manipulate
• Both lists and strings are sequences

COMPSCI 101 - Principles of Programming

Next Tuesday
• Slices of Sequences

COMPSCI 101 - Principles of Programming