New answers tagged

3

Testing for state repetition by if state in seen: is suboptimal. The number of states could grow exponentially with number of bits (and you are dealing with 200000 bits universe). Since seen is a list, the search time is linear, making the total time complexity quadratic with the size of the period. Using a set instead of list would immediately improve the ...


6

You are representing each state of the universe as a list of bits. However, CPUs are experts at handling sequences of bits — that's what an integer is! Given two bits, the way to find out whether exactly one of them is 1 is to use the ^ operator (XOR). You should be able to solve this challenge using bit-shifting (<< and >> operators) and ^.


1

General feedback There are a few ways that some of the loops can be simplified (see below). These may or may not improve the high CPU issue. The code makes some repeated calls to functions with the same values in close proximity (e.g. calling th() with each value of rgb twice in rgbToHex()). Storing the returned values and re-using the stored values would ...


1

After consulting with a friend, I ended up with this code: #include <fstream> #include <vector> #include <algorithm> #include <iostream> #include <set> using namespace std; ifstream in("planificare.in"); ofstream out("planificare.out"); int main() { int participants, tv_chan; vector<pair<int, int> > prog; ...


5

continue # Forbid a dice-roll that lands on a snake This may prevent finding the shortest path - it's possible to imagine a board with two long ladders, where the first ladder passes the bottom of the second ladder, but a snake descends from after the top of the first ladder to before the bottom of the second ladder. Be careful: if you do follow snakes, ...


7

Don't use mutable default arguments. If you need to default to a list then default to None and then change to an empty list. Take the following example code: >>> def example_list_builder(value, list_=[]): list_.append(value) return list_ >>> example_list_builder(1) [1] >>> example_list_builder(2) [1, 2] This makes list_ ...


12

By performing recursion, you are performing a depth-first search of four-letter words. However, this task involves finding a shortest path, and shortest-path problems are generally better done using breadth-first search. With BFS, the first solution you encounter will be an optimal solution — which is not the case with DFS.


5

The problem is that you are erasing elements from prog and then without bounds checking access it with prog[j]. That is sure to fall over. Your end_time function is commonly known as operator< So you should use the appropriate name. There is no comment on the significance of 1 and why a program should not be reordered when it starts at 1. It seems that ...


4

When reading your data, you open a file but never .close() it. You should take the habit to use the with keyword to avoid this issue. You should also benefit from the csv module to read this file as it will remove boilerplate and handle special cases for you: def open_file(filename='./Data.csv'): cost_center = [] # 0 cost_center_name = []# 1 ...


0

Doing def get_dupes(df): if sum(df.loc[1]=='No')<2: return None else: return list(df.loc[:,df.loc[1]=='No'].columns) df.groupby(axis=1,by=df.loc[0]).apply(get_dupes) Got me 0 124 None 123 [1234, 1235] dtype: object Your question wasn't quite clear on what you want the output to be if there are multiple ...


7

It's easier to read code that tuple unpacks the values in the for from dict.items(). for key1, (code1, option1) in Duplicate_combos.items(): archive_duplicates is a duplicate of Real_duplicates. There's no need for it. It doesn't seem like the output needs to be ordered, and so you can just make Real_duplicates a set. This means it won't have duplicates, ...


4

A simple dictionary will do once you get the idea ;) def get_neighbor(r, c, i, neighbors): if (r, c, i) in neighbors: return neighbors[(r, c, i)] if i == 'N': return (r - 1, c) elif i == 'S': return (r + 1, c) elif i == 'E': return (r, c + 1) else: # 'W' return (r, c - 1) def link_neighbors(r, c,...


5

At first glance, you code appears to have a lot of complicated and repetitive sections. That's a bad smell, and often a hint that you should either split the repetitive parts off into a function or otherwise reorganize your code to get rid of the repetition. (Sometimes, one option can be to just simplify the code until the repetitive parts become trivial.) ...


Top 50 recent answers are included