Micro Challenge #18 – Sorting socks into pairs

15 May 2020 - 3 minute read

Welcome to the Eighteenth of Altitude Foundation’s #MicroChallenges2020

Today we’re taking an everyday task and creating a programme to optimise the solution.

What’s a Micro Challenge?

These challenges are short activities to help you develop, revise or refresh your coding skills, posted every Monday, Wednesday and Friday.

Micro Challenge #18

This week, we are exploring ‘best possible solutions’ – finding different solutions to issues and determining which is the best. This challenge links to last week’s theme of searches. If you want to explore further, you can find the relevant challenges here: Challenge 13 and Challenge 14.

For the weekend, we have a bit of a fun challenge – how to create a programme which matches your socks together.

For the purposes of this challenge, assume that you have 10 pairs of socks, all different: for example, a pair of white socks, black socks, robot-themed socks, spotty socks, etc. You could organise these in the following way (as an example, we’re only looking at 5 pairs of socks):

White 1Robot 1Spots 1Black 1Space 1
White 2Robot 2Spots 2Black 2Space 2

For the purposes of the challenge, however, the socks are not paired together, so our organisation might look more like this:

White 1Robot 1Spots 1Black 1Space 1
Robot 2Black 2Space 2Spots 2White 2

The Challenge:

Can you create a series of programmes which match the socks:

  • If you have selected a random first sock (input) and want the programme to find its partner (output)
  • If you are looking for a specific sock and then want to find its partner
  • If you want to make sure you always have odd socks

Top tips:

  • You might want to use an IF THEN function
  • Think about how you organise your array – will you need more than one?

Remember: you need to have at least 10 different pairs!

Review it:

Which input generates the fastest results? Why do you think that is? Is this consistently the case?

Could you organise your socks in a specific way to speed up your sorting and matching process?

Advanced:

Could you create your programme on either the BBC Micro:Bit MakeCode simulator or in Trinket

Share it

We would love to see what you have created! Please send any pictures, videos, or files of your activities to us – either via Facebook, Twitter or Instagram  using #MicroChallenges2020 or to challenges@altitudefoundation.org. If you are emailing them to us, please let us know if you are happy for us to share your stuff on our social media platforms (with credit, of course). 

Sign-up here to receive a weekly email with Micro Challenges top tips and solutions:



Leave a comment

Leave a Reply