TASK 1

Sorting

One of the most common types of algorithm is the sorting algorithm.

In this task, you may implement your sort however you choose, so long as the output of this function is the input array sorted from smallest to largest

You must implement

def sort(array : list[int]) -> list[int]:

Try this challenge multiple times, and try and implement different sorting algorithms each time.

Well done!

You have completed all of the tasks.

Return home or take a look at the tutorials

Something isn't quite right...

Close this message and have another go!

GUIDE

This guide is intended to help with syntax. See the prelude section for pre-defined functions and variables. See the tutorials page for tutorials on how to write code.

  • Data Types

    • int

      int

      Integer values, such as 2, -4 and 254.

    • float

      float

      Floating point values, such as 2.0, -4.72 and 254.2681.

    • string

      string

      String values, such as "hello, world!", "haha" and "33".

    • boolean

      boolean

      Boolean values True and False.

    • colour

      colour

      Colour values for drawing on the canvase, See the prelude for more.

    • list

      list[element_type]

      List values. Note the element type must be correctly specified. Examples are [1,2,3], [["hello"], ["world"]] and [].

  • Variables

    • declaration

      name: type

      A variable must first be declared with a type before it can be used

    • assignment

      name = value
    • increment

      name += value

      Only applicable to integer values. Equivalent to name = name + value .

    • decrement

      name -= value

      Only applicable to integer values. Equivalent to name = name - value .

  • Expressions

    • logic

      Applicable to boolean values. and, or and not.

    • mathematical

      Applicable to numerical values. +, -, *, **, /, // and %.

    • relational

      Applicable to comparable values. >, <, ==, !=, <= and >=.

  • Selection Statements

    • if

      if condition: statements elif condition: statements else: statements
  • Loops

    • while

      while condition: statements
    • for

      for ident in list: statements
  • Function

    • function

      def name(parameter: parameter_type) -> return_type: statements

      Values can be returned with return

    • procedure

      def name(parameter: parameter_type): statements

      Equivalent to a function which returns a None type



Can't find what you're looking for?

Check the tutorials page for more!

PRELUDE

