Print Number as String of A and B in Lexicographic Order in C++

sudhir sharma
Updated on 27-Jan-2021 05:00:50

151 Views

In this problem, we are given a number N. Our task is to create a program to Print a number as string of ’A’ and ‘B’ in lexicographic order. Representation of all numbers as string of ‘A’ and ‘B’ is1 = A 2 = B 3 = AA 4 = AB 5 = BA 6 = BB 7 = AAA 8 = AABLet’s take an example to understand the problem,  Input: N = 12Output: BABSolution ApproachThe string of ‘A’ and ‘B’ is similar to a binary number. To find the string, we will first find the length of string, using the fact that there are 2 numbers of length 1 ... Read More

Print All Longest Common Sub-Sequences in Lexicographical Order in C++

sudhir sharma
Updated on 27-Jan-2021 05:00:17

1K+ Views

In this problem, we are given two string str1 and str2. Our task is to create a program to Print all longest common subsequences in lexicographical order. Let’s take an example to understand the problem,  Input: str1 = “gfare” ,  str2 = “rfare”Output: fareSolution ApproachIn this problem, we will find all possible longest common subsequences and store them in a 2D matrix using Dynamic programming. After this, we will print sorted output by searching characters from a to z in the LCS.Program to illustrate the working of our solution, ExampleLive Demo#include #include #define MAX 100 using namespace std; int LCSLength = 0; ... Read More

Sum of the Series 1 + 1 + 3 + 1 + 3 + 5 + 1 + 3 + 5 + 7 + ... + 2n - 1 in C++

sudhir sharma
Updated on 27-Jan-2021 04:59:42

591 Views

In this problem, we are given a number n. Our task is to create a program to find the sum of series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1)). Let’s take an example to understand the problem,  Input:   n = 5Output:  55So, according to the question suppose a user gives us a number ‘n’ and we have to add series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1)).Let's understand the meaning of this series better first.We take n=1, then the series becomes 1.We take n=2, then the series becomes 1+ (1+3) because the ... Read More

Sum Over Subsets using Dynamic Programming in C++

sudhir sharma
Updated on 27-Jan-2021 04:59:09

341 Views

In this problem, we are given an array arr[] of size 2n. Our task is to create a program to find the sum over subset using dynamic programming to solve it.We need to calculate function, F(x) = Σ Ai such that x&i == i for all x. i.e. i is a bitwise subset of x.Let’s take an example to understand the problem,  Input: A[] = {5, 7, 1, 9} , n = 2Output: 5 12 6 22Explanation: For n = 2 , there are 4 values of x. They are 0, 1, 2, 3.Now, calculating values of the function:F(0) = A0 ... Read More

Sum of XOR of All Pairs in an Array in C++

sudhir sharma
Updated on 27-Jan-2021 04:57:57

2K+ Views

In this problem, we are given an array arr[] of size n. Our task is to create a program to find the sum of XOR of sum of all pairs in an array.Let’s see an example to understand the problem,  Input: arr[5, 7, 9]Output: 22Explanation: (5+5) ^ (5+7) ^ (5+9) ^ (7+5) ^ (7+7) ^ (7+9) ^ (9+5) ^ (9+7) ^ (9+9) = 22A simple solution to the problem is by using a nested loop. And creating all possible pairs from the array. And calculate the XOR of the sum of each pair.Algorithm: Initialise XorSum = 0Step 1: iterate from 0 to n. Follow:Step 1.1: iterate ... Read More

XML Processing Modules in Python

Pradeep Elance
Updated on 25-Jan-2021 07:51:34

1K+ Views

XML stands for "Extensible Markup Language". It is mainly used in webpages, where the data has a specific structure. It has elements, defined by a beginning and an ending tag. A tag is a markup construct that begins with < and ends with >. The characters between the start-tag and end-tag, are the element's content. Elements can contain other elements, which are called "child elements".ExampleBelow is the example of an XML file we are going to use in this tutorial.           Vicky, Matthew       Geo-Spatial Data Analysis       Python     ... Read More

XML-RPC Server and Client Modules in Python

Pradeep Elance
Updated on 25-Jan-2021 07:51:19

4K+ Views

We can create our own cross-platform, language-independent server using the XML-RPC protocol.We use the SimpleXMLRPCServer to create the SimpleXMLRPCServer instance and tell it to listen for incoming requests. Next we define some functions to be part of the service and register those functions so that the server knows how to call it.Running the ServerIn the below example we create a server using the SimpleXMLRPCServer instance and register some pre-defined as well as custom functions. Finally, we put the server into an infinite loop receiving and responding to requests.Examplefrom xmlrpc.server import SimpleXMLRPCServer from xmlrpc.server import SimpleXMLRPCRequestHandler class RequestHandler(SimpleXMLRPCRequestHandler):    rpc_paths = ... Read More

The Null Object Pattern in Python

Pradeep Elance
Updated on 25-Jan-2021 07:46:49

3K+ Views

Python does not have a null object. But the most closely related similar object is none. In this article, we will see how how None behaves in Python.Checking the type of Null and None we see that there is no Null Type and the None object is of type NoneType.Exampleprint(type(None)) print(type(Null))OutputRunning the above code gives us the following result −Traceback (most recent call last):    File "C:\Users\xxx\scratch.py", line 4, in       print(type(Null)) NameError: name 'Null' is not definedKey facts about NoneNone is the same as False.None is the same as False.None is the same as False.None is an ... Read More

Python Type Object

Pradeep Elance
Updated on 25-Jan-2021 07:44:57

8K+ Views

Everything in Python is an object including classes. All classes are instances of a class called "type".The type object is also an instance of type class. You can inspect the inheritance hierarchy of class by examining the __bases__ attribute of a class object. type() method returns class type of the argument(object) passed as parameter.If single argument type(obj) is passed to type method, it returns the type of given object. If three arguments type(name, bases, dict) is passed, it returns a new type object.Using type()Let’s look at the classes for the most used data types. In the below program we initialize ... Read More

Python Code Objects

Pradeep Elance
Updated on 25-Jan-2021 07:40:16

2K+ Views

Code objects are a low-level detail of the CPython implementation. Each one represents a chunk of executable code that hasn’t yet been bound into a function. Though code objects represent some piece of executable code, they are not, by themselves, directly callable. To execute a code object, you must use the exec keyword.In the below example we see how the code objects are created for a given piece of code and what are the various attributes associated with hat code object.Example Live Democode_str = """ print("Hello Code Objects") """ # Create the code object code_obj = compile(code_str, '', 'exec') # get ... Read More

Advertisements