如何计算线的缩进级别? [英] how to compute the indentation level of the line ?

查看:108
本文介绍了如何计算线的缩进级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给我示例程序示例

推荐答案

你的问题有点模糊,但我会试着解释一下我以为你在问什么。正如我理解你的问题,你想要计算缩进深度,我有两种解释,每种解释都有不同的答案,正如人们所期望的那样。



问题1:线已经正确缩进,你想要缩进深度。



A 1 :从行首开始占用所有空白区域,如果是所有标签,只返回所有空白字符串的长度。

如果是空格,你必须知道到目前为止,此文件中找到的最少数量的缩进空间大于零,然后确保当前的空白计数可以除以其余的余数。所述除法的结果是缩进级别

如果你遇到混合缩进字符的东西已经发生冲突,你应该返回一些错误说明这一点。



Q 2:给定一些没有压痕的代码我怎样才能计算每条线的压痕深度?



A 2:你不能! Python中的缩进具有块语义,如VB.NET中的ENDIF或C#中的大括号{}。您可以更改Python程序的缩进,但仍然保留有效代码,但语义可能已完全改变。



问候,

- Manfred
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.

Regards,
— Manfred


这篇关于如何计算线的缩进级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