Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
How can i write a function which has two parameters,and it should generate combination arbitrary range bits for example:
function(n,k) n=range, k=number of 1 digits. if i give 4 and 2 to function input: function(4,2)
output:1100,1010,1001,0110,0011,0101
This gives you a list of random digits:
rands[n_, k_] := Module[{out},
out = Table[0, {n}];
out[[RandomSample[Range[n], k]]] = 1;
out]
rands[4, 2]
(* {1, 1, 0, 0} *)
Not sure what output form you want, but you might use FromDigits.
edit a somewhat cleaner approach..
rands[n_, k_] := Table[0, {n - k}]~Join~Table[1, {k}] // RandomSample
Edit: based on your comment I guess what you want are permutations: (I suppose I took "arbitrary" to mean random..)
p[n_, k_] := Permutations[Table[0, {n - k}]~Join~Table[1, {k}], {n}]
(*
{{0, 0, 1, 1}, {0, 1, 0, 1}, {0, 1, 1, 0}, {1, 0, 0, 1},
{1, 0, 1, 0}, {1, 1, 0, 0}}
*)
function[n_, k_] := Permutations[PadRight[ConstantArray[1, k], n]]
In[72]:= function[4, 2]
Out[72]= {{1, 1, 0, 0}, {1, 0, 1, 0}, {1, 0, 0, 1}, {0, 1, 1, 0}, {0, 1, 0, 1}, {0, 0, 1, 1}}
In[73]:= function[3, 1]
Out[73]= {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
var sudoku = map[int][9]int{ // map of 9 x 9
0: {3, 0, 6, 5, 0, 8, 4, 0, 0},
1: {5, 2, 0, 0, 0, 0, 0, 0, 0},
2: {0, 8, 7, 0, 0, 0, 0, 3, 1},
3: {0, 0, 3, 0, 1, 0, 0, 8, 0},
4: {9, 0, 0, 8, 6, 3, 0, 0, 5},
5: {0, 5, 0, 0, 9, 0, 6, 0, 0},
6: {1, 3, 0, 0, 0, 0, 2, 5, 0},
7: {0, 0, 0, 0, 0, 0, 0, 7, 4},
8: {0, 0, 5, 2, 0, 6, 3, 0, 0},
}
fmt.Println("Old value:", sudoku[1][0])
sudoku[1][0] = append(sudoku[1][0],10)
fmt.Println("New value:", sudoku[1][0])
Value is not changed, error message- cannot assign to sudoku[1][0]
sudoku[1][0] is an int, you can't append to an int, only to slices. Also [9]int is an array, not a slice.
You most likely want to change an element, not append to it, so use a simple assignment:
sudoku[1][0] = 10
This will output (try it on the Go Playground):
Old value: 5
New value: 10
Strongly recommended to take the Go Tour if you're not clear with the basics.
I am trying to extend a function like this: (a 4x4 square)
ListPointPlot3D[Table[{x, y, 0}, {x, 0, 4, 1}, {y, 0, 4, 1}]]
into something like this: (a 4x4x4 cube)
ListPointPlot3D[Table[{x, y, z}, {x, 0, 4, 1}, {y, 0, 4, 1}, {z, 0, 4, 1}]]
by adding a 3rd dimension.
However, the dimensions of the latter seem to be incorrect. It seems to form a 2x2 matrix of 3d points rather than a list.
Any ideas how to fix this?
If you look a bit more closely you'll see that the expression
Table[{x, y, z}, {x, 0, 4, 1}, {y, 0, 4, 1}, {z, 0, 4, 1}]
returns a structure with 5x5x5 triplets. That is exactly what the expression is supposed to return. You can see this if you apply the Dimensions[] function to the returned structure.
There are several ways to turn the table into a list of 125 triplets, one is to use Flatten like this
Flatten[Table[{x, y, z}, {x, 0, 4, 1}, {y, 0, 4, 1}, {z, 0, 4, 1}], 2]
Or you could simply generate your list of triplets directly; for your example one alternative would be
Tuples[Range[0, 4], 3]
Given a character or a string s, generate a result string with n (an integer) repeats of s
Given a list of characters or strings, and a list of the frequencies of their appearance (in correspondence), generate a result string with each string in the list repeated with the desired times as specified in the second list and StringJoin them together. For example, given {"a", "b", "c"} and {1,0,3}, I want to have "accc".
I of course want to have the most efficient way of doing these. Otherwise, my own way is too ugly and slow.
Thank you for your help!
rep[s_String, n_] := StringJoin[ConstantArray[s, n]]
then
rep["f", 3]
(*fff*)
next
chars = {"a", "b", "c"};
freqs = {1, 0, 3};
StringJoin[MapThread[rep, {chars, freqs}]]
gives "accc"
For 1, Table will do what you need.
s = "samplestring";
StringJoin[Table[s, {3}]]
"samplestringsamplestringsamplestring"
But acl's answer using ContantArray is faster, if you care about the last 1/100th second.
Do[StringJoin[Table[s, {30}]];, {10000}] // Timing
{0.05805, Null}
Do[StringJoin[ConstantArray[s, 30]];, {10000}] // Timing
{0.033306, Null}
Do[StringJoin[Table[s, {300}]];, {10000}] // Timing
{0.39411, Null}
Do[StringJoin[ConstantArray[s, 300]];, {10000}] // Timing
{0.163103, Null}
For 2, MapThread will handle cases where the second list is known to be non-negative integers.
StringJoin #
MapThread[Table[#1, {#2}] &, {{"a", "b", "c"} , {1, 0, 3}}]
"accc"
If the second list contains negative integers, these are treated as zeros.
Non-integer elements in the second list are treated as if they are the integer part. I am not sure if this is what you want.
StringJoin #
MapThread[Table[#1, {#2}] &, {{"a", "b", "c"} , {1, 0, 3.7}}]
"accc"
Knowing your application I propose using Inner:
sets = {{0, 0, 0, 4}, {0, 0, 1, 3}, {0, 1, 0, 3}, {0, 1, 1, 2}, {0, 2, 0, 2},
{0, 2, 1, 1}, {1, 0, 0, 3}, {1, 0, 1, 2}, {1, 1, 0, 2}, {1, 1, 1, 1},
{1, 2, 0, 1}, {1, 2, 1, 0}, {2, 0, 0, 2}, {2, 0, 1, 1}, {2, 1, 0, 1},
{2, 1, 1, 0}, {2, 2, 0, 0}};
chars = {"a", "b", "c", "d"};
Inner[ConstantArray[#2, #] &, sets, chars, StringJoin]
{"dddd", "cddd", "bddd", "bcdd", "bbdd", "bbcd", "addd", "acdd",
"abdd", "abcd", "abbd", "abbc", "aadd", "aacd", "aabd", "aabc", "aabb"}
I'm writing a game of life program in mathematica however there is a caveat in that I need to be able to apply the reproduction rules to some percentage of the cells, I want to try a new method using MapAt but liveNeighbors doesn't work elementwise, and I can't think of a way of fixing it without doing exactly what I did before (lots of messy indexing), does anyone have any suggestions? (I am assuming this will be more efficient then the old method, which is listed below, if not please let me know, I am just a beginner!).
What I am trying to do:
Map[ArrayPlot,FixedPointList[MapAt[update[#,liveNeighbors[#]]&,#,coords]&,Board, 1]]
What I have done already:
LifeGame[ n_Integer?Positive, steps_] := Module [{Board, liveNeighbors, update},
Board = Table [Random [Integer], {n}, {n}];
liveNeighbors[ mat_] :=
Apply[Plus,Map[RotateRight[mat,#]&,{{-1,-1},{-1, 0},{-1,1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}]];
update[1, 2] := 1;
update[_, 3] := 1;
update[ _, _] := 0;
SetAttributes[update, Listable];
Seed = RandomVariate[ProbabilityDistribution[0.7 UnitStep[x] + 0.3 UnitStep[x - 1], {x, 0, 1, 1}], {n, n}];
FixedPointList[Table[If[Seed[[i, j]] == 1,update[#[[i, j]], liveNeighbors[#][[i, j]]],#[[i, j]]], {i, n}, {j, n}]&, Board, steps]]]
Thanks!
In[156]:=
LifeGame2[n_Integer?Positive, steps_] :=
Module[{Board, liveNeighbors, update},
Board = RandomInteger[1, {n, n}];
liveNeighbors[mat_] :=
ListConvolve[{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}},
ArrayPad[mat, 1, "Periodic"]];
SetAttributes[update, Listable];
Seed = RandomVariate[BernoulliDistribution[0.3], {n, n}];
update[0, el_, nei_] := el;
update[1, 1, 2] := 1;
update[1, _, 3] := 1;
update[1, _, _] := 0;
FixedPointList[MapThread[update, {Seed, #, liveNeighbors[#]}, 2] &,
Board, steps]
]
This implementation does the same as yours, except is quite a lot faster:
In[162]:= AbsoluteTiming[
res1 = BlockRandom[SeedRandom[11]; LifeGame[20, 100]];]
Out[162]= {6.3476347, Null}
In[163]:= Timing[BlockRandom[Seed[11]; LifeGame2[20, 100]] == res1]
Out[163]= {0.047, True}
Assuming you don't have to roll your own code for a homework problem, have you considered just using the built-in CellularAutomaton function?
Straight from the documentation, the 2D CA rule:
GameOfLife = {224, {2, {{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}}, {1, 1}};
And iterate over a 100x100 grid for 100 steps:
ArrayPlot[CellularAutomaton[GameOfLife, RandomInteger[1, {100, 100}], {{{100}}}]]
It would at least give you a baseline for a speed comparison.
Instead of MapAt, you could use Part with the Span syntax to replace a whole subarray at once:
a = ConstantArray[0, {5, 5}];
a[[2 ;; 4, 2 ;; 4]] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
HTH!
Here you have my golfed version.
I have a problem similar to IntegerPartitions function, in that I want to list all non-negative integer xi's such that, for a given list of integers {c1,c2,...,cn} and an integer n:
x1*c1+x2*c2+...+xn*cn=n
Please share your thoughts. Many thanks.
The built-in function FrobeniusSolve solves the case where the c1, c2, ..., cn are positive integers (and the right hand side is not n):
In[1]:= FrobeniusSolve[{2, 3, 5, 6}, 13]
Out[1]= {{0, 1, 2, 0}, {1, 0, 1, 1}, {1, 2, 1, 0}, {2, 1, 0, 1}, {2,
3, 0, 0}, {4, 0, 1, 0}, {5, 1, 0, 0}}
Is this the case you need, or do you need negative c1, c2, ..., cn also?
Construct your list of ci's and coefficients using
n = 10;
cList = RandomInteger[{1, 20}, n]
xList = Table[Symbol["x" <> ToString[i]], {i, n}]
Then, if there's a set of solutions for non-negative xi's, it will be found by
Reduce[cList.xList == n && And##Thread[xList >= 0], xList, Integers]