Skip to content

Instantly share code, notes, and snippets.

@GeorgiKarapetrov
Last active October 14, 2019 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GeorgiKarapetrov/27b7fa91ffdd167718dcad103990d7fc to your computer and use it in GitHub Desktop.
Save GeorgiKarapetrov/27b7fa91ffdd167718dcad103990d7fc to your computer and use it in GitHub Desktop.
101 with Python, entrance exam, round 1

#https://github.com/HackBulgaria/Programming-101-Python-2019/blob/master/Application/2.Intervals/README.md

Intervals

You are presented with a bunch of lines with integers on each line:

-181
-414
441
889
-547
-313
622
679
782
-640

The integers can be positive and negative.

The task

In a language of your choice, implement a program, that reads those lines from the standard input (stdin), and outputs all consecutive intervals of the numbers, in ascending order.

For example, for the input:

-181
-414
441
889
-547
-313
622
679
782
-640

The output should be:

[-640, -640] with consecutive count of 1
[-547, -547] with consecutive count of 1
[-414, -414] with consecutive count of 1
[-313, -313] with consecutive count of 1
[-181, -181] with consecutive count of 1
[441, 441] with consecutive count of 1
[622, 622] with consecutive count of 1
[679, 679] with consecutive count of 1
[782, 782] with consecutive count of 1
[889, 889] with consecutive count of 1

Another example, for the input:

123
567
124
568
-100
-99

The output should be:

[-100, -99] with consecutive count of 2
[123, 124] with consecutive count of 2
[567, 568] with consecutive count of 2

The output should end with a newline.

The tests

Alongside the task, add tests, that proove your solution is correct. This is entirely up to you.

/*
* This program lists in ascending order the consecute intervals between any set of integer generated by a user.
*
* N.B. No mechanisms have been implemented to safeguard the user from missusing the program.
* This can be done with some conditional statements in the prompt method and reprompting the user to correct the input, as well as try/catch clauses.
*
* N.B. It is not clear what to do when the user inputs an odd number of integers.
* While a number of approaches resolve this issue, I decided to treat the largest number to appear twice.
* In other word, input:
* 1
* 2
* 3
* Would generate output:
* [1, 2] with consecutive count of 2
* [3, 3] with consecutive count of 1
*
* N.B. What's more, nothing in the condition seems to indicate what "with consecutive count of 1" is supposed to compute.
* I took this to be the lenght of the interval + 1 as this is a reasonable generalisation of the example outputs.
* I implemented this computation as is, since it is consistent through out the examples.
*
* To reiterate, one reason the problem may be confusing to some is
* the inconsistency between the general condition and the example inputs,
* and then yet again - between the examples themselves.
* The first example illustrating how the program should work
* takes each input integer i and outputs the interval [i, i].
* Only the second example satisfies the condition,
* even though it computes the additinal " j - i + 1 " for each interval [i, j],
* instead of the interval lenght " j - i ".
*/
package intervals;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Georgi Karapetrov
*/
public class Intervals
{
//store all integers in a list
private static ArrayList<Integer> points;
//constructor
public Intervals()
{
points = new ArrayList<Integer>();
}
//returns the list
private ArrayList<Integer> getPonts()
{
return points;
}
//promt, and storing the keys and summing over the values
public static void prompt()
{
Scanner input = new Scanner( System.in ); // scanner for standart input
String line = ""; // initialize a string to store input line by line
//prompt
System.out.print( "This program outputs in ascending order the consecutive intervals between integers.\n"
+ "Enter the integers one by one each on it's onw line like this:\n"
+ "1\n"
+ "2\n"
+ "3\n"
+ "Enter EXIT if you are finished.\n" );
//store the integers
while (input.hasNext())
{
line = input.nextLine(); //store line
//break if terminated by user
if (line.toLowerCase().contains("exit"))
{
break;
}
//add each integer to the list
points.add( Integer.parseInt(line) );
}
}
public static void printResults()
{
int lengthPlusOne; //the condition seems to require us to print one longer than the lenght of the interval, for each interval
//handle an even number of points
if (points.size() % 2 == 0)
{
//word wrapping
for( int i=0; i < points.size(); i = i + 2 )
{
lengthPlusOne = points.get(i+1) - points.get(i);
System.out.println( "[" + points.get(i) + ", " + points.get(i+1) + "] with consecutive count of " + lengthPlusOne );
}
}
//handle odd number of points
else
{
//word wrapping
for( int i=0; i < points.size(); i = i + 2 )
{
if( i < points.size() - 1 )
{
lengthPlusOne = points.get(i+1) - points.get(i);
System.out.println( "[" + points.get(i) + ", " + points.get(i+1) + "] with consecutive count of " + lengthPlusOne );
}
//consider the odd point out to be its own interval
else
{
System.out.println( "[" + points.get(i) + ", " + points.get(i) + "] with consecutive count of 1" );
}
}
}
System.out.println( );
}
//test the computetional methods
/*
* This test is no proof that the program always halts or always works as intended.
* Neither is this test able to test the prompt() method from the main file as we cannot simulate stdin.
* However as many times as I have run this test, the tested methods run as intended.
* Also as many times as I ran the main program, it works as long as you keep the input format (int + "\n")
*/
public static void generatePoints()
{
Random integerGenerator = new Random(); //generate random integers
int integer; //store the random number so we can both print it and use it in testing
//add nineteen numbers to the list and print them (nineteen for certainty, though any other number would work.)
for(int i=1; i <= 19; ++i)
{
integer = integerGenerator.nextInt(); //store the random number so we can both print it and use it in testing
points.add( integer );
System.out.println( integer );
}
System.out.println("Output:" );
}
public static void main(String[] args)
{
Intervals example = new Intervals();
//call prompt (comment out the following line if you want to test automatically
example.prompt();
//or generate points automatically (uncomment the following line if you want to test automatically)
//example.generatePoints();
//compute the three highest values
Collections.sort( example.getPonts() );
//print result
example.printResults();
} // end method main
} // end class Keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment