-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class Enigma{ | ||
public static void enigma(int x){ | ||
if(x == 0){ | ||
return; | ||
}else{ | ||
enigma(x/2); //recursively calling enigma | ||
} | ||
System.out.print(x%2); | ||
} | ||
public static void main(String[] args) { | ||
enigma(5); | ||
System.out.println(""); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import java.util.Scanner; | ||
|
||
public class Palindrome{ | ||
public static char first(String s){ | ||
return s.charAt(0); | ||
} | ||
|
||
public static char last(String s){ | ||
int length = s.length(); | ||
return s.charAt(length - 1); | ||
} | ||
|
||
public static String middle(String s){ | ||
int length = s.length(); | ||
return s.substring(1,length - 1); | ||
} | ||
|
||
public static boolean isPalindrome(String s) | ||
{ | ||
if(s.length() == 0 || s.length() == 1) | ||
return true;//if length =0 OR 1 then it is | ||
if(first(s) == last(s))//check for first and last char of String, if they are same then do the same thing for a substring with first and last char removed. and carry on this until you string completes or condition fails | ||
return isPalindrome(middle(s)); //if its not the case than string is not. | ||
return false; | ||
} | ||
|
||
public static boolean isPalindromeIter(String s){ | ||
int length = s.length(); //get the length of the string | ||
int mid = length/2; // get the middle point of the string | ||
if(length == 0 || length == 1) //return if the string is empty or 1 chracter | ||
return true; | ||
for(int i = 0; i < mid; i++){ | ||
if (s.charAt(i) != s.charAt(length-i-1)){ //iterate and return if the characters are not matching | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public static boolean isPalindromewhile(String s) { | ||
int left = 0; // index of leftmost unchecked char | ||
int right = s.length() -1; // index of the rightmost | ||
|
||
while (left < right) { // continue until they reach center | ||
if (s.charAt(left) != s.charAt(right)) { | ||
return false; // if chars are different, finished | ||
} | ||
left++; // move left index toward the center | ||
right--; // move right index toward the center | ||
} | ||
return true; // if finished, all chars were same | ||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
Scanner scan = new Scanner(System.in); | ||
String word = scan.nextLine(); | ||
System.out.println(isPalindromeIter("word")); | ||
System.out.println(isPalindromewhile("word")); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
public class abecedarian { | ||
public static boolean isAbecedarian(String s) { | ||
int index = 0; | ||
char c = 'a'; | ||
while (index < s.length()) { | ||
if (c > s.charAt(index)) { | ||
return false; | ||
} | ||
c = s.charAt(index); | ||
index = index + 1; | ||
|
||
} | ||
return true; | ||
} | ||
|
||
public static boolean isAbecedarianrec(String s){ | ||
char first = s.charAt(0); | ||
char second = first ++; | ||
System.out.println(first); | ||
System.out.println(second); | ||
if (s.charAt(0)<second){ | ||
return isAbecedarianrec(s); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static void main(String[] args) { | ||
//System.out.println(isAbecedarian("abcde")); | ||
System.out.println(isAbecedarianrec("abcde")); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class reverseDigit{ | ||
public static void main(String[] args) { | ||
reversedigit(81); | ||
} | ||
public static void reversedigit(int n){ | ||
int lastDigit = n%10; | ||
int firstDigit = n/10; | ||
System.out.print(lastDigit); | ||
System.out.println(firstDigit); | ||
} | ||
} |