![]() Otherwise, the False value is what you get. If that assumption will be true, True will be returned. It checks that the input string only contains 16 digits. The second function simply uss search function of re module. But when we want our program to stop when a bad input came in, I think using exceptions is the best way. There are a lot of people out there who are against exceptions and I agree with most of their reasons. The first function gets argv list as an input parameter and if its length is equal to 2, returns the second parameter. I separated your code into two functions. Raise Exception("You should enter a 16-digit number as input argument") We want to parse command line parameters and be sure that the input is correct.Įven though we only call those codes once, I think it is a great favor to the readability of the code to move those lines in a separate function. Now let's go to the beginning of your code. This code is somehow 1.2 times faster than previous. It's faster, you don't need to write new code and every Python programmer can tell what you are doing at first glance. For calculating the sum of digits in a list, I strongly recommend you that always use built-in function sum. We are using functions to avoiding duplication, so I think it's a bad idea to write the same code here. I think this name is clearer and everyone could tell what this code does.įor converting the number to a list of digits, I used convert_to_list function instead of writing the whole code again. Like the previous time, I changed the name. I want to choose pythonic way again here: def sum_of_digits(number): Now let's move on and take another look at addDigits function. How much? I have written a simple script for it the result is that on average, the second version is 1.7 times faster. ![]() Less code means probably fewer bugs.īut the second advantage here is this code is so much faster. ![]() The first advantage, we have written less code. Next change is I replaced all your code with a single line list comprehension. You can read more about the Python style guide at PEP8. First of all, I changed the name of the function from camelCase ( convertToList) to snake_case ( convert_to_list) because according to the Python style guide, it is the better way. I want to do this by "List Comprehension": def convert_to_list(num): I want to follow your algorithm and do this by converting the number to the str and then splitting it. The goal of this function is to split digits of a number to a list of int values. #resolve 2 digit number conflict by adding the digits of the number and returning itįor making your code faster, we can choose more Pythonic ways in some parts of it.įirst, let's take a look at convertToList function. #if digit is every second digit multiply by 2ĭigitList = addDigits(digitList) #converts initial passed int variable to list Print('Not enough or too many command line arguments! \n Proper use \"python Check.py \" ') I'm looking for feedback or tips to maybe make my code more readable or faster or just general tips to help get myself started with app making with Python.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |