From 440dc1b402c74ab3115b008f4205eaaea5f7d95f Mon Sep 17 00:00:00 2001 From: Alucard008 Date: Sun, 18 Sep 2022 15:31:57 +0500 Subject: [PATCH 1/2] Kodify Task answers Solutions to the questions in the given task --- Answers/Kodify_Task_Question_1.py | 51 ++++++++++++++ Answers/Kodify_Task_Question_2.py | 109 ++++++++++++++++++++++++++++++ Answers/Kodify_Task_Question_3.py | 89 ++++++++++++++++++++++++ 3 files changed, 249 insertions(+) create mode 100644 Answers/Kodify_Task_Question_1.py create mode 100644 Answers/Kodify_Task_Question_2.py create mode 100644 Answers/Kodify_Task_Question_3.py diff --git a/Answers/Kodify_Task_Question_1.py b/Answers/Kodify_Task_Question_1.py new file mode 100644 index 0000000..8d4fa3d --- /dev/null +++ b/Answers/Kodify_Task_Question_1.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[15]: + + +# Question 1 +# Regarding factorial and the sume of individual numbers of the result gotten from the +# factorial of the number + + +# Program will keep running till a correct number is inputed to calculate its factorial. +result = 1 +choice = True +while (choice): + user_number = int(input("Enter a number to calculate factorial -> ")) + + if user_number < 0: + print(" Number is negative") + elif user_number == 0: + print("1 is the factorial of 0") + else: + for i in range(1,user_number + 1): + result = result*i + print("The factorial of",user_number,"is",result) + choice = False + +# We have the factorial in the result variable +# Moving to move to 2nd phase + +sum = 0 + +while result!=0: + digit = int(result%10) + sum += digit + result = result/10 + +print("The sum of the factorial we got is --> " , sum) + + +# In[16]: + + + + + +# In[ ]: + + + + diff --git a/Answers/Kodify_Task_Question_2.py b/Answers/Kodify_Task_Question_2.py new file mode 100644 index 0000000..9f41f60 --- /dev/null +++ b/Answers/Kodify_Task_Question_2.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[31]: + + +## Node Class +## A typical node in the list +class Node: + def __init__(self, value): + self.value =value + self.next = None ## this is the pointer to the next Node + + +# In[59]: + + + +# Our List class with basic functions for adding and deleteing nodes. + +class MyLinkedList: + + def __init__(self): ## we initialzie the list with an empty head pointer as no nodes are present + self.head = None + + def add(self , value): ## To add a new node to the list + + next_node = Node(value) ## creating a new node with some value + next_node.next =self.head ## assigning its next pointer to point to the head of the top node of the list + self.head = next_node ## assiging the header pointer to the newly created node + + + def Delete(self, index): ## To delete the nTH from the end of the list. + + # We will use two pointers to achiece this + + + first_pointer = self.head ## this pointer will be used to point to the head of the list + + second_pointer = self.head ## this will be used to point to the nth node that we want to delete + + + for i in range(index): ## run the loop till the required index + #print("i --> " , i) + #print("first-->", first_pointer.value) + #print("second-->",second_pointer.value) + + # If count of nodes in the + # given list is less than 'index (position of node for deletion)' + if(second_pointer.next == None): + + # If index = n then + # delete the head node + if(i == index - 1): + self.head = self.head.next + return self.head + + #print("2nd , second_pointer" , second_pointer) + second_pointer = second_pointer.next + + while(second_pointer.next != None): + second_pointer = second_pointer.next + first_pointer = first_pointer.next + + first_pointer.next = first_pointer.next.next + + def printLinkedList(self): ## print the whole list + position = self.head + while(position): + print(" %d " % (position.value), end=" ") + position = position.next + + + + +# In[ ]: + + + + + +# In[58]: + + +# CHECK THE WORKING OF THE CODE + +#Creating and filling the list with values +llist = MyLinkedList() + +llist.add(10) +llist.add(20) +llist.add(30) +llist.add(40) +llist.add(50) + +print("Created Linked List: ") +llist.printLinkedList() + +llist.Delete(3) + +print("\nLinked List after Deletion at position 3 from end --> ") +llist.printLinkedList() + + +# In[ ]: + + + + diff --git a/Answers/Kodify_Task_Question_3.py b/Answers/Kodify_Task_Question_3.py new file mode 100644 index 0000000..bde249c --- /dev/null +++ b/Answers/Kodify_Task_Question_3.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[11]: + + + +## A recursive implementation of quick sort as its the go to +## algorithm for best average case time and space complexity + +## Was a bit rusty about which sorting algo to use, so decided to look it up in the internet :) +## Reference https://www.programiz.com/dsa/quick-sort + +def sort_array(array): + + """Sort the array by using quicksort.""" + + less = [] + equal = [] + greater = [] + + if len(array) > 1: ## check if the array has atleast 2 values + + pivot = array[0] ## our base value for which we check the other values + for x in array: + + ## We basically have 3 pointers moving around determining the values to swap + + if x < pivot: + less.append(x) + elif x == pivot: + equal.append(x) + elif x > pivot: + greater.append(x) + + return sort_array(less)+equal+sort_array(greater) # Just use the + operator to join arrays + + else: # when we have only one element in the array, just return the array. + return array + + +# In[21]: + + +#Question 3 +# Return the median of the conjuntion of 2 sorted arrays + + +def Median(array): + + size_of_array = len(array) + # check for even case + if size_of_array % 2 != 0: + return float(array[int(n/2)]) ## we can get values in decimal so we cast the result as a float + + return float((array[int((size_of_array-1)/2)] + array[int(size_of_array/2)])/2.0) + + + + +array1 =[] +array2 =[] + +size_array=int(input("Number of elements in first array:")) +for i in range(0 , size_array): + inp=int(input()) + array1.append(inp) +print(array1) + +size_array = int(input("Number of elements in second array:")) +for i in range(0,size_array): + inp=int(input()) + array2.append(inp) +print(array2) + +joint_array = list(set(array1 + array2)) ## remove duplicates +final_array = sort_array(joint_array) +print("joint sorted array --> " , joint_array) +Median = Median(final_array) +print("Median for the 2 sorted arrays is -->", Median) + + +# + +# In[ ]: + + + + From fe06a935c5acb1d38977d64e7fba068e61848dbb Mon Sep 17 00:00:00 2001 From: Alucard008 Date: Sun, 18 Sep 2022 18:00:08 +0500 Subject: [PATCH 2/2] Drive link for Videos of Task answers --- Drive Link for videos.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Drive Link for videos.txt diff --git a/Drive Link for videos.txt b/Drive Link for videos.txt new file mode 100644 index 0000000..3e80a13 --- /dev/null +++ b/Drive Link for videos.txt @@ -0,0 +1,4 @@ +https://drive.google.com/drive/folders/1etNXDkl4S1qJ-Lq68_x2tN-P04BLNS85?usp=sharing + + * As videos were above 100 MB so decided to upload them on google drive and share the link + ** Only people with this link can access the drive \ No newline at end of file