No, moc neužijeme. Tohle je totiž věc, která se učí v každém rozumném kurzu úvodu do funkcionálního programování, takže u toho člověk nebude muset moc přemýšlet.Tomáš Slavíček wrote:Ale s F# si tam asi užijete...
![Usmev :-)](./images/smilies/001.gif)
No, moc neužijeme. Tohle je totiž věc, která se učí v každém rozumném kurzu úvodu do funkcionálního programování, takže u toho člověk nebude muset moc přemýšlet.Tomáš Slavíček wrote:Ale s F# si tam asi užijete...
Zrovna v F# to jde napsat na jeden radekTomáš Slavíček wrote:Tahle poslední, to je taková vysloveně "úloha z reálného života" :D Ale s F# si tam asi užijete...
Code: Select all
let fibonacci = Seq.unfold (fun (current,next) -> Some(current, (next, current+next))) (0,1)
let sum =
fibonacci
|> Seq.filter (fun x -> x% 2 = 0)
|> Seq.takeWhile (fun x-> x<4000000)
|> Seq.sum
Code: Select all
sing System;
using System.Reactive.Linq;
namespace FibonacciKata
{
class Program
{
public const int LIMIT = 4000000;
static void Main(string[] args)
{
Observable.Generate(new
{
Previous = 1,
Current = 2,
Total = 0
},
state => state.Previous <= LIMIT,
state => new
{
Previous = state.Current,
Current = state.Previous + state.Current,
Total = state.Total + (state.Current % 2 == 0 ? state.Current : 0)
},
state => state.Total)
.TakeLast(1)
.Subscribe(Console.WriteLine);
Console.ReadLine();
}
Code: Select all
public static class Fibonacci
{
public static IEnumerable<int> GetNumbers()
{
yield return 0;
int current = 1;
int previous = 0;
while (true)
{
yield return current;
current += previous;
previous = current - previous;
}
}
}
class Program
{
static void Main(string[] args)
{
int sum = Fibonacci.GetNumbers().Where(x => x % 2 == 1).TakeWhile(x => x < 4000000).Sum();
Console.WriteLine(sum);
}
}
Code: Select all
int sum = 0;
Stopwatch sw = new Stopwatch();
sw.Start();
int c = 2;
int t1, t2 = 1;
while (c < 4000000)
{
sum += c;
t1 = t2 + c;
t2 = c + t1;
c = t1 + t2;
}
sw.Stop();
Console.WriteLine(sum + " - " + sw.Elapsed);
Console.ReadKey();
Code: Select all
private static void CodeKata2()
{
const long limit = 4000000;
long result = FibSeq()
.TakeWhile(l => l < limit)
.Where(l => l%2 == 1)
.Sum();
Console.WriteLine(result); // 4613731
}
// infinite Fibonacci sequence
public static IEnumerable<long> FibSeq()
{
long a = 0;
long b = 1;
while (true)
{
long next = a + b;
if (a < b) a = next; else b = next;
yield return next;
}
}
AK programuje sam pre seba, tak možno. Ak jeho kód majú čítať a udržiavať iní ľudia, tak je jednoznačne cennejšia čitateľnosť. Hardvér je lacnejší ako ľudská práca.martan1981 wrote:Asi preto, že u programátora to má byť samozrejmosťou, či nie?
Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes.
Want to know something scary ? – the majority of comp sci graduates can’t. I’ve also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.
tak našiel som programátora (androiďák), ktorý to nezvládol. Jeho výhovorka bola že keby to spustil na počítači tak by zistil, že to nefunguje.matoman wrote:mozno som blby, ale toto snad musi napisat do dvoch minut skolak na prvom stupni na zakladke nie?
Code: Select all
[1..100]
|> List.map (fun n->
match n with
| n when n%5 = 0 && n%3 = 0 -> "FizzBuzz"
| n when n%3 = 0 -> "Fizz"
| n when n%5 = 0 -> "Buzz"
| n -> string n
)
|> List.iter (printfn "%s")
Code: Select all
sing System.Linq;
namespace KATA_NASA
{
using System;
public class FizzBuzzKata : IFizzBuzzKata
{
public string Answer(int i)
{
Func<int, string, string>[] analysers = new Func<int, string, string>[]
{
(input, current) => i % 3==0 ? current + "fizz" : current,
(input, current) => i % 5==0 ? current + "buzz" : current,
(input, current) => current==String.Empty ? input.ToString() : current
};
return analysers.Aggregate(String.Empty,
(data, function) => function(i, data));
//Klasika
//string result = i.ToString();
//bool isFizz = false;
//if (i % 3 == 0)
//{
// result = "fizz";
// isFizz = true;
//}
//if (i % 5 == 0)
//{
// result = isFizz ? result + "buzz" : "buzz";
//}
//return result;
}
}
public interface IFizzBuzzKata
{
/// <summary>
/// Give an answer to the current game
/// </summary>
/// <param name="number">current number in the game sequence</param>
/// <returns>appropriate answer to the current number</returns>
string Answer(int number);
}
}
Code: Select all
IFizzBuzzKata kata = new FizzBuzzKata();
Enumerable.Range(1, 100).Select(kata.Answer).ToList().ForEach(Console.WriteLine);
Code: Select all
private void FizzBuzz()
{
for (int i = 1; i <= 100; i++)
{
string s = string.Empty;
if (i % 3 == 0) s += "Fizz";
if (i % 5 == 0) s += "Buzz";
if (s != string.Empty) Console.WriteLine(s);
else Console.WriteLine(i.ToString());
}
}
Users browsing this forum: No registered users and 1 guest