diff --git a/week-4/Python/Myles-Cherebin.py b/week-4/Python/Myles-Cherebin.py new file mode 100644 index 0000000..a002ace --- /dev/null +++ b/week-4/Python/Myles-Cherebin.py @@ -0,0 +1,73 @@ +class Node: + # constructor + def __init__(self, data = None, next=None): + self.data = data + self.next = next + +# A Linked List class with a single head node +class LinkedList: + def __init__(self): + self.head = None + + def insert_front(self,Node): + '''Insert a node at the front of a LinkedList''' + Node.next = self.head + self.head = Node + + def insert_behind(self,frNode,node): + '''Insert a node behind a frNode in a LinkedList''' + node.next = frNode.next + frNode.next = node + + def print(self): + a = self.head + out = "" + while (a is not None): + out = out + str(a.data) + a = a.next + return out + + + +#prompt 1 +def sum_list(LinkedList1,LinkedList2): + '''Given two numbers represented by two linked lists, write a function + that returns sum list. The sum list is linked list representation + of addition of two input numbers.''' + + out = LinkedList() + j = LinkedList1.head + i = LinkedList2.head + + while(i is not None and j is not None ): + data = i.data + j.data + temp = Node(data) + out.insert_behind(out.head,temp) + i = i.next + j = j.next + b = out.head.next + out.head.next = None + out.head = b + return(out) + +#prompt 2 +def rev_sentence(LinkedList): + '''Given a Linked List which represents a sentence S such that each node + represents a letter, the task is to reverse the sentence without + reversing individual words.''' + + out = LinkedList() + + out.head = LinkedList.head + track = LinkedList.head.next + while (track is not None): + if (track.data == ' '): + out.insert_front(track) + track=track.next + if (track != None): + out.insert_front(track) + track = track.next + else: + out.insert_behind(out.head,track) + track = track.next + return(out) diff --git a/week-8/Python/Myles-Cherebin.py b/week-8/Python/Myles-Cherebin.py new file mode 100644 index 0000000..c73a1f5 --- /dev/null +++ b/week-8/Python/Myles-Cherebin.py @@ -0,0 +1,31 @@ +def stone_love(N,Q): # N:list of stones collected each day Q: list of queries + for x in Q: + days = 0 + ind = 0 + while x> 0: + x -= N[ind] + ind += 1 + days += 1 + + print(days) + + + +def repeated_ktimes(num, k): + num.sort() + for x in num: + if num.count(x) == k: + return x + +if __name__ == "__main__": + n = [1,2,4,6,7,3,2,5,0,6] + q = [5,10,3,8,31] + + stone_love(n,q) + + + a = repeated_ktimes([1,2,6,6,8,5,6,1,1,4],3) + assert a == 1 + + a = repeated_ktimes([1,2,6,7,8, 8,5,6,5,4],2) + assert a = 5