Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

A challenge problem for the night

 
author
Posts: 284
32
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, it's getting late in Berlin where I am now. So, here is a challenge for all insomniac JavaScripters. Write a function length(str) that yields the number of Unicode characters in a string str. For example, length('Hello 🌐') or length('Hello \u{1F310}') should be 7 and length(''😙💛🍺') should be 3. Pay attention that 🌐 and the other emoji have two code units, so you can't just return str.length (which would be 8 or 6). Try to come up with as many implementations as possible. I can think of at least 5 implementations, and the shortest one has length 31. Can you beat that? And how would you do it in Java?

Cheers,

Cay
 
Marshal
Posts: 70370
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
22 characters in Java® if you start with String s = "Hello ðŸŒ"; and you have a one‑letter identifier.
 
Cay Horstmann
author
Posts: 284
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, maybe we need firmer ground rules.

In Java, you need 44 characters just for



Then you can complete the function with another 27 characters. (At least that's the shortest that I can come up with.)

In JavaScript, you need 14 characters for



and then you can complete the function with another 13 characters. (At least that's the shortest that I can come up with.)

Cheers,

Cay
 
Saloon Keeper
Posts: 12295
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you change the starting code to:then you can get the number of Unicode code points with 22 additional characters of code. I'm assuming my solution is the same as Campbell's.
 
Rancher
Posts: 3636
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cay Horstmann wrote:
In Java, you need 44 characters just for



Then you can complete the function with another 27 characters. (At least that's the shortest that I can come up with.)



Well, I would increase your original 44 chars by one, 45, and then I can complete it successfully with only 22 more characters.  But I think we're talking about the same thing, essentially.  And your version has the more correct return type.
 
Mike Simmons
Rancher
Posts: 3636
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Curses - two minutes!
 
Cay Horstmann
author
Posts: 284
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We agree on the Java version. But back to JavaScript.

Looking at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String, there is nothing about counting code points. You could loop over the code points

>

That's 78 characters.

What about regular expressions? I can get it down to 42 characters by replacing all matches of /[ -\uFFFF]/ug

Cheers,

Cay
 
Campbell Ritchie
Marshal
Posts: 70370
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it's cheating to use JSheell, butThere is probably a code point count method.
 
Mike Simmons
Rancher
Posts: 3636
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well on the JavaScript side, I can get 39, Cay's root method declaration of 14 chars plus 25 characters of implementation:
 
Mike Simmons
Rancher
Posts: 3636
40
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if we give IE the finger, we have the lovely

27 chars, 14 + 13
 
Cay Horstmann
author
Posts: 284
32
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's another sweet thing about modern JavaScript. You don't worry about IE.
 
Mike Simmons
Rancher
Posts: 3636
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.  Can you say more about that?  Is that because IE just isn't so important anymore?  Or because we rely on other libraries or tools to compensate?  How does that work?  Thanks...
 
Cay Horstmann
author
Posts: 284
32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose somewhere out there someone must still support IE. In the market where I work (educational software), there has been zero pushback in the last three years to exclude IE from the list of supported browsers. More tellingly, Microsoft's Office cloud apps will no longer work with IE.

 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's been many a year since I've worked on a project where the attitude towards IE has not been "get over it".
 
Stephan van Hulst
Saloon Keeper
Posts: 12295
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our company works for some Dutch government agencies, and (sadly but understandably) many of them will not drop support for IE. Funny enough, many of them have completely ditched IE for internal services.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic