Click here to Skip to main content
15,914,225 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Coding Challenge - Morris Sequence Pin
Dave Kreskowiak5-Dec-17 15:40
mveDave Kreskowiak5-Dec-17 15:40 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 131943026-Dec-17 3:13
Member 131943026-Dec-17 3:13 
GeneralRe: Coding Challenge - Morris Sequence Pin
Dave Kreskowiak6-Dec-17 4:57
mveDave Kreskowiak6-Dec-17 4:57 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 131943027-Dec-17 4:22
Member 131943027-Dec-17 4:22 
GeneralRe: Coding Challenge - Morris Sequence Pin
PIEBALDconsult2-Dec-17 12:38
mvePIEBALDconsult2-Dec-17 12:38 
GeneralRe: Coding Challenge - Morris Sequence Pin
Tony Riddiough3-Dec-17 10:57
Tony Riddiough3-Dec-17 10:57 
GeneralRe: Coding Challenge - Morris Sequence Pin
PIEBALDconsult3-Dec-17 12:32
mvePIEBALDconsult3-Dec-17 12:32 
GeneralRe: Coding Challenge - Morris Sequence Pin
Tony Riddiough4-Dec-17 14:36
Tony Riddiough4-Dec-17 14:36 
The quick and dirty code I wrote was
C++
#include <stdio.h>
#include <stdlib.h>

class s {
private:
	char indx;
	char ondx;
	char v[10];
public:
	bool done;
	unsigned long long count;
	s(void) {
		indx = '\0';
		ondx = '\0';
		done = false;
		count= 0UL;
	}
	void put (char c) {
		v[indx++] = c;
		indx = indx % 10;
	}
	char get (void) {
		char c = v[ondx++];
		ondx = ondx %10;
		return c;
	}
	char peek (void) {
		return v[ondx];
	}
	bool isEmpty (void) {
		return indx == ondx;
	}
};

s context[100];

// the two functions "nextItem" and "doCount" call each other recursively.

bool nextItem (char& c, int level);

// count number of consecutive instances of v from level below
// return count as a character in 'c'; save v with msb set. and
// character which terminated count in context.
void doCount (char& c, char v, int level) {
	bool	r = false;
	s&	x = context[level];
	c = '1';
	x.put (v + 0X80);
	while (!x.done) {
		char t;
		x.done = nextItem (t, level - 1);
		if (t == v) {
			c++;
		}
		else {
			// count is complete so we need to put the terminating value
			// in the buffer, value we are counting is already there
			x.put (t);
			break;
		}
	}
}

// return in 'c' the next character from the specified level
// signal done if that character is the last.
// there are two special cases:
// 1) level = 0, the single character '1' is returned and done is signalled
// 2) There are no characters held in the context (this must be the first entry)
// Otherwise there are one or two characters in the context. If the next character 
// held in the context ha the msb set, the count has already been returned so the 
// character should be returned. Otherwise the character is the terminating character
// from the last count and more repetitions (if any) must be counted, after which the
// count is returned. When the lower level has signalled done, then when the last
// character is returned also signal done.
// Count the number of characters returned and when the last character is returned and 
// done s signalled, report the level and the total.

bool nextItem (char& c, int level) {
	s&	x = context[level];
	bool		r = false;
	if (!x.isEmpty()) {
		// more ready to output
		char v = x.get();
		c = v & 0X7F;
		if (v & 0X80) {
			r = x.isEmpty();
		}
		else {
			// this is the next value and we need to count any more
			doCount (c, v, level);
		}
	}
	else if (level == 0) {
		// at the lowest level the seed is a single '1'
		c = '1';
		x.done = true;
		r = true;
	}
	else if (x.isEmpty()) {
		// nothing prepared, must be first time get character from lower level
		char t;
		x.done = nextItem(t, level - 1);
		doCount (c, t, level);
	}
	x.count++;
	if (r) {
		fprintf (stdout, "%3d: %llu digits\n", level + 1, x.count);
	}
	return r;
}

int main(int argc, char *argv[]) {
	char t;
	
	while (!nextItem(t, 99)) {
		;
	}
}
</blockquote>

GeneralRe: Coding Challenge - ANSWER Pin
Dave Kreskowiak4-Dec-17 4:15
mveDave Kreskowiak4-Dec-17 4:15 
GeneralRe: Coding Challenge - ANSWER Pin
PeejayAdams5-Dec-17 22:48
PeejayAdams5-Dec-17 22:48 
GeneralRe: Coding Challenge - ANSWER Pin
Paulo_JCG12-Dec-17 4:59
professionalPaulo_JCG12-Dec-17 4:59 
GeneralRe: Coding Challenge - ANSWER Pin
Dave Kreskowiak12-Dec-17 5:24
mveDave Kreskowiak12-Dec-17 5:24 
GeneralRe: Coding Challenge - Morris Sequence Pin
Paulo_JCG12-Dec-17 4:53
professionalPaulo_JCG12-Dec-17 4:53 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 1319430215-Mar-18 5:07
Member 1319430215-Mar-18 5:07 
GeneralRe: Coding Challenge - Morris Sequence Pin
Dave Kreskowiak15-Mar-18 5:53
mveDave Kreskowiak15-Mar-18 5:53 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 1319430215-Mar-18 6:23
Member 1319430215-Mar-18 6:23 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 1319430215-Mar-18 7:05
Member 1319430215-Mar-18 7:05 
GeneralRe: Coding Challenge - Morris Sequence Pin
Dave Kreskowiak15-Mar-18 7:34
mveDave Kreskowiak15-Mar-18 7:34 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 1319430215-Mar-18 8:24
Member 1319430215-Mar-18 8:24 
GeneralThey're getting better. Pin
Jörgen Andersson30-Nov-17 7:58
professionalJörgen Andersson30-Nov-17 7:58 
GeneralRe: They're getting better. Pin
CPallini30-Nov-17 8:14
mveCPallini30-Nov-17 8:14 
GeneralRe: They're getting better. Pin
MarkTJohnson30-Nov-17 8:32
professionalMarkTJohnson30-Nov-17 8:32 
GeneralRe: They're getting better. Pin
ZurdoDev30-Nov-17 8:47
professionalZurdoDev30-Nov-17 8:47 
GeneralRe: They're getting better. Pin
Jeremy Falcon30-Nov-17 10:56
professionalJeremy Falcon30-Nov-17 10:56 
GeneralRe: They're getting better. Pin
Jörgen Andersson30-Nov-17 18:38
professionalJörgen Andersson30-Nov-17 18:38 

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.