Click here to Skip to main content
15,888,201 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Sith Interviewing Tactics Pin
Jeroen De Dauw5-Jul-09 14:14
Jeroen De Dauw5-Jul-09 14:14 
GeneralRe: Sith Interviewing Tactics Pin
0x3c05-Jul-09 20:27
0x3c05-Jul-09 20:27 
GeneralRe: Sith Interviewing Tactics Pin
Fatbuddha 16-Jul-09 1:04
Fatbuddha 16-Jul-09 1:04 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes6-Jul-09 3:39
ProtoBytes6-Jul-09 3:39 
GeneralRe: Sith Interviewing Tactics Pin
Fatbuddha 16-Jul-09 5:56
Fatbuddha 16-Jul-09 5:56 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes6-Jul-09 3:16
ProtoBytes6-Jul-09 3:16 
GeneralRe: Sith Interviewing Tactics Pin
Lutosław8-Jul-09 10:18
Lutosław8-Jul-09 10:18 
GeneralRe: Sith Interviewing Tactics [modified] Pin
Super Lloyd6-Jul-09 1:03
Super Lloyd6-Jul-09 1:03 
Recursion is indeed dangerous, particularly in this case!
However there is nothing wrong with a little bit of algo interview I believe.
Heck you could have impressed them by doing a non recursive version! Wink | ;-)

from the top of my mind here is a decomposition of an input number in a sum of fibonnaci number (which is unique)(pseudo code,i.e. might not compile, run and/or be full of error, use at your own risks!)

	public struct FibonacciNumber<br />
	{<br />
		public FibonacciNumber(int i, int v) { Index = i; Value = v; }<br />
		public int Value;<br />
		public int Index;<br />
	}<br />
	class Foo<br />
	{<br />
		static List<FibonacciNumber> Decompose(int number)<br />
		{<br />
			var allf = AllFibonacciTo(number);<br />
			var result = new List<FibonacciNumber>();<br />
			for (int i = allf.Count; i-- > 0; )<br />
			{<br />
				var f = allf[i];<br />
				if (f.Value <= number)<br />
				{<br />
					result.Add(f);<br />
					number -= f.Value;<br />
				}<br />
				if (number == 0)<br />
					break;<br />
			}<br />
			result.Reverse();<br />
			return result;<br />
		}<br />
<br />
		static List<FibonacciNumber> AllFibonacciTo(int number)<br />
		{<br />
			var result = new List<FibonacciNumber>();<br />
			result.Add(new FibonacciNumber(0, 1));<br />
			result.Add(new FibonacciNumber(1, 2));<br />
			int last = 2;<br />
			while (last < number)<br />
			{<br />
				var fn = new FibonacciNumber();<br />
				int N = result.Count;<br />
				fn.Index = N;<br />
				fn.Value = result[N - 1].Value + result[N - 2].Value;<br />
				last = fn.Value;<br />
				result.Add(fn);<br />
			}<br />
			return result;<br />
		}<br />
<br />
		static void Main()<br />
		{<br />
			int N = 78;<br />
			Dump(AllFibonacciTo(N));<br />
			Dump(Decompose(N));<br />
		}<br />
<br />
		static void Dump(List<FibonacciNumber> lf)<br />
		{<br />
			foreach (var item in lf)<br />
				Console.Write("({0}, {1}) ", item.Index, item.Value);<br />
			Console.WriteLine();<br />
		}<br />
	}


A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.

modified on Monday, July 6, 2009 7:10 AM

GeneralRe: Sith Interviewing Tactics Pin
Super Lloyd6-Jul-09 1:42
Super Lloyd6-Jul-09 1:42 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes6-Jul-09 3:19
ProtoBytes6-Jul-09 3:19 
GeneralRe: Sith Interviewing Tactics Pin
Super Lloyd6-Jul-09 3:30
Super Lloyd6-Jul-09 3:30 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes6-Jul-09 3:48
ProtoBytes6-Jul-09 3:48 
GeneralRe: Sith Interviewing Tactics Pin
Super Lloyd6-Jul-09 4:04
Super Lloyd6-Jul-09 4:04 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes6-Jul-09 5:13
ProtoBytes6-Jul-09 5:13 
GeneralRe: Sith Interviewing Tactics Pin
Super Lloyd6-Jul-09 4:07
Super Lloyd6-Jul-09 4:07 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes6-Jul-09 5:17
ProtoBytes6-Jul-09 5:17 
GeneralRe: Sith Interviewing Tactics Pin
molesworth6-Jul-09 4:14
molesworth6-Jul-09 4:14 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes18-Jul-09 5:10
ProtoBytes18-Jul-09 5:10 
GeneralRe: Sith Interviewing Tactics Pin
Super Lloyd18-Jul-09 14:11
Super Lloyd18-Jul-09 14:11 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes18-Jul-09 14:28
ProtoBytes18-Jul-09 14:28 
AnswerRe: Sith Interviewing Tactics Pin
ProtoBytes18-Jul-09 20:19
ProtoBytes18-Jul-09 20:19 
GeneralRe: Sith Interviewing Tactics [modified] Pin
Super Lloyd18-Jul-09 21:38
Super Lloyd18-Jul-09 21:38 
GeneralRe: Sith Interviewing Tactics Pin
Daniel Grunwald20-Jul-09 4:05
Daniel Grunwald20-Jul-09 4:05 
GeneralRe: Sith Interviewing Tactics Pin
Super Lloyd20-Jul-09 13:05
Super Lloyd20-Jul-09 13:05 
GeneralRe: Sith Interviewing Tactics Pin
Daniel Grunwald20-Jul-09 23:25
Daniel Grunwald20-Jul-09 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.