how to repeat a string n times in ruby
30.times {
# do something
}
# or, the equivalent
30.times do
# do something
end
how to repeat a string n times in ruby
30.times {
# do something
}
# or, the equivalent
30.times do
# do something
end
repeaded string explained in ruby
function repeatedString($s, $n) {
// check the number of occurrences of letter 'a' on given string $s
$a_occurrences_in_s = substr_count($s, 'a');
// check how many occurrences of string $s belongs to an repeated string $s of length of $n
$s_occurrences_qty = floor($n / strlen($s));
// get the $s occurrences left
$s_occurrences_left = $n - ($s_occurrences_qty * strlen($s));
// left string
$s_left = substr($s, 0, $s_occurrences_left);
// 'a' occurrences in left string
$a_occurrences_in_s_left = substr_count($s_left, 'a');
// occurrencies of 'a' on the entire repeated string of $s with a length of $n
return ($a_occurrences_in_s * $s_occurrences_qty) + $a_occurrences_in_s_left;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us