Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Is it ok / good style to use python built in functions like map() in unit tests?
for example instead of writing an individual assert for all test cases something like:
def double_x(x):
return x*2
def test_double_x():
orig_vals = [1, 2, 3, 4, 5, 6]
expected_vals = [2, 4, 6, 8, 10, 12]
assert list(map(double_x, orig_vals)) == expected_vals
There's no problem, but like anywhere else, a list comprehension may be preferable.
assert [double_x(x) for x in orig-vals] == expected_vals
Individual assertions, though, may make it easier to identify the failed test.
for x, y in zip(orig_vals, expected_vals):
z = double_x(x)
assert z == y, f'double_x({x}) returned {z}, not {y} as expected'
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 days ago.
Improve this question
Consider,
tp = (2, 3, 5, 7) # tuple
ls = [2, 3, 5, 7] # list
st = {2, 3, 5, 7} # set
Python has twin delimiters for different types of ensembles. As shown above, we have () for tuples, [] for lists and {} for sets. Is it possible to define new ones for something one wrote themselves or pre-written? E. g., say we wanted to define a numpy array np.array([2, 3, 5, 7]) as <2, 3, 5, 7>?
Edit: Not quite looking to define new infix relations. That's why I used the term twin delimiters.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Can someone help me writing a code in order to increasingly iterate on multiple iterators.
As an example imagine that I have several lists (for example : [1, 9] and [2, 3, 4]) and I want to get an iterable from those lists yielding 1, 2, 3, 4 and 9. Of course the idea is to use more big and complex iterators (otherwise it is easy to just merge and sort).
Thank you !
heapq is exactly designed for what you want : It creates an iterator, so you don't have to handle huge lists. Here is the code for a simple example :
import heapq
a = [1, 4, 7, 10]
b = [2, 5, 6, 11]
for c in heapq.merge(a, b):
print(c)
Of course, it works only if your lists are sorted, you have to sort them before if they are not sorted.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am writing this question using python but it may apply to any programming language. I would like to know if there is a more efficient way to write the following code:
for x in range(9):
if x == 5:
list_ = [101, 102]
for x in list_:
print(x)
else:
print(x)
I would be happy not to have to repeat print(x)
Not repeating print(x) won't change the efficiency of this code, because there's an if/else here so every number is printed exactly once.
You can write it shorter, for example:
for x in [*range(5), 101, 102, *range(6, 9)]:
print(x)
Or even:
for x in [0, 1, 2, 3, 4, 101, 102, 6, 7, 8]:
print(x)
Which I have added here just in order to illustrate that your question doesn't seem like a real problem to deal with in the first place...
This doesn't shorten the code... it may be a bit slower depending on what you are doing in your real application, but its a common technique to stage a call in one step and make the call in another. This is especially useful if you have common post-call processing. Its a bit of overkill for this example though
for x in range(9):
if x == 5:
list = [101, 102]
else:
list = [x]
for x in list:
print(x)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have 5 arrays with 6 questions in each one.
I need the script to pick 2 questions from each array and create an input() function. The part I can't seem to think of is how to make an output for a correct answer for the questions. I understand how a specified input would work but what about randomized.
I think you're looking for something like this:
randomNumber1=***some generated number (0 thru 6)
randomNumber2=***some generated number (0 thru 6)
array1=['what is the meaning of life', 'how far away is the sun',...]
array2=['what did is your favorite color', 'how many pennies are in 1 dollar'...]
q1=array1[randomNumber1]
q2=array2[randomNumber2]
input1=input(q1)
input2=input(q2)
#stores answers in a dictionary
answers={q1:input1, q2:input2}
I do not think the random module has the function that you want.
But it is easy to build one if you like. Python is easy.
Does this work?
import random
from typing import Iterable
def get_sub_random_list(sub_length: int, iterable: Iterable) -> list:
iterable_copy = list(iterable)
result = []
for __ in range(sub_length):
length = len(iterable_copy)
if length == 0:
raise ValueError(f"the iterable should longer than {sub_length}")
index = random.choice(range(length))
result.append(iterable_copy[index])
del iterable_copy[index]
return result
example:
>>> get_sub_random_list(1, [1, 2, 3, 4, 5, 6])
[5]
>>> get_sub_random_list(6, [1, 2, 3, 4, 5, 6])
[4, 1, 5, 2, 6, 3]
The complexity is O(n+m): n is the length of iterable, and the m is the the times of the loop.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm currently trying to complete Computer Science Circles online but I am stuck on part 14: Methods. Here is the question.
Using index and other list methods, write a function replace(list, X, Y) which replaces all occurrences of X in list with Y. For example, if L = [3, 1, 4, 1, 5, 9] then replace(L, 1, 7) would change the contents of L to [3, 7, 4, 7, 5, 9]. To make this exercise a challenge, you are not allowed to use [].
Note: you don't need to use return.
I would probably be able to do this if we were allowed to use square brackets.
Here is what I have so far.
def replace(L, X, Y):
while X in L:
var = L.index(X)
var = Y
return(L)
I'll give some tips since this is an exercise.
1) You already found out the index where you're supposed to replace one element with another. What other way is there to replace a value in a given index? Check all the methods of list.
2) A list comprehension also allows an elegant solution:
[...???... for value in list]
You'll need to figure out what the expression should be, and how to make the comprehension modify your original list, not just create a new one.