I was given a problem that takes in a table of information (grid coordinates and characters) and asked to place them into a table to make a message.
I've spent time working on some Python to work it out but my answer is coming out upside down and I can't figure out why.
import requests
from bs4 import BeautifulSoup
webpage_response = requests.get('https://docs.google.com/document/d/e/2PACX-1vRMx5YQlZNa3ra8dYYxmv-QIQ3YJe8tbI3kqcuC7lQiZm-CSEznKfN_HYNSpoXcZIV3Y_O3YoUB1ecq/pub')
webpage = webpage_response.content
#print(webpage)
soup = BeautifulSoup(webpage, 'html.parser')
table = soup.find('table')
#print(doc_table)
rows = table.find_all('tr')
data = []
for row in rows[1:]:
cells = row.find_all('td')
character = cells[1].text.strip()
x = cells[0].text.strip()
y = cells[2].text.strip()
data.append((character,x,y))
#print(data)
max_x = max(x for _,x,_ in data)
max_y = max(y for _, _,y in data)
#print(max_x)
#print(max_y)
grid = [[' ' for _ in range(int(max_x) + 1)] for _ in range(int(max_y) + 1)]
for character, x, y in data:
grid[int(y)][int(x)] = character
for row in grid:
print(''.join(row))
This is my code. It should print out a grid of characters that look like a capital F but its upside down. I think the problem arises when I populate the grid but I'm not sure how to fix it.
In the web page y=0 is the bottom of the screen rather than the top.
But when doing for row in gridthe y coordinates goes from 0 to 2 and of course you write first the top line of the produced figure.
So you can just modify this loop doing for row in reversed(grid), or your way to fill grid (#modified show where the line are modified) :
import requests
from bs4 import BeautifulSoup
webpage_response = requests.get('https://docs.google.com/document/d/e/2PACX-1vRMx5YQlZNa3ra8dYYxmv-QIQ3YJe8tbI3kqcuC7lQiZm-CSEznKfN_HYNSpoXcZIV3Y_O3YoUB1ecq/pub')
webpage = webpage_response.content
#print(webpage)
soup = BeautifulSoup(webpage, 'html.parser')
table = soup.find('table')
#print(doc_table)
rows = table.find_all('tr')
data = []
for row in rows[1:]:
cells = row.find_all('td')
character = cells[1].text.strip()
x = cells[0].text.strip()
y = cells[2].text.strip()
data.append((character,x,y))
#print(data)
max_x = max(x for _,x,_ in data)
max_y = int(max(y for _, _,y in data)) # modified
#print(max_x)
#print(max_y)
grid = [[' ' for _ in range(int(max_x) + 1)] for _ in range(max_y + 1)]
for character, x, y in data:
grid[max_y - int(y)][int(x)] = character # modified
for row in grid:
print(''.join(row))
in both case that produces :
bruno@raspberrypi:/tmp $ python p.py
█▀▀▀
█▀▀
█
bruno@raspberrypi:/tmp $
Of course you can also indicate where each character must be written forcing their position on the terminal using curses or the appropriate escape sequence
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With