Tag Archives: ruby

Reflections on Orlando Code Camp and BarCamp Orlando

In the last two weeks I’ve had the great fortune to attend two fantastic conferences here in Orlando.

Both completely free events, and both were fun, inspiring, and eye-opening in their own ways. I came away feeling inspired, invigorated, and with a couple of new nerdy (read: cool) t-shirts!

Orlando Code Camp
Orlando CodeCamp was incredibly educational and informative with it’s tech-heavy, hour-long formal presentations. I can’t imagine all the hard work put in by the speakers and volunteers to have such a large and superior quality event run, and run so smoothly.

There was a definite collegial vibe (taking place at the beautiful Seminole County College campus didn’t hurt!) but oh how I wish my university experience was as interesting, efficient, relevant, and productive.

Team Foundation Server
Special thanks to the ONETUG president Esteban Garcia for patiently and thoroughly answering all my inane TFS questions. I’ve avoided TFS talks in the past because I thought they would be boring, but boy was I wrong! It really cool to see everything up and integrated like that!

Also another special thanks to SubMain for the CodeIt.Right licensce I won. I’m already putting it to good use!

BarCamp Orlando
If CodeCamp felt like school, then BarCamp felt more like…Woodstock. The event took place in the Wall Street Plaza, with talks taking place in local venues like One-Eyed Jacks, Slingapours, and The Gibson Showroom. The presentation times and topics were updated throughout the day on a giant white board, and there was a great little mobile site for keeping track of the ever evolving schedule the on your iPhone. (I’m an Android fanboy, but I don’t think I’ve ever seen so many Apple products in one place before!)

The talks ranged from the technical to the artistic to the entrepreneurial, and there were strong revolutionary, community and outside-the-box undercurrents behind most talks.

The session that most picqued my interest was done by Stan Schultes, Microsoft MVP and organizer of the BarCamp Sarasota. He led a ‘birds of a feather’ style centered around software start-ups, and what cities like San Francisco, Boston and New York have that we in the “Greater Central Florida Region” don’t….and how we can organize to fix it!

The biggest take-away for me however is getting to see and meet so many inspired and passionate makers, shakers and enthusiasts out there trying to make a difference.

Topics and Times

Photo Credit: Nick Pettit

I’m looking forward to checking out recently discovered entities like Orlando Coding Dojo, Urban ReThink, and Familab events in the near future.

Big shout out to ONETUG, ORUG, and Envy Labs for being such big drivers behind these events and a big thanks to all the organizers, sponsors and community. Fantastic job!

Oh, and I almost forgot to mention the best part about these two events: They’re recurring!

I’m looking forward to seeing you all again soon!

Project Euler: Problem 35 in Ruby

I used my prime generator from Problem 27 for this one. It would have been faster to build the rotation into my generator, but it ran fine without it.

Problem 35

How many circular primes are there below one million?

require 'prime_generator'

primer = Prime_Generator.new 1_000_000

def is_rot_prime? primer, chars
	chars.size.times do |i|
		chars = Array.new(chars.size) { |i| chars[i - 1] }
		return false if !primer.is_prime?(chars.join("").to_i)
	end
	true
end

count = 0
primer.stack.each do |n|
	count += 1 if is_rot_prime? primer, n.to_s.split("")
end

# subtract 1 because "1" doesn't count
puts count - 1

Speaking of the rotation, the ruby array initialize methods and negative indexers make it a cinch to rotate. How cool is this?

chars = Array.new(chars.size) { |i| chars[i - 1] }

Project Euler: Problem 34 in Ruby

Practically every Project Euler problem benefits from memoization, however the issues I ran into with #34 had nothing to do with my algorithm.

The first hurdle was figuring out the upper bound. After scratching around in my notebook, I figured any number I would be looking for would have 7 digits or less. That gives us an upper bound of 9! * 7.

The second hurde took me much, much longer to figure out.

Here’s the secret: 0! = 1

I had taken it for granted that 0! would (of course!) be 0, and I had pre-filled my cache with the number 0. I went round, and round, and round, and round, and round, and round before figuring out (quite accidentally) my error.

It doesn’t make any sense to me, but you can’t argue with math!

Problem 34

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

# cache the digit factorials
factorials = [1]
(1..9).each do |i|
	factorials.push(i * factorials.last)
end

result = 0
(3..2_540_160).each do |n|
	sum = n.to_s.split("").inject(0) do |sum,n|
		sum + factorials[n.to_i]
	end
	result += n if n == sum
end
puts result

Runs in just under a minute 🙂

Project Euler : Problem 36 in Ruby

I’ve been ill today, but there’s no better medicine than an easy Project Euler problem! I tried doing some bitwise magic, but in the end my simplest solution proved the fastest as well.

Problem 36

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

def palindrome? s
	s == s.reverse
end

sum = (1..1_000_000).inject(0) do |sum, n|
	sum += palindrome?(n.to_s) && palindrome?(n.to_s 2) ? n : 0
end

puts sum

Project Euler: Problem 33 in Ruby

It’s fugly, but it works. The hardest part was understanding the question. If the longer description didn’t say that there were exactly 4 fractions, I might have gone crazy.

For reals.

Problem 33

Discover all the fractions with an unorthodox cancelling method.

top, bottom = 1, 1

(10..98).each do |i|
	((i/10)..9).each do |jt|
		jt *= 10
		(1..9).each do |jo|
			j = jt + jo
			next if i >= j
			if i % 10 == j / 10 && i.to_f / j == (i / 10).to_f / (j % 10)
				top *= i
				bottom *= j
			end
		end
	end
end

puts bottom / bottom.gcd(top)

Find the Longest Palindrome in a String

I recently discovered programmingpraxis.com, and one of the more recent posts dealt with one of the greplin programming challenges. I figured since people were posting their code there, then it wouldn’t be too bad for me to post mine here!

Find the Longest Palindrome in a string:

I doubt this is an optimal solution, but I like how it works:

text = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"

def find_longest_palindrome s1, size
	longest = ""

	s1.size.times do |start|
		break if start + size > s1.size
		s2 = s1[start, size].reverse
		if s1.include? s2
			return s2
		end
	end

	find_longest_palindrome s1, size - 1
end

puts find_longest_palindrome(text, text.length)

I found the site via this post, 10 puzzle websites to sharpen your programming skills. Now that the wedding’s over (pics soon!) I can’t wait to get back to a regular extracurricular programming schedule!

Project Euler : Problem 31 in Ruby

I kept trying to make this problem harder than it actually was, but ultimately a simple greedy solution worked just fine.

I would have saved myself a lot of time by actually solving the problem before attempting to optimize. C’est la vie!

Problem #29

Investigating combinations of English currency denominations.

def count_coins coins, target, last_coin = 0

	return 1 if target == 0
	total = 0

	coins.each do |c|
		next if c < last_coin
		total += count_coins(coins, target - c, c) if (target >= c)
	end

	total
end

puts count_coins(
	[1,2,5,10,20,50,100,200],
	200
)

Searching for Read-Only Files with Ruby

Wrote a quick ruby script that someone might find useful. It will recursively find and list readonly files from a passed in directory. There’s also a an array of file extensions you can exclude.

Nothing Fancy:

require 'find'

# update to exclude by file extension
exclude_extensions = ['.jpg','.txt','.png','.gif','.git']

if(ARGV[0] == nil) then
	puts "Please pass in a directory."
	exit
end

puts "Searching for NON read only files"

puts "Excluding: " + exclude_extensions.join("s")

writable = []
Find.find(ARGV[0]) do |path|

	if File.file?(path) and File.writable?(path) then
		if exclude_extensions.include?(File.extname(path))
			writable.push path
		end
	end
end


if writable.size then

	puts "Writable Files:"

	puts "t" + writable.join("nt")

else

	puts "No writable files."

end

Project Euler : Problem 29 in Ruby

I spent some time playing around with a way to reduce calculations by constructing something akin to a sieve, (2^16 is the same as 4^8 and 16^4), but it turns out that the brute force solutions runs in under a second on my machine so it seemed silly to spend any more time with it.

Ruby even minds the big numbers for me, so the solution is quite trivial:

Problem #29

How many distinct terms are in the sequence generated by a^(b) for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

MIN, MAX = 2,100
values = []

(MIN..MAX).each do |base|
  (MIN..MAX).each do |power|
    values << base**power
  end
end

puts values.uniq.length