Found 33676 Articles for Programming

Program to find number of arithmetic subsequences from a list of numbers in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 07:19:24

443 Views

Suppose we have a list of numbers called nums, we have to find the number of arithmetic subsequences of length ≥ 3. As we know an arithmetic sequence is a list of numbers where the difference between one number and the next is the same.So, if the input is like nums = [6, 12, 13, 8, 10, 14], then the output will be 3, as we have subsequences like: [6, 8, 10], [6, 10, 14], [12, 13, 14].To solve this, we will follow these steps:dp := a new mapn := size of numsres := 0for i in range 0 to ... Read More

Program to find number of arithmetic sequences from a list of numbers in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 07:16:50

2K+ Views

Suppose we have a list of numbers called nums, we have to find the number of contiguous arithmetic sequences of length ≥ 3. As we know an arithmetic sequence is a list of numbers where the difference between one number and the next number is the same.So, if the input is like nums = [6, 8, 10, 12, 13, 14], then the output will be 4, as we have the arithmetic sequences like: [6, 8, 10] [8, 10, 12] [6, 8, 10, 12] [12, 13, 14]To solve this, we will follow these steps −count := 0, ans := 0for i ... Read More

How to restrict argument values using choice options in Python?

Kiran P
Updated on 10-Nov-2020 09:13:26

3K+ Views

Introduction..Assume you are asked to code a program to accept the number of tennis grandslam titles from the user and process them. We already know, Federer and Nadal share the maximum grandslam titles in Tennis which is 20 (As of 2020) while the minimum is 0, lot of players are still fighting to get their first grandslam title.Let us create a program to accept the titles.Note - Execute the program from terminal.Exampleimport argparse def get_args(): """ Function : get_args parameters used in .add_argument 1. metavar - Provide a hint to the user about the data type. - By default, ... Read More

How to use one or more same positional arguments in Python?

Kiran P
Updated on 10-Nov-2020 06:22:40

853 Views

Introduction..If we were writing a program that performs arithematic operations on two numbers, we could define them as two positional arguments. But since they are the same kinds/python data types of arguments, it might make more sense to use the nargs option to tell argparse that you want exactly two of same types.How to do it..1. Let's write a program to substract two numbers (both the arguments are of same type).Exampleimport argparse def get_args(): """ Function : get_args parameters used in .add_argument 1. metavar - Provide a hint to the user about the data type. - By default, all ... Read More

How to plot pie-chart with a single pie highlighted with Python Matplotlib?

Kiran P
Updated on 10-Nov-2020 06:16:11

928 Views

Introduction..What is your most favorite chart type ? If you ask this question to management or a business analyst, the immediate answer is Pie charts!. It is a very common way of presenting percentages.How to do it..1. Install matplotlib by following command.pip install matplotlib2. Import matplotlibimport matplotlib.pyplot as plt3. Prepare temporary data.tennis_stats = (('Federer', 20), ('Nadal', 20), ('Djokovic', 17), ('Murray', 3), )4. Next step is to prepare the data.titles = [title for player, title in tennis_stats] players = [player for player, title in tennis_stats]5. Create the pie chart with the values as titles and labels as player names.autopct parameter - ... Read More

How to plot 4D scatter-plot with custom colours and cutom area size in Python Matplotlib?

Kiran P
Updated on 10-Nov-2020 06:12:56

872 Views

Introduction..Scatter-plot are very useful when representing the data with two dimensions to verify whether there's any relationship between two variables. A scatter plot is chart where the data is represented as dots with X and Y values.How to do it..1. Install matplotlib by following command.pip install matplotlib2. Import matplotlibimport matplotlib.pyplot as plt tennis_stats = (('Federer', 20), ('Nadal', 20), ('Djokovic', 17), ('Sampras', 14), ('Emerson', 12), ('laver', 11), ('Murray', 3), ('Wawrinka', 3), ('Zverev', 0), ('Theim', 1), ('Medvedev', 0), ('Tsitsipas', 0), ('Dimitrov', 0), ('Rublev', 0))3. Next step is to prepare the data in any array format. We can also read the data from ... Read More

How to extract required data from structured strings in Python?

Kiran P
Updated on 10-Nov-2020 06:08:49

2K+ Views

Introduction...I will show you couple of methods to extract require data/fields from structured strings. These approaches will help, where the format of the input structure is in a known format.How to do it..1. Let us create one dummy format to understand the approach.Report: - Time: - Player: - Titles: - Country: Report: Daily_Report - Time: 2020-10-16T01:01:01.000001 - Player: Federer - Titles: 20 - Country: Switzerlandreport = 'Report: Daily_Report - Time: 2020-10-10T12:30:59.000000 - Player: Federer - Titles: 20 - Country: Switzerland'2. First thing I noticed from the report is the seperator which is "-". We will go ahead ... Read More

How to create Microsoft Word paragraphs and insert Images in Python?

Kiran P
Updated on 10-Nov-2020 06:00:18

473 Views

Introduction...Being a Data Engineering specialist, I often receive test results from testers in Microsoft word. Sigh! they put so much information into word document right from capturing screen shots and very big paragraphs.Other day, I was asked by testing team to help them with a program to insert the tool generated Text and images (taken by automatic screen shots. Not covered in this article).MS Word document unlike others doesn't have the concept of a page, as it works in paragraphs unfortunately.So we need to use breaks and sections to properly divide a document.How to do it..1. Go ahead and install ... Read More

How to save HTML Tables data to CSV in Python

Kiran P
Updated on 10-Nov-2020 05:53:33

2K+ Views

Problem:One of the most challenging taks for a data sceintist is to collect the data. While the fact is, there is plenty of data available in the web it is just extracting the data through automation.Introduction..I wanted to extract the basic operations data which is embedded in HTML tables from https://www.tutorialspoint.com/python/python_basic_operators.htm.Hmmm, The data is scattered in many HTML tables, if there is only one HTML table obviously I can use Copy & Paste to .csv file.However, if there are more than 5 tables in a single page then obviously it is pain. Isn't it ?How to do it..1. I will ... Read More

How to Add Legends to charts in Python?

Kiran P
Updated on 10-Nov-2020 05:46:55

4K+ Views

Introduction...The main purpose of charts is to make understand data easily. "A picture is worth a thousand words" means complex ideas that cannot be expressed in words can be conveyed by a single image/chart.When drawing graphs with lot of information, a legend may be pleasing to display relevant information to improve the understanding of the data presented.How to do it..In matplotlib, legends can be presented in multiple ways. Annotations to draw attention to specific points are also useful to help the reader understand the information displayed on the graph.1. Install matplotlib by opening up the python command prompt and firing ... Read More

Advertisements