-
Notifications
You must be signed in to change notification settings - Fork 1
/
approx-c2f.py
executable file
·54 lines (42 loc) · 1.63 KB
/
approx-c2f.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
#
# approx-c2f.py - approximate Celcius to Fahrenheit conversion
#
# Copyright (C) 2019 Michael Davies <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
import os
import sys
def approx_celcius_to_fahrenheit(celcius):
"""Approximate Celcius to Fahrenheit conversion.
1) Double the Celcius value, i.e. 25C -> 50
2) Subtract the first digit, i.e. 50-5 -> 45
3) Add 32, i.e. 45 + 32 -> 77F"""
double = celcius * 2
s = str(double)
return float(double - int(s[0]) + 32)
def celcius_to_fahrenheit(celcius):
return (9.0/5 * celcius) + 32
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.exit("Usage: {} <temp-in-fahrenheit>".format(
os.path.basename(sys.argv[0])))
celcius = int(sys.argv[1])
approx = approx_celcius_to_fahrenheit(celcius)
actual = celcius_to_fahrenheit(celcius)
print("{}C is approximately {}F (actual conversion is {}F)".format(
celcius, approx, actual))