NI Home
Cart Cart | Help
Hello Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI
You are here: 
NI Home > NI Developer Zone > NI Discussion Forums


Reply
Active Participant
ChrissyC87
Posts: 224

Coffee Break Coding Challenge

I really enjoyed having a go at Jcarmody's FizzBuzz Challenge a couple of weeks ago.

 

It was a bit of a distraction during a lunch break, but more importantly seeing other peoples solutions gave me new insight on how some functions operate.

 

So i'm posting a new Coffee Break Coding Challenge (fanfare please! :smileyvery-happy:). 

 

The challenge is supposed to be a simple 5-10 min puzzle (but some Knights might struggle to make it last 5 - 10 secs :smileywink:), hopefully producing some varied solutions that we can all learn from.

 

If this takes off and enough people enjoy it, perhaps we could make a regular feature. Anyway I'm getting ahead of myself; so here is the first Challenge:

 

 

"For any number, find the next highest number that uses all of the same digits. E.g. The next highest number after 123, using all the digits 1, 2 & 3, is 132."

 

 

-CC

 

p.s. Post your solutions in a spoiler box to avoid sending people down the same path as you!  

----------------------------------------------------------------------------------------------------------------------------
"If anyone needs me, I'll be in the Angry Dome!"


Knight of NI
altenbach
Posts: 22,853
0 Kudos

Re: Coffee Break Coding Challenge

Interesting. I probably won't have time for this before the weekend.

 

Anyway, what are the limits on "any number", does it need to fit in one of the integer data types or can it have a near infinite number of digits?

Is the input number a decimally formatted string?


LabVIEW Champion . Oh, by the way, I work for peanutsKudos .

Knight of NI
Knight of NI
Ben
Posts: 16,091
0 Kudos

Re: Coffee Break Coding Challenge

And since I am doing a stand-up coding routine let me ask...

 

THe input value will be assumed to have a larger available eg 321 ?

 

Ben

Ben Rayner
Who is NOT John Galt... yet... just building Rayner's Ridge

Active Participant
ChrissyC87
Posts: 224
0 Kudos

Re: Coffee Break Coding Challenge

I did think about putting in strictly defined conditions on the input number, but I thought this might limit the creativity of some solutions.

 

A basic program might consider that only integers data types are allowed, but a more complex one would consider alternatives.

 

 

I guess my answer would be that it is up to you to define what "any number" means and either state the conditions your solution works for or impliment controls to ensure that people can't break it!

 

 

-CC

 

p.s. If this is too vague then we could easily introduce type definitions into the challenge description

----------------------------------------------------------------------------------------------------------------------------
"If anyone needs me, I'll be in the Angry Dome!"


Knight of NI
altenbach
Posts: 22,853
0 Kudos

Re: Coffee Break Coding Challenge

As ben pointed out, there are quite a few numbers that don't have a solution. (321, 555555555555555555, etc.)


LabVIEW Champion . Oh, by the way, I work for peanutsKudos .

Active Participant
ChrissyC87
Posts: 224
0 Kudos

Re: Coffee Break Coding Challenge

[ Edited ]

Ben, Altenbach

 

I realised this when I was going to use 321 as the example in the challenge description! :smileyvery-happy:

 

 

I would say that it's the programmers responsibility to check that an input will function correctly with his/her programme and alert the user if it won't. :smileywink:

 

-CC 

----------------------------------------------------------------------------------------------------------------------------
"If anyone needs me, I'll be in the Angry Dome!"


Proven Zealot
Jeff·Þ·Bohrer
Posts: 5,333
0 Kudos

Re: Coffee Break Coding Challenge

[ Edited ]

are we limited to decimal digits or limited by non-duplicated digits in any place? (eg. for "123" would 1123 be a valid solution?  This, I believe but do not wish to prove mathematically today, would theoretically unbound the solution by allowing 123, 1233, 123.3, 123.23, ad nasuem as member of the set solving the problem )

 

Love these types of posts!  Really tough to pose these problems!  noobs do it better:smileysurprised:

 

If you can pose these problems.... I would love to play!  I'm just not sharp enough to forment the questions.  Really (I do puzzles often, and really like the problem solving... I do not author puzzles.  That takes a skill set I lack)

 

Spoiler
123.1

 



"All shall be well and all shall be well and all manner of things shall be well" -Julian
Active Participant
ChrissyC87
Posts: 224
0 Kudos

Re: Coffee Break Coding Challenge

Right ok guys, this is all my fault for not properly defining the challenge (surely this never happens in the real world! :smileywink:)

 

So now i'm going to redefine the problem:

 

 

Your task (should you choose to accept it) is to produce a piece of code that will accept any integer input and calculate the next highest integer that uses all of the same digits.

 

Limits:

  • only integer numbers can be inputted or returned.
  • each digit can only be used once. e.g  for 123 the next highest is 132, you cannot use the 1 twice to give 131.

 

Cue the A-team music and good luck! 

----------------------------------------------------------------------------------------------------------------------------
"If anyone needs me, I'll be in the Angry Dome!"


Knight of NI
Knight of NI
tst
Posts: 10,229

Re: Coffee Break Coding Challenge

[ Edited ]

OK, so here is a baseline - the stupid, brute force approach. No math, no elegance, just a quick and dirty solution, which seems to work correctly. It even handles duplicate digits, but it only works with positive integers.

 

Spoiler
Find Next Number.png

 


___________________
Try to take over the world!
Active Participant
ChrissyC87
Posts: 224
0 Kudos

Re: Coffee Break Coding Challenge

Here's my crack at this, done over lunch.

 

I'm sure there are much more elegant ways of doing the maths and sorting the arrays etc. so comments/improvements on any pieces of code in this thread here would be a great help to everyone.  

 

I confidently expect to see this re-posted in the Rube thread :smileyvery-happy:

 

Spoiler
NHN.png

 You're going to have to excuse the wiring - I've still got a giant sandwich to get through before the lunch hour is over! 

 

-CC  

 

----------------------------------------------------------------------------------------------------------------------------
"If anyone needs me, I'll be in the Angry Dome!"


By using this web site, you accept the Terms of Use for this web site. Please read these Terms of Use carefully before using any part of this site. Please go here for information on ni.com's copyright infringement policy.
My Profile | Privacy | Legal | Contact NI © 2011 National Instruments Corporation. All rights reserved.    |    E-Mail this Page E-Mail this Page