The prelude is all the pre-defined functions and variables available to use. See the tutorials page for tutorials on how to write code.

  • Terminal Functions

    • print

      print(expr)

      Prints the value of the expression to the terminal.

    • print_str

      print_str(expr)

      Prints the value of the expression as a string to the terminal.

    • clear_terminal

      clear_terminal()

      Clears the terminal.

    • input

      input(message: str) -> str

      Displays message to the user, and returns their input as a string.

  • Type Functions

    • str

      str(expr) -> str

      Where possible returns the string representation of expr.

    • int

      int(expr) -> int

      Where possible parses the integer representation of expr.

    • float

      float(expr) -> float

      Where possible parses the float representation of expr.

    • len

      len(expr) -> int

      Where possible returns the length of expr.

    • type

      type(expr) -> str

      Returns the name of the type of expr.

  • Math Functions

    • round

      round(n: float) -> int

      Rounds the value of n using bankers rounding.

    • random_int

      random_int(from: int, to: int) -> int

      Returns a random integer between from and to.

    • random_float

      random_float(from: float, to: float) -> float

      Returns a random float between from and to.

    • pi

      pi: int

      The value of π.

  • String Functions

    • upper

      str.upper()

      Returns the uppercase of str.

    • lower

      str.lower()

      Returns the lowercase of str.

    • replace

      str.replace(s1: str, s2: str)

      Returns a string will all occurances of s1 replaced with s2.

    • split

      str.split(d: str)

      Returns a list of strings of str split on all occurances of d.

    • join

      str.join(ss: list[str])

      Returns a string from joining all strings in ss using d.

    • isnumeric

      str.isnumeric() -> bool

      Returns true if all characters in str are numbers.

  • List Functions

    • append

      l.append(elem)

      Appends elem to the list l.

    • remove

      l.remove(elem)

      Removes the first instance of elem from the list l.

    • count

      l.count(elem)

      Returns the total of elems present in the list l.

    • range

      range(f: int, t: int) -> list[int]

      Returns the list of values from f to t.

  • Drawing Functions

    • line

      line(x1: int, y1: int, x2: int, y2: int)

      Draws a line from (x1,y1) to (x2,y2).

    • rect

      rect(x: int, y: int, w: int, h: int)

      Draws a rectangle of dimensions w by h on the canvas at position (x,y).

    • circle

      circle(x: int, y: int, r: int)

      Draws a circle with radius r on the canvas at position (x,y).

    • ellipse

      ellipse(x: int, y: int, r1: int, r2: int)

      Draws an ellispe with horizontal radius r1 and vertical radius r2 at position (x,y).

    • arc

      arc(x: int, y: int, r: int, f: int, t: int)

      Draws an arc with radius r on the canvas at position (x,y) from f degrees to t degrees.

    • sect

      sect(x: int, y: int, r: int, f: int, t: int)

      Draws a sector with radius r on the canvas at position (x,y) from f degrees to t degrees.

    • triangle

      triangle(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int)

      Draws a triangle with corners at positions (x1,y1), (x2,y2) and (x3,y3).

    • polygon

      polygon(xs: list[int], ys: list[int])

      Draws a polygon with corners at positions (xi,yi) from xs and ys.

    • img

      img(x: int, y: int, w: int, h: int url: string)

      Draws the image loaded from the url at position (x,y) with width w and height h.

    • text

      text(x: int, y: int, t: string)

      Draws the text t at position (x,y).

  • Colour Functions

    • rbg

      rgb(r: int, g: int, b: int) -> colour

      Returns the colour for the respecive red, green and blue values.

    • rbga

      rgba(r: int, g: int, b: int, a: float) -> colour

      Returns the colour for the respecive red, green and blue and alpha values. Alpha is opacity, in the range 0.0 to 1.0.

    • red

      red: colour

      The built in colour red, equivalent to rgb(240, 51, 51).

    • green

      green: colour

      The built in colour green, equivalent to rgb(98, 203, 166).

    • blue

      blue: colour

      The built in colour blue, equivalent to rgb(117, 117, 232).

    • pink

      pink: colour

      The built in colour pink, equivalent to rgb(197, 89, 146).

    • yellow

      yellow: colour

      The built in colour yellow, equivalent to rgb(221, 192, 107).

    • white

      white: colour

      The built in colour white, equivalent to rgb(255, 255, 255).

    • black

      black: colour

      The built in colour black, equivalent to rgb(0, 0, 0).

    • transparent

      transparent: colour

      The built in colour transparent, equivalent to rgba(0, 0, 0, 0.0).

  • Canvas Functions

    • width

      width: int

      The width of the canvas in pixels.

    • height

      height: int

      The height of the canvas in pixels.

    • line_colour

      line_colour(c: colour)

      Sets the canvas line colour to c.

    • fill_colour

      fill_colour(c: colour)

      Sets the canvas fill colour to c.

    • line_width

      line_width(w: int)

      Sets the canvas line width to w.

    • font_size

      font_size(s: int)

      Sets the canvas font size to s.

    • translate

      translate(x: int, y: int)

      Translates the canvas by (x,y).

    • rotate

      rotate(a: int)

      Rotates the canvas by angle a in degrees.

    • scale

      scale(x: float, y: float)

      Scales the canvas size by (x,y).

    • frame_rate

      frame_rate(s: int)

      Sets the frame rate of the definable draw function.

    • stop_draw

      stop_draw(s: int)

      Stops the execution of the definable draw function.

  • Definable Functions

    • draw

      def draw():

      Is called at frame_rate, intended for animations and games on the canvas.

    • left_pressed

      def left_pressed():

      Is called whenever the left arrow key is pressed.

    • right_pressed

      def right_pressed():

      Is called whenever the right arrow key is pressed.

    • up_pressed

      def up_pressed():

      Is called whenever the up arrow key is pressed.

    • down_pressed

      def down_pressed():

      Is called whenever the down arrow key is pressed.

    • space_pressed

      def space_pressed():

      Is called whenever the space key is pressed.



Can't find what you're looking for?

Check the tutorials page for more!