Click here to Skip to main content
13,148,503 members (51,284 online)
Rate this:
Please Sign up or sign in to vote.
See more:
give me examples of sample programs
Posted 3-Jun-13 2:41am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You question is a bit vague, but I'll try to explain what I thought you were asking. As I understand your question you want to calculate the indentation depth to which I have two interpretations each with different answer as one might expect.

Q 1: The line is already properly indented and you want the indentation depth.

A 1: Take all the white space from the beginning of the line and if it is all tabs just return the length of this all white space string.
If it is spaces you you'd have to have knowledge of the least number of indenting spaces found in this file so far greater than zero and then make sure the current white space count can be divided without remainder by that. The result of said division is the indentation level
If you encounter mixed indentation characters something has run afoul and you should return some error stating this.

Q 2: Given some code without indentation how can I calculate the indentation depth of each line?

A 2: You can't! Indentation in Python has block semantic like ENDIF in VB.NET or braces {} in C#. You can change the indentation of a Python program still leaving you with valid code, but the semantics may have changed completely.

— Manfred

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170924.2 | Last Updated 3 Jun 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100