|
How to check if a string is a palindrome:
a b c d e d c b a
^ ^
| |
1st letter last letter
|
To check whether: x pass all the tests test1, test2, ... , testk do:
(1) Assume that x passes // Initial assumption
(2) For each test testi:
check if x passes the test testi
if x fails, then x did not pass all tests
|
Given a string x (e.g.: "level" or "abc")
x must pass these palindrome tests:
1st character = last character
2nd character = last-1 character
....
|
boolean isPalindrome = true; // Assume it is a palindrone
for ( i = 0; i < x.length(); i++ )
{
if ( character i in string x != character last - i in string x )
{
isPalindrome = false; // Fail: x is not a palindrome
}
}
// Unsolved problems: (1) how to find character i in string x
(2) how to find character last - i in string x
|
// Why last = length() - 1: "abcd" length() = 4 // index pos: 0123 // ^ last =3 = 4 - 1 = length() - 1 public static void main(String[] args) { Scanner input = new Scanner(System.in); String x = input.next(); int i, last; // Index of the characters that we are comparing last = x.length() - 1; boolean isPalindrome = true; // Initial assumption for ( i = 0; i < x.length(); i++ ) { if ( x.charAt( i ) != x.charAt( last-i ) ) { isPalindrome = false; // x is not a palindrome } } System.out.println(isPalindrome); } |
DEMO: demo/05-loops/08-alg-design/Palindrome.java
// Why last = length() - 1: "abcd" length() = 4 // index pos: 0123 // ^ last =3 = 4 - 1 = length() - 1 public static void main(String[] args) { Scanner input = new Scanner(System.in); String x = input.next(); int i, last; // Index of the characters that we are comparing last = x.length() - 1; boolean isPalindrome = true; // Initial assumption for ( i = 0; i < x.length()/2; i++ ) { if ( x.charAt( i ) != x.charAt( last-i ) ) { isPalindrome = false; // x is not a palindrome } } System.out.println(isPalindrome); } |
DEMO: demo/05-loops/08-alg-design/Palindrome2.java
// Why last = length() - 1: "abcd" length() = 4 // index pos: 0123 // ^ last =3 = 4 - 1 = length() - 1 public static void main(String[] args) { Scanner input = new Scanner(System.in); String x = input.next(); int i, last; // Index of the characters that we are comparing last = x.length() - 1; boolean isPalindrome = true; // Initial assumption for ( i = 0; i < x.length()/2; i++ ) { if ( x.charAt( i ) != x.charAt( last-i ) ) { isPalindrome = false; // x is not a palindrome break; // Skip the remaining tests.. } } System.out.println(isPalindrome); } |
DEMO: demo/05-loops/08-alg-design/Palindrome2b.java
|
How to check if a number is prime:
n = 41
Divide the number by: 2 3 4 .... 40 (= n-1)
|
To check whether: n pass the tests (test 1, test 2, ... , test k)
(1) Assume that n passes // Initial assumption
(2) For each test test i:
check if n passes the test test i
if n fails, then n did not pass all tests
|
Given integer n
n must pass these tests:
n%2 != 0
n%3 != 0
....
n%(n-1) != 0
|
Java program to check if a number n is a prime number:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n = input.nextInt();
boolean isPrime = true;
int i;
for ( i = 2; i < n; i++ )
{
if ( n%i == 0 )
{
isPrime = false;
}
}
System.out.println(isPrime);
}
|
DEMO: demo/05-loops/08-alg-design/IsPrime.java
Improvement: if some test failed, we can skip the remaining tests...
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n = input.nextInt();
boolean isPrime = true;
int i;
for ( i = 2; i < n; i++ )
{
if ( n%i == 0 )
{
isPrime = false;
break;
}
}
System.out.println(isPrime);
}
|
DEMO: demo/05-loops/08-alg-design/IsPrime2.java (step demo with an even number !)