Java:改进打印数字金字塔,具有对齐和反转功能

发布于 2024-12-17 08:44:13 字数 745 浏览 0 评论 0原文

注 1:我已经有了蛮力、冗长、循环混乱的代码。
注 2:寻找更优雅的方式来建立这一点 - “更少”的行数和巧妙的逻辑。 注3:不是作业,我是一个试图提高的业余爱好者。

要编码的函数:

printNumericPyramid(int depth, String alignment, String orientation);

深度:+ve 最大 4 的整数
对齐方式:“左”/“右”之一
方向:“直立”/“倒立”之一

0 始终位于顶点,无论是直立还是倒立
0259/9520 永远是金字塔的高度。


示例调用和预期输出

printNumericPyramid(4, "left", "inverted");

9876
543.
21..
0...

printNumericPyramid(4, "right", "upright");

...0
..12
.345
6789

printNumericPyramid(4, "right", "inverted");

6789
.345
..12
...0


printNumericPyramid(4, "left", "upright");

0...
21..
543.
9876

如果“较少”的行数可以成为所提议解决方案的重点,那就太好了。不是通过删除所有换行符:),而是通过最小化代码(即使它会损害可读性)。

Note 1: I already have the brute force, verbose, loopy messy code in place.
Note 2: Looking for more elegant ways of establishing this - "lesser" number of lines and clever logic.
Note 3: Not homework, I am a hobbyist trying to improve.

Function to be coded:

printNumericPyramid(int depth, String alignment, String orientation);

depth: +ve integer up to 4
alignment: one of "left"/"right"
orientation: one of "upright"/"inverted"

0 will always be at the apex, whether upright or inverted
0259/9520 will always be the height of the pyramid.


Samples calls and expected outputs:

printNumericPyramid(4, "left", "inverted");

9876
543.
21..
0...

printNumericPyramid(4, "right", "upright");

...0
..12
.345
6789

printNumericPyramid(4, "right", "inverted");

6789
.345
..12
...0


printNumericPyramid(4, "left", "upright");

0...
21..
543.
9876

It would be great if "lesser" number of lines could be the focus of the proposed solution. Not by removing all linebreaks:), but by minimization of code (even if it hurts readibility).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2024-12-24 08:44:13

(首先请注意,您的方法定义很容易出错:如果参数只能采用两个值,您可以使用布尔值而不是字符串。如果您拼写错误“左”或“右”或“倒置”或“直立” ",你的方法可以工作,但会产生错误的结果)

如果你想专注于少量的行,那么你不应该重复自己,你可以使用三元运算符和可用的 Java API。

可以基于以下类型的循环中非常“智能”且完全不可读的代码编写一个非常简短的解决方案:

   public void printNumericPyramid( final int depth, final String alignment, final String orientation ) {
        for (int i = 0; i < depth; i++) {
            for (int j = 0; j < depth; j++) {
                System.out.print ( ... ) // insert some smart hackery in here computing if we should print 0-9 or '.'
            }
            System.out.println();
        }
    }

然而,这最终可能不会那么容易阅读,而且很容易得到逻辑错误。

所以我写了另一个解决方案:

  • 在每个字符处,您要么打印下一个值(从 0 到 9),要么打印一个点“.”。

  • 当一行“完成”时,我要么添加该行,要么反转该行,具体取决于alignment参数的值。

  • 如果要求反转表示,则只需反转集合即可

    public void printNumericPyramid( 最终 int 深度, 最终字符串对齐方式, 最终字符串方向 ) {
        最终列表 l1 = new ArrayList();
        for (int i = 0, c = 0; i < 深度; i++) {
            最终 StringBuilder sb = new StringBuilder();
            for (int j = 0; j < 深度; j++) {
                sb.append( j >= 深度 - (i + 1) ? c++ : "." );
            }
            l1.add("left".equals(alignment) ? sb.reverse().toString() : sb.toString());
        }
        if ( "inverted".equals(orientation) ) Collections.reverse(l1);
    
        for ( 最终字符串 s : l1 ) {
            System.out.println( s );
        }
    }
    

(First note that your method definition is error-prone: if an argument can only take two values you can use a boolean instead of a String. If you mispell "left" or "right" or "inverted" or "upright", your method will work yet produce the wrong result)

If you want to focus on a low number of lines then you should not repeat yourself and you can make use of the ternary operator and of the available Java APIs.

You could write a very short solution based on very "smart" and totally unreadable code inside the following kind of loop:

   public void printNumericPyramid( final int depth, final String alignment, final String orientation ) {
        for (int i = 0; i < depth; i++) {
            for (int j = 0; j < depth; j++) {
                System.out.print ( ... ) // insert some smart hackery in here computing if we should print 0-9 or '.'
            }
            System.out.println();
        }
    }

However this may not end up being that easy to read and it's going to be easy to get the logic wrong.

So I wrote another solution:

  • at each character you either print the next value (from 0 to 9) or you print a dot '.'.

  • when a line is "done", I either add that line or that line reversed, depending on the value of the alignment parameter.

  • if the inversed representation is asked, you can simply reverse the collection

    public void printNumericPyramid( final int depth, final String alignment, final String orientation ) {
        final List<String> l1 = new ArrayList<String>();
        for (int i = 0, c = 0; i < depth; i++) {
            final StringBuilder sb = new StringBuilder();
            for (int j = 0; j < depth; j++) {
                sb.append( j >= depth - (i + 1) ? c++ : "." );
            }
            l1.add("left".equals(alignment) ? sb.reverse().toString() : sb.toString());
        }
        if ( "inverted".equals(orientation) ) Collections.reverse(l1);
    
        for ( final String s : l1 ) {
            System.out.println( s );
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文