From d8f2bd44d7e8f8dd7284b260848a16932e5efe02 Mon Sep 17 00:00:00 2001 From: Chee Justin Date: Mon, 19 Oct 2020 18:03:48 +0800 Subject: [PATCH] Made t13_mean_1 more compact and completed set_theory_find_unique_elements --- set_theory_find_unique_elements.py | 18 ++++++------------ t13_mean1.py | 15 ++++++--------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/set_theory_find_unique_elements.py b/set_theory_find_unique_elements.py index 78744fc..003f79e 100644 --- a/set_theory_find_unique_elements.py +++ b/set_theory_find_unique_elements.py @@ -5,17 +5,11 @@ appeared = {} for i in range (len(arr)): - # Do something here - ''' - Hint: - Use a dictionary to store the counts of the elements - Example: appeared[9] = 2 - ''' + if arr[i] in appeared: + appeared[arr[i]] += 1 + else: + appeared[arr[i]] = 1 for i in appeared: - # Do something here - ''' - Hint: - How do you check if something is unique? - What will appeared[i] equals to? - ''' + if appeared[i] == 1: + print(i, end=" ") \ No newline at end of file diff --git a/t13_mean1.py b/t13_mean1.py index 842f670..b8c0bc9 100644 --- a/t13_mean1.py +++ b/t13_mean1.py @@ -1,11 +1,8 @@ # Find the mean of n numbers -n = int(input("Enter number of numbers: ")) -numbers = [] -total = 0 -for i in range(n): - numbers.append(float(input("Enter number: "))) -for number in numbers: - total = total + number -print(numbers) -print(total / n) +n = int(input("Enter an integer number of numbers: ")) + +numbers = [float(input("Enter number: ")) for i in range(n)] + +print(f"List of numbers: {numbers}\nMean of the numbers: {sum(numbers)/ n}") # print the list of numbers and the mean +