Skip to content
Binary file added Week-3/__pycache__/test_pendulum.cpython-37.pyc
Binary file not shown.
Binary file added Week-3/__pycache__/test_toys.cpython-37.pyc
Binary file not shown.
Binary file added Week-3/__pycache__/toys.cpython-37.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion Week-3/test_pendulum.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'''

import unittest
import pendulum as pen
import test_pendulum as pen

class TestSum(unittest.TestCase):
def test_valid_input(self):
Expand Down
2 changes: 1 addition & 1 deletion Week-3/test_toys.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'''
test_toys.py

print("hi")
Unit tests for toy functions.
'''

Expand Down
32 changes: 25 additions & 7 deletions Week-3/toys.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,54 @@
'''



# 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
# numbers, adds them together using
# 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
Expand All @@ -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))
Binary file added Week-4/__pycache__/structures.cpython-37.pyc
Binary file not shown.
98 changes: 84 additions & 14 deletions Week-4/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,78 @@

# 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
# second and third parameter, respectively. The returned part should be in
# 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
Expand All @@ -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

47 changes: 19 additions & 28 deletions Week-4/test_structures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'''
test_toys.py

test_structures.py
Unit tests for structures functions.
'''

Expand All @@ -11,7 +10,7 @@

import structures as st

class TestSum(unittest.TestCase):
class TestStructures(unittest.TestCase):
def set_up(self):
pass

Expand All @@ -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
'''
Expand All @@ -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):
# '''
Expand All @@ -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):
'''
Expand All @@ -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()