Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lecture "Organising information: ordered structures", exercise 1 #14

Open
essepuntato opened this issue Oct 24, 2021 · 27 comments
Open
Labels

Comments

@essepuntato
Copy link
Contributor

Write a sequence of instructions in Python to create a list with the following elements ordered alphabetically: "​Harry"​, "Draco"​, "​Hermione"​, ​"​Ron"​, "​Severus"​.

@giorgimariachiara
Copy link

my_list = list ()
my_list.append (Draco)
my_list.append (Harry)
my_list.append (Hermione)
my_list.append (Ron)
my_list.append (Severus)

@elizastuglik
Copy link

IMG_20211025_114812

@sotarega
Copy link

sotarega commented Oct 25, 2021

Approach 1 (the one mentioned above, since lists care about the order of addition):

name_list = list()
name_list.append("Draco")
name_list.append("Harry")
name_list.append("Hermione")
name_list.append("Ron")
name_list.append("Severus")
print(name_list)

Approach 2 (add names in any order then use .sort)

name_list = list()
name_list.append("Harry")
name_list.append("Ron")
name_list.append("Hermione")
name_list.append("Draco")
name_list.append("Severus")
name_list.sort()
print(name_list)

@tommasobattisti
Copy link

first_list = list()
first_list.append('Draco')
first_list.append('Harry')
first_list.append('Hermione')
first_list.append('Ron')
first_list.append('Severus')

@ManueleVeggi
Copy link

ManueleVeggi commented Oct 25, 2021

The list can be create through this Py code:

hp_list = list()
hp_list.append("Draco")
hp_list.append("Harry")
hp_list.append("Hermione")
hp_list.append("Ron")
hp_list.append("Severus")

Indeed, if we execute the command print(hp_list) the result is ['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']

@OrsolaMBorrini
Copy link

OrsolaMBorrini commented Oct 25, 2021

new_list=list()
new_list.append("Harry")
new_list.append("Draco")
new_list.append("Hermione")
new_list.append("Ron")
new_list.append("Severus")
print(new_list)

@CarmenSantaniello
Copy link

hp_characters_list = list()
hp_characters_list.append("Draco")
hp_characters_list.append("Harry")
hp_characters_list.append("Hermione")
hp_characters_list.append("Ron")
hp_characters_list.append("Severus") 

If we run the list:

print(hp_characters_list)

['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']

@ghasempouri1984
Copy link

I used the sort method to sort the list alphabetically.
I wrote a test in order to test my code>

def order_list(listOfWords):
    listOfWords.sort()
    return listOfWords

def test_order_list(mylist, expected):
    ordered = order_list(mylist)
    if ordered == expected:
        return True
    else:
        return False
    
mylist = ["Harry", "Draco", "Hermione", "Ron", "Severus"]
print(test_order_list(mylist, ['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']))
print(order_list(mylist))

@angstigone
Copy link

my_hp_list = list()

my_hp_list.append("Draco")
my_hp_list.append("Harry")
my_hp_list.append("Hermione")
my_hp_list.append("Ron")
my_hp_list.append("Severus")

print(my_hp_list)

['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']

@MaddaGh
Copy link

MaddaGh commented Oct 26, 2021

alph_list

@NoraPs
Copy link

NoraPs commented Oct 26, 2021

my_list = list ()
my_list.append ("Harry")
my_list.insert (0,"Draco")
my_list.append ("Hermione")
my_list.append ("Ron")
my_list.append ("Severus")
print(my_list)

['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']

@francescabudel
Copy link

my_list = list()
my_list.append("Draco")
my_list.append("Harry")
my_list.append("Hermione")
my_list.append("Ron")
my_list.append("Severus") 


print(my_list)
["Draco", "Harry", "Hermione", "Ron", "Severus"]

@sterenz
Copy link

sterenz commented Oct 27, 2021

hogwarts_list = list()
hogwarts_list.append("Harry")
hogwarts_list.append("Draco")
hogwarts_list.append("Hermione")
hogwarts_list.append("Ron")
hogwarts_list.append("Severus")

hogwarts_list.sort()
print(hogwarts_list)

@RebeccaJillianBeattie
Copy link

harry_potter_list = list()
harry_potter_list.append("Draco")
harry_potter_list.append("Harry")
harry_potter_list.append("Hermione")
harry_potter_list.append("Ron")
harry_potter_list.append("Severus")

harry_potter_list.sort()
or
harry_potter_list = sorted(harry_potter_list)

@olgagolgan
Copy link

my_list = list()
my_list.append("Draco")
my_list.append("Harry")
my_list.append("Hermione")
my_list.append("Ron")
my_list.append("Severus")

@SaraVell1
Copy link

my _list = list()
my_list.append("Draco")
my_list.append("Harry")
my_list.append("Hermione")
my_list.append("Ron")
my_list.append("Severus")

@chloeppd
Copy link

chloeppd commented Oct 27, 2021

friends_list = list()
friends_list.append("Harry")
friends_list.append("Draco")
friends_list.append("Hermione")
friends_list.append("Ron")
friends_list.append("Severus")

friends_list =sorted(friends_list)


print(friends_list) returns the names in alphabetical order

@martasoricetti
Copy link

harry_potter_list = list ()
harry_potter_list.append("Draco")
harry_potter_list.append("Harry")
harry_potter_list.append("Hermione")
harry_potter_list.append("Ron")
harry_potter_list.append("Severus")
print(harry_potter_list)

@AnastasiyaSopyryaeva
Copy link

Version 1
harry_freinds = ["Harry", "Draco", "Hermione", "Ron", "Severus"]
harry_freinds =sorted(harry_freinds)
print(harry_freinds)

Version 2
harry_freinds = list()
harry_freinds.append("Harry")
harry_freinds.append("Draco")
harry_freinds.append("Hermione")
harry_freinds.append("Ron")
harry_freinds.append("Severus")
harry_freinds =sorted(harry_freinds)
print(harry_freinds)

@PirjoElbrecht
Copy link

my_list = ["Harry", "Draco", "Hermione", "Ron", "Severus"]
x=sorted(my_list)
print(x)

@AmeliaLamargese
Copy link

#create list and assign to variable
names = list() 
#add names as given in input
names.append("Harry")
names.append("Draco")
names.append("Hermione")
names.append("Ron")
names.append("Severus")
#print the list to check if the names are correctly saved
print(names)
#sort the items in alphabetical order
names.sort()
#print the ordered list to check if it's correct
print(names)

@essepuntato
Copy link
Contributor Author

Hi,

just a note for @giorgimariachiara and @elizastuglik: try to run your code in Python, and see what happens.

@Bianca-LM
Copy link

my_list = list ()
my_list.append("Harry")
my_list.append("Draco")
my_list.append("Hermione")
my_list.append("Ron")
my_list.append("Severus")
my_list = sorted(my_list)
print (my_list)

@sarabecchi
Copy link

esercizio 2

@Postitisnt
Copy link

my_list = []

my_list.append("Severus") 
my_list.append("Harry")
my_list.append("Ron")
my_list.append("Hermione")
my_list.append("Draco")

my_list.sort()

@sanyuezoe
Copy link

my_first_list = list()
my_first_list.append("Draco")
my_first_list.append("Harry")
my_first_list.append("Hermione")
my_first_list.append("Ron")
my_first_list.append("Severus")
print(my_first_list)

['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']

@teragramgius
Copy link

hp_characters = list()
hp_characters.append ("Draco")
hp_characters.append ("Harry")
hp_characters.append ("Hermione")
hp_characters.append ("Ron")
hp_characters.append ("Severus")

print (hp_characters)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests