Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

center a string in padding using String.format  RSS feed

 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I want to output pascals triangle and have it LOOK like an equilateral triangle.

I have some code:
This code does not work but I think it gets the idea across. I am trying to get an output in a monospaced text field to look like this.

This is apparently not a trivial output. Any help is appreciated.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
part of the problem is that you don't know how to format a row until you know how wide the last row is, which you won't know until you generate it. each new row has one more item, but the item themselves get wider.

I think you can't build the formatted string untils you're done generating the whole triangle.
 
Campbell Ritchie
Marshal
Posts: 55687
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it difficult? If you have numbers every 5 spaces, then the nth row will require (n - 1) * 5 spaces across.
So your padding for the 1st row would be (n - 1) * 5 / 2 spaces.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is difficult because each element in the pascals triangle must also be padded. I have the following code that works but it is UGLY! The function is being passed the triangle with a space between each number (horizontally). Like this:

1
1 1
1 2 1

Does anyone have any suggestions for making it more succinct?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Is it difficult? If you have numbers every 5 spaces, then the nth row will require (n - 1) * 5 spaces across.
So your padding for the 1st row would be (n - 1) * 5 / 2 spaces.

I don't think it's that simple. Eventually, you will get to rows where the numbers are bit long ..i.e. 283774. you need to take into account how wide the numbers you are displaying will be as well, and that is not a simple to compute.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure that you would find the answer in one of Paul Erdős's papers... but which one?
 
Campbell Ritchie
Marshal
Posts: 55687
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: . . . to take into account how wide the numbers you are displaying will be . . .
I take your point, Fred. Maybe the %d tag with a width? But that won't put the numbers neatly in the middle of their columns. One way to compute width is with the log of the number.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!