Argument Parsing in Python

Pradeep Elance
Updated on 26-Aug-2020 06:25:32

2K+ Views

Every programming language has a feature to create scripts and run them from the terminal or being called by other programs. When running such scripts we often need to pass on arguments needed by the script for various functions to be executed inside the script. In this article we will see what are the various ways to pass arguments into a python script.Using sys.argvThis is a inbuilt module sys.argv can process arguments that are passed on with the script. By default the first argument which is considered at sys.argv[0] is the file name. the rest of the arguments are indexed ... Read More

Add Style to Python Tkinter Button

Pradeep Elance
Updated on 26-Aug-2020 06:22:39

3K+ Views

Tkinter has great support for creating the GUI programs based on python. It offers different ways of styling a button on the Tkinter canvas based on its font, size, colour etc. In this article we will see how to apply style to specific buttons or all buttons in general on the canvas.Applying to Specific ButtonsLets consider the case when we have two buttons in the canvas and we want to apply some styling only to the first button. We use the W.TButton as part of the configuration along with the font and the foreground colour.Examplefrom tkinter import * from tkinter.ttk ... Read More

Find Triplet in Balanced BST that Adds to Zero in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:33:38

226 Views

Suppose we have a balanced binary search tree, we have to create a function named is_valid_triplet() that returns true when there exist a triplet in given BST whose sum equals to 0, otherwise returns false. Design the method by following these constraints −expected time complexity is O(n^2)O(logn) extra space can be used.So, if the input is likethen the output will be True, as triplet is [-15, 7, 8]To solve this, we will follow these steps −Define a function bst_to_doubli_list(), this will take root, head, tail, if root is same as NULL, then −returnif left of root is not null, then ... Read More

Find Itinerary from a Given List of Tickets in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:29:58

185 Views

Suppose we have a list of tickets represented by pairs of departure and arrival airports like [from, to], we have to find the itinerary in order. All of the tickets belong to a man who departs from Chennai. So, the itinerary must begin with Chennai.So if the input is like [["Mumbai", " Kolkata"], ["Chennai ", " Mumbai"], ["Delhi", "Bangalore"], ["Kolkata", " Delhi"]], then the output will be ["Chennai", " Mumbai", " Kolkata", " Delhi", "Bangalore"].To solve this, we will follow these steps −Define array ret and a map called graph.Define a method called visit. This will take airport name as ... Read More

Find Jobs Involved in Weighted Job Scheduling in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:25:51

229 Views

Suppose we have a list of N jobs where each job has three parameters. 1. Start Time 2. Finish Time 3. Profit We have to find a subset of jobs associated with maximum profit so that no two jobs in the subset overlap.So, if the input is like N = 4 and J = {{2, 3, 55},{4, 6, 25},{7, 20, 150},{3, 150, 250}} , then the output will be [(2, 3, 55),(3, 150, 250)] and optimal profit 305To solve this, we will follow these steps −Define a function find_no_conflict(), this will take an array jobs, index,left := 0, right := index - 1while left

Find K-th Smallest Element in BST

Arnab Chakraborty
Updated on 25-Aug-2020 12:23:13

566 Views

Suppose we have a binary search tree and a value K as input, we have to find K-th smallest element in the tree.So, if the input is likek = 3, then the output will be 15.To solve this, we will follow these steps −Define a function find_kth_smallest(), this will take root, count, k, if root is NULL, then −return NULLleft = find_kth_smallest(left of root, count, k)if left is not NULL, then −return left(increase count by 1)if count is same as k, then −return rootreturn find_kth_smallest(right of root, count, k)From the main method, do the following −count := 0res = find_kth_smallest(root, ... Read More

Find Memory Conflicts Among Multiple Threads in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:19:14

201 Views

Suppose we have a RAM and that RAM is organized in blocks. there are multiple processes running on the system. We have to keep in mind that every process gets following information, (Thread T, Memory Block M, time t, R/W) This indicates the thread T was implementing memory block M at given time t and operation could be either read(R) or write(W).The following case is indicating whether it is memory conflict or not −More than one read operations at the same location are not the reason of conflict.When writing operation is being performed between x+5 to x-5 to location of ... Read More

Find Minimum S-T Cut in a Flow Network in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:16:47

352 Views

Suppose we have following flow network. As we know an s-t cut is a cut that requires the source s node and a sink t node to be in different subsets, and it includes edges going from the source set to the sink side. Here the capacity of an s-t cut is represented by the sum of each edge capacity in the cut-set. Here we have to find minimum capacity s-t cut of the given network. Here the expected output is all edges of the minimum cut.So, if the input is likethen the output will be [(1, 3), (4, 3), ... Read More

Find Pair for Given Sum in a Sorted Singly Linked List in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:11:04

172 Views

Suppose we have a singly linked list and a value x; we have to find a pair whose sum is same as x. We have to keep in mind that we cannot use any extra space and expected time complexity will be O(n).So, if the input is like 4→7→8→9→10→11→12, x = 19, then the output will be [(7, 12), (8, 11), (9, 10)]To solve this, we will follow these steps −Define a function convert_to_xor(), this will take start, prev := NULLwhile start is NULL, do −next_list_node := next of startnext of start := XOR of the address of next_list_node and ... Read More

Find Pair of Rows in a Binary Matrix with Maximum Bit Difference in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:06:43

177 Views

Suppose we have a binary matrix; we have to find the pair of rows in the given matrix that has maximum bit difference.So, if the input is like matrix, then the output will be [2, 3] as bit difference between rows 2 and row 3 is 4, this is maximum.To solve this, we will follow these steps −Define Trie structure, with value and two children.Define a function get_max_bit_diff(), this will take root of a trie, matrix, n, row_index, temp := root, count := 0for initialize i := 0, when i < n, update (increase i by 1), do−if child[ matrix[row_index, ... Read More

Advertisements