Skip to content

Commit

Permalink
Merge branch 'main' into challenge_28_perfect_numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Glucose authored Jan 13, 2025
2 parents c0e7e93 + d962fae commit d0fae9f
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Chrome",
"port": 9222,
"request": "attach",
"type": "chrome",
"webRoot": "${workspaceFolder}"
},
{
"name": "ET: Debug Python (unittest)",
"type": "debugpy",
Expand Down
9 changes: 8 additions & 1 deletion collaboration/learning_goals.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@

### Adolfo Fumero

- Improve my problem-tackling skills and creativity in my approach through
the challenges in this class
- Become more engaged with my peers and teachers in the MIT talents community
- Achieve computer programming skills that can help me in my day to day life
- Improve my organizational and productivity schedules in order to study
and learn better

---

### Ana Isabel Murillo
Expand Down Expand Up @@ -86,7 +93,7 @@

---

### Chrismy
### Chrismy Augustin

- Gain more knowledge and hands-on experience with industry-standard tools and
technologies like VS Code, Python, GitHub.
Expand Down
Empty file.
61 changes: 61 additions & 0 deletions solutions/challenge_23/celsius_to_fahrenheit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""conversion of temperature from celsius to fahrenheit."""


def celsius_to_fahrenheit(celsius_list):
"""
Convert a list of temperatures from Celsius to Fahrenheit.
Args:
celsius_list (list): List of temperatures in Celsius.
Returns:
list: List of temperatures converted to Fahrenheit.
Raises:
ValueError: If input is not a list or contains invalid elements.
>>> celsius_to_fahrenheit([0, 100])
[32.0, 212.0]
>>> celsius_to_fahrenheit([-40, -10])
[-40.0, 14.0]
>>> celsius_to_fahrenheit([25.5, 0.5])
[77.9, 32.9]
"""

if not isinstance(celsius_list, list):
raise ValueError("Input must be a list.")

fahrenheit_list = []

for temp in celsius_list:
if not isinstance(temp, (int, float)):
raise ValueError("List elements must be integers or floats.")

fahrenheit = (9 / 5) * temp + 32
fahrenheit_list.append(round(fahrenheit, 2))

return fahrenheit_list


if __name__ == "__main__":
user_input = input("Enter the temperature in celsius: ")

input_celsius_list = []
if not user_input.strip():
print("Error: No input provided.")
else:
try:
input_celsius_list = [float(temp.strip()) for temp in user_input.split(",")]
except ValueError:
print(
"Error: Invalid input. Please enter a list of numbers separated by commas."
)
input_celsius_list = []

try:
result = celsius_to_fahrenheit(input_celsius_list)
print("The list of temperature in fahrenheit is:", result)
except ValueError as e:
print("Error:", e)
Empty file.
45 changes: 45 additions & 0 deletions solutions/challenge_9/sum_of_digits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A module for calculating the sum of the digits of a positive integer.
Module contents:
sum_of_digits: calculates the sum of the digits of a positive integer.
Created on January 3rd, 2025
@author: Semira Tesfai
"""


def sum_of_digits(n: int) -> int:
"""Calculate the sum of the digits of a positive integer.
Parameters
----------
n : int
The non-negative integer whose digits will be summed.
Returns
-------
int
The sum of the digits of the given integer.
Raises
------
ValueError
If the argument is not a non-negative integer.
Examples
--------
>>> sum_of_digits(123)
6
>>> sum_of_digits(4567)
22
>>> sum_of_digits(0)
0
"""

if not isinstance(n, int) or n < 0:
raise ValueError("input must be a non-negative integer.")

return sum(int(digit) for digit in str(n))
36 changes: 36 additions & 0 deletions solutions/tests/challenge_23/test_celsius_to_fahrenheit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Test the celsius_to_fahrenheit function from the challenge_23 module."""

import unittest
from solutions.challenge_23.celsius_to_fahrenheit import celsius_to_fahrenheit


class TestCelsiusToFahrenheit(unittest.TestCase):
"""This tests module provide test for the celsius_to_fahrenheit function.
It tests the function with different inputs and checks if the output is as expected."""

def test__with_positive_temperatures(self):
"""Test the program with positive temperature list."""
self.assertEqual(celsius_to_fahrenheit([0, 100]), [32.0, 212.0])

def test_with_negative_temperatures(self):
"""Test the program with negative temperatures list."""
self.assertEqual(celsius_to_fahrenheit([-40, -10]), [-40.0, 14.0])

def test_with_mixed_temperatures(self):
"""Test the program with mixed temperatures list"""
self.assertEqual(celsius_to_fahrenheit([-10, 0, 25]), [14.0, 32.0, 77.0])

def test_float_temperatures(self):
"""Test the program with float temperatures."""
result = celsius_to_fahrenheit([0.5, -0.5])
self.assertAlmostEqual(result[0], 32.9, places=1)

def test_invalid_input_non_list(self):
"""Test invalid input that is not a list."""
with self.assertRaises(ValueError):
celsius_to_fahrenheit("not a list")

def test_invalid_input_non_numeric_elements(self):
"""Test the program with invalid input that contains non-numeric elements."""
with self.assertRaises(ValueError):
celsius_to_fahrenheit([30, "a string", 45])
Empty file.
39 changes: 39 additions & 0 deletions solutions/tests/challenge_9/test_sum_of_digits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Unit tests for the sum_of_digits function.
Created on January 3rd, 2025
"""

import unittest

from solutions.challenge_9.sum_of_digits import sum_of_digits


class TestSumOfDigits(unittest.TestCase):
"""Tests for the sum_of_digits function."""

def test_with_positive_integer(self):
"""Ensure that the sum of digits of a positive integer is calculated correctly."""
self.assertEqual(sum_of_digits(123), 6)

def test_with_large_integer(self):
"""Ensure that the sum of digits of a large positive integer is calculated correctly."""
self.assertEqual(sum_of_digits(4567), 22)

def test_with_zero(self):
"""Ensure that the sum of digits of zero is zero."""
self.assertEqual(sum_of_digits(0), 0)

def test_with_invalid_input(self):
"""Ensure that the function raises an assertion error for invalid inputs."""
with self.assertRaises(ValueError):
sum_of_digits(-5)
with self.assertRaises(ValueError):
sum_of_digits(4.5)
with self.assertRaises(ValueError):
sum_of_digits("123")


if __name__ == "__main__":
unittest.main()

0 comments on commit d0fae9f

Please sign in to comment.