Found 33676 Articles for Programming

Find three integers less than or equal to N such that their LCM is maximum - C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:27:54

495 Views

In this tutorial, we are going to write a program that is based on the concepts of LCM. As the title says, we have to find three numbers that are less than or equal to the given number whose LCM is maximum.Let's see an example.Before diving into the problem let's see what LCM is and write program for it.LCM is the least common multiple of a number. It is also known as Least common divisor. For two positive number a and b, the LCM is the smallest integer that is evenly divisible by both a and b.If the given integers ... Read More

Python - Add Label to kivy window

Pradeep Elance
Updated on 28-Dec-2020 11:25:51

685 Views

Kivy is an pen source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. It is used to develop the Android application, as well as Desktops applications. In this article we will see how to add labels to the windows created through Kivy.Creating LabelIn the below example we create a window and give it a custom label by using Label function available in uix.lable module. Once we run the app with this code we get the new window showing the custom label.Examplefrom kivy.app import App from kivy.uix.label import Label class ... Read More

Python - Combine two lists by maintaining duplicates in first list

Pradeep Elance
Updated on 28-Dec-2020 11:22:16

411 Views

In data analysis using python we may come across situation when two lists need to be merged. But it may be a challenge to handle the duplicate elements present in those lists. In this article we will see how to combine two lists by maintain all the elements form the first list and only the unique elements from the second list.Using extendIn this approach we take the first list and create a result list. Then we design a for loop to check for the presence of element of the first list in the second list and if the element is ... Read More

Python - Convert 1D list to 2D list of variable length

Pradeep Elance
Updated on 28-Dec-2020 11:20:56

3K+ Views

A list in python is normally a 1D list where the elements are listed one after another. But in a 2D list we have list nested inside the outer list. In this article we will see how to create a 2D list from a given 1D list. We also supply the values for the number of elements inside the 2D list to the program.Using append and indexIn this approach we will create a for loop to loop through each element in the 2D list and use it as an index for the new list to be created. We keep incrementing ... Read More

Python - Convert a list of lists into tree-like dict

Pradeep Elance
Updated on 28-Dec-2020 11:19:45

1K+ Views

Given a nested list we want to convert it to a dictionary whose elements can be considered as part of a tree data structure. In this article we will see the two approaches to convert a nested list into to add dictionary whose elements represent a tree like data structure.Using SlicingWe reverse the items in the list aby slicing and then check if the item is present in the list. If it is not present we ignore it else we add it to the tree.Exampledef CreateTree(lst):    new_tree = {}    for list_item in lst:       currTree = ... Read More

Python - Convert flattened dictionary into nested dictionary

Pradeep Elance
Updated on 28-Dec-2020 11:18:13

710 Views

Python dictionaries have keys and values. If we have two or more dictionaries to be merged a nested dictionary, then we can take the below approaches. Here year the dictionaries are given along with the new keys that will become a key in the nested dictionary.Assigning keysIn this approach we will create a new empty dictionary. Then assigned the given dictionaries to each new key. The resulting dictionary will be a nested dictionary with the keys assigned.ExampledictA = {'Sun': 1, 'Mon': 2} dictB = {'Tue': 3, 'Sun': 5} # Given Dictionaries print("DictA : ", dictA) print("DictB: ", dictB) ... Read More

Python - Combine two dictionary adding values for common keys

Pradeep Elance
Updated on 28-Dec-2020 11:17:05

2K+ Views

When analyzing data with python we come across situations when we have to merge two dictionaries in such a way that we add the values of those elements whose keys have equal values. In this article we will see such two dictionaries getting added.With For loop and | OperatorIn this approach we design a for loop to check for the presence of the value of the key in both the dictionaries and then add them. Finally we merge the two dictionaries using the | operator available for the dictionaries.ExampledictA = {'Mon': 23, 'Tue': 11, 'Sun': 6} dictB = {'Wed': 10, ... Read More

Python - Check if k occurs atleast n times in a list

Pradeep Elance
Updated on 28-Dec-2020 11:15:57

334 Views

Many times during data analysis using lists we come across a situation where we need to find out if a given element is present at least N Times in the given list. For example if 5 is present at least three times in the list or not. In this article we will see 2 approaches on how to achieve this.Counting OccurrencesIn the below approach we take the number and it's occurrences as an input. Then we e designer follow to keep the count of the occurrences. If the count value is greater than or equal to the required value then ... Read More

Pygorithm module in Python

Pradeep Elance
Updated on 28-Dec-2020 11:14:03

381 Views

The Pygorithm module is an educational module containing the implementation of various algorithms. The best use of this module is to get the code of an algorithm implemented using python. But it can also be used for actual programming where we can apply the various algorithms to a given data set.Finding the Data StructuresAfter installing the module in the python environment we can find the various data structures that is present in the package.Examplefrom pygorithm import data_structures help(data_structuresRunning the above code gives us the following result −OutputHelp on package pygorithm.data_structures in pygorithm: NAME    pygorithm.data_structures - Collection of data structure ... Read More

OS Path module in Python

Pradeep Elance
Updated on 28-Dec-2020 11:11:29

10K+ Views

The os.path module is a very extensively used module that is handy when processing files from different places in the system. It is used for different purposes such as for merging, normalizing and retrieving path names in python . All of these functions accept either only bytes or only string objects as their parameters. Its results are specific to the OS on which it is being run.os.path.basenameThis function gives us the last part of the path which may be a folder or a file name. Please the difference in how the path is mentioned in Windows and Linux in terms ... Read More

Advertisements