Factoring: decomposing a complex problem into simpler parts.
Refactoring: restructuring existing code.
Reading the code, is it clear what each piece does? How could it be be easier?
If you needed to change some part of the functionality, would that be easy? Would you have to change the same thing in several places?
If you break down what the function does into steps, how many steps are there? It’s best to have each function doing only one thing.
Is there unnecessary repetition? Does every piece of code get used? Could anything be more succinct whilst still being readable? This is called the DRY (Don’t Repeat Yourself) principle.
def check_answers(my_answers, answers): results = [None, None, None, None, None] if my_answers[0] == answers[0]: results[0] = True elif my_answers[0] != answers[0]: result[0] = False if my_answers[1] == answers[1]: result[1] = True elif my_asnwers[1] != anwers[0]: results[1] = False if my_answers[2] == answers[2]: results[2] = True elif my_answers[2] != answers[2]: results[2] = False if my_answers[3] == answers[3]: results[3] = True elif my_answers[3] != answers[3]: results[3] = False if my_answers[4] == answers[4]: results[4] = True elif my_answers[4] != answers[4]: results[4] = False count_correct = 0 count_incorrect = 0 for result in results: if result == True: count_correct += 1 if result != True: count_incorrect += 1 if count_correct/5 > 0.7: return "Congratulations, you passed the test! you scored " + str(count_correct) + " out of 5." elif count_incorrect/5 >= 0.3: return "Unfortunately, you did not pass. You scored " + str(count_correct) + " out of 5."
>>> len(countries) 785 >>> countries[:5] ['Angola', 'Maldives', 'India', 'United States', 'India']
def remove_duplicates(source): target = [] for element in source: if element not in target: target.append(element) return target
country_set = set(countries) len(country_set) country_set.add("Florin")
squares = set()
def nearest_square(limit):
answer = 0
while (answer+1)**2 < limit:
answer += 1
return answer**2
n = 1
while n**2 < 2000:
squares.add(n**2)
n += 1
[/python]