Found 34494 Articles for Programming

Python program to right rotate a list by n

AmitDiwan
Updated on 11-Aug-2022 11:29:49

2K+ Views

In this article, we will see how to right rotate a list from the given rotation number. A list has comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type Let’s say the following is our input list − myList = [5, 20, 34, 67, 89, 94, 98, 110] The following is the output with n = 4 − 89, 94, 98, 110, 5, 20, 34, 67 Right rotate a list by n using slicing Here, slicing is used to right rotate a ... Read More

Get similar words suggestion using Enchant in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

272 Views

When we write something, many times happen with us that we misspelled some words. To overcome this problem, Python provides Enchant module. This is mainly used to check the spelling of words and suggest corrections for words that are miss-spelled. It is also used in many popular spellchecking packages to perform this task, including ispell, aspell and MySpell. It is very flexible at handling multiple dictionaries and multiple languages. For installing this, we use this command line in command prompt. pip install pyenchant Example Input >>> import enchant >>> d.suggest("prfomnc") Output::['prominence', 'performance', 'preform', 'Provence', 'preferment', 'proforma'] Example code ... Read More

Python Implementation of automatic Tic Tac Toe game using random number

Samual Sam
Updated on 30-Jul-2019 22:30:23

467 Views

This is very funny game. In this game no player is needed, it is an automatic game. Here we are using two Python modules numpy and random. In this game the mark on the board is put automatically instead of asking the user to put a mark on the board, and it will display the board after each turn unless a player wins. It returns -1 If the game gets draw. Example code import numpy as np import random from time import sleep # first creates an empty board def my_create_board(): return(np.array([[0, 0, 0], [0, ... Read More

When are static C++ class members initialized?

Arjun Thakur
Updated on 26-Jun-2020 13:41:03

382 Views

Static C++ class members can be defined using the static keyword. The static member in a class is shared by all the class objects as there is only one copy of the static class member in the memory, regardless of the number of objects of the class.The static class member is initialized to zero when the first object of the class is created if it is not initialized in any other way.A program that demonstrates static class members in C++ is given as follows.Example Live Demo#include using namespace std; class Example {    public :    static int a;   ... Read More

Declare variable as constant in C

Ankith Reddy
Updated on 26-Jun-2020 13:44:11

18K+ Views

Variables can be declared as constant using the const keyword or the #define preprocessor directive. Details about these are given as follows.The const keywordVariables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.A program that demonstrates the declaration of constant variables in C using const keyword is given as follows.Example Live Demo#include int main() {    const int a;    const int b = 12;    printf("The default value of variable a : %d", a);    printf("The ... Read More

Initialization of global and static variables in C

Arjun Thakur
Updated on 26-Jun-2020 13:45:22

4K+ Views

In C language both the global and static variables must be initialized with constant values. This is because the values of these variables must be known before the execution starts. An error will be generated if the constant values are not provided for global and static variables.A program that demonstrates the initialization of global and static variables is given as follows.Example Live Demo#include int a = 5; static int b = 10; int main() {    printf("The value of global variable a : %d", a);    printf("The value of global static variable b : %d", b);    return 0; }OutputThe ... Read More

Calling a member function on a NULL object pointer in C++

Chandu yadav
Updated on 26-Jun-2020 13:47:22

863 Views

A class member function can be called using a NULL object pointer.Note − This is undefined behaviour and there is no guarantee about the execution of the program. The actual results depend on the compiler used.A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; class Demo {    public :    void fun() {       cout fun();    return 0; }OutputThe output of the above program is as follows.This member function is called through Null object pointer.Now, let us understand the above program.The class Demo contains a member function fun(). This function displays ... Read More

Initialization of a normal array with one default value in C++

George John
Updated on 26-Jun-2020 13:49:16

15K+ Views

The entire array can be initialized to zero very simply. This is shown below.int arr[10] = {0};However, it is not possible to initialize the entire array to a non-zero value using the above method. This is shown below.int arr[10] = {5};In the above example, only the first element will be initialized to 5. All others are initialized to 0.A for loop can be used to initialize an array with one default value that is not zero. This is shown below.for(i = 0; i

Which is the fastest algorithm to find prime numbers using C++?

Ankith Reddy
Updated on 26-Jun-2020 13:50:52

3K+ Views

The Sieve of Eratosthenes is one of the most efficient ways to find the prime numbers smaller than n when n is smaller than around 10 million.A program that demonstrates the Sieve of Eratosthenes is given as follows.Example#include using namespace std; void SieveOfEratosthenes(int num) {    bool pno[num+1];    memset(pno, true, sizeof(pno));    for (int i = 2; i*i< = num; i++) {       if (pno[i] == true) {          for (int j = i*2; j< = num; j + = i)          pno[j] = false;       }    }    for (int i = 2; i< = num; i++)    if (pno[i])    cout

Calling class method through NULL class pointer in C++

Arjun Thakur
Updated on 26-Jun-2020 13:51:40

361 Views

A class method can be can be called using a NULL class pointer.Note − This is undefined behaviour and there is not guarantee about the execution of the program. The actual results depend on the compiler used.A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; class Example {    public :    void func() {       cout func();    return 0; }OutputThe output of the above program is as follows.The function is called through Null class pointer.Now, let us understand the above program.The class Example contains a member function func(). This function displays ... Read More

Advertisements