Title: Fundamentals%20of%20Perfect%20Developer
1Fundamentals of Perfect Developer
- A one-day hands-on tutorial
- Answers to Exercises
2Suggested answers to Exercise 1
- const zeroToOneHundred seq of int 0..100
- property assert 42 in zeroToOneHundred, 101
in zeroToOneHundred - function divides(i, j int) boolpre j gt 0 i
j 0 - const squaresOfPrimes seq of int for those
i2..100 - forall j2..lti - divides(i,
j) yield i 2 - function max(S set of int) intpre S.empty
that xS - forall yS - y lt x
3Suggested answers to Exercise 4
- A recursive solution to the first problem is
- function numLeadingSpaces(s string)
natdecrease s ( s.empty s.head
0, 1 numLeadingSpaces(s.tail) - )
- A non-recursive solution to the first is
- function numLeadingSpaces(s string) nat that
j0..s - (j s sj )
(forall k0..ltj - sk ) - The following member functions may be useful
- take drop findFirst prepend
4Suggested answers 4 (contd)
- function removeLeadingSpaces(s string)
string s.drop(numLeadingSpaces(s)) - function firstWord(s string) string ( let n
s.findFirst( ) n lt 0 s,
s.take(n) )
5Suggested answers 4 (contd)
- function splitIntoWords(s string) seq of
stringdecrease s ( let stripped
removeLeadingSpaces(s) stripped.empty
seq of string, ( let w
firstWord(stripped) splitIntoWords(stri
pped.drop(w)) .prepend(w) )
)
6Suggested answers to Exercise 5
- function min2a(x, y int) int
- satisfy result lt x,
- result lt y,
- result x result y
- via
- if x gt y value y value x fi
- end
-
- function min2b(x, y int) int
- satisfy result lt x,
- result lt y,
- result x result y
- via
- value (x gt y y, x)
- end
7Suggested answers 5 (contd)
- function findFirst1(s seq of int, x int) int
- satisfy 0 lt result lt s,
- result s sresult x,
- forall j0..ltresult - sj x
- via
- loop
- var i (nat in 0..s)! 0
- keep forall j0..lti' - sj x
- until i' s
- decrease s - i'
- if si x value i fi
- i! 1
- end
- value s
- end
8Suggested answers 5 (contd)
- function findFirst1(s seq of int, x int) int
- satisfy 0 lt result lt s,
- result s sresult x,
- forall j0..ltresult - sj x
- via
- var i (nat in 0..s)! 0
- loop
- change i
- keep forall j0..lti' - sj x
- until i' s si' x
- decrease s - i'
- i! 1
- end
- value i
- end
9Suggested answers 5 (contd)
- function numLeadingSpaces(s string) nat
- that j0..s
- - (j s sj )
- (forall k0..ltj - sk )
- via
- var i (nat in 0..s)! 0
- loop
- change i
- keep forall j0..lti'- sj
- until i' s si'
- decrease s - i'
- i! 1
- end
- value i
- end
10Suggested answers 5 (contd)
- function splitIntoWords(s string) seq of string
- decrease s
- ( let stripped removeLeadingSpaces(s)
- stripped.empty
- seq of string,
-
- ( let w firstWord(stripped)
- assert w.empty
- splitIntoWords(stripped.drop(w)).prepen
d(w) - )
- )
- ...
11Suggested answers 5 (contd)
- via
- var rslt seq of string ! seq of string
- loop
- var i (nat in 0..s)! 0
- change rslt
- keep splitIntoWords(s)
- rslt' splitIntoWords(s.drop(i'))
- until i' s
- decrease s - i'
- i! numLeadingSpaces(s.drop(i))
- if i lt s
- let w firstWord(s.drop(i))
- rslt! rslt.append(w), i! w
- fi
- end
- value rslt
- end
12Suggested answers to Exercise 6
function longest(s seq of string) string
decrease s ( s.empty "",
( let temp
longest(s.front) s.last gt
temp s.last,
temp ) )
13Suggested answers 6 (contd)
class ListOfStrings abstract var list seq
of string internal var long string
invariant long longest(list) interface
build post list! seq of string via
list! seq of string, long! "
end
14Suggested answers 6 (contd)
schema !add(s string) post list!
list.append(s) via list!
list.append(s) if s gt long
long! s fi end function
longest string longest(list) via
value long end end