乐高积木如何分类更科学_我如何使用(计算机)科学! 处理超过一千个乐高积木

乐高积木如何分类更科学_我如何使用(计算机)科学! 处理超过一千个乐高积木乐高积木如何分类更科学MostkidsabsolutelyLOVELegos.MysonhasbeenplayingwiththeDuploones,butwehavesincerecently“upgraded”hi

乐高积木如何分类更科学_我如何使用(计算机)科学! 处理超过一千个乐高积木

乐高积木如何分类更科学

Most kids absolutely LOVE Legos. My son has been playing with the Duplo ones, but we have since recently “upgraded” him to the normal ones. Since we didn’t have a collection of regular legos yet, we opted to just inherit someone else’s. We finally got our break when we found someone selling off their kids’ Lego collection since they were too old for them.

大多数孩子绝对喜欢乐高积木 。 我的儿子一直在与Duplo玩,但是我们最近才将他“升级”为普通人。 由于我们还没有常规的乐高玩具,所以我们选择继承别人的乐高玩具。 当我们发现有人卖掉了孩子们的乐高积木时,我们终于分手了,因为他们年纪太大了。

Since we had just gotten a payload of more than a thousand pieces, we now had to sort them by colour just to organise them. I sure wasn’t going to sort them by part or catalogue them, since that would have taken forever.

由于我们刚刚获得了超过一千件的有效载荷,因此我们现在只能按颜色对它们进行分类以进行组织。 我确定不会按部分对它们进行分类或分类 ,因为那样做将永远花费。

So given the insurmountable task, we then decided to get into it. We started sorting by picking up all the green bricks and collecting them in a basin for washing. Yep, this was a very old collection, so it was very dusty.

因此,鉴于这项艰巨的任务,我们决定加入其中。 我们首先捡起所有的绿色砖,然后将它们收集在盆中进行洗涤进行分类。 是的,这是一个非常古老的收藏,所以上面满是灰尘。

The initial plan of attack was this:

最初的攻击计划是这样的:

  • I sort the bricks and my wife cleans them and the shelves included. Yes the deal included 3 shelves, 9 boxes, 1 Lego bag (in the photo) and 1 Lego head in addition to probably five to ten thousand pieces

    我对砖进行分类,我的妻子清理它们,并包括架子。 是的,除了大概五到一万件外,这笔交易还包括3个架子,9个盒子,1个乐高袋(如图)和1个乐高头

  • We then pack them away — easy, right?

    然后,我们将它们收拾好-很简单,对吧?

This sounded like a good plan, until I realised that sorting by colour the way I was doing it was too slow. I was only picking up one colour at a time. I’d probably only sorted about 30 bricks and it had taken me five minutes. Imagine how long it would have taken for me to finish with the other colours like black, grey, red and blue!

听起来这是一个不错的计划,直到我意识到按颜色进行排序的过程太慢了。 我一次只捡一种颜色。 我大概只整理了大约30块砖,花了我五分钟。 想象一下我要花多长时间才能完成其他颜色,例如黑色,灰色,红色和蓝色!

So I had to rethink my plan. I was joking around with a couple of friends who then mentioned I was probably doing bubble sort, which was one of the slowest sorting algorithms our there (yes there are some other slower algorithms!). I laughed at the joke, then realised I might be able to use my computer science knowledge here — at least what’s left of it! Uni was ages ago, so I knew I’d have to improvise.

所以我不得不重新考虑我的计划。 我和几个朋友开玩笑,然后他们提到我可能正在进行冒泡排序 ,这是我们那里最慢的排序算法之一(是的,还有其他一些较慢的算法!)。 我笑了笑,然后意识到我也许可以在这里使用我的计算机科学知识-至少还剩下什么! 大学时代很久以前,所以我知道我必须即兴创作。

输入水平缩放 (Enter horizontal scaling)

