struct Test { const int t = 42; }; template <class T> struct TestTemplateThis : T { int get_t() const { return this->t; } }; template <class T> struct TestTemplateNoThis : T { int get_t() const { return this->t; } };
- struct Test {
- const int t = 42;
- };
- template <class T>
- struct TestTemplateThis : T {
- int get_t() const { return this->t; }
- };
- template <class T>
- struct TestTemplateNoThis : T {
int get_t() const { return t; }- int get_t() const { return this->t; }
- };
If x contains the digit 3 return true, otherwise return false
using System.Linq; public class Kumite { public static bool IsThree(int x) => $"{x}".Any(x => x == '3'); }
fn solution(x: i32) -> bool {x.to_string().chars().any(|x| x == '3')- using System.Linq;
- public class Kumite {
- public static bool IsThree(int x) =>
- $"{x}".Any(x => x == '3');
- }
using NUnit.Framework; namespace Tests { [TestFixture] class TestsClass { [Test] public void Tests() { Assert.IsFalse(Kumite.IsThree(525458)); Assert.IsFalse(Kumite.IsThree(2222)); Assert.IsTrue(Kumite.IsThree(354523)); Assert.IsTrue(Kumite.IsThree(748504231)); } } }
// Add your tests here.// See https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html- using NUnit.Framework;
#[cfg(test)]mod tests {use super::*;#[test]fn test_add() {assert_eq!(solution(525458), false);assert_eq!(solution(2222), false);assert_eq!(solution(354523), true)- namespace Tests {
- [TestFixture]
- class TestsClass {
- [Test]
- public void Tests() {
- Assert.IsFalse(Kumite.IsThree(525458));
- Assert.IsFalse(Kumite.IsThree(2222));
- Assert.IsTrue(Kumite.IsThree(354523));
- Assert.IsTrue(Kumite.IsThree(748504231));
- }
}- }
- }
-
Replaced the Where(char.IsDigit) with Math.Abs() for negatives
-
Changed from query format to method format
-
You can convert each digit to a number in .Sum(), no need to use select
-
Instead of using char.GetNumericValue(), you can avoid casting back to int by subtracting the char by 48, the ASCII value of '0', which will return an int
using System; using System.Linq; using System.Collections.Generic; namespace Kumite { public class Problem { public static int SumDigitsOf(long integer) => Math.Abs(integer) .ToString() .Sum(x => x-48); } }
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace Kumite
- {
- public class Problem
- {
public static int SumDigitsOf(long integer) => (int)(from ch in integer.ToString()where Char.IsDigit(ch)select Char.GetNumericValue(ch)).Sum();- public static int SumDigitsOf(long integer) =>
- Math.Abs(integer)
- .ToString()
- .Sum(x => x-48);
- }
- }
public class FizzBuzz { public string GetOutput(int number) { bool divisThree = number % 3 == 0; bool divisFive = number % 5 == 0; // If the two values are different, one is true // If the two values are the same, both or neither are true return divisThree != divisFive ? divisThree ? "Fizz" : "Buzz" : divisThree ? "FizzBuzz" : number.ToString(); // Fizz buzz is a popular computer science interview question. // The function above is given a number - if the number is // divisible by 3, return "fizz", if it's divisible by 5, // return "buzz", if not divisble by 3 or 5 - return the // number itself. } }
- public class FizzBuzz
- {
- public string GetOutput(int number) {
if (number % 15 == 0) return "FizzBuzz";else if (number % 3 == 0) return "Fizz";else if (number % 5 == 0) return "Buzz";else return number.ToString();- bool divisThree = number % 3 == 0;
- bool divisFive = number % 5 == 0;
- // If the two values are different, one is true
- // If the two values are the same, both or neither are true
- return divisThree != divisFive
- ? divisThree ? "Fizz" : "Buzz"
- : divisThree ? "FizzBuzz" : number.ToString();
- // Fizz buzz is a popular computer science interview question.
- // The function above is given a number - if the number is
- // divisible by 3, return "fizz", if it's divisible by 5,
- // return "buzz", if not divisble by 3 or 5 - return the
- // number itself.
- }
- }