bottom-up minimal-change algorithm issueAlgorithm to return all combinations of k elements from nWhat is the best algorithm for an overridden System.Object.GetHashCode?What's the Hi/Lo algorithm?What is the difference between a generative and a discriminative algorithm?Ukkonen's suffix tree algorithm in plain EnglishImage Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionHow to find time complexity of an algorithmWhat is the optimal algorithm for the game 2048?how to avoid memory error in generating and processing python permutations?Unique combinations of numbers that add up to a sum
How can I fix/modify my tub/shower combo so the water comes out of the showerhead?
How can saying a song's name be a copyright violation?
Is it possible to create light that imparts a greater proportion of its energy as momentum rather than heat?
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
What killed these X2 caps?
Stopping power of mountain vs road bike
Is it possible to run Internet Explorer on OS X El Capitan?
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
Facing a paradox: Earnshaw's theorem in one dimension
What's the difference between 'rename' and 'mv'?
Where does SFDX store details about scratch orgs?
What does it mean to describe someone as a butt steak?
I would say: "You are another teacher", but she is a woman and I am a man
Why is the 'in' operator throwing an error with a string literal instead of logging false?
1960's book about a plague that kills all white people
Western buddy movie with a supernatural twist where a woman turns into an eagle at the end
Why does Arabsat 6A need a Falcon Heavy to launch
What mechanic is there to disable a threat instead of killing it?
Is it canonical bit space?
How to say in German "enjoying home comforts"
How is it possible to have an ability score that is less than 3?
What's the point of deactivating Num Lock on login screens?
How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?
Arrow those variables!
bottom-up minimal-change algorithm issue
Algorithm to return all combinations of k elements from nWhat is the best algorithm for an overridden System.Object.GetHashCode?What's the Hi/Lo algorithm?What is the difference between a generative and a discriminative algorithm?Ukkonen's suffix tree algorithm in plain EnglishImage Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionHow to find time complexity of an algorithmWhat is the optimal algorithm for the game 2048?how to avoid memory error in generating and processing python permutations?Unique combinations of numbers that add up to a sum
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Im trying to get all the permutations of the number 4 so like 1,12,21,123,132,...1234,1243,1423,4123,...ect (there are 24) and we have to use the bottom-up minimal-change algrothem but in my code when it should be moving on it gets stuck at 1,2 and 2,1 sooo i was wondering if anyone could help. My code is a method that you call and give a number then it should give you back a list of all the permutations. All the print statments where for debugging so in the end they wont be there. Any help would be amazing! Thanks
def min_change(num):
end=[]
A=[]
for i in range(1,num):
A.append(i)
print("appened i",A)
end.append(A)
for j in end:
print("entered j",j)
if len(j)==i:
print("made past if")
if len(j)!=1:
x=len(j)-1
while x>0:
print("this is x ",x)
B=A
end.append(B)
temp=B[x-1]
B[x-1]=B[x]
B[x]=temp
print(B)
x=x-1
return end
python-3.x algorithm repeating
|
show 1 more comment
Im trying to get all the permutations of the number 4 so like 1,12,21,123,132,...1234,1243,1423,4123,...ect (there are 24) and we have to use the bottom-up minimal-change algrothem but in my code when it should be moving on it gets stuck at 1,2 and 2,1 sooo i was wondering if anyone could help. My code is a method that you call and give a number then it should give you back a list of all the permutations. All the print statments where for debugging so in the end they wont be there. Any help would be amazing! Thanks
def min_change(num):
end=[]
A=[]
for i in range(1,num):
A.append(i)
print("appened i",A)
end.append(A)
for j in end:
print("entered j",j)
if len(j)==i:
print("made past if")
if len(j)!=1:
x=len(j)-1
while x>0:
print("this is x ",x)
B=A
end.append(B)
temp=B[x-1]
B[x-1]=B[x]
B[x]=temp
print(B)
x=x-1
return end
python-3.x algorithm repeating
Have you tried doing the permutations of 2, then 3...?
– barny
Mar 21 at 22:43
why shouldlen(j)==i
? Isj
a list?
– barny
Mar 21 at 22:46
2
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. Most notably, "make it easy for others to help you". You've used meaningless variable names to hand us an undocumented algorithm. Your code has an infinite loop, and much of the useful information scrolls out of range.
– Prune
Mar 22 at 0:00
So the code is doing every permutation up to the number entered and the reason why I have len(j)==i is so it doesnt touch say the perms of 3 when on 4
– Bruce Craig
Mar 22 at 0:22
Try printinglen(j)
and convince mej
is a list then tell me what the comparisonlen(j)==i
is doing.
– barny
Mar 23 at 21:23
|
show 1 more comment
Im trying to get all the permutations of the number 4 so like 1,12,21,123,132,...1234,1243,1423,4123,...ect (there are 24) and we have to use the bottom-up minimal-change algrothem but in my code when it should be moving on it gets stuck at 1,2 and 2,1 sooo i was wondering if anyone could help. My code is a method that you call and give a number then it should give you back a list of all the permutations. All the print statments where for debugging so in the end they wont be there. Any help would be amazing! Thanks
def min_change(num):
end=[]
A=[]
for i in range(1,num):
A.append(i)
print("appened i",A)
end.append(A)
for j in end:
print("entered j",j)
if len(j)==i:
print("made past if")
if len(j)!=1:
x=len(j)-1
while x>0:
print("this is x ",x)
B=A
end.append(B)
temp=B[x-1]
B[x-1]=B[x]
B[x]=temp
print(B)
x=x-1
return end
python-3.x algorithm repeating
Im trying to get all the permutations of the number 4 so like 1,12,21,123,132,...1234,1243,1423,4123,...ect (there are 24) and we have to use the bottom-up minimal-change algrothem but in my code when it should be moving on it gets stuck at 1,2 and 2,1 sooo i was wondering if anyone could help. My code is a method that you call and give a number then it should give you back a list of all the permutations. All the print statments where for debugging so in the end they wont be there. Any help would be amazing! Thanks
def min_change(num):
end=[]
A=[]
for i in range(1,num):
A.append(i)
print("appened i",A)
end.append(A)
for j in end:
print("entered j",j)
if len(j)==i:
print("made past if")
if len(j)!=1:
x=len(j)-1
while x>0:
print("this is x ",x)
B=A
end.append(B)
temp=B[x-1]
B[x-1]=B[x]
B[x]=temp
print(B)
x=x-1
return end
python-3.x algorithm repeating
python-3.x algorithm repeating
asked Mar 21 at 21:50
Bruce CraigBruce Craig
62
62
Have you tried doing the permutations of 2, then 3...?
– barny
Mar 21 at 22:43
why shouldlen(j)==i
? Isj
a list?
– barny
Mar 21 at 22:46
2
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. Most notably, "make it easy for others to help you". You've used meaningless variable names to hand us an undocumented algorithm. Your code has an infinite loop, and much of the useful information scrolls out of range.
– Prune
Mar 22 at 0:00
So the code is doing every permutation up to the number entered and the reason why I have len(j)==i is so it doesnt touch say the perms of 3 when on 4
– Bruce Craig
Mar 22 at 0:22
Try printinglen(j)
and convince mej
is a list then tell me what the comparisonlen(j)==i
is doing.
– barny
Mar 23 at 21:23
|
show 1 more comment
Have you tried doing the permutations of 2, then 3...?
– barny
Mar 21 at 22:43
why shouldlen(j)==i
? Isj
a list?
– barny
Mar 21 at 22:46
2
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. Most notably, "make it easy for others to help you". You've used meaningless variable names to hand us an undocumented algorithm. Your code has an infinite loop, and much of the useful information scrolls out of range.
– Prune
Mar 22 at 0:00
So the code is doing every permutation up to the number entered and the reason why I have len(j)==i is so it doesnt touch say the perms of 3 when on 4
– Bruce Craig
Mar 22 at 0:22
Try printinglen(j)
and convince mej
is a list then tell me what the comparisonlen(j)==i
is doing.
– barny
Mar 23 at 21:23
Have you tried doing the permutations of 2, then 3...?
– barny
Mar 21 at 22:43
Have you tried doing the permutations of 2, then 3...?
– barny
Mar 21 at 22:43
why should
len(j)==i
? Is j
a list?– barny
Mar 21 at 22:46
why should
len(j)==i
? Is j
a list?– barny
Mar 21 at 22:46
2
2
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. Most notably, "make it easy for others to help you". You've used meaningless variable names to hand us an undocumented algorithm. Your code has an infinite loop, and much of the useful information scrolls out of range.
– Prune
Mar 22 at 0:00
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. Most notably, "make it easy for others to help you". You've used meaningless variable names to hand us an undocumented algorithm. Your code has an infinite loop, and much of the useful information scrolls out of range.
– Prune
Mar 22 at 0:00
So the code is doing every permutation up to the number entered and the reason why I have len(j)==i is so it doesnt touch say the perms of 3 when on 4
– Bruce Craig
Mar 22 at 0:22
So the code is doing every permutation up to the number entered and the reason why I have len(j)==i is so it doesnt touch say the perms of 3 when on 4
– Bruce Craig
Mar 22 at 0:22
Try printing
len(j)
and convince me j
is a list then tell me what the comparison len(j)==i
is doing.– barny
Mar 23 at 21:23
Try printing
len(j)
and convince me j
is a list then tell me what the comparison len(j)==i
is doing.– barny
Mar 23 at 21:23
|
show 1 more comment
1 Answer
1
active
oldest
votes
You can use itertools for that task:
# Option 1.
list(it.chain(*(it.permutations(range(1, i+1)) for i in range(1, 5))))
# Option 2.
list(j for i in range(1, 5) for j in it.permutations(range(1, i+1)))
The output are the 33 different possible permutations:
[(1,),
(1, 2),
(2, 1),
(1, 2, 3),
(1, 3, 2),
(2, 1, 3),
(2, 3, 1),
(3, 1, 2),
(3, 2, 1),
(1, 2, 3, 4),
(1, 2, 4, 3),
(1, 3, 2, 4),
(1, 3, 4, 2),
(1, 4, 2, 3),
(1, 4, 3, 2),
(2, 1, 3, 4),
(2, 1, 4, 3),
(2, 3, 1, 4),
(2, 3, 4, 1),
(2, 4, 1, 3),
(2, 4, 3, 1),
(3, 1, 2, 4),
(3, 1, 4, 2),
(3, 2, 1, 4),
(3, 2, 4, 1),
(3, 4, 1, 2),
(3, 4, 2, 1),
(4, 1, 2, 3),
(4, 1, 3, 2),
(4, 2, 1, 3),
(4, 2, 3, 1),
(4, 3, 1, 2),
(4, 3, 2, 1)]
But this is not using bottom-up minimal change algorithm
– Onyambu
Mar 21 at 22:41
I think you missed the point of the OP’s question: he is trying to write code to solve this problem himself, and is asking for help with that code.
– barny
Mar 21 at 22:42
This does get me what I want but I have to use the bottom up way to do so I cant use any tools but thanks for trying
– Bruce Craig
Mar 21 at 22:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55289783%2fbottom-up-minimal-change-algorithm-issue%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use itertools for that task:
# Option 1.
list(it.chain(*(it.permutations(range(1, i+1)) for i in range(1, 5))))
# Option 2.
list(j for i in range(1, 5) for j in it.permutations(range(1, i+1)))
The output are the 33 different possible permutations:
[(1,),
(1, 2),
(2, 1),
(1, 2, 3),
(1, 3, 2),
(2, 1, 3),
(2, 3, 1),
(3, 1, 2),
(3, 2, 1),
(1, 2, 3, 4),
(1, 2, 4, 3),
(1, 3, 2, 4),
(1, 3, 4, 2),
(1, 4, 2, 3),
(1, 4, 3, 2),
(2, 1, 3, 4),
(2, 1, 4, 3),
(2, 3, 1, 4),
(2, 3, 4, 1),
(2, 4, 1, 3),
(2, 4, 3, 1),
(3, 1, 2, 4),
(3, 1, 4, 2),
(3, 2, 1, 4),
(3, 2, 4, 1),
(3, 4, 1, 2),
(3, 4, 2, 1),
(4, 1, 2, 3),
(4, 1, 3, 2),
(4, 2, 1, 3),
(4, 2, 3, 1),
(4, 3, 1, 2),
(4, 3, 2, 1)]
But this is not using bottom-up minimal change algorithm
– Onyambu
Mar 21 at 22:41
I think you missed the point of the OP’s question: he is trying to write code to solve this problem himself, and is asking for help with that code.
– barny
Mar 21 at 22:42
This does get me what I want but I have to use the bottom up way to do so I cant use any tools but thanks for trying
– Bruce Craig
Mar 21 at 22:43
add a comment |
You can use itertools for that task:
# Option 1.
list(it.chain(*(it.permutations(range(1, i+1)) for i in range(1, 5))))
# Option 2.
list(j for i in range(1, 5) for j in it.permutations(range(1, i+1)))
The output are the 33 different possible permutations:
[(1,),
(1, 2),
(2, 1),
(1, 2, 3),
(1, 3, 2),
(2, 1, 3),
(2, 3, 1),
(3, 1, 2),
(3, 2, 1),
(1, 2, 3, 4),
(1, 2, 4, 3),
(1, 3, 2, 4),
(1, 3, 4, 2),
(1, 4, 2, 3),
(1, 4, 3, 2),
(2, 1, 3, 4),
(2, 1, 4, 3),
(2, 3, 1, 4),
(2, 3, 4, 1),
(2, 4, 1, 3),
(2, 4, 3, 1),
(3, 1, 2, 4),
(3, 1, 4, 2),
(3, 2, 1, 4),
(3, 2, 4, 1),
(3, 4, 1, 2),
(3, 4, 2, 1),
(4, 1, 2, 3),
(4, 1, 3, 2),
(4, 2, 1, 3),
(4, 2, 3, 1),
(4, 3, 1, 2),
(4, 3, 2, 1)]
But this is not using bottom-up minimal change algorithm
– Onyambu
Mar 21 at 22:41
I think you missed the point of the OP’s question: he is trying to write code to solve this problem himself, and is asking for help with that code.
– barny
Mar 21 at 22:42
This does get me what I want but I have to use the bottom up way to do so I cant use any tools but thanks for trying
– Bruce Craig
Mar 21 at 22:43
add a comment |
You can use itertools for that task:
# Option 1.
list(it.chain(*(it.permutations(range(1, i+1)) for i in range(1, 5))))
# Option 2.
list(j for i in range(1, 5) for j in it.permutations(range(1, i+1)))
The output are the 33 different possible permutations:
[(1,),
(1, 2),
(2, 1),
(1, 2, 3),
(1, 3, 2),
(2, 1, 3),
(2, 3, 1),
(3, 1, 2),
(3, 2, 1),
(1, 2, 3, 4),
(1, 2, 4, 3),
(1, 3, 2, 4),
(1, 3, 4, 2),
(1, 4, 2, 3),
(1, 4, 3, 2),
(2, 1, 3, 4),
(2, 1, 4, 3),
(2, 3, 1, 4),
(2, 3, 4, 1),
(2, 4, 1, 3),
(2, 4, 3, 1),
(3, 1, 2, 4),
(3, 1, 4, 2),
(3, 2, 1, 4),
(3, 2, 4, 1),
(3, 4, 1, 2),
(3, 4, 2, 1),
(4, 1, 2, 3),
(4, 1, 3, 2),
(4, 2, 1, 3),
(4, 2, 3, 1),
(4, 3, 1, 2),
(4, 3, 2, 1)]
You can use itertools for that task:
# Option 1.
list(it.chain(*(it.permutations(range(1, i+1)) for i in range(1, 5))))
# Option 2.
list(j for i in range(1, 5) for j in it.permutations(range(1, i+1)))
The output are the 33 different possible permutations:
[(1,),
(1, 2),
(2, 1),
(1, 2, 3),
(1, 3, 2),
(2, 1, 3),
(2, 3, 1),
(3, 1, 2),
(3, 2, 1),
(1, 2, 3, 4),
(1, 2, 4, 3),
(1, 3, 2, 4),
(1, 3, 4, 2),
(1, 4, 2, 3),
(1, 4, 3, 2),
(2, 1, 3, 4),
(2, 1, 4, 3),
(2, 3, 1, 4),
(2, 3, 4, 1),
(2, 4, 1, 3),
(2, 4, 3, 1),
(3, 1, 2, 4),
(3, 1, 4, 2),
(3, 2, 1, 4),
(3, 2, 4, 1),
(3, 4, 1, 2),
(3, 4, 2, 1),
(4, 1, 2, 3),
(4, 1, 3, 2),
(4, 2, 1, 3),
(4, 2, 3, 1),
(4, 3, 1, 2),
(4, 3, 2, 1)]
answered Mar 21 at 22:30
a_guesta_guest
7,12231345
7,12231345
But this is not using bottom-up minimal change algorithm
– Onyambu
Mar 21 at 22:41
I think you missed the point of the OP’s question: he is trying to write code to solve this problem himself, and is asking for help with that code.
– barny
Mar 21 at 22:42
This does get me what I want but I have to use the bottom up way to do so I cant use any tools but thanks for trying
– Bruce Craig
Mar 21 at 22:43
add a comment |
But this is not using bottom-up minimal change algorithm
– Onyambu
Mar 21 at 22:41
I think you missed the point of the OP’s question: he is trying to write code to solve this problem himself, and is asking for help with that code.
– barny
Mar 21 at 22:42
This does get me what I want but I have to use the bottom up way to do so I cant use any tools but thanks for trying
– Bruce Craig
Mar 21 at 22:43
But this is not using bottom-up minimal change algorithm
– Onyambu
Mar 21 at 22:41
But this is not using bottom-up minimal change algorithm
– Onyambu
Mar 21 at 22:41
I think you missed the point of the OP’s question: he is trying to write code to solve this problem himself, and is asking for help with that code.
– barny
Mar 21 at 22:42
I think you missed the point of the OP’s question: he is trying to write code to solve this problem himself, and is asking for help with that code.
– barny
Mar 21 at 22:42
This does get me what I want but I have to use the bottom up way to do so I cant use any tools but thanks for trying
– Bruce Craig
Mar 21 at 22:43
This does get me what I want but I have to use the bottom up way to do so I cant use any tools but thanks for trying
– Bruce Craig
Mar 21 at 22:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55289783%2fbottom-up-minimal-change-algorithm-issue%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you tried doing the permutations of 2, then 3...?
– barny
Mar 21 at 22:43
why should
len(j)==i
? Isj
a list?– barny
Mar 21 at 22:46
2
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. Most notably, "make it easy for others to help you". You've used meaningless variable names to hand us an undocumented algorithm. Your code has an infinite loop, and much of the useful information scrolls out of range.
– Prune
Mar 22 at 0:00
So the code is doing every permutation up to the number entered and the reason why I have len(j)==i is so it doesnt touch say the perms of 3 when on 4
– Bruce Craig
Mar 22 at 0:22
Try printing
len(j)
and convince mej
is a list then tell me what the comparisonlen(j)==i
is doing.– barny
Mar 23 at 21:23