+ All Categories
Home > Documents > What Is Countdown?

What Is Countdown?

Date post: 12-Jan-2016
Category:
Upload: gurit
View: 127 times
Download: 1 times
Share this document with a friend
Description:
What Is Countdown?. A popular quiz programme on British television that has been running for almost 20 years. Based upon an original French version called "Des Chiffres et Des Lettres". Includes a numbers game that we shall refer to as the countdown problem. 1. 3. 7. 10. 25. 50. +. - PowerPoint PPT Presentation
27
THE COUNTDOWN PROBLEM Graham Hutton University of Nottingham
Transcript
Page 1: What Is Countdown?

THE COUNTDOWN PROBLEM

Graham HuttonUniversity of Nottingham

Page 2: What Is Countdown?

2

What Is Countdown?

A popular quiz programme on British television that has been running for almost 20 years.

Based upon an original French version called "Des Chiffres et Des Lettres".

Includes a numbers game that we shall refer to as the countdown problem.

Page 3: What Is Countdown?

3

Example

1 3 7 10 25 50

Using the numbers

and the arithmetic operators

765

+ -

construct an expression whose value is

Page 4: What Is Countdown?

4

Rules

All the numbers, including intermediate results, must be integers greater than zero.

Each of the source numbers can be used at most once when constructing the expression.

We abstract from other rules that are adopted on television for pragmatic reasons.

Page 5: What Is Countdown?

5

For our example, one possible solution is

There are 780 solutions for this example.

Changing the target number to gives an example that has no solutions.

Notes:

831

(25-10) (50+1) 765=

Page 6: What Is Countdown?

6

Evaluating Expressions

Operators:

data Op = Add | Sub | Mul | Div

Apply an operator:

apply :: Op Int Int Intapply Add x y = x + yapply Sub x y = x - yapply Mul x y = x * yapply Div x y = x `div` y

Page 7: What Is Countdown?

7

Decide if the result of applying an operator to two integers greater than zero is another such:

valid :: Op Int Int Boolvalid Add _ _ = Truevalid Sub x y = x > yvalid Mul _ _ = Truevalid Div x y = x `mod` y == 0

Expressions:

data Expr = Val Int | App Op Expr Expr

Page 8: What Is Countdown?

8

eval :: Expr [Int]eval (Val n) = [n | n > 0]eval (App o l r) = [apply o x y | x eval l , y eval r , valid o x y]

Return the overall value of an expression, provided that it is an integer greater than zero:

Either succeeds and returns a singleton list, or fails and returns

the empty list.

Page 9: What Is Countdown?

9

Specifying The Problem

Return a list of all possible ways of selecting zero or more elements from a list:

subbags :: [a] [[a]]

For example:

> subbags [1,2]

[[],[1],[2],[3],[1,2],[2,1]]

Page 10: What Is Countdown?

10

Return a list of all the values in an expression:

values :: Expr [Int]values (Val n) = [n]values (App _ l r) = values l ++ values r

Decide if an expression is a solution for a given list of source numbers and a target number:

solution :: Expr [Int] Int Boolsolution e ns n = elem (values e) (subbags ns) && eval e == [n]

Page 11: What Is Countdown?

11

Brute Force Implementation

Return a list of all possible ways of splitting a list into two non-empty parts:

nesplit :: [a] [([a],[a])]

For example:

> nesplit [1,2,3,4]

[([1],[2,3,4]),([1,2],[3,4]),([1,2,3],[4])]

Page 12: What Is Countdown?

12

Return a list of all possible expressions whose values are precisely a given list of numbers:

exprs :: [Int] [Expr]exprs [] = []exprs [n] = [Val n]exprs ns = [e | (ls,rs) nesplit ns , l exprs ls , r exprs rs , e combine l r]

The key function in this lecture.

Page 13: What Is Countdown?

13

combine :: Expr Expr [Expr]combine l r = [App o l r | o [Add,Sub,Mul,Div]]

Combine two expressions using each operator:

