[go: up one dir, main page]

0% found this document useful (0 votes)
28 views2 pages

Searching and Sorting

The document contains code snippets and explanations of Python concepts like lists, searching lists, recursion, and the __name__ == '__main__' conditional. It includes examples of using enumerate to iterate over lists, defining and calling a recursive binary search function, and checking if a number is in a list.

Uploaded by

jeevangowda2709
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Searching and Sorting

The document contains code snippets and explanations of Python concepts like lists, searching lists, recursion, and the __name__ == '__main__' conditional. It includes examples of using enumerate to iterate over lists, defining and calling a recursive binary search function, and checking if a number is in a list.

Uploaded by

jeevangowda2709
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

5zhjwgmvx

June 17, 2023

[ ]: list1=[4,2,7,5,12,54,21,64,12,32]
x=int(input("Please enter a number to search for : "))
for i in list1:
if x==i:
print("We have found",x,"and it is located at index number",list1.
↪index(i))

[ ]: list1 = [4,2,7,5,12,54,21,64,12,2,32]
x=int(input("Please enter a number to search for : "))
for idx, i in enumerate(list1):
if x==i:
print("We have found",x,"and it is located at index number",idx)

[ ]: l1 = ["eat", "sleep", "repeat"]


for ele in enumerate(l1):
print (ele)

[ ]: def binary_search_recursive(array, low, high, E):


# Base Condition
if high >= low:

# First calculating the mid index


mid = (high + low) // 2

# If the element is present at the middle index, return the index as␣
↪the answer.

if array[mid] == E:
return mid

# If element is smaller than the middle element, then recursively␣


↪checking the left sub array.

elif array[mid] > E:


return binary_search_recursive(array, low, mid - 1, E)

# Else recursively checking the right sub array.


else:
return binary_search_recursive(array, mid + 1, high, E)

1
# As the element is not present in the array, return -1.
else:
return -1

if __name__ == "__main__":
array = [13, 110, 115, 120, 135, 140, 260]
E = 115
low = 0
high = len(array) - 1

index = binary_search_recursive(array, low, high, E)

if index != -1:
print(f"Element {E} is found in the array at index", str(index))
else:
print(f"Element {E} is not present in the array.")

[ ]: l=[10,20]
str(l[0])

[ ]: If the __name__ == "__main__" expression is True , then the indented code␣


↪following the conditional statement executes.

If the __name__ == "__main__" expression is False , then Python skips the␣


↪indented code.

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

You might also like