Shorter but not necessary as readable. I chose to break the line at what i would consider each thought. Like an "if, else"
"If #calculations# is true? Do this. Otherwise keep going"
"If #calculations# is true? Do this. Otherwise keep going"
"If #calculations# is true? Do this. Otherwise keep going"
Chould definitely be a oneliner, but in my opinion oneLiners makes it less readable which will make it harder for the next programmer to understand.
// 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) { return (number % 15 == 0) ? "FizzBuzz": (number % 3 == 0) ? "Fizz": (number % 5 == 0) ? "Buzz": 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();}// 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.- return (number % 15 == 0) ? "FizzBuzz":
- (number % 3 == 0) ? "Fizz":
- (number % 5 == 0) ? "Buzz":
- number.ToString();
- }
- }
//Given 2 Arrays, Return True if arrays contain common item, else false. //e.g a= ['a','b','g','c'] and b =['z','e','c'] returns true //input : 2 arrays //output: bool using System.Collections.Generic; using System.Linq; public class Kata{ public static bool ContainsCommonItem(char[] a, char[] b){ //not mine for(int i=0;i<a.Length;i++) for(int j=0; j< b.Length; j++) if(a[i]==b[j]) return true; return false; } public static bool ContainsCommonItemBetter(char[] a,char[]b){ //not min HashSet<char> items = new HashSet<char>(); foreach(char item in a) items.Add(item); for (int i =0; i< b.Length; i++) if (items.Contains(b[i])) return true; return false; } public static bool ContainsCommonItemMoreSimple(char[] a, char[] b) // mine { var charsExcistingInBoth = a.Intersect(b); return charsExcistingInBoth.Count() > 0? true : false; } }
- //Given 2 Arrays, Return True if arrays contain common item, else false.
- //e.g a= ['a','b','g','c'] and b =['z','e','c'] returns true
// naive approach, loop through first array, with each item in a, loop through b and compare item to//each item in b returning true if a match is found or false.- //input : 2 arrays
- //output: bool
- using System.Collections.Generic;
- using System.Linq;
- public class Kata{
public static bool ContainsCommonItem(char[] a, char[] b){ //naive O(a*b)- public static bool ContainsCommonItem(char[] a, char[] b){ //not mine
- for(int i=0;i<a.Length;i++)
- for(int j=0; j< b.Length; j++)
- if(a[i]==b[j])
- return true;
- return false;
- }
public static bool ContainsCommonItemBetter(char[] a,char[]b){- public static bool ContainsCommonItemBetter(char[] a,char[]b){ //not min
- HashSet<char> items = new HashSet<char>();
- foreach(char item in a)
- items.Add(item);
- for (int i =0; i< b.Length; i++)
- if (items.Contains(b[i]))
- return true;
- return false;
- }
- public static bool ContainsCommonItemMoreSimple(char[] a, char[] b) // mine
- {
- var charsExcistingInBoth = a.Intersect(b);
- return charsExcistingInBoth.Count() > 0? true : false;
- }
- }