Category Archives: C#

Words used in Tumblr’s usernames (380,000 users)

The data is a parse of 380,000 usernames. Link Here

C# was written to:

  • parse an English dictionary for words. Link here
  • parse all tumblr usernames
  • search for each English word with a length of 5 or greater within each username
  • store each English word found, and count its frequency throughout all names
  • Wordle was used to generate a word cloud of the most frequent words.

Most Frequent English Words Found in TUMBLR’s Usernames
TUMBLR_USERNAME_ENGLISHWORDS
Continue reading

C#: Tumblr Blog Description Top Word Frequency List Visualization

Browsing reddit; I found a parsed dataset from Tublr. Link Here.
I used code from an older post to obtain a list of unique words, and their frequencies.
Next, I used a default TermCloud – Sample from GoogleCharts’s Additional Charts Gallery, to generate this image in a web browser.

Tublr blog description: words with a frequency greater than 5,000, ordered by most to least frequent.
FINAL_TUBLR_WORDFREQ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace ordertumblrwords
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = 0;
            string line;
            Dictionary<string, int> hashfreq = new Dictionary<string, int>();
            System.IO.StreamReader file =
               new System.IO.StreamReader("C:\\Book\\New folder\\OUTPUT.txt");
            while ((line = file.ReadLine()) != null)
            {
                string[] parts = line.Split(':');
                int freq = int.Parse(parts[1].ToString().Trim());
                hashfreq.Add(parts[0], freq);
                counter++;
            }
            file.Close();
            int q = 0;
            string masterstring = "";
            foreach (KeyValuePair<string, int> item in hashfreq.OrderByDescending(key => key.Value))
            {
                if (item.Value > 5000)
                {
                    Console.WriteLine("data.setValue(" + q + ", 0, '" + item.Key + "');");
                    Console.WriteLine("data.setValue(" + q + ", 1, "+item.Value+");");
                    q += 1;
                    masterstring += "data.setValue(" + q + ", 0, '" + item.Key + "');\r\n";
                    masterstring += "data.setValue(" + q + ", 1, " + item.Value + ");\r\n";
                }
 
            }
            StreamWriter streamWrite;
            streamWrite = File.AppendText("C:\\Book\\MANTASMAIN.txt");
            streamWrite.WriteLine(masterstring);
            streamWrite.Close();
        }
    }
}

C#: Euler 46

EulerLEVEL2XXDD

Goldbach’s other conjecture

https://projecteuler.net/problem=46
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

static void Main(string[] args)
{
    Console.BufferHeight = 8000;
    Console.WriteLine("Goldbach's other conjecture");
 
    for (int n = 3; n < 100000; n+=2)
    {
        if ( !isPrime(n) )
        {
            Console.WriteLine(n);
            bool works = false;
            while (!works)
            {
                for (int abc = 1; abc < 50; abc++)
                {
                    for (int j = 0; j < 10000; j++)
                    {
                        if (isPrime(j))
                        {
                            double check = j + (2 * Math.Pow(abc, 2));
                            if ((j + 2 * Math.Pow(abc,2)) > n)
                                break;
                            if (n == check)
                            {
//Console.WriteLine(n + " = " + j + " + 2*" + abc + "^2");
                                works = true;
                            }
                            if (works)
                                break;
                        }
                    }
                }
            }
            Console.WriteLine("----");
        }
    }
}
 
public static bool isPrime(int n)
{
    if (n == 1)
        return false;
    if (n == 2)
        return true;
    for (int i = 2; i < n; ++i)
    {
        if ((n % i) == 0)
            return false;
    }
    return true;
}

output
output

C#: Euler 36

https://projecteuler.net/problem=36

The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.
Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

//Euler 36
static void Main(string[] args)
{
    int num = 0;
    int sum = 0;
    while (num &lt; 1000000)
    {
        num += 1;
        string sBits = Convert.ToString(num, 2);
        string sNum = num.ToString();
        if (isSym(sNum) &amp;&amp; isSym(sBits))
        {
            sum += num;
        }
    }
    Console.WriteLine("Answer : "+sum);
}
 
public static string Reverse(string s)
{
    char[] charArray = s.ToCharArray();
    Array.Reverse(charArray);
    return new string(charArray);
}
 
public static bool isSym(string n)
{
    double half = n.Length / 2;
    string a  = n.ToString();
    string b  = n.ToString();
    b = Reverse(b);
    string new_a = "";
    string new_b = "";
    for (int i = 0; i &lt; half; i++)
    {
        new_a += a[i];
        new_b += b[i];
    }
    if (new_a == new_b)
        return true;
    return false;
}

