My CodeGolf solutions

2 minute read

Published:

Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that solves a certain problem.

I occasionally take part in a codegolf challenge. I want to save some code I found kinda brilliant of me :-) (Of courses I couldn’t have reached that brilliantness without the help of suggesters in the field, thanks so much!).

This will be updated weekly, as soon as I found something new when attending these contests. You can use the codes freely, but please give credit.

aaabbabbc

Problem:

One element of the list aa, ab, ba, bb, c has been removed, and the remainder shuffled and joined with no separator. Find the missing element.

For example, if the input was aaabbac, this is made by concatenating aa ab ba c, so the output would be bb.

Solution (Python):

lambda a:'%x'%(-int(a.replace('c','0c'),16)%255-39)

Explain:

lambda a:                                                
         '%x'                                           # Observe that a,b,c are base-16 digits, this convert int to base-16 number
              %(-int(a.replace('c','0c'),               # Replace 'c' to '0c' so every string (aa, ab, ba, bb, 0c) have equal length, this serve for next step to extract sum of each string
                                          16)           # Convert to base 16
                                              %255      # Use int(a.replace('c',''),x)%(x*x-1) to extract sum of character at odd and even position.
                                                  -39)  # Shift to our answer

Monotone sequence beatitude

Problem:

Provided that the input is a monotone sequence of three or more integers:

  • Output -2 if the sequence strictly decreases. Example: [7,4,3,2]
  • Output -1 if the sequence monotone decreases but is neither strict nor constant. Example: [6,5,5]
  • Output 0 if the sequence is constant. Example: [0,0,0]
  • Output +1 if the sequence monotone increases but is neither strict nor constant. Example: [-1,2,2,2,4]
  • Output +2 if the sequence strictly increases. Example: [0,1,2]

Solution (Python 2):

lambda s:cmp(s[::-1],s)<<len(set(s))/len(s)

Explain:

lambda s:
         cmp(s[::-1],s)                                 # Technically compare first and last char to check if the array is increasing (output 1), constant (0) or decreasing (-1)
                       <<len(set(s))/len(s)             # Check if the array is stricly increasing/decreasing by checking for duplicate elements

Maximum meetings in one room

Problem

Provide two sequence, starting (a) and ending time (b) of N meetings. Find the maximum number of meetings that can be accommodated in the meeting room.

Input: a[] = {1, 3, 0, 5, 8, 5}, b[] = {2, 4, 6, 7, 9, 9} Output: 4

Solution (Javascript):

f=(a,b,l=1/0)=>Math.max(...a.map((d,p)=>b[p]<l&&f(a,b,d)+1))

Explain:

f=(a,b,
      l=1/0)=>                                                # l save current maximum b[i], dafault is infinity
              Math.max(...                                    # maximum f
                          a.map((d,p)=>                       # transform every element into the answer with it being new current maximum
                                       b[p]<l&&               # only count those b[p] < current maximum
                                                f(a,b,d)+1))  # recursive find answer with l=a[i] and plus 1