• 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

Kata: Concatenate N N Times

 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Last night was our local Functional Kats {Belfast} meetup group that an ex-colleague and I organise. The kata we worked on was a particularly fun one and good enough to share with all you good Ranchers.

Functional Kats Kata wrote:
Concatenate N N Times

A number like 7777777 consists of the number 7 concatenated to itself 7 times. A number like 121212121212121212121212 consists of the number 12 concatenated to itself 12 times. That is to say: the number X consists of the number N concatenated to itself N times.

Your task is to write a program that calculates the number that is concatenated to itself, and the number of times it appears.


(Source: programmingpraxis.com)

Have a go yourself and post your solutions. And most importantly... Have fun!
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll start with a Clojure solution:


With output:
 
Saloon Keeper
Posts: 15523
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should it be functional or is a good old OO approach allowed?
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the context of our meet up we don't prescribe a language but we do stipulate encourage that it's done in a functional style.

But, in the context of CodeRanch anything goes. Choose whatever language and style you wish.
 
Bartender
Posts: 598
26
Oracle Notepad Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oracle SQL. This ought to be easier in SQL Server, because of REPEAT().

Output:
 
Stephan van Hulst
Saloon Keeper
Posts: 15523
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haskell:

Output:
 
Master Rancher
Posts: 4827
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Scala:
 
Stephan van Hulst
Saloon Keeper
Posts: 15523
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inspired by Mike (shows you it pays off if you know the libraries):

This solution probably runs a lot slower for longer strings though.
 
Mike Simmons
Master Rancher
Posts: 4827
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I went for brevity and simplicity over performance - could be a big difference for larger strings, as you note. I came up with a much more efficient alternative, rejecting many candidates based only on the input length, before it ever looks as the individual characters (once, at most). Roughly analogous solutions in functional Scala:

...and old-school Java
 
Mike Simmons
Master Rancher
Posts: 4827
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My mind keeps coming back to this one. Here's a much shorter Java solution, probably not quite as fast as the last two, but much faster than my first Scala solution above. And it doesn't even use any features newer than JDK 1.4:
 
Mike Simmons
Master Rancher
Posts: 4827
74
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though I guess that should really be

And the formula used to calculate word length (varible n) is an approximation, but from testing, it's accurate through the range of all possible input array lengths, up through Integer.MAX_VALUE. So unless we need to test longer input than that, we're good...
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great one, Mike!

Minor point: it fails with "0202", but it is not clear if such a string is within the bounds of the assignment.
My Pojo (with some obligatory lambdas, although an old fashioned java one would be more readable):

 
reply
    Bookmark Topic Watch Topic
  • New Topic