• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Machiavellian ways to get a String's length without using length()

 
Rancher
Posts: 1044
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Seems to me that would exclude bitLength()



While you were writing the post, I thought about this and made a variation without bitLength.

[Edit by Winston: Split from here as we were getting into the realms of fantasy. Great fun though; and any further contributions happily received. ]


 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like it. My offering:
Winston
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To replace all characters with a digit, and then to produce a numeric value, and to tell the length on an arithmetic basis was an idea
which we elaborated many varations for.

Let us see a solution which needs only the given string: it does not produce an auxiliary one.


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another super horrible ugly solution.

Do not ever use this for real. I'll shame you on TheDailyWTF.com if you do.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Here's another super horrible ugly solution.


Hate to burst your bubble, but the original post did say "without using any built in length methods or length properties".

I appreciate its dastardly approach though; particularly the UTF-16 and '/ 2'.

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My take:

Magic number needs to be determined experimentally for given platform, string length and JVM settings.

Not very reliable, but definitely Machavellian
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahem. "Machiavellian"
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Ahem. "Machiavellian"


I was surprised too, but "Machavellian" passed the Wikipedia test: http://en.wikipedia.org/wiki/Machavellian
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what that's supposed to signify - looks like a redirect to the proper spelling. No indication that Machavelli is actually correct in any way.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're probably right. I didn't expect Wikipedia to contain redirects from an incorrect spelling to a correct one and though that "Machavellian" is used in some language or region. In any case, "Machavellian" gets a lot of hits on Google, so it looks like this incorrect form is in a quite widespread use.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Ahem. "Machiavellian"


Ah. My fault. It was misspelt in the subject too. Corrected.

Well spotted.

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The word comes from the name Niccolò Machiavelli so there is not much freedom how to spell it.
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Jesper de Jong wrote:Here's another super horrible ugly solution.


Hate to burst your bubble, but the original post did say "without using any built in length methods or length properties".

I appreciate its dastardly approach though; particularly the UTF-16 and '/ 2'.


I was disappointed to see Jesper's contribution above, if only because it was essentially the same as the idea I came up with, and days before I came along. However, I offer improvements: One, a way to avoid the "built in length methods or length properties" (as much as any of these do, anyway). And two, a minor bug fix: UTF-16 allows an optional byte order mark, which can give you an off by one error - it did show up on my Mac, but not on my Linux box. This is eliminated by using UTF-16LE or UTF-16BE instead. Oh, there's also three: this method should be considerably more performant, avoiding file I/O.

 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also question why it was necessary to split this off from the original thread - anything other than simply using length() is a silly waste of time, in a professional setting. The whole premise is really fodder for Programming Diversions.

I also note that almost all these methods also use length internally - we're just delegating the "dirty" work to other standard libraries. The only potential exception I see is Martin's contribution - if we eliminate the "Multiply it 4096 times" loop, it actually does seem to not rely on any internal length properties. Of course it then becomes very imprecise. But for a long enough string, it can at least give an approximation. Kudos for a truly original idea there.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:I also question why it was necessary to split this off from the original thread...


Maybe overkill on my part; I'm still relatively new to this moderating business. It just seemed to me that we'd got the message across to OP that not using length() wasn't a great idea, and that the rest was more suitable as a fun thread for the PD forum.

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> as a fun thread

It has become one indeed.
 
Let nothing stop you! Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic