Skip to content

Commit

Permalink
update: add zhihan data
Browse files Browse the repository at this point in the history
Refine Zhihan Split 981-1031
  • Loading branch information
terryyz authored May 7, 2024
2 parents b1d0a69 + c943772 commit 3f180c1
Show file tree
Hide file tree
Showing 91 changed files with 4,741 additions and 3,920 deletions.
82 changes: 0 additions & 82 deletions data/raw/f_1000_zhihan.py

This file was deleted.

112 changes: 112 additions & 0 deletions data/raw/f_1000_zhihan_refined.py
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()
84 changes: 0 additions & 84 deletions data/raw/f_1001_zhihan.py

This file was deleted.

74 changes: 74 additions & 0 deletions data/raw/f_1001_zhihan_refined.py
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()
Loading

0 comments on commit 3f180c1

Please sign in to comment.