Skip to main content

2 posts tagged with "code"

Coding-related posts.

View All Tags

Attempting Challenge of Wits 2023

· One min read

In August 2023, I submitted my solution to the second challenge of Chalenge of Wits 2023 organised by the Defence Science & Technology sector of the Government of Singapore, which is about data science. However, since I am not a Singaporean, I was not qualified to receive the rewards of the challenge.

The problem statement of the challenge (with required data set and answer) can be found here.

Here is my submitted code for the challenge, which can also be found via this GitHub repository.

import xml.etree.ElementTree as ET
import os
import matplotlib.pyplot as plt
import traceback

cwd = os.getcwd()
K = 2254
coordinates = []
invalid = [317, 1768]

for i in range(K):
if i in invalid:
continue
try:
filename = cwd + '/log_' + str(i) + '.xml'
# with open(filename, 'r') as f:
# read_data = f.read()
tree = ET.parse(filename)
root = tree.getroot()
location_raw = root[0].text
coordinate = tuple(map(lambda text: int(text), location_raw.split(',')))
coordinates.append(coordinate)
except Exception:
print(traceback.format_exc())
print(i)

plt.scatter(*zip(*coordinates))
plt.savefig('key.jpeg')

Higher-order Functions and Set Theory

· 3 min read

Higher-order function (HOF) is a type of nested function that offers a higher level of abstraction than ordinary functions, where instead of taking primitives or variables as arguments (parameters) and outputting objects, functions are taken in and output by HOFs.

The subtleties between a function composition and HOF can be shown through the syntax of Python, which can result in different behaviours depending on how we close the parentheses.

A classic example is demonstrated as follows.

def thrice(f):
return lambda x: f(f(f(x)))

add1 = lambda x: x + 1

x = thrice(thrice(add1))(0) # 9
y = thrice(thrice)(add1)(0) # 27

In the example above, x will output 9 because the defining line of code is interpreted ‘right-to-left’ (like function composition), first composing the add1 function thrice to produce a function equivalent to lambda x: x + 3, then composing the first output thrice to produce a function equivalent to lambda x: x + 9, thus 0 + 9 = 9.

As for y, it will output 27 because it is defined ‘left-to-right’, first composing the thrice function itself three times, resulting in an HOF that effectively composes an input function 33=273^3=27 times, which then takes in the function add1 to produce a function equivalent to lambda x: x + 27, and finally we get 0 + 27 = 27.

Higher-order functions are closely related to mathematics, in the sense that they enable the implementation of operators in mathematics, as well as a family of functions, using code.

For example1, in set theory, we can define a family of functions FF, which itself is also considered a function from NNNN\mathbb{N}^{\mathbb{N}} \to \mathbb{N}^{\mathbb{N}}. This operator FF takes in a function f:NNf : \mathbb{N} \to \mathbb{N} and outputs another function F(f):NNF(f) : \mathbb{N} \to \mathbb{N} that takes in whatever ff takes in and outputs whatever ff outputs plus one.

Phrased in a set-theoretic language as ‘pure’ as possible, FF is defined as

F={(f,{(n,f(n)+1):nN}):fNN}.F=\{(f,\{(n,f(n)+1) : n \in \mathbb{N}\}) : f \in \mathbb{N}^{\mathbb{N}}\}.

An HOF implementation of FF in Python can then look like the following:

# The following type hints are to indicate that the functions are in N^N, and are unnecessary for practical purposes.
from typing import Annotated, Callable
from annotated_types import Ge # requires additional pip install: https://pypi.org/project/annotated-types/

def F(f: Callable[Annotated[int, Ge(0)], Annotated[int, Ge(0)]]) -> Callable[Annotated[int, Ge(0)], Annotated[int, Ge(0)]]:
def output(n: Annotated[int, Ge(0)]) -> Annotated[int, Ge(0)]:
return f(n) + 1
return output
f = lambda x: x + 1
F(f)(0) # 2

Footnotes

  1. This example is taken from the lecture notes of the Set Theory course conducted during the Fall 2022 semester by Dilip Raghavan in National University of Singapore.