id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

orion.a.smith@gmail.com apcsa-loops > apcsaLoopsFindPattern
prev  |  next  |  chance

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)

public int apcsaLoopsFindPattern(int start, int end, int increment, String pattern) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 250

Copyright Nick Parlante 2017 - privacy