Program this!, Programming thread |
Program this!, Programming thread |
![]()
Post
#1
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 2,648 Joined: Apr 2008 Member No: 639,265 ![]() |
I'm of the understand that there are a few Technology forum aficionados that are programmers, or are at least interested in programming, so I thought I'd start a thread -- a sort of game. The game was inspired by this thread. It's pretty simple: just implement the following program in whatever language you desire. If someone's already posted a program in your favorite language, see if you can do better! You get bragging rights if you implement the program in a weird/unusual/rare language.
Here are the guidelines for the program:
|
|
|
![]() |
![]()
Post
#2
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 2,648 Joined: Apr 2008 Member No: 639,265 ![]() |
Ruby
CODE def matches_pattern_1?(s) if s =~ /[abc]*/ sums = {:a => 0, :b => 0, :c => 0} s.each_char { |ch| sums[ch.intern] += 1 } sums[:a] + sums[:b] == sums[:c] else false end end def matches_pattern_2?(s) if s =~ /(a*)(b*)(c*)/ $1.length + $2.length == $3.length else false end end def matches_pattern_3?(s) if s =~ /(a+)b*(c+)/ $1.length == $2.length else false end end Source: matcher.rb |
|
|
![]() ![]() |