A "for all" claim is refuted by one counterexample, and a "there exists" claim is proved by one witness. Your program tests both claims about a list of numbers and lists the counterexamples.
What to build
A program that checks "every number is divisible by k" and "some number is divisible by k".
Requirements
- Read a line of whole numbers, then a line with k.
- Print
for all: yesorfor all: nofor the claim that every number is divisible by k. - Print
there exists: yesorthere exists: nofor the claim that at least one number is divisible by k. - Print
counterexamples:followed by the numbers that are not divisible by k, in input order, ornone.
Input
Line 1: the numbers. Line 2: k.
Output
Three lines.
Constraints
1 to 30 numbers, each from -1000 to 1000; 1 ≤ k ≤ 100.
Example
Input:
4 8 12
4
Output:
for all: yes
there exists: yes
counterexamples: none
Why it works
The two claims are all(...) and any(...) over the same predicate. The counterexamples are exactly the elements that make the predicate false: the list is empty if and only if the 'for all' claim is true.
Examples
4 8 12 4
for all: yes there exists: yes counterexamples: none
4 7 8 9 4
for all: no there exists: yes counterexamples: 7 9
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
all(x % k == 0 for x in nums) is the 'for all' statement and any(...) is 'there exists'.
Hint 2
The counterexamples are the numbers x with x % k != 0, in the order they appear.
Hint 3
Print `none` when the list of counterexamples is empty.
Solution
Show one correct solution (try it yourself first)
nums = [int(x) for x in input().split()]
k = int(input())
yes = lambda b: "yes" if b else "no"
bad = [x for x in nums if x % k != 0]
print("for all:", yes(all(x % k == 0 for x in nums)))
print("there exists:", yes(any(x % k == 0 for x in nums)))
print("counterexamples:", " ".join(map(str, bad)) if bad else "none")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step