1

code:

 a=['1','2','3','4','5','6']
 for i in range(1,6):
   for j in range(i+1):
    for k in range(j+1):
        for l in range(k+1):
            for m in range(l+1):
                 for p in range(m+1):
                    print(i,j,k,l,m,p)

#------------------------------------------------------------------------------------------
output: 1 0 0 0 0 0
1 1 0 0 0 0
1 1 1 0 0 0
1 1 1 1 0 0
1 1 1 1 1 0
1 1 1 1 1 1
2 0 0 0 0 0
2 1 0 0 0 0
2 1 1 0 0 0
2 1 1 1 0 0
2 1 1 1 1 0
2 1 1 1 1 1
2 2 0 0 0 0
2 2 1 0 0 0
2 2 1 1 0 0
2 2 1 1 1 0
2 2 1 1 1 1
2 2 2 0 0 0
2 2 2 1 0 0
2 2 2 1 1 0
2 2 2 1 1 1
2 2 2 2 0 0
2 2 2 2 1 0
2 2 2 2 1 1
2 2 2 2 2 0
2 2 2 2 2 1
2 2 2 2 2 2
3 0 0 0 0 0
3 1 0 0 0 0
3 1 1 0 0 0
3 1 1 1 0 0
and so on....

This is the code I have tried but im not getting desired output can someone please explain..Thankyou

sinisake
  • 11,240
  • 2
  • 19
  • 27
  • It looks like you didn't understand the question - what do you think actually needs to happen? You're generating a ton of answers that aren't asked for - how would you go about it manually? What code would correspond to that? – Grismar Jun 23 '21 at 11:44
  • [How to generate all permutations of a list?](https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list) – Lei Yang Jun 23 '21 at 11:45
  • 1
    @LeiYang that just generates all permutations and isn't an answer to the question asked here. – Grismar Jun 23 '21 at 11:45

5 Answers5

1

It would appear to me that you need to look at the examples very carefully, and do something with the use of the word 'rotate'.

A fairly simple solution:

def rotate(xs):
    for i in range(len(xs)):
        yield tuple(xs[i:] + xs[:i])


for result in rotate([1,2,3,4,5,6]):
    print(result)

Output:

(1, 2, 3, 4, 5, 6)
(2, 3, 4, 5, 6, 1)
(3, 4, 5, 6, 1, 2)
(4, 5, 6, 1, 2, 3)
(5, 6, 1, 2, 3, 4)
(6, 1, 2, 3, 4, 5)
Grismar
  • 27,561
  • 4
  • 31
  • 54
0

Do you mean this?

# Create new list containing all "rotated" versions of lst
lst = list(range(7))
new_lists = [lst[-i:] + lst[:-i] for i in range(len(lst))]

# print results
for l in new_lists:
    print(l)

Output:

[0, 1, 2, 3, 4, 5, 6]
[6, 0, 1, 2, 3, 4, 5]
[5, 6, 0, 1, 2, 3, 4]
[4, 5, 6, 0, 1, 2, 3]
[3, 4, 5, 6, 0, 1, 2]
[2, 3, 4, 5, 6, 0, 1]
[1, 2, 3, 4, 5, 6, 0]
JoostVn
  • 242
  • 1
  • 9
0

Use itertools library and import permutation. Permutation will find all combination. And, it is an easy method too. It will print all possible combination of your input.

from itertools import permutations

a = [1,2,3,4,5,6] 
perm = permutations(a)
 
for i in list(perm):
    print (i)
0

I got the desired output using forloops, hence I am posting this answer so it can help any of you struggling with the same question

    import numpy as np 

    count = 0
    n = "kashmeen" 
    for i in range(0,8):
        for j in range(0,8):
            for k in range(0,8):
                for l in range(0,8):
                    for m in range(0,8):
                        for o in range(0,8):
                            for p in range(0,8):
                                for q in range(len(n)):
                                    new = [i, j, k, l, m, o, p, q]
                                    new = np.array(new)
                                    new_1 = np.unique(new)
                            
                                   if len(new_1)==8:
                                  print(n[i],n[j],n[k],n[l],n[m],n[o],n[p],n[q])
                                   count += 1
  • I thought you said "nothing but a `for` loop" and yet you recommend an inefficient solution involving `numpy` which is an extremely heavyweight library in relation to what you seem to be using it for. – BoarGules Jul 12 '21 at 07:05
  • I had to get all unique combinations possible for the given string, so by using the numpy unique function i got 40320 unique combinations which i was not getting by using only forloop or any other function – Kashmeen Ansari Jul 12 '21 at 07:54
  • That is not what the problem statement in your question header says. It says *rotate* the input, not *permute* the input. Maybe you should edit your question to reflect what you really want, and give an example of the output you want. But since your code is limited to an input of exactly 8 elements, it is hardly useful. If you want permutations you can use `for` to loop through the output of `itertools.permutations("kashmeen")`. – BoarGules Jul 12 '21 at 07:58
  • I was not allowed to use itertools.permutations that is why I chose np.unique as an alternative and also I am new to coding so i got confused between rotate and permute, my bad – Kashmeen Ansari Jul 12 '21 at 08:08
  • Not allowed to use `itertools` which is a standard library module, but nonetheless allowed to use `numpy`, which isn't? If your concern is with uniqueness then just use `len(set(new)) == 8`. That avoids all libraries. – BoarGules Jul 12 '21 at 08:11
  • You have convinced yourself that your assignment is to write your own implementation of `itertools.permutations()`. Several commenters, beginning with @Grismar, consider that unlikely, and that your interpretation is mistaken. We say this for two reasons. 1. Writing your own permutation function, even a half-decent one, is a project way beyond what could be expected of a novice programmer. That makes your interpretation unlikely. 2. Requiring that such a goal be achieved using only a `for`-loop makes it not just unlikely but fantastic. – BoarGules Jul 12 '21 at 09:44
  • The solution I have posted above is exactly what I was asked to do by my institute, so its not like I have convinced myself into not using itertools.permutation() , I was just not allowed to. I would appreciate if you would just end this discussion over here or if you have anymore suggestions we can move our discussion to chat. – Kashmeen Ansari Jul 12 '21 at 10:14
0

It is clear that the intent of this exercise is to for you to demonstrate knowledge of slicing operations. Using slices, this can be done in a single for-loop:

>>> n = "kashmeen"
>>> for index in range(len(n)):
        print (n[index:]+n[:index])

which gives the output

kashmeen
ashmeenk
shmeenka
hmeenkas
meenkash
eenkashm
enkashme
nkashmee

This solution will work for a string of any length (not just 8) and will work equally well for lists or tuples.

BoarGules
  • 16,440
  • 2
  • 27
  • 44