import java.io.File; import java.util.Date; import java.util.ArrayList; public class Day10 { public static int gcd( int m, int n ) { if( n == 0 ) return m; return gcd( n, m % n ); } /* * This routine makes lots of extra strings but is otherwise sound. */ /* public static boolean isPalindrome( String str ) { if( str.length( ) <= 1 ) return true; if( str.charAt( 0 ) != str.charAt( str.length( ) -1 ) ) return false; return isPalindrome( str.substring( 1, str.length( ) - 1 ) ); } * */ /** * @param str string to check * @param beginIdx first index in the view * @param endIdx last index in the view * @return true if str[beginIdx..endIdx] is a palindrome */ // Private recursive routine with a few extra parameters private static boolean isPalindrome( String str, int beginIdx, int endIdx ) { if( endIdx - beginIdx <= 0 ) // view of length 1 or empty view return true; if( str.charAt( beginIdx ) != str.charAt( endIdx ) ) return false; return isPalindrome( str, beginIdx + 1, endIdx - 1 ); } // Driver routine: for public use public static boolean isPalindrome( String str ) { return isPalindrome( str, 0, str.length( ) - 1 ); } public static long totalSize( File d ) { if( !d.isDirectory( ) ) return d.length( ); long total = 0; for( File f : d.listFiles( ) ) total += totalSize( f ); return total; } public static void listDir( String dirName ) { File d = new File( dirName ); ArrayList subDirectories = new ArrayList( ); System.out.println( ); System.out.println( "Directory of " + d.getAbsolutePath( ) ); System.out.println( ); for( File f : d.listFiles( ) ) { boolean isDirectory = false; System.out.print( new Date( f.lastModified( ) ) + " " ); isDirectory = f.isDirectory( ); if( isDirectory ) System.out.print( " " ); else System.out.print( " " ); if( isDirectory ) System.out.print( " " ); else System.out.print( String.format( "%10d", f.length( ) ) ); if( isDirectory ) subDirectories.add( f.getAbsolutePath( ) ); // add in f System.out.print( " " + f.getName( ) ); System.out.println( ); } for( String s : subDirectories ) listDir( s ); } public static void main( String [ ] args ) { // System.out.println( gcd( 750000, 4500 ) ); // System.out.println( isPalindrome( "hannah" ) ); // System.out.println( isPalindrome( "abcba" ) ); // System.out.println( isPalindrome( "abcca" ) ); // System.out.println( isPalindrome( "hanneh" ) ); final String DIR = "."; listDir( DIR ); System.out.println( "TOTAL BYTES OF ALL FILES " + totalSize( new File( DIR ) ) ); } }