big-o

Coin change problem: difference between these two methods

Coin change problem: difference between these two methods Question: I am implementing the coin change problem in python in CS50’s pset6. When I first tackled the problem, this was the algorithm I used: import time while True: try: totalChange = input(‘How much change do I owe you? ‘) totalChange = float(totalChange) # check it it’s …

Total answers: 2

How come list element lookup is O(1) in Python?

How come list element lookup is O(1) in Python? Question: Today in class, we learned that retrieving an element from a list is O(1) in Python. Why is this the case? Suppose I have a list of four items, for example: li = [“perry”, 1, 23.5, “s”] These items have different sizes in memory. And …

Total answers: 3

Why are dict lookups always better than list lookups?

Why are dict lookups always better than list lookups? Question: I was using a dictionary as a lookup table but I started to wonder if a list would be better for my application — the amount of entries in my lookup table wasn’t that big. I know lists use C arrays under the hood which …

Total answers: 3

Linear time v.s. Quadratic time

Linear time v.s. Quadratic time Question: Often, some of the answers mention that a given solution is linear, or that another one is quadratic. How to make the difference / identify what is what? Can someone explain this, the easiest possible way, for the ones like me who still don’t know? Asked By: DevLounge || …

Total answers: 4

Python dictionary keys. "In" complexity

Python dictionary keys. "In" complexity Question: Quick question to mainly satisfy my curiosity on the topic. I am writing some large python programs with an SQlite database backend and will be dealing with a large number of records in the future, so I need to optimize as much as I can. For a few functions, …

Total answers: 6

Big-O of list slicing

Big-O of list slicing Question: Say I have some Python list, my_list which contains N elements. Single elements may be indexed by using my_list[i_1], where i_1 is the index of the desired element. However, Python lists may also be indexed my_list[i_1:i_2] where a “slice” of the list from i_1 to i_2 is desired. What is …

Total answers: 3

Complexity of list.index(x) in Python

Complexity of list.index(x) in Python Question: I’m referring to this: http://docs.python.org/tutorial/datastructures.html What would be the running time of list.index(x) function in terms of Big O notation? Asked By: user734027 || Source Answers: It’s O(n), also check out: http://wiki.python.org/moin/TimeComplexity This page documents the time-complexity (aka “Big O” or “Big Oh”) of various operations in current CPython. …

Total answers: 6