Visualizing Project Euler Problem 15 using C# Winforms | Lattice Paths

Mapping paths..

public static Brush aBrush = (Brush)Brushes.Black;
public static string path_collect = "";
public static LinkedList<string> complete_paths = new LinkedList<string>();
public bool wall = false;
public bool floor = false;
 
public static int miliseconds = 15;
 
public int current_x = 10;
public int current_y = 10;
 
 
public Form1()
{
    InitializeComponent();
}
 
 
private void Form1_Load(object sender, EventArgs e)
{
    label3.Text = miliseconds + "ms";
}
 
private void Form1_Paint(object sender, PaintEventArgs e)
{
    int x = 10;
    int y = 10;
    for (int i = 1; i < 21; i++)
    {
        for (int j = 1; j < 21; j++)
        {
            e.Graphics.DrawEllipse(Pens.Black, x* i , y *j, 6, 6);
        }
    }
}
 
private void bntPath_Click(object sender, EventArgs e)
{
    bntPath.Enabled = false;
    backgroundWorker1.RunWorkerAsync();
}
 
 
protected void Move(string path)
{
    Graphics g = this.CreateGraphics();
 
    foreach (char c in path)
    {
        if (c == '0')
        {
            g.FillRectangle(aBrush, current_x + 10, current_y, 10, 10);
            current_x += 10;
            path_collect += "→";
        }
        else if (c == '1')
        {
            g.FillRectangle(aBrush, current_x, current_y + 10, 10, 10);
            current_y += 10;
            path_collect += "↓";
        }
    }
 
    current_x = 10;
    current_y = 10;
 
    label2.Text = "New Unique Path\r\n" + path_collect;
 
    Brush NewBrush = new SolidBrush(GetRandomColor());
    aBrush = NewBrush;
    path_collect = "";
 
}
 
 
private void backgroundWorker1_DoWork_1(object sender, DoWorkEventArgs e)
{
    /*
    while (true) 
    {
        MoveRandom();
        Thread.Sleep(miliseconds);
    }
        */
    Int64 i = 0;
    Int64 count_unique_paths = 0;
 
 
    while (true)
    {
        string bits = Convert.ToString(i, 2).PadLeft(38, '0');
        string check = bits;
        check = Regex.Replace(check, "0", "");
        if (check.Length == 19)
        {
            Move(bits);
            //Thread.Sleep(50);
            count_unique_paths += 1;
            label4.Text = "Distinct Paths " + count_unique_paths;
        }
        i += 1;
    }
 
}
 
string row = "";
 
public bool completerow = true;
 
private Random random;
private Color GetRandomColor()
{
    random = new Random();
    return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
}
 
protected void MoveRandom()
{
    Graphics g = this.CreateGraphics();
    Random rand = new Random();
    int random = rand.Next(0, 2);
 
    if (random == 0)
    {
        if (!wall)
        {
            g.FillRectangle(aBrush, current_x + 10, current_y, 10, 10);
            current_x += 10;
            path_collect += "→";
        }
    }
    else if (random == 1)
    {
        if (!floor)
        {
            g.FillRectangle(aBrush, current_x, current_y + 10, 10, 10);
            current_y += 10;
            path_collect += "↓";
        }
    }
    CheckWall();
}
 
protected void CheckWall()
{
    if (current_x == 200)
        wall = true;
    if (current_y == 200)
        floor = true;
    if (wall && floor)
    {
        current_x = 10;
        current_y = 10;
        wall = false;
        floor = false;
        if (!complete_paths.Contains(path_collect))
        {
            complete_paths.AddLast(path_collect);
            label1.Text = "Complete " + complete_paths.Count;
            label2.Text = "New Unique Path\r\n" + path_collect;
 
        }
        Brush NewBrush = new SolidBrush(GetRandomColor());
        aBrush = NewBrush;
        path_collect = "";
    }
}

//Some fun with random paths at different speeds.
speed 50-1ms

Chicago Gov-Job Average Salary Bubble Charts.

According to this data : Current Employee Names, Salaries, and Position Titles on City Of Chicago Data Portal https://data.cityofchicago.org/;

The Chicago government employees 32,160 employees, and pays approximately $2,429,980,941.36 in annual salaries.

Google Charts API was used.

CLICK ON Pictures FOR Focus. (temporary courtesy of imgur)

All Employees.

image1

Let’s remove distinct Position-Titles with Less Then 10 Employees, and the position title of Police Officer from the data.

image2

(20 or more), and, (200 or less); employee counts within a position title.

image3

(10 or more), and,( 50 or less); employee counts within a position title.

FINALBIGCHART

C#

