Mozy Contest Answers

Steve smorrey at gmail.com
Sat Nov 4 18:28:05 MST 2006


Well due to a connectivity issue I only got to do the first question.
I did it in c++ and it basically looked like this (I've been working
with it a little to clean it up since then)

#include <iostream>
#include <string>
using namespace std;

int main ()
{
	string str;
	char charopen = '(';
	char charclose = ')';

	while(getline(cin,str))
	{
	
		if(str.empty())
		{
			return 0;
		}

		int counter;
		int c_open = 0;
		int c_close = 0;
		int depth = 0;
		counter = str.length();
	
		for(int x = 0; x <= counter; x++)
		{

			if(str[x] == charopen)
			{
				c_open++;
				depth++;
				if(str[x+1] == charclose)
				{
					depth--;
				}
			}

			if(str[x] == charclose)
			{
				c_close++;
			}
		}
	
		if(c_close != c_open)
		{
			cout << 0 << endl;
		}
		else
		{
			cout << depth+1 << endl;
		}
	}
}

On 11/4/06, Jacob Fugal <lukfugl at gmail.com> wrote:
> Round 1 answers in Ruby:
>
> 1. Lost in Stupid Parentheses
> ---------------------------------------
>
> ARGF.each do |line|
>   chars = line.chomp.split(//)
>   max_depth = 0
>   depth = 0
>   begin
>     chars.each do |c|
>       case c
>       when '(': depth += 1
>       when ')': depth -= 1
>       else raise "invalid character"
>       end
>       raise "unmatched" if depth < 0
>       max_depth = depth if max_depth < depth
>     end
>     raise "unmatched" if depth != 0
>   rescue
>     max_depth = 0
>   end
>   puts max_depth
> end
>
> 2. Multiples of Seven
> ---------------------------------------
>
> ARGF.each do |line|
>   n = line.chomp.to_i
>   i = (n / 7) * 7
>   while i >= 0
>     if (i.to_s.reverse.to_i % 7).zero?
>       puts i
>     end
>     i -= 7
>   end
> end
>
> 3. Sum Numbers Only Please
> ---------------------------------------
>
> sum = 0
> ARGF.each do |line|
>   chars = line.chomp.split(//)
>   chars.each do |c|
>     next unless c =~ /\d/
>     sum += c.to_i
>   end
> end
> puts sum
>
> 4. Mozy Words
> ---------------------------------------
>
> puts ARGF.select{ |line| line =~ /m.*o.*z.*y/ }.size
>
> 5. Socket To Ya'
> ---------------------------------------
>
> require 'socket'
> include Socket::Constants
> socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
> sockaddr = Socket.pack_sockaddr_in( 8787, 'mozy.com' )
> socket.connect( sockaddr )
> results = socket.read
> puts results[13484].chr
>
> 6. NP-Butt-Hard
> ---------------------------------------
>
> ARGF.each do |line|
>   chars = line.chomp.split(//)
>   rot1 = chars.map do |c|
>     case c
>     when /[a-y]/i ; (c[0] + 1).chr
>     when /z/i     ; (c[0] - 25).chr
>     else c
>     end
>   end
>   puts rot1.join('')
> end
>
> /*
> PLUG: http://plug.org, #utah on irc.freenode.net
> Unsubscribe: http://plug.org/mailman/options/plug
> Don't fear the penguin.
> */
>



More information about the PLUG mailing list