Skip to content

Commit 20f3cc3

Browse files
committed
update: add all splits to workflows
1 parent af5aec2 commit 20f3cc3

File tree

2,055 files changed

+216209
-2032
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,055 files changed

+216209
-2032
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import itertools
2+
from random import shuffle
3+
4+
def f_1000(numbers=list(range(1, 3))):
5+
"""
6+
Calculates the average of the sums of absolute differences between each pair of consecutive numbers
7+
for all permutations of a given list. Each permutation is shuffled before calculating the differences.
8+
9+
Args:
10+
- numbers (list): A list of numbers. Default is numbers from 1 to 10.
11+
12+
Returns:
13+
float: The average of the sums of absolute differences for each shuffled permutation of the list.
14+
15+
Requirements:
16+
- itertools
17+
- random.shuffle
18+
19+
Example:
20+
>>> result = f_1000([1, 2, 3])
21+
>>> isinstance(result, float)
22+
True
23+
"""
24+
permutations = list(itertools.permutations(numbers))
25+
sum_diffs = 0
26+
27+
for perm in permutations:
28+
perm = list(perm)
29+
shuffle(perm)
30+
diffs = [abs(perm[i] - perm[i+1]) for i in range(len(perm)-1)]
31+
sum_diffs += sum(diffs)
32+
33+
avg_sum_diffs = sum_diffs / len(permutations)
34+
35+
return avg_sum_diffs
36+
37+
import unittest
38+
from unittest.mock import patch
39+
from random import seed, shuffle
40+
import itertools
41+
42+
def run_tests():
43+
suite = unittest.TestSuite()
44+
suite.addTest(unittest.makeSuite(TestCases))
45+
runner = unittest.TextTestRunner()
46+
runner.run(suite)
47+
48+
class TestCases(unittest.TestCase):
49+
def test_default_numbers(self):
50+
# Test with default number range (1 to 10) to check that the result is a positive float.
51+
result = f_1000()
52+
self.assertIsInstance(result, float)
53+
self.assertGreater(result, 0)
54+
55+
def test_custom_list(self):
56+
# Test with a custom list of small positive integers to ensure proper handling and positive result.
57+
result = f_1000([1, 2, 3])
58+
self.assertIsInstance(result, float)
59+
self.assertGreater(result, 0)
60+
61+
def test_negative_numbers(self):
62+
# Test with negative numbers to verify the function handles and returns a positive result.
63+
result = f_1000([-3, -2, -1])
64+
self.assertIsInstance(result, float)
65+
self.assertGreater(result, 0)
66+
67+
def test_single_element(self):
68+
# Test with a single element list to confirm the return is zero since no pairs exist.
69+
result = f_1000([5])
70+
self.assertIsInstance(result, float)
71+
self.assertEqual(result, 0)
72+
73+
def test_empty_list(self):
74+
# Test with an empty list to ensure the function handles it gracefully and returns zero.
75+
result = f_1000([])
76+
self.assertIsInstance(result, float)
77+
self.assertEqual(result, 0)
78+
79+
def test_identical_elements(self):
80+
# Test with a list of identical elements to confirm that differences are zero and the average is zero.
81+
result = f_1000([2, 2, 2])
82+
self.assertIsInstance(result, float)
83+
self.assertEqual(result, 0)
84+
85+
def test_mixed_numbers(self):
86+
# Test with a list of mixed positive and negative numbers to check correct average of differences.
87+
result = f_1000([-10, 10, -5])
88+
self.assertIsInstance(result, float)
89+
self.assertGreater(result, 0)
90+
91+
def test_specific_value_with_seed(self):
92+
# Set seed for reproducibility and check the computed value
93+
with patch('random.shuffle', side_effect=lambda x: seed(42) or shuffle(x)):
94+
result = f_1000([1, 2, 3])
95+
self.assertAlmostEqual(result, 2.5, delta=0.5) # This expected value should be calculated beforehand
96+
97+
def test_large_list_with_seed(self):
98+
# Set seed and test with a larger list for specific computed value
99+
with patch('random.shuffle', side_effect=lambda x: seed(99) or shuffle(x)):
100+
result = f_1000(list(range(1, 11)))
101+
self.assertAlmostEqual(result, 33.0, delta=0.5) # This expected value should be calculated beforehand
102+
103+
def test_random_behavior(self):
104+
# Test to ensure different seeds produce different outputs, demonstrating randomness
105+
with patch('random.shuffle', side_effect=lambda x: seed(1) or shuffle(x)):
106+
result1 = f_1000([1, 2, 3])
107+
with patch('random.shuffle', side_effect=lambda x: seed(1) or shuffle(x)):
108+
result2 = f_1000([1, 2, 4])
109+
self.assertNotEqual(result1, result2)
110+
111+
if __name__ == "__main__":
112+
run_tests()
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import collections
2+
import random
3+
import string
4+
5+
def f_1001(length=100):
6+
"""
7+
Generate a random string of the specified length composed of uppercase and lowercase letters,
8+
and then count the occurrence of each character in this string.
9+
10+
Parameters:
11+
length (int, optional): The number of characters in the generated string. Default is 100.
12+
13+
Returns:
14+
dict: A dictionary where each key is a character from the generated string and the value
15+
is the count of how many times that character appears in the string.
16+
17+
Requirements:
18+
- collections
19+
- random
20+
- string
21+
22+
Raises:
23+
ValueError if the length is a negative number
24+
25+
Example:
26+
>>> import random
27+
>>> random.seed(42) # Ensures reproducibility for demonstration
28+
>>> f_1001(10)
29+
{'h': 1, 'B': 2, 'O': 1, 'L': 1, 'm': 1, 'j': 1, 'u': 1, 'E': 1, 'V': 1}
30+
"""
31+
if length < 0:
32+
raise ValueError
33+
random_string = ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase, k=length))
34+
char_counts = collections.Counter(random_string)
35+
return dict(char_counts)
36+
37+
import unittest
38+
import string
39+
40+
def run_tests():
41+
suite = unittest.TestSuite()
42+
suite.addTest(unittest.makeSuite(TestCases))
43+
runner = unittest.TextTestRunner()
44+
runner.run(suite)
45+
46+
class TestCases(unittest.TestCase):
47+
def setUp(self):
48+
# Prepare valid characters and set a random seed for reproducibility
49+
self.valid_chars = string.ascii_uppercase + string.ascii_lowercase
50+
random.seed(42) # Ensuring reproducibility for tests
51+
52+
def test_generated_string_properties(self):
53+
# Consolidated test for different lengths to check structure and correctness
54+
test_lengths = [10, 50, 100, 150, 5]
55+
for length in test_lengths:
56+
with self.subTest(length=length):
57+
result = f_1001(length)
58+
self.assertTrue(len(result) <= length, "Length of result should be <= requested string length")
59+
self.assertEqual(sum(result.values()), length, f"Total counts should sum to {length}")
60+
self.assertTrue(all(char in self.valid_chars for char in result), "All characters should be valid letters")
61+
62+
def test_zero_length(self):
63+
# Test edge case where length is zero
64+
result = f_1001(0)
65+
self.assertEqual(len(result), 0, "Result should be empty for zero length")
66+
self.assertEqual(sum(result.values()), 0, "Sum of counts should be zero for zero length")
67+
68+
def test_negative_length(self):
69+
# Test handling of negative length input
70+
with self.assertRaises(ValueError, msg="Negative length should raise an error"):
71+
f_1001(-1)
72+
73+
if __name__ == "__main__":
74+
run_tests()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import random
2+
import statistics
3+
4+
def f_1002(LETTERS):
5+
"""
6+
Create a dictionary in which keys are random letters and values are lists of random integers.
7+
The dictionary is then sorted by the mean of the values in descending order, demonstrating the use of the statistics library.
8+
9+
Parameters:
10+
LETTERS (list of str): A list of characters used as keys for the dictionary.
11+
12+
Returns:
13+
dict: The sorted dictionary with letters as keys and lists of integers as values, sorted by their mean values.
14+
15+
Requirements:
16+
- random
17+
- statistics
18+
19+
Example:
20+
>>> import random
21+
>>> random.seed(42)
22+
>>> sorted_dict = f_1002(['a', 'b', 'c'])
23+
>>> list(sorted_dict.keys())
24+
['a', 'b', 'c']
25+
>>> isinstance(sorted_dict['a'], list)
26+
True
27+
>>> type(sorted_dict['a']) # Check type of values
28+
<class 'list'>
29+
"""
30+
random_dict = {k: [random.randint(0, 100) for _ in range(random.randint(1, 10))] for k in LETTERS}
31+
sorted_dict = dict(sorted(random_dict.items(), key=lambda item: statistics.mean(item[1]), reverse=True))
32+
return sorted_dict
33+
34+
import unittest
35+
36+
def run_tests():
37+
suite = unittest.TestSuite()
38+
suite.addTest(unittest.makeSuite(TestCases))
39+
runner = unittest.TextTestRunner()
40+
runner.run(suite)
41+
42+
class TestCases(unittest.TestCase):
43+
44+
def setUp(self):
45+
# Setting up a common letters array and sorted dictionary for use in all tests
46+
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']
47+
self.sorted_dict = f_1002(self.letters)
48+
49+
def test_case_1(self):
50+
# Check if the function returns a dictionary
51+
self.assertIsInstance(self.sorted_dict, dict, "The function should return a dictionary.")
52+
53+
def test_case_2(self):
54+
# Ensure all keys in the sorted dictionary are within the provided letters
55+
all_letters = all([key in self.letters for key in self.sorted_dict.keys()])
56+
self.assertTrue(all_letters, "All keys of the dictionary should be letters.")
57+
58+
def test_case_3(self):
59+
# Ensure all values are lists of integers
60+
all_lists = all([isinstance(val, list) and all(isinstance(i, int) for i in val) for val in self.sorted_dict.values()])
61+
self.assertTrue(all_lists, "All values of the dictionary should be lists of integers.")
62+
63+
def test_case_4(self):
64+
# Check if the dictionary is sorted by the mean values in descending order
65+
means = [statistics.mean(val) for val in self.sorted_dict.values()]
66+
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.")
67+
68+
def test_case_5(self):
69+
# Check if the dictionary includes all provided letters as keys
70+
self.assertEqual(set(self.sorted_dict.keys()), set(self.letters), "The dictionary should have all provided letters as keys.")
71+
72+
if __name__ == "__main__":
73+
run_tests()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import random
2+
import numpy as np
3+
4+
def f_1003(LETTERS):
5+
"""
6+
Create a dictionary where keys are specified letters and values are lists of random integers.
7+
Then calculate the mean of these integers for each key and return a dictionary of these means.
8+
9+
Parameters:
10+
LETTERS (list of str): List of single-character strings to be used as keys in the output dictionary.
11+
12+
Returns:
13+
dict: A dictionary where each key is a letter from the input list and the value is the mean of
14+
a randomly generated list of integers (with each list having 1 to 10 integers ranging from 0 to 100).
15+
16+
Requirements:
17+
- random
18+
- np (numpy)
19+
20+
Example:
21+
>>> LETTERS = ['a', 'b', 'c']
22+
>>> mean_dict = f_1003(LETTERS)
23+
>>> isinstance(mean_dict, dict)
24+
True
25+
>>> 'a' in mean_dict.keys() and 'b' in mean_dict.keys() and 'c' in mean_dict.keys()
26+
True
27+
>>> all(isinstance(v, float) for v in mean_dict.values()) # Check if all values are floats
28+
True
29+
"""
30+
random_dict = {k: [random.randint(0, 100) for _ in range(random.randint(1, 10))] for k in LETTERS}
31+
mean_dict = {k: np.mean(v) for k, v in random_dict.items()}
32+
return mean_dict
33+
34+
import unittest
35+
36+
def run_tests():
37+
suite = unittest.TestSuite()
38+
suite.addTest(unittest.makeSuite(TestCases))
39+
runner = unittest.TextTestRunner()
40+
runner.run(suite)
41+
42+
class TestCases(unittest.TestCase):
43+
def setUp(self):
44+
# Common setup for all tests: explicitly define the list of letters
45+
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']
46+
47+
def test_case_1(self):
48+
# Test if the function returns a dictionary
49+
mean_dict = f_1003(self.letters)
50+
self.assertIsInstance(mean_dict, dict)
51+
52+
def test_case_2(self):
53+
# Test if the dictionary contains all letters of the alphabet
54+
mean_dict = f_1003(self.letters)
55+
self.assertTrue(all(letter in mean_dict for letter in self.letters))
56+
57+
def test_case_3(self):
58+
# Test if the values in the dictionary are floats (means of lists of integers)
59+
mean_dict = f_1003(self.letters)
60+
self.assertTrue(all(isinstance(val, float) for val in mean_dict.values()))
61+
62+
def test_case_4(self):
63+
# Test if the mean values are reasonable given the range of random integers (0-100)
64+
mean_dict = f_1003(self.letters)
65+
self.assertTrue(all(0 <= val <= 100 for val in mean_dict.values()))
66+
67+
def test_case_5(self):
68+
# Test if the dictionary has 26 keys (one for each letter of the alphabet)
69+
mean_dict = f_1003(self.letters)
70+
self.assertEqual(len(mean_dict), 26)
71+
72+
if __name__ == "__main__":
73+
run_tests()

0 commit comments

Comments
 (0)