Server Side Programming Articles - Page 2122 of 2650

Find k closest numbers in an unsorted array in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:06:33

486 Views

Consider we have an array A with few elements. The array is unsorted. We have two other value X and k. Our task is to find the k number of nearest elements of X from the array A. If the element X is present in the array, then it will not be shown in the output. If A = [48, 50, 55, 30, 39, 35, 42, 45, 12, 16, 53, 22, 56] and X = 35, k = 4. The output will be 30, 39, 42, 45.To solve this, we will use the heap data structure. The steps will be ... Read More

Find k closest elements to a given value in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:00:58

182 Views

Consider we have an array A with few elements. We have two other value X and k. Our task is to find the k number of nearest elements of X from the array A. If the element X is present in the array, then it will not be shown in the output. If A = [12, 16, 22, 30, 35, 39, 42, 45, 48, 50, 53, 55, 56] and X = 35, k = 4. The output will be 30, 39, 42, 45.To solve this, we will use the binary search approach. Using this we will get the crossover point. ... Read More

Find if n can be written as product of k numbers in C++

Arnab Chakraborty
Updated on 01-Nov-2019 09:57:45

179 Views

Suppose we have a number N. We have another number k. We have to check the number can be represented using k numbers or not. Suppose a number 54, and k = 3, then it will print the numbers like [2, 3, 9], if it cannot be represented, then print that.To solve this, we will find all prime factors of N, and store them into a vector, then to find k numbers greater than 1, we check the size of the vector is greater than k or not. If size is less than k, then return -1, otherwise print first ... Read More

Find if array can be divided into two subarrays of equal sum in C++

Arnab Chakraborty
Updated on 01-Nov-2019 09:54:12

189 Views

Suppose we have an array A. We have to check whether we can split the array into two parts, whose sum are equal. Suppose the elements are [6, 1, 3, 2, 5], then [6, 1], and [2, 5] can be two subarrays.This problem can be solved easily by following these rules. We have to find the sum of all elements of the array at first, then for each element of the array, we can calculate the right sum by using total_sum – sum of elements found so far.Example#include #include using namespace std; void displaySubArray(int arr[], int left, int right) {    cout

append() and extend() in Python program

Hafeezul Kareem
Updated on 01-Nov-2019 09:53:04

536 Views

In this tutorial, we are going to learn about the most common methods of a list i.e.., append() and extend(). Let's see them one by one.append()append() method is used to insert an element at the end of a list. The time complexity of append() method is O(1).Syntaxlist.append(element) -> element can be any data type from the list of data types.Let's see some examples.Example# initializing a list nums = [1, 2, 3, 4] # displaying the list print('----------------Before Appending-------------------') print(nums) print() # appending an element to the nums # 5 will be added at the end of the nums nums.append(5) # ... Read More

Apply function to every row in a Pandas DataFrame in Python

Hafeezul Kareem
Updated on 01-Nov-2019 09:50:20

1K+ Views

In this tutorial, we are going to learn about the most common methods of a list i.e.., append() and extend(). Let's see them one by one.apply()It is used to apply a function to every row of a DataFrame. For example, if we want to multiply all the numbers from each and add it as a new column, then apply() method is beneficial. Let's see different ways to achieve it.Example# importing the pandas package import pandas as pd # function to multiply def multiply(x, y):    return x * y # creating a dictionary for DataFrame data = {    'Maths': ... Read More

Apply uppercase to a column in Pandas dataframe in Python

Hafeezul Kareem
Updated on 01-Nov-2019 09:47:01

2K+ Views

In this tutorial, we are going to see how to make a column of names to uppercase in DataFrame. Let's see different ways to achieve our goal.ExampleWe can assign a column to the DataFrame by making it uppercase using the upper() method.Let's see the code.# importing the pandas package import pandas as pd # data for DataFrame data = {    'Name': ['Hafeez', 'Aslan', 'Kareem'],    'Age': [19, 21, 18],    'Profession': ['Developer', 'Engineer', 'Artist'] } # creating DataFrame data_frame = pd.DataFrame(data) # displaying the DataFrame print('---------------------Before-------------------') print(data_frame) print() # making the Name column strings to upper case data_frame['Name'] = ... Read More

Arithmetic Operations on Images using OpenCV in Python

Hafeezul Kareem
Updated on 01-Nov-2019 09:45:00

1K+ Views

In this tutorial, we are going to learn about the arithmetic operations on Images using OpenCV. We can apply operations like addition, subtraction, Bitwise Operations, etc.., Let's see how we can perform operations on images.We need the OpenCV module to perform operations on images. Install the OpenCV module using the following command in the terminal or command line.pip install opencv-python==4.1.1.26If you run the above command, you will get the following successful message.Collecting opencv-python==4.1.1.26 Downloading https://files.pythonhosted.org/packages/1f/51/e0b9cef23098bc31c77b0e0 6221dd8d05119b9782d4c2b1d1482e22b5f5e/opencv_python-4.1.1.26-cp37-cp37m-win_amd64.w hl (39.0MB) Requirement already satisfied: numpy>=1.14.5 in c:\users\hafeezulkareem\anaconda3\l ib\site-packages (from opencv-python==4.1.1.26) (1.16.2) Installing collected packages: opencv-python Successfully installed opencv-python-4.1.1.26AdditionWe can add two images using ... Read More

as_integer_ratio() in Python for reduced fraction of a given rational

Hafeezul Kareem
Updated on 01-Nov-2019 09:42:09

415 Views

In this tutorial, we are going to write a program that returns two numbers whose ratio is equal to the given float value. We have a method called as_integer_ratio() that helps to achieve our goal.Let's see some examples.Input: 1.5 Output: 3 / 2 Input: 5.3 Output: 5967269506265907 / 1125899906842624Let's examine the code.Example# initializing the float value float_value = 1.5 # getting integers tuple using the as_integer_ratio() method integers = float_value.as_integer_ratio() # printing the integers print(f'{integers[0]} / {integers[1]}')OutputIf you run the above code, you will get the following results.3 / 2 Let's see another example.Example# initializing the float value float_value = ... Read More

Arithmetic operations using OpenCV in Python

Hafeezul Kareem
Updated on 01-Nov-2019 09:40:12

344 Views

In this tutorial, we are going to perform arithmetic operations on images using OpenCV in Python. We need to install the OpenCV module.Run the following command to install the OpenCV module.pip install opencv-python==4.1.1.26If you run the above command, you will get the following successful message.Collecting opencv-python==4.1.1.26 Downloading https://files.pythonhosted.org/packages/1f/51/e0b9cef23098bc31c77b0e0 6221dd8d05119b9782d4c2b1d1482e22b5f5e/opencv_python-4.1.1.26-cp37-cp37m-win_amd64.w hl (39.0MB) Requirement already satisfied: numpy>=1.14.5 in c:\users\hafeezulkareem\anaconda3\l ib\site-packages (from opencv-python==4.1.1.26) (1.16.2) Installing collected packages: opencv-python Successfully installed opencv-python-4.1.1.26Adding Two ImagesWe need two images for the addition. We have a method called cv2.add(image_one, image_two) to perform addition. It's very hand method. The sizes of the two images must be the same. ... Read More

Advertisements