Given a string of digits and a number n String = 500055 n = 2
then output -> 510352 explanation: here I'm counting the frequency of consecutive digits and storing them. in 500055,
5 is occurring consecutively 1 time --- (51)
0 is occurring consecutively 3 times --- (5103)
5 is occurring consecutively 2 times --- (510352)
example 2:
String = 500055
n = 3
500055 -> 510352 -> 511101315121
(basically applying the same function again on n=2)
example 3:
String = 500055
n = 4
500055 -> 510352 -> 511101315121 -> 51130111311151112111
example 4:
String = 500055
n = 1
output 500055
Is there any way to solve this in one pass?
like: String = 500055 n = 4
500055 -> 51130111311151112111
CodePudding user response:
Sounds like you are wondering if there is some predictive formula to use. Since the actual digits change based on previous constructions, I would think it unlikely (at least not straight mathematically - but I could be wrong). In any event, this was certainly faster to derive (for me) and you don't need to explicitly count anything. Just use a regex
and the String.length
method.
String start = "500055";
for (int n = 0; n < 4; n ) {
String s = consecutiveCount(start, n);
System.out.printf("n = %d : %s -> %s%n", n, start, s);
}
prints
n = 0 : 500055 -> 500055
n = 1 : 500055 -> 510352
n = 2 : 500055 -> 511101315121
n = 3 : 500055 -> 51130111311151112111
The method.
(\\d)\\1*
- capture a digit followed by 0 or more of the same digit (using a back reference togroup1
.- concatentate the character in
group1
followed by the length of the entire match (group0
). - repeat until
find()
returns false - then create a new
matcher
for the new string. - set the string to empty.
- and continue the next iteration.
final static String REGEX = "(\\d)\\1*";
final static Pattern PATTERN = Pattern.compile(REGEX);
public static String consecutiveCount(String str, int iterations) {
while (iterations-- > 0) {
Matcher m = PATTERN.matcher(str);
str = "";
while (m.find()) {
str = m.group(1) ""
m.group(0).length();
}
}
return str;
}
Note: As mentioned in my comment, you may need to alter either your or my interpretation of n
CodePudding user response:
You can use streams
for that job:
public static String frequency(String digits, int n) {
return n < 2 ? digits : frequency(frequency(digits), n - 1);
}
public static String frequency(String digits) {
return IntStream.range(0, digits.length())
.filter(i -> i == digits.length() - 1 || digits.charAt(i) != digits.charAt(i 1))
.mapToObj(i -> String.valueOf(digits.charAt(i))
(i == 0 ? i 1 : IntStream.range(0, i)
.filter(j -> digits.substring(j, i).chars()
.allMatch(c -> c == digits.charAt(i)))
.count() 1))
.collect(Collectors.joining());
}
Then:
String digits = "500055";
System.out.println("n = 1: " frequency(digits, 1));
System.out.println("n = 2: " frequency(digits, 2));
System.out.println("n = 3: " frequency(digits, 3));
System.out.println("n = 4: " frequency(digits, 4));
Output:
n = 1: 500055
n = 2: 510352
n = 3: 511101315121
n = 4: 51130111311151112111
CodePudding user response:
you can use this code javascript countdown 10 seconds:
var timeleft = 10;
var downloadTimer = setInterval(function(){
if(timeleft <= 0){
clearInterval(downloadTimer);
}
document.getElementById("progressBar").value = 10 - timeleft;
timeleft -= 1;
}, 1000);
<progress value="0" max="10" id="progressBar"></progress>
countdown in javascript :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Countdown timer using HTML and JavaScript</title>
</head>
<body>
Registration closes in <span id="timer">05:00<span> minutes!
<!-- custom js -->
<script>
window.onload = function () {
var minute = 5;
var sec = 60;
setInterval(function () {
document.getElementById("timer").innerHTML =
minute " : " sec;
sec--;
if (sec == 00) {
minute--;
sec = 60;
if (minute == 0) {
minute = 5;
}
}
}, 1000);
};
</script>
</body>
</html>
countdown in js:
<body>
<div>Registration closes in <span id="time">05:00</span> minutes!</div>
</body>