-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2,055 changed files
with
216,209 additions
and
2,032 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import itertools | ||
from random import shuffle | ||
|
||
def f_1000(numbers=list(range(1, 3))): | ||
""" | ||
Calculates the average of the sums of absolute differences between each pair of consecutive numbers | ||
for all permutations of a given list. Each permutation is shuffled before calculating the differences. | ||
Args: | ||
- numbers (list): A list of numbers. Default is numbers from 1 to 10. | ||
Returns: | ||
float: The average of the sums of absolute differences for each shuffled permutation of the list. | ||
Requirements: | ||
- itertools | ||
- random.shuffle | ||
Example: | ||
>>> result = f_1000([1, 2, 3]) | ||
>>> isinstance(result, float) | ||
True | ||
""" | ||
permutations = list(itertools.permutations(numbers)) | ||
sum_diffs = 0 | ||
|
||
for perm in permutations: | ||
perm = list(perm) | ||
shuffle(perm) | ||
diffs = [abs(perm[i] - perm[i+1]) for i in range(len(perm)-1)] | ||
sum_diffs += sum(diffs) | ||
|
||
avg_sum_diffs = sum_diffs / len(permutations) | ||
|
||
return avg_sum_diffs | ||
|
||
import unittest | ||
from unittest.mock import patch | ||
from random import seed, shuffle | ||
import itertools | ||
|
||
def run_tests(): | ||
suite = unittest.TestSuite() | ||
suite.addTest(unittest.makeSuite(TestCases)) | ||
runner = unittest.TextTestRunner() | ||
runner.run(suite) | ||
|
||
class TestCases(unittest.TestCase): | ||
def test_default_numbers(self): | ||
# Test with default number range (1 to 10) to check that the result is a positive float. | ||
result = f_1000() | ||
self.assertIsInstance(result, float) | ||
self.assertGreater(result, 0) | ||
|
||
def test_custom_list(self): | ||
# Test with a custom list of small positive integers to ensure proper handling and positive result. | ||
result = f_1000([1, 2, 3]) | ||
self.assertIsInstance(result, float) | ||
self.assertGreater(result, 0) | ||
|
||
def test_negative_numbers(self): | ||
# Test with negative numbers to verify the function handles and returns a positive result. | ||
result = f_1000([-3, -2, -1]) | ||
self.assertIsInstance(result, float) | ||
self.assertGreater(result, 0) | ||
|
||
def test_single_element(self): | ||
# Test with a single element list to confirm the return is zero since no pairs exist. | ||
result = f_1000([5]) | ||
self.assertIsInstance(result, float) | ||
self.assertEqual(result, 0) | ||
|
||
def test_empty_list(self): | ||
# Test with an empty list to ensure the function handles it gracefully and returns zero. | ||
result = f_1000([]) | ||
self.assertIsInstance(result, float) | ||
self.assertEqual(result, 0) | ||
|
||
def test_identical_elements(self): | ||
# Test with a list of identical elements to confirm that differences are zero and the average is zero. | ||
result = f_1000([2, 2, 2]) | ||
self.assertIsInstance(result, float) | ||
self.assertEqual(result, 0) | ||
|
||
def test_mixed_numbers(self): | ||
# Test with a list of mixed positive and negative numbers to check correct average of differences. | ||
result = f_1000([-10, 10, -5]) | ||
self.assertIsInstance(result, float) | ||
self.assertGreater(result, 0) | ||
|
||
def test_specific_value_with_seed(self): | ||
# Set seed for reproducibility and check the computed value | ||
with patch('random.shuffle', side_effect=lambda x: seed(42) or shuffle(x)): | ||
result = f_1000([1, 2, 3]) | ||
self.assertAlmostEqual(result, 2.5, delta=0.5) # This expected value should be calculated beforehand | ||
|
||
def test_large_list_with_seed(self): | ||
# Set seed and test with a larger list for specific computed value | ||
with patch('random.shuffle', side_effect=lambda x: seed(99) or shuffle(x)): | ||
result = f_1000(list(range(1, 11))) | ||
self.assertAlmostEqual(result, 33.0, delta=0.5) # This expected value should be calculated beforehand | ||
|
||
def test_random_behavior(self): | ||
# Test to ensure different seeds produce different outputs, demonstrating randomness | ||
with patch('random.shuffle', side_effect=lambda x: seed(1) or shuffle(x)): | ||
result1 = f_1000([1, 2, 3]) | ||
with patch('random.shuffle', side_effect=lambda x: seed(1) or shuffle(x)): | ||
result2 = f_1000([1, 2, 4]) | ||
self.assertNotEqual(result1, result2) | ||
|
||
if __name__ == "__main__": | ||
run_tests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import collections | ||
import random | ||
import string | ||
|
||
def f_1001(length=100): | ||
""" | ||
Generate a random string of the specified length composed of uppercase and lowercase letters, | ||
and then count the occurrence of each character in this string. | ||
Parameters: | ||
length (int, optional): The number of characters in the generated string. Default is 100. | ||
Returns: | ||
dict: A dictionary where each key is a character from the generated string and the value | ||
is the count of how many times that character appears in the string. | ||
Requirements: | ||
- collections | ||
- random | ||
- string | ||
Raises: | ||
ValueError if the length is a negative number | ||
Example: | ||
>>> import random | ||
>>> random.seed(42) # Ensures reproducibility for demonstration | ||
>>> f_1001(10) | ||
{'h': 1, 'B': 2, 'O': 1, 'L': 1, 'm': 1, 'j': 1, 'u': 1, 'E': 1, 'V': 1} | ||
""" | ||
if length < 0: | ||
raise ValueError | ||
random_string = ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase, k=length)) | ||
char_counts = collections.Counter(random_string) | ||
return dict(char_counts) | ||
|
||
import unittest | ||
import string | ||
|
||
def run_tests(): | ||
suite = unittest.TestSuite() | ||
suite.addTest(unittest.makeSuite(TestCases)) | ||
runner = unittest.TextTestRunner() | ||
runner.run(suite) | ||
|
||
class TestCases(unittest.TestCase): | ||
def setUp(self): | ||
# Prepare valid characters and set a random seed for reproducibility | ||
self.valid_chars = string.ascii_uppercase + string.ascii_lowercase | ||
random.seed(42) # Ensuring reproducibility for tests | ||
|
||
def test_generated_string_properties(self): | ||
# Consolidated test for different lengths to check structure and correctness | ||
test_lengths = [10, 50, 100, 150, 5] | ||
for length in test_lengths: | ||
with self.subTest(length=length): | ||
result = f_1001(length) | ||
self.assertTrue(len(result) <= length, "Length of result should be <= requested string length") | ||
self.assertEqual(sum(result.values()), length, f"Total counts should sum to {length}") | ||
self.assertTrue(all(char in self.valid_chars for char in result), "All characters should be valid letters") | ||
|
||
def test_zero_length(self): | ||
# Test edge case where length is zero | ||
result = f_1001(0) | ||
self.assertEqual(len(result), 0, "Result should be empty for zero length") | ||
self.assertEqual(sum(result.values()), 0, "Sum of counts should be zero for zero length") | ||
|
||
def test_negative_length(self): | ||
# Test handling of negative length input | ||
with self.assertRaises(ValueError, msg="Negative length should raise an error"): | ||
f_1001(-1) | ||
|
||
if __name__ == "__main__": | ||
run_tests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import random | ||
import statistics | ||
|
||
def f_1002(LETTERS): | ||
""" | ||
Create a dictionary in which keys are random letters and values are lists of random integers. | ||
The dictionary is then sorted by the mean of the values in descending order, demonstrating the use of the statistics library. | ||
Parameters: | ||
LETTERS (list of str): A list of characters used as keys for the dictionary. | ||
Returns: | ||
dict: The sorted dictionary with letters as keys and lists of integers as values, sorted by their mean values. | ||
Requirements: | ||
- random | ||
- statistics | ||
Example: | ||
>>> import random | ||
>>> random.seed(42) | ||
>>> sorted_dict = f_1002(['a', 'b', 'c']) | ||
>>> list(sorted_dict.keys()) | ||
['a', 'b', 'c'] | ||
>>> isinstance(sorted_dict['a'], list) | ||
True | ||
>>> type(sorted_dict['a']) # Check type of values | ||
<class 'list'> | ||
""" | ||
random_dict = {k: [random.randint(0, 100) for _ in range(random.randint(1, 10))] for k in LETTERS} | ||
sorted_dict = dict(sorted(random_dict.items(), key=lambda item: statistics.mean(item[1]), reverse=True)) | ||
return sorted_dict | ||
|
||
import unittest | ||
|
||
def run_tests(): | ||
suite = unittest.TestSuite() | ||
suite.addTest(unittest.makeSuite(TestCases)) | ||
runner = unittest.TextTestRunner() | ||
runner.run(suite) | ||
|
||
class TestCases(unittest.TestCase): | ||
|
||
def setUp(self): | ||
# Setting up a common letters array and sorted dictionary for use in all tests | ||
self.letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] | ||
self.sorted_dict = f_1002(self.letters) | ||
|
||
def test_case_1(self): | ||
# Check if the function returns a dictionary | ||
self.assertIsInstance(self.sorted_dict, dict, "The function should return a dictionary.") | ||
|
||
def test_case_2(self): | ||
# Ensure all keys in the sorted dictionary are within the provided letters | ||
all_letters = all([key in self.letters for key in self.sorted_dict.keys()]) | ||
self.assertTrue(all_letters, "All keys of the dictionary should be letters.") | ||
|
||
def test_case_3(self): | ||
# Ensure all values are lists of integers | ||
all_lists = all([isinstance(val, list) and all(isinstance(i, int) for i in val) for val in self.sorted_dict.values()]) | ||
self.assertTrue(all_lists, "All values of the dictionary should be lists of integers.") | ||
|
||
def test_case_4(self): | ||
# Check if the dictionary is sorted by the mean values in descending order | ||
means = [statistics.mean(val) for val in self.sorted_dict.values()] | ||
self.assertTrue(all(means[i] >= means[i + 1] for i in range(len(means) - 1)), "The dictionary should be sorted in descending order based on the mean of its values.") | ||
|
||
def test_case_5(self): | ||
# Check if the dictionary includes all provided letters as keys | ||
self.assertEqual(set(self.sorted_dict.keys()), set(self.letters), "The dictionary should have all provided letters as keys.") | ||
|
||
if __name__ == "__main__": | ||
run_tests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import random | ||
import numpy as np | ||
|
||
def f_1003(LETTERS): | ||
""" | ||
Create a dictionary where keys are specified letters and values are lists of random integers. | ||
Then calculate the mean of these integers for each key and return a dictionary of these means. | ||
Parameters: | ||
LETTERS (list of str): List of single-character strings to be used as keys in the output dictionary. | ||
Returns: | ||
dict: A dictionary where each key is a letter from the input list and the value is the mean of | ||
a randomly generated list of integers (with each list having 1 to 10 integers ranging from 0 to 100). | ||
Requirements: | ||
- random | ||
- np (numpy) | ||
Example: | ||
>>> LETTERS = ['a', 'b', 'c'] | ||
>>> mean_dict = f_1003(LETTERS) | ||
>>> isinstance(mean_dict, dict) | ||
True | ||
>>> 'a' in mean_dict.keys() and 'b' in mean_dict.keys() and 'c' in mean_dict.keys() | ||
True | ||
>>> all(isinstance(v, float) for v in mean_dict.values()) # Check if all values are floats | ||
True | ||
""" | ||
random_dict = {k: [random.randint(0, 100) for _ in range(random.randint(1, 10))] for k in LETTERS} | ||
mean_dict = {k: np.mean(v) for k, v in random_dict.items()} | ||
return mean_dict | ||
|
||
import unittest | ||
|
||
def run_tests(): | ||
suite = unittest.TestSuite() | ||
suite.addTest(unittest.makeSuite(TestCases)) | ||
runner = unittest.TextTestRunner() | ||
runner.run(suite) | ||
|
||
class TestCases(unittest.TestCase): | ||
def setUp(self): | ||
# Common setup for all tests: explicitly define the list of letters | ||
self.letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] | ||
|
||
def test_case_1(self): | ||
# Test if the function returns a dictionary | ||
mean_dict = f_1003(self.letters) | ||
self.assertIsInstance(mean_dict, dict) | ||
|
||
def test_case_2(self): | ||
# Test if the dictionary contains all letters of the alphabet | ||
mean_dict = f_1003(self.letters) | ||
self.assertTrue(all(letter in mean_dict for letter in self.letters)) | ||
|
||
def test_case_3(self): | ||
# Test if the values in the dictionary are floats (means of lists of integers) | ||
mean_dict = f_1003(self.letters) | ||
self.assertTrue(all(isinstance(val, float) for val in mean_dict.values())) | ||
|
||
def test_case_4(self): | ||
# Test if the mean values are reasonable given the range of random integers (0-100) | ||
mean_dict = f_1003(self.letters) | ||
self.assertTrue(all(0 <= val <= 100 for val in mean_dict.values())) | ||
|
||
def test_case_5(self): | ||
# Test if the dictionary has 26 keys (one for each letter of the alphabet) | ||
mean_dict = f_1003(self.letters) | ||
self.assertEqual(len(mean_dict), 26) | ||
|
||
if __name__ == "__main__": | ||
run_tests() |
Oops, something went wrong.