From 3431cee4eaee60f2f5564e9ff4d6380d1e976535 Mon Sep 17 00:00:00 2001 From: Adolfo Fumero Date: Sun, 12 Jan 2025 17:41:32 -0500 Subject: [PATCH] Added both of my challenges --- solutions/triangle_challenge/README.md | 37 ++++++++++++++++++ solutions/triangle_challenge/__init__.py | 0 solutions/triangle_challenge/main.py | 41 +++++++++++++++++++ solutions/voting_age/README.md | 47 ++++++++++++++++++++++ solutions/voting_age/__init__.py | 0 solutions/voting_age/main.py | 50 ++++++++++++++++++++++++ 6 files changed, 175 insertions(+) create mode 100644 solutions/triangle_challenge/README.md create mode 100644 solutions/triangle_challenge/__init__.py create mode 100644 solutions/triangle_challenge/main.py create mode 100644 solutions/voting_age/README.md create mode 100644 solutions/voting_age/__init__.py create mode 100644 solutions/voting_age/main.py diff --git a/solutions/triangle_challenge/README.md b/solutions/triangle_challenge/README.md new file mode 100644 index 000000000..ae0525ad9 --- /dev/null +++ b/solutions/triangle_challenge/README.md @@ -0,0 +1,37 @@ +# Challenge: Triangular Sequence Formula + +## Description + +Write a function that defines the triangular number sequence +with a formula +and input ability in order to insert several different +integer inputs + +The triangle sequence goes as follows: + +```python +1, 3, 6, 10, 15 +``` + +It is a formula of ***n(n+1)/2*** where ***n*** +is any given number in the sequence (easier read as {1+2+3+4...}) +If you wanted to check for the 5th number of the sequence, +It would look like 5(5+1)/2 = 15 +*Ex. Code* + +```python +triangle(1) ➞ 1 + +triangle(6) ➞ 21 + +triangle(215) ➞ 23220 +``` + +### Testing + +- Use assertion tests to check for integer inputs to +ensure the right output is given + +### Helpful links + +- [Triangular Number Sequence Formula](https://www.mathsisfun.com/algebra/triangular-numbers.html) diff --git a/solutions/triangle_challenge/__init__.py b/solutions/triangle_challenge/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/triangle_challenge/main.py b/solutions/triangle_challenge/main.py new file mode 100644 index 000000000..f6c8cf42a --- /dev/null +++ b/solutions/triangle_challenge/main.py @@ -0,0 +1,41 @@ +""" +Module for the Triangular number sequence and a code that can calculate it. + +Contents: +- A formula that establishes the triangular number sequence n(n+1)/2 +- Code for the formula to be able to take input + +Created on 2025-01-03 +Author: Adolfo Fumero +""" + + +def triangle_side(n): + """ + Calculate the nth triangular number. + Uses a formula of n(n+1)/2 to calculate the number of sides of the triangle in the sequence + + Parameters: + n (int): The position in the triangular number sequence. + + Returns: + int: The nth triangular number. + + Raises: + If any value below 0 is input, it will throw an Error message + If any non-integer value is input also, same output will occur + """ + if n < 1: + return "Input must be a positive integer." + return n * (n + 1) // 2 + + +# Example usage +try: + user_input = int( + input("Enter an integer value for the triangular number sequence: ") + ) + result = triangle_side(user_input) + print(f"The {user_input}th triangular number is: {result}") +except ValueError: + print("Please enter a valid integer.") diff --git a/solutions/voting_age/README.md b/solutions/voting_age/README.md new file mode 100644 index 000000000..d11e07e42 --- /dev/null +++ b/solutions/voting_age/README.md @@ -0,0 +1,47 @@ +# **Challenge: Voting Age Checker** + +## Description + +This code should be able to state and verify a voter's age +And compare it to a set voting age +In order to return a value containing the voter's name and +if they are old enough to vote + +Code should also ask for Input for the specific voter's name +Be able to return a correct value based on name + +This code should use *min_age* and a defined *can_vote* functions +in order to state and verify ages of all the listed voters + +It should also set the ages of the voters with their: + +1. Name +2. Age + +### Ex. Code + +```python +min_age = 18 + +participants = { + 'alice': {'age': 18}, + 'bob': {'age': 40}, + 'charlotte': {'age': 17}, +} + + +# Write a function that would allow +# A Boolean output corresponding to their age +# And if the person can vote + +``` + +### Testing + +- Use ```print``` unit tests that set age and compare back to the minimum age +- Use unit tests that check for precise spelling and comparisons + +### Helpful Links + +- [Python Functions](https://www.w3schools.com/python/python_functions.asp) +- [How to Define and Call back to a function](https://www.geeksforgeeks.org/how-to-define-and-call-a-function-in-python/) diff --git a/solutions/voting_age/__init__.py b/solutions/voting_age/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/voting_age/main.py b/solutions/voting_age/main.py new file mode 100644 index 000000000..e396d4a5f --- /dev/null +++ b/solutions/voting_age/main.py @@ -0,0 +1,50 @@ +""" +Module for the Voting age challenge. + +Contents: +- Contextual code for names and a list to refer back to +- Code that can refer back to minimum age and name list and produce an output base on name selection + +Created on 2025-01-03 +Author: Adolfo Fumero +""" + +MIN_AGE = 18 # this states the minimum age for voting + +participants = { + "Jackson": {"age": 18}, + "Bob": {"age": 40}, + "Charlotte": {"age": 17}, + "Marley": {"age": 28}, + "Erica": {"age": 6}, +} + + +def check_voting_age(person): + """ + General: + This function's goal is to check the age of the participants + and if they are equal + or above the minimum voting age based on input of name + It pulls name and age from the Participants list and provides a Boolean output + + Parameters: + Name: Correct name from Participants list + Name list: Jackson , Bob, Charlotte, Marley, Erica + + Raises: + If an invalid name is written, it will provide a "Participant not found." Output + Correct capitalization is required, otherwise "Participant not found" Error will be raised + """ + return person["age"] >= MIN_AGE + + +# Ask for the name input +name = input("Enter the participant's name (Jackson, Bob, Charlotte, Marley, Erica): ") + +# Check if the name exists in the participants dictionary +if name in participants: + result = check_voting_age(participants[name]) + print(f"Can {name} vote?: {result}") +else: + print("Participant not found.")