Skip to main content

National Coding Week

It’s National Coding Week!  This week started in 2014 and aims to promote the learning of new digital skills. Therefore, starting from today, a new coding problem will pop up on this blog post for the week – ranging from easy to expert!

Make sure to come back and check for new challenges as the week goes on!

Problem 1 (20/9/22)

Java

Hash algorithms are easy to do one way, but essentially impossible to do in reverse. For example, if you hash something simple, like password123, it will give you a long code, unique to that word or phrase. Ideally, there’s no way to do this in reverse. You can’t take the hash code and go back to the word or phrase you started with.

Make a function that returns the SHA-256 secure hash for a given string. The hash should be formatted in a hexadecimal digit string.

getSha256Hash("password123") ➞ "ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f"

getSha256Hash("Fluffy@home") ➞ "dcc1ac3a7148a2d9f47b7dbe3d733040c335b2a3d8adc7984e0c483c5b2c1665"

getSha256Hash("Hey dude!") ➞ "14f997f08b8ad032dcb274198684f995d34043f9da00acd904dc72836359ae0f"

Notes: Bonus if you can do it without importing any libraries!

Problem 2 (21/9/22)

Python

An out-shuffle, also known as an out faro shuffle or a perfect shuffle, is a controlled method for shuffling playing cards. It is performed by splitting the deck into two equal halves and interleaving them together perfectly, with the condition that the top card of the deck remains in place.

Using an array to represent a deck of cards, an out-shuffle looks like:

[1, 2, 3, 4, 5, 6, 7, 8] ➞ [1, 5, 2, 6, 3, 7, 4, 8]
// Card 1 remains in the first position.

If we repeat the process, the deck eventually returns to original order:

Shuffle 1:

[1, 2, 3, 4, 5, 6, 7, 8] ➞ [1, 5, 2, 6, 3, 7, 4, 8]

Shuffle 2:

[1, 5, 2, 6, 3, 7, 4, 8] ➞ [1, 3, 5, 7, 2, 4, 6, 8]

Shuffle 3:

[1, 3, 5, 7, 2, 4, 6, 8] ➞ [1, 2, 3, 4, 5, 6, 7, 8]
// Back where we started.

Write a function that takes a positive even integer representing the number of the cards in a deck, and returns the number of out-shuffles required to return the deck to its original order.

Examples

shuffle_count(8) ➞ 3

shuffle_count(14) ➞ 12

shuffle_count(52) ➞ 8

Notes

  • The number of cards is always even and greater than one. Thus, the smallest possible deck size is two.
  • An iterative version of this challenge can be found here.

Problem 3 (22/9/22)

C#

Joseph is in the middle of packing for a vacation. He’s having a bit of trouble finding all of his socks, though.

Write a function that returns the number of sock pairs he has. A sock pair consists of two of the same letter, such as "AA" The socks are represented as an unordered sequence.

Examples

SockPairs("AA") ➞ 1

SockPairs("ABABC") ➞ 2

SockPairs("CABBACCC") ➞ 4

Notes

  • If given an empty string (no socks in the drawer), return 0
  • There can be multiple pairs of the same type of sock, such as two pairs of CC for the last example.

Problem 4 (23/9/22)

PHP

The ABACABA pattern is a recursive fractal pattern that shows up in many places in the real world (such as in geometry, art, music, poetry, number systems, literature and higher dimensions).

Create a function that takes a number n as an argument and returns a string that represents the full pattern.

Examples

abacabaPattern(1) ➞ "A"

abacabaPattern(2) ➞ "ABA"

abacabaPattern(3) ➞ "ABACABA"

Notes

  • Result should always be uppercase.