It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have just finished creating a game of life simulation for c# and found one of the most confusing aspects was whether the seed I was using would actually work.
The following are seeds that can be used to ensure that your game of life is working:
//two island pseudo still life
[2, 1] [3, 1] [5, 1] [6, 1]
[3, 2] [5, 2]
[3, 3] [5, 3]
[2, 4] [3, 4] [5, 4] [6, 4]
//hacker emblem
[1, 3]
[2, 1]
[2, 3]
[3, 2]
[3, 3]
The two island pseudo still life should not change.
The hacker emblem should mutate and crawl from top left to bottom right until it hits the corner.
Has anyone got anything better?
I guess this answer is the best for now, i'm happy to update it if anyone replies with anything at all.
Related
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 4 months ago.
def append_arr(arr):
t_arr = arr
print('arr before',arr)
t_arr.extend(arr)
print('arr affter',arr)
arr = ['a','b','c']
append_arr(arr)
I had list a, assign b=a, and change list b by functions (append, insert, extend)
I didn't touch list a any more, but when b change, a is also change follow b.
How to change b by (append, insert, extend) and a not change
def test():
arr_m = ['a','b','c']
print('arr_m before', arr_m)
append_arr(arr_m)
print('arr_m affter', arr_m)
test()
arr_m before ['a', 'b', 'c']
arr before ['a', 'b', 'c']
arr affter ['a', 'b', 'c', 'a', 'b', 'c']
arr_m affter ['a', 'b', 'c', 'a', 'b', 'c']
I don't know why arr_m change too
You are expecting t_arr = arr to make a new list which has the same contents, but all it does is make a second name which refers to the same list.
It works like this because:
A lot of programming languages were designed based on how computers work. It is normal to think of lists as things, but at the low level in a computer a list is more like a place - the spot in memory where all of the list's contents are stored. To this day many programmers are used to "place-oriented programming" (though almost nobody calls it that), and Python was written way back in 1991 or so.
Until recently, languages where everything was immutable, or where assignment did a copy by default, were often far less efficient - it takes some tricky optimizations to turn code that semantically always makes a copy into machine operations that only copy the minimum of data only when they need to. So back when Python was new, it would've been a lot slower and would've used a lot more memory if it had been designed to make copies all the time.
Programmer culture has not yet finished understanding "the value of values" (a great video by Rich Hickey, the creator of the language Clojure which unlike Python doesn't behave this way) - so we've still got a lot of place-oriented shared-mutable-state-by-default languages in widespread use and most people don't see it as a big deal or major problem.
But now you might be thinking "but why doesn't that happen with integers and strings!?" And you'd be right, if you did t_string = string and then t_string += string, it would just work exactly like you expected the list to work. And that's because:
Integers were historically a special case because they're so small in a computer, and so fundamental to computer operations, that it was often more efficient, or in any case efficient enough, to implement them as immutable values. But perhaps also people had an easier time understanding intuitively that it would be insane if x = 0; y = x; x += 1 turned the zero in y into a one as well.
Strings were deliberately made immutable, even though they're basically lists, because by the time Python was being made, the world of programming had seen so many pitfalls (and reactions like yours from new programmers) to strings being mutable that "strings should be immutable" managed to overcome the tradition of mutable state enough, especially for languages that were aiming to be more human-friendly like Python, despite the memory and speed costs.
The linked duplicate question will show you how to make it instead do what you want, and has some other decent elaborations on why it works this way.
This question already has answers here:
Finite comprehension of an infinite list
(3 answers)
How to filter an infinite list in Haskell [duplicate]
(4 answers)
Closed 4 years ago.
I thought this :
[i | i <- [1..], i < 5]
would produce
[1, 2, 3, 4]
just like
take 4 [i | i <- [1..]]
A finite length list.
But it doesn't, it seems to be infinite because any attempt to treat it as a finite list just causes hang (ghci).
I am not sure how to understand this exactly. Is it some kind of infinite generator which simply produces nothing after the fourth item but never stops?
Basically the code keeps generating new items because it doesn't know they can never satisfy the criterion?
You literally told the program to check every element of the infinite list and include only the ones that are less than 5. As you say, the compiler doesn’t realize that no remaining element of the list will ever satisfy the condition. Nor could it, even in theory, create such a proof at runtime if passed an arbitrary list. It just does what you said, and keeps checking every element.
This is not necessarily a bug if the program does not try to evaluate xs in the result 1:2:3:4:xs, due to lazy evaluation. Taking the head should work just fine. If you tell it to find the length of the list or something like that, though, it’s an infinite loop.
One way to do what you (probably?) want is takeWhile, which stops when the condition is no longer true.
I want to understand, What is the internal design strategy so that, it can not allow element insertion in set.
Following link describe that set is implemented using dictionary,where every element of set is a key.
https://docs.python.org
So,why is it not supporting similar operation like update in dictionary.
yes you can, look:
>>> a=set()
>>> a.add(1)
>>> a
{1}
>>> a.update([2,3,4,5])
>>> a
{1, 2, 3, 4, 5}
>>>
I'll take a shot at this...Sets are implemented using dictionaries, but the function is a bit different. However, what do you mean by "can not allow element insertion"? You can insert elements using .update() and .add() (see the documentation: https://docs.python.org/3/library/stdtypes.html#set).
Unless you are referring to immutable sets (i.e. frozenset), in which case that is an entirely different function whose goal is to be, well, immutable, so it doesn't allow updating values.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I was wondering if there are functions in any programming languages that allow you to test set theory. For example a library or series of decent algorithms that can perform Combinatorics on large sets.
I don't need basic push/pop I would like to know for what programming languages do libraries exist for functions like UNION CONCAT INTERSECTIONS and Compliments, and comparison of sub sets for 100k+ element sets.
I know this sounds like a math question... maybe not but I am more looking for a Programming language that is designed to handle large sets quickly, because I know my algorithms will be slow.
The standard Python set type provides these operations. No guarantees that the speed will be what you need, since you haven't stated your performance requirements.
LINQ
Functional languages
R
....
You can use Scala, it has great support of sets! For example:
val set1 = Set(1,2,3,4)
val set2 = Set(3,4,5,6)
set1 & set2 //gives intersection
set1 intersect set2 //also possible to write
set1 | set2 //or set1 union set2 gives union
set1 &~ set2 //or set1 diff set2 gives difference
Also different implementations that good for particular issues, they are SortedSet, BitSet, HashSet and etc.
Lets say I have a function f[x_, y_], and two lists l1, l2. I'd like to evaluate f[x,y] for each pair x,y with x in l1 and y in l2, and I'd like to do it without having to make all pairs of the form {l1[[i]],l2[[j]]}.
Essentially, what I want is something like Map[Map[f[#1, #2]&, l1],l2] where #1 takes values from l1 and #2 takes values from l2, but this doesn't work.
(Motivation: I'm trying to implement some basic Haskell programs in Mathematica. In particular, I'd like to be able to code the Haskell program
isMatroid::[[Int]]->Bool
isMatroid b =and[or[sort(union(xs\\[x])[y]'elem'b|y<-ys]|xs<-b,ys<-b, xs<-x]
I think I can do the rest of it, if I can figure out the original question, but I'd like the code to be Haskell-like. Any suggestions for implementing Haskell-like code in Mathematica would be appreciated.)
To evaluate a function f over all pairs from two lists l1 and l2, use Outer:
In[1]:= Outer[f, {a,b}, {x,y,z}]
Out[1]:= {{f[a,x],f[a,y],f[a,z]}, {f[b,x],f[b,y],f[b,z]}}
Outer by default works at the lowest level of the provided lists; you can also specify a level with an additional argument:
In[2]:= Outer[f, {{1, 2}, {3, 4}}, {{a, b}, {c, d}}, 1]
Out[2]:= {{f[{1,2},{a,b}], f[{1,2},{c,d}]}, {f[{3,4},{a,b}], f[{3,4},{c,d}]}}
Note that this produces a nested list; you can Flatten it if you like.
My original answer pointed to Thread and MapThread, which are two ways to apply a function to corresponding pairs from lists, e.g. MapThread[f,{a,b},{1,2}] == {f[a,1], f[b,2]}.
P.S. I think as you're learning these things, you'll find the documentation very helpful. There are a lot of general topic pages, for example, applying functions to lists and list manipulation. These are generally linked to in the "more about" section at the bottom of specific documentation. This makes it a lot easier to find things when you don't know what they'll be called.
To pick up on OP's request for suggestions about implementing Haskell-like code in Mathematica. A couple of things you'll have to deal with are:
Haskell evaluates lazily, by default Mathematica does not, it's very eager. You'll need to wrestle with Hold[] and its relatives to write lazily evaluating functions, but it can be done. You can also subvert Mathematica's evaluation process and tinker with Prolog and Epilog and such like.
Haskell's type system and type checking are probably more rigorous than Mathematica's defaults, but Mathematica does have the features to implement strict type checking.
I'm sure there's a lot more but I'm not terribly familiar with Haskell.
In[1]:= list1 = Range[1, 5];
In[2]:= list2 = Range[6, 10];
In[3]:= (f ## #) & /# Transpose[{list1, list2}]
Out[3]= {f[1, 6], f[2, 7], f[3, 8], f[4, 9], f[5, 10]}