• Nov 19, 2022 •CodeCatch
0 likes • 1 view
# Python code to demonstrate # method to remove i'th character # Naive Method # Initializing String test_str = "CodeCatch" # Printing original string print ("The original string is : " + test_str) # Removing char at pos 3 # using loop new_str = "" for i in range(len(test_str)): if i != 2: new_str = new_str + test_str[i] # Printing string after removal print ("The string after removal of i'th character : " + new_str)
• Feb 23, 2025 •hasnaoui1
0 likes • 7 views
print("hello world")
# function which return reverse of a string def isPalindrome(s): return s == s[::-1] # Driver code s = "malayalam" ans = isPalindrome(s) if ans: print("Yes") else: print("No")
• Mar 26, 2023 •AustinLeath
import os # Get the current directory current_dir = os.getcwd() # Loop through each file in the current directory for filename in os.listdir(current_dir): # Check if the file name starts with a number followed by a period and a space if filename[0].isdigit() and filename[1] == '.' and filename[2] == ' ': # Remove the number, period, and space from the file name new_filename = filename[3:] # Rename the file os.rename(os.path.join(current_dir, filename), os.path.join(current_dir, new_filename))
• Jun 16, 2024 •lagiath
print('hello, world')
0 likes • 6 views
""" Binary Search Algorithm ---------------------------------------- """ #iterative implementation of binary search in Python def binary_search(a_list, item): """Performs iterative binary search to find the position of an integer in a given, sorted, list. a_list -- sorted list of integers item -- integer you are searching for the position of """ first = 0 last = len(a_list) - 1 while first <= last: i = (first + last) / 2 if a_list[i] == item: return ' found at position '.format(item=item, i=i) elif a_list[i] > item: last = i - 1 elif a_list[i] < item: first = i + 1 else: return ' not found in the list'.format(item=item) #recursive implementation of binary search in Python def binary_search_recursive(a_list, item): """Performs recursive binary search of an integer in a given, sorted, list. a_list -- sorted list of integers item -- integer you are searching for the position of """ first = 0 last = len(a_list) - 1 if len(a_list) == 0: return ' was not found in the list'.format(item=item) else: i = (first + last) // 2 if item == a_list[i]: return ' found'.format(item=item) else: if a_list[i] < item: return binary_search_recursive(a_list[i+1:], item) else: return binary_search_recursive(a_list[:i], item)