commit
e092cb029c
252 files changed
+35037
No files matched your search
@@ -0,0 +1,24 @@
|
||||
// CALL: CONTAINS_SORTED([2, 5, 8, 13, 21], 13)
|
||||
// EXPECT: CONTAINS_SORTED returned TRUE
|
||||
// Is X in the sorted list L? Halve the range that could hold it each time.
|
||||
FUNCTION CONTAINS_SORTED(L : LIST OF INTEGER, X : INTEGER) RETURNS BOOL
|
||||
REQUIRE FORALL K IN [0, LENGTH(L) - 1) : L[K] <= L[K + 1]
|
||||
DECLARE Lo : INTEGER
|
||||
DECLARE Hi : INTEGER
|
||||
DECLARE Mid : INTEGER
|
||||
Lo <- 0
|
||||
Hi <- LENGTH(L)
|
||||
WHILE Lo < Hi
|
||||
INVARIANT 0 <= Lo AND Lo <= Hi AND Hi <= LENGTH(L)
|
||||
DECREASES Hi - Lo
|
||||
Mid <- (Lo + Hi) DIV 2
|
||||
IF L[Mid] == X THEN
|
||||
RETURN TRUE
|
||||
ELSEIF L[Mid] < X THEN
|
||||
Lo <- Mid + 1
|
||||
ELSE
|
||||
Hi <- Mid
|
||||
ENDIF
|
||||
ENDWHILE
|
||||
RETURN FALSE
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,9 @@
|
||||
cell: ; cell(Int, Int, Int)
|
||||
xor eax, eax ; mojo line 9
|
||||
cmp rdi, 0 ; mojo line 9
|
||||
cmovg rax, rdi ; mojo line 9
|
||||
cmp rsi, rax ; mojo line 11
|
||||
cmovg rax, rsi ; mojo line 11
|
||||
cmp rdx, rax ; mojo line 13
|
||||
cmovg rax, rdx ; mojo line 13
|
||||
ret ; mojo line 15
|
||||
@@ -0,0 +1,5 @@
|
||||
# cell(Int, Int, Int): the arguments arrive in rdi, rsi, rdx (System V)
|
||||
# the diagonal 4, a gap in the reference 9, a gap in the read -2: the cell keeps 9
|
||||
rdi = 4
|
||||
rsi = 9
|
||||
rdx = -2
|
||||
@@ -0,0 +1,7 @@
|
||||
// CALL: COUNT_BASES('A', "GATTACA")
|
||||
// EXPECT: COUNT_BASES returned 3
|
||||
To count the B bases in T:
|
||||
Start with Count at 0.
|
||||
For each letter c of T:
|
||||
If c is B, add 1 to Count.
|
||||
The answer is Count.
|
||||
@@ -0,0 +1,14 @@
|
||||
# origin: Programming+ exercise count-base (was translations.mojo, by course staff)
|
||||
# status: unverified; Stage 1 verifies it on the pinned compiler
|
||||
# probe: does the course's hand-written count_base compile and agree with the pseudo?
|
||||
# expect-stdout: 3
|
||||
def count_base(b: String, t: String) -> Int:
|
||||
var total = 0
|
||||
for i in range(t.count_codepoints()):
|
||||
if String(t[codepoint=i]) == b:
|
||||
total += 1
|
||||
return total
|
||||
|
||||
|
||||
def main():
|
||||
print(count_base("A", "GATTACA"))
|
||||
@@ -0,0 +1,26 @@
|
||||
// CALL: COUNT_BASE('A', "GATTACA")
|
||||
// EXPECT: COUNT_BASE returned 3
|
||||
// How many positions of T hold the base B.
|
||||
FUNCTION COUNT_BASE(B : CHAR, T : STRING) RETURNS INTEGER
|
||||
|
||||
// Set up
|
||||
DECLARE N : INTEGER
|
||||
DECLARE Total : INTEGER
|
||||
N <- LENGTH(T)
|
||||
Total <- 0
|
||||
|
||||
// Visit each position
|
||||
FOR I <- 0 TO N
|
||||
|
||||
// Test this position
|
||||
IF T[I] == B THEN
|
||||
|
||||
// Update the running total
|
||||
Total <- Total + 1
|
||||
|
||||
ENDIF
|
||||
NEXT I
|
||||
|
||||
// Report
|
||||
RETURN Total
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,4 @@
|
||||
SPEC COUNT_BASE(B, T)
|
||||
ENSURE result = |{ K ∈ [0, |T|) : T[K] = B }|
|
||||
LOOP FOR I
|
||||
INVARIANT Total = |{ K ∈ [0, I) : T[K] = B }|
|
||||
@@ -0,0 +1,15 @@
|
||||
// CALL: COUNT_GC("GATTACA")
|
||||
// ERRORS: 2
|
||||
// Count the G and C bases in T. This version has five mistakes;
|
||||
// the coach finds all of them before anything runs.
|
||||
FUNCTION COUNT_GC(T : STRING) RETURNS INTEGER
|
||||
DECLARE Total : INTEGER
|
||||
DECLARE Unused : FLOAT
|
||||
FOR I <- 0 TO LENGTH(T) - 1
|
||||
Total <- 0
|
||||
IF T[I] == "G" OR T[I] == 'C' THEN
|
||||
Total = Total + 1
|
||||
ENDIF
|
||||
NEXT I
|
||||
RETURN Total
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,38 @@
|
||||
# probe: copy and move constructors through var arguments and transfers
|
||||
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
|
||||
# expect-stdout: new a
|
||||
# expect-stdout: copy a
|
||||
# expect-stdout: consumed a+
|
||||
# expect-stdout: free a+
|
||||
# expect-stdout: move a
|
||||
# expect-stdout: holding a
|
||||
# expect-stdout: free a
|
||||
struct Box(Copyable, Movable):
|
||||
var label: String
|
||||
|
||||
def __init__(out self, label: String):
|
||||
self.label = label
|
||||
print("new", label)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.label = copy.label + "+"
|
||||
print("copy", copy.label)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.label = move.label^
|
||||
print("move", self.label)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("free", self.label)
|
||||
|
||||
|
||||
def consume(var b: Box):
|
||||
print("consumed", b.label)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Box("a")
|
||||
var b = a.copy()
|
||||
consume(b^)
|
||||
var c = a^
|
||||
print("holding", c.label)
|
||||
@@ -0,0 +1,33 @@
|
||||
// CALL: MATCH("ATT", "GATTACA")
|
||||
// STOPS: index
|
||||
// One bound is wrong. Every position where pattern P occurs exactly in text T.
|
||||
FUNCTION MATCH(P : STRING, T : STRING) RETURNS LIST OF INTEGER
|
||||
|
||||
// Set up
|
||||
DECLARE N : INTEGER
|
||||
DECLARE M : INTEGER
|
||||
DECLARE Matched : BOOL
|
||||
DECLARE Answer : LIST OF INTEGER
|
||||
N <- LENGTH(T)
|
||||
M <- LENGTH(P)
|
||||
|
||||
// Visit each legal start
|
||||
FOR I <- 0 TO N
|
||||
|
||||
// Test: does the pattern match here?
|
||||
Matched <- TRUE
|
||||
FOR J <- 0 TO M
|
||||
IF T[I + J] != P[J] THEN
|
||||
Matched <- FALSE
|
||||
BREAK
|
||||
ENDIF
|
||||
NEXT J
|
||||
|
||||
// Update
|
||||
IF Matched THEN
|
||||
APPEND(Answer, I)
|
||||
ENDIF
|
||||
NEXT I
|
||||
|
||||
RETURN Answer
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,3 @@
|
||||
% CALL: X_BAR(3, [1.0, 2.0, 6.0])
|
||||
% EXPECT: X_BAR returned 3.0
|
||||
\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i
|
||||
@@ -0,0 +1,27 @@
|
||||
; rdi = list A rsi = |A| rdx = list B rcx = |B|
|
||||
; rax = how many values appear in both
|
||||
xor eax, eax
|
||||
xor r8, r8
|
||||
xor r9, r9
|
||||
.step:
|
||||
cmp r8, rsi
|
||||
jae .done
|
||||
cmp r9, rcx
|
||||
jae .done
|
||||
mov r10, qword ptr [rdi + r8*8]
|
||||
mov r11, qword ptr [rdx + r9*8]
|
||||
cmp r10, r11
|
||||
je .both
|
||||
jb .advanceA
|
||||
add r9, 1 ; the second list is behind
|
||||
jmp .step
|
||||
.advanceA:
|
||||
add r8, 1 ; the first list is behind
|
||||
jmp .step
|
||||
.both:
|
||||
add rax, 1
|
||||
add r8, 1
|
||||
add r9, 1
|
||||
jmp .step
|
||||
.done:
|
||||
ret
|
||||
@@ -0,0 +1,6 @@
|
||||
rdi = 0x1000
|
||||
rsi = 6
|
||||
rdx = 0x2000
|
||||
rcx = 7
|
||||
memory 0x1000 bytes 1 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 13 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0
|
||||
memory 0x2000 bytes 2 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 31 0 0 0 0 0 0 0
|
||||
Reference in new issue
Block a user