diff --git a/DATA/float_values.txt b/DATA/float_values.txt new file mode 100644 index 0000000..6d2b644 --- /dev/null +++ b/DATA/float_values.txt @@ -0,0 +1,25 @@ +73.34 +94.39 +60.16 +45.47 +88.36 +54.51 +63.15 +18.19 +34.64 +0.94 +32.55 +64.37 +73.96 +52.28 +50.23 +6.84 +27.61 +39.18 +43.48 +61.11 +34.24 +11.88 +98.55 +92.36 +78.62 diff --git a/DATA/presidents.txt b/DATA/presidents.txt new file mode 100644 index 0000000..6b0b7f5 --- /dev/null +++ b/DATA/presidents.txt @@ -0,0 +1,45 @@ +1:Washington:George:1732-02-22:1799-12-14:Westmoreland County:Virginia:1789-04-30:1797-03-04:no party +2:Adams:John:1735-10-30:1826-07-04:Braintree, Norfolk:Massachusetts:1797-03-04:1801-03-04:Federalist +3:Jefferson:Thomas:1743-04-13:1826-07-04:Albermarle County:Virginia:1801-03-04:1809-03-04:Democratic - Republican +4:Madison:James:1751-03-16:1836-06-28:Port Conway:Virginia:1809-03-04:1817-03-04:Democratic - Republican +5:Monroe:James:1758-04-28:1831-07-04:Westmoreland County:Virginia:1817-03-04:1825-03-04:Democratic - Republican +6:Adams:John Quincy:1767-07-11:1848-02-23:Braintree, Norfolk:Massachusetts:1825-03-04:1829-03-04:Democratic - Republican +7:Jackson:Andrew:1767-03-15:1845-06-08:Waxhaw:South Carolina:1829-03-04:1837-03-04:Democratic +8:Van Buren:Martin:1782-12-05:1862-07-24:Kinderhook:New York:1837-03-04:1841-03-04:Democratic +9:Harrison:William Henry:1773-02-09:1841-04-04:Berkeley:Virginia:1841-03-04:1841-04-04:Whig +10:Tyler:John:1790-03-29:1862-01-18:Charles City County:Virginia:1841-04-04:1845-03-04:Whig +11:Polk:James Knox:1795-11-02:1849-06-15:Mecklenburg County:North Carolina:1845-03-04:1849-03-03:Democratic +12:Taylor:Zachary:1784-11-24:1850-07-09:Orange County:Virginia:1849-03-05:1850-07-09:Whig +13:Fillmore:Millard:1800-01-07:1874-03-08:Cayuga County:New York:1850-07-09:1853-03-04:Whig +14:Pierce:Franklin:1804-11-23:1869-10-08:Hillsboro:New Hampshire:1853-03-04:1857-03-04:Democratic +15:Buchanan:James:1791-04-23:1868-06-01:Cove Gap:Pennsylvania:1857-03-04:1861-03-04:Democratic +16:Lincoln:Abraham:1809-02-12:1865-04-15:Hodgenville, Hardin County:Kentucky:1861-03-04:1865-04-15:Republican +17:Johnson:Andrew:1808-12-29:1875-07-31:Raleigh:North Carolina:1865-04-15:1869-03-04:Republican +18:Grant:Ulysses Simpson:1822-04-27:1885-07-23:Point Pleasant:Ohio:1869-03-04:1877-03-04:Republican +19:Hayes:Rutherford Birchard:1822-10-04:1893-01-17:Delaware:Ohio:1877-03-04:1881-03-04:Republican +20:Garfield:James Abram:1831-11-19:1881-09-19:Orange, Cuyahoga County:Ohio:1881-03-04:1881-09-19:Republican +21:Arthur:Chester Alan:1829-10-05:1886-11-18:Fairfield:Vermont:1881-09-20:1885-03-04:Republican +22:Cleveland:Grover:1837-03-18:1908-06-24:Caldwell:New Jersey:1885-03-04:1889-03-04:Democratic +23:Harrison:Benjamin:1833-08-20:1901-03-13:North Bend:Ohio:1889-03-04:1893-03-04:Republican +24:Cleveland:Grover:1837-03-18:1908-06-24:Caldwell:New Jersey:1893-03-04:1897-03-04:Democratic +25:McKinley:William:1843-01-29:1901-09-14:Niles:Ohio:1897-03-04:1901-09-14:Republican +26:Roosevelt:Theodore:1858-10-27:1919-01-06:New York City:New York:1901-09-14:1909-03-04:Republican +27:Taft:William Howard:1857-09-15:1930-03-08:Cincinnati:Ohio:1909-03-04:1913-03-04:Republican +28:Wilson:Woodrow:1856-12-28:1924-02-03:Staunton:Virginia:1913-03-04:1921-03-04:Democratic +29:Harding:Warren Gamaliel:1865-11-02:1923-08-02:Blooming Grove:Ohio:1921-03-04:1923-08-02:Republican +30:Coolidge:Calvin:1872-07-04:1933-01-05:Plymouth:Vermont:1923-08-03:1929-03-04:Republican +31:Hoover:Herbert Clark:1874-08-10:1964-10-20:West Branch:Iowa:1929-03-04:1933-03-04:Republican +32:Roosevelt:Franklin Delano:1882-01-30:1945-04-12:Hyde Park:New York:1933-03-04:1945-04-12:Democratic +33:Truman:Harry S.:1884-05-08:1972-12-26:Lamar:Missouri:1945-04-12:1953-01-20:Democratic +34:Eisenhower:Dwight David:1890-10-14:1969-03-28:Denison:Texas:1953-01-20:1961-01-20:Republican +35:Kennedy:John Fitzgerald:1917-05-29:1963-11-22:Brookline:Massachusetts:1961-01-20:1963-11-22:Democratic +36:Johnson:Lyndon Baines:1908-08-27:1973-01-22:near Stonewall:Texas:1963-11-22:1969-01-20:Democratic +37:Nixon:Richard Milhous:1913-01-09:1994-04-22:Yorba Linda:California:1969-01-20:1974-08-09:Republican +38:Ford:Gerald Rudolph:1913-07-14:2006-12-26:Omaha:Nebraska:1974-08-09:1977-01-20:Republican +39:Carter:James Earl 'Jimmy':1924-10-01:NONE:Plains:Georgia:1977-01-20:1981-01-20:Democratic +40:Reagan:Ronald Wilson:1911-02-06:2004-06-05:Tampico:Illinois:1981-01-20:1989-01-20:Republican +41:Bush:George Herbert Walker:1924-06-12:2018-11-30:Milton:Massachusetts:1989-01-20:1993-01-20:Republican +42:Clinton:William Jefferson 'Bill':1946-08-19:NONE:Hope:Arkansas:1993-01-20:2001-01-20:Democratic +43:Bush:George Walker:1946-07-06:NONE:New Haven:Connecticut:2001-01-20:2009-01-20:Republican +44:Obama:Barack Hussein:1961-08-04:NONE:Honolulu:Hawaii:2009-01-20:2017-01-20:Democratic +45:Trump:Donald John:1946-06-14:NONE:Queens, NYC:New York:2017-01-20:NONE:Republican diff --git a/README.md b/README.md index 0017208..1b053d3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # pythonExamples -A collection of example programs I made while learning python 3. +A collection of example programs I made during an Andvanced Python course. diff --git a/__pycache__/president.cpython-37.pyc b/__pycache__/president.cpython-37.pyc new file mode 100644 index 0000000..07bf629 Binary files /dev/null and b/__pycache__/president.cpython-37.pyc differ diff --git a/asyncio_web_crawler.py b/asyncio_web_crawler.py new file mode 100644 index 0000000..9812217 --- /dev/null +++ b/asyncio_web_crawler.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +import asyncio +import aiohttp +import re +from sys import argv + +def get_links(body: str): + links = re.findall('"((http)?://.*?)"', body) + return links + +async def fetch(session, url): + async with session.get(url) as response: + return await response.text() + + +async def main(): + async with aiohttp.ClientSession() as session: + html = await fetch(session, argv[1]) + found_links = get_links(html) + try: + for links in found_links: + print(await fetch(session, links[0])) + except aiohttp.client_exceptions.ClientConnectorError: + pass +loop = asyncio.get_event_loop() +loop.run_until_complete(main()) diff --git a/count_ext.py b/count_ext.py new file mode 100644 index 0000000..4b40b0b --- /dev/null +++ b/count_ext.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +import sys, os +from collections import Counter + +def printExtensions(path: str): + list_of_files = [f.split('.')[1] for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))] + + counter = Counter() + for ext in list_of_files: + counter[ext] += 1 + + for item, count in counter.items(): + print(f'{item} {count}') + +printExtensions('/Users/jcasey/Downloads') \ No newline at end of file diff --git a/dir_counter.py b/dir_counter.py new file mode 100644 index 0000000..3bf6797 --- /dev/null +++ b/dir_counter.py @@ -0,0 +1,26 @@ +import os +from sys import argv + +if __name__ == '__main__': + top = argv[1] + total_files = 0 + total_lines = 0 + total_words = 0 + total_bytes = 0 + + for path, dirs, files in os.walk(top): + total_files += len(files) + for fname in files: + fpath = os.path.join(path, fname) + try: + with open(fpath) as fh: + for line in fh: + total_lines += 1 + total_words += len(line.split()) + total_bytes += len(bytes(line, 'utf-8')) + except: + print('unicode error') + pass + + print(f'{total_lines} lines') + print(f'{total_files} files') \ No newline at end of file diff --git a/doubledeco.py b/doubledeco.py new file mode 100644 index 0000000..4ca3a66 --- /dev/null +++ b/doubledeco.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python +from functools import wraps + +# decorator that will +# double the value +# returned by the method +def doubler(old_func): + def new_func(*args): + res = old_func(*args) + return res * 2 + return new_func + +#using the decorator +@doubler +def addition(x): + return x + +a = addition(1) +print(a) \ No newline at end of file diff --git a/make_zip.py b/make_zip.py new file mode 100644 index 0000000..ac3b6c2 --- /dev/null +++ b/make_zip.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python + +from zipfile import ZipFile, ZIP_DEFLATED +import os.path + +# make a zip file consisting +# of the file listed below +wzip = ZipFile("chap8.zip", mode='w', compression=ZIP_DEFLATED) +for base in "save_potus_info.py read_potus_info.py pres_list.pic".split(): + wzip.write(base) \ No newline at end of file diff --git a/overload_methods.py b/overload_methods.py new file mode 100644 index 0000000..d5a36ec --- /dev/null +++ b/overload_methods.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +from typing import List +import multimethod + +class Seed: pass +class Pig: pass + +def grep(x: str) -> str: + while True: + coll = yield + if x in coll: + yield x + +def sed (pattern: str, replacement: str) -> str: + while True: + value = yield + if pattern in value: + yield replacement + +@multimethod +def sow(location, obj: Seed): + print("this is a seed") + +@multimethod +def sow(location, obj: Pig): + print("this is a pig") + +def append_42(x :List[int]): + x.append(42) + return x + +if __name__ == '__main__': + sow('test', Seed()) + sow('test', Pig()) + newlist = [32] + append_42(newlist) + print(newlist) \ No newline at end of file diff --git a/pres_attr.py b/pres_attr.py new file mode 100644 index 0000000..526f61d --- /dev/null +++ b/pres_attr.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +class President(): + def __init__(self, first, last, party): + self.first = first + self.last = last + self.party = party + +pres = President("George", "Washingtino", "no part") + +# access the field using getattr +# instead of pres.first +print(f'{getattr(pres, "first")}') \ No newline at end of file diff --git a/pres_by_death.py b/pres_by_death.py index 0d4d0f5..c1ff42e 100644 --- a/pres_by_death.py +++ b/pres_by_death.py @@ -1,7 +1,7 @@ #!/usr/bin/env python list_of_pres = [] -with open('/Users/jcasey/Downloads/presidents.txt') as PRES: +with open('./DATA/presidents.txt') as PRES: for line in PRES: splitLine = line.split(':') last_name = splitLine[1] @@ -11,6 +11,7 @@ pol_party = splitLine[9] list_of_pres.append((first_name, last_name, date_birth, date_death, pol_party)) +# sorting list of tuples using lambda. tup[3] = date of death list_of_pres = sorted(list_of_pres, key=lambda tup:tup[3]) for i in list_of_pres: diff --git a/pres_by_state.py b/pres_by_state.py index 1f6f214..722dd30 100644 --- a/pres_by_state.py +++ b/pres_by_state.py @@ -1,7 +1,8 @@ #!/usr/bin/env python state_list = {} -with open('/Users/jcasey/Downloads/presidents.txt') as PRES: +# read in data and add to dictionary +with open('./DATA/presidents.txt') as PRES: for line in PRES: splitLine = line.split(':') state = splitLine[6] @@ -10,5 +11,6 @@ else: state_list[state] = 1 +# print the key-value pair for key, value in sorted(state_list.items()): print(f'{key:30s} {value}') \ No newline at end of file diff --git a/pres_by_state_map.py b/pres_by_state_map.py new file mode 100644 index 0000000..cf547c7 --- /dev/null +++ b/pres_by_state_map.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +from itertools import groupby + +# use map and lambda to split line +# on 6th field. The values get added +# to the list +result = list(map(lambda x: x.split(':')[6:7], open('./DATA/presidents.txt'))) + +# use group by to show +# number of presidents +# from which state +count = groupby(result, key=lambda x: x) + +for a in count: + print(a) \ No newline at end of file diff --git a/pres_dates.py b/pres_dates.py index b346ea6..2399e05 100644 --- a/pres_dates.py +++ b/pres_dates.py @@ -4,10 +4,10 @@ name_query = input("Enter Last Name\n") # Read data into dictionary -with open('/Users/jcasey/Downloads/presidents.txt') as PRES: +with open('./DATA/presidents.txt') as PRES: for line in PRES: split_line = line.split(":") - # if last name field equals the query + # if last_name field equals the query # pull out the values and print them if split_line[1] == name_query: last_name = split_line[1] diff --git a/pres_gen.py b/pres_gen.py index 60dcd91..3e48eaf 100644 --- a/pres_gen.py +++ b/pres_gen.py @@ -1,6 +1,11 @@ #!/usr/bin/env python -list_of_pres = [] -with open('/Users/jcasey/Downloads/presidents.txt') as PRES: - for line in PRES: - splitLine = line.split(':') \ No newline at end of file +# Generator function. Is iterable +def presidentGen(file_name): + with open(file_name) as PRES: + for line in PRES: + splitLine = line.split(':') + yield str(splitLine[2] + ' ' + splitLine[1]).upper() + +for president in presidentGen('./DATA/presidents.txt'): + print(president) \ No newline at end of file diff --git a/pres_monkey.py b/pres_monkey.py new file mode 100644 index 0000000..0bd53d1 --- /dev/null +++ b/pres_monkey.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +class President(): + def __init__(self, first, last, party): + self.first = first + self.last = last + self.party = party + + def first_name(self): + print('{}'.format(self.first)) + + +def fullname(self): + print('{} {}'.format(self.first, self.last)) + +pres = President('george', 'wash', 'demo') + +# adding a method to a class +setattr(President, "first_name", fullname) + +pres.first_name() \ No newline at end of file diff --git a/pres_thread.py b/pres_thread.py new file mode 100644 index 0000000..dfa20dd --- /dev/null +++ b/pres_thread.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +import multiprocessing as mp +import datetime +import dateutil.parser +poolsize = 10 + +def date_to_obj(date_str: str): + return dateutil.parser.parse(date_str) + +# take to date objects +def date_subtract(line): + birth_date = line[0] + pres_date = line[1] + age = (pres_date - birth_date).days / 365.2425 + return round(age, 2) + +pool = mp.Pool(poolsize) + +# list of tuples consisting of +# birth date and innaguration date +# this will give age at inauguration +# converted to date obj +list_of_names = [(date_to_obj(line[3]), date_to_obj(line[7])) for line in (line.split(':') for line in open('./DATA/presidents.txt'))] + +# create new list holding the +# ages of the pres at inauguration +age_list = pool.map(date_subtract, list_of_names) + +for i in age_list: + print(i) \ No newline at end of file diff --git a/pres_upper.py b/pres_upper.py index e81a459..98b4f57 100644 --- a/pres_upper.py +++ b/pres_upper.py @@ -1,11 +1,12 @@ #!/usr/bin/env python last_names = [] -with open('/Users/jcasey/Downloads/presidents.txt') as PRES: +with open('./Data/presidents.txt') as PRES: for line in PRES: splitLine = line.split(':') last_names.append(splitLine[1]) +# list comprehension last_names = [name.upper() for name in last_names] for name in last_names: diff --git a/pres_upper_py_comprehension.py b/pres_upper_py_comprehension.py new file mode 100644 index 0000000..be3776e --- /dev/null +++ b/pres_upper_py_comprehension.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python + +#list comprehension +# read in data from file +# store as tuple in list +list_of_names = [(line[2], line[1]) for line in (line.split(':') for line in open('./DATA/presidents.txt'))] + + # use list comprehension + # to store upper forms of + # names +list_of_names_joined = [(name[0]+ ' ' + name[1]).upper() for name in list_of_names] + +for name in list_of_names_joined: + print(name) diff --git a/president.py b/president.py new file mode 100644 index 0000000..5687c15 --- /dev/null +++ b/president.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python + +class President: + + # constructor + def __init__(self, number): + self._get_data(number) + + # read in data from file, set the member vars accordingly + # if none match i.e. > 45 || < 0 raise error + def _get_data(self, number): + with open('./DATA/presidents.txt') as data: + for line in data: + splitLine = line.split(':') + if int(splitLine[0]) == int(number): + self._term_number = splitLine[0] + self._first_name = splitLine[2] + self._last_name = splitLine[1] + self._birth_date = splitLine[3] + self._death_date = splitLine[4] + self._birth_city = splitLine[5] + self._birth_state = splitLine[6] + self._term_start = splitLine[7] + self._term_end = splitLine[8] + self._party = splitLine[9] + break + else: + raise ValueError("Invalid input") + + # getters will return the member var value + @property + def term_number(self): + return self._term_number + @property + def first_name(self): + return self._first_name + + @property + def last_name(self): + return self._last_name + + @property + def birth_date(self): + return self._birth_date + + @property + def death_date(self): + return self._death_date + + @property + def birth_city(self): + return self._birth_city + + @property + def birth_state(self): + return self._birth_state + + @property + def term_start(self): + return self._term_start + + @property + def term_end(self): + return self._term_end + + @property + def party(self): + return self._party \ No newline at end of file diff --git a/president_main.py b/president_main.py new file mode 100644 index 0000000..d7e583d --- /dev/null +++ b/president_main.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python + +from president import President + +p = President(44) + +print(f'President {p.first_name} {p.last_name}, the {p.term_number}th president, was born in {p.birth_city} {p.birth_state}') \ No newline at end of file diff --git a/read_potus_info.py b/read_potus_info.py new file mode 100644 index 0000000..d7435d7 --- /dev/null +++ b/read_potus_info.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python +import pickle +from collections import namedtuple + +# named tuple must be recreated exactly +# in order to be deserialized +pres = namedtuple('pres', 'first last city state part') + +# use pickle module to read +# in serialized file +pres = pickle.load(open('./pres_list.pic', 'rb')) + +print(pres) \ No newline at end of file diff --git a/save_potous_info.py b/save_potous_info.py new file mode 100644 index 0000000..c50c2ba --- /dev/null +++ b/save_potous_info.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +from collections import namedtuple +import pickle + +# create named tuple +pres = namedtuple('pres', 'first last city state party') +pres_list = [] + +# add named tuple to a list +with open('./DATA/presidents.txt') as file: + for line in file: + splitLine = line.rstrip().split(':') + p = pres(splitLine[2], splitLine[1], splitLine[5], splitLine[6], splitLine[9]) + pres_list.append(p) + +# use pickle module to serialize +# the data +pickle.dump(pres_list, open("pres_list.pic", "wb")) \ No newline at end of file diff --git a/sillystring.py b/sillystring.py new file mode 100644 index 0000000..68aae7e --- /dev/null +++ b/sillystring.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +# print every other letter in +# the string +def every_other(self): + return(self.name[::2]) + +def set_up(self, name): + self.name = name + +# create a class programtically +# add the methods above to the +# class +SillyString = type("SillyString", (), { + "name":"lame", + "every_other": every_other, + "__init__": set_up + }) + +ss = SillyString('this is a testssssssssssssssssss') +print(ss.every_other()) \ No newline at end of file diff --git a/sum_of_values_reduce.py b/sum_of_values_reduce.py new file mode 100644 index 0000000..2f60a9c --- /dev/null +++ b/sum_of_values_reduce.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python + +from operator import add +from functools import reduce + + # add all float values using reduce and map + # lambda function converts str to float. + # using the add operator +result = reduce(add, list(map(float, open('./DATA/float_values.txt')))) + +print(f'{result:.2f}') \ No newline at end of file diff --git a/thread_web_crawler.py b/thread_web_crawler.py new file mode 100644 index 0000000..47fb37b --- /dev/null +++ b/thread_web_crawler.py @@ -0,0 +1,29 @@ +#!/usr/bin/evn python + +import threading +import requests +import re + +def get_links(body: str): + links = re.findall('"((http)?://.*?)"', body) + return links + +def get_website(url: str): + #gets html from website + html_data = requests.get(url) + return html_data.text + +def main(): + html = get_website('http://www.google.com') + found_links = get_links(html) + print(found_links) + threads = [] + for link in found_links: + try: + t = threading.Thread(target=get_website, args=(link,)) + threads.append(t) + t.start() + except: + pass + +main() \ No newline at end of file diff --git a/yahtzee.py b/yahtzee.py new file mode 100644 index 0000000..900ffc5 --- /dev/null +++ b/yahtzee.py @@ -0,0 +1,206 @@ +#!/usr/bin/env python + +from collections import Counter +from typing import List +import operator + +class Type(): + + ACES = "aces" + TWOS = "twos" + THREES = "threes" + FOURS = "fours" + FIVES = "fives" + SIXES = "sixes" + THREE_OF_A_KIND = "three_of_a_kind" + FOUR_OF_A_KIND = "four_of_a_kind" + FULL_HOUSE = "full_house" + YAHTZEE = "yahtzee" + SMALL_STRAIGHT = "small_straight" + LARGE_STRAIGHT = "large_straight" + CHANCE = "chance" + +class Roll(Type): + + def __init__(self, *args, **kwargs) -> None: + #try: + if len(args) == 5: + self.die1 = args[0] + self.die2 = args[1] + self.die3 = args[2] + self.die4 = args[3] + self.die5 = args[4] + self.die_list = [self.die1, self.die2, self.die3, self.die4, self.die5] + elif len(args) == 1: + self.die1 = args[0][0] + self.die2 = args[0][1] + self.die3 = args[0][2] + self.die4 = args[0][3] + self.die5 = args[0][4] + self.die_list = [self.die1, self.die2, self.die3, self.die4, self.die5] + elif len(args) < 5: + raise TypeError("Wrong number of args") + #except TypeError: + # pass + + def check_for_nums(self, type_of_check: int) -> int: + total = 0 + for i in self.die_list: + if i == type_of_check: + total += type_of_check + return total + + def multi_of_a_kind(self, num: int) -> int: + counter = Counter(self.die_list) + result = [i for i,j in counter.items() if j >= num] + if len(result) == 1: + return sum(self.die_list) + else: + return 0 + + # this is because i was getting an odd error on this test: + # self.assertEqual(Type.FOUR_OF_A_KIND, Roll(1, 1, 5, 1, 1).best_lower) + def multi_of_a_kind_alt_version(self, num: int) -> int: + counter = Counter(self.die_list) + result = [i for i,j in counter.items() if j == num] + if len(result) == 1: + return sum(self.die_list) + else: + return 0 + + + def is_consecutives(self, num_list_unsorted: List[int], num_of_consecutives: int) -> bool: + total_consecutive = 1 + num_list = sorted(num_list_unsorted) + for i in range(4): + if (num_list[i] + 1) == (num_list[i+1]): + total_consecutive += 1 + if total_consecutive >= num_of_consecutives: + return True + return False + + def is_full_house(self, num_list: List[int]) -> bool: + counter = Counter(num_list) + if len(counter) > 2: + return False + for key, value in counter.items(): + if value == 3 or value == 2: + return True + return False + + @property + def aces(self) -> int: + return self.check_for_nums(1) + + @property + def twos(self) -> int: + return self.check_for_nums(2) + + @property + def threes(self) -> int: + return self.check_for_nums(3) + + @property + def fours(self) -> int: + return self.check_for_nums(4) + + @property + def fives(self) -> int: + return self.check_for_nums(5) + + @property + def sixes(self) -> int: + return self.check_for_nums(6) + + @property + def three_of_a_kind(self) -> int: + return self.multi_of_a_kind(3) + + @property + def four_of_a_kind(self) -> int: + return self.multi_of_a_kind(4) + + @property + def full_house(self) -> int: + if self.is_full_house(self.die_list) == True: + return 25 + else: return 0 + + @property + def small_straight(self) -> int: + if (self.is_consecutives(self.die_list, 4)) == True: + return 30 + else: + return 0 + + @property + def large_straight(self) -> int: + if (self.is_consecutives(self.die_list, 5)) == True: + return 40 + else: + return 0 + + @property + def yahtzee(self) -> int: + num = self.multi_of_a_kind(5) + if num > 0: + return 50 + else: + return 0 + + @property + def chance(self) -> int: + return sum(self.die_list) + + @property + def best_upper(self) -> str: + scores = {} + scores[Type.ACES] = self.aces + scores[Type.TWOS] = self.twos + scores[Type.THREES] = self.threes + scores[Type.FOURS] = self.fours + scores[Type.FIVES] = self.fives + scores[Type.SIXES] = self.sixes + maximum = max(scores, key=scores.get) + return maximum + + @property + def best_lower(self) -> str: + scores = {} + scores[Type.THREE_OF_A_KIND] = self.multi_of_a_kind_alt_version(3) + scores[Type.FOUR_OF_A_KIND] = self.multi_of_a_kind_alt_version(4) + scores[Type.FULL_HOUSE] = self.full_house + scores[Type.SMALL_STRAIGHT] = self.small_straight + scores[Type.LARGE_STRAIGHT] = self.large_straight + scores[Type.YAHTZEE] = self.yahtzee + scores[Type.CHANCE] = self.chance + maximum = max(scores, key=scores.get) + return maximum + + def score_as(self, the_type: str) -> int: + if the_type == self.ACES: + return self.aces + elif the_type == self.TWOS: + return self.twos + elif the_type == self.THREES: + return self.threes + elif the_type == self.FOURS: + return self.fours + elif the_type == self.FIVES: + return self.fives + elif the_type == self.SIXES: + return self.sixes + elif the_type == self.THREE_OF_A_KIND: + return self.three_of_a_kind + elif the_type == self.FOUR_OF_A_KIND: + return self.four_of_a_kind + elif the_type == self.FULL_HOUSE: + return self.full_house + elif the_type == self.SMALL_STRAIGHT: + return self.small_straight + elif the_type == self.LARGE_STRAIGHT: + return self.large_straight + elif the_type == self.YAHTZEE: + return self.yahtzee + else: + return self.chance \ No newline at end of file