diff --git a/first_exam/__pycache__/practice_questions.cpython-314.pyc b/first_exam/__pycache__/practice_questions.cpython-314.pyc new file mode 100644 index 0000000..423f344 Binary files /dev/null and b/first_exam/__pycache__/practice_questions.cpython-314.pyc differ diff --git a/first_exam/practice_questions.py b/first_exam/practice_questions.py index 744ef00..9d21793 100644 --- a/first_exam/practice_questions.py +++ b/first_exam/practice_questions.py @@ -4,46 +4,88 @@ class PracticeExam: def count_odd_numbers(self, numbers): """Return the count of odd numbers in the list.""" - pass + if len(numbers) ==0: + return 0 + return sum(1 for i in numbers if i%2 != 0) def sum_list(self, numbers): """Return the sum of all numbers in the list.""" - pass + return sum(numbers) def reverse_words_order(self, sentence): """Reverse the order of words in a sentence.""" - pass + return ' '.join(reversed(sentence.split())) def contains_vowel(self, text): """Return True if the string contains at least one vowel.""" - pass + + return True if sum(1 for char in 'aeiou' if char in text.lower())>0 else False def smallest_number(self, numbers): """Return the smallest number in the list or None if empty.""" - pass - + return min(numbers) if numbers else None # ===== INTERMEDIATE QUESTIONS ===== def remove_vowels(self, text): """Return the string with all vowels removed (case-insensitive).""" - pass + vowels ='aeiouAEIOU' + for i in text: + if i in vowels: + text = text.replace(i,'') + return text + + print(remove_vowels(self='',text= 'hellow')) + def count_character_frequency(self, text): """Return a dictionary with character frequencies.""" - pass + freq = {} + + # for char in text: + # if char in freq: + # freq[char] += 1 + + # else: + # freq[char] = 1 + # return freq + for char in text: + freq[char] = freq.get(char,0)+1 + return freq + + def is_prime(self, n): """Return True if n is a prime number, otherwise False.""" - pass + if n < 2: + return False + elif n == 2: + return True + else: + for i in range(2,n-1): + if n % i == 0: + return False + + return True + + + print(is_prime(self='',n =13)) + def flatten_list(self, nested): """Flatten a 2D list into a 1D list.""" - pass + + unnest = [] + + for i in nested: + for j in i: + unnest.append(j) + return unnest def longest_common_prefix(self, words): """Return the longest common prefix among a list of words.""" - pass + for word in words: + pass # ===== ADVANCED QUESTIONS ===== diff --git a/second_test/Answerguide.py b/second_test/Answerguide.py new file mode 100644 index 0000000..f93a2dc --- /dev/null +++ b/second_test/Answerguide.py @@ -0,0 +1,199 @@ +class AssessmentTwo: + '''These are answers, not all and feel free if there is something I did wrong or you don't understand so we can both learn''' + # ===== BASIC ===== + + def count_negative_numbers(self, numbers): + """Return the number of negative values in the list.""" + + count = 0 + if numbers is None: + return count + else: + + for num in numbers: + if num < 0: + count+=1 + return count + # print(count_negative_numbers(self = '', numbers=[])) + + def average(self, numbers): + """Return the average of numbers or None if list is empty.""" + + + if len(numbers) == 0: + return None + else: + count = 0 + for i in numbers: + count +=1 + avg = sum(numbers)/count + + return avg + + # print(average(self='',numbers=[3,4])) + + + + def first_and_last(self, items): + """Return a tuple of (first, last) item or None if list is empty.""" + first = None + last = None + + if len(items) == 0: + return None + else: + for index,item in enumerate(items): + if index == 0: + first = item + if index == len(items)-1: + last = item + + return (first,last) + + def count_consonants(self, text): + """Return the number of consonants in the string (letters only).""" + + count = 0 + + for i in text: + if i.upper() in 'BCDFGHJKLMNPQRSTVWXYZ': + count +=1 + return count + + + # print(count_consonants(self='',text ='hzelhlo')) + + def is_even_length(self, text): + """Return True if the string length is even.""" + if len(text)%2 == 0: + return True + else: + return False + + + # ===== INTERMEDIATE ===== + + def remove_duplicates_preserve_order(self, numbers): + """Remove duplicates while preserving order.""" + numbers = set(numbers) + numbers = [num for num in numbers] + return numbers + + + def word_lengths(self, sentence): + """Return a dictionary mapping each word to its length.""" + sentence = sentence.split() + + dictionary = {} + + for element in sentence: + dictionary[element] = len(element) + + return dictionary + + def second_largest(self, numbers): + """Return the second largest number or None if it doesn't exist.""" + if len(numbers) <=1: + return None + else: + numbers = sorted(numbers) + numbers = numbers[::-1] + + return numbers[1] + + # print(second_largest(self='', numbers=[2,1,5,6,4])) + + def chunk_list(self, numbers, size): + """Split list into chunks of given size.""" + numberlist = [] + for num in range(0,len(numbers)-1,size): + number = numbers[num : num + size ] + numberlist.append(number) + if len(numbers) %2 != 0: + + numbers = numbers[::-1] + numberlist.append([numbers[0]]) + return numberlist + print(chunk_list(self='', numbers=[1,2,3,4,5],size=2)) + + def is_anagram(self, s1, s2): + """Return True if the two strings are anagrams (ignore case & spaces).""" + + s1 = s1.replace(' ','') + s2 = s2.replace(' ','') + + s1 = sorted(s1) + s2 = sorted(s2) + + if s1 == s2 :return True + + + + # ===== ADVANCED ===== + + def running_sum(self, numbers): + """Return a list of running sums.""" + add = 0 + num = [] + if len(numbers) == 0: + return [] + else: + + for i in numbers: + add +=i + num.append(add) + return num + def longest_unique_substring(self, text): + """Return the length of the longest substring without repeating characters.""" + + return sum(1 for char in 'abcdefghijklmnopqrstuvwxyz' if char in text.lower()) + + + def rotate_matrix_90(self, matrix): + """Rotate a square matrix 90 degrees clockwise.""" + rotate =[[],[]] + + rotate[0].append(matrix[1][0]) + rotate[0].append(matrix[0][0]) + rotate[1].append(matrix[1][1]) + rotate[1].append(matrix[0][1]) + return rotate + # print(rotate_matrix_90(self='',matrix=[[1,2],[3,4]])) + def validate_palindrome_number(self, n): + """Return True if integer n is a palindrome.""" + # n = str(n) + # print(n) + # print(''.join(reversed(n))) + # if n == ''.join(reversed(n)): + # return True + # else: + # return False + + # n = [i for i in str(n) if str(n).isdigit()] + + # if n == n[::-1]: + # return True + # else: + # return False + + return True if str(n) == ''.join(reversed(str(n))) else False + print(validate_palindrome_number(self='',n=121)) + def generate_pascal_row(self, n): + """Return the nth row of Pascal's Triangle (0-indexed).""" + + ''' + 0 1 + 1 1 1 + 2 1 2 1 + 3 1 3 3 1 + 4 1 4 6 4 1 + + ''' + + pascal = [] + for i in range(n+1): + if n == 0: + pascal.append(1) + else: + + pass diff --git a/second_test/__pycache__/assessment_two.cpython-314.pyc b/second_test/__pycache__/assessment_two.cpython-314.pyc new file mode 100644 index 0000000..6756ba5 Binary files /dev/null and b/second_test/__pycache__/assessment_two.cpython-314.pyc differ diff --git a/second_test/assessment_two.py b/second_test/assessment_two.py index cd4aa67..93df4f2 100644 --- a/second_test/assessment_two.py +++ b/second_test/assessment_two.py @@ -4,66 +4,114 @@ class AssessmentTwo: def count_negative_numbers(self, numbers): """Return the number of negative values in the list.""" - pass + if len(numbers)==0: + return 0 + else: + count = 0 + for num in numbers: + if num < 0: + count +=1 + + return count + + def average(self, numbers): """Return the average of numbers or None if list is empty.""" - pass + + if not numbers: + return None + else: + return sum(numbers)/len(numbers) + + + + def first_and_last(self, items): """Return a tuple of (first, last) item or None if list is empty.""" - pass - + if items is None: + return [] + else: + return (items[0],items[-1]) + + def count_consonants(self, text): """Return the number of consonants in the string (letters only).""" - pass + + return sum(1 for i in text if i.lower() in 'bcdfghjklmnpqrstvwxyz' ) + def is_even_length(self, text): """Return True if the string length is even.""" - pass - + return True if len(text)%2 == 0 else False # ===== INTERMEDIATE ===== def remove_duplicates_preserve_order(self, numbers): """Remove duplicates while preserving order.""" - pass + return set(numbers) def word_lengths(self, sentence): """Return a dictionary mapping each word to its length.""" - pass + + def second_largest(self, numbers): """Return the second largest number or None if it doesn't exist.""" - pass + + def chunk_list(self, numbers, size): """Split list into chunks of given size.""" - pass - + num = [] + + if numbers is None: + return [] + else: + for number in range(0,len(numbers),size): + num.append(numbers[number:number+size]) + + return num + print(chunk_list(self=None,numbers=[1,2,3,4,5,6],size=3)) def is_anagram(self, s1, s2): """Return True if the two strings are anagrams (ignore case & spaces).""" - pass + + + # ===== ADVANCED ===== def running_sum(self, numbers): """Return a list of running sums.""" - pass - + + + def longest_unique_substring(self, text): """Return the length of the longest substring without repeating characters.""" - pass + + return sum(1 for char in 'abcdefghijklimopqrstuvwxyz' if char in text.lower()) + def rotate_matrix_90(self, matrix): """Rotate a square matrix 90 degrees clockwise.""" - pass + def validate_palindrome_number(self, n): """Return True if integer n is a palindrome.""" - pass - + def generate_pascal_row(self, n): """Return the nth row of Pascal's Triangle (0-indexed).""" - pass + + ''' + 0 1 + 1 1 1 + 2 1 2 1 + 3 1 3 3 1 + 4 1 4 6 4 1 + 5 1 5 10 10 5 1 + + ''' + +