![]() |
|
Challenge (3) Get USA Temperature Extremes - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Coding (https://sinister.li/Forum-Coding--71) +--- Thread: Challenge (3) Get USA Temperature Extremes (/Thread-Challenge-3-Get-USA-Temperature-Extremes) |
Challenge (3) Get USA Temperature Extremes - m0dem - 12-22-2015 Scrape the temperature extremes of the US states, from this source: (only) https://en.wikipedia.org/wiki/U.S._state_temperature_extremes With that data, find the average record high and low of all the states. Here is my solution, I made it a few months back. It is a bit more complex than needed (cli args), but it still works. Usage: Code: ./ustemp.py [-a/--average] [high/low]Code: ./ustemp.py --average lowCode: #!/usr/bin/python
# -*- coding: utf-8 -*-
import argparse
import HTMLParser
import re
import urllib2
US_TEMP_EXTREMES = "https://en.wikipedia.org/wiki/U.S._state_temperature_extremes"
class TableParser(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.in_td = False
self.table = []
def handle_starttag(self, tag, attrs):
if tag == "td":
self.in_td = True
def handle_data(self, data):
if self.in_td:
self.table.append(data)
def handle_endtag(self, tag):
self.in_td = False
def getAvg(l):
return sum(l) / len(l)
# extract the first int value from our `data` string
def getFirstInt(data):
tmpData = ""
for char in data:
try:
int(char)
tmpData += char
except ValueError:
if char == "-":
tmpData += char
else:
break
return int(tmpData)
def getColumnData(table, columns, column):
temps = []
for i in range(0, 11):
data = table[i * columns + column]
data = getFirstInt(data)
temps.append(data)
for i in range(11, 50):
data = table[i * columns + column + 1]
data = getFirstInt(data)
temps.append(data)
return temps
# handle the arguments
argparser = argparse.ArgumentParser()
argparser.add_argument("-a", "--average", nargs = 1, default = "high", choices = ["low", "high"])
args = argparser.parse_args()
FLAG = args.average
if type(FLAG) == list:
FLAG = FLAG[0]
# parse the HTML table data
html = urllib2.urlopen(US_TEMP_EXTREMES).read()
parser = TableParser()
parser.feed(html)
table = []
for string in parser.table:
tmpString = ""
dashHex = ["\xe2", "\x88", "\x92"]
c = 0
for i in range(0, len(string)):
nextHex = dashHex[c]
if string[i].isalnum() or string[i] == nextHex:
if string[i] == nextHex:
c += 1
if c >= len(dashHex):
c = 0
tmpString += "-"
else:
c = 0
tmpString += string[i]
if tmpString != "":
table.append(tmpString)
if FLAG == "high":
temps = getColumnData(table, 7, 1)
elif FLAG == "low":
temps = getColumnData(table, 7, 4)
print("The average temperature {} in the US among the 50 states is {} °F".format(FLAG, getAvg(temps)))RE: Challenge (3) Get USA Temperature Extremes - m0dem - 12-24-2015 Well... is anyone even able to complete this challenge? It's pretty easy... RE: Challenge (3) Get USA Temperature Extremes - Nil - 12-24-2015 Not I. I'm literally only familiar with a couple of python libraries. But I guess I could use these challenges to actually do some outside research and not expect to start coding as soon as I read the challenge. RE: Challenge (3) Get USA Temperature Extremes - insidious - 12-24-2015 (12-24-2015, 04:40 AM)m0dem Wrote: Well... is anyone even able to complete this challenge? It's pretty easy... I'll try it, right after I take a nap. See you in a few hours ![]() EDIT: Czerwony Barscz, Uszki, Pierogi, fish and alcohol got the best of me. I swear I'll try this in the near future. |