Loading...
More Python Posts
x[cat_var].isnull().sum().sort_values(ascending=False)
primes=[]products=[]def prime(num):if num > 1:for i in range(2,num):if (num % i) == 0:return Falseelse:primes.append(num)return Truefor n in range(30,1000):if len(primes) >= 20:break;else:prime(n)for previous, current in zip(primes[::2], primes[1::2]):products.append(previous * current)print (products)
""" Binary Search Algorithm----------------------------------------"""#iterative implementation of binary search in Pythondef 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 integersitem -- integer you are searching for the position of"""first = 0last = len(a_list) - 1while first <= last:i = (first + last) / 2if a_list[i] == item:return ' found at position '.format(item=item, i=i)elif a_list[i] > item:last = i - 1elif a_list[i] < item:first = i + 1else:return ' not found in the list'.format(item=item)#recursive implementation of binary search in Pythondef binary_search_recursive(a_list, item):"""Performs recursive binary search of an integer in a given, sorted, list.a_list -- sorted list of integersitem -- integer you are searching for the position of"""first = 0last = len(a_list) - 1if len(a_list) == 0:return ' was not found in the list'.format(item=item)else:i = (first + last) // 2if 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)
filename = "data.txt"with open(filename, "r") as file:file_contents = file.readlines()file_contents = [line.strip() for line in file_contents]print("File contents:")for line in file_contents:print(line)
prime_lists=[] # a list to store the prime numbersdef prime(n): # define prime numbersif n <= 1:return False# divide n by 2... up to n-1for i in range(2, n):if n % i == 0: # the remainder should'nt be a 0return Falseelse:prime_lists.append(n)return Truefor n in range(30,1000): # calling function and passing starting point =30 coz we need primes >30prime(n)check=0 # a var to limit the output to 10 onlyfor n in prime_lists:for x in prime_lists:val= n *xif (val > 1000 ):check=check +1if (check <10) :print("the num is:", val , "=",n , "* ", x )break
import os# Get the current directorycurrent_dir = os.getcwd()# Loop through each file in the current directoryfor filename in os.listdir(current_dir):# Check if the file name starts with a number followed by a period and a spaceif filename[0].isdigit() and filename[1] == '.' and filename[2] == ' ':# Remove the number, period, and space from the file namenew_filename = filename[3:]# Rename the fileos.rename(os.path.join(current_dir, filename), os.path.join(current_dir, new_filename))