int counter = 0;
string line;
Dictionary&lt;string, int&gt; dCount = new Dictionary&lt;string, int&gt;();
Dictionary&lt;string, decimal&gt; dSalTotal = new Dictionary&lt;string, decimal&gt;();
Dictionary&lt;string, LinkedList&gt; dDeps = new Dictionary&lt;string, LinkedList&gt;();
System.IO.StreamReader file =
    new System.IO.StreamReader("C:\\AAAChicagoEmployees\\Current_Employee_Names__Salaries__and_Position_Titles (1).csv");
while ((line = file.ReadLine()) != null)
{
    string[] parts = line.Split(',');
    try
    {
        string name = parts[2];
        name = Regex.Replace(name, "'", "");
        string dep = parts[3];
        decimal sal = decimal.Parse(parts[4].TrimStart('$')   );
        if ( dCount.ContainsKey(name  ) )
        {
            int a = dCount[name];
            a += 1;
            dCount[name] = a;
            decimal b = dSalTotal[name];
            b += sal;
            dSalTotal[name] = b;
            LinkedList c = dDeps[name];
            c.AddLast(dep);
            dDeps[name] = c;
        }
        else
        {
            dCount.Add(name, 1);
            dSalTotal.Add(name, sal);
            LinkedList newDepList = new LinkedList();
            newDepList.AddLast(dep);
            dDeps.Add(name, newDepList);
        }
    }
    catch (Exception x)
    {
        //header line
    }
    counter++;
}
file.Close();
Dictionary&lt;string, decimal&gt; dAveOrder = new Dictionary&lt;string, decimal&gt;();
foreach (KeyValuePair&lt;string, int&gt; item in dCount.OrderBy(key =&gt; key.Value))
{
    if (item.Value &gt;= 10 &amp;&amp; item.Value &lt;= 50)
    {
        decimal aveSal = dSalTotal[item.Key] / item.Value;
        dAveOrder.Add(item.Key, aveSal);
    }
}
string json = "";
foreach (KeyValuePair&lt;string, decimal&gt; item in dAveOrder.OrderBy(key =&gt; key.Value))
{
    Hashtable frequencyHash = new Hashtable();
    LinkedList uniqueList = new LinkedList();
    foreach (var element in dDeps[item.Key])
    {
        if (uniqueList.Contains(element))
        {
            int elementCount = int.Parse(frequencyHash[element].ToString());
            elementCount++;
            frequencyHash[element] = elementCount;
        }
        else
        {
            uniqueList.AddLast(element);
            frequencyHash.Add(element, 1);
        }
    }
    int max = 0;
    string sMaxDep = "";
    foreach (string element in frequencyHash.Keys)
    {
        int iDep = int.Parse(frequencyHash[element].ToString());
        if (iDep &gt; max)
        {
            max = iDep;
            sMaxDep = element;
        }
    }
    if (item.Key == "POLICE OFFICER")
    { }
    else
    {
        json += "[ '" + item.Key + "', " + dCount[item.Key] + "  , " + item.Value + " ,'" + Regex.Replace(sMaxDep, "'", "") + "' , " + item.Value + " ],\r\n";
    }
}
System.IO.StreamWriter fileWrite = new System.IO.StreamWriter("c:\\AAAChicagoEmployees\\data.txt");
fileWrite.WriteLine(json);
fileWrite.Close();

DataViz: Visualizing Chicago Narcotics Crime Data.

In this post, I will give a semi-detailed overview of how to make an animated heat map over a time range.  This is my first attempt at making an animated heat map.

I will be revisiting Chicago crime statistics from City of Chicago Data Portal 2001-2013

This dataset reflects reported incidents of crime (with the exception of murders where data exists for each victim) that occurred in the City of Chicago from 2001 to present, minus the most recent seven days.Data is extracted from the Chicago Police Department’s CLEAR (Citizen Law Enforcement Analysis and Reporting) system. Read More about the dataset here

On 11/28/2013 I downloaded a csv from chicago’s data portal named Crimes_-_2001_to_present.csv, its size was 1,050,688 KB

Here is an example of how I would parse out all coordinates of narcotics violations for the month of January in 2013.
rubyspecific

Lets test the coordinate outputs by appending them into a google maps heatmap.
SingleMonthTest

Okay, now I’m going to manually make one of these images (with static center and size) for each month from January 2012 to October 2013. Next, I wrote a quick c# application using ImageMagick to programmatically crop each Browser screenshot.

