Last active
August 29, 2015 14:07
-
-
Save anupamkumar/59d54c5e58b05c40e62e to your computer and use it in GitHub Desktop.
dailyprogrammer challenge 182
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
package dailyprogrammer; | |
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
/** | |
* solution to challenge : http://www.reddit.com/r/dailyprogrammer/comments/2hssx6/29092014_challenge_182_easy_the_column_conundrum/ | |
* @author anupam | |
*/ | |
public class Ch182 { | |
static String format=""; | |
static char[] content=null; | |
static int formatCols,formatCharsPerCols, formatSpaceBetCols; | |
public static void main(String[] args) throws IOException { | |
//change file to point to yours. | |
String s = openFile("D:\\learn python the hard way\\c182e-input.txt"); | |
content = s.toCharArray(); | |
int sl = s.length(); | |
formatCols = Integer.parseInt(format.split(" ")[0]); | |
formatCharsPerCols = Integer.parseInt(format.split(" ")[1]); | |
formatSpaceBetCols = Integer.parseInt(format.split(" ")[2]); | |
print(); | |
System.out.println("---------\nExtension\n---------"); | |
printExtended(s); | |
} | |
static void printExtended(String s) { | |
//split into words | |
String[] sa = s.split(" "); | |
ArrayList<String> validLines = new ArrayList<>(); | |
int charLen=0; | |
StringBuilder validLine = new StringBuilder(); | |
//try to form valid columns O(n) | |
for(int i=0;i<sa.length;i++) { | |
//check if line can take the word and space without spilling out of the column character limit | |
if((charLen+sa[i].length()+1) < formatCharsPerCols) { | |
validLine.append(sa[i]); | |
validLine.append(" "); | |
charLen = charLen + sa[i].length()+1; | |
} | |
//check if line can take just the word | |
else if((charLen+sa[i].length()) == formatCharsPerCols) { | |
validLine.append(sa[i]); | |
charLen = charLen + sa[i].length(); | |
} | |
//if the word causing spillage. Take the word to the next line | |
else { | |
int curLen=validLine.length(); | |
for(int x=curLen;x<formatCharsPerCols;x++) { | |
validLine.append(" "); | |
} | |
validLines.add(validLine.toString()); | |
validLine = new StringBuilder(); | |
charLen=0; | |
i--; | |
} | |
} | |
validLines.add(validLine.toString()); | |
//at this point we have rewritten the content into valid size sentences | |
//now to split the array list evenly into the columns... | |
//first see how many lines are necessary per column... | |
int linesPerCol = (int) Math.ceil(validLines.size()/(double)formatCols); | |
//create a 2d array of strings and fill it with the 1d arraylist's strings ... basically rearranging for printing | |
//eg: for a 4 column printing rearrange the list of 354 valid sentences to a 89x4 grid of sentences. | |
String[][] colLines = new String[linesPerCol][formatCols]; | |
int col=0,i=0; | |
for(String line : validLines) { | |
if(i==linesPerCol) { | |
i=0; | |
col++; | |
} | |
colLines[i][col]=line; | |
i++; | |
} | |
//print out the created string grid horizontally | |
for(i=0;i<linesPerCol;i++) { | |
StringBuilder line = new StringBuilder(); | |
for(int j=0;j<formatCols;j++) { | |
if(colLines[i][j]==null) | |
continue; | |
line.append(colLines[i][j]); | |
for(int k=0;k<formatSpaceBetCols;k++) { | |
line.append(" "); | |
} | |
} | |
System.out.println(line.toString()); | |
} | |
} | |
static void print() { | |
float linespercol = content.length / (float) formatCols; | |
linespercol = linespercol / formatCharsPerCols; | |
int charIndex=0; | |
ArrayList<ArrayList<String>> formattedLines = new ArrayList<>(); | |
for(int i=0;i<formatCols;i++) { | |
ArrayList<String> lines = new ArrayList<>(); | |
for(int j=0;j<linespercol;j++) { | |
StringBuilder sb = new StringBuilder(); | |
for(int k=0;k<25;k++) { | |
if(charIndex >= content.length) { | |
break; | |
} | |
else { | |
sb.append(content[charIndex++]); | |
} | |
} | |
lines.add(sb.toString()); | |
} | |
formattedLines.add(lines); | |
} | |
for(int i=0;i<linespercol;i++) { | |
StringBuilder line = new StringBuilder(); | |
for(ArrayList<String> f : formattedLines) { | |
line.append(f.get(i)); | |
for(int j=0;j<formatSpaceBetCols;j++) { | |
line.append(" "); | |
} | |
} | |
System.out.println(line.toString()); | |
} | |
} | |
static String openFile(String filename) throws IOException { | |
BufferedReader br = new BufferedReader(new FileReader(filename)); | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
format = br.readLine(); | |
while((line = br.readLine())!= null) { | |
sb.append(line); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment