Just in Chronicles

Life as a Voyage

Quiz: 3-6-9 Game with Your Programming Language

leave a comment »

There is a very popular game in Korea, called “3-6-9”. Its rule is very simple:

  • A player starts counting from 1.
  • Next player count the number added 1. Repeat this rule until one player fails counting.
  • However, when a player meets a number containing digits such as 3, 6, or 9, the person has to do a clap for the number of times.
  • This is the example of playing:
    • 1, 2, “clap” (3), 4, 5, “clap” (6), 7, 8, “clap” (9), 10, 11, 12, “clap” (13), 14, 15, “clap” (16), 17, 18, “clap” (19), 20, 21, 22, “clap” (23), 24, 25, “clap” (26), 27, 28, “clap”  (29), “clap” (30), “clap” (31), “clap” (32), “clap clap” (33), “clap” (34), “clap” (35), “clap clap” (36), “clap” (37), “clap” (38), “clap clap” (39), 40, 41, 42, “clap” (43), …

The question is…

Use your preferred programming language, create a function or method to perform this game up to 100.

And my solution using C# is… (substituted “clap” with “X”)

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Aliencube.Quizes
{
	public partial class ThreeSixNine
	{
		public static string Execute(int maxNumber = 100)
		{
			List<string> results = new List<string>();
			for (int i = 1; i <= maxNumber; i++)
			{
				int xs = Regex.Matches(Convert.ToString(i), "[369]").Count;
				results.Add(xs == 0 ? Convert.ToString(i) : new String('X', xs));
			}
			return String.Join(" ", results);
		}
	}
}

How’s yours guys?

Advertisements

Written by Justin Yoo

15/04/2012 at 18:36

Posted in For .NET

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s