string[] files = Directory.GetFiles(@"c:\__FULLCHICAGOCRIME\Months\ScreenShots");
foreach (string file in files)
{
    string ext = Path.GetExtension(file);
    if (ext == ".png")
    {
        string filenameNoExt = Path.GetFileNameWithoutExtension(file);
        string filenameExt = Path.GetFileName(file);
        Directory.CreateDirectory(@"c:\ImageConversion\" + filenameNoExt);
        Process imProcess = new Process();
        string im_command =  file + @" -crop 800x800+500+300 c:\__FULLCHICAGOCRIME\Months\ScreenShots\cropped\" + filenameNoExt + "_cropped.png";
        imProcess.StartInfo.UseShellExecute = false;
        imProcess.StartInfo.RedirectStandardOutput = true;
        imProcess.StartInfo.FileName = @"c:\__FULLCHICAGOCRIME\Months\ScreenShots\convert";
        imProcess.StartInfo.Arguments = im_command;
        imProcess.Start();
        imProcess.WaitForExit();
    }
}

Now I have the frames for my animation.
croppedimages

Lastly, I append all these images into a movie using Windows Movie Maker, and add some Beethoven as background music.

C#: Programmatically Playing Prince of Persia.

In this post I will programmatically pass level 1 in Prince of Persia using c#.

Prince of Persia
This game came out in 1989, it is now freeware.

In order to run MS DOS Prince of Persia(In 2013 win7 era), I had to download DOSBOX.
Dos Box Website: http://www.dosbox.com/

I used this input simulator in my application:
Windows input Simulator http://inputsimulator.codeplex.com/.

Here is a fun video I uploaded to youtube:

C# Code used.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using WindowsInput;
namespace Prince
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool AllocConsole();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            AllocConsole();
        }
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName,
            string lpWindowName);
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        private void button1_Click(object sender, EventArgs e)
        {
            Process[] localAll = Process.GetProcesses();
            IntPtr calculatorHandle = FindWindow(null, "DOSBox 0.74, Cpu speed:     3000 cycles, Frameskip  0, Program:   PRINCE");
             if (calculatorHandle == IntPtr.Zero)
                 return;
             SetForegroundWindow(calculatorHandle);
             Thread.Sleep(500);
             if (SetForegroundWindow(calculatorHandle))
             {
                    commandPrince(VirtualKeyCode.NUMPAD6, 3700);
                    commandPrince(VirtualKeyCode.SHIFT, 2000);
                    commandPrince(VirtualKeyCode.NUMPAD4, 4200);
                    Thread.Sleep(500);
                    commandPrince(VirtualKeyCode.NUMPAD8, 2000);
                    Thread.Sleep(1400);
 
                    //....  This ^ is a small sample of commands I give to Prince
                    //....  Full level 1 commands are below in the post.
 
             } 
        }
        public void standingJumpForw(VirtualKeyCode key, VirtualKeyCode key2, int ms)
        {
            Console.WriteLine("Standing Forward Jump");
            InputSimulator.SimulateKeyDown(key);
            Thread.Sleep(100);
            InputSimulator.SimulateKeyDown(key2);
            Thread.Sleep(ms);
            InputSimulator.SimulateKeyUp(key2);
            InputSimulator.SimulateKeyUp(key);
            Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " up ");
        }
        public void commandClibUp(VirtualKeyCode key, VirtualKeyCode key2, int ms)
        {
            Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " down ");
            InputSimulator.SimulateKeyDown(key);
            Thread.Sleep(ms);
            InputSimulator.SimulateKeyDown(key2);
            Thread.Sleep(500);
            InputSimulator.SimulateKeyUp(key2);
            InputSimulator.SimulateKeyUp(key);
            Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " up ");
        }
        public void commandPrince(VirtualKeyCode key, int ms)
        {
            Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " down ");
            InputSimulator.SimulateKeyDown(key);
            Thread.Sleep(ms);
            InputSimulator.SimulateKeyUp(key);
            Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " up ");
        }
 
        public void runningJump(VirtualKeyCode key, VirtualKeyCode key2, int ms)
        {
            Console.WriteLine("Running Jump ");
            InputSimulator.SimulateKeyDown(key);
            Thread.Sleep(ms);
            InputSimulator.SimulateKeyDown(key2);
            Thread.Sleep(550);
            InputSimulator.SimulateKeyUp(key2);
            InputSimulator.SimulateKeyUp(key);
        }
        public void runningJumpFast(VirtualKeyCode key, VirtualKeyCode key2, int ms)
        {
            Console.WriteLine("Running Jump Fast");
            InputSimulator.SimulateKeyDown(key);
            Thread.Sleep(ms);
            InputSimulator.SimulateKeyDown(key2);
            Thread.Sleep(550);
            InputSimulator.SimulateKeyUp(key2);
            InputSimulator.SimulateKeyUp(key);
        }
    }
}

Continue reading

C#: Parsing Horses.

Horserace

My goal with this post is to parse out all the horses which came in 1st to 3rd place from each race that took place at Arlington Park this season.

The website http://www.arlingtonpark.com/racing-handicapping/equibase/charts provides a calendar which links to the result file for each race date from the past. Clicking on a particular date will link you to a pdf which contains various statistics.

calendar

So lets download all of these programmatically. To achieve this, I wrote a small program to iterate over dates in the uri, requesting every race date pdf file.

Then, I save them into a folder on my c: drive called \HORSEDATA\. I also, wait 1 -3 second before each request, alternate the web client’s header, and delete the downloaded file if it is too small (No Race happened on that date).

DateTime Today = DateTime.Now;
int randomWait = 0;
Random random = new Random();
for (int i = 0; i &lt; 200; i++)
{
    randomWait = random.Next(1000, 3000);
    Thread.Sleep(randomWait);
    Console.WriteLine("Downloading : " + Today.ToString("d"));
    Today =  Today.AddDays( -1 );
    try
    {
        using (WebClient client = new WebClient())
        {
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.370"+i+";)");
client.DownloadFile("http://www.equibase.com/premium/eqbPDFChartPlus.cfm?RACE=A&amp;BorP=P&amp;TID=AP&amp;CTRY=USA&amp;DT=" 
    + Today.ToString("d") + "&amp;DAY=D&amp;STYLE=EQB", @"C:\HORSEDATA\\" + Today.ToString("MM-dd-yy") + ".pdf");
        }
    }
    catch (Exception x)
    {Console.WriteLine("No Race on this Date.");}
    FileInfo f = new FileInfo(@"C:\HORSEDATA\\" + Today.ToString("MM-dd-yy") + ".pdf");
    long s1 = f.Length;
    Console.WriteLine(s1.ToString());
    if (s1 &lt; 4000)
        File.Delete(@"C:\HORSEDATA\\" + Today.ToString("MM-dd-yy") + ".pdf");
}

Now your \HORSEDATA\ folder should contain the proper pdfs.
horsedatapdf

Next, read through all the race date PDFs. For each PDF, I want to get the names of horses which came in 1st to 3rd place, and count up the amount of times each unique horse at least showed.

3 horse parse

Using iTextSharp library to read the PDFs and count up my horses.

using iTextSharp;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.pdf.parser;
using System.Text.RegularExpressions;
Console.BufferHeight = 6000;
Dictionary&lt;string, int&gt; dictHorseShowCount = new Dictionary&lt;string, int&gt;();
LinkedList uniqueHorse = new LinkedList();
string[] filePaths = Directory.GetFiles(@"c:\HORSEDATASET\");
foreach (string file in filePaths)
{
    Console.WriteLine("Reading : " + file);
    PdfReader reader = new PdfReader(file); 
    StringWriter output = new StringWriter();  
    for (int i = 1; i &lt;= reader.NumberOfPages; i++)
        output.WriteLine(PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy()));
    Regex regPattern = new Regex(@"Total WPS Pool(.*?)Copyright", RegexOptions.Singleline);
    MatchCollection matchX = regPattern.Matches(output.ToString());
    foreach (Match match in matchX)
    {
        Match match2 = Regex.Match(match.ToString(), @"Win Place Show(.*?)Wager Type", RegexOptions.Singleline);
        if (match2.Success)
        {
            string [] lineparts = Regex.Split(match2.ToString(),"\n");
            for( int i = 1; i &lt; lineparts.Length - 1; i++ )
            {
                string horseOnly = Regex.Replace(lineparts[i].ToString(), "[^a-zA-Z -]", "");
                if (!uniqueHorse.Contains(horseOnly.Trim()))
                    uniqueHorse.AddLast(horseOnly.Trim());
                if (dictHorseShowCount.ContainsKey(horseOnly.Trim()))
                {
                    int existing_int = dictHorseShowCount[horseOnly.Trim()];
                    existing_int += 1;
                    dictHorseShowCount[horseOnly.Trim()] = existing_int;
                }
                else
                    dictHorseShowCount[horseOnly.Trim()] = 1;
            }
        }
    }
}
var sortedDict = (from entry in dictHorseShowCount orderby entry.Value ascending select entry)
.ToDictionary(pair =&gt; pair.Key, pair =&gt; pair.Value);
foreach (string key in sortedDict.Keys)
    Console.WriteLine(key + "   "   + sortedDict[key]);
Console.WriteLine("");
Console.WriteLine("Distinct Horse Showed : " + uniqueHorse.Count);
Console.WriteLine("");

Output
outputHORSES