diff --git a/Python/Python-Lottery-Simulation/lottery.py b/Python/Python-Lottery-Simulation/lottery.py new file mode 100644 index 000000000..3fd4423e4 --- /dev/null +++ b/Python/Python-Lottery-Simulation/lottery.py @@ -0,0 +1,105 @@ +import random +import json + +white_possibles = list(range(1, 70)) +red_possibles = list(range(1, 27)) + +tickets_per_drawing = 100 +num_drawings = 15600 + +total_spent = 0 +earnings = 0 + +times_won = { + "5+P": 0, + "5": 0, + "4+P": 0, + "4": 0, + "3+P": 0, + "3": 0, + "2+P": 0, + "1+P": 0, + "P": 0, + "0": 0, +} + + +def calc_win_amt(my_numbers, winning_numbers): + win_amt = 0 + + white_matches = len(my_numbers["whites"].intersection( + winning_numbers["whites"])) + power_match = my_numbers["red"] == winning_numbers["red"] + + if white_matches == 5: + if power_match: + win_amt = 2_000_000_000 + times_won["5+P"] += 1 + else: + win_amt = 1_000_000 + times_won["5"] += 1 + elif white_matches == 4: + if power_match: + win_amt = 50_000 + times_won["4+P"] += 1 + else: + win_amt = 100 + times_won["4"] += 1 + elif white_matches == 3: + if power_match: + win_amt = 100 + times_won["3+P"] += 1 + else: + win_amt = 7 + times_won["3"] += 1 + elif white_matches == 2 and power_match: + win_amt = 7 + times_won["2+P"] += 1 + elif white_matches == 1 and power_match: + win_amt = 4 + times_won["1+P"] += 1 + elif power_match: + win_amt = 4 + times_won["P"] += 1 + else: + times_won["0"] += 1 + + return win_amt + + +# for drawing in range(num_drawings): +hit_jp = False +drawings = 0 +years = 0 +while True: + drawings += 1 + white_drawing = set(random.sample(white_possibles, k=5)) + red_drawing = random.choice(red_possibles) + + winning_numbers = {"whites": white_drawing, "red": red_drawing} + + for ticket in range(tickets_per_drawing): + total_spent += 2 + my_whites = set(random.sample(white_possibles, k=5)) + my_red = random.choice(red_possibles) + + my_numbers = {"whites": my_whites, "red": my_red} + + win_amt = calc_win_amt(my_numbers, winning_numbers) + earnings += win_amt + + if win_amt == 2_000_000_000: + hit_jp = True + break + + if drawings % 156 == 0: + years += 1 + print(f'{years} years') + + if hit_jp: + break + +print(f'Spent: ${total_spent}') +print(f'Earnings: ${earnings}') + +print(json.dumps(times_won, indent=2)) diff --git a/Python/Python-Lottery-Simulation/snippets.txt b/Python/Python-Lottery-Simulation/snippets.txt new file mode 100644 index 000000000..6771c9787 --- /dev/null +++ b/Python/Python-Lottery-Simulation/snippets.txt @@ -0,0 +1,40 @@ +times_won = { + '5+P': 0, + '5': 0, + '4+P': 0, + '4': 0, + '3+P': 0, + '3': 0, + '2+P': 0, + '1+P': 0, + 'P': 0, + '0': 0 +} + +------------------------ + +elif white_matches == 4: + if power_match: + win_amt = 50_000 + times_won['4+P'] += 1 + else: + win_amt = 100 + times_won['4'] += 1 + elif white_matches == 3: + if power_match: + win_amt = 100 + times_won['3+P'] += 1 + else: + win_amt = 7 + times_won['3'] += 1 + elif white_matches == 2 and power_match: + win_amt = 7 + times_won['2+P'] += 1 + elif white_matches == 1 and power_match: + win_amt = 4 + times_won['1+P'] += 1 + elif power_match: + win_amt = 4 + times_won['P'] += 1 + else: + times_won['0'] += 1 diff --git a/Python/YouTube-API/01-Getting-Started/yt.py b/Python/YouTube-API/01-Getting-Started/yt.py new file mode 100644 index 000000000..81556c738 --- /dev/null +++ b/Python/YouTube-API/01-Getting-Started/yt.py @@ -0,0 +1,15 @@ + +from googleapiclient.discovery import build + +api_key = '#YOURAPIKEY' + +youtube = build('youtube', 'v3', developerKey=api_key) + +request = youtube.channels().list( + part='statistics', + forUsername='schafer5' + ) + +response = request.execute() + +print(response) diff --git a/Python/YouTube-API/02-Playlist-Duration/playlist.py b/Python/YouTube-API/02-Playlist-Duration/playlist.py new file mode 100644 index 000000000..49583b4a2 --- /dev/null +++ b/Python/YouTube-API/02-Playlist-Duration/playlist.py @@ -0,0 +1,68 @@ +import os +import re +from datetime import timedelta +from googleapiclient.discovery import build + +api_key = os.environ.get('YT_API_KEY') + +youtube = build('youtube', 'v3', developerKey=api_key) + +hours_pattern = re.compile(r'(\d+)H') +minutes_pattern = re.compile(r'(\d+)M') +seconds_pattern = re.compile(r'(\d+)S') + +total_seconds = 0 + + +nextPageToken = None +while True: + pl_request = youtube.playlistItems().list( + part='contentDetails', + playlistId="PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU", + maxResults=50, + pageToken=nextPageToken + ) + + pl_response = pl_request.execute() + + vid_ids = [] + for item in pl_response['items']: + vid_ids.append(item['contentDetails']['videoId']) + + vid_request = youtube.videos().list( + part="contentDetails", + id=','.join(vid_ids) + ) + + vid_response = vid_request.execute() + + for item in vid_response['items']: + duration = item['contentDetails']['duration'] + + hours = hours_pattern.search(duration) + minutes = minutes_pattern.search(duration) + seconds = seconds_pattern.search(duration) + + hours = int(hours.group(1)) if hours else 0 + minutes = int(minutes.group(1)) if minutes else 0 + seconds = int(seconds.group(1)) if seconds else 0 + + video_seconds = timedelta( + hours=hours, + minutes=minutes, + seconds=seconds + ).total_seconds() + + total_seconds += video_seconds + + nextPageToken = pl_response.get('nextPageToken') + + if not nextPageToken: + break + +total_seconds = int(total_seconds) + +minutes, seconds = divmod(total_seconds, 60) +hours, minutes = divmod(minutes, 60) + +print(f'{hours}:{minutes}:{seconds}') diff --git a/Python/YouTube-API/03-Most-Popular-Video-Playlist/start.py b/Python/YouTube-API/03-Most-Popular-Video-Playlist/start.py new file mode 100644 index 000000000..a76d410ff --- /dev/null +++ b/Python/YouTube-API/03-Most-Popular-Video-Playlist/start.py @@ -0,0 +1,55 @@ +import os +from googleapiclient.discovery import build + +api_key = os.environ.get('YT_API_KEY') + +youtube = build('youtube', 'v3', developerKey=api_key) + +playlist_id = 'PL8uoeex94UhHFRew8gzfFJHIpRFWyY4YW' + +videos = [] + +nextPageToken = None +while True: + pl_request = youtube.playlistItems().list( + part='contentDetails', + playlistId=playlist_id, + maxResults=50, + pageToken=nextPageToken + ) + + pl_response = pl_request.execute() + + vid_ids = [] + for item in pl_response['items']: + vid_ids.append(item['contentDetails']['videoId']) + + vid_request = youtube.videos().list( + part="statistics", + id=','.join(vid_ids) + ) + + vid_response = vid_request.execute() + + for item in vid_response['items']: + vid_views = item['statistics']['viewCount'] + + vid_id = item['id'] + yt_link = f'https://youtu.be/{vid_id}' + + videos.append( + { + 'views': int(vid_views), + 'url': yt_link + } + ) + + nextPageToken = pl_response.get('nextPageToken') + + if not nextPageToken: + break + +videos.sort(key=lambda vid: vid['views'], reverse=True) + +for video in videos[:10]: + print(video['url'], video['views'])