![Page 1: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/1.jpg)
CSCI 141
![Page 2: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/2.jpg)
CSCI 141
Lecture 25
![Page 3: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/3.jpg)
CSCI 141
Lecture 25Searching and Sorting
![Page 4: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/4.jpg)
Announcements
![Page 5: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/5.jpg)
Announcements• Reminder: No A5 submissions accepted past
Thursday 12/5 at 10pm
![Page 6: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/6.jpg)
Announcements• Reminder: No A5 submissions accepted past
Thursday 12/5 at 10pm
• Reminder: Extra TA OH today and tomorrow:
![Page 7: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/7.jpg)
Announcements• Reminder: No A5 submissions accepted past
Thursday 12/5 at 10pm
• Reminder: Extra TA OH today and tomorrow:
• Kirsten: 10-12 Monday and Tuesday (CF 163)
![Page 8: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/8.jpg)
Announcements• Reminder: No A5 submissions accepted past
Thursday 12/5 at 10pm
• Reminder: Extra TA OH today and tomorrow:
• Kirsten: 10-12 Monday and Tuesday (CF 163)
• Rory 12-2 Monday and Tuesday (CF 477)
![Page 9: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/9.jpg)
Announcements• Reminder: No A5 submissions accepted past
Thursday 12/5 at 10pm
• Reminder: Extra TA OH today and tomorrow:
• Kirsten: 10-12 Monday and Tuesday (CF 163)
• Rory 12-2 Monday and Tuesday (CF 477)
• Monday 12/2 (a week from today) there will be a review quiz.
![Page 10: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/10.jpg)
Announcements• Reminder: No A5 submissions accepted past
Thursday 12/5 at 10pm
• Reminder: Extra TA OH today and tomorrow:
• Kirsten: 10-12 Monday and Tuesday (CF 163)
• Rory 12-2 Monday and Tuesday (CF 477)
• Monday 12/2 (a week from today) there will be a review quiz.
• Delivered via Socrative, but counted towards QOTD credit; worth 2 QOTDs.
![Page 11: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/11.jpg)
Announcements• Regular QOTDs are done! Suggestion:
spend the time doing a bit of studying every day.
• Sample coding questions for the final exam will be out by next Monday (aiming for tomorrow).
![Page 12: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/12.jpg)
Suppose the file rick.txt contains:Never gonna give you up
What is the output of the following code?print(open("rick.txt", "r").read(5).split("e"))
A.NvrB.NeverC.["N", "e", "v", "e", "r"]D.["N", "v", "r"]
![Page 13: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/13.jpg)
QOTD
print("_".join("88r4if4r462".split("r4")))
![Page 14: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/14.jpg)
QOTD
print("_".join("88r4if4r462".split("r4")))
"88r4if4r462".split("r4")
![Page 15: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/15.jpg)
QOTD
print("_".join("88r4if4r462".split("r4")))
"88r4if4r462".split("r4")
print("_".join(["88", "if4, "62"]))
![Page 16: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/16.jpg)
QOTD
print("_".join("88r4if4r462".split("r4")))
"88r4if4r462".split("r4")
print("_".join(["88", "if4, "62"]))
print("88_if4_62")
![Page 17: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/17.jpg)
QOTDStart: 4pmEnd: 7pm3h
fin.txt:
fin = open("fin.txt", 'r')fot = open("fot.txt", 'w')
for line in fin: fl = line.strip().split(': ')
fot.write(fl[-1]) fot.write(fl[0]) fot.close()fin.close()
fot = open("fot.txt", 'r')a = fot.read(3)fot.seek(8)print(a, fot.read())fot.close()
![Page 18: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/18.jpg)
QOTDStart: 4pmEnd: 7pm3h
fin.txt:
fin = open("fin.txt", 'r')fot = open("fot.txt", 'w')
for line in fin: fl = line.strip().split(': ')
fot.write(fl[-1]) fot.write(fl[0]) fot.close()fin.close()
fot = open("fot.txt", 'r')a = fot.read(3)fot.seek(8)print(a, fot.read())fot.close()
4pmStart7pmEnd3h3h
fout.txt:
![Page 19: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/19.jpg)
QOTDStart: 4pmEnd: 7pm3h
fin.txt:
fin = open("fin.txt", 'r')fot = open("fot.txt", 'w')
for line in fin: fl = line.strip().split(': ')
fot.write(fl[-1]) fot.write(fl[0]) fot.close()fin.close()
fot = open("fot.txt", 'r')a = fot.read(3)fot.seek(8)print(a, fot.read())fot.close()
4pmStart7pmEnd3h3h
fout.txt:
0123456789
![Page 20: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/20.jpg)
QOTDStart: 4pmEnd: 7pm3h
fin.txt:
fin = open("fin.txt", 'r')fot = open("fot.txt", 'w')
for line in fin: fl = line.strip().split(': ')
fot.write(fl[-1]) fot.write(fl[0]) fot.close()fin.close()
fot = open("fot.txt", 'r')a = fot.read(3)fot.seek(8)print(a, fot.read())fot.close()
4pmStart7pmEnd3h3h
fout.txt:
0123456789
4pm
![Page 21: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/21.jpg)
QOTDStart: 4pmEnd: 7pm3h
fin.txt:
fin = open("fin.txt", 'r')fot = open("fot.txt", 'w')
for line in fin: fl = line.strip().split(': ')
fot.write(fl[-1]) fot.write(fl[0]) fot.close()fin.close()
fot = open("fot.txt", 'r')a = fot.read(3)fot.seek(8)print(a, fot.read())fot.close()
4pmStart7pmEnd3h3h
fout.txt:
0123456789
4pm7pmEnd3h3h
![Page 22: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/22.jpg)
QOTDStart: 4pmEnd: 7pm3h
fin.txt:
fin = open("fin.txt", 'r')fot = open("fot.txt", 'w')
for line in fin: fl = line.strip().split(': ')
fot.write(fl[-1]) fot.write(fl[0]) fot.close()fin.close()
fot = open("fot.txt", 'r')a = fot.read(3)fot.seek(8)print(a, fot.read())fot.close()
4pmStart7pmEnd3h3h
fout.txt:
0123456789
4pm7pmEnd3h3h
4pm 7pmEnd3h3hOutput:
![Page 23: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/23.jpg)
Computer Science: An Analogy
CSCI 141 : spelling and grammar
::
Most of computer science : literature
![Page 24: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/24.jpg)
Computer Science: An Analogy
CSCI 141 : learning to use a telescope
::
computer science : astronomy
![Page 25: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/25.jpg)
What does the rest of computer science look like?Many, many "subfields":
• Systems: designing computer systems that do useful stuff.Thank these people for your operating system, most of the underpinnings of the internet, etc. (CSCI 247, 347, 447, 367, ...)
• Theory: answering questions about what can be done (efficiently) on a computer. Thank these people for the ability to do many of the things you take for granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...)
• Programming languages: studying, designing, and implementing languages. Thank these people for the ability to write Python instead of typing out 1's and 0's. (CSCI 301, 410, 450)
![Page 26: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/26.jpg)
What does the rest of computer science look like?Many other sub-fields, some of which are more "applied":
• computer vision (497P)
• computer graphics (480)
• human-computer interaction (346)
• machine learning (471)
• robotics (372)
• ...
![Page 27: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/27.jpg)
finding a value in a listdef find(v, lst): """ Return the index of the first occurrence of v in lst. Return -1 if v is not in the list. Precondition: lst is a list. """
Task: write pseudocode to solve this problem.
![Page 28: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/28.jpg)
Analysis of Algorithms: Searching a list
What's the most efficient way to find a value in a list?
In a list of N elements, how many comparisons does our algorithm make:
• in the "best" case?
• in the "worst" case?
• in the "average" case?
![Page 29: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/29.jpg)
finding a value in a sorted listdef find(v, sorted_lst): """ Return the index of the first occurrence of v in lst. Return -1 if v is not in the list. Precondition: lst is a list of things that can be compared with the < operator, and is in sorted order (i.e. lst[i] <= lst[i+1] for all i in range(len(lst)-1) """
Task: write pseudocode to solve this problem.
![Page 30: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/30.jpg)
Analysis of Algorithms: Searching a sorted list
What's the most efficient way to find a value in a list?
In a list of N elements, how many comparisons does our algorithm make:
• in the "best" case?
• in the "worst" case?
• in the "average" case?
![Page 31: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/31.jpg)
Sorting a List
![Page 32: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/32.jpg)
Sorting a ListOkay, so searching is fast in a sorted list.
![Page 33: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/33.jpg)
Sorting a ListOkay, so searching is fast in a sorted list.
... how can we sort a list?
![Page 34: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/34.jpg)
Sorting a Listdef sort(lst): """ Sort the given list. Precondition: lst is a list of things that can be compared with the < operator. Postcondition: lst[i] <= lst[i+1] for all i in range(len(list)-1). """
Example list: [8, 4, 7, 9, 2, 1, 6, 4]
Task: write pseudocode to solve this problem.
![Page 35: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/35.jpg)
Analysis of Algorithms: Sorting a list
![Page 36: CSCI 141 - Western Washington Universitywehrwes/courses/csci141_19f/lectur… · granted without (literally) having to wait hundreds of years. (CSCI 301, 305, 405, ...) • Programming](https://reader034.vdocuments.us/reader034/viewer/2022050512/5f9d11365d833771f251f644/html5/thumbnails/36.jpg)
Analysis of Algorithms: Sort
What's the most efficient way to sort a list?
In a list of N elements, how many comparisons does your algorithm make:
• in the "best" case?
• in the "worst" case?
• in the "average" case?