diff options
| author | Yuval Adam <yuv.adm@gmail.com> | 2011-05-07 17:49:23 +0300 |
|---|---|---|
| committer | Yuval Adam <yuv.adm@gmail.com> | 2011-05-07 17:49:23 +0300 |
| commit | ff3299c98ab68f35874b916ac1a83cc53186c165 (patch) | |
| tree | 3e2e0761ae88eca4bdc50893d360bd3f141d298d /2011/qual | |
| parent | 4ce443495a73befb2fd2d9f547c8f25b454cdc42 (diff) | |
added exception throwing to continue loop
Diffstat (limited to '2011/qual')
| -rw-r--r-- | 2011/qual/d/d.py | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/2011/qual/d/d.py b/2011/qual/d/d.py index 0f4a6d5..b1b6500 100644 --- a/2011/qual/d/d.py +++ b/2011/qual/d/d.py @@ -1,33 +1,33 @@ -from time import sleep def solve(a): steps = 0 sol = range(1,len(a)+1) # stop if solved while a != sol: - print "%s is not %s" % (a, sol) - flipped = False - # first flip all pairs - for i in range(len(a)): - if a[i] != i+1 and a[a[i]-1] == i+1: - print 'PAIR FLIP %d and %d' % (a[a[i]-1], a[i]) - t = a[a[i]-1] - a[a[i]-1] = a[i] - a[i] = t - steps += 2 - flipped = True - - if flipped: - continue + try: + print "%s is not %s" % (a, sol) + next = False + # first flip all pairs + for i in range(len(a)): + if a[i] != i+1 and a[a[i]-1] == i+1: + print 'PAIR FLIP %d and %d' % (a[a[i]-1], a[i]) + t = a[a[i]-1] + a[a[i]-1] = a[i] + a[i] = t + steps += 2 + raise Exception - # now find the first mixed index to fix - for i in range(len(a)): - if a[i] != i+1: - print 'FLIP %d and %d' % (a[a[i]-1], a[i]) - t = a[a[i]-1] - a[a[i]-1] = a[i] - a[i] = t - steps += 2 + # now find the first mixed index to fix + for i in range(len(a)): + if a[i] != i+1: + print 'FLIP %d and %d' % (a[a[i]-1], a[i]) + t = a[a[i]-1] + a[a[i]-1] = a[i] + a[i] = t + steps += 2 + raise Exception + except: + continue return steps |
