(wx)Maxima: list factors of an integer - list

How can one return the factors of an integer in a list? e.g. list_factors(6); > [1,2,3,6]? Is something like this possible? I looked through the documentation but didn't find anything like this tied to "factor" or "prime".

I think this is what you want:
listify(divisors(6)); > [1, 2, 3, 6]

Related

Find sum of number in multiple sets using exactly one number of each set

Background
Hi, I'm trying to solve a programming problem and I'm stuck on the following problem:
Assume you have multiple lists of numbers. All are sorted in decreasing order.
You now have to take exactly one number from each list to make the biggest possible sum.
So far so easy, to solve this you could just take the first number of each list and you're done.
But now, I need the second-largest sum while still using exactly one number from each list.
To achieve this, I would take the first element in each list but for the list which has the least difference between the first and second number the second number will be used.
This is still pretty doable.
The Problem
But I need an Iterator over every possible sum using exactly one number of each list sorted in decreasing order.
For performance reasons, it isn't possible to just compute every sum and then sort it. The algorithm must already provide the sums in decreasing order. If there are multiple combinations for a sum then the sum must be returned multiple times.
Additional Requirements
The Iterator should be lazy (only calculate the next sum when required).
The Lists are already lazy, which means you should require as few values as possible to calculate the fitting sum.
Example
For the Lists:
List 1: [5, 2, 1]
List 2: [10, 2]
List 3: [6, 1]
The Iterator then should return:
[5, 10, 6] = 21
[2, 10, 6] = 18
[1, 10, 6] = 17
[5, 10, 1] = 16
[5, 2, 6] = 13
[2, 10, 1] = 13
[1, 10, 1] = 12
[2, 2, 6] = 10
[1, 2, 6] = 9
[5, 2, 1] = 8
[2, 2, 1] = 5
[1, 2, 1] = 4
Comment
I don't need code as an answer to my question (you're still welcome to provide it if it helps to explain). What I'm looking for are ideas to solve this, or solutions that I can implement myself.
Thanks in advance!
First of all, Thanks to wlui155 for the help.
For Anyone interested, I coded a BFS algorithm that acts as follows:
Definitions:
Entry: Struct containing indices of used numbers and sum
BSet: Ordered set which can only contain unique Entries
Algorithm:
Pop Entry with biggest sum from BSet
Create a clone for each list
Advance in each clone a different index by one
Put new entries in BSet
Print current Entry
Goto 1.
Now you only have to ensure that no entry appears again after you've popped it. This can be ensured with a separate set containing all combinations for the current sum. Once the current sum gets smaller this set can be cleared.
If you have ideas to improve this, you're welcome to tell me.

Python 3 list with range and other individual numbers

I need to make a list of numbers. These numbers represent binary masks. The first 100 or so masks are all included in this range. In the next group of masks only certain masks are included. I need a list similar to the following.
[1,2,3,5,6,7,8,9,10,30,34,48,53,62]
Can I do something like [range(1,10),30,34,48,53,62]
or do I need to create my list using range(1,10) and then append the next list to it?
Thanks
Python 3 actually allow you to build a list literal prepending an * to any iterable objects - which are in turn expanded in place:
>>> [1,2, *range(10), *range(2)]
[1, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
If you need this n older Pythons, or if you'd prefer to keep readability for people not too proeficient in Python who might have to walk through your code, an option is just to concatenate your different list fragments using the + operator:
a = list(range(1,10)) + [ 30,34,48,53,62]
Looks like I had to use the list(range(1,10)+[47,34,57]
solution

Creation of unexpected tuple value

I am dealing with somewhat of a mystery and hoped for some clarity. I wrote a script for finding dice roll combinations adding to 24 that looks like the following:
start=[3,3,3,3,3,3,3,3]
outcomes=set(tuple(start)) #Use a set to ensure uniqueness
index_list=np.random.randint(0,8,1000)
#This little snippet adds one and subtracts one randomly, keeping total at 24
for i in xrange(0,500):
upper=index_list[i]
downer=index_list[i+20]
if start[upper]!=6 and start[downer]!=1:
start[upper]=start[upper]+1
start[downer]=start[downer]-1
outcomes.add(tuple(start))
print outcomes
What I am running into, is that When I look at outcomes, there is a single 3 of type 'int' in there.
set([(4, 4, 4, 3, 2, 2, 2, 3), 3, (2, 5, 4, 3, 1, 4, 2, 3), (4, 4, 4, 2, 3, 1, 3, 3),(4, 2, 5, 2, 3, 4, 1, 3)])
While I could certainly remove it, I am just curious how it is getting in there to begin with? My initial guess was the index list might be producing an index outside of [0-7], but it is not. I've looked for a similar question other places, but have yet to find a similar issue. Thanks!
set expects an iterable. You're passing a tuple which is an iterable.
set iterates through it, leaving just 1 value: 3 (because your tuple only contains the same 3 value).
You have to put your element in a list or tuple so it is seen as a single element (exactly the same problem when you pass a string and it is unexpectedly iterated upon)
The rest of your code is OK and has nothing to do with the problem.
Do this instead:
outcomes=set([tuple(start),])
now set iterates through a list of 1 tuple, effectively creating tuple elements.
You could do that also, maybe simpler:
outcomes=set()
outcomes.add(tuple(start))
there's no ambiguity since you're adding 1 element. It's not iterated through.

Scan list in prolog

I want to scan a list in Prolog.
In particular, I want to write a predicate scan_list (list), and I want to make it check to see if the current element is a positive integer and if so print it.
Thank's.
If this is homework, be assured that the only way to learn any programming language is to practice it and think about the assignments. However, here is a version that might be, what you want
scan_list([]).
scan_list([H|T]) :- H > 0,!, print(H),nl,scan_list(T).
scan_list([_|T]) :- scan_list(T).
It works like that:
?- scan_list([1,-2,7,9,0,-1,14]).
1
7
9
14
yes
In SWI-Prolog there is include/3, e.g. you can write
?- include(<(0), [1, -2, 7, 9, 0, -1, 14, 0.8], L).
L = [1, 7, 9, 14, 0.8].
(Warning: this particular code accepts more numbers than positive integers.)

Appending Nested Lists in Python

I have the following question for homework
Define a function append lists that
takes a list of lists and returns a
new list containing the sublist
values. For example, append lists([[1,
2], [3, 4], [5]]) should return the
list [1, 2, 3, 4, 5] and append
lists([[1, 2], [3], [[4, 5]]]) should
return the list [1, 2, 3, [4, 5]].
I've tried various ways of creating this function in order to append the list so it gives the desired output to no avail so I came here looking for some help. I've found a few other ways of going about this online, but they use extensive methods that we haven't even dabbled in as of yet in my CPSC 121 class. We're limited to the basics in what we've learned.
Any help would be much appreciated!
By now, it is likely that the assignment is gone, but here is a solution:
def append_lists(lists):
output = []
for l in lists:
for e in l:
output.append(e)
return output
This appends each element of each list to the output of the function, which eliminates exactly one level of nesting in the elements.