BreakPoint

cancel
Showing results for 
Search instead for 
Did you mean: 

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! Smiley Very 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 Smiley Wink), 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!"


Message 1 of 26
(8,247 Views)

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?

0 Kudos
Message 2 of 26
(8,228 Views)

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

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 3 of 26
(8,223 Views)

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!"


0 Kudos
Message 4 of 26
(8,221 Views)

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

0 Kudos
Message 5 of 26
(8,219 Views)

Ben, Altenbach

 

I realised this when I was going to use 321 as the example in the challenge description! Smiley Very 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. Smiley Wink

 

-CC 

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


0 Kudos
Message 6 of 26
(8,214 Views)

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 betterSmiley Surprised

 

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

 



"Should be" isn't "Is" -Jay
0 Kudos
Message 7 of 26
(8,163 Views)

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

 

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!"


0 Kudos
Message 8 of 26
(8,146 Views)

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!
Message 9 of 26
(8,133 Views)

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 Smiley Very 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!"


0 Kudos
Message 10 of 26
(8,123 Views)