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 5 years ago.
Improve this question
create a list or an array of random integers (e.g., between [0, 50]) and a target number (e.g., 60), then find all subsets of the integers of the list/array such that the numbers in each subsets add up to the target number. Print the numbers of each subset, you also need to print the original indices of the numbers. For example, given array A = {5, 3, 4, 2, 6, 7}, target = 9, the number pairs are (5, 4), (3, 6), (2, 7), and (3, 4, 2). Their indices are (0, 2), (1, 4), (3, 5), and (1, 2, 3), respectively. (Optional) If you implement the programs using both array and list, compare the difference between sorting array and sorting list in Lisp.
Well, obviously you are missing a clear question here. I would consider that you think about what SO is meant to be and how others can participate the most from your questions and the given answers.
Nevertheless, while you are thinking about the problem you could start with something like this:
(defun random-list (&optional (len 10) (limit 50))
"Returns a list of length LEN with random integers upto LIMIT."
(loop :for n :from 1 :upto len
:collect (1+ (random limit))))
Another way would be to implement it as a recursive function. There are great online resources given in the info section of the CL tag.
If you are facing difficulties while implementing the complete tasks, I am sure you will find help here.
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 6 months ago.
Improve this question
I am interested in taking a small sequence of numbers, for instance: -1, 0, -1.
And then looking within a larger dataset to find the most similar sequence of numbers within it. For example, the larger dataset could be: 1, -1, 0, -1, -1, 0, 0
The most similar sequence within it would be: 1, -1, 0, -1, -1, 0, 0
I believe the best strategy is to separate the larger dataset into several strings of the same length as the smaller dataset, in this case a length of 3, and then compare the smaller dataset to each of these strings and find the ones with the highest correlation. I would like to know which one is the closest, second-closest, third-closest, etc.
One key thing, I'm interested in which string has the most similar shape visually.
Please see my image below for a visualization of what I'm looking for:
I am a beginner, so if you could write out the code for me I would hugely appreciate it.
By the way, I am hoping to apply this function to much larger datasets than the one in this example.
Thank you!
It's not clear what format you would like the answer in. Also, the notion of closest "shape" of data is too vague to encode. There are too many ways to interpret this. A simple Euclidean distance between the shorter vector and chunks of the longer vector of the same length makes most sense mathematically. You could code that like this:
closest_match <- function(needle, haystack) {
ln <- length(needle)
dist <- sapply(seq(length(haystack) - ln + 1) - 1, function(i) {
sqrt(sum((haystack[i + seq(ln)] - needle)^2))
})
list(index = which.min(dist),
closest_sequence = haystack[which.min(dist) + seq(ln) -1])
}
And test it using your example vectors.
closest_match(c(-1, 0, -1), c(1, -1, 0, -1, -1, 0, 0))
#> $index
#> [1] 2
#>
#> $closest_sequence
#> [1] -1 0 -1
Here, index is the index of the long vector where the best match starts and closest_sequence is the actual best-fitting sequence within the longer vector.
Created on 2022-08-27 with reprex v2.0.2
I had an interview yesterday and was asked to give a method to find all of the pairs of numbers from a list which add up to an integer which is given separate to the list. The list can be infinitely long, but for example:
numbers = [11,1,5,27,7,18,2,4,8]
sum = 9
pairs = [(1,8),(5,4),(7,2)]
I got as far as sorting the list and eliminating all numbers greater than the sum number and then doing two nested for loops to take each index and iterate through the other numbers to check whether they sum up to the given number, but was told that there was a more efficient way of doing it...
I've been trying to figure it out but have nothing, other than doing the nested iteration backwards but that only seems marginally more efficient.
Any ideas?
This can be done in O(n) time and O(n) auxiliary space; testing for membership of a set takes O(1) time. Since the output also takes up to O(n) space, the auxiliary space should not be a significant issue.
def pairs_sum(numbers, k):
numbers_set = set(numbers)
return [(x, y) for x in numbers if (y := k - x) in numbers_set and x < y]
Example:
>>> pairs_sum([11, 1, 5, 27, 7, 18, 2, 4, 8], 9)
[(1, 8), (2, 7), (4, 5)]
It is kind of a classic and not sure stackoverflow is the right place to ask that kind of question.
Sort the list is acsending order
Two iterators one starting from the end of the list descending i1, one starting from the beginning of the list ascending i2
Loop
while i1 > i2
if (list[i1] + list[i2] == target)
store {list[i1], list[i2]) in results pairs
i1--
i2++
else if (list[i1] + list[i2] > target)
i1--
else if (list[i1] + list[i2] < target)
i2++
This should be in O(n) with n the length of the list if you avoid the sorting algorithm which can be done with a quick sort on average in O(n log n)
Note: this algorithm doesn't take into account the case where the input list have several times the same number
I want to create a vector/array of length n to be filled afterwards.
How can I do that?
And does it have to be filled already with something?
For example if you want a Vector of Ints of length 10 you can write
v = Vector{Int}(undef, 10)
And more general for an Array of Ints of dimensions (2, 3, 4)
a = Array{Int}(undef, (2, 3, 4))
Note that this fills the Vector/Array with garbage values, so this can be a bit dangerous. As an alternative you can use
v = Vector{Int}()
sizehint!(v, 10)
push!(v, 1) # add a one to the end of the Vector
append!(v, (2, 3, 4, 5, 6, 7, 8, 9, 10)) # add values 2 to 9 to the end of the vector
sizehint! is not necessary, but it can improve performance, because it tells Julia to expect 10 values.
There are other functions such as zeros, ones or fill that can provide a Vector/Array with already filled in data.
Just as a side note, this isn't asking someone to help with homework it's just a example question in my lecture notes that i'm not grasping and would greatly appreciate if someone could explain to me how exactly to work it out.
It says simply this:
Let G = {3,5,7}. Write down some examples of 4-tuples.
Thank you to anyone who tries to help, this is mathematics to understand a systems unit :)
Your collection G is a set, which is unordered and non-repeating, containing three elements. You want some 4-tuples, which is an ordered collection of possibly-repeating elements, and there must be 4 elements.
We show tuples by using parentheses around the collection, while a set like G is written using braces (curly brackets). Some examples of 4-tuples using the elements of G are
(3, 3, 3, 3)
(3, 3, 3, 5)
(3, 3, 3, 7)
(3, 3, 5, 3)
...
(7, 7, 7, 5)
(7, 7, 7, 7)
That list of mine was in a particular order, called the lexigraphical order. Since there are 4 elements and each element has 3 choices regardless of the other choices, the total number of 4-tuples is
3x3x3x3 = 81
As another answer implied, your question is somewhat ambiguous. I assumed that each 4-tuple was to have elements taken from your set G, but your question did not actually say that. It does seem to be implied, however.
A 4-tuple, in math, is any vector populated with 4 objects. An example of a 4-tuple is {1, 5, 3, 7}.
Without more context, I can't use the fact that G = {3, 5, 7}.
so below I have some mathematica code and I would like to know how to do this in R. For those not familiar with mathematica I will explain what this code does.
We are looking at the permutations of the list (1,2,3,4).
We generate a list of these permutations: {(1,2,3,4),(1,2,4,3)...etc}.
Then for each permutation, we check to see how many of the elements are in the right spot. E.g. if the current permutation is (4,2,3,1) then 2 and 3 are both in the correct spot and I want to recode this as the value 2, (1,4,2,3) would be 1, etc
then I want to randomly sample 1000000 times from these recoded list.
I know in R this last part is in the form of
sample(vectorToSample,10^6,replace=True)
Here it the mathematica code
RandomChoice[
Table[
Total[
Table[Permutations[{1, 2, 3, 4}][[i]][[j]] == j, {j, 1,4}]
]
, {i, 1,24}]/. {True -> 1,False -> 0}
, 10^6]
Or in a much cleaner way
RandomChoice[{9/24, 8/24, 6/24, 1/24} -> {0, 1, 2, 4}, 10^6]