solutions :: [Int] Int [Expr]solutions ns n = [e | ns' subbags ns , e exprs ns' , eval e == [n]]

Return a list of all possible expressions that solve an instance of the countdown problem:

Page 14: What Is Countdown?

14

Correctness Theorem

Our implementation returns all the expressions that satisfy our specification of the problem:

elem e (solutions ns n)

solution e ns n

Page 15: What Is Countdown?

15

How Fast Is It?

System:

Compiler:

Example:

One solution:

All solutions:

solutions [1,3,7,10,25,50] 765

1GHz Pentium-III laptop

GHC version 5.00.2

0.89 seconds

113.74 seconds

Page 16: What Is Countdown?

16

Many of the expressions that are considered will typically be invalid - fail to evaluate.

For our example, only around 5 million of the 33 million possible expressions are valid.

Combining generation with evaluation would allow earlier rejection of invalid expressions.

Can We Do Better?

Page 17: What Is Countdown?

17

results :: [Int] [Result]results ns = [(e,n) | e exprs ns , n eval e]

type Result = (Expr,Int)

Valid expressions and their values:

Specification of a function that fuses together the generation and evaluation of expressions:

Applying Program Fusion

Page 18: What Is Countdown?

18

results [] = []results [n] = [(Val n,n) | n > 0]results ns = [res | (ls,rs) nesplit ns , lx results ls , ry results rs , res combine' lx ry]

We can now calculate an implementation:

combine' :: Result Result [Result]

where

Page 19: What Is Countdown?

19

solutions' :: [Int] Int [Expr]solutions' ns n = [e | ns' subbags ns , (e,m) results ns' , m == n]

Return a list of all possible expressions that solve an instance of the countdown problem:

Correctness Theorem:

solutions' solutions=

Page 20: What Is Countdown?

20

How Fast Is It Now?

Example:

One solution:

All solutions:

solutions' [1,3,7,10,25,50] 765

0.08 seconds

5.76 seconds

Around 10 times faster.

Around 20 times faster.

Page 21: What Is Countdown?

21

Many expressions will be essentially the same using simple arithmetic properties, such as:

Exploiting such properties would considerably reduce the search and solution spaces.

Can We Do Better?

x y y x

x 1 x

=

=

Page 22: What Is Countdown?

22

Exploiting Properties

Strengthening the valid predicate to take account of commutativity and identity properties:

valid :: Op Int Int Bool

valid Add x y = True

valid Sub x y = x > y

valid Mul x y = True

valid Div x y = x `mod` y == 0

x yx y && x 1x y && x 1 && y 1

x y

&& y 1

Page 23: What Is Countdown?

23

Using this new predicate gives a new version of our specification of the countdown problem.

The new specification is sound with respect to our original specification.

It is also complete up to equivalence of expr-essions under the exploited properties.

Notes:

Page 24: What Is Countdown?

24

Using the new predicate also gives a new version of our implementation, written as solutions''.

The new implementation requires no new proof of correctness, because none of our previous proofs depend upon the definition of valid.

Hence our previous correctness results still hold under changes to this predicate.

Notes:

Page 25: What Is Countdown?

25

How Fast Is It Now?

Example:

Valid:

Solutions:

solutions'' [1,3,7,10,25,50] 765

250,000 expressions

49 expressions

Around 20 times

less.

Around 16 times

less.

Page 26: What Is Countdown?

26

One solution:

All solutions:

0.04 seconds

0.86 seconds

Around 2 times faster.

Around 7 times faster.

More generally, our program usually produces a solution to problems from the television show in an instant, and all solutions in under a second.

Page 27: What Is Countdown?

27

Is Carol Now Redundant?

Vorderman said of the computer program:"It's a nice idea, but thankfully the bosses of Countdown don't want to do away with

me and would prefer to keep it human against human!"

How the computer program would handle a makeover show, or dressing up for an awards ceremony remains to be seen.

(London Evening Standard, 1/11/2001)


Recommended