The code reads my RSS feed and looks at the labels - this should work for any blog..
trying dragging for fun! I'll post the code once I've mad a few more enhancements (and checked my math!)
<script type="text/javascript" src="http://www.figmentengine.com/assets/script/silverlight.js" ></script>
<div id="simplekineticsHost" style="text-align: center;"><object data="data:application/x-silverlight-2," type="application/x-silverlight-2"></object></div>
<script type="text/javascript"> var host = "http://www.figmentengine.com/"; var source = host + "silverlight/kinetics/simplekinetics.xap"; var parentElement = document.getElementById("simplekineticsHost"); var callbackId = "simplekineticsHost2"; var properties = { width: "500", height: "350", version: "2.0.31005.0"}; var events = {}; Silverlight.createObject(source, parentElement, callbackId, properties, events);</script>
/// <summary>
/// Chain implements a markov chain for a type T
/// allows the generation of sequences based on
/// a sample set of T items
/// </summary>
/// <typeparam name="T">the type of elements</typeparam>
public class Chain<T>
{
Link<T> root = new Link<T>(default(T));
int length;
/// <summary>
/// creates a new chain
/// </summary>
/// <param name="input">Sample set</param>
/// <param name="length">window size for sequences</param>
public Chain(IEnumerable<T> input, int length)
{
this.length = length;
root.Process(input, length);
}
/// <summary>
/// generate a new sequence based on the samples first entry
/// </summary>
/// <param name="max">maximum size of result</param>
/// <returns></returns>
public IEnumerable<T> Generate(int max)
{
foreach (Link<T> next in root.Generate(root.SelectRandomLink().Data, length, max))
yield return next.Data;
}
/// <summary>
/// generate a new sequence based on the sample
/// </summary>
/// <param name="start">the item to start with</param>
/// <param name="max">maximum size of result</param>
/// <returns></returns>
public IEnumerable<T> Generate(T start, int max)
{
foreach (Link<T> next in root.Generate(start, length, max))
yield return next.Data;
}
}
/// <summary>
/// parts of a chain (markcov)
/// </summary>
/// <typeparam name="T">link type</typeparam>
internal class Link<T>
{
T data;
int count;
// following links
Dictionary<T, Link<T>> links;
private Link()
{
}
/// <summary>
/// create a new link
/// </summary>
/// <param name="data">value of the item in sequence</param>
internal Link(T data)
{
this.data = data;
this.count = 0;
links = new Dictionary<T, Link<T>>();
}
/// <summary>
/// process the input in window sized chunks
/// </summary>
/// <param name="input">the sample set</param>
/// <param name="length">size of sequence window</param>
public void Process(IEnumerable<T> input, int length)
{
// holds the current window
Queue<T> window = new Queue<T>(length);
// process the input, a window at a time (overlapping)
foreach (T part in input)
{
if (window.Count == length)
window.Dequeue();
window.Enqueue(part);
ProcessWindow(window);
}
}
/// <summary>
/// process the window to construct the chain
/// </summary>
/// <param name="window"></param>
private void ProcessWindow(Queue<T> window)
{
Link<T> link = this;
foreach (T part in window)
link = link.Process(part);
}
/// <summary>
/// process an item following us
/// keep track of how many times
/// we are followed by each item
/// </summary>
/// <param name="part"></param>
/// <returns></returns>
internal Link<T> Process(T part)
{
Link<T> link = Find(part);
// not been followed by this
// item before
if (link == null)
{
link = new Link<T>(part);
links.Add(part, link);
}
link.Seen();
return link;
}
private void Seen()
{
count++;
}
public T Data
{
get
{
return data;
}
}
public int Occurances
{
get
{
return count;
}
}
/// <summary>
/// Total number of incidences after this link
/// </summary>
public int ChildOccurances
{
get
{
// sum all followers occurances
int result = links.Sum(link => link.Value.Occurances);
return result;
}
}
public override string ToString()
{
return String.Format("{0} ({1})", data, count);
}
/// <summary>
/// find a follower of this link
/// </summary>
/// <param name="start">item to be found</param>
/// <returns></returns>
internal Link<T> Find(T follower)
{
Link<T> link = null;
if (links.ContainsKey(follower))
link = links[follower];
return link;
}
static Random rand = new Random();
/// <summary>
/// select a random follower weighted
/// towards followers that followed us
/// more often in the sample set
/// </summary>
/// <returns></returns>
public Link<T> SelectRandomLink()
{
Link<T> link = null;
int universe = this.ChildOccurances;
// select a random probability
int rnd = rand.Next(1, universe+1);
// match the probability by treating
// the followers as bands of probability
int total = 0;
foreach (Link<T> child in links.Values)
{
total += child.Occurances;
if (total >= rnd)
{
link = child;
break;
}
}
return link;
}
/// <summary>
/// find a window of followers that
/// are after this link, returns where
/// the last link if found, or null if
/// this window never occured after this link
/// </summary>
/// <param name="window">the sequence to look for</param>
/// <returns></returns>
private Link<T> Find(Queue<T> window)
{
Link<T> link = this;
foreach (T part in window)
{
link = link.Find(part);
if (link == null)
break;
}
return link;
}
/// <summary>
/// a generated set of followers based
/// on the likelyhood of sequence steps
/// seen in the sample data
/// </summary>
/// <param name="start">a seed value to start the sequence with</param>
/// <param name="length">how bug a window to use for sequence steps</param>
/// <param name="max">maximum size of the set produced</param>
/// <returns></returns>
internal IEnumerable<Link<T>> Generate(T start, int length, int max)
{
var window = new Queue<T>(length);
window.Enqueue(start);
for (Link<T> link = Find(window); link != null && max != 0; link = Find(window), max--)
{
var next = link.SelectRandomLink();
yield return link;
if (window.Count == length-1)
window.Dequeue();
if (next != null)
window.Enqueue(next.Data);
}
}
}
static void Main(string[] args)
{
// sample data set
string seed = Tidy(@"Twinkle, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky!
When the blazing sun is gone,
When he nothing shines upon,
Then you show your little light,
Twinkle, twinkle, all the night.
Then the traveller in the dark,
Thanks you for your tiny spark,
He could not see which way to go,
If you did not twinkle so.
In the dark blue sky you keep,
And often through my curtains peep,
For you never shut your eye,
Till the sun is in the sky.
As your bright and tiny spark,
Lights the traveller in the dark,—
Though I know not what you are,
Twinkle, twinkle, little star.
");
// tokenise the input string
var seedList = new List<string>(Split(seed.ToLower()));
// create a chain with a window size of 4
var chain = new Chain<string>(seedList, 4);
// generate a new sequence using a starting word, and maximum return size
var generated = new List<string>(chain.Generate("twinkle", 2000));
// output the results to the console
generated.ForEach(item => Console.Write("{0}", item));
}
// tokenise a string into words (regex definition of word)
private static IEnumerable<string> Split(string subject)
{
List<string> tokens = new List<string>();
Regex regex = new Regex(@"(\W+)");
tokens.AddRange(regex.Split(subject));
return tokens;
}
twinkle, little star,
how i know not twinkle so.
in the sky.
as your tiny spark,
lights the sky!
when the night.
then the traveller in the dark blue sky you never shut your eye,
till the sky.
as your eye,
till the sun is gone,
when he nothing shines upon,
then you are!
up above the dark,
thanks you did not what you are,
twinkle, twinkle, all the sky!
static void Main(string[] args)
{
string subject = "abcdefg";
// test harness, check it handles multiples of size and negatives and zero
for (int amount = -subject.Length * 3; amount < subject.Length * 3; amount++)
{
string result = RotateStringTest(subject, amount);
char[] inplace = subject.ToCharArray();
RotateString(inplace, amount);
if (amount % subject.Length == 0)
Debug.Assert(result.CompareTo(subject) == 0);
Debug.Assert(result.CompareTo(new String(inplace)) == 0);
}
}
/// <summary>
/// Rotates a string
/// </summary>
/// <param name="subject">string to rotate</param>
/// <param name="amount">< 0 left, 0 none, > 0 right</param>
/// <returns></returns>
private static void RotateString(char[] subject, int amount)
{
int length = subject.Length;
// negate rotations can be re-expressed as positive amounts
if (amount < 0)
amount = length - (-amount % length);
// 0 rotation mean return unchanged
if (amount % length == 0)
return;
int dest = amount;
char prev = subject[0];
do
{
dest %= length;
Swap(ref subject[dest], ref prev);
dest += amount;
} while (dest != amount);
}
/// <summary>
/// swaps two chars wihout using temp variable
/// </summary>
private static void Swap(ref char a, ref char b)
{
a ^= b;
b ^= a;
a ^= b;
}
/// <summary>
/// Rotates a string test (slow)
/// </summary>
/// <param name="subject">string to rotate</param>
/// <param name="amount">< 0 left, 0 none, > 0 right</param>
/// <returns></returns>
private static string RotateStringTest(string subject, int amount)
{
char[] result = subject.ToCharArray();
int length = subject.Length;
// negate rotations can be re-expressed as positive amounts
if (amount < 0)
amount = length - (-amount % length);
// 0 rotation mean return unchanged
if (amount % length != 0)
{
for (int pos = 0; pos < length; pos++)
{
int dest = (pos + amount) % length;
result[dest] = subject[pos];
}
}
return new String(result);
}
The code also allows the user to visualize the calculations taking place by checking/unchecking the checkbox in the top left hand corner. Checking this shows the focal point, the lines of sight from each eye and the key values at the top (angle, distance, start point, end point)
There are a number of implementations of eye movement on the web, however a lot of them do not proportionally move the pupil away from the centre, this means the pupil in these animations are generally stuck at the edge of the eye. If you play with the application you will see that a proportional movement is more realistic - human brains are very good at calculating what other people are looking at based on the pupil positions.
<UserControl x:Class="Eyes.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Canvas x:Name="LayoutRoot" Background="White" MouseMove="LayoutRoot_MouseMove" Cursor="Arrow">
<TextBlock Name="TextDebug" Canvas.Left="10" Canvas.Top="0" Width="1000" Height="100" />
<CheckBox Name="ShowCalculation" Canvas.Left="0" Canvas.Top="0"
Checked="CheckBox_Changed" Unchecked="CheckBox_Changed" Canvas.ZIndex="7"
Cursor="Arrow"/>
<Ellipse Canvas.Left="300" Canvas.Top="100" Width="230" Height="300" Stroke="Black" Name="Face" Fill="WhiteSmoke" Canvas.ZIndex="1"/>
<Canvas Canvas.Left="340" Canvas.Top="200" Canvas.ZIndex="2" >
<Ellipse Canvas.Left="0" Canvas.Top="0" Width="60" Height="40" Stroke="Black" Name="LeftEye" Fill="White" Canvas.ZIndex="2" />
<Ellipse Canvas.Left="10" Canvas.Top="10" Width="20" Height="20" Fill="Green" Name="LeftPupil" Canvas.ZIndex="2" />
<Canvas.Clip>
<EllipseGeometry Center="30,20" RadiusX="30" RadiusY="20" />
</Canvas.Clip>
</Canvas>
<Canvas Canvas.Left="420" Canvas.Top="200" Canvas.ZIndex="2" >
<Ellipse Canvas.Left="0" Canvas.Top="0" Width="60" Height="40" Stroke="Black" Name="RightEye" Fill="White" Canvas.ZIndex="2" />
<Ellipse Canvas.Left="10" Canvas.Top="10" Width="20" Height="20" Fill="Green" Name="RightPupil" Canvas.ZIndex="2" />
<Canvas.Clip>
<EllipseGeometry Center="30,20" RadiusX="30" RadiusY="20" />
</Canvas.Clip>
</Canvas>
<Ellipse Canvas.Left="395" Canvas.Top="270" Width="30" Height="40" Stroke="Black" Name="Nose" Fill="DarkGray" Canvas.ZIndex="1"/>
<Ellipse Canvas.Left="380" Canvas.Top="320" Width="60" Height="30" Stroke="Black" Name="Mouth" Fill="DarkGray" Canvas.ZIndex="1"/>
<Ellipse Canvas.Left="10" Canvas.Top="60" Width="10" Height="10" Fill="Red" Name="FocusPoint" Canvas.ZIndex="6" />
<Line Stroke="Red" Name="LineSightLeft" Canvas.ZIndex="5" StrokeDashArray="2 5" />
<Line Stroke="Red" Name="LineSightRight" Canvas.ZIndex="5" StrokeDashArray="2 5" />
</Canvas>
</UserControl>
public partial class Page : UserControl
{
bool showCalcs;
public Page()
{
InitializeComponent();
// default to showing calculations
ShowCalculation.IsChecked = this.showCalcs = true;
}
private void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
{
// update UI based on cursor location
Point cursorPoint = e.GetPosition(LayoutRoot);
UpdateUI(cursorPoint);
}
private void UpdateUI(Point cursorPoint)
{
// calculate the location of the focus based using the center of the circle
Point centerPoint = new Point(cursorPoint.X, cursorPoint.Y);
if (this.showCalcs)
FocusPoint.PointSet(new Point(cursorPoint.X - (FocusPoint.Width / 2), cursorPoint.Y - (FocusPoint.Height / 2)));
// focus eyes on point
FocusEye(centerPoint, LeftEye, LeftPupil, LineSightLeft);
FocusEye(centerPoint, RightEye, RightPupil, LineSightRight);
}
private void FocusEye(Point pointFocus, Ellipse eye, Ellipse pupil, Line sight)
{
// the centre of the eye needs to be converted to a global coordinate
// which it's parent has
Point pointStart = new Point(eye.Parent.PointGet().X + (eye.Width / 2), eye.Parent.PointGet().Y + (eye.Height / 2));
Point pointEnd = pointFocus;
// cartesian to polar conversion
double angle = Trigonometry.CalculateAngleInDegreesBetween(pointStart, pointEnd);
double distance = Trigonometry.CalculateDistanceBetween(pointStart, pointEnd);
if (this.showCalcs)
TextDebug.Text = String.Format(" {2:N1}deg {3:N1} {0} {1}", new object[] { pointStart, pointEnd, angle, distance });
// maximum focal distance
const double MAX_DISTANCE = 500d;
// the eye will move away from the center
// of the eye ball proportionally to its maxim view
// reduced by π to keep some of it visible
double pupilDistance = (eye.Width / Math.PI) * (distance / MAX_DISTANCE);
// polar to cartesian conversion
Point pointPupilOffset = Trigonometry.ConvertPolarToPoint(pupilDistance, angle);
// adjust for centre of eyeball and pupil
Point pupilLocation = new Point(
(eye.Width / 2) + pointPupilOffset.X - (pupil.Width / 2),
(eye.Height / 2) - pointPupilOffset.Y - (pupil.Height / 2));
pupil.PointSet(pupilLocation);
if (this.showCalcs)
{
sight.X1 = pointStart.X;
sight.Y1 = pointStart.Y;
sight.X2 = pointEnd.X;
sight.Y2 = pointEnd.Y;
}
}
private void CheckBox_Changed(object sender, RoutedEventArgs e)
{
// show calculations visually
this.showCalcs = ShowCalculation.IsChecked ?? false;
Visibility visibility = (this.showCalcs) ? Visibility.Visible : Visibility.Collapsed;
FocusPoint.Visibility = visibility;
LineSightLeft.Visibility = visibility;
LineSightRight.Visibility = visibility;
TextDebug.Visibility = visibility;
if (this.showCalcs)
LayoutRoot.Cursor = Cursors.None;
else
LayoutRoot.Cursor = Cursors.Arrow;
}
}
public static class XamlExtensions
{
public static Point PointGet(this DependencyObject sobj)
{
Point point = new Point((double)sobj.GetValue(Canvas.LeftProperty), (double)sobj.GetValue(Canvas.TopProperty));
return point;
}
public static void PointSet(this DependencyObject shape, Point newLocation)
{
shape.SetValue(Canvas.LeftProperty, newLocation.X);
shape.SetValue(Canvas.TopProperty, newLocation.Y);
}
public static Point GetCenterPoint(this Shape shape)
{
Point location = shape.PointGet();
Point centerPoint = new Point(location.X + (shape.Width / 2), location.Y + (shape.Height / 2));
return centerPoint;
}
}
public class Trigonometry
{
public static double DegreesToRadians(double angle)
{
return ((angle * Math.PI) / 180f);
}
public static double RadiansToDegrees(double angle)
{
return ((angle * 180) / Math.PI);
}
public static double CalculateAngleInDegreesBetween(Point pointStart, Point pointEnd)
{
// difference between points on Y-Axis
// Silverlight Y axis needs to be inverted of cartesian coordinates
double dy = -(pointEnd.Y - pointStart.Y);
// difference between points on X-Axis
double dx = (pointEnd.X - pointStart.X);
// angle of the vector defined by dy, dx
double angleRadians = Math.Atan2(dy, dx);
// results in (-Ï€,Ï€], which needs mapping to [0,2Ï€) by adding 2Ï€ to negative
if (angleRadians < 0)
angleRadians += 2 * Math.PI;
double angleDegrees = RadiansToDegrees(angleRadians);
return angleDegrees;
}
public static double CalculateDistanceBetween(Point pointStart, Point pointEnd)
{
double dySquared = Math.Pow(pointEnd.Y - pointStart.Y, 2);
double dxSquared = Math.Pow(pointEnd.X - pointStart.X, 2);
double distance = Math.Sqrt(dySquared + dxSquared);
return distance;
}
internal static Point ConvertPolarToPoint(double radius, double angleDegrees)
{
double angleRadians = DegreesToRadians(angleDegrees);
double x = radius * Math.Cos(angleRadians);
double y = radius * Math.Sin(angleRadians);
return new Point(x, y);
}
}
public static class XamlExtensions
{
public static void PointSet(this DependencyObject shape, Point newLocation)
{
shape.SetValue(Canvas.LeftProperty, newLocation.X);
shape.SetValue(Canvas.TopProperty, newLocation.Y);
}
}
shape.PointSet(cursorPoint);
PointSet(shape, cursorPoint);
<UserControl x:Class="SimpleKinetics.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="500" Height="350">
<Grid x:Name="LayoutRoot" Background="White">
<Canvas x:Name="Container">
<Button x:Name="ButtonAdd" Canvas.Top="10" Canvas.Left="8" ClickMode="Release" Content="New balls" Click="ButtonAdd_Click" />
<Rectangle x:Name="Controls" Canvas.Top="0" Canvas.Left="0" Width="75" Height="300">
</Rectangle>
</Canvas>
</Grid>
</UserControl>
public partial class Page : UserControl
{
List<Vector> vectors = new List<Vector>();
const int RADIUS = 5;
Random rand = new Random();
public Page()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 30);
timer.Tick += new EventHandler(OnTick);
timer.Start();
}
private void OnTick(object sender, EventArgs e)
{
List<Vector> deads = new List<Vector>();
foreach (Vector vector in vectors)
{
vector.Transition();
if (vector.IsDead)
deads.Add(vector);
}
deads.ForEach(dead =>
{
vectors.Remove(dead);
Container.Children.Remove(dead.ellipse);
});
}
private void ButtonAdd_Click(object sender, RoutedEventArgs e)
{
for (int explosions = 0; explosions < rand.Next(10)+1; explosions++)
GenerateExplode();
}
private void GenerateExplode()
{
Point point = new Point(rand.Next((int)Width), rand.Next((int)Height));
for (int i = 0; i < 100; i++)
{
Ellipse ellipse = new Ellipse();
ellipse.Width = RADIUS;
ellipse.Height = RADIUS;
Vector vector = new Vector(rand.Next(360), (rand.Next(30) / 10D) + 0.5D, ellipse);
vectors.Add(vector);
// make it a random colour
ellipse.Fill = new SolidColorBrush(
Color.FromArgb(
255,
GetRandomColour(),
GetRandomColour(),
GetRandomColour()));
// add it to the interface
Container.Children.Add(ellipse);
// put it in a random location
vector.Point = point;
}
}
private byte GetRandomColour()
{
return (byte)rand.Next(byte.MaxValue);
}
private Point Offset(Point inpoint, double offset)
{
Point point = new Point(inpoint.X + offset, inpoint.Y + offset);
return point;
}
private static Point GetPoint(DependencyObject sobj)
{
Point point = new Point((double)sobj.GetValue(Canvas.LeftProperty), (double)sobj.GetValue(Canvas.TopProperty));
return point;
}
}
internal class Vector
{
public double direction;
public double magnitude;
public Ellipse ellipse;
public Vector(double direction, double magnitude, Ellipse ellipse)
{
this.direction = direction;
this.magnitude = magnitude;
this.ellipse = ellipse;
}
public Point Point
{
get
{
Point point = new Point((double)ellipse.GetValue(Canvas.LeftProperty), (double)ellipse.GetValue(Canvas.TopProperty));
return point;
}
set
{
ellipse.SetValue(Canvas.LeftProperty, value.X);
ellipse.SetValue(Canvas.TopProperty, value.Y);
}
}
internal void Transition()
{
Point newPoint = CalculateNextPosition();
Point = newPoint;
double fade = ((magnitude * magnitude) / magnitude) / 800d;
ellipse.Fill.Opacity -= fade;
}
public bool IsDead
{
get
{
return !(ellipse.Fill.Opacity > 0);
}
}
private double DegreesToRadians(double angle)
{
return ((angle*Math.PI) / 180f);
}
private double RadiansToDegrees(double angle)
{
return ((angle*180) / Math.PI);
}
private Point CalculateNextPosition()
{
double vx = magnitude * Math.Cos(DegreesToRadians(direction));
double vy = magnitude * Math.Sin(DegreesToRadians(direction));
Point current = this.Point;
double x = current.X + vx;
double y = current.Y + vy;
CheckCollision(vx, vy, x, y);
Point newPoint = new Point(x, y);
return newPoint;
}
private void CheckCollision(double vx, double vy, double x, double y)
{
const int maxX = 500;
const int maxY = 500;
if (x < 0 x > maxX)
vx *= -1;
if (y < 0 y > maxY)
vy *= -1;
magnitude = Math.Sqrt((vx * vx) + (vy * vy));
direction = RadiansToDegrees(Math.Atan2(vy, vx));
}
}
Updated silverlight puzzle to use the release version of silverlight 2.
The only issue with the upgrade was that using "storyBoard.SkipToFill()" does not stop the animation in the new version - this generated an exception:
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.dll
Additional information: Operation is not valid on an active Animation or Storyboard. Root Storyboard must be stopped first.
This exception was caused by trying to resue the same animation, in the beta SkipToFill also stopped the animation. Adding a "storyBoard.Stop()" after the SkipToFill() fixed this. However this caused a side-effect. It appears that in the beta SkipToFill actually updates the DependencyProperty, what does this mean?
In my beta code I relied on using the animation to "move" the visual tile from one position to another. In the release version once the animation was removed (i.e. applied to a different object) the tile would snap back to its original position. My work around this was to set the tiles position before the animation - this would mean that the tile would be at the correct location after the animation (or if the animation was removed).
Enter KING CLAUDIUS, attended
KING CLAUDIUS and POLONIUS
LORD POLONIUS
[Behind] O, ho! do you mark that?QUEEN GERTRUDE
As kill a king!FRANCISCO
Nay, I know not:KING CLAUDIUS
Give you good night.Exit POLONIUS
Will you be honest and fair, your honesty should
admit no discourse to your rest; at night we'll hear a play
Let me be accurst!
None wed the second time I kill my husband dead,
Till the foul crimes done in my life,
That I have found
The observed of all your son's distemper.
QUEEN GERTRUDE
Alas, poor Yorick! I knew him, Horatio: welcome, good Marcellus.
a little ere the mightiest julius fell,
the graves stood tenantless and the suits of solemn black,
nor windy suspiration of forced breath,
no, nor the fruitful river in the sky.
as your bright and tiny spark,
he could not see which way to heaven;
whiles, like a diamond in the roman streets:
as stars with trains of fire and dews of blood,
disasters in the same covenant,
and carriage of the land.
bernardo
i think i hear them. stand, ho! who's there?
enter horatio and marcellus,
the rivals of my watch, bid them make haste.
francisco
i think it was about to speak, when the blazing sun is gone,
when he nothing shines upon,
then you show your little light,
twinkle, twinkle, all the holy vows of heaven
where now it burns, marcellus and bernardo, on their watch,
in the land,
and why such daily cast of brazen cannon,
and foreign mart for implements of war;
why such impress of shipwrights, whose sore task
does not grow alone
in thews and bulk, but, as this temple waxes,
the inward service of the cock.
some say that ever 'gainst that season comes
wherein our saviour's birth is celebrated,
the bird of dawning singeth all night long:
and then, they say, you spirits oft walk in death.
private static bool IsAnAlliteration(string subject)
{
bool result = true;
char lead = default(char);
bool inWord = false;
foreach (char c in subject)
{
char current = char.ToLower(c);
bool isLetter = char.IsLetter(current);
// lead contains first character in string
// all words should start with lead character
if (lead == default(char) && isLetter)
lead = current;
if (!inWord && isLetter && lead != current)
result = false; // start of new word, but does not start with lead char
else if (!inWord && isLetter && lead == current)
inWord = true; // start of new word starts with lead char
else if (inWord && char.IsWhiteSpace(current))
inWord = false; // end of word found
if (!result)
break;
}
// did we find at least one word
result &= (lead != default(char));
return result;
}
Fibonacci resistance leads us to believe that the golden ratio is shining, but there is a risk of oscillation as the Ichimoku cloud could be coming in Elliott waves.
Stick that in you markov chain and smoke it...
Finance a science? methinks it looks like art.
class Permutation<T>
{
IEnumerable<T> set;
private Permutation()
{
}
public Permutation(IEnumerable<T> set)
{
this.set = set;
}
public IEnumerable<IEnumerable<T>> Mutations
{
get
{
int length = set.Count();
int factorialK = (int)Factorial(length);
for (int k = 0; k < factorialK; k++)
yield return GeneratePermutation(k, set, length);
}
}
private static IEnumerable<T> GeneratePermutation(long k, IEnumerable<T> set, int length)
{
T[] result = set.ToArray();
for (int j = 2; j <= length; j++)
{
k = k / (j - 1);
int dest = (int)(k % j);
int source = j - 1;
if (source != dest)
Swap(ref result[dest], ref result[source]);
}
return result;
}
private static void Swap(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}
private static long Factorial(int n)
{
long result = 1L;
for (int integer = 2; integer <= n; integer++)
result *= integer;
return result;
}
}
static void Main(string[] args)
{
string word = "ape";
List<string> anagrams = GenerateAnagrams(word);
anagrams.Sort();
Console.WriteLine("{0} anagrams", anagrams.Count);
anagrams.ForEach(anagram => Console.WriteLine(anagram));
}
private static List<string> GenerateAnagrams(string word)
{
List<string> sets = new List<string>();
Permutation<char> perm = new Permutation<char>(word);
foreach (char[] combo in perm.Mutations)
sets.Add(combo.ToString());
return sets;
}