0% found this document useful (0 votes)
5 views4 pages

CS 135 Spring 2025 Mid-Term Practice Problems

The document contains practice problems for a CS 135 mid-term exam, covering topics such as logical argument validity, set theory, strong induction, and recursive functions in Scheme. Each problem includes a statement, proof or counterexample, and sometimes an inductive step. The problems emphasize logical reasoning, mathematical proofs, and programming concepts.

Uploaded by

alanhujr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

CS 135 Spring 2025 Mid-Term Practice Problems

The document contains practice problems for a CS 135 mid-term exam, covering topics such as logical argument validity, set theory, strong induction, and recursive functions in Scheme. Each problem includes a statement, proof or counterexample, and sometimes an inductive step. The problems emphasize logical reasoning, mathematical proofs, and programming concepts.

Uploaded by

alanhujr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS 135 Spring 2025: Mid-Term Practice Problems.

Problem 1. Either prove that the following argument is valid or give a counterexample:
If Bob carries an umbrella, then either it is raining, or it is cloudy
If it is cloudy then Bob does not carry an umbrella
Therefore, if it is raining Bob carries an umbrella.

U: Bob carries an umbrella


R: It is raining
C: It is cloudy

𝑈 → (𝑅 ∨ 𝐶)
𝐶 → ¬𝑈
∴𝑅→𝑈
When R is True and U is False both hypotheses are True (regardless of C), and the conclusion is
False. Therefore, the argument is invalid.

Problem 2. Either prove that the following argument is valid or give a counterexample:
𝐴 ⇒ 𝐵 ∨ ¬𝐷
¬𝐶 ⇒ ¬𝐵 ∧ 𝐷
𝐷 ⇒𝐴∨𝐶
¬𝐴 ⇒ (𝐷 ⇒ 𝐵)

When C and D are True, and A and B are False, each of the hypotheses is True and the
conclusion is False. Therefore, the argument is invalid.

Problem 3. Prove (using logic or set identities) that for all sets 𝐴, 𝐵, 𝐶:
(𝐵 − 𝐴) ∪ (𝐶 − 𝐴) ⊆ (𝐵 ∪ 𝐶) − 𝐴
(𝐵 − 𝐴) ∪ (𝐶 − 𝐴)
= (𝐵 ∩ 𝐴̅) ∪ (𝐶 ∩ 𝐴̅)
= (𝐵 ∪ 𝐶) ∩ 𝐴̅
= (𝐵 ∪ 𝐶) − 𝐴
⊆ (𝐵 ∪ 𝐶) − 𝐴

Problem 4. Determine all amounts of postage that can be formed using only 4-cent and 11-cent
stamps. Prove your answer using strong induction. Be sure to show the base case, the inductive
hypothesis, and the inductive step.

Claim: ∀𝑛 ≥ 30 we can make exact postage for 𝑛-cents using only 4-cent and 11-cent stamps.
Basis: We can make 30, 31, 32, 33 as follows:
30 = 2 ⋅ 4 + 2 ⋅ 11
31 = 5 ⋅ 4 + 1 ⋅ 11
32 = 8 ⋅ 4
33 = 3 ⋅ 11
Inductive Hypothesis: For some 𝑘 ≥ 33, we can make exact postage for all 𝑖: 30 ≤ 𝑖 ≤ 𝑘.
Inductive Step: Since 30 ≤ 𝑘 − 3 we can make exact postage for 𝑘 cents.
By adding one 4-cent stamp to that solution, we can make exact postage for
(𝑘 − 3) + 4 = 𝑘 + 1 cents.

Problem 5. Consider the following Scheme function:


(define (mystery L)
(if (null? L) 0
(if (null? (cdr L)) 1
(+ 1 (mystery (cddr L))))))

(a) What is (mystery (list 1 2 3 4))? What is (mystery (list 1 2 3 4 5))?


2 and 3.
(b) What is the function computed by mystery? Justify your answer with a proof.

