Two Homework Problems
August 21, 2015
I can see from my statistics that the new academic year is beginning. Again, as in a previous exercise, in the spirit of helping programming students who are just starting a new school year, we have two typical homework problems:
1. Given an array of positive integers, find the inflection point where the total of the integers before the inflection point and the total of the integers after the inflection point are least different. For instance, given the array [3, 7, 9, 8, 2, 5, 6], the inflection point is between the 9 and 8, which leaves a total of 19 before the inflection point and 21 after, a difference of 2.
2. Write a program that reads a file from disk and writes the last n lines of the file, where n is an input parameter.
Your task is to write programs to solve these problems. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
1st
2nd
tail -5 a_file.txt
To compare with the last-lines function that reads the whole file first:
Exercise to the reader: there’s an obvious bug in big-file-last-lines ;-)
Jura gur ovt svyr fgvyy pbagnvaf srjre yvarf guna A (vg pbhyq pbagnva whfg 1 irel ybat yvar), gura gur SEBZ cbfvgvba pbhyq orpbzr artngvir naq YVAR-PBHAG nyjnlf or yrff A. Jr zhfg hfr SYBBE vafgrnq bs GEHAPNGR va EBHAQ-SVYR-CBFVGVBA gb qrny cebcreyl jvgu artngvir cbfvgvbaf, naq grfg sbe gurz va gur ybbc pnyyvat PBHAG-YVARF.
For the second problem, if we are supplied with an actual file, why read it at all? Let’s just memory map it and look for line ends backwards from the end. Error checking etc. omitted for brevity:
Scala:
fun inflection_point [] = raise Domain
| inflection_point (R as x::xs) = let
val sum = foldl (op +) 0 R
fun inflection_point_aux [] difference current_index diff_index prev_sum sum = (difference, diff_index)
| inflection_point_aux (x::xs) difference current_index diff_index prev_sum sum = let
val left = x + prev_sum
val right = sum – x
val new_difference = Int.abs(left-right)
in
if new_difference < difference then inflection_point_aux xs new_difference (current_index + 1) current_index left right
else inflection_point_aux xs difference (current_index + 1) diff_index left right
end
in
inflection_point_aux R sum 0 0 0 sum
end
> use "inflection_point.sml";
val inflection_point = fn : Int.int list -> Int.int * int
val it = () : unit
> inflection_point [3, 7, 9, 8, 2, 5, 6];
val it = (2, 2) : Int.int * int
> inflection_point [1,2,3,4,5,15];
val it = (0, 4) : Int.int * int
Seems like every third submission ignores my sourcecode tags:
1st Problem:
public class InflectionPoint {
public static int point(int a[])
{
int diff;
int sums =0;
int suml = 0;
for(int s=0,l = a.length-1;s =s;)
{
if(sums suml)
{
diff = sums-suml;
}else diff = suml – sums;
return diff;
}
public static void main(String[] args) {
int arr[] = {3, 7, 9, 8, 2, 5, 6};
//Random r = new Random();
for(int i:arr)
{
// i = r.nextInt(50);
System.out.println(i);
}
System.out.println(“The infleation point is:” + point(arr) );
}
}
2nd Problem:
public class FewLines {
public static int countLines(String fileName) throws FileNotFoundException, IOException
{
int count = 0;
String lines = “”;
LineNumberReader reader = new LineNumberReader(new FileReader(fileName));
while((lines = reader.readLine())!=null){
reader.skip(Long.MAX_VALUE);
count = reader.getLineNumber();
}
reader.close();
return count;
}
public static void main(String[] args) throws IOException {
int n;
int i = 0;
String fileName = “Find.txt”;
String lines = “”;
LineNumberReader reader = new LineNumberReader(new FileReader(fileName));
n = countLines(“Find.txt”);
System.out.println(“Total number of lines is:” + n);
Scanner s =new Scanner(System.in);
System.out.println(“On which line you want to read File:”);
i = s.nextInt();
while(reader.getLineNumber()!= i)
{
lines = reader.readLine();
}
while(reader.getLineNumber() == i)
{
lines = reader.readLine();
System.out.println(lines);
}
}
}