Code Kata: staňte sa lepším programátorom
Posted: Thu Aug 30, 2012 9:12
@Matajon Ale řekl bych, že tohle tu katu nesplní ne? ;) Vypíše se Fizz[]Buzz, ale ne ostatní čísla do stovky.
Android, iPhone, Windows, chytrá domácnost a technologie…
https://smartmania.cz/forum/
To jsem si taky uvědomil chvíli po tom, co jsem to poslal a ten příspěvek jsem smazal.RSTEIN wrote:@Matajon Ale řekl bych, že tohle tu katu nesplní ne? ;) Vypíše se Fizz[]Buzz, ale ne ostatní čísla do stovky.
Tak daj fungujúce riešenieMatajon wrote:To jsem si taky uvědomil chvíli po tom, co jsem to poslal a ten příspěvek jsem smazal.
Code: Select all
private static void fizzbuzz_normal()
{
string output = string.Empty;
for (int i = 0; i <= 100; i++)
{
if (i % 3 == 0)
{ output += "fizz"; }
if (i % 5 == 0)
{ output += "buzz"; }
if (output == string.Empty)
{ output = i.ToString(); }
Console.WriteLine(output);
output = string.Empty;
}
}Code: Select all
static int NthFibonacci(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return NthFibonacci(n - 1) + NthFibonacci(n - 2);
}
static int NthFibonacci2(int n)
{
int result = 0;
int prev = 0;
int prevPrev = 0;
for(int i = 0; i <= n; i++)
{
if (i == 1)
{
result = 1;
}
else
{
result = prevPrev + prev;
}
prevPrev = prev;
prev = result;
}
return result;
}Code: Select all
let rec permutations (input: 'a list) = seq {
if (input.IsEmpty) then
yield []
else
for i in input do
yield! input
|> List.filter (fun x-> x<> i)
|> permutations
|> Seq.map (fun x->i::x)
}
Code: Select all
public IEnumerable<IEnumerable<T>> Permutation<T>(IEnumerable<T> input)
{
if (input == null || !input.Any()) yield break;
if (input.Count() == 1) yield return input;
foreach (var item in input)
{
var next = input.Where(l => !l.Equals(item)).ToList();
foreach (var perm in Permutation(next))
{
yield return (new List<T>{item}).Concat(perm);
}
}
}
Code: Select all
foreach i in X
permutacia = (i, permutacia X\i)Code: Select all
perms [] = [[]]
perms xs = [ x:ps | x <- xs , ps <- perms ( xs\\[x] ) ]
To daval Krylmatoman wrote:Napadla ma nova kata:
Napiste program, ktory pre danu permutaciu, vrati dalisu v poradi (pri lexikografickom usporiadani)