This shows you the differences between two versions of the page.
ii:labs:s2:01:tasks:03 [2023/03/18 19:39] florin.stancu created |
ii:labs:s2:01:tasks:03 [2024/03/15 13:30] (current) florin.stancu [03. [20p] Making the list] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== 03. Making the list ==== | + | ==== 03. [25p] Making the list ==== |
Solve these subtasks all in the same file (use ''print()'' statements to display the results): | Solve these subtasks all in the same file (use ''print()'' statements to display the results): | ||
- | * Use the following list: <code python> | + | Use the following list: <code python> |
sample = ["a", 90, 8, "X55", "1z", 102, "asdf", 65, 10, "word", "567", 567] | sample = ["a", 90, 8, "X55", "1z", 102, "asdf", 65, 10, "word", "567", 567] | ||
</code> | </code> | ||
- | * Print the sorted list; | + | |
- | * Filter out all non-numbers in the list (note: those values of the ''int'' type should remain!)... can you make it using **a single line of code**? now it's the time to introduce [[https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions|list comprehensions]]; | + | Well: |
- | * Print the original list (too easy? [[|there's always a catch :P]]); | + | |
+ | * Sort the list (does it work? if not, why?); | ||
+ | * Reverse the list (this again? note: save the original one!); | ||
+ | * Filter out all non-numbers from the list (i.e., only ''int'' type values should remain)... can you make it using **a single line of code**? now it's the time to introduce [[https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions|list comprehensions]]; | ||
+ | <note> | ||
+ | Don't know how to check the type of a variable? | ||
+ | [[https://www.google.com/search?q=python+check+type+is+int|LMGTFY]]! | ||
+ | </note> | ||
+ | * Print the original list (remember the first 2 subtasks? [[https://stackoverflow.com/questions/2612802/how-do-i-clone-a-list-so-that-it-doesnt-change-unexpectedly-after-assignment|read this answer!]]); | ||
* Define a function that builds a matrix filled with numbers from 1 to n like the following example:<code python> | * Define a function that builds a matrix filled with numbers from 1 to n like the following example:<code python> | ||
m = [[1, 2, 3, 4], | m = [[1, 2, 3, 4], | ||
Line 15: | Line 23: | ||
[2, 3, 4, 1]] | [2, 3, 4, 1]] | ||
</code> | </code> | ||
+ | |||