Found 31499 Articles for Programming

Swift Program to remove the last given number of items from the array

Ankita Saini
Updated on 27-Jan-2023 12:38:26
In this article, we will learn how to write a swift program to remove the last given number of items from the array. An array is used to store elements of same data type in an order. So, here we use the following methods to remove the last given number of items from the array − Using removeLast(_:) function Using dropLast(_:) function Using removeSubrange() function Method 1: Using removeLast(_:) Function Swift provide an in-built function named as removeLast() function to remove the specified number of items from the end of the array. Syntax func removeLast(_x: Int) ... Read More

Swift Program to remove the first given number of items from the array

Ankita Saini
Updated on 27-Jan-2023 12:34:59
In this article, we will learn how to write a swift program to remove the first given number of items from the array. An array is used to store elements of same data type in an order. So, here we use the following methods to remove the first given number of items from the array − Using removeFirst(_:) function Using dropFirst(_:) function Using removeSubrange() function Method 1: Using removeFirst(_:) Function Swift provide an in-built function named as removeFirst() function to remove the specified number of items from the start of the array. Syntax func removeFirst(_x: Int) ... Read More

Swift Program to Print object of a class

Ankita Saini
Updated on 27-Jan-2023 12:33:02
In this article, we will learn how to write a swift program to print object of a class. A class object is known as an instance of a class. For example, colour is a class then obj1, obj2, obj3 and obj4 are the objects from the class. A class can have multiple objects. Syntax var objectname = Classname() Here, using the above syntax we can create an object of a class. Algorithm Step 1 − Create a class with properties and a constructor. Step 2 − Create a function to print the object of the given class. Step ... Read More

Swift Program to get the last given number of items from the array

Ankita Saini
Updated on 27-Jan-2023 12:30:54
In this article, we will learn how to write a swift program to get the last given number of items from the array. An array is used to store elements of same data type in an order. So, here we are using the following methods to get the last given number of items from the array − Using user defined function Using suffix(_:) function Using suffix(from:) function Method 1: Using a user-defined function To get the last given number of items from the array we can create a function. This function will return an array containing the ... Read More

Swift Program to get the first given number of items from the array

Ankita Saini
Updated on 27-Jan-2023 12:29:23
In this article, we will learn how to write a swift program to get the first given number of items from the array. An array stores elements of the same data type in order. Therefore, here we use the following methods to get the first given number of items from the array − Using subscript with range Using prefix(_:) function Using prefix(upTo:) function Method 1: Using subscript with range To get the first given number of items from the array we can use subscript with different range operators like closed range operator, closed range operator with one-sided ranges, ... Read More

Swift Program to fill an array with a specific element

Ankita Saini
Updated on 27-Jan-2023 12:28:31
In this article, we will learn how to write a swift program to fill an array with a specific element. An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order. In an array, every element has an index. The array index is start from 0 and goes up to N-1. Here N represent the total number of array elements. We can fill an array with a specified element using the following methods − Using user defined function Using ... Read More

Python program to Uppercase selective indices

Vikram Chiluka
Updated on 27-Jan-2023 12:22:26
In this article, we will learn a python program to convert the string elements to uppercase for the given selective indices. Methods Used The following are the various methods to accomplish this task − Using For Loop and upper() function Using List Comprehension Using index() & join() functions Example Assume we have taken an input string and a list containing index values. We will now convert the characters of an input string to uppercase at those given indices and print the resultant string. Input inputString = 'hello tutorialspoint python' indexList = [0, 6, 8, 10, 15, ... Read More

Python Program to Test if string contains element from list

Vikram Chiluka
Updated on 27-Jan-2023 12:28:22
In this article, we will learn how to check if a string contains an element from a list in python. Methods Used Using Nested For Loops Using List Comprehension Using any() function Using the find() function Example Assume we have taken an input string and input list. We will now check whether the input string contains at least any one input list element. Input inputString = "tutorialspoint is a best learning platform for coding" inputList = ['hello', 'tutorialspoint', 'python'] Output YES, the string contains elements from the input list In the above example, the ... Read More

Python Program to Test if any set element exists in List

Vikram Chiluka
Updated on 27-Jan-2023 12:19:16
In this article, we will learn how to check if any set element exists in the list in python. Methods Used Using any() function Using Bitwise & operator Using Counter(), filter() and lambda functions Example Assume we have taken an input set and input list. We will now check whether any input set element exists in the input list using the above methods. Input inputSet = {4, 8, 1, 3, 5, 7} inputList = [7, 15, 20] Output Checking whether any set element present in the input list: True In the above example, 7 exists ... Read More

Python Program to Split a String by Custom Lengths

Vikram Chiluka
Updated on 27-Jan-2023 12:08:51
In this article, we will learn how to split a string by custom lengths in python. Methods Used The following are the various methods to accomplish this task − Using for loop and slicing Using List Comprehension, join() and next() functions Example Assume we have taken an input string and custom lengths list. We will now split an input string by input custom lengths using the above methods. Input inputString = 'hitutorialspoint' customLengths = [4, 1, 6, 5] Output ['hitu', 't', 'orials', 'point'] In the above example, firstly the input list is split based on the ... Read More
1 2 3 4 5 6 7 ... 3150 Next
Advertisements