Jump to content

Module:USHRseats

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Swpb (talk | contribs) at 15:12, 6 February 2019. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Returns the number of seats in the US House of Representatives held by a given state in a given year (If no year is given, the current number of seats is returned)
local p = {}

function p.main(frame)
	state = frame.args[1]
	if mw.ustring.len(state)>2 then
		state = frame:expandTemplate{ title = 'ConvertAbbrev/ISO 3166-2/US', args = {state} }
	end

	year = frame.args[2]
	if year then
		census_number = (year - math.fmod(year,10))/10 - 177
	end
		
	states = {
	AK = {'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',  1,  1,  1,  1,  1,  1},
	AL = {'-','-','-','-',  3,  5,  7,  7,  6,  8,  8,  9,  9, 10,  9,  9,  9,  8,  7,  7,  7,  7,  7},
	AR = {'-','-','-','-','-','-',  1,  2,  3,  4,  5,  6,  7,  7,  7,  7,  6,  4,  4,  4,  4,  4,  4},
	AZ = {'-','-','-','-','-','-','-','-','-','-','-','-','-',  1,  1,  2,  2,  3,  4,  5,  6,  8,  9},
	CA = {'-','-','-','-','-','-','-',  2,  3,  4,  6,  7,  8, 11, 20, 23, 30, 38, 43, 45, 52, 53, 53},
	CO = {'-','-','-','-','-','-','-','-','-','-',  1,  2,  3,  4,  4,  4,  4,  4,  5,  6,  6,  7,  7},
	CT = {  5,  7,  7,  7,  6,  6,  4,  4,  4,  4,  4,  4,  5,  5,  6,  6,  6,  6,  6,  6,  6,  5,  5},
	DE = {  1,  1,  1,  2,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1},
	FL = {'-','-','-','-','-','-','-',  1,  1,  2,  2,  2,  3,  4,  5,  6,  8, 12, 15, 19, 23, 25, 27},
	GA = {  3,  2,  4,  6,  7,  9,  8,  8,  7,  9, 10, 11, 11, 12, 10, 10, 10, 10, 10, 10, 11, 13, 14},
	HI = {'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',  2,  2,  2,  2,  2,  2},
	IA = {'-','-','-','-','-','-','-',  2,  6,  9, 11, 11, 11, 11,  9,  8,  8,  7,  6,  6,  5,  5,  4},
	ID = {'-','-','-','-','-','-','-','-','-','-','-',  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2},
	IL = {'-','-','-','-',  1,  3,  7,  9, 14, 19, 20, 22, 25, 27, 27, 26, 25, 24, 24, 22, 20, 19, 18},
	IN = {'-','-','-','-',  3,  7, 10, 11, 11, 13, 13, 13, 13, 13, 12, 11, 11, 11, 11, 10, 10,  9,  9},
	KS = {'-','-','-','-','-','-','-','-',  1,  3,  7,  8,  8,  8,  7,  6,  6,  5,  5,  5,  4,  4,  4},
	KY = {'-','2',  6, 10, 12, 13, 10, 10,  9, 10, 11, 11, 11, 11,  9,  9,  8,  7,  7,  7,  6,  6,  6},
	LA = {'-','-','-',  1,  3,  3,  4,  4,  5,  6,  6,  6,  7,  8,  8,  8,  8,  8,  8,  8,  7,  7,  6},
	MA = {  8, 14, 17, 20, 13, 12, 10, 11, 10, 11, 12, 13, 14, 16, 15, 14, 14, 12, 12, 11, 10, 10,  9},
	MD = {  6,  8,  9,  9,  9,  8,  6,  6,  5,  6,  6,  6,  6,  6,  6,  6,  7,  8,  8,  8,  8,  8,  8},
	ME = {'-','-','-','-',  7,  8,  7,  6,  5,  5,  4,  4,  4,  4,  3,  3,  3,  2,  2,  2,  2,  2,  2},
	MI = {'-','-','-','-','-','-',  3,  4,  6,  9, 11, 12, 12, 13, 17, 17, 18, 19, 19, 18, 16, 15, 14},
	MN = {},
	MO = {},
	MS = {},
	MT = {},
	NC = {},
	ND = {},
	NE = {},
	NH = {},
	NJ = {},
	NM = {},
	NV = {},
	NY = {},
	OH = {},
	OK = {},
	OR = {},
	PA = {},
	RI = {},
	SC = {},
	SD = {},
	TN = {},
	TX = {},
	UT = {},
	VA = {},
	VT = {},
	WA = {},
	WI = {},
	WV = {},
	WY = {}}

	if not states[state] then
		seats = frame:expandTemplate{ title = 'error', args = {'Error: state not recognized'} }
	else
		if census_number then
			seats = states[state][census_number]
			if seats=='-' or not seats then
				seats = frame:expandTemplate{ title = 'error', args = {"Error: given year is prior to the first census of this state"} }
			end
		else
			seats = states[state][#states[state]]
		end
	end
	
	return seats
end

return p