!
⌈𝑛/2⌉, where 𝑛 is the length of the argument list. If 𝑛 is even, then the value is , and
"
when 𝑛 is odd, the value is (𝑛 + 1)/2.
Proof: By induction on 𝑛
Basis: For 𝑛 = 0, the value returned for the null list in Line 2 is 0.
#
For 𝑛 = 1, the value returned in Line 3 is 1 = ⌈"⌉.
Inductive Hypothesis: For some 𝑛 ≥ 1, the value returned is ⌈𝑛/2⌉.
Inductive Step: For a list of length 𝑛 + 1, the value returned is 1 plus the value returned
for a list of length (𝑛 + 1) − 2 = 𝑛 − 1.
!$#
By the inductive hypothesis, the value returned for a list of length 𝑛 − 1 is K "
L.
!$# !%#
So the value returned for a list of length 𝑛 + 1 is 1 + K "
L= K "
L.

(c) Now change the last line of mystery so the new definition becomes:
(define (mystery L)
(if (null? L) 0
(if (null? (cdr L)) 1
(+ (mystery (cdr L)) (mystery (cddr L))))))

What function does mystery now compute? Justify your answer with a proof.
For a list of length 𝑛, let 𝑓(𝑛) be the returned value.
Looking at the code, we see that:
𝑓(0) = 0, 𝑓(1) = 1, 𝑓(𝑛) = 𝑓(𝑛 − 1) + 𝑓(𝑛 − 2)
This is the definition of the Fibonacci function!

Problem 6. Prove by
induction the statement ∀𝑛 ≥ 0: 1 + 2 + 2" + ⋯ 2! = 2!%# − 1
a. State and establish the base case.
1 = 2&%# − 1, which is true.
b. State the inductive hypothesis.
For some 𝑘 ≥ 0, 1 + 2 + 2" + ⋯ 2' = 2'%# − 1
c. Establish the inductive step.
1 + 2 + 2" + ⋯ 2'%#
= (1 + 2 + 2" + ⋯ 2' ) + 2'%#
= 2'%# − 1 + 2(%#
= 2 ⋅ 2(%# − 1
= 2(%" − 1

Problem 7. Prove the following equality, for all 𝑛 ≥ 1:


1) + 2) + ⋯ + 𝑛) = (1 + 2 + ⋯ + 𝑛)"
Basis: 𝑛 = 1. 1) = 1" which is true.
Inductive Hypothesis: For some 𝑘 ≥ 1: 1) + 2) + ⋯ + 𝑘 ) = (1 + 2 + ⋯ + 𝑘)"
Inductive Step: 1) + 2) + ⋯ + (𝑘 + 1))
= (1) + 2) + ⋯ + 𝑘 ) ) + (𝑘 + 1))
= (1 + 2 + ⋯ + 𝑘)" + (𝑘 + 1))
"
𝑘(𝑘 + 1)
=O P + (𝑘 + 1))
2
"
𝑘"
= (𝑘 + 1) O + 𝑘 + 1P
4
"
(𝑘 + 1)(𝑘 + 2)
=O P
2
"
= Q1 + 2 + ⋯ (𝑘 + 1)R

Problem 8. Consider the following Scheme functions:


(define (func A) (myfun A ‘() ))
(define (myfun A B)
(if (null? A) B
(myfun (cdr A) (cons (car A ) B))))

a. What is the value returned by (func ‘(1 2 3))?


(3 2 1)
b. What is the value returned by func on a list of length 𝑁?
The function func returns the reverse of its input list A.
c. Prove, by induction on the length of list A, the correctness of your assertion in part b.
First we prove that the value of (myfun A B) is the list obtained by appending the
reverse of A to B.
Basis: The length 𝑛 of the input list A is 0. The returned value is B, which is the same as
appending the empty list (the reverse of A is A in this case) to B.
Inductive Hypothesis: For some 𝑛 ≥ 0, if the length of A is 𝑛 then (myfun A B) returns
the reverse of A appended to B.
Inductive Step: Consider a list A of length 𝑛 + 1 whose first element is a. In line 3, the
return value is seen to be (myfun (cdr A) (cons a B))
By the inductive hypothesis, this is equivalent to reversing (cdr A) and appending it to
the list consisting of a followed by the elements of B.
This is equivalent to appending the reverse of A to B.

Common questions

Powered by AI

The Scheme function 'func' reverses a list through 'myfun', appending elements in reverse order. Inductive proof: For n=0, myfun A B returns B, valid as reversing nothing adds nothing. Assume for n, reversing A of length n appends to B correctly. For n+1, 'myfun (cdr A) (cons (car A) B)' integrates the head of A after reverse of cdr. Inductively following this process confirms reversal correctness for any n-length A, as returning elements prepended ensures full list reversal .

