Any other way to solve the same problem

Hi everyone,
I was wondering if there is any other way to solve the same problem

Here’s my solution:


all_students = ["Alex", "Briana", "Cheri", "Daniele", "Dora", "Minerva", "Alexa", "Obie", "Arius", "Loki"]
students_in_poetry = []

capacity = 6
while len(students_in_poetry) < capacity:
  student = all_students.pop()
  students_in_poetry.append(student)
  
print(students_in_poetry)
1 Like

Which problem?
Reversing it?
If you’re making some kind of copy then you shouldn’t need to modify the original. However, it does come with the benefit that there’s no need to keep track of a position in the list which is something that I generally like (hard to make mistakes when doing it that way)
If it’s reversed, then 6 seems like an oddly specific number, where does that come from?
If the copy is supposed to become some size then I’d probably phrase the condition as while that list is too small.

1 Like

Hi, just started de pro course and came across this problem. I was wondering if a shortened version was possible to answer the problem and came up with this:

all_students = [“Alex”, “Briana”, “Cheri”, “Daniele”, “Dora”, “Minerva”, “Alexa”, “Obie”, “Arius”, “Loki”]
students_in_poetry =

while len(students_in_poetry) < 7:
students_in_poetry = all_students.pop()
print(students_in_poetry)

Or is this a shortcut that misses out on something (that i yet do not see?)

l love seeing all the different ways we can generate the same solution. Below is what I put together.

while len(students_in_poetry) < 6:
	students_in_poetry.append(all_students.pop())

print(students_in_poetry)
1 Like

Without using append, IF there already were one or more students enrolled in the students_in_poetry list they would get replaced by the name(s) at the end of the all_students list and we don’t want to kick out any students that are already enrolled in the course. You can test this by adding a student name to the students_in_poetry list before hitting Run.

Also, for reasons I do not understand, it seems your code returns 5 names instead of 6. If anyone can explain why, I would love to know the answer.

They’re checking the length of strings instead of counting how many has been done/remain/equivalent
So if you for example made all long strings or all short strings you’d get all or nothing with that code

2 Likes

You’re correct! Thanks :slight_smile:

Why there are only 5 i don’t know either. I put an 8 where there was a 7 ( while len(students_in_poetry) < 8:) and i got the whole list including an error : IndexError: pop from empty list …

Your solution so neat and simple. Much appreciated for sharing it. :grin:

My bad for not including the problem statement.:sweat_smile:
That 6 is an amount of given array/list, consisting of those names. It just came to my mind that I could’ve phrased while condition as comparing the lengths of two lists, i.e. while len(students_in_poetry) < len(all_students).

Hi i have worked upon different solution which does not use pop() function.

Below is the code :

all_students = [“Alex”, “Briana”, “Cheri”, “Daniele”, “Dora”, “Minerva”, “Alexa”, “Obie”, “Arius”, “Loki”]
students_in_poetry =

def add_student(index):
students_in_poetry.append(all_students[index])
return (students_in_poetry)

index=-1
while index>(-7):
add_student(index)
if index == (-6):
print(students_in_poetry)
index+= (-1)

print(students_in_poetry)

The above solution is not using any pop(). I tried to use python that i have learned so far.
Kindly help whether finding the solution in this way is correct or not or using pop() has any added advantage.

Moreover , i can add starting name also : for ex - starting from Alex.

Kindly give any suggestions to improve this code also.

This code is doesn’t pass the test but it works XD

all_students = [“Alex”, “Briana”, “Cheri”, “Daniele”, “Dora”, “Minerva”, “Alexa”, “Obie”, “Arius”, “Loki”]
students_in_poetry =

while len(students_in_poetry) < 6:
students_in_poetry.append(all_students[-1:])
all_students.pop()

print(students_in_poetry)