Skip to content

Commit 53f6af8

Browse files
authored
Add files via upload
1 parent 9a230f7 commit 53f6af8

6 files changed

+499
-0
lines changed
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
def circular_array_loop(nums):
2+
size = len(nums)
3+
4+
for i in range(size):
5+
slow = fast = i
6+
forward = nums[i] > 0
7+
8+
while True:
9+
slow = next_step(slow, nums[slow], size)
10+
if is_not_cycle(nums, forward, slow):
11+
break
12+
13+
fast = next_step(fast, nums[fast], size)
14+
if is_not_cycle(nums, forward, fast):
15+
break
16+
17+
fast = next_step(fast, nums[fast], size)
18+
if is_not_cycle(nums, forward, fast):
19+
break
20+
21+
if slow == fast:
22+
return True
23+
24+
return False
25+
26+
# A function to calculate the next step
27+
def next_step(pointer, value, size):
28+
result = (pointer + value) % size
29+
if result < 0:
30+
result += size
31+
return result
32+
33+
# A function to detect a cycle doesn't exist
34+
def is_not_cycle(nums, prev_direction, pointer):
35+
36+
curr_direction = nums[pointer] >= 0
37+
if (prev_direction != curr_direction) or (abs(nums[pointer] % len(nums)) == 0):
38+
return True
39+
else:
40+
return False
41+
42+
# Driver code
43+
def main():
44+
45+
input = (
46+
[-2, -3, -9],
47+
[-5, -4, -3, -2, -1],
48+
[-1, -2, -3, -4, -5],
49+
[2, 1, -1, -2],
50+
[-1, -2, -3, -4, -5, 6],
51+
[1, 2, -3, 3, 4, 7, 1],
52+
[2, 2, 2, 7, 2, -1, 2, -1, -1]
53+
)
54+
num = 1
55+
56+
for i in input:
57+
print(f"{num}.\tCircular array = {i}")
58+
print(f"\n\tFound loop = {circular_array_loop(i)}")
59+
print("-"*100, "\n")
60+
num += 1
61+
62+
63+
if __name__ == "__main__":
64+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def find_duplicate(nums):
2+
fast = slow = nums[0]
3+
4+
while True:
5+
slow = nums[slow]
6+
fast = nums[nums[fast]]
7+
if slow == fast:
8+
break
9+
10+
slow = nums[0]
11+
12+
while slow != fast:
13+
slow = nums[slow]
14+
fast = nums[fast]
15+
16+
return fast
17+
18+
19+
20+
21+
22+
# Driver code
23+
def main():
24+
nums = [
25+
[1, 3, 2, 3, 5, 4],
26+
[2, 4, 5, 4, 1, 3],
27+
[1, 6, 3, 5, 1, 2, 7, 4],
28+
[1, 2, 2, 4, 3],
29+
[3, 1, 3, 5, 6, 4, 2]
30+
]
31+
for i in range(len(nums)):
32+
print(i + 1, ".\tnums = ", nums[i], sep="")
33+
print("\tDuplicate number = ", find_duplicate(nums[i]), sep="")
34+
print("-" * 100)
35+
36+
37+
if __name__ == '__main__':
38+
main()

