code
stringlengths
63
8.54k
code_length
int64
11
747
# Write a Python program to extract numbers from a given string. def test(str1): result = [int(str1) for str1 in str1.split() if str1.isdigit()] return result str1 = "red 12 black 45 green" print("Original string:", str1) print("Extract numbers from the said string:") print(test(str1))
42
# Write a Pandas program to filter rows based on row numbers ended with 0, like 0, 10, 20, 30 from world alcohol consumption dataset. import pandas as pd # World alcohol consumption data w_a_con = pd.read_csv('world_alcohol.csv') print("World alcohol consumption sample data:") print(w_a_con.head()) print("\nFilter r...
59
# Write a Python program to extract characters from various text files and puts them into a list. import glob char_list = [] files_list = glob.glob("*.txt") for file_elem in files_list: with open(file_elem, "r") as f: char_list.append(f.read()) print(char_list)
37
# Write a NumPy program to extract second and fourth elements of the second and fourth rows from a given (4x4) array. import numpy as np arra_data = np.arange(0,16).reshape((4, 4)) print("Original array:") print(arra_data) print("\nExtracted data: Second and fourth elements of the second and fourth rows ") print(arr...
48
# Write a Pandas program to split a given dataset, group by one column and remove those groups if all the values of a specific columns are not available. import pandas as pd pd.set_option('display.max_rows', None) pd.set_option('display.max_columns', None) df = pd.DataFrame({ 'school_code': ['s001','s002','s003'...
116
# Write a Python Code for time Complexity plot of Heap Sort # Python Code for Implementation and running time Algorithm # Complexity plot of Heap Sort # by Ashok Kajal # This python code intends to implement Heap Sort Algorithm # Plots its time Complexity on list of different sizes # ---------------------Important ...
332
# Write a Python Program to Replace Text in a File # Python program to replace text in a file s = input("Enter text to replace the existing contents:") f = open("file.txt", "r+") # file.txt is an example here, # it should be replaced with the file name # r+ mode opens the file in read and write mode f.truncate(0) f...
65
# Write a Pandas program to count the number of missing values of a specified column in a given DataFrame. import pandas as pd import numpy as np pd.set_option('display.max_rows', None) #pd.set_option('display.max_columns', None) df = pd.DataFrame({ 'ord_no':[70001,np.nan,70002,70004,np.nan,70005,np.nan,70010,70003,...
57
# Write a Python program to add two given lists of different lengths, start from left , using itertools module. from itertools import zip_longest def elementswise_left_join(l1, l2): result = [a + b for a,b in zip_longest(l1, l2, fillvalue=0)][::1] return result nums1 = [2, 4, 7, 0, 5, 8] nums2 = [3, 3, -1,...
91
# Python Program to Determine all Pythagorean Triplets in the Range limit=int(input("Enter upper limit:")) c=0 m=2 while(c<limit): for n in range(1,m+1): a=m*m-n*n b=2*m*n c=m*m+n*n if(c>limit): break if(a==0 or b==0 or c==0): break print(a,b...
34
# Write a Python program to make a chain of function decorators (bold, italic, underline etc.) in Python. def make_bold(fn): def wrapped(): return "<b>" + fn() + "</b>" return wrapped def make_italic(fn): def wrapped(): return "<i>" + fn() + "</i>" return wrapped def make_underline(...
67
# You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers. The tuples are input by console. The sort criteria is: 1: Sort based on name; 2: Then sort based on age; 3: Then sort by score. The priority is that name > age > score. If th...
124
# Find sum and average of List in Python # Python program to find the sum # and average of the list    L = [4, 5, 1, 2, 9, 7, 10, 8]       # variable to store the sum of  # the list count = 0    # Finding the sum for i in L:     count += i        # divide the total elements by # number of elements avg = count/len(L) ...
77
# Create a dataframe of ten rows, four columns with random values. Write a Pandas program to highlight the entire row in Yellow where a specific column value is greater than 0.5. import pandas as pd import numpy as np np.random.seed(24) df = pd.DataFrame({'A': np.linspace(1, 10, 10)}) df = pd.concat([df, pd.DataFram...
76
# Program to Find the nth Palindrome Number rangenumber=int(input("Enter a Nth Number:")) c = 0 letest = 0 num = 1 while c != rangenumber:     num2=0     num1 = num     while num1 != 0:         rem = num1 % 10         num1 //= 10         num2 = num2 * 10 + rem     if num==num2:         c+=1         letest = num    ...
64
# Write a Python program to get the difference between two given lists, after applying the provided function to each list element of both. def difference_by(a, b, fn): _b = set(map(fn, b)) return [item for item in a if fn(item) not in _b] from math import floor print(difference_by([2.1, 1.2], [2.3, 3.4], floor))...
68
# numpy.sqrt() in Python # Python program explaining # numpy.sqrt() method     # importing numpy import numpy as geek     # applying sqrt() method on integer numbers  arr1 = geek.sqrt([1, 4, 9, 16]) arr2 = geek.sqrt([6, 10, 18])    print("square-root of an array1  : ", arr1) print("square-root of an array2  : ", arr2...
50
# Write a NumPy program to get the number of items, array dimensions, number of array dimensions and the memory size of each element of a given array. import numpy as np array_nums = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) print("Original array:") print(array_nums) print("\nNumber of items of the sai...
75
# Write a Python program to convert an array to an array of machine values and return the bytes representation. from array import * print("Bytes to String: ") x = array('b', [119, 51, 114, 101, 115, 111, 117, 114, 99, 101]) s = x.tobytes() print(s)
45
# Write a Python program to add two given lists of different lengths, start from left. def elementswise_left_join(l1, l2): f_len = len(l1)-(len(l2) - 1) for i in range(0, len(l2), 1): if f_len - i >= len(l1): break else: l1[i] = l1[i] + l2[i] return l1 nums1 = [2,...
94
# Write a Python program to remove duplicates from a list. a = [10,20,30,20,10,50,60,40,80,50,40] dup_items = set() uniq_items = [] for x in a: if x not in dup_items: uniq_items.append(x) dup_items.add(x) print(dup_items)
32