Showing posts with label Sort. Show all posts
Showing posts with label Sort. Show all posts

Monday, September 14, 2026

Python sorted() Quiz

 Python sorted() Quiz


1. What is the output? print(sorted([3, 1, 2]))

   A. [3, 2, 1]

   B. [1, 2, 3]

   C. None

   D. [2, 1, 3]


2. What is the output? print(sorted([3, 1, 2], reverse=True))

   A. [2, 3, 1]

   B. [3, 2, 1]

   C. None

   D. [1, 2, 3]


3. What is the output? words = ["banana", "fig", "apple"] print(sorted(words, key=len))

   A. ['fig', 'apple', 'banana']

   B. ['banana', 'apple', 'fig']

   C. ['apple', 'banana', 'fig']

   D. ['fig', 'banana', 'apple']


4. What is the output? nums = [10, 2, 30, 4] print(sorted(nums, key=lambda x: x))

   A. [30, 10, 4, 2]

   B. [4, 2, 10, 30]

   C. [10, 2, 4, 30]

   D. [2, 4, 10, 30]


5. What is the output? names = ["Bob", "alice", "Charlie"] print(sorted(names, key=str.lower))

   A. ['Charlie', 'Bob', 'alice']

   B. ['alice', 'Bob', 'Charlie']

   C. ['alice', 'Charlie', 'Bob']

   D. ['Bob', 'Charlie', 'alice']

______________________________


1. What is the output? nums = [7, 2, 9, 1] print(sorted(nums))

   A. [7, 2, 9, 1]

   B. [1, 2, 7, 9]

   C. [2, 1, 7, 9]

   D. [9, 7, 2, 1]


2. What is the output? nums = [4, 8, 1, 6] print(sorted(nums, reverse=True))

   A. [6, 8, 4, 1]

   B. [8, 6, 4, 1]

   C. [4, 8, 1, 6]

   D. [1, 4, 6, 8]


3. What is the output? words = ["pear", "apple", "banana"] print(sorted(words))

   A. ['banana', 'pear', 'apple']

   B. ['pear', 'banana', 'apple']

   C. ['apple', 'pear', 'banana']

   D. ['apple', 'banana', 'pear']


4. What is the output? words = ["cat", "elephant", "dog"] print(sorted(words, key=len))

   A. ['cat', 'elephant', 'dog']

   B. ['dog', 'cat', 'elephant']

   C. ['elephant', 'cat', 'dog']

   D. ['cat', 'dog', 'elephant']


5. What is the output? words = ["one", "three", "two"] print(sorted(words, key=len, reverse=True))

   A. ['one', 'two', 'three']

   B. ['three', 'one', 'two']

   C. ['two', 'one', 'three']

   D. ['three', 'two', 'one']


6. What is the output? items = [("B", 2), ("A", 3), ("C", 1)] print(sorted(items, key=lambda x: x[1]))

   A. [('C', 1), ('B', 2), ('A', 3)]

   B. [('A', 3), ('B', 2), ('C', 1)]

   C. [('A', 3), ('C', 1), ('B', 2)]

   D. [('B', 2), ('A', 3), ('C', 1)]


7. What is the output? students = [("John", 80), ("Amy", 95), ("Bob", 70)] print(sorted(students, key=lambda x: x[1], reverse=True))

   A. [('John', 80), ('Amy', 95), ('Bob', 70)]

   B. [('Amy', 95), ('John', 80), ('Bob', 70)]

   C. [('Bob', 70), ('John', 80), ('Amy', 95)]

   D. [('Amy', 95), ('Bob', 70), ('John', 80)]


8. What is the output? pairs = [(3, 1), (1, 5), (2, 2)] print(sorted(pairs))

   A. [(1, 5), (3, 1), (2, 2)]

   B. [(2, 2), (1, 5), (3, 1)]

   C. [(1, 5), (2, 2), (3, 1)]

   D. [(3, 1), (2, 2), (1, 5)]


9. What is the output? words = ["apple", "kiwi", "banana", "fig"] print(sorted(words, key=lambda x: (len(x), x)))

   A. ['banana', 'apple', 'kiwi', 'fig']

   B. ['fig', 'kiwi', 'apple', 'banana']

   C. ['fig', 'apple', 'kiwi', 'banana']

   D. ['kiwi', 'fig', 'apple', 'banana']

Monday, April 7, 2014

Perl: Sort

Perl: Sort


  • sort
  • $a
  • $b
  • cmp
  • <=>
  • spaceship operator
examples/arrays/sort.pl
#!/usr/bin/perl
use strict;
use warnings;

my @words = qw(Foo Moo Bar);

my @sorted_words = sort @words;
print "@sorted_words\n";  # Bar Foo Moo

my @data = (11, 2, 23, 12);
my @sorted = sort @data;
print "@sorted\n";         # 11 12 2 23

my @sorted_ascii = sort {$a cmp $b} @data;

my @sorted_numeric = sort {$a <=> $b} @data;
print "@sorted_numeric\n";  # 2 11 12 23

my @sorted_by_length
    = sort {length($a) <=> length($b)} @data;

my @sorted_by_length_and_ascii
    = sort {
               length($a) <=> length($b)
                       or
               $a cmp $b
           } @data;

my @sorted_by_abc = sort {lc($a) cmp lc($b)} @data;

my @sorted_abc_ascii
    = sort {
           lc($a) cmp lc($b)
                   or
               $a cmp $b
           } @data;


Source: Edu Maven