Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 34 additions & 41 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions src/main/java/interview/MainClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package interview;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class MainClass {

private static final String IN_FILE_PATH = "src/main/resources/input-data-file.txt";
private static TidyNumbers tidy = new TidyNumbers();
public static void main(String[] args) {
System.out.println("Testing one, two, three");

FileInputStream file = null;
Scanner in = null;
FileWriter fw = null;
try {
file = new FileInputStream(IN_FILE_PATH);
in = new Scanner(file);
int t = in.nextInt();
for (int i = 1; i <= t; i++) {
int n = in.nextInt();
int result = tidy.getTidyNumber(n);
System.out.println(result);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
assert in != null;
in.close();
assert file != null;
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/interview/ProblemStatement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Problem
Tatiana likes to keep things tidy. Her toys are sorted from smallest to largest,
her pencils are sorted from shortest to longest and her computers from oldest to newest.
One day, when practicing her counting skills, she noticed that some integers, when written in base 10 with no leading zeroes, have their digits sorted in non-decreasing order. Some examples of this are 8, 123, 555, and 224488. She decided to call these numbers tidy. Numbers that do not have this property, like 20, 321, 495 and 999990, are not tidy.
She just finished counting all positive integers in ascending order from 1 to N. What was the last tidy number she counted?

## Input
The first line of the input gives the number of test cases, T. T lines follow. Each line describes a test case with a single integer N, the last number counted by Tatiana.

## Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the last tidy number counted by Tatiana.

## Limits
1 ≤ T ≤ 100.

1 ≤ N ≤ 1000.

### Example 1
Input: 132

Output: 129

### Example 2
Input: 1000

Output: 999

### Example 3
Input: 7

Output: 7
30 changes: 30 additions & 0 deletions src/main/java/interview/TidyNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package interview;

public class TidyNumbers {

public int getTidyNumber(int n) {
int i = 0;
return lastTidyNumber(i, n);
}
public int lastTidyNumber(int i, long n) {
String ns = String.valueOf(n);
if (isTidy(ns)) return (int)n;

int lastDigit = Integer.parseInt(String.valueOf(ns.charAt(ns.length() - 1 - i))) + 1;

long subs = (long) Math.pow(10, i) * lastDigit;
i++;
return lastTidyNumber(i, n - subs);
}

private boolean isTidy(String ns) {
int max = Integer.parseInt(String.valueOf(ns.charAt(0)));

for (int i = 1; i < ns.length(); i++) {
int c = Integer.parseInt(String.valueOf(ns.charAt(i)));
if (c < max) return false;
else max = c;
}
return true;
}
}
101 changes: 101 additions & 0 deletions src/main/resources/input-data-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
100
132
1000
7
700
154
709
181
735
441
424
364
649
925
87
692
177
397
375
342
596
768
107
967
825
393
502
999
372
155
826
383
217
173
946
212
124
560
484
924
232
264
223
980
891
179
103
636
564
244
1
117
199
453
56
814
553
27
460
433
43
34
787
879
828
9
146
328
296
792
948
498
110
357
135
584
561
903
938
982
954
638
425
922
894
827
675
930
994
757
191
704
626
469
764
303
239
653
869
932
557
30 changes: 30 additions & 0 deletions src/test/java/interview/TidyNumbersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package interview;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class TidyNumbersTest {

private TidyNumbers coda;
private List<Integer> testcases;

@BeforeEach
void setUp() {
coda = new TidyNumbers();
testcases = List.of(132, 1000, 7);
}

@Test
void lastTidyNumber() {
List<Integer> expectedResult = List.of(129, 999, 7);

for (int i = 0; i < testcases.size(); i++) {
int actualResult = coda.getTidyNumber(testcases.get(i));
assertEquals(expectedResult.get(i), actualResult);
}
}
}