Monday, April 9, 2012

Simple Roman Numeral Converter

So I was reading the daily wtf, and noticed this article. 

Since I am trying to learn python, I decided to give this little task a quick shot.

The first thing to do here is to go and read about Roman numerals.  Once you do, you will find that the only trick is that if a lower Roman numeral precedes a larger one, it actually acts as a subtract.


#!/usr/bin/env python

import sys

ROMAN_NUMERAL_DICTIONARY = {
                                'I' : 1,
                                'V' : 5,
                                'X' : 10,
                                'L' : 50,
                                'C' : 100,
                                'D' : 500,
                                'M' : 1000
                            }


def IsRomanNumeral(romanNumeralPart):
    return romanNumeralPart in ROMAN_NUMERAL_DICTIONARY

def GetDigitValueOf(romanNumeralPart):
    return ROMAN_NUMERAL_DICTIONARY[romanNumeralPart]


args = sys.argv
if (len(args) < 2):
    print "Please pass an argument to convert."
    sys.exit(0)

romanNumeral = args[1]

decimalNumber = 0
counter = 0

while counter <= (len(romanNumeral) -1):
    currentNumeral = romanNumeral[counter]
    if not IsRomanNumeral(currentNumeral):
        print "Invalid roman numeral... exiting.. "
        sys.exit(0)
    
    currentDigit = GetDigitValueOf(currentNumeral)
    if (counter + 1 >= len(romanNumeral)):
        decimalNumber = decimalNumber + currentDigit
        break
    nextDigit = GetDigitValueOf(romanNumeral[counter + 1])
    if (currentDigit < nextDigit):
        decimalNumber = decimalNumber + nextDigit - currentDigit
        counter = counter + 2
    else:
        decimalNumber = decimalNumber + currentDigit
        counter = counter + 1


print romanNumeral + "=" + str(decimalNumber)



Probably not the best python.. but I'm still learning!

Friday, March 9, 2012

Fedora 16 Tripwire(OS) Installation

Today I spend some time installing Tripwire Open Source.  With the binaries already being present in the Fedora 16 yum repos, it was pretty easy to get set up.

As root:

yum install tripwire

The RPM comes with a default settings already configured.  You can see them if you browse to the /etc/tripwire directory on your system.  There are a couple of steps that you have to follow before you can initialize the database, however.

As per the docs (the man pages have all the info you are looking for) you need to set up both a site and a local key.  The site key is used for encrypting the policy files across multiple systems.  The local key is used for encrypting files used only on the local machine.  The docs state that they one or both of the keys may be required based on what operation is being conducted.  I just set up both keys.  Remember to use strong pass-phrases.  The key locations are configured in the /etc/tripwire/twcfg.txt file, which will later be encrypted for use by the system.


twadmin -m G -v -S /etc/tripwire/site.key -Q passphrase
twadmin -m G -v -L /etc/tripwire/hostname-local.key -P passphrase

Now that you have the keys configured, you can go ahead and encrypt the configuration and policy files.  Tripwire does this so that the files in use by the tripwire system cannot be modified.  If an attacker does get in, technically they can't modify those files.....


twadmin -m F -c /etc/tripwire/tw.cfg -S /etc/tripwire/site.key -Q passphrase /etc/tripwire/twcfg.txt
twadmin -m P -p /etc/tripwire/tw.pol -S /etc/tripwire/site.key -Q passphrase /etc/tripwire/twpol.txt

After that you can run the tripwire database init.


tripwire -m i

After that, you should be able to use tripwire open source.

Saturday, October 29, 2011

Hoppity Solution

I recently stumbled upon the facebook engineering puzzles located here and decided to try and do a few myself.  Hoppity is the first and easiest one, but I thought I'd take a stab at it here.  I'm not really sure what facebook would be looking for in order to get a job interview, but it would be have been cool to be able to view the submissions that got jobs. Please note that I didn't bother with the file reading code, but you can easily add it yourself.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HoppitySolution
{
    class Program
    {
        private const string DIV_3 = "Hoppity";
        private const string DIV_5 = "HopHop";
        private const string DIV_3_AND_5 = "Hop";

        static void Main(string[] args)
        {
            var maximumInt = GetMaximumIntFrom(args);
            Enumerable.Range(1, maximumInt).ToList().ForEach(x => ProcessHop(x));
        }

        private static void ProcessHop(int i)
        {
            if(DivisibleBy3And5(i))
            {
                Console.WriteLine(DIV_3_AND_5);
                return;
            }

            if (DivisibleBy5(i))
            {
                Console.WriteLine(DIV_5);
                return;
            }

            if (DivisibleBy3(i))
            {
                Console.WriteLine(DIV_3);
                return;
            }
        }

        private static bool DivisibleBy5(int i)
        {
            return i % 5 == 0;
        }

        private static bool DivisibleBy3(int i)
        {
            return i % 3 == 0;
        }

        private static bool DivisibleBy3And5(int i)
        {
            return DivisibleBy5(i) && DivisibleBy3(i);
        }

        private static int GetMaximumIntFrom(string[] args)
        {
            // Add File Read Code here.....
            return 15;
        }
    }
}