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:
codedef 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
Here is the code with the mean, median and mode functions implemented and fully rewritten:
from collections import Counter
def calculate_mean(numbers): # Calculate the mean by summing all numbers and dividing by the total count return sum(numbers) / len(numbers)
def calculate_median(numbers): # Sort the numbers and retrieve the middle value(s) based on whether the list has an odd or even length sorted_numbers = sorted(numbers) length = len(numbers) middle_index = length // 2
codeif length % 2 == 0: return (sorted_numbers[middle_index - 1] + sorted_numbers[middle_index]) / 2 else: return sorted_numbers[middle_index]def calculate_mode(numbers): # Retrieve the most common number(s) using Counter count = Counter(numbers) max_count = max(count.values()) mode = [k for k, v in count.items() if v == max_count]
codeif len(mode) == len(numbers): return None else: return modeTest the functions
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 9] print("Mean:", calculate_mean(numbers)) print("Median:", calculate_median(numbers)) print("Mode:", calculate_mode(numbers))
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)