I told everyone to stop what they were doing and help me sort out the bag. This meant that now there were more people sorting, so I basicallyscaled horizontally by adding more resources to finish the job.

我告诉所有人停止他们在做什么,并帮助我整理行李。 这意味着现在有更多的人进行分类,因此我基本上通过添加更多的资源来完成工作来进行水平扩展 。

As a web developer, I have seen this as a common solution to a problem with server load.

作为Web开发人员,我将其视为解决服务器负载问题的常用解决方案。

When your server is overloaded by a lot of incoming traffic, you usually have two options: vertical or horizontal scaling.

当服务器由于大量传入流量而超载时,通常有两个选择:垂直或水平缩放。

Vertical scaling means you basically add more power to your server. For example, if you’re using AWS, instead of having a t2.micro which only has 1 CPU and 1GB of RAM, you upgrade it to a t2.xlarge which has 4 CPUs and 16GB of RAM.

垂直扩展意味着您基本上可以为服务器增加功率。 例如,如果您正在使用AWS,而不必一个t2.micro只的RAM 1个CPU和1GB,可以将其升级到t2.xlarge其中有4个CPU和16GB的RAM。

Horizontal scaling means you just add more resources. So, instead of upgrading your singlet2.micro instance, you add 5 more to accommodate the load.

水平缩放意味着您只需添加更多资源。 因此,您无需再升级单个t2.micro实例,而是再添加5个实例来适应负载。

Both have their use cases, but for this specific instance, horizontal scaling was the solution.

两者都有其用例,但是对于此特定实例,水平缩放是解决方案。

I mean, I wouldn’t have been able to vertically scale myself by adding more brain power, so the only choice was to horizontally scale by adding more people.

我的意思是,我无法通过增加更多的脑力来纵向扩展自己,因此唯一的选择是通过增加更多的人来横向扩展自己。

After five minutes of sorting, I noticed that we did make some sort of progress. It wasn’t enough for me though. Time was passing and I was getting tired. We needed to make it faster!

经过五分钟的整理,我注意到我们确实取得了一些进展。 但是这对我来说还不够。 时间在流逝,我越来越疲倦。 我们需要使其更快!

分而治之算法 (Divide and conquer algorithm)

I had a think. There were three of us with a large bag full of bricks in front of us. I estimated it to be about two thousand pieces at this point. And while we had made some progress in the last five minutes, we were still looking at hours of sorting.

我有一个想法。 我们三个人面前有一个装满砖头的大袋子。 我估计目前大约有2000件。 尽管我们在过去五分钟中取得了一些进展,但我们仍在等待数小时的排序。

I have since then changed my initial technique of just looking for green blocks. Instead, I was having a quick look at which colour looked like the majority, and getting as many as I could with my hand. After putting the bricks in their appropriate colour basket, I looked at the pile again and picked the “majority” again. It usually changed, since after getting a bunch of say, reds, it would have fewer reds. The next majority would be blue or green, for example.

从那以后,我改变了只寻找绿色方块的最初技术。 取而代之的是,我快速看了看哪种颜色占大多数,并用手获得了尽可能多的颜色。 将砖块放入合适的颜色篮中后,我再次看了看那堆并再次选择了“多数”。 它通常会更改,因为在得到一堆红字之后,它的红字会更少。 例如,下一个多数将是蓝色或绿色。

This was looking good. But after analysing it, I was basically looking at two thousand pieces, getting the maximum count of a colour, getting that colour and subtracting it from the pile. My processing was slow, because how do you actually get the majority without counting or estimating?

看起来不错。 但是经过分析之后,我基本上只看了两千块,得到了一种颜色的最大数量,得到了该颜色,然后从堆中减去了该颜色。 我的处理速度很慢,因为您实际上如何在不计算或估计的情况下获得多数?

Since I was taking too much cognitive load, I was slowing down. So I stopped looking at the colour with the most bricks and just picked a random colour every time I dumped the handful of bricks I had just collected. This sped up my processing a little, but I thought we could still improve.

