-2

Below is the problem assignment using tree recursion approach:

Maximum Subsequence A subsequence of a number is a series of (not necessarily contiguous) digits of the number. For example, 12345 has subsequences that include 123, 234, 124, 245, etc. Your task is to get the maximum subsequence below a certain length.

def max_subseq(n, l):
    """
    Return the maximum subsequence of length at most l that can be found in the given number n.
    For example, for n = 20125 and l = 3, we have that the subsequences are
        2
        0
        1
        2
        5
        20
        21
        22
        25
        01
        02
        05
        12
        15
        25
        201
        202
        205
        212
        215
        225
        012
        015
        025
        125
    and of these, the maxumum number is 225, so our answer is 225.

    >>> max_subseq(20125, 3)
    225
    >>> max_subseq(20125, 5)
    20125
    >>> max_subseq(20125, 6) # note that 20125 == 020125
    20125
    >>> max_subseq(12345, 3)
    345
    >>> max_subseq(12345, 0) # 0 is of length 0
    0
    >>> max_subseq(12345, 1)
    5
    """
    "*** YOUR CODE HERE ***"

There are two key insights for this problem

  • You need to split into the cases where the ones digit is used and the one where it is not. In the case where it is, we want to reduce l since we used one of the digits, and in the case where it isn't we do not.
  • In the case where we are using the ones digit, you need to put the digit back onto the end, and the way to attach a digit d to the end of a number n is 10 * n + d.

I could not understand the insights of this problem, mentioned below 2 points:

  1. split into the cases where the ones digit is used and the one where it is not

  2. In the case where we are using the ones digit, you need to put the digit back onto the end


My understanding of this problem:

Solution to this problem looks to generate all subsequences upto l, pseudo code looks like:

digitSequence := strconv.Itoa(n) // "20125"

printSubSequence = func(digitSequence string, currenSubSequenceSize int) { // digitSequence is "20125" and currenSubSequenceSize is say 3
        printNthSubSequence(digitSequence, currenSubSequenceSize) + printSubSequence(digitSequence, currenSubSequenceSize-1)
    }

where printNthSubSequence prints subsequences for (20125, 3) or (20125, 2) etc...

Finding max_subseq among all these sequences then becomes easy


Can you help me understand the insights given in this problem, with an example(say 20125, 1)? here is the complete question

overexchange
  • 15,768
  • 30
  • 152
  • 347

1 Answers1

2

Something like this? As the instructions suggest, try it with and without the current digit:

function f(s, i, l){
  if (i + 1 <= l)
    return Number(s.substr(0, l));

  if (!l)
    return 0;
    
  return Math.max(
    // With
    Number(s[i]) + 10 * f(s, i - 1, l - 1),
    // Without
    f(s, i - 1, l)
  );
}

var input = [
  ['20125', 3],
  ['20125', 5],
  ['20125', 6],
  ['12345', 3],
  ['12345', 0],
  ['12345', 1]
];

for (let [s, l] of input){
  console.log(s + ', l: ' + l);
  console.log(f(s, s.length-1, l));
  console.log('');
}
גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61
  • I did not get *With case:* `Number(s[i]) + 10 * f(s, i - 1, l - 1)` – overexchange Aug 24 '20 at 01:33
  • @overexchange `Number(s[i])` is the digit we are trying converted from character to number. We want to append it to a number "prefix," that we get as a result of the recursion. But the prefix needs to be "shifted" once to the left for us to append the digit numerically. We achieve this shift by multiplying the prefix by 10. – גלעד ברקן Aug 24 '20 at 01:51
  • What is the purpose of this base case? `if (i + 1 <= l)` – overexchange Aug 24 '20 at 02:11
  • @overexchange `(i + 1 == l)` means the length of the current string (`i`th index plus 1) is equal to `l` so a number with less digits than the whole string length can only be smaller, so we return the whole string as a number. But the instructions also gave the example where the string is smaller than `l`, prepending zeros, hence `<=`. I would post a separate question about getting all the subsequences of a certain length. – גלעד ברקן Aug 24 '20 at 02:30
  • How would this code look, if it needs to print all subsequences of size 3? Instead of finding max subsequence of size 3 – overexchange Aug 24 '20 at 03:34
  • @overexchange I would post a separate question on Stackoverflow about getting all the subsequences of a certain length. Or search - there is probably an answer in it already. – גלעד ברקן Aug 24 '20 at 03:46