Fundamentals
Numbers
Data Types
Integers
public static class Kata { public static bool IsDivisible(int input, int div) => input % div == 0; }
- public static class Kata
- {
public static bool IsOdd(int input, int div){return input % div == 0;}- public static bool IsDivisible(int input, int div) => input % div == 0;
- }
namespace Solution { using NUnit.Framework; using System; // TODO: Replace examples and use TDD by writing your own tests [TestFixture] public class SolutionTest { [Test] public void MyTest() { Assert.AreEqual(true, Kata.IsDivisible(1,1)); Assert.AreEqual(false, Kata.IsDivisible(2,3)); Assert.AreEqual(true, Kata.IsDivisible(12,3)); Assert.AreEqual(false, Kata.IsDivisible(18284,13)); } } }
- namespace Solution {
- using NUnit.Framework;
- using System;
- // TODO: Replace examples and use TDD by writing your own tests
- [TestFixture]
- public class SolutionTest
- {
- [Test]
- public void MyTest()
- {
Assert.AreEqual(true, Kata.IsOdd(1,1));Assert.AreEqual(false, Kata.IsOdd(2,3));Assert.AreEqual(true, Kata.IsOdd(12,3));Assert.AreEqual(false, Kata.IsOdd(18284,13));- Assert.AreEqual(true, Kata.IsDivisible(1,1));
- Assert.AreEqual(false, Kata.IsDivisible(2,3));
- Assert.AreEqual(true, Kata.IsDivisible(12,3));
- Assert.AreEqual(false, Kata.IsDivisible(18284,13));
- }
- }
- }
More Linq :-)
using System.Linq; public static class Palindrome { public static bool IsPalindrome(this string word) { return word.Take(word.Length / 2) .Zip(word.Reverse().Take(word.Length / 2), (x, y) => char.ToUpperInvariant(x) == char.ToUpperInvariant(y)) .All(z => z); } }
- using System.Linq;
- public static class Palindrome
- {
- public static bool IsPalindrome(this string word)
- {
// Not optimized for worst case (when it is a palindrome)... but fails faster if it is not a palindrome// Zip first and last element pair, as neededforeach (var pair in word.Take(word.Length / 2).Zip(word.Reverse().Take(word.Length / 2), (x, y) => (x, y))){// Case invariant comparsionif (char.ToUpperInvariant(pair.Item1) != char.ToUpperInvariant(pair.Item2)){// This is not a palindromereturn false;}}// This turned out to be a palindromereturn true;- return word.Take(word.Length / 2)
- .Zip(word.Reverse().Take(word.Length / 2),
- (x, y) => char.ToUpperInvariant(x) == char.ToUpperInvariant(y))
- .All(z => z);
- }
- }
Fundamentals
Numbers
Data Types
Integers
Fun with mod 10.
public static class Kata { public static bool IsOdd(int input) { int x = 97531; while (x > 0) { if (input % 10 == x % 10) return true; x /= 10; } return false; } }
- public static class Kata
- {
- public static bool IsOdd(int input)
- {
return ((input % 10 == 1) || (input % 10 == 3) || (input % 10 == 5) || (input % 10 == 7) || (input % 10 == 9));- int x = 97531;
- while (x > 0)
- {
- if (input % 10 == x % 10) return true;
- x /= 10;
- }
- return false;
- }
- }
Message hidden in plain sight.
using System; class Kata { public static void Main() { char[] symbols = {'#', ',', ' ', '!', 'C', 'H', 'e', 'l', 'o', '\n'}; int[] message = {468635, 992176, 932097, 102907, 574968, 647851, 567232, 855764, 256630, 220323, 121509}; Array.ForEach(message, x => Console.Write(symbols[x % 10])); } }
- using System;
- class Kata
- {
private static string LANGUAGE = "C#";- public static void Main()
- {
string[] hello = {"H","e","l","l","o"};Array.ForEach(hello, s => Console.Write(s));Console.WriteLine(", " + LANGUAGE + "!" );- char[] symbols = {'#', ',', ' ', '!', 'C', 'H', 'e', 'l', 'o', '\n'};
- int[] message = {468635, 992176, 932097, 102907, 574968, 647851, 567232, 855764, 256630, 220323, 121509};
- Array.ForEach(message, x => Console.Write(symbols[x % 10]));
- }
- }
using System.Linq; public static class Palindrome { public static bool IsPalindrome(this string word) { // Not optimized for worst case (when it is a palindrome)... but fails faster if it is not a palindrome // Zip first and last element pair, as needed foreach (var pair in word.Take(word.Length / 2).Zip(word.Reverse().Take(word.Length / 2), (x, y) => (x, y))) { // Case invariant comparsion if (char.ToUpperInvariant(pair.Item1) != char.ToUpperInvariant(pair.Item2)) { // This is not a palindrome return false; } } // This turned out to be a palindrome return true; } }
- using System.Linq;
- public static class Palindrome
- {
- public static bool IsPalindrome(this string word)
- {
// Normalize the inputvar upper = word.ToUpperInvariant();- // Not optimized for worst case (when it is a palindrome)... but fails faster if it is not a palindrome
// For every chatacter in the first half..for (var i = 0; i < upper.Length / 2; ++i)// if the char doesn't match the one in the second half..if (upper[i] != upper[upper.Length - i - 1])// the word is not a palindromereturn false;// The word is not a palindrome- // Zip first and last element pair, as needed
- foreach (var pair in word.Take(word.Length / 2).Zip(word.Reverse().Take(word.Length / 2), (x, y) => (x, y)))
- {
- // Case invariant comparsion
- if (char.ToUpperInvariant(pair.Item1) != char.ToUpperInvariant(pair.Item2))
- {
- // This is not a palindrome
- return false;
- }
- }
- // This turned out to be a palindrome
- return true;
- }
- }
Step by space between multiples of 3 and 5
def euler(num): #0 3 6 9 12 15 #0 5 10 15 #0 3 2 1 3 1 2 3 inc = [3, 2, 1, 3, 1, 2, 3] x = 0 sum = 0 i = 0 while x < num: sum += x x += inc[i] i += 1 if i == 7: i = 0 return sum
- def euler(num):
n3 = 0n5 = 0- #0 3 6 9 12 15
- #0 5 10 15
- #0 3 2 1 3 1 2 3
- inc = [3, 2, 1, 3, 1, 2, 3]
- x = 0
- sum = 0
for x in range(num):count = Falseif x == n3:n3 += 3count = Trueif x == n5:n5 += 5count = Trueif count:sum += x- i = 0
- while x < num:
- sum += x
- x += inc[i]
- i += 1
- if i == 7:
- i = 0
- return sum
Without using the modulus operator
def euler(num): n3 = 0 n5 = 0 sum = 0 for x in range(num): count = False if x == n3: n3 += 3 count = True if x == n5: n5 += 5 count = True if count: sum += x return sum
- def euler(num):
return sum([x for x in range(num) if not x % 3 or not x % 5])- n3 = 0
- n5 = 0
- sum = 0
- for x in range(num):
- count = False
- if x == n3:
- n3 += 3
- count = True
- if x == n5:
- n5 += 5
- count = True
- if count:
- sum += x
- return sum