From d70ceb0d2ee70ec92162107a6561c5676acbc86a Mon Sep 17 00:00:00 2001 From: HankChow <280630620@qq.com> Date: Sat, 13 Oct 2018 22:50:58 +0800 Subject: [PATCH] translated --- ...ump- A script for filling your gas tank.md | 103 ------------------ ...ump- A script for filling your gas tank.md | 101 +++++++++++++++++ 2 files changed, 101 insertions(+), 103 deletions(-) delete mode 100644 sources/tech/20181008 Python at the pump- A script for filling your gas tank.md create mode 100644 translated/tech/20181008 Python at the pump- A script for filling your gas tank.md diff --git a/sources/tech/20181008 Python at the pump- A script for filling your gas tank.md b/sources/tech/20181008 Python at the pump- A script for filling your gas tank.md deleted file mode 100644 index 265048b93b..0000000000 --- a/sources/tech/20181008 Python at the pump- A script for filling your gas tank.md +++ /dev/null @@ -1,103 +0,0 @@ -HankChow translating - -Python at the pump: A script for filling your gas tank -====== -Here's how I used Python to discover a strategy for cost-effective fill-ups. - -![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/bulb-light-energy-power-idea.png?itok=zTEEmTZB) - -I recently began driving a car that had traditionally used premium gas (93 octane). According to the maker, though, it requires only 91 octane. The thing is, in the US, you can buy only 87, 89, or 93 octane. Where I live, gas prices jump 30 cents per gallon jump from one grade to the next, so premium costs 60 cents more than regular. So why not try to save some money? - -It’s easy enough to wait until the gas gauge shows that the tank is half full and then fill it with 89 octane, and there you have 91 octane. But it gets tricky to know what to do next—half a tank of 91 octane plus half a tank of 93 ends up being 92, and where do you go from there? You can make continuing calculations, but they get increasingly messy. This is where Python came into the picture. - -I wanted to come up with a simple scheme in which I could fill the tank at some level with 93 octane, then at the same or some other level with 89 octane, with the primary goal to never get below 91 octane with the final mixture. What I needed to do was create some recurring calculation that uses the previous octane value for the preceding fill-up. I suppose there would be some polynomial equation that would solve this, but in Python, this sounds like a loop. - -``` -#!/usr/bin/env python -# octane.py - -o = 93.0 -newgas = 93.0   # this represents the octane of the last fillup -i = 1 -while i < 21:                   # 20 iterations (trips to the pump) -    if newgas == 89.0:          # if the last fillup was with 89 octane -                                # switch to 93 -        newgas = 93.0 -        o = newgas/2 + o/2      # fill when gauge is 1/2 full -    else:                       # if it wasn't 89 octane, switch to that -        newgas = 89.0 -        o = newgas/2 + o/2      # fill when gauge says 1/2 full -    print str(i) + ': '+ str(o) -    i += 1 -``` - -As you can see, I am initializing the variable o (the current octane mixture in the tank) and the variable newgas (what I last filled the tank with) at the same value of 93. The loop then will repeat 20 times, for 20 fill-ups, switching from 89 octane and 93 octane for every other trip to the station. - -``` -1: 91.0 -2: 92.0 -3: 90.5 -4: 91.75 -5: 90.375 -6: 91.6875 -7: 90.34375 -8: 91.671875 -9: 90.3359375 -10: 91.66796875 -11: 90.333984375 -12: 91.6669921875 -13: 90.3334960938 -14: 91.6667480469 -15: 90.3333740234 -16: 91.6666870117 -17: 90.3333435059 -18: 91.6666717529 -19: 90.3333358765 -20: 91.6666679382 -``` - -This shows is that I probably need only 10 or 15 loops to see stabilization. It also shows that soon enough, I undershoot my 91 octane target. It’s also interesting to see this stabilization of the alternating mixture values, and it turns out this happens with any scheme where you choose the same amounts each time. In fact, it is true even if the amount of the fill-up is different for 89 and 93 octane. - -So at this point, I began playing with fractions, reasoning that I would probably need a bigger 93 octane fill-up than the 89 fill-up. I also didn’t want to make frequent trips to the gas station. What I ended up with (which seemed pretty good to me) was to wait until the tank was about 7⁄12 full and fill it with 89 octane, then wait until it was ¼ full and fill it with 93 octane. - -Here is what the changes in the loop look like: - -``` -    if newgas == 89.0:             -                                  -        newgas = 93.0 -        o = 3*newgas/4 + o/4       -    else:                         -        newgas = 89.0 -        o = 5*newgas/12 + 7*o/12 -``` - -Here are the numbers, starting with the tenth fill-up: - -``` -10: 92.5122272978 -11: 91.0487992571 -12: 92.5121998143 -13: 91.048783225 -14: 92.5121958062 -15: 91.048780887 -``` - -As you can see, this keeps the final octane very slightly above 91 all the time. Of course, my gas gauge isn’t marked in twelfths, but 7⁄12 is slightly less than 5⁄8, and I can handle that. - -An alternative simple solution might have been run the tank to empty and fill with 93 octane, then next time only half-fill it for 89—and perhaps this will be my default plan. Personally, I’m not a fan of running the tank all the way down since this isn’t always convenient. On the other hand, it could easily work on a long trip. And sometimes I buy gas because of a sudden drop in prices. So in the end, this scheme is one of a series of options that I can consider. - -The most important thing for Python users: Don’t code while driving! - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/18/10/python-gas-pump - -作者:[Greg Pittman][a] -选题:[lujun9972](https://github.com/lujun9972) -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://opensource.com/users/greg-p diff --git a/translated/tech/20181008 Python at the pump- A script for filling your gas tank.md b/translated/tech/20181008 Python at the pump- A script for filling your gas tank.md new file mode 100644 index 0000000000..396ed17291 --- /dev/null +++ b/translated/tech/20181008 Python at the pump- A script for filling your gas tank.md @@ -0,0 +1,101 @@ +使用 Python 为你的油箱加油 +====== +我来介绍一下我是如何使用 Python 来节省成本的。 + +![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/bulb-light-energy-power-idea.png?itok=zTEEmTZB) + +我最近在开一辆烧 93 号汽油的车子。根据汽车制造商的说法,它只需要加 91 号汽油就可以了。然而,在美国只能买到 87 号、89 号、93 号汽油。而我家附近的汽油的物价水平是每增加一号,每加仑就要多付 30 美分,因此如果加 93 号汽油,每加仑就要多花 60 美分。为什么不能节省一些钱呢? + +一开始很简单,只需要先加满 93 号汽油,然后在油量表显示油箱半满的时候,用 89 号汽油加满,就得到一整箱 91 号汽油了。但接下来就麻烦了,剩下半箱 91 号汽油加上半箱 93 号汽油,只会变成一箱 92 号汽油,再接下来呢?如果继续算下去,只会越来越混乱。这个时候 Python 就派上用场了。 + +我的方案是,可以根据汽油的实时状态,不断向油箱中加入 93 号汽油或者 89 号汽油,而最终目标是使油箱内汽油的号数不低于 91。我需要做的是只是通过一些算法来判断新旧汽油混合之后的号数。使用多项式方程或许也可以解决这个问题,但如果使用 Python,好像只需要进行循环就可以了。 + +``` +#!/usr/bin/env python +# octane.py + +o = 93.0 +newgas = 93.0 # 这个变量记录上一次加入的汽油号数 +i = 1 +while i < 21: # 20 次迭代 (加油次数) + if newgas == 89.0: # 如果上一次加的是 89 号汽油,改加 93 号汽油 + newgas = 93.0 + o = newgas/2 + o/2 # 当油箱半满的时候就加油 + else: # 如果上一次加的是 93 号汽油,则改加 89 号汽油 + newgas = 89.0 + o = newgas/2 + o/2 # 当油箱半满的时候就加油 + print str(i) + ': '+ str(o) + i += 1 +``` + +在代码中,我首先将变量 `o`(油箱中的当前混合汽油号数)和变量 `newgas`(上一次加入的汽油号数)的初始值都设为 93,然后循环 20 次,也就是分别加入 89 号汽油和 93 号汽油一共 20 次,以保持混合汽油号数稳定。 + +``` +1: 91.0 +2: 92.0 +3: 90.5 +4: 91.75 +5: 90.375 +6: 91.6875 +7: 90.34375 +8: 91.671875 +9: 90.3359375 +10: 91.66796875 +11: 90.333984375 +12: 91.6669921875 +13: 90.3334960938 +14: 91.6667480469 +15: 90.3333740234 +16: 91.6666870117 +17: 90.3333435059 +18: 91.6666717529 +19: 90.3333358765 +20: 91.6666679382 +``` + +从以上数据来看,只需要 10 到 15 次循环,汽油号数就比较稳定了,也相当接近 91 号汽油的目标。这种交替混合直到稳定的现象看起来很有趣,每次交替加入同等量的不同号数汽油,都会趋于稳定。实际上,即使加入的 89 号汽油和 93 号汽油的量不同,也会趋于稳定。 + +因此,我尝试了不同的比例,我认为加入的 93 号汽油需要比 89 号汽油更多一点。在尽量少补充新汽油的情况下,我最终计算到的结果是 89 号汽油要在油箱大约 7/12 满的时候加进去,而 93 号汽油则要在油箱 1/4 满的时候才加进去。 + +我的循环将会更改成这样: + +``` + if newgas == 89.0: + + newgas = 93.0 + o = 3*newgas/4 + o/4 + else: + newgas = 89.0 + o = 5*newgas/12 + 7*o/12 +``` + +以下是从第十次加油开始的混合汽油号数: + +``` +10: 92.5122272978 +11: 91.0487992571 +12: 92.5121998143 +13: 91.048783225 +14: 92.5121958062 +15: 91.048780887 +``` + +如你所见,这个调整会令混合汽油号数始终略高于 91。当然,我的油量表并没有 1/12 的刻度,但是 7/12 略小于 5/8,我可以近似地计算。 + +一个更简单地方案是每次都首先加满 93 号汽油,然后在油箱半满时加入 89 号汽油直到耗尽,这可能会是我的常规方案。但就我个人而言,这种方法并不太好,有时甚至会产生一些麻烦。但对于长途旅行来说,这种方案会相对简便一些。有时我也会因为油价突然下跌而购买一些汽油,所以,这个方案是我可以考虑的一系列选项之一。 + +当然最重要的是:开车不写码,写码不开车! + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/18/10/python-gas-pump + +作者:[Greg Pittman][a] +选题:[lujun9972](https://github.com/lujun9972) +译者:[HankChow](https://github.com/HankChow) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/greg-p +