重构算法斐波纳契 [英] Refactoring Fibonacci Algorithm

查看:127
本文介绍了重构算法斐波纳契的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用过多年静态类型语言,并建立自己起床用C#来加速的任务。我用我在这里<随后的十五个练习惯用的伎俩href=\"http://www.jobsnake.com/seek/articles/index.cgi?openarticle&8533\">http://www.jobsnake.com/seek/articles/index.cgi?openarticle&8533作为我的第一个任务。

I haven't used a statically typed language in many years and have set myself the task of getting up to speed with C#. I'm using my usual trick of following the fifteen exercises here http://www.jobsnake.com/seek/articles/index.cgi?openarticle&8533 as my first task.

我刚刚完成第二斐波纳契任务,没有考虑到长期工作得很好,但在我看来,长相丑陋,我相信能够在更优雅code的少得多的线路来实现。

I've just finished the second Fibonacci task which didn't take to long and works just fine but in my opinion looks ugly and I'm sure could be achieved in far fewer lines of more elegant code.

我通常喜欢通过与别人谁已经知道他们在做什么,但今天的选择是不开放给我结对编程学习,所以我希望在这里张贴将是下一个最好的事情。

I usually like to learn by pair programming with someone who already knows what they're doing, but that option isn't open to me today, so I'm hoping posting here will be the next best thing.

因此​​,要所有的C#绝地武士的摆在那里,如果你要重构低于code,会是什么模样?

So to all the C# Jedi's out there, if you were going to refactor the code below, what would it look like?

using System;
using System.Collections;

namespace Exercises
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Find all fibinacci numbers between:");
			int from = Convert.ToInt32(Console.ReadLine());
			Console.WriteLine("And:");
			int to = Convert.ToInt32(Console.ReadLine());
			Fibonacci fibonacci = new Fibonacci();
			fibonacci.PrintArrayList(fibonacci.Between(from, to));

		}
	}

	class Fibonacci
	{	
		public ArrayList Between(int from, int to)
		{				
			int last = 1;
			int penultimate = 0;
			ArrayList results = new ArrayList();
			results.Add(penultimate);
			results.Add(last);

			while(last<to)
			{
				int fib = last + penultimate;
				penultimate = last;
				last = fib;
				if (fib>from && fib<to) results.Add(fib.ToString());
			}
			return results;
		}

		public void PrintArrayList(ArrayList arrayList)
		{
			Console.WriteLine("Your Fibonacci sequence:");
			Console.Write(arrayList[0]);
			for(int i = 1; i<arrayList.Count; i++)
			{
				Console.Write("," + arrayList[i]);
			}
			Console.WriteLine("");
		}

	}
}

问候,

克里斯

推荐答案

作为一个迭代器块:

using System;
using System.Collections.Generic;
using System.Linq;

static class Program {
    static IEnumerable<long> Fibonacci() {
        long n = 0, m = 1;

        yield return 0;
        yield return 1;
        while (true) {
            long tmp = n + m;
            n = m;
            m = tmp;
            yield return m;
        }
    }

    static void Main() {
        foreach (long i in Fibonacci().Take(10)) {
            Console.WriteLine(i);
        }
    }
}

这是现在已经完全懒了,使用LINQ的跳过 / 等,可以控制的开始/结束容易。例如,对于你的的查询:

This is now fully lazy, and using LINQ's Skip/Take etc allows you to control the start/end easily. For example, for your "between" query:

foreach (long i in Fibonacci().SkipWhile(x=>x < from).TakeWhile(x=>x <= to)) {...}

这篇关于重构算法斐波纳契的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