How to resolve the algorithm Non-continuous subsequences step by step in the CoffeeScript programming language
How to resolve the algorithm Non-continuous subsequences step by step in the CoffeeScript programming language
Table of Contents
Problem Statement
Consider some sequence of elements. (It differs from a mere set of elements by having an ordering among members.) A subsequence contains some subset of the elements of this sequence, in the same order. A continuous subsequence is one in which no elements are missing between the first and last elements of the subsequence. Note: Subsequences are defined structurally, not by their contents. So a sequence a,b,c,d will always have the same subsequences and continuous subsequences, no matter which values are substituted; it may even be the same value.
Task: Find all non-continuous subsequences for a given sequence.
For the sequence 1,2,3,4, there are five non-continuous subsequences, namely:
There are different ways to calculate those subsequences. Demonstrate algorithm(s) that are natural for the language.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Non-continuous subsequences step by step in the CoffeeScript programming language
Source code in the coffeescript programming language
is_contigous_binary = (n) ->
# return true if binary representation of n is
# of the form 1+0+
# examples:
# 0 true
# 1 true
# 100 true
# 110 true
# 1001 false
# 1010 false
# special case zero, or you'll get an infinite loop later
return true if n == 0
# first remove 0s from end
while n % 2 == 0
n = n / 2
# next, take advantage of the fact that a continuous
# run of 1s would be of the form 2^n - 1
is_power_of_two(n + 1)
is_power_of_two = (m) ->
while m % 2 == 0
m = m / 2
m == 1
seq_from_bitmap = (arr, n) ->
# grabs elements from array according to a bitmap
# e.g. if n == 13 (1101), and arr = ['a', 'b', 'c', 'd'],
# then return ['a', 'c', 'd'] (flipping bits to 1011, so
# that least significant bit comes first)
i = 0
new_arr = []
while n > 0
if n % 2 == 1
new_arr.push arr[i]
n -= 1
n /= 2
i += 1
new_arr
non_contig_subsequences = (arr) ->
# Return all subsqeuences from an array that have a "hole" in
# them. The order of the subsequences is not specified here.
# This algorithm uses binary counting, so it is limited to
# small lists, but large lists would be unwieldy regardless.
bitmasks = [0...Math.pow(2, arr.length)]
(seq_from_bitmap arr, n for n in bitmasks when !is_contigous_binary n)
arr = [1,2,3,4]
console.log non_contig_subsequences arr
for n in [1..10]
arr = [1..n]
num_solutions = non_contig_subsequences(arr).length
console.log "for n=#{n} there are #{num_solutions} solutions"
You may also check:How to resolve the algorithm Discordian date step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Compound data type step by step in the CLU programming language
You may also check:How to resolve the algorithm Align columns step by step in the Clojure programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the OCaml programming language