Programming Articles - Page 2121 of 3363

Binary Tree to Binary Search Tree Conversion using STL set C++?

Arnab Chakraborty
Updated on 29-Jan-2020 12:11:48

785 Views

In case of a given Binary Tree, convert it to a Binary Search Tree in such a way that keeps the original structure of Binary Tree intact.Sets of C++ STL will be used by this solution instead of array based solution.ExamplesExample 1Input     11     /  \    3    8 /     \ 9 5Output     9    /   \   5     11  /        \ 3        8Example 2Input      11      /   \     31    16    /        \  21 ... Read More

Required arguments in Python

Mohd Mohtashim
Updated on 30-Jan-2020 06:14:12

9K+ Views

Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition.To call the function printme(), you definitely need to pass one argument, otherwise it gives a syntax error as follows −Example Live Demo#!/usr/bin/python # Function definition is here def printme( str ): "This prints a passed string into this function" print str return; # Now you can call printme function printme()OutputWhen the above code is executed, it produces the following result −Traceback (most recent call last): File "test.py", line 11, in printme(); TypeError: ... Read More

Binary Number System - Overflow in Arithmetic Addition in C/C++?

Arnab Chakraborty
Updated on 29-Jan-2020 11:43:59

941 Views

2’s Complement Number System is widely implemented in computer architecture.N-bit 2’s Complement number System can be able to represent Number from -2n-1 to 2n-1- 14 Bit can be able to represent numbers from ( -8 to 7 )5 Bit can be able to represent numbers from ( -16 to 15 ) in 2’s Complementary System.Overflow happens with respect to addition when 2 N-bit 2’s Complement Numbers are appended and the answer is too large to fit into that N-bit Group.A computer contains N-Bit Fixed registers. Result of addition of two N-Bit Number will result maximum N+1 Bit number.Carry Flag stores ... Read More

Binary Indexed Tree or Fenwick Tree in C++?

Arnab Chakraborty
Updated on 29-Jan-2020 11:11:23

480 Views

In case of comparing with a flat array of numbers, the Fenwick tree results a much better balance between two operations: element update and prefix sum computation. In case of a flat array of m numbers, we can either store the elements, or the prefix sums. In case of first instance, calculating prefix sums needs linear time; in case of second instance, modifying or updating the array elements needs linear time (in both instances, the other operation can be accomplished in constant time). Fenwick trees permit both operations to be accomplished in O(log m) time. This is obtained by representing ... Read More

Bin Packing Problem (Minimize number of used Bins) in C++?

Arnab Chakraborty
Updated on 29-Jan-2020 11:08:42

4K+ Views

In case of given m elements of different weights and bins each of capacity C, assign each element to a bin so that number of total implemented bins is minimized. Assumption should be that all elements have weights less than bin capacity.ApplicationsPlacing data on multiple disks.Loading of containers like trucks.Packing advertisements in fixed length radio/TV station breaks.Job scheduling.ExampleInput: weight[] = {4, 1, 8, 1, 4, 2} Bin Capacity c = 10 Output: 2 We require at least 2 bins to accommodate all elements First bin consists {4, 4, 2} and second bin {8, 2}Lower BoundWe can always calculate a lower ... Read More

Pass by reference vs value in Python

SaiKrishna Tavva
Updated on 20-Nov-2024 17:40:48

12K+ Views

In Python Call by Value and Call by Reference are two types of generic methods to pass parameters to a function. In the Call-by-value method, the original value cannot be changed, whereas in Call-by-reference, the original value can bechanged. Call by Value in Python When we pass an argument to a function, it is stored locally (in the stack memory), i.e the scope of these variables lies with in the function and these will not have effect the values of the golbal variables (variables outside function). In Python, "passing by value" is possible only with the immutable types such as integers, floats, strings, ... Read More

Calling a Function in Python

Mohd Mohtashim
Updated on 29-Jan-2020 11:10:26

546 Views

Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code.Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is the example to call printme() function − Live Demo#!/usr/bin/python # Function definition is here def printme( str ): "This prints a passed string into this function" print str return; # Now you can call printme function printme("I'm first call to user defined function!") printme("Again second call to the same function")OutputWhen ... Read More

The calendar Module in Python

Mohd Mohtashim
Updated on 29-Jan-2020 11:09:25

652 Views

The calendar module supplies calendar-related functions, including functions to print a text calendar for a given month or year.By default, calendar takes Monday as the first day of the week and Sunday as the last one. To change this, call calendar.setfirstweekday() function.Here is a list of functions available with the calendar module −Sr.NoFunction with Description1calendar.calendar(year, w=2, l=1, c=6)Returns a multiline string with a calendar for year year formatted into three columns separated by c spaces. w is the width in characters of each date; each line has length 21*w+18+2*c. l is the number of lines for each week.2calendar.firstweekday( )Returns the current setting ... Read More

Barabasi Albert Graph (for Scale Free Models) in C/C++?

Arnab Chakraborty
Updated on 29-Jan-2020 11:00:00

332 Views

The Barabási-Albert model is treated as one of several proposed models that produce scale-free networks. It combines two important general concepts: growth and preferential attachment. Both concepts i.e. growth and preferential attachment have wide existence in real networks. The meaning of growth is that the number of nodes in the network increases over time.The meaning of preferential attachment is that the more connected a node is, the more chance it is to receive new links.Higher degree nodes have stronger ability to catch or grab links added to the network. Basically, the preferential attachment can be well understood if we think ... Read More

The time Module in Python

Mohd Mohtashim
Updated on 29-Jan-2020 11:08:47

513 Views

There is a popular time module available in Python which provides functions for working with times and for converting between representations. Here is the list of all available methods −Sr.NoFunction with Description1time.altzoneThe offset of the local DST timezone, in seconds west of UTC, if one is defined. This is negative if the local DST timezone is east of UTC (as in Western Europe, including the UK). Only use this if daylight is nonzero.2time.asctime([tupletime])Accepts a time-tuple and returns a readable 24-character string such as 'Tue Dec 11 18:07:14 2008'.3time.clock( )Returns the current CPU time as a floating-point number of seconds. To measure computational ... Read More

Advertisements