life is short - you need Python!
36 FOLLOWERS
A blog on Python with tutorials, code, programs, tips and tricks, how-to, book lists, crawler/spider help, data structure, algorithm implementation, and many more.
life is short - you need Python!
3y ago
In this post I am going to show you how we can write a program in Python that can count the frequency of items in a list. First I shall implement it using the dictionary data structure in Python. Then I shall make it shorter (and nicer) using the defaultdict and Counter data structures from the collections module.
Here is our code that we will use to run and test the program -
if __name__ == "__main__": li = [2, 6, 9, 2, 8, 2, 9, 9, 3, 1, 4, 5, 7, 1, 8, 10, 2, 10, 10, 5] freq = frequency_counter(li) assert freq[1] == 2 assert freq[2] == 4 assert freq[3] == 1 assert freq[11 ..read more
life is short - you need Python!
3y ago
In this post I am going to write a program in python that finds all the prime factors of a number. I am going to start by writing an empty function and a test.
def get_prime_factors(number): prime_factors = [] return prime_factors if __name__ == "__main__": n = 8 expected = [2, 2, 2] result = get_prime_factors(n) assert expected == result, result
Now, if you run the program above, it will give an AssertionError, as we are returning an empty list. Let's write some code to make our test pass.
def get_prime_factors(number): prime_factors ..read more