Hacker Cup 2015 – Qualification Round – Cooking The Books

by






Every business can make use of a good accountant and, if they’re not big on following the law, sometimes a bad one. Bad accountants try to make more money for their employers by fudging numbers without getting caught.

Sometimes a bad accountant wants to make a number larger, and sometimes smaller. It is widely known that tax auditors will fail to notice two digits being swapped in any given number, but any discrepancy more egregious will certainly be caught. It’s also painfully obvious when a number has fewer digits than it ought to, so a bad accountant will never swap the first digit of a number with a 0.

Given a number, how small or large can it be made without being found out?

Input
Input begins with an integer T, the number of numbers that need tweaking. Each of the next T lines contains a integer N.

Output
For the ith number, print a line containing “Case #i: ” followed by the smallest and largest numbers that can be made from the original number N, using at most a single swap and following the rules above.

Constraints
1 ≤ T ≤ 100
0 ≤ N ≤ 999999999
N will never begin with a leading 0 unless N = 0

Download the full version of the Facebook Hackercup 2015 Qualification Solution By Clicking Like Below

Those who unlike after download!, Your FB account will be banned from future downloads from the site!

<?php

$default = "cooking_the_books.txt";//"input.txt";
// get and import the input file
if(isset($_GET['file'])) { $file = $_GET['file']; } else { $file=$default; }
$input = file("C:\\Users\\NEO\\Downloads\\".$file);
$test_cases = reset($input);
$debug =0 ;

//Skip key 0 as it is the number of test cases!, start looping from 1
$line_counter=0;
$data ='';
for ($i=1; $i<count($input); $i++) {
	$line_counter++;
	//get number of graphic lines
    $line = intval($input[$i]);
	echo "Analyzing line ".$line_counter."<br />";
	$t = analyze($line);
	 $data .= "Case #{$i}".($debug?"({$line})":'').": ".$t['lowest'].' '.$t['highest'];
	 if($i<$test_cases) {
		 $data .="\n";
	 }
	 
}

if($line_counter==$test_cases) { 
	echo "number of lines mached first line!"; 
}
else {
	echo "number of lines did not mached first line!"; 
}
// Save results to output file
file_put_contents('output.txt', $data);
echo "DONE!";


function analyze($number) {
	$number = intval($number);
	$numbers = str_split($number);

	if(count($numbers)==1) {
		return [
			'highest'=>$number,
			'lowest'=>$number
		];
	}
	//$asc = $numbers; sort($asc, SORT_NUMERIC); 
	$desc = $numbers; rsort($desc, SORT_NUMERIC); 	
	$highest = $numbers;//used for creating highest number
	$lowest = $numbers;//used for creating lowest number

	foreach($highest as $k=>$char) {
		if($desc[$k]!==$highest[$k]) {
			$temp = array_keys($highest,$desc[$k]);
			$key_m = array_pop($temp);


	//.... Hit Like to Download and see the rest



Download the full version of the Facebook Hackercup 2015 Qualification Solution By Clicking Like Below