Alternating order of items ( no modules ) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Alternating order of items ( no modules )

#your code goes here #Let´s make this example without using modules ( if you want to make it with modules, it is at the end of first example ) #You are given a list of items, and need to find all the possible orders of the items. #The output should be a list, containing all possible orders. #Sample Input #['a', 'b'] #Sample Output #----->[('a', 'b'), ('b', 'a')] #Who wish to improves mind and logic, trial so: items = ['x', 'y'] newItems = []; i = 0; elements = len( items ); posiciton = elements - 1; while ( posiciton >= i ): newItems.append( items[ posiciton ] ); posiciton=posiciton-1; result = "" resultNewItems = "" finalResult = "" finalResultNewItems = "" for j in items: result += "'"+j+"', " for k in newItems: resultNewItems += "'"+k+"', " resultNoComasEnd = result resultNewItemsNoComasEnd = resultNewItems finalResult = "[("+resultNoComasEnd+")" finalResultNewItems = "("+resultNewItemsNoComasEnd+")]" fixedFinalResult = finalResult.replace( finalResult[len(finalResult)-3] + " )" , ")" ) fixedFinalResultNewItems = finalResultNewItems.replace( finalResultNewItems[len(finalResultNewItems)-4] + " )" , ")" ) print( "----->" + fixedFinalResult + ", " + fixedFinalResultNewItems ) #On top, it is done with modules, right here let make it with one. Take in account, it gets code saved 50 lines off #Easy Example: from itertools import permutations items = ['x', 'y'] result = list( permutations( items ) ) print( result )

24th Mar 2022, 11:04 PM
ivan gonzalez
ivan gonzalez - avatar
0 Answers