Ruby: Solution to Project Euler Problem # 28

http://projecteuler.net/problem=28

Number spiral diagonals

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21 22 23 24 25
20  7  8  9 10
19  6  1  2 11
18  5  4  3 12
17 16 15 14 13

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

Ruby: Solution to Project Euler Problem # 25

Recently, I’ve been typing more Ruby then usual. In Ruby you can have a fairly big number.
http://projecteuler.net/problem=25

1000-digit Fibonacci number

The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.

The 12th term, F12, is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?

C#: Parse a Sentence Containing a Word from Text using Regular Expressions.

Recently, I had received an email from someone asking me how to obtain all sentences containing a specific word. So, I made this quick post. The code below shows how to use regular expressions to parse all sentences from text, then check to see if the sentence contains a specific word.

//Look for sentences containing the word "bank"
string word = "bank";
//Text String
string fulltext = @"Starting in the early 1960s federal banking regulators interpreted provisions of the Glass–Steagall Act to permit commercial banks and especially commercial bank affiliates to engage in an expanding list and volume of securities activities. By the time the affiliation restrictions in the Glass–Steagall Act were repealed through the Gramm–Leach–Bliley Act of 1999 (GLBA), many commentators argued Glass–Steagall was already “dead.” Most notably, Citibank’s 1998 affiliation with Salomon Smith Barney, one of the largest US securities firms, was permitted under the Federal Reserve Board’s then existing interpretation of the Glass–Steagall Act. President Bill Clinton publicly declared ""the Glass–Steagall law is no longer appropriate."" Many commentators have stated that the GLBA’s repeal of the affiliation restrictions of the Glass–Steagall Act was an important cause of the late-2000s financial crisis.  Some critics of that repeal argue it permitted Wall Street investment banking firms to gamble with their depositors' money that was held in affiliated commercial banks. Others have argued that the activities linked to the financial crisis were not prohibited (or, in most cases, even regulated) by the Glass–Steagall Act. Commentators, including former President Clinton in 2008 and the American Bankers Association in January 2010, have also argued that the ability of commercial banking firms to acquire securities firms (and of securities firms to convert into bank holding companies) helped mitigate the financial crisis.";
 
//Match Collection for every sentence
MatchCollection matchSentences = 
    Regex.Matches(fulltext, @"([A-Z][^\.!?]*[\.!?])");
//Alternative pattern :  @"(\S.+?[.!?])(?=\s+|$)"
 
//counter for sentences.
int foundSentenceWithWord = 0;
foreach (Match sFound in matchSentences)
{
    foreach (Capture capture in sFound.Captures)
    {
        string current_sentence = capture.Value;
        //if you don't want to match for words like 'bank'er  or  'bank'ing
        //use the word boundary "\b"
        //change this pattern to   @"\b"+word+@"\b"
        Match matchWordInSentence = 
            Regex.Match(capture.Value, word, RegexOptions.IgnoreCase);
        if (matchWordInSentence.Success)
        {
            Console.WriteLine("Sentence Found Containing '" + word+"' :");
            Console.WriteLine(current_sentence); Console.WriteLine();
            foundSentenceWithWord++;
        }
    }
}
Console.WriteLine();
Console.WriteLine("Found " + foundSentenceWithWord 
    + " Sentences Containing the word '" + word + "'");
Console.WriteLine();

Output:

C#: Split GIF frames into PNGs using ImageMagick

How to split apart a GIF image frame by frame, and save each frame as PNG into a sub-folder.

To achieve this I will be using ImageMagick
http://www.imagemagick.org/

Download, Install and locate an executable named convert.exe

I created a folder on my C: Drive called
/ImageConversion/

Place your GIFs into the ImageConversion Folder, also copy convert.exe into there as well. So the folder should look like this:

Execute this code:

//get all files in folder
string[] files = Directory.GetFiles(@"c:\ImageConversion\");
 
//loop through each file
foreach (string file in files)
{
    //check if .GIF
    string ext = Path.GetExtension(file);
    if (ext == ".gif")
    {
        Console.WriteLine("Splitting : "+file);
        //Get the name of the file without extension
        string filenameNoExt = Path.GetFileNameWithoutExtension(file);
        //Get the file name with extention
        string filenameExt = Path.GetFileName(file);
        Console.WriteLine();
        //Create a Sub Directory with the same as the GIF's filename
        Directory.CreateDirectory(@"c:\ImageConversion\"+filenameNoExt);
        Process imProcess = new Process();
        //Arguments
        string im_command = @"c:\ImageConversion\"
        + filenameExt + @" -scene 1 +adjoin -coalesce c:\ImageConversion\" + filenameNoExt + @"\" + filenameNoExt + ".png";
        imProcess.StartInfo.UseShellExecute = false;
        imProcess.StartInfo.RedirectStandardOutput = true;
        imProcess.StartInfo.FileName = @"c:\ImageConversion\convert";
        imProcess.StartInfo.Arguments = im_command;
        imProcess.Start();
        imProcess.WaitForExit();
    }
}

That’s it, you’re done.
Now the ImageConversion folder should be populated with a folder for every gif containing every frame as an individual png.

C#: Programmatically download all Images from a website and save them locally.

For this example, I will be downloading all the .gifs from a specific page on imgur. Then, I will save them into a folder called _Images.

This is the url to the specific page I will be downloading images from http://imgur.com/a/GPlx4

What the website looks like:

Below is C# code that will:
-use a WebClient.DownloadString() to download the html into a string
-parse out specific image links from the returned html string using regular expressions
-use another WebClient.DownloadFile() to download each image link parsed

WebClient wchtml = new WebClient();
string htmlString = wchtml.DownloadString("http://imgur.com/a/GPlx4");
int mastercount = 0;
Regex regPattern = new Regex(@"http://i.imgur.com/(.*?)alt=""", RegexOptions.Singleline);
MatchCollection matchImageLinks = regPattern.Matches(htmlString );
 
foreach (Match img_match in matchImageLinks)
{
    string imgurl = img_match.Groups[1].Value.ToString();
    Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?",
    RegexOptions.IgnoreCase);
    MatchCollection ms = regx.Matches(imgurl);
    foreach (Match m in ms)
    {
        Console.WriteLine("Downloading..  "  + m.Value);
        mastercount++;
        try
        {
            WebClient wc = new WebClient();
            wc.DownloadFile(m.Value, @"C:\_Images\bg_" + mastercount + ".gif");
            Thread.Sleep(1000);
        }
        catch (Exception x)
        {
            Console.WriteLine("Failed to download image.");
        }
        break;
    }
}

Output: C:\_Images