diff --git a/DSA/__init__.py b/DSA/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/DSA/number_theory/__init__.py b/DSA/number_theory/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/DSA/number_theory/prime_number.py b/DSA/number_theory/prime_number.py new file mode 100644 index 0000000..e2ad249 --- /dev/null +++ b/DSA/number_theory/prime_number.py @@ -0,0 +1,29 @@ +# prime number is only divisible by 1 and that number itself +# prime number is greater then 1 + +import math + + +def is_prime(number: int): + """Brute force""" + # if number is 1 or less then 1 then its not a prime number + if number <= 1: + return False + + # iter every item which starts from 2 and ends before number: + limit = int(math.sqrt(number + 1)) + for value in range(2, limit): + # if number is divisible by value: which is number less then the number + # then its not prime number as prime number can't be divided by any other number itself + print("dude!") + if number % value == 0: + return False + + if limit <= 2 and number % limit == 0: + return False + + return True + + +number = 3 +print(f"Number {number} is prime: {is_prime(number)}") diff --git a/HackerRank/linked_list_insert_position.py b/HackerRank/linked_list_insert_position.py index 68004ea..c2fc8c7 100644 --- a/HackerRank/linked_list_insert_position.py +++ b/HackerRank/linked_list_insert_position.py @@ -77,3 +77,141 @@ def print_nodes(self): result.append(current_head.data) current_head = current_head.next return result + + +def push_at(self, newElement, position): + + # 1. allocate node to new element + newNode = Node(newElement) + + # 2. check if the position is > 0 + if position < 1: + print("\nposition should be >= 1.") + elif position == 1: + + # 3. if the position is 1, make next of the + # new node as head and new node as head + newNode.next = self.head + self.head = newNode + else: + + # 4. Else, make a temp node and traverse to the + # node previous to the position + temp = self.head + for i in range(1, position - 1): + if temp != None: + temp = temp.next + + # 5. If the previous node is not null, make + # newNode next as temp next and temp next + # as newNode. + if temp != None: + newNode.next = temp.next + temp.next = newNode + else: + + # 6. When the previous node is null + print("\nThe previous node is null.") + + +# Python3 program for insertion in a single linked +# list at a specified position + + +# A linked list Node +class Node: + def __init__(self, data): + self.data = data + self.nextNode = None + + +# function to create and return a Node +def getNode(data): + + # allocating space + newNode = Node(data) + return newNode + + +# function to insert a Node at required position +def insertPos(headNode, position, data): + head = headNode + + # This condition to check whether the + # position given is valid or not. + if position < 1: + print("Invalid position!") + + if position == 1: + newNode = Node(data) + newNode.nextNode = headNode + head = newNode + + else: + + # Keep looping until the position is zero + while position != 0: + position -= 1 + + if position == 1: + + # adding Node at required position + newNode = getNode(data) + + # Making the new Node to point to + # the old Node at the same position + newNode.nextNode = headNode.nextNode + + # Replacing headNode with new Node + # to the old Node to point to the new Node + headNode.nextNode = newNode + break + + headNode = headNode.nextNode + if headNode == None: + break + if position != 1: + print("position out of range") + return head + + +# This function prints contents +# of the linked list +def printList(head): + while head != None: + print(" " + str(head.data), end="") + head = head.nextNode + print() + + +# Driver Code +if __name__ == "__main__": + + # Creating the list 3.5.8.10 + head = getNode(3) + head.nextNode = getNode(5) + head.nextNode.nextNode = getNode(8) + head.nextNode.nextNode.nextNode = getNode(10) + print("Linked list before insertion: ", end="") + printList(head) + data = 12 + position = 3 + head = insertPos(head, position, data) + print("Linked list after insertion of 12 at position 3: ", end="") + printList(head) + + # front of the linked list + data = 1 + position = 1 + head = insertPos(head, position, data) + print("Linked list after insertion of 1 at position 1: ", end="") + printList(head) + + # insertion at end of the linked list + data = 15 + position = 7 + head = insertPos(head, position, data) + print("Linked list after insertion of 15 at position 7: ", end="") + printList(head) + +# This code iscontributed by rutvik_56 diff --git a/__init__.py b/__init__.py index e2abbd3..6a0e991 100644 --- a/__init__.py +++ b/__init__.py @@ -3,3 +3,4 @@ path = os.getcwd() sys.path.append(path) + diff --git a/tests/test_number_theory.py b/tests/test_number_theory.py new file mode 100644 index 0000000..0be9094 --- /dev/null +++ b/tests/test_number_theory.py @@ -0,0 +1,4 @@ +def test_is_prime(): + from DSA.number_theory.prime_number import is_prime + + \ No newline at end of file