about | help | code help+videos | done | prefs |
apcsaLoopsFindPattern
Given the start and end of a range, an increment and a String pattern of digits, check every number from start to end (but going up by increment, not just by one) and return the first number between start and end, which includes the digits of pattern. If no such number is found, return -1. Preconditions: 0 < start < end, increment > 1, pattern is only digits. Example: suppose start is 3, end is 1000, increment is 3, pattern is "02". The numbers to check are 3,6,9,12,...,99,102,104,...,999. Because 102 ends in 02, return 102. HINTS: the first three parameters are intended to structure a for loop header. Checking whether a number "includes the digits of pattern" can be done easily, though not efficiently, by using Strings. For example, the String class has a method called contains which could be useful. https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence) https://beginnersbook.com/2017/10/java-string-contains-method/apcsaLoopsFindPattern(3, 1000, 3, "02") → 102 apcsaLoopsFindPattern(3, 1000, 3, "011") → -1 apcsaLoopsFindPattern(104, 205, 2, "7") → 170 ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 250
Copyright Nick Parlante 2017 - privacy