diff --git a/Week-3/__pycache__/test_pendulum.cpython-37.pyc b/Week-3/__pycache__/test_pendulum.cpython-37.pyc new file mode 100644 index 0000000..8a90bc5 Binary files /dev/null and b/Week-3/__pycache__/test_pendulum.cpython-37.pyc differ diff --git a/Week-3/__pycache__/test_toys.cpython-37.pyc b/Week-3/__pycache__/test_toys.cpython-37.pyc new file mode 100644 index 0000000..73dfe43 Binary files /dev/null and b/Week-3/__pycache__/test_toys.cpython-37.pyc differ diff --git a/Week-3/__pycache__/toys.cpython-37.pyc b/Week-3/__pycache__/toys.cpython-37.pyc new file mode 100644 index 0000000..31fcfac Binary files /dev/null and b/Week-3/__pycache__/toys.cpython-37.pyc differ diff --git a/Week-3/test_pendulum.py b/Week-3/test_pendulum.py index 1a4f9d0..ce361d3 100644 --- a/Week-3/test_pendulum.py +++ b/Week-3/test_pendulum.py @@ -5,7 +5,7 @@ ''' import unittest -import pendulum as pen +import test_pendulum as pen class TestSum(unittest.TestCase): def test_valid_input(self): diff --git a/Week-3/test_toys.py b/Week-3/test_toys.py index 70d1921..276da60 100644 --- a/Week-3/test_toys.py +++ b/Week-3/test_toys.py @@ -1,6 +1,6 @@ ''' test_toys.py - +print("hi") Unit tests for toy functions. ''' diff --git a/Week-3/toys.py b/Week-3/toys.py index 51868b5..e846d4f 100644 --- a/Week-3/toys.py +++ b/Week-3/toys.py @@ -6,23 +6,34 @@ ''' + # write a function that adds 1 # to the input and prints the result -def inc(a): - print(a) + +def inc(val1): + result = val1 + 1 + print(result) + return result + # write a function that adds 1 # to the input and returns the result def inc_return(a): - return # hint this is incomplete + result = a + 1 + return result # hint this is incomplete + +print(inc_return(3)) # write a function that adds # the two input numbers together # and returns the sum def sum(a, b): - return + result = a + b + return result + +print(sum(3, 4)) # write a function that takes two @@ -30,14 +41,19 @@ def sum(a, b): # sum() and then increments the sum # using inc_return def sum_inc(a, b): - return + result = sum(a, b) + result1 = inc_return(result) + return result1 + +print(sum_inc(4, 6)) + # write a function that returns a # boolean (True or False) for whether # the input number is even def is_even(a): - return + return a%2 == 0 # create for loop that takes a string @@ -49,5 +65,7 @@ def is_even(a): def string_repeat(phrase, repeat): # hint: you can add strings together # in order to concatenate them - return + result = (phrase * repeat) + return result +print(string_repeat("hello", 3)) \ No newline at end of file diff --git a/Week-4/__pycache__/structures.cpython-37.pyc b/Week-4/__pycache__/structures.cpython-37.pyc new file mode 100644 index 0000000..b2e6e52 Binary files /dev/null and b/Week-4/__pycache__/structures.cpython-37.pyc differ diff --git a/Week-4/structures.py b/Week-4/structures.py index 461415c..6899dd9 100644 --- a/Week-4/structures.py +++ b/Week-4/structures.py @@ -8,8 +8,14 @@ # write a function that returns a list containig the first and the last element # of "the_list". -def first_and_last(the_list): - return [] +the_list = [1, 2, 3, 4, 5] + +def first_and_last(the_list =[1,2,3]): + first = the_list[0] + last = the_list[-1] + return [first, last] + +print(first_and_last(the_list)) # write a function that returns part of "the_list" between indices given by the @@ -17,30 +23,63 @@ def first_and_last(the_list): # reverse order than in the original "the_list". # If "end" is greater then "beginning" or any og the indices is out of the # list, raise a "ValueError" exception. -def part_reverse(the_list, beginning, end): - return # hint this is incomplete +def part_reverse(): + the_list = [1, 2, 3, 4, 5] + beginning = the_list[0] + end = the_list[-1] + result = (end, beginning) + try: + if end > beginning: + print("This should be the case") + else: + print("Please check the code again!") + except ValueError as err: + print("there is a ValueError!") + return result + #the_list, beginning, end +print(part_reverse()) # hint this is incomplete # write a function that at the "index" of "the_list" inserts three times the # same value. For example if the_list = [0,1,2,3,4] and index = 3 the function # will return [0,1,2,3,3,3,4]. def repeat_at_index(the_list, index): - return + the_list.insert(the_list.index(index), index) + the_list.insert(the_list.index(index), index) + return the_list + +list1 = [1, 2, 3, 4, 5] +ind1 = 2 + +print(repeat_at_index(list1, ind1)) # Strings # write a function that checks whether the word is a palindrome, i.e. it reads # the same forward and backwards -def palindrome_word(word): - return - +def palindrome_word(): + word = "hello" + if word == word[::-1]: + print("this is a palindrome") + else: + print("this is not a palindrome") + return None + +print(palindrome_word()) # write a function that checks whether the sentence is a palindrome, i.e. it # read the same forward and backward. Ignore all spaces and other characters # like fullstops, commas, etc. Also do not consider whether the letter is # capital or not. -def palindrome_sentence(sentence): - return +def palindrome_sentence(): + sentence = "hello olleh" + if sentence == sentence[::-1]: + print("this is a palindrome") + else: + print("this is not a palindrome") + return + +print(palindrome_sentence()) # write a function that concatenates two sentences. First the function checks # whether the sentence meets the following criteria: it starts with a capital @@ -49,23 +88,54 @@ def palindrome_sentence(sentence): # the end. The concatenated sentence must have no white space at the beginning # or at the end and the must be exactly one space after the end of the first # sentence. -def concatenate_sentences(sentenece1, sentence2): +def concatenate_sentences(): + sentence1 = "Hi my name is Julian." + sentence2 = " I am 21 yrs old" + if sentence1.capitalize() and "." or "?" or "!" in sentence1: + print(sentence1 + sentence2) return +print(concatenate_sentences()) + # Dictionaries # write a function that checks whether there is a record with given key in the # dictionary. Return True or False. def index_exists(dictionary, key): - return + for keyy in dictionary.keys(): + if keyy == key: + return True + else: + continue + return False + +print(index_exists({"name": "cock", +"age": "sucker", +"home": "youre mums house" +},"name")) # write a function which checks whether given value is stored in the # dictionary. Return True or False. def value_exists(dictionary, value): - return + for valuee in dictionary.values(): + if valuee == value: + return True + else: + continue + return False + +print(value_exists({"name": "cock", +"age": "sucker", +"home": "youre mums house" +},"cock")) # write a function that returns a new dictionary which contains all the values # from dictionary1 and dictionary2. def merge_dictionaries(dictionary1, dictionary2): - return + '''Combines two dictionaries''' + dict1 = dictionary1 + for key,value in dictionary2.items(): + dict1[key] = value + return dict1 + diff --git a/Week-4/test_structures.py b/Week-4/test_structures.py index 1d54815..b05b746 100644 --- a/Week-4/test_structures.py +++ b/Week-4/test_structures.py @@ -1,6 +1,5 @@ ''' -test_toys.py - +test_structures.py Unit tests for structures functions. ''' @@ -11,7 +10,7 @@ import structures as st -class TestSum(unittest.TestCase): +class TestStructures(unittest.TestCase): def set_up(self): pass @@ -23,7 +22,7 @@ def test_first_and_last(self): result = st.first_and_last([0,1,2,3]) self.assertEqual(result, [0,3]) - def test_part_reverse(selff): + def test_part_reverse(self): ''' Test ''' @@ -41,51 +40,51 @@ def test_palindrome_word1(self): ''' Test palindrome word ''' - resutl = st.palindrome_word("madam") - self.asserIs(result, True) + result = st.palindrome_word("madam") + self.assertIs(result, True) def test_palindrome_word2(self): ''' Test palindrome word ''' - resutl = st.palindrome_word("Madam") - self.asserIs(result, True) + result = st.palindrome_word("Madam") + self.assertIs(result, True) def test_palindrome_word3(self): ''' Test palindrome word ''' - resutl = st.palindrome_word("palindrome") - self.asserIs(result, False) + result = st.palindrome_word("palindrome") + self.assertIs(result, False) def test_palindrome_sentence1(self): ''' Test palindrome sentence ''' - resutl = st.palindrome_word("Was it a car or a cat I saw") - self.asserIs(result, True) + result = st.palindrome_sentence("Was it a car or a cat I saw") + self.assertIs(result, True) def test_palindrome_sentence2(self): ''' Test palindrome sentence ''' - resutl = st.palindrome_word("Random sentence") - self.asserIs(result, False) + result = st.palindrome_sentence("Random sentence") + self.assertIs(result, False) def test_palindrome_sentence3(self): ''' Test palindrome sentence ''' - resutl = st.palindrome_word(" Do geese see God ") - self.asserIs(result, True) + result = st.palindrome_sentence(" Do geese see God ") + self.assertIs(result, True) def test_concatenate_sentences1(self): ''' Test sentence concatenation ''' result = st.concatenate_sentences("First sentence.", "Second sentence.") - self.assertEquual(results, "First sentence. Second sentence.") + self.assertEqual(result, "First sentence. Second sentence.") #def test_concatenate_sentences2(self): # ''' @@ -101,12 +100,6 @@ def test_index_exists1(self): result = st.index_exists({"ind1": "val1", "ind2": "val2"} , "ind1") self.assertIs(result, True) - def test_index_exists2(self): - ''' - Test - ''' - result = st.index_exists({"ind1": "val1", "ind2": "val2"} , "ind3") - self.assertIs(result, False) def test_value_exists1(self): ''' @@ -127,11 +120,9 @@ def test_merge_dictionaries(self): ''' Test ''' - result = st.merge_dictionaries({"a": 1, "c": 3}, {"b:" 2, "d": 4}) - self.assertEqual(result, {"a": 1, "c": 3, "b:" 2, "d": 4}) - - + result = st.merge_dictionaries({"a": 1, "c": 3}, {"b": 2, "d": 4}) + self.assertEqual(result, {"a": 1, "c": 3, "b": 2, "d": 4}) if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file