Ad
Code
Diff
  • use itertools::Itertools;
    
    fn sort_desc(number: u64) -> u64 {
        number.to_string().chars().sorted().rev().collect::<String>().parse().unwrap()
    }
    • use itertools::Itertools;
    • fn print(number: u64) -> u64 {
    • fn sort_desc(number: u64) -> u64 {
    • number.to_string().chars().sorted().rev().collect::<String>().parse().unwrap()
    • }
Algorithms
Arrays
Mathematics
Geometry
Strings
Algorithms
Data Structures
Simulation
Logic
Games
Date Time
Code
Diff
  • use std::collections::HashMap;
    
    fn time_parse(time: &str) -> f64 {
        let (h, m) = time.split_once(':').unwrap();
        h.parse::<f64>().unwrap() * 60. + m.parse::<f64>().unwrap()
    }
    fn from_min_to_string(min: f64) -> String {
        let h = (min / 60.).floor() % 24.;
        let m = (min % 60.).floor();
        format!("{:02}:{:02}", h, m)
    }
    
    const BUS_SCHEDULES: &str = r#"Skyport	Crystalium	15 min	2 hours
    Crystalium	Skyport	10 min	2 hours
    Skyport	Oasis	20 min	3 hours
    Oasis	Skyport	15 min	3 hours
    Oasis	Crystalium	15 min	1.5 hours
    Crystalium	Oasis	10 min	1.5 hours
    Skyport	Nexus	15 min	4 hours
    Nexus	Skyport	10 min	4 hours
    "#;
    
    fn arrival_time(route: Vec<&str>, departure_time: &str) -> String {
        let origin: Vec<&str> = vec!("Skyport");
        if route.is_empty() {
            return "The Train Driver has the day off".to_string();
        } else {
            let from = 0;
            let to = 1;
            let standby_time = 2;
            let travel_time = 4;
    
            let mut bus: HashMap<(String, String), (String, String)> = HashMap::new();
            for line in BUS_SCHEDULES.lines() {
                let data = line.split_whitespace().collect::<Vec<_>>();
                let bus_stop = (data[from].to_string(), data[to].to_string());
                let data_stop = (
                    data[standby_time].to_string(),
                    data[travel_time].to_string(),
                );
                bus.insert(bus_stop, data_stop);
            }
            let mut total_travel_time = 0.0;
    
            let full_route: Vec<&&str> = origin.iter().chain(route.iter()).collect::<Vec<_>>();
    
            for item in full_route.windows(2) {
                if item[0] == item[1] {
                    total_travel_time += 1. * 60.;
                } else {
                    let bus_stop = (item[0].to_string(), item[1].to_string());
                    match bus.get(&bus_stop) {
                        Some((waiting_time, travel_time)) => {
                            total_travel_time += travel_time.parse::<f64>().unwrap() * 60.
                                + waiting_time.parse::<f64>().unwrap();
                        }
                        None => {
                            let back_to_origin = (item[0].to_string(), origin[0].to_string());
                            let from_origin_to_route = (origin[0].to_string(), item[1].to_string());
                            match bus.get(&back_to_origin) {
                                Some((waiting_time, travel_time)) => {
                                  
                                    total_travel_time += travel_time.parse::<f64>().unwrap() * 60.
                                        + waiting_time.parse::<f64>().unwrap();
                                }
                                None => {
                                    panic!("Could not find route between {} and Skyport", item[0]);
                                }
                            }
                            match bus.get(&from_origin_to_route) {
                                Some((waiting_time, travel_time)) => {
                                    total_travel_time += travel_time.parse::<f64>().unwrap() * 60.
                                        + waiting_time.parse::<f64>().unwrap();
                                }
                                None => {
                                    panic!("Could not find route between Skyport and {}", item[1]);
                                }
                            }
                        }
                    }
                }
            }
            from_min_to_string(total_travel_time + time_parse(departure_time))
        }
    }
    
    • import java.time.LocalTime;
    • import java.time.format.DateTimeFormatter;
    • import java.util.HashMap;
    • import java.util.Map;
    • public class Kata {
    • public static String arrivalTime(final String[] route, final String departureTime) {
    • if (route.length == 0) return "The Train Driver has the day off";
    • LocalTime timeResult = LocalTime.parse(departureTime, DateTimeFormatter.ofPattern("HH:mm"));
    • String origin = "Skyport";
    • int index = 0;
    • while (index < route.length) {
    • String destination = route[index];
    • Map<String, double[]> destinationMap = tableTime.get(origin);
    • if (destinationMap != null && destinationMap.containsKey(destination)) {
    • double[] times = destinationMap.get(destination);
    • timeResult = calculateTime(timeResult, times);
    • origin = destination;
    • index++;
    • } else if (origin.equals(destination)) {
    • timeResult = timeResult.plusHours(1);
    • index++;
    • } else {
    • String transfer = "Skyport";
    • double[] times = tableTime.get(origin).get(transfer);
    • timeResult = calculateTime(timeResult, times);
    • origin = transfer;
    • }
    • }
    • return timeResult.format(DateTimeFormatter.ofPattern("HH:mm"));
    • }
    • private static Map<String, Map<String, double[]>> tableTime = new HashMap<String, Map<String, double[]>>() {
    • {
    • addRecord("Skyport", "Crystalium", 15, 2);
    • addRecord("Skyport", "Oasis", 20, 3);
    • addRecord("Oasis", "Crystalium", 15, 1.5);
    • addRecord("Skyport", "Nexus", 15, 4);
    • }
    • private void addRecord(final String origin, final String destiny, final double minutesWait,
    • final double hoursTravel) {
    • putIfAbsent(origin, new HashMap<>());
    • get(origin).put(destiny, new double[] { minutesWait, hoursTravel });
    • putIfAbsent(destiny, new HashMap<>());
    • get(destiny).put(origin, new double[] { minutesWait - 5, hoursTravel });
    • }
    • };
    • private static LocalTime calculateTime(final LocalTime currentTime, final double[] times) {
    • int minutes = (int) times[0];
    • int remainingMinutes = (int) (times[1] * 60);
    • return currentTime.plusMinutes(minutes).plusMinutes(remainingMinutes);
    • }
    • }
    • use std::collections::HashMap;
    • fn time_parse(time: &str) -> f64 {
    • let (h, m) = time.split_once(':').unwrap();
    • h.parse::<f64>().unwrap() * 60. + m.parse::<f64>().unwrap()
    • }
    • fn from_min_to_string(min: f64) -> String {
    • let h = (min / 60.).floor() % 24.;
    • let m = (min % 60.).floor();
    • format!("{:02}:{:02}", h, m)
    • }
    • const BUS_SCHEDULES: &str = r#"Skyport Crystalium 15 min 2 hours
    • Crystalium Skyport 10 min 2 hours
    • Skyport Oasis 20 min 3 hours
    • Oasis Skyport 15 min 3 hours
    • Oasis Crystalium 15 min 1.5 hours
    • Crystalium Oasis 10 min 1.5 hours
    • Skyport Nexus 15 min 4 hours
    • Nexus Skyport 10 min 4 hours
    • "#;
    • fn arrival_time(route: Vec<&str>, departure_time: &str) -> String {
    • let origin: Vec<&str> = vec!("Skyport");
    • if route.is_empty() {
    • return "The Train Driver has the day off".to_string();
    • } else {
    • let from = 0;
    • let to = 1;
    • let standby_time = 2;
    • let travel_time = 4;
    • let mut bus: HashMap<(String, String), (String, String)> = HashMap::new();
    • for line in BUS_SCHEDULES.lines() {
    • let data = line.split_whitespace().collect::<Vec<_>>();
    • let bus_stop = (data[from].to_string(), data[to].to_string());
    • let data_stop = (
    • data[standby_time].to_string(),
    • data[travel_time].to_string(),
    • );
    • bus.insert(bus_stop, data_stop);
    • }
    • let mut total_travel_time = 0.0;
    • let full_route: Vec<&&str> = origin.iter().chain(route.iter()).collect::<Vec<_>>();
    • for item in full_route.windows(2) {
    • if item[0] == item[1] {
    • total_travel_time += 1. * 60.;
    • } else {
    • let bus_stop = (item[0].to_string(), item[1].to_string());
    • match bus.get(&bus_stop) {
    • Some((waiting_time, travel_time)) => {
    • total_travel_time += travel_time.parse::<f64>().unwrap() * 60.
    • + waiting_time.parse::<f64>().unwrap();
    • }
    • None => {
    • let back_to_origin = (item[0].to_string(), origin[0].to_string());
    • let from_origin_to_route = (origin[0].to_string(), item[1].to_string());
    • match bus.get(&back_to_origin) {
    • Some((waiting_time, travel_time)) => {
    • total_travel_time += travel_time.parse::<f64>().unwrap() * 60.
    • + waiting_time.parse::<f64>().unwrap();
    • }
    • None => {
    • panic!("Could not find route between {} and Skyport", item[0]);
    • }
    • }
    • match bus.get(&from_origin_to_route) {
    • Some((waiting_time, travel_time)) => {
    • total_travel_time += travel_time.parse::<f64>().unwrap() * 60.
    • + waiting_time.parse::<f64>().unwrap();
    • }
    • None => {
    • panic!("Could not find route between Skyport and {}", item[1]);
    • }
    • }
    • }
    • }
    • }
    • }
    • from_min_to_string(total_travel_time + time_parse(departure_time))
    • }
    • }
Code
Diff
  • fn print(n: u64) -> u64 {
        let mut digits = n
            .to_string()
            .chars()
            .map(|c| c.to_digit(10).unwrap() as u64)
            .collect::<Vec<u64>>();
        digits.sort();
        digits
            .into_iter()
            .rev()
            .fold(0, |acc, digit| acc * 10 + digit)
    }
    • fn print(mut n: u64) -> u64 {
    • let mut digits = Vec::new();
    • while n > 0 {
    • digits.push(n % 10);
    • n /= 10;
    • }
    • fn print(n: u64) -> u64 {
    • let mut digits = n
    • .to_string()
    • .chars()
    • .map(|c| c.to_digit(10).unwrap() as u64)
    • .collect::<Vec<u64>>();
    • digits.sort();
    • digits.into_iter().rev().fold(0, |acc, digit| acc * 10 + digit)
    • }
    • digits
    • .into_iter()
    • .rev()
    • .fold(0, |acc, digit| acc * 10 + digit)
    • }
Code
Diff
  • fn sum(arr: &[i32]) -> i32 {
        match arr.is_empty() {
            true => 0,
            false => arr[..].iter().sum::<i32>(),
        }
    }
    • def sum(arr):
    • result = 0
    • for i in arr:
    • result += i
    • return result
    • fn sum(arr: &[i32]) -> i32 {
    • match arr.is_empty() {
    • true => 0,
    • false => arr[..].iter().sum::<i32>(),
    • }
    • }
Code
Diff
  • fn hello_world(world: bool) -> String {
        if world {
            return "Hello, World".to_string();
        } 
        return "Goodbye, World".to_string();
    }
    • def hello_world(world):
    • if world == True:
    • return "Hello, World"
    • return "Goodbye, World"
    • fn hello_world(world: bool) -> String {
    • if world {
    • return "Hello, World".to_string();
    • }
    • return "Goodbye, World".to_string();
    • }