由于我承担了太多的认知负担,所以我放慢了脚步。 因此,我停止查看砖块数量最多的颜色,而每次抛弃刚刚收集的少量砖块时,都只是选择一种随机颜色。 这加快了我的处理速度,但我认为我们仍然可以改进。

So, divide and conquer it was. In CS terms, this meant an algorithm that broke down a huge problem into smaller bits so they were simple enough to solve in a faster time.

因此, 分而治之。 用CS来说,这意味着一种算法可以将一个巨大的问题分解为更小的位,因此它们足够简单,可以在更短的时间内解决。

Let’s say your site accepts user uploaded zip files with photos and processes them. If your server accepted the zip file, unzipped it, and processed it the minute you uploaded it, everyone else would be waiting for it to finish. Sure you can horizontally or vertically scale your server, but the wait time is unnecessary. Also, what happens when a user uploads a zip file with 100 photos in it?

假设您的网站接受用户上传的带有照片的zip文件并进行处理。 如果您的服务器接受了zip文件,将其解压缩并在您上传该文件的那一刻对其进行了处理,那么其他所有人将在等待该文件完成。 当然,您可以水平或垂直扩展服务器,但是不需要等待时间。 另外,当用户上传其中包含100张照片的zip文件时,会发生什么情况?

You can solve this by using a divide and conquer technique instead. First off, you delegate the processing to a delayed job infrastructure like Rails’ ActiveJob. Or if you’re not using Rails, Sidekiq. Still, that job would take a long time if it was a 100 photos, and there would be the possibility of your worker dying from the workload.

您可以使用分而治之的方法来解决此问题。 首先,将处理委托给延迟的工作基础架构,例如Rails的ActiveJob 。 或者,如果您不使用Rails, 请使用Sidekiq 。 但是,如果只有100张照片,该工作将花费很长时间,并且您的工作人员可能会因工作量丧命。

One solution would be to have a job that unzips it, and then enqueues each of the photos to be processed as a separate job. Now instead of your worker having to process 100 photos by itself, it then puts 100 tiny jobs of processing single photos in the queue, which can then be picked up by other workers.

一种解决方案是拥有一个将其解压缩的作业, 然后将要处理的每张照片放入一个单独的作业中。 现在,您的工作人员不必自己处理100张照片,而是将100个处理单张照片的小工作放在队列中,然后其他工作人员可以接管。

With that in mind, I made a factory line deal with my son: he had to get a handful (or two) of bricks from the bag and dump them into my corner. That meant I only had about 50 bricks to sort which was easier and faster — mainly because by this point, I knew the colour with the most bricks: grey.

考虑到这一点,我和儿子做了一条工厂线交易:他必须从袋子里拿出几块(或两块)砖头,然后扔到我的角落。 那意味着我只需要分类大约50块砖,这将变得更加容易和快捷-主要是因为到现在为止,我知道砖块最多的颜色:灰色。

So what if I knew it was grey? Well, that meant that I only had to pick all the red, green, yellow, blue, black and white bricks. When I was left with grey, I dumped the remaining bricks into the grey box — that saved me one less colour to sort that I would not have been able to do on a larger scale.

那么,如果我知道它是灰色的呢? 好吧,这意味着我只需要挑选所有红色,绿色,黄色,蓝色,黑色和白色的砖块即可。 当我剩下灰色时,我将剩下的砖块扔到了灰色的盒子里,这为我节省了少一点的颜色,使我无法进行更大范围的处理。

一小时后 … (An hour later …)

All done! Now we just had to clean them. The initial plan was:

全做完了! 现在我们只需要清洁它们。 最初的计划是:

  • Dump a box of colours into a basin.

    将一盒颜色倒入盆中。

  • Wash with water and soap

    用水和肥皂清洗

  • Dry

    干燥

What was the problem here? I’ll give you a guess.

这是什么问题? 我给你一个猜测。

