Jed Rembold
December 8, 2021
The code for the main function of Merge Sort looked like below:
def merge_sort(array):
if len(array) > 1:
mid = len(array)//2
a1 = array[:mid]
a2 = array[mid:]
merge_sort(a1)
merge_sort(a2)
merge(array, a1, a2)
L = [45, 32, 68, 78, 12, 95, 312, 4, 32]
merge_sort(L)
How many times (including line 10) does merge_sort
get called before the list is sorted?
Responsible for interacting with http webpages
Generally used to download some information from the web
Most frequently use the get
method to download a url
response = requests.get("desired_url")
text
attributeprint(response.text)
The text returned by requests
is generally raw HTML
You could parse this yourself, but using a parser like BeautifulSoup can greatly streamline things
You pass in HTML and are returned a custom soup object with many useful methods built in
One of the more common things you’d want to do with the HTML is find certain elements on the page
soup.find_all('tag_name', more_filters)
import requests
from bs4 import BeautifulSoup
def topic_finder():
def is_lecture_link(tag):
""" Filters out anchor tags with Lecture in the text. """
return tag.name == "a" and "Lecture" in tag.text
def find_lecture_containing(topic):
""" Returns a list of lectures that contain the desired topic string. """
lectures = []
for lecture in lecture_links:
content = requests.get("https:/" + lecture.get("href")).text
if topic.lower() in content.lower():
lectures.append((lecture.text, "https:/" + lecture.get("href")))
return lectures
url = "http://willamette.edu/~jjrembold/classes/cs151/slides/"
response = requests.get(url)
soup = BeautifulSoup(response.text)
lecture_links = soup.find_all(is_lecture_link)
finished = False
while not finished:
target = input("\nWhat topic are you looking for? ")
if target == "":
finished = True
else:
found = find_lecture_containing(target)
for lecture in reversed(found):
name, url = lecture
print(f"{name}: {url}")
if __name__ == '__main__':
topic_finder()
PyAutoGUI
library gives you methods to simulate moving and clicking the mouse and pressing keys on the keyboard
PyAutoGUI
has a failsafe if you slam the mouse into one of the window cornersCommand | Description |
---|---|
.click() |
Clicks the mouse at the current location |
.mouseDown() |
Presses the mouse button down |
.mouseUp() |
Releases the mouse button |
.moveTo(x,y) |
Moves to the coordinate (x,y) |
.move(dx, dy) |
Moves from current position dx horizontally and dy vertically |
.drag(dx,dy) |
Clicks the mouse down and moves dx over and dy down |
.position() |
Gets the current mouse position |
.write(text) |
Presses the keys represented in the string text |
import pyautogui as pag
CHANGE = 50
pag.countdown(10)
pag.click() # Click to make the window active.
distance = 500
while distance > 0:
pag.drag(distance, 0, duration=0.5) # Move right.
distance -= CHANGE
pag.drag(0, distance, duration=0.5) # Move down.
pag.drag(-distance, 0, duration=0.5) # Move left.
distance -= CHANGE
pag.drag(0, -distance, duration=0.5) # Move up.