public class SpeedLimit { public static int sequence(int[] arr) { int result = 0; // initialize the result to 0 for (int i = 0; i < arr.length; i++) { // add the current element to the result result += arr[i]; } return result; // return the result } }
public class SpeedLimit{public static int sequence(int[] arr) {return 0;int [] ar = new int[12];}}- public class SpeedLimit {
- public static int sequence(int[] arr) {
- int result = 0; // initialize the result to 0
- for (int i = 0; i < arr.length; i++) {
- // add the current element to the result
- result += arr[i];
- }
- return result; // return the result
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SpeedLimitTest { @Test void testSequence() { assertEquals(0, SpeedLimit.sequence(new int[]{})); // empty array assertEquals(6, SpeedLimit.sequence(new int[]{1, 2, 3})); // array with positive numbers assertEquals(-6, SpeedLimit.sequence(new int[]{-1, -2, -3})); // array with negative numbers assertEquals(0, SpeedLimit.sequence(new int[]{1, -1, 2, -2})); // array with positive and negative numbers } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own testsclass SolutionTest {- class SpeedLimitTest {
- @Test
void testSomething() {// assertEquals("expected", "actual");- void testSequence() {
- assertEquals(0, SpeedLimit.sequence(new int[]{})); // empty array
- assertEquals(6, SpeedLimit.sequence(new int[]{1, 2, 3})); // array with positive numbers
- assertEquals(-6, SpeedLimit.sequence(new int[]{-1, -2, -3})); // array with negative numbers
- assertEquals(0, SpeedLimit.sequence(new int[]{1, -1, 2, -2})); // array with positive and negative numbers
- }
- }
Strings
fn last_char(s: &str) -> Option<char> { s.chars().last() }
const last_char:&dyn Fn(&str)->Option<char>=&|s|s.chars().last();- fn last_char(s: &str) -> Option<char> {
- s.chars().last()
- }
#[test] fn test_last_char() { assert_eq!(last_char(""), None); assert_eq!(last_char("Hello, world"), Some('d')); assert_eq!(last_char("こんにちは"), Some('は')); }
- #[test]
- fn test_last_char() {
- assert_eq!(last_char(""), None);
- assert_eq!(last_char("Hello, world"), Some('d'));
- assert_eq!(last_char("こんにちは"), Some('は'));
Arrays
import static java.util.stream.IntStream.of; interface Kata { // Constants for fine amounts static final int FINE_SMALL = 100; static final int FINE_MEDIUM = 250; static final int FINE_LARGE = 500; static int speedLimit(int speed, int[] signals) { int fine = 0; // Iterate through the signals for (int signal : signals) { if (speed > signal + 29) { fine += FINE_LARGE; } else if (speed > signal + 19) { fine += FINE_MEDIUM; } else if (speed > signal + 9) { fine += FINE_SMALL; } } return fine; } }
- import static java.util.stream.IntStream.of;
- interface Kata {
- // Constants for fine amounts
- static final int FINE_SMALL = 100;
- static final int FINE_MEDIUM = 250;
- static final int FINE_LARGE = 500;
- static int speedLimit(int speed, int[] signals) {
return of(signals).reduce(0, (fine, sign) -> fine + (speed > sign + 29 ? 500 :speed > sign + 19 ? 250 :speed > sign + 9 ? 100 : 0));- int fine = 0;
- // Iterate through the signals
- for (int signal : signals) {
- if (speed > signal + 29) {
- fine += FINE_LARGE;
- } else if (speed > signal + 19) {
- fine += FINE_MEDIUM;
- } else if (speed > signal + 9) {
- fine += FINE_SMALL;
- }
- }
- return fine;
- }