Write a python script to sort a list of numbers and words by their total ord() values
- User
Write a python script to sort a list of numbers and words by their total ord() values
- Assistant
Here is a Python script that sorts a list of numbers and words by their total ord() values:
def calculate_ord_total(item): if isinstance(item, int): item = str(item) return sum(ord(char) for char in item) def sort_by_ord_total(items): return sorted(items, key=calculate_ord_total) # Example usage: mixed_list = [42, 'apple', 33, 'banana', 'kiwi', 21, 'orange'] sorted_list = sort_by_ord_total(mixed_list) print(f"Original list: {mixed_list}") print(f"Sorted list: {sorted_list}")This script defines a function calculate_ord_total() to calculate the total ord() values of an item in the list. The sort_by_ord_total() function sorts the list by the total ord() values using the sorted() function with a custom key. The example usage demonstrates how to use the script to sort a mixed list of numbers and words by their total ord() values.
- User
Thank you for explaining this.
Now please add the mean, median and mode functions.
- Assistant
Certainly, here's an updated version of the code that includes functions to calculate the mean, median, and mode of a list of numbers:
def calculate_ord_total(item): if isinstance(item, int): item = str(item) return sum(ord(char) for char in item) def sort_by_ord_total(items): return sorted(items, key=calculate_ord_total) def calculate_mean(numbers): return sum(numbers) / len(numbers) def calculate_median(numbers): sorted_numbers = sorted(numbers) length = len(numbers) middle_index = length // 2 if length % 2 == 0: return (sorted_numbers[middle_index - 1] + sorted_numbers[middle_index]) / 2 else: return sorted_numbers[middle_index] def calculate_mode(numbers): counts = {} for number in numbers: if number in counts: counts[number] += 1 else: counts[number] = 1 max_count = max(counts.values()) modes = [number for number, count in counts.items() if count == max_count] if len(modes) == len(numbers): return None else: return modes[0] # Example usage: mixed_list = [42, 'apple', 33, 'banana', 'kiwi', 21, 'orange'] sorted_list = sort_by_ord_total(mixed_list) numbers_only = [number for number in sorted_list if isinstance(number, int)] mean = calculate_mean(numbers_only) median = calculate_median(numbers_only) mode = calculate_mode(numbers_only) print(f"Original list: {mixed_list}") print(f"Sorted list: {sorted_list}") print(f"Mean: {mean}") print(f"Median: {median}") print(f"Mode: {mode}")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)