The function 'func' uses auxiliary 'myfun' to reverse its input list. For '(1 2 3)', it returns '(3 2 1)'. The function acts by recursively moving the first element to the back of accumulating parameter B, reversing the entire list cumulatively. This behavior proves consistent for lists of any length, applying recursion until all input elements are transferred to B in reverse order .

By changing the recursive combination line in the function, the structure fits Fibonacci's definition: f(n) = f(n-1) + f(n-2). In the original scheme, adding the result of the next step onwards with two-stepped prior echoes Fibonacci progression. Validation uses induction: Given base case fits f(0)=0, f(1)=1, modify recursion validates f(n)=f(n-1)+f(n-2), confirming Fibonacci calculation specification, building numbers sequentially in recursive results .

The function 'mystery' computes ⌈n/2⌉, where n is the length of the list. This indicates it counts the number of elements, considering every two elements count as one. Proof by induction: For n=0, it returns 0. For n=1, it returns 1, which matches ⌈1/2⌉. Assuming for n, it holds ⌈n/2⌉, then for n+1, analyzing 'mystery' shows return value of 1 plus the value for the list of length n-1: 1 + ⌈n/2⌉ = ⌈(n+1)/2⌉. This confirms the functionality .

To prove 1 + 2 + 2^2 + ... + 2^n = 2^(n+1) - 1 by induction, establish the base case for n=0: 1 = 2^1 - 1, which holds. Inductive hypothesis assumes true up to some k, i.e., 1 + 2 + ... + 2^k = 2^(k+1) - 1. The step shows for k+1, adding 2^(k+1) to both sides gives 2^(k+1) - 1 + 2^(k+1) = 2^(k+2) - 1. The manipulation concludes the hypothesis holds for k+1 as well, proving the statement .

To prove that any postage amount of 30 cents or more can be created using 4-cent and 11-cent stamps, start with a base case showing 30, 31, 32, 33 can be made explicitly: 30 = 2*4 + 2*11, 31 = 5*4 + 1*11, 32 = 8*4, 33 = 3*11. Inductive hypothesis assumes for some k≥33, all amounts from 30 to k can be made. The inductive step shows that by adding a 4-cent stamp to any k≥33 whose postage can be created, the next number k+1 can be formed: (k−3) + 4 = k+1. This completes the inductive proof .

The formula 1^2 + 2^2 + ... + n^2 = (1 + 2 + ... + n)^2 can be proven by induction. Base case n=1 confirms 1^2 = 1^2. Inductive hypothesis assumes true for some k, i.e., 1^2 + ... + k^2 = (1 + 2 + ... + k)^2. The step considers adding (k+1)^2 to both sides: LHS becomes 1^2 + ... + (k+1)^2, and RHS transforms with binomial expansion and simplification to match (1 + 2 + ... + (k+1))^2, confirming the hypothesis for k+1 .

With the modification, 'mystery' computes the Fibonacci sequence. Defined through recursive equations: f(0) = 0, f(1) = 1, f(n) = f(n−1) + f(n−2), the function construction mirrors this. The change to add results of mystery(cdr L) and mystery(cddr L) reflects the Fibonacci addition between previous terms. Identifying each step with Fibonacci definitions justifies the computation of the Fibonacci function by the modified 'mystery' .

Using set identities, (B−A) ∪ (C−A) can be rewritten as (B∩A̅) ∪ (C∩A̅). This is equivalent to (B∪C)∩A̅, which simplifies to (B∪C)−A, showing that (B−A) ∪ (C−A) is indeed a subset of (B∪C)−A. The transformation uses the identities of union, intersection, and set complement, thus proving the statement .

The argument's validity can be analyzed by examining logical propositions and checking if any counterexamples exist. The propositions are: U: Bob carries an umbrella, R: It is raining, C: It is cloudy. We have U→(R∨C) and C→¬U with the conclusion R→U. The argument is invalid because when R is true and U is false, both hypotheses are true regardless of C, and the conclusion is false. Therefore, this scenario serves as a valid counterexample showing the argument is invalid .

You might also like