Fundamentals
Strings
Data Types
fn minutes(mins: u32) -> String { format!("{}:{:02}", mins / 60, mins % 60) }
def minutes(mins):return '{}:{:02}'.format(*divmod(mins, 60))- fn minutes(mins: u32) -> String {
- format!("{}:{:02}", mins / 60, mins % 60)
- }
#[test] fn test_minutes() { assert_eq!(minutes(0), "0:00"); assert_eq!(minutes(1), "0:01"); assert_eq!(minutes(18), "0:18"); assert_eq!(minutes(13267), "221:07"); assert_eq!(minutes(985), "16:25"); assert_eq!(minutes(351), "5:51"); assert_eq!(minutes(156113), "2601:53"); }
import unittestclass TestMinutes(unittest.TestCase):def test_minutes(self):self.assertEquals(minutes(0), "0:00", "better try again");self.assertEquals(minutes(1), "0:01", "better try again");self.assertEquals(minutes(18), "0:18", "better try again");self.assertEquals(minutes(13267), "221:07", "better try again");self.assertEquals(minutes(985), "16:25", "better try again");self.assertEquals(minutes(351), "5:51", "better try again");self.assertEquals(minutes(156113), "2601:53", "better try again");- #[test]
- fn test_minutes() {
- assert_eq!(minutes(0), "0:00");
- assert_eq!(minutes(1), "0:01");
- assert_eq!(minutes(18), "0:18");
- assert_eq!(minutes(13267), "221:07");
- assert_eq!(minutes(985), "16:25");
- assert_eq!(minutes(351), "5:51");
- assert_eq!(minutes(156113), "2601:53");
- }
Strings
Data Types
Take any string and return the last character.
If the string is empty, return nothing.
fn last_char(string: &str) -> Option<char> { string.chars().rev().next() }
function lastChar(string) {return string[string.length - 1];- fn last_char(string: &str) -> Option<char> {
- string.chars().rev().next()
- }
#[test] fn test_last_char() { assert_eq!(last_char(""), None); assert_eq!(last_char("Hello, world"), Some('d')); assert_eq!(last_char("こんにちは"), Some('は')); }
describe("basic test", function(){var domain='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',lC=(t)=>t.slice(-1);const randomString=(length, chars)=>{var result = '';for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];return result;}var rString=randomString(Math.floor(Math.random() * (domain.length - 1 + 1)) +1,domain)console.log(rString,lastChar(rString))it ("Test passed",()=>{Test.assertEquals(lastChar(rString), lC(rString), 'Test passed')});});- #[test]
- fn test_last_char() {
- assert_eq!(last_char(""), None);
- assert_eq!(last_char("Hello, world"), Some('d'));
- assert_eq!(last_char("こんにちは"), Some('は'));
- }