Fast_Slow_Pointers/happy_numbers.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def is_happy_number(n):
2+
3+
# Define function to calculate sum of the squared digits
4+
def sum_of_square_digits(num):
5+
total_sum = 0
6+
for digit in str(num):
7+
total_sum += int(digit)**2
8+
return total_sum
9+
10+
# Initialize slow and fast pointers
11+
slow_pointer = n
12+
fast_pointer = sum_of_square_digits(n)
13+
14+
# Continue incrementally moving pointers until we've found a cycle or a happy number
15+
while fast_pointer != 1 and slow_pointer != fast_pointer:
16+
slow_pointer = sum_of_square_digits(slow_pointer)
17+
fast_pointer = sum_of_square_digits(sum_of_square_digits(fast_pointer))
18+
19+
# Verify if result is a happy number or not
20+
if fast_pointer == 1:
21+
return True
22+
23+
return False
24+
25+
26+
27+
28+
def main():
29+
inputs = [1, 5, 19, 25, 7]
30+
for i in range(len(inputs)):
31+
print(i+1, ".\tInput Number: ", inputs[i], sep="")
32+
print("\n\tIs it a happy number? ", is_happy_number(inputs[i]))
33+
print("-" * 100)
34+
35+
36+
if __name__ == '__main__':
37+
main()
38+
39+
+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
class LinkedListNode:
2+
# __init__ will be used to make a LinkedListNode type object.
3+
def __init__(self, data, next=None):
4+
self.data = data
5+
self.next = next
6+
7+
8+
# Template for the linked list
9+
class LinkedList:
10+
# __init__ will be used to make a LinkedList type object.
11+
def __init__(self):
12+
self.head = None
13+
14+
# insert_node_at_head method will insert a LinkedListNode at head
15+
# of a linked list.
16+
def insert_node_at_head(self, node):
17+
if self.head:
18+
node.next = self.head
19+
self.head = node
20+
else:
21+
self.head = node
22+
23+
# create_linked_list method will create the linked list using the
24+
# given integer array with the help of InsertAthead method.
25+
def create_linked_list(self, lst):
26+
for x in reversed(lst):
27+
new_node = LinkedListNode(x)
28+
self.insert_node_at_head(new_node)
29+
30+
# returns the number of nodes in the linked list
31+
def get_length(self, head):
32+
temp = head
33+
length = 0
34+
while(temp):
35+
length+=1
36+
temp = temp.next
37+
return length
38+
39+
# returns the node at the specified position(index) of the linked list
40+
def get_node(self, head, pos):
41+
if pos != -1:
42+
p = 0
43+
ptr = head
44+
while p < pos:
45+
ptr = ptr.next
46+
p += 1
47+
return ptr
48+
49+
# __str__(self) method will display the elements of linked list.
50+
def __str__(self):
51+
result = ""
52+
temp = self.head
53+
while temp:
54+
result += str(temp.data)
55+
temp = temp.next
56+
if temp:
57+
result += ", "
58+
result += ""
59+
return result
60+
61+
62+
def print_list_with_forward_arrow(linked_list_node):
63+
temp = linked_list_node
64+
while temp:
65+
print(temp.data, end=" ") # print node value
66+
67+
temp = temp.next
68+
if temp:
69+
print("→", end=" ")
70+
else:
71+
# if this is the last node, print null at the end
72+
print("→ null", end=" ")
73+
74+
def print_list_with_forward_arrow_loop(linked_list_node):
75+
temp = linked_list_node
76+
while temp:
77+
print(temp.data, end=" ") # print node value
78+
79+
temp = temp.next
80+
if temp:
81+
print("→", end=" ")
82+
83+
84+
85+
86+
87+
# DETECT CYCLE
88+
def detect_cycle(head):
89+
90+
# Initialize slow and fast pointers at the head
91+
slow_pointer = head
92+
fast_pointer = head
93+
94+
# Continue moving pointers as long as end of list not found
95+
while fast_pointer is not None and fast_pointer.next is not None:
96+
slow_pointer = slow_pointer.next
97+
fast_pointer = fast_pointer.next.next
98+
99+
# If cycle is found, return True
100+
if slow_pointer == fast_pointer:
101+
return True
102+
103+
# If end of list found return False
104+
return False
105+
106+
107+
108+
109+
110+
# Driver code
111+
def main():
112+
113+
input = (
114+
[2, 4, 6, 8, 10, 12],
115+
[1, 3, 5, 7, 9, 11],
116+
[0, 1, 2, 3, 4, 6],
117+
[3, 4, 7, 9, 11, 17],
118+
[5, 1, 4, 9, 2, 3],
119+
)
120+
pos = [0, -1, 1, -1, 2]
121+
j = 1
122+
123+
for i in range(len(input)):
124+
125+
input_linked_list = LinkedList()
126+
input_linked_list.create_linked_list(input[i])
127+
print(f"{j}.\tInput: ", sep="", end="")
128+
if pos[i] == -1:
129+
print_list_with_forward_arrow(input_linked_list.head)
130+
else:
131+
print_list_with_forward_arrow_loop(input_linked_list.head)
132+
print("\n\tpos:", pos[i])
133+
if pos[i] != -1:
134+
length = input_linked_list.get_length(input_linked_list.head)
135+
last_node = input_linked_list.get_node(input_linked_list.head, length - 1)
136+
last_node.next = input_linked_list.get_node(input_linked_list.head, pos[i])
137+
138+
print(f"\n\tDetected cycle = {detect_cycle(input_linked_list.head)}")
139+
j += 1
140+
print("-"*100, "\n")
141+
142+
143+
if __name__ == "__main__":
144+
main()
145+

0 commit comments

Comments
 (0)