Skip to content
🤔prompts chat🧠
🔍
question:Create a Python function that generates a list of random integers of a user-specified length and within a user-specified range. The function should print out the list and also the sum, mean, and median of the list. Implement this with proper exception handling to ensure any potential errors, such as inputs being non-integer or non-positive values, are handled.

answer:import random import statistics def generate_random_list(length, start_range, end_range): Generates a list of random integers of specified length within a specified range and prints the list, sum, mean, and median of the list. Parameters: - length: int, length of the list (must be a positive integer) - start_range: int, start of the range for random integers - end_range: int, end of the range for random integers Returns: - list of random integers try: if not all(isinstance(i, int) for i in [length, start_range, end_range]): raise TypeError("All inputs must be integers.") if length <= 0: raise ValueError("Length must be a positive integer.") if start_range >= end_range: raise ValueError("Start range must be less than end range.") random_list = [random.randint(start_range, end_range) for _ in range(length)] total = sum(random_list) mean_val = statistics.mean(random_list) median_val = statistics.median(random_list) print("Random List:", random_list) print("Sum:", total) print("Mean:", mean_val) print("Median:", median_val) return random_list, total, mean_val, median_val except (TypeError, ValueError) as e: print(f"Error: {e}") return None

question:Create a program in Python that will identify and tally the number of unique words in a paragraph of text, provided by a user input. Additionally, your solution should ignore case sensitiveness (i.e. 'This' and 'this' should be treated as the same word) and punctuation marks (i.e. 'dog.' and 'dog' should be treated as the same word). Also, implement a feature to exclude common English stop words (such as 'is', 'the', 'and', etc.) from being counted. Your program should return the tally in descending order of occurrence. Eventually, allow the user to specify a number 'n', and your program should only display the 'n' most frequent unique words. Your solution should be optimized for performance while handling large inputs.

answer:import string from collections import Counter def remove_punctuation(text): return text.translate(str.maketrans('', '', string.punctuation)) def count_unique_words(paragraph, n, stop_words=None): if stop_words is None: stop_words = {'a', 'and', 'is', 'the', 'in', 'at', 'of', 'on', 'for', 'with', 'to', 'from', 'by', 'it'} # Remove punctuation cleaned_paragraph = remove_punctuation(paragraph) # Lowercase all words and split by whitespace words = cleaned_paragraph.lower().split() # Filter out stop words filtered_words = [word for word in words if word not in stop_words] # Count occurrences of each word word_counts = Counter(filtered_words) # Get the n most common words most_common_words = word_counts.most_common(n) return most_common_words

question:First, create a list of integers from 0 to 64 (inclusive). Then, implement a binary search algorithm to find a specific number in the list. The number to be found should be an input from the user. Make sure to handle potential exceptions related to user input. For a further challenge, instead of searching for only one number, the program should keep running and accept new numbers to search for until the user decides to quit. Here's an example in Python where we repeatedly prompt the user for a number to search in a predefined list until the user decides to quit. The user is expected to provide integer input within the range of 0 to 64. Invalid inputs are handled appropriately, while successful searches and failed searches return appropriate responses.

answer:def create_number_list(): return list(range(65)) def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 def main(): numbers = create_number_list() while True: user_input = input("Enter a number to search for (or 'quit' to exit): ") if user_input.lower() == 'quit': break try: number_to_find = int(user_input) if number_to_find < 0 or number_to_find > 64: print("Number out of range. Please enter a number between 0 and 64.") continue except ValueError: print("Invalid input. Please enter a valid number.") continue index = binary_search(numbers, number_to_find) if index != -1: print(f"Number {number_to_find} found at index {index}.") else: print(f"Number {number_to_find} not found.") if __name__ == '__main__': main()

question:Write a function to compute a hash code for a string. The function should use a custom hashing algorithm and not rely on any built-in hash functions.

answer:def custom_hash(s): Computes a custom hash code for a string using a simple algorithm. hash_code = 0 prime = 31 # Using a prime number for hash computation for char in s: hash_code = hash_code * prime + ord(char) return hash_code

Released under the chat License.

has loaded