Chapter 1 - Programming for the Puzzled
Tags: python, book
Published: 2019-10-09 12:18:53

---
title: Chapter 1 - Programming for the Puzzled
published: true
description: 
tags: [python, book]
---

[Programming for the Puzzled](https://mitpress.mit.edu/books/programming-puzzled)

I could not find the answer of Q4. Is it somewhere?

```python
# Q1

def pleaseConfirm(caps):
    caps = caps + ['END']
    start = forward = backward = 0
    intervals = []
    for i in range(1, len(caps)):
        if caps[start] != caps[i]:
            intervals.append((start, i - 1, caps[start]))
            if caps[start] == 'F':
                forward += 1
            else:
                backward += 1
            start = i

    if forward < backward:
        flip = 'F'
    else:
        flip = 'B'
    for t in intervals:
        if t[2] == flip:
            if t[0] != t[1]:
                print('People in positions', t[0], 'through', t[1], 'flip your caps!')
            else:
                print('People in positions', t[0], 'flip your caps!')
        
caps1 = ['F', 'F', 'B', 'B', 'B', 'F', 'B', 'B', 'B', 'F', 'F', 'B', 'F'];
caps2 = ['F', 'F', 'B', 'B', 'B', 'F', 'B', 'B', 'B', 'F', 'F', 'F', 'F'];

pleaseConfirm(caps1)
print('next')
pleaseConfirm(caps2)
```

```python
# Q2

def pleaseConfirmOnePass(caps):
    caps = caps + [caps[0]]
    start = 0
    for i in range(1, len(caps)):
        if caps[i] != caps[i - 1]:
            if caps[i] != caps[0]:
                print('People in positions', i, end = " ")
                start = i
            else:
                if start == i - 1:
                    print('flip your caps!')
                else:
                    print('through', i - 1, 'flip your caps!')
      
caps1 = ['F', 'F', 'B', 'B', 'B', 'F', 'B', 'B', 'B', 'F', 'F', 'B', 'F'];
caps2 = ['F', 'F', 'B', 'B', 'B', 'F', 'B', 'B', 'B', 'F', 'F', 'F', 'F'];

pleaseConfirmOnePass(caps1)
print('next')
pleaseConfirmOnePass(caps2)
```

```python
# Q3

def pleaseConfirm(caps):
    caps = caps + ['END']
    start = forward = backward = 0
    intervals = []
    for i in range(1, len(caps)):
        if caps[i - 1] == 'H':
            start = i

        if caps[start] != caps[i]:
            intervals.append((start, i - 1, caps[start]))
            if caps[start] == 'F':
                forward += 1
            else:
                backward += 1
            if caps[i] != 'H':
                start = i

    if forward < backward:
        flip = 'F'
    else:
        flip = 'B'
    for t in intervals:
        if t[2] == flip:
            if t[0] != t[1]:
                print('People in positions', t[0], 'through', t[1], 'flip your caps!')
            else:
                print('People in positions', t[0], 'flip your caps!')
        
caps = ['F', 'F', 'B', 'H', 'B', 'F', 'B', 'B', 'B', 'F', 'H', 'F', 'F'];
pleaseConfirm(caps)
```

```python
# Q4

def encrypt(s):
    s += '0'
    start = 0
    encrypted = ''
    for i in range(1, len(s)):
        if s[start] != s[i]:
            encrypted += s[start] + str(i - start)
            start = i
    return encrypted

def decrypt(s):
    character = ''
    decrypted = ''
    for i in range(len(s)):
        if not s[i].isalpha():
            decrypted += s[i - 1] * int(s[i])
    return decrypted
      
s = 'BWWWWWBWWWW'
enc = encrypt(s)
dec = decrypt(enc)
print('encrypted=', enc, ', decrypted=', dec)
```