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.
Related
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 1 year ago.
Improve this question
For a homework assignment I am given this task:
Read a list of numbers and generate a new list which squares the members of the original list.
generate a third list of ints that ranks the second list of ints.
What does it mean to "rank the list"? What is this asking me to do?
I am NOT asking for a solution to the programming assignment, please do not provide one.
To create a list of integers between 0 and N, a solution is to use the range(N) function.
Then you can use the append() method which adds an item to the end of the list while getting the square of each element.
Finally use the sort() method which sorts the list ascending by default. You can also make a function to decide the sorting criteria(s)
def printSortedList():
l = list()
for i in range(1,21):
l.append(i**2)
l.sort()
print(l)
printSortedList()
Ranking means to number the items in a list from lowest to highest, or vice-versa.
So, for example, “ranking” the list [639, 810, 150, 162, 461, 853, 648] produces the list [4, 6, 1, 2, 3, 7, 5], where 1 corresponds to the lowest value (150), 2 to the next-lowest value (162).
Or maybe you're supposed to rank them the other way, giving [4, 2, 7, 6, 5, 1, 3].
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 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 know in both MATLAB and numpy in Python, you can return the indices that were used to sort an array a to get a sorted array, b. I'm asking for the general algorithm implementation behind returning these indices when you're not sorting, but simply scrambling. This must be a known algorithm, but I didn't know what to look for when searching for this.
E.g. say I have an array a == [7,6,8,9], and I set b=a[3,4,2,1] so b == [8,9,6,7]. Given this information, what is the most efficient code that would take me back from b to a (i.e. code that would yield the array i = [4,3,1,2], because b[i] == a). This should be applicable to an array of any size. I'm asking for an explanation and code in either MATLAB or Python, thanks!
In Matlab, just use the second output of sort:
ind = [3,4,2,1];
[~, result] = sort(ind);
Or, as suggested by #CrisLuengo, you can do it just with indexing:
result(ind) = 1:numel(ind);
Either of the above gives
result =
4 3 1 2
Python Code :
a = [7, 6, 8, 9, 8]
b = [8, 8, 9, 7, 6]
i = []
for x in a:
index = b.index(x)
i.append(index)
# for duplicates
b[index] = -1
print(i)
Output :
[3, 4, 0, 2, 1]
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
Write a function filter_mult5 that takes a list of integers called numbers, and returns another list that contains all the integers in numbers which are not multiples of 5:
def filter_mult5(numbers):
return #a list of all numbers that are not a multiple of 5
For example, filter_mult5([4,6,8,10,12]) should return [4,6,8,12]
You can use a so called list comprehension and filter out the multiples of five by using the modulus operator (multiples of five yield zero if passed to the modulus operator) as follows:
def filter_mult5(numbers):
return [n for n in numbers if n%5 != 0]
# Test
>>> filter_mult5(list(range(12)))
[1, 2, 3, 4, 6, 7, 8, 9, 11] # 0, 5, 10 are filtered
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.