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 ![]() |
Python
CODE def matches_pattern_1(s): sums = {'a': 0, 'b': 0, 'c': 0} for ch in s: try: sums[ch] += 1 except KeyError: pass return sums['a'] + sums['b'] == sums['c'] def matches_pattern_2(s): sums = {'a': 0, 'b': 0, 'c': 0} saw = {'a': False, 'b': False, 'c': False} for ch in s: after = [chr(x) for x in xrange(ord(ch)+1, ord('c')+1)] for a in after: if saw[a]: return False saw[ch] = True sums[ch] += 1 return sums['a'] + sums['b'] == sums['c'] def matches_pattern_3(s): sums = {'a': 0, 'c': 0} saw = {'a': False, 'b': False, 'c': False} for ch in s: if ch == 'a': if saw['b'] or saw['c']: return False saw['a'] = True sums['a'] += 1 elif ch == 'b': if saw['c']: return False saw['b'] = True else: saw['c'] = True sums['c'] += 1 return 0 < sums['a'] == sums['c'] Source: matcher.py |
|
|
![]() ![]() |