Skip to content

Added: Birthday paradox plugin #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

</div>

# Table of Contents
# Table of Contents

- [📮 About](#-about)
- [🕹 Project Motivation](#-project-motivation)
Expand Down Expand Up @@ -132,6 +132,7 @@ GUI clients are used to manage plugins, launch bot as well as specify credential
| 🖼 Random Image | Returns a random image url. | [@rakeshseal0](https://github.com/rakeshseal0) |
| 🛢 URL Shortener | Shortens a url | [@rakeshseal0](https://github.com/rakeshseal0) |
| 😁 emoji | Returns emoji meaning | [@deadex-ng](https://github.com/deadex-ng) |
| :birthday: birthday | Shows birthday match probability on a people group. | [@paulosgf](https://github.com/paulosgf) |

## ⚡ Quickstart

Expand Down Expand Up @@ -186,14 +187,15 @@ you should see the bot as hbot ... or as it's name is in [settings](https://gith

## Docker

** Building Docker image **
**Building Docker image**

Change SERVER, PORT and NICKNAME variables to match your preferences

```
docker build -t "honeybot/honeybot:6.0.2" .
```

** Running Docker image **
**Running Docker image**

```
docker run -d --name=honeybot honeybot/honeybot:v6.0.2 honeybot run
Expand Down
Empty file.
16 changes: 16 additions & 0 deletions src/honeybot/plugins/downloaded/birthparadox/info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
NAME = "Birthday Paradox"

ORIGINAL_AUTHORS = ["Paulo Ferraz"]

ABOUT = """
Show the probability that two or more people
have the same birthday in the given group.
"""

COMMANDS = """
>>> .birth
returns help on plugin use

>>> .birth <<number>>
returns the birthday matches and probability in %.
"""
118 changes: 118 additions & 0 deletions src/honeybot/plugins/downloaded/birthparadox/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""
[birthparadox.py]
Bithday Paradox Plugin

[Author]
Paulo Ferraz

[About]
show the probability that two or more people
have the same birthday in the given group.

[Commands]
>>> .birth <<number>>
returns the birthday matches and probability in % of a group.
"""
import datetime
import random


class Plugin:
"""
Show the high probability that two or more people
have the same birthday, even in a small group!
"""

def __init__(self):
pass

"""
AUXILIARY FUNCTIONS
"""

def calc_probability(self, num):
"""
Calculate the match probability in %
"""
soma = 366
ano = 366
for i in range(num - 1):
soma *= ano - (i + 1)
perc = round((1 - (soma / 366**num)) * 100, 2)
return perc

def create_birthdays(self, num_people):
"""
Simulate some birthdays in a group
"""
start_date = datetime.date(1970, 1, 1)
birthday_group = []
while True:
birthday_group.clear()
for i in range(num_people):
birth = start_date + datetime.timedelta(random.randint(0, 366))
birthday_group.append(birth.strftime("%B, %d"))
if len(birthday_group) != len(set(birthday_group)):
break
return birthday_group

def find_match(self, birthday_group):
"""
Try to to find a match in a group
"""
matches = []
dates = ()
for i, first in enumerate(birthday_group):
for j, second in enumerate(birthday_group[i + 1 :]):
if first == second:
matches.append(first)
dates = set(matches)
return dates

def help(self, methods, info):
"""
Presentation and guidance function
"""
title = "THE BIRTHDAY PARADOX"
statement = (
"It's the high probability that two or more people have the same birthday, even in a"
" small group!"
)
use = (
"Use: .birth <<number>>, where num is the size of the group of people to calculate"
" probability."
)
methods["send"](info["address"], title)
methods["send"](info["address"], statement)
methods["send"](info["address"], use)

def run(self, incoming, methods, info, bot_info):
"""
Main function
"""
try:
params = info["args"][1].split()
if len(params) <= 1:
if info["command"] == "PRIVMSG" and params[0] == ".birth":
Plugin.help(self, methods, info)
if info["command"] == "PRIVMSG" and params[1]:
if params[1].isdecimal() and int(params[1]) >= 5:
num = int(params[1])
perc = Plugin.calc_probability(self, num)
group = Plugin.create_birthdays(self, num)
dates = "; ".join(Plugin.find_match(self, group))
methods["send"](
info["address"],
"In this simulation, we found these [{}] matches, but on average, ".format(
dates
),
)
methods["send"](
info["address"],
"this group has {} % of probability of having one or more people "
"with the same birthday!".format(perc),
)
else:
methods["send"](info["address"], "Please, inform integer value >= 5 only!")
except Exception as e:
print("woops!! birthparadox plugin error ", e)