Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
Is there any shorter, more legible code style to solve this problem? I am trying to classify some float values into interregional folders.
def classify(value): if value < -0.85 and value >= -0.95: ts_folder = r'\-0.9' elif value < -0.75 and value >= -0.85: ts_folder = r'\-0.8' elif value < -0.65 and value >= -0.75: ts_folder = r'\-0.7' elif value < -0.55 and value >= -0.65: ts_folder = r'\-0.6' elif value < -0.45 and value >= -0.55: ts_folder = r'\-0.5' elif value < -0.35 and value >= -0.45: ts_folder = r'\-0.4' elif value < -0.25 and value >= -0.35: ts_folder = r'\-0.3' elif value < -0.15 and value >= -0.25: ts_folder = r'\-0.2' elif value < -0.05 and value >= -0.15: ts_folder = r'\-0.1' elif value < 0.05 and value >= -0.05: ts_folder = r'\0.0' elif value < 0.15 and value >= 0.05: ts_folder = r'\0.1' elif value < 0.25 and value >= 0.15: ts_folder = r'\0.2' elif value < 0.35 and value >= 0.25: ts_folder = r'\0.3' elif value < 0.45 and value >= 0.35: ts_folder = r'\0.4' elif value < 0.55 and value >= 0.45: ts_folder = r'\0.5' elif value < 0.65 and value >= 0.55: ts_folder = r'\0.6' elif value < 0.75 and value >= 0.65: ts_folder = r'\0.7' elif value < 0.85 and value >= 0.75: ts_folder = r'\0.8' elif value < 0.95 and value >= 0.85: ts_folder = r'\0.9' return ts_folder
One of the first rules with a block of code like this, is to always make the comparisons be in the same direction. So instead of
elif value < -0.75 and value >= -0.85:
write
elif -0.85 <= value and value < -0.75:
At this point you can observe that python allows chaining of comparisons, so you can write:
elif -0.85 <= value < -0.75:
Which is an improvement itself. Alternatively, you can observe this is an ordered list of comparisons, so if you add in an initial comparisons, you can just write
if value < -0.95: ts_folder = '' elif value < -0.85: ts_folder = r'\-0.9' elif value < -0.75: ts_folder = r'\-0.8' elif value < -0.65: ts_folder = r'\-0.7' elif value < -0.55: ts_folder = r'\-0.6' elif value < -0.45: ts_folder = r'\-0.5' elif value < -0.35: ts_folder = r'\-0.4' elif value < -0.25: ts_folder = r'\-0.3' elif value < -0.15: ts_folder = r'\-0.2' elif value < -0.05: ts_folder = r'\-0.1' elif value < 0.05: ts_folder = r'\0.0' elif value < 0.15: ts_folder = r'\0.1' elif value < 0.25: ts_folder = r'\0.2' elif value < 0.35: ts_folder = r'\0.3' elif value < 0.45: ts_folder = r'\0.4' elif value < 0.55: ts_folder = r'\0.5' elif value < 0.65: ts_folder = r'\0.6' elif value < 0.75: ts_folder = r'\0.7' elif value < 0.85: ts_folder = r'\0.8' elif value < 0.95: ts_folder = r'\0.9' else: ts_folder = ''That's still quite long, but a) it's a lot more readable; b) it has explicit code to handle value < -0.95 or 0.95 <= value
if value < -0.95: ts_folder = '' elif value < -0.85: ts_folder = r'\-0.9' elif value < -0.75: ts_folder = r'\-0.8' elif value < -0.65: ts_folder = r'\-0.7' elif value < -0.55: ts_folder = r'\-0.6' elif value < -0.45: ts_folder = r'\-0.5' elif value < -0.35: ts_folder = r'\-0.4' elif value < -0.25: ts_folder = r'\-0.3' elif value < -0.15: ts_folder = r'\-0.2' elif value < -0.05: ts_folder = r'\-0.1' elif value < 0.05: ts_folder = r'\0.0' elif value < 0.15: ts_folder = r'\0.1' elif value < 0.25: ts_folder = r'\0.2' elif value < 0.35: ts_folder = r'\0.3' elif value < 0.45: ts_folder = r'\0.4' elif value < 0.55: ts_folder = r'\0.5' elif value < 0.65: ts_folder = r'\0.6' elif value < 0.75: ts_folder = r'\0.7' elif value < 0.85: ts_folder = r'\0.8' elif value < 0.95: ts_folder = r'\0.9' else: ts_folder = ''
That's still quite long, but a) it's a lot more readable; b) it has explicit code to handle value < -0.95 or 0.95 <= value
value < -0.95 or 0.95 <= value
You can use the round() built-in :
round()
ts_folder = "\\" + str(round(value + 1e-16, 1)) # To round ref="https://forum.tuteehub.com/tag/values">values like .05 to .1, not .0 if ts_folder == r"\-0.0": ts_folder = r"\0.0"
Actually in Python 3 .85 will be round to .8. As per the question .85 should be round to .9.
.85
.8
.9
Can you try the following:
round2 = lambda x, y=None: round(x+1e-15, y) ts_folder = r'\{}'.format(str(round2(value, 1)))
Output:
>>> round2(.85, 1) 0.9 >>> round2(-.85, 1) -0.8
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.