Found 26504 Articles for Server Side Programming

Fraction to Recurring Decimal in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:36:18

1K+ Views

Suppose we have two integers representing the numerator and denominator of a fraction, we have to find fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. So if the numerator is 2 and denominator is 3, then the output will be “0.(6)”To solve this, we will follow these steps −if numerator is 0, then return 0define one array ansif the numerator < 0 and denominator > 0 or numerator 0 and denominator < 0, then insert negative symbol ‘-’ into the ans arraydivisor := |numerator| and dividend := |denominator|, remainder := divisor mod ... Read More

Sort List in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:29:28

264 Views

Suppose we have a list, we have to sort this in O(n logn) time using constant space complexity, so if the list is like [4, 2, 1, 3], then it will be [1, 2, 3, 4]To solve this, we will follow these steps −Define a method for merging two lists in sorted, order, that method is merge(), this takes two lists l1 and l2.The sort list method will work like below −if head is null, or next of head is null, then return headslow := head, fast := head, and prev = nullwhile fast is not null and next of ... Read More

Reorder List in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:35:15

913 Views

Suppose we have a linked list like l1 -> l2 -> l3 -> l4 -> … -> l(n-1) -> ln. We have to rearrange this list into the form l1 -> ln -> l2 -> l(n - 1) -> … and so on. Here the constraint is that, we cannot modify the values in the list nodes, only the node itself may be changed. So for example, if the list is like [1, 2, 3, 4, 5], then the output will be [1, 5, 2, 4, 3]To solve this, we will follow these steps −Define a method called reverse to ... Read More

Flatten Binary Tree to Linked List in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:21:28

225 Views

Suppose we have a binary tree; we have to flatten it into linked list in place. So if the tree is like −The output tree will be −To solve this, we will follow these steps −ser prev := nullDefine a recursive function solve(), that will take root as input.if root is null, then returnsolve(right of root)solve(left of root)right of root := prev, left of root := nullprev := rootLet us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class TreeNode{    public:    int val;    TreeNode *left, *right;    TreeNode(int data){     ... Read More

Binary Tree Zigzag Level Order Traversal in Python

Arnab Chakraborty
Updated on 30-Apr-2020 09:13:33

748 Views

Suppose we have a binary tree; we have to find the Zigzag level order traversal. So for the first row, scan from left to right, then right to left from the second row, then again left to right and so on. So if the tree is like −The traversal sequence will be [[3], [20, 9], [15, 7]]To solve this, we will follow these steps −if the tree is empty, return empty listqueue := make a queue and insert root into the queue, create two empty lists res and res2, and set flag as Truewhile queue is not emptymake a list ... Read More

Convert list of string to list of list in Python

Pradeep Elance
Updated on 03-Mar-2020 06:34:31

974 Views

In this article we will see how to create a list of lists which contain string data types. The inner list themselves or of string data type and they may contain numeric or strings as their elements.Using strip and splitWe use these two methods which will first separate out the lists and then convert each element of the list to a string.Example Live Demolist1 = [ '[0, 1, 2, 3]', '["Mon", "Tue", "Wed", "Thu"]' ] print ("The given list is : " + str(list1)) print("") # using strip() + split() result = [k.strip("[]").split(", ") for k in list1] print ("Converting list ... Read More

Python - cmp() Method

Pradeep Elance
Updated on 03-Mar-2020 06:30:00

4K+ Views

The cmp() is part of the python standard library which compares two integers. The result of comparison is -1 if the first integer is smaller than second and 1 if the first integer is greater than the second. If both are equal the result of cmp() is zero.Below example illustrates different scenario showing the use of cmp() method.Example Live Demodef cmp(x, y):    return (x > y) - (x < y) #x>y x = 5 y = 3 print("The cmp value for x>y is : ",cmp(x, y),"") #xy is : 1 The cmp value for x

Python - Clearing list as dictionary value

Pradeep Elance
Updated on 03-Mar-2020 06:27:16

170 Views

In this article we consider a dictionary where the values are presented as lists. Then we consider clearing those values from the lists. We have two approaches here. One is to use the clear methods and another is to designate empty values to each key using list comprehension.Example Live Demox1 = {"Apple" : [4, 6, 9, 2], "Grape" : [7, 8, 2, 1], "Orange" : [3, 6, 2, 4]} x2 = {"mango" : [4, 6, 9, 2], "pineapple" : [7, 8, 2, 1], "cherry" : [3, 6, 2, 4]} print("The given input is : " + str(x1)) # using loop + ... Read More

Python - Check if frequencies of all characters of a string are different

Pradeep Elance
Updated on 03-Mar-2020 06:21:19

212 Views

In this article we will see how to find the frequency of each character in a given string. Then see if two or more characters have the same frequency in the given string or not. We will accomplish this in two steps. In the first program we will just find out the frequency of each character.Frequency of each characterHere we find the frequency of each character in the given input screen. We declare a empty dictionary and then add each character as a string. We also assign keys to each of the character to create the key-value pair needed by ... Read More

Python - Check if dictionary is empty

Pradeep Elance
Updated on 03-Mar-2020 06:17:58

6K+ Views

During analysis of data sets we may come across situations where we have to deal with empty dictionaries. In tis article we will see how to check if a dictionary is empty or not.Using ifThe if condition evaluates to true if the dictionary has elements. Otherwise it evaluates to false. So in the below program we will just check the emptiness of a dictionary using only the if condition.Example Live Demodict1 = {1:"Mon", 2:"Tue", 3:"Wed"} dict2 = {} # Given dictionaries print("The original dictionary : " ,(dict1)) print("The original dictionary : " ,(dict2)) # Check if dictionary is empty if dict1: ... Read More

Advertisements