![]() The memory footprint is considerably smaller, though. Since you are already using itertools this is not a too heavy solution, I guess, but it still requires you to go through the whole list. # here we can still use self.remaining_possibilities # exhausting the 'teed' generator, leaving the 'original' intact: Self.remaining_possibilities, perm_count = itertools.tee(self.remaining_possibilities) If this is not the case, for some reason, it is possible to use itertools.tee() to generate a second generator from the first, and use it only for counting: def solve_current_level(self): This, however, requires that this data is available or that the length is passed along from the place where the original permutations generator was created. The simplest way to solve this is probably to calculate the number of permutations generated, using the permutations formula, which can be defined as: from math import factorial Return it.permutations(range(0, self.numWeapons), self.numGladiators) Here is that code both in python and ruby: return RETURNS AN ENUMERATOR OBJECT) ![]() Question 3: The Duplicity Dilemma Question: Write a Python function to remove duplicates from a list while preserving the order. NOTE: I am using itertools.permutations to calculate permutations that later gets saved in "self.remaining_possibilities" from itertools import permutationsdef allpermutations (s): return ''.join (p) for p in permutations (s) Expert Tip: Try optimizing the solution for larger strings using recursive techniques like Heap’s Algorithm. This in python before if condition is passed/failed. I cannot extend ram as I need to use less ram exactly like ruby and i absolutely want to avoid While in python there is not way to get the length of generator without converting to list before everything, I need it to work that way as at this moment when i get some bigger range with big size, it stucks and gets killed by my server. Now you see in ruby code the length of permutations is calculated before it is converted to array/hash to continue processing and if the number is bigger than 10**6 then it calls another method "reduce_weapon_options". Game.evaluate(guess, possibility) = response Here is the code in ruby: def solve_current_level I converted this piece of code from ruby and in ruby one can get the size of enumerator without converting to list as well and this issue never happens there. This is my current solution but issue is when it gets very huge script gets killed. Now the problem happens when very large permutations is generated and then converted into a list to check if the length is over 10**6 then do something else and come back. Response = _level(self.current_level.levelNum, guess) ![]() Guess = list(random.choice(self.remaining_possibilities)) If len(self.remaining_possibilities) > 10 ** 6: Self.remaining_possibilities = list(self.remaining_possibilities) ![]() Here is my code: def solve_current_level(self): Okay i have written a function/method in which I do some operations after a certain if condition is failed mostly but only 1-2 times it will be true. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |