comp-think / 2018-2019

The GitHub repository containing all the material related to the Computational Thinking and Programming course of the Digital Humanities and Digital Knowledge degree at the University of Bologna (a.a. 2018/2019).
Other
30 stars 8 forks source link

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

Open essepuntato opened 6 years ago

essepuntato commented 6 years ago

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

hizclick commented 6 years ago
names =  ["Harry","Draco","Hermione","Ron","Severus"]
sorted_names = sorted(names)
print(sorted_names)

/////////////////////////////////////////////////////////////

names= []
names.insert(0, 'Harry')
names.insert(1, 'Draco')
names.insert(2, 'Hermione')
names.insert(3, 'Ron')
names.insert(4, 'Severus')
sorted_names = sorted(names)
print(sorted_names)

/////////////////////////////////////////////////////////////


names= list()
names.append('Harry')
names.append('Draco')
names.append('Hermione')
names.append( 'Ron')
names.append('Severus')
sorted_names = sorted(names)
print(sorted_names)
delfimpandiani commented 6 years ago
# METHOD 1: alphabetize manually and add apend them already in alphabetical order

harrypotter_list0 = list()  # this creates a new list

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

# check our list looks right
print(harrypotter_list0)

# METHOD 2: first create the list without regard the alphabetization and alphabetize later
harrypotter_list = list()  # this creates a new list

harrypotter_list.append("Harry")
harrypotter_list.append("Draco")
harrypotter_list.append("Hermione")
harrypotter_list.append("Ron")
harrypotter_list.append("Severus")
# so far harrypotter_list contains:
# list([ "Harry", "Draco", "Hermione", "Ron", "Severus"])

# now, find a way of sorting alphabetically (used Google to find the command)
harrypotter_list.sort()

# check our list looks right
print(harrypotter_list)
bluebell94 commented 6 years ago

protagonist_list ={"Harry","Draco","Hermione","Ron","Severus"}

  1 protagonist_list=list()
2 protagonist_list.append("Harry")
3 protagonist_list.append("Draco")
4 protagonist_list.append("Hermione")
5 protagonist_list.append("Ron")
6 protagonist_list.append("Severus")
7 protagonist_list.sort()
8 print (protagonist_list)

I was thinking also about using the commands >, < in order to put the names in alphabetical order, However, it seems as a too long and much more complex process, especially when the command "sort" is available and has the same functions.

ilsamoano commented 6 years ago
# Write a sequence of instructions in Python 
# so as to create a list with the following elements ordered alphabetically: 
#​"​Harry"​​, ​"​Draco"​​, ​"​Hermione"​​, ​​"​Ron"​​, "​Severus"​​.

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

Grinwald.sort()

print(Grinwald)
LuciaGiagnolini12 commented 6 years ago
hp_list = list()

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

print(hp_list)
simayguzel commented 6 years ago

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

list_str.sort( ) print (list_str) output : ['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']

Totaro1996 commented 6 years ago

Characters_list=list( )

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

print (Characters_list)

DavideApolloni commented 6 years ago

screenshot at nov 24 11-22-27

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

SeverinJB commented 6 years ago

harry_potter_und_ein_stein = list(["Harry","Draco","Hermione","Ron","Severus"]) harry_potter_und_ein_stein = sorted(harry_potter_und_ein_stein)

Optional: print(harry_potter_und_ein_stein)

friendlynihilist commented 6 years ago
potter_list = list() #I've just sorted the list manually
potter_list.append("Draco")
potter_list.append("Harry")
potter_list.append("Hermione")
potter_list.append("Ron")
potter_list.append("Severus")
print(potter_list)
potter_list = list()
potter_list.append("Hermione")
potter_list.append("Draco")
potter_list.append("Ron")
potter_list.append("Harry")
potter_list.append("Severus")
potter_list.sort() #I've found that built-in function named _sort_, 
don't know if it's considered cheating for the sake of the exercise :)
I suppose it's also possible to write just one line and put strings inside the sort bracket.
print(potter_list)
MattiaSpadoni commented 6 years ago

"​Harry"​, "​Draco"​, "​Hermione"​, ​"​Ron"​, "​Severus"

dislike_harrypotter = list() dislike_harrypotter.append ("draco") dislike_harrypotter.append ("Harry") dislike_harrypotter.append ("Hermione") dislike_harrypotter.append ("Ron") dislike_harrypotter.append ("Severus")

print dislike_harrypotter

With the use of "sort" built-in function

dislike_harrypotter = list() dislike_harrypotter.append("​Harry"​, "​Draco"​, "​Hermione"​, ​"​Ron"​, "​Severus") dislike_harrypotter.sort dislike_harrypotter("Draco","Harry","Hermione","Ron","severus")

federicabologna commented 6 years ago
harry_potter_list = list()
harry_potter_list.append("Harry")
harry_potter_list.append("Draco")
harry_potter_list.append("Hermione")
harry_potter_list.append("Severus")
harry_potter_list.append("Ron")
sorted_harry_potter_list = sorted(harry_potter_list)
print(sorted_harry_potter_list)

or

harry_potter_list = list(["Harry", "Ron", "Hermione", "Draco", "Severus"])
sorted_harry_potter_list = sorted(harry_potter_list)
print(sorted_harry_potter_list)
saraarmaroli commented 6 years ago

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)

VittoriaMoccia commented 6 years ago

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

Starting from the list items in the given order, the first list would look this way:

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

print(harry_potter_characters)

And the result would be:

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

But then, we need to order properly all the names. This can be done in two ways (even though the third one is writing them by hand, of course). The first method doesn't use instructions to give the order. It just removes all the items but "Draco" from the list, and then adds them back in order. Like this:

[...] harry_potter_characters.remove("Harry") harry_potter_characters.remove("Hermione") harry_potter_characters.remove("Ron") harry_potter_characters.remove("Severus")

And here only "Draco" remains in the list. Then:

harry_potter_characters.append("Harry") harry_potter_characters.append("Hermione") harry_potter_characters.append("Ron") harry_potter_characters.append("Severus")

print(harry_potter_characters)

The result is the list in the alphabetical order:

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

The second method uses a proper instruction:

[...] harry_potter_characters.remove("Draco") harry_potter_characters.insert(0, "Draco")

print(harry_potter_characters)

And the result is the same, but it's way faster to obtain it: ['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']

katalu commented 6 years ago

my_harrypotter_list = list ()

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

my_harrypotter_list.sort()

print(my_harrypotter_list)

lisasiurina commented 6 years ago

HPCharacters_list = list()

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

HPCharacters_list.sort( )

print(HPCharacters_list)

Output: [‘Draco', 'Harry', 'Hermione', 'Ron', 'Severus']

dersuchendee commented 6 years ago
list_hp = list ()
list_hp.append("Draco")
list_hp.append("Harry")
list_hp.append("Hermione")
list_hp.append("Ron")
list_hp.append("Severus")

print(list_hp)

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

I don't see in the pdf where the .sort method is, and how to use it.

MilenaCorbellini commented 6 years ago

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

essepuntato commented 6 years ago

Hi all,

thanks for the answers, and also thanks for proposing additional strategies that go beyond the simpler "organise the item in the list by hand", that was of course one of the possibilities.

Just a suggestion: try always to test your code in Python (if you didn't), since I've found a bunch of syntactical error here and there.

tceron commented 6 years ago

"​Harry"​, "​Draco"​, "​Hermione"​, ​"​Ron"​, "​Severus"

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

happy_potter_list.sort()

print (happy_potter_list)

list ["Draco", "Harry", "Hermione", "Ron", "Severus"]

andreamust commented 6 years ago

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

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