Still there? Okay. If we washed with water and soap, that meant we had to rinse the soap off — which meant we had to wash it twice! No way! So I decided not to add soap but just thoroughly wash with warm water.

还在那里? 好的。 如果我们用水和肥皂清洗,那意味着我们必须冲洗掉肥皂-这意味着我们必须清洗两次! 没门! 因此,我决定不添加肥皂,而只是用温水彻底清洗。

The modified plan was:

修改后的计划是:

  • Dump a box of colours into a basin

    将一箱颜色倒入盆中

  • Wash with water thoroughly

    用水彻底清洗

  • Dry

    干燥

On to the next problem: drying the Legos.

接下来的问题是:乐高积木干燥。

离心力抢救! (Centrifugal force to the rescue!)

Initially, we tried drying the pieces with a towel. It wasn’t very effective. Next was to use a hairdryer and blow hot air into the pieces. It was okay, but it still didn’t dry the pieces — we were getting hopeless and almost decided to just lay them all on the floor and let all the water evaporate.

最初,我们尝试用毛巾擦干。 这不是很有效。 接下来是使用吹风机,将热空气吹成碎片。 没关系,但仍然没有使碎片变干-我们感到绝望,几乎决定将它们全部铺在地板上,让所有的水分蒸发。

Only, it was autumn so it wouldn’t have been hot enough. So while pondering about how to dry thousands of Lego pieces, I suddenly remembered how I dry salad leaves using a salad spinner. “I wish I could use the salad spinner,” I thought. Then it dawned on me: a salad spinner works by using centrifugal force to separate the water from the leaves. I could do the same!

只不过是秋天,所以天气还不够热。 因此,在思考如何干燥成千上万块乐高积木的同时,我突然想起我是如何使用沙拉旋转器来干燥沙拉叶的。 我想:“我希望我可以用沙拉旋转器。” 然后它突然降临在我身上: 沙拉旋转器通过利用离心力将水从树叶中分离出来而起作用。 我可以一样!

I wrapped the lego pieces into a towel and secured them by turning the towel into a giant candy wrapper. I stepped on one end of the towel, pulled the other end as tight as I could and started spinning the towel.

我将乐高玩具块包裹在毛巾中,然后将毛巾变成一个巨大的糖果包装纸,以固定它们。 我踩到毛巾的一端,尽力拉紧另一端,开始旋转毛巾。

What do you know — it actually worked! I could see the towel suddenly become wet as the water from the pieces flew out of them and into the towel. SCIENCE!

您知道什么-它确实有效! 我可以看到毛巾突然湿了,因为碎片中的水从它们中飞出并进入毛巾中。 科学!

The Legos weren’t completely dry, of course, so I still had to use the hairdryer to help the remaining droplets evaporate. But that was all right — the hard part was over.

当然,Legos并没有完全干燥,因此我仍然必须使用吹风机来帮助剩余的水滴蒸发。 但这没关系-困难的部分已经过去。

I never thought I’d use my stock CS knowledge for something like sorting Legos. In any case, it was a good and fun experience scaling horizontally, using a divide and conquer strategy, and even bringing out centrifugal force to organise my son’s newly inherited Lego collection. I don’t look forward to the day he jumbles them all up and we have to sort them again, though!

我从没想过我会用我的股票CS知识来对乐高玩具进行排序。 无论如何,使用分而治之策略,甚至是利用离心力来组织我儿子新近继承的Lego系列,都是一次很好的有趣体验,可以水平缩放。 我不希望他把它们弄乱的那一天,但是我们必须再次对它们进行分类!

翻译自: https://www.freecodecamp.org/news/using-computer-science-to-deal-with-more-than-a-thousand-lego-pieces-439a2d5a3278/

乐高积木如何分类更科学

今天的文章乐高积木如何分类更科学_我如何使用(计算机)科学! 处理超过一千个乐高积木分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/87786.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注