#Create sub-lists from input

1 messages · Page 1 of 1 (latest)

proper cradle
#

Entré = input
Résultat = Result (output)

The input must be on one line and the output must be displayed as the image. The values can be any length and any numbers

other examples

# input
5 1

# output
[5]
[1]
[5, 1]
# input
53 12 79 21 51 87

# output
[53]
[12]
[79]
[21]
[51]
[87]
[53, 12]
[12, 79]
[79, 21]
[21, 51]
[51, 87]
[53, 12, 79]
[12, 79, 21]
[79, 21, 51]
[21, 51, 87]
[53, 12, 79, 21]
[12, 79, 21, 51]
[79, 21, 51, 87]
[53, 12, 79, 21, 51]
[12, 79, 21, 51, 87]
[53, 12, 79, 21, 51, 87]
# input
6 1 82313

# output
[6]
[1]
[82313]
[6, 1]
[1, 82313]
[6, 1, 82313]
#

Create sub-lists from input

cursive phoenix
# proper cradle

each element have 2 choices:

  1. Include it in the sublist
  2. Ignore it
proper cradle
#

No they need to be included. You need to take in input multiple numbers (on the same line) and generate a list like shown in the image.

#

The values can be any numbers

cursive phoenix
proper cradle
#

Yea

#

But that's the thing, it's not 1, it's the first value

fringe geode
#

Submit where?

proper cradle
#

You can send it to me or here and obfuscated (although DM would be better for now)

cursive phoenix
novel vine
#

One can use 2 pointers method to solve it i think the time complexity would be O(n^2) though

#

Ill implement it once i come home

#

I cant think of a dp soln though

#

Only 2 pointer sliding through the array would be the only soln i think

strange dove
#

I think the solution I have in mind would be O(n^3) hmm

proper cradle
strange dove
novel vine
#

the implementation i though of doesnt work in cpp

#

it might work in python using list indexing

proper cradle
#

You can do it in Python if you want too

#

I did this exercise in a few languages such as Python

novel vine
#

Ive thought of another approach

novel vine
#

will take me a day i suppose but ill do it

#

wont be able to do it today

#

have work to do

strange dove
#

alright I have my O(n^3) solution coded

#

I just need to find a way to optimize it

strange dove
#

is it even possible to do it O(n^2)?

proper cradle
#

You can send it to me if you want and I might be able to give you some pointers if I can

And yes you can do it in O(n^2)

strange dove
#

sure

novel vine
#

My algorithm itself is O(n^3)

strange dove
#

@proper cradle after testing both of my algorithm, it looks like my first algo is better than my second lol

#

I dont remember the formula to get the right exponent tho

#

its probably n^2ish

frosty fjord
#

it's recursive like C(n,k) probability sets

tepid maple
#

||```java
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

    String[] input = inout.nextLine().split();

    int i = input.length;
    while (i > 0) {

        for (int j = 0; j < i; j++) {

            if ((input.length - i) > 0) {
                System.out.print("[");
                for (int k = 0; k < (input.length - i + 1); k++) {

                    System.out.print(input[j + k]);
                    if (k != (input.length - i)) {
                        System.out.print(", ");
                    }
                }
                System.out.println("]");
            } else {
                System.out.println("[" + input[j] + "]");
            }

        }
        i--;
    }

}
o(n**3) solution, iimma try to make it better brb
nvm, I'm too lazy to make it better
||
proper cradle
#

The input must be on one line
It's supposed to be a string that a user enters. You can't do inputs with the compiler bot but it still needs to come from a string

Ex.

String input = "5 1";

And you can simplify this

tepid maple
#

oopsie

#

I know it probably is possible to simplify it to be less verbose, haven't looked it that much

proper cradle
tepid maple
#

that would be making arrays inside of that Array to consist the indexes the new list that has to be constructed by?

proper cradle
#

I don't understand what you're saying but yes?

tepid maple
#

I assume I would create like a blueprint on how to make the sublists (marked by indexes) into that Arrays object?

proper cradle
#

No

tepid maple
#

then I'm not sure what you're referring to

proper cradle
#

java.util.Arrays is a class containing utility functions for arrays.

#

If you use some of those, (I have 2 in mind) you can simplify everything a lot.

For the second part, you can simplify the core logic of the solution by using only 2 for loops and not ifs

frosty fjord
frosty fjord
#

;compile java
||

import java.util.*;

public class TestSubList
{
  public static void printShiftList(
    final List<String> list, final int amount)
  {
    if (list == null) { System.out.println("[]"); return; }

    final int n = list.size();
    if (n == amount)
    {
      System.out.println(list);
    }
    else
    {
      for(int i = 0; i < n; ++i)
      {
        final int toIndex = i + amount;
        if (toIndex <= n)
        {
          final List<String> subList = list.subList(i, toIndex);
          System.out.println(subList);
        }
      }
    }
  }

  public static void printShiftList(final String input0)
  {
    final String input = (input0 == null) ? "" : input0.trim();

    System.out.println("# input");
    System.out.println(input);

    final String[] arr = input.split("\\s+");
    final List<String> list = Arrays.asList(arr);

    System.out.println("");
    System.out.println("# output");

    final int n = list.size();
    for(int i = 1; i <= n; ++i)
    {
      printShiftList(list, i);
    }

    System.out.println("");
  }

  // Test cases
  public static void main(String[] args)
  {
    final String input1234 = "1 2 3 4";
    printShiftList(input1234);

    final String input53 = "53 12 79 21 51 87";
    printShiftList(input53);

    final String input51 = "5 1";
    printShiftList(input51);

    final String input82 = "6 1 82313";
    printShiftList(input82);

    final String inputEmpty = "";
    printShiftList(inputEmpty);

    final String input5 = "5";
    printShiftList(input5);
  }
}

||

gloomy hillBOT
#
Program Output
# input
1 2 3 4

# output
[1]
[2]
[3]
[4]
[1, 2]
[2, 3]
[3, 4]
[1, 2, 3]
[2, 3, 4]
[1, 2, 3, 4]

# input
53 12 79 21 51 87

# output
[53]
[12]
[79]
[21]
[51]
[87]
[53, 12]
[12, 79]
[79, 21]
[21, 51]
[51, 87]
[53, 12, 79]
[12, 79, 21]
[79, 21, 51]
[21
frosty fjord
#

Works for all use cases 🙂 and null / empty cases

#

You forgot to mention if you wanted just print or return List<List<String>>

#

If you want a Scanner version I could wrap that into also

#

;compile java
||

import java.util.*;

public class TestSubList2
{
  public static <V> void printShiftList(final List<V> list, final int amount, final List<List<V>> output)
  {
    if (list == null)
    {
      System.out.println("[]");
      output.add(Collections.<V>emptyList());
      return;
    }

    final int n = list.size();
    if (n == amount)
    {
      System.out.println(list);
      output.add(list);
    }
    else
    {
      for(int i = 0; i < n; ++i)
      {
        final int toIndex = i + amount;
        if (toIndex <= n)
        {
          final List<V> subList = list.subList(i, toIndex);
          System.out.println(subList);
          output.add(subList);
        }
      }
    }
  }

  public static void printShiftList(final String input0)
  {
    final String input = (input0 == null) ? "" : input0.trim();

    System.out.println("# input");
    System.out.println(input);

    final String[] arr = input.split("\\s+");
    final List<String> list = Arrays.asList(arr);

    System.out.println("");
    System.out.println("# output");

    final int n    = list.size();
    final int half = n / 2;
    final int size = n * half + half;

    final List< List<String> > output = new ArrayList< List<String> >(size);
    for(int i = 1; i <= n; ++i)
    {
      printShiftList(list, i, output);
    }

    System.out.println("");
    System.out.println("# result");
    System.out.println("" + output);
    System.out.println("");

    assert(output.size() == size);
  }

  // Test cases
  public static void main(String[] args)
  {
    final String input1234 = "1 2 3 4";
    printShiftList(input1234);

    final String input53 = "53 12 79 21 51 87";
    printShiftList(input53);

    final String input51 = "5 1";
    printShiftList(input51);

    final String input82 = "6 1 82313";
    printShiftList(input82);

    final String inputEmpty = "";
    printShiftList(inputEmpty);

    final String input5 = "5";
    printShiftList(input5);
  }
}

||

gloomy hillBOT
#
Program Output
# input
1 2 3 4

# output
[1]
[2]
[3]
[4]
[1, 2]
[2, 3]
[3, 4]
[1, 2, 3]
[2, 3, 4]
[1, 2, 3, 4]

# result
[[1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3], [2, 3, 4], [1, 2, 3, 4]]

# input
53 12 79 21 51 87

# output
[53]
[12]
[79]
[21]
[51]
[
frosty fjord
#

Funnily, I found this criteria:

int half = n / 2;          
int output_size = n * half + half;
#

my solution is O(n²)

#

Total complexity probably something like:
O(n²/2 + 3n/2)
including the splitting part.

Excluding splitting, where List<V> is provided:
O(n²/2 + n/2)

frosty fjord
#

Had less sucess with the Perl5 translation from Java 😦

frosty fjord
#

;compile php
||

<?php
function printDumpArray2D($output){
  print "[\n";
  foreach($output as $i => $list){
    print ($i == 0) ? "\t" : ",\n\t";
    printDumpArray($list);
  }
  print "\n]\n";
}

function printDumpArray($list){
  print "[ ";
  print implode(', ', $list);
  print " ]";
}

function printShiftListItem($list, $amount, $output){
  if (!is_array($list)){
    $arr = array();
    printDumpArray($arr);
    print("\n");
    array_push($output, $arr);
    return $output;
  }

  $n = count($list);
  if ($n == $amount){
    printDumpArray($list);
    print("\n");
    array_push($output, $list);
  }else{
    for($i = 0; $i < $n; ++$i){
      $toIndex = $i + $amount;
      if ($toIndex <= $n){
        $subList = array_slice($list, $i, $amount);
        printDumpArray($subList);
        print("\n");
        array_push($output, $subList);
      }
    }
  }

  return $output;
}

function printShiftList($input0){
  $input = (!is_string($input0)) ? "" : trim($input0);
  print("# input\n");
  print($input);
  print("\n");
  $list = preg_split("/[\s,]+/", $input);

  print("\n");
  print("# output\n");

  $n    = count($list);
  $half = $n / 2;
  $size = $n * $half + $half;

  $output = array();
  for($i = 1; $i <= $n; ++$i){
    $output = printShiftListItem($list, $i, $output);
  }

  print("\n");
  print("# result");
  print("\n");
  printDumpArray2D($output);
  print("\n");

  return $output;
}

function main(){
$input1234 = "1 2 3 4";
printShiftList($input1234);

$input53 = "53 12 79 21 51 87";
printShiftList($input53);

$input51 = "5 1";
printShiftList($input51);

$input82 = "6 1 82313";
printShiftList($input82);

$inputEmpty = "";
printShiftList($inputEmpty);

$input5 = "5";
printShiftList($input5);
}
main();exit();

||

gloomy hillBOT
#
Program Output
# input
1 2 3 4

# output
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 1, 2 ]
[ 2, 3 ]
[ 3, 4 ]
[ 1, 2, 3 ]
[ 2, 3, 4 ]
[ 1, 2, 3, 4 ]

# result
[
[ 1 ],
[ 2 ],
[ 3 ],
[ 4 ],
[ 1, 2 ],
[ 2, 3 ],
[ 3, 4 ],
[ 1, 2, 3 ],
[ 2, 3, 4 ],
[ 1, 2, 3, 4 ]
]

# input
53 12 79 21 
frosty fjord
#

||

<html><body><pre><script>
function printDumpArray2D(output){
  var i=0,n=output ? +output.length : 0,list=[];
  document.write("\n[\n");
  for(;i<n;++i){
    document.write( (i == 0) ? "\t" : ",\n\t" );
    list = output[i];
    printDumpArray(list,true);
  }
  document.write("\n]\n");
}

function printDumpArray(list,nl){
  document.write("[ ");
  if (list && +list.length) document.write( list.join(', ') );
  document.write(" ]");
  document.write(nl ? "" : "\n");
}

function printShiftListItem(list, amount, output){
  if (!list){
    var arr = [];
    printDumpArray(arr);
    output.push(arr);
    return output;
  }

  var n = list ? +list.length : 0;
  if (n == amount){
    printDumpArray(list);
    output.push(list);
    return output;
  }else{
    for(var i = 0; i < n; ++i){
      var toIndex = i + amount;
      if (toIndex <= n){
        var subList = list.slice(i, toIndex);
        printDumpArray(subList);
        output.push(subList);
      }
    }
  }
  return output;
}

function printShiftList(input0){
  var input = !input0 ? "" : input0.replace(/^\s+|\s+$/gm,'');
  document.write("# input\n");
  document.write(input);
  document.write("\n");

  var list = input.split(/[\s,]+/);
  document.write("\n");
  document.write("# output\n");

  var n    = list ? +list.length : 0;
  var half = n / 2;
  var size = n * half + half;
  var output = [];
  for(var i = 1; i <= n; ++i){
    output = printShiftListItem(list, i, output);
  }

  document.write("\n");
  document.write("# result");
  document.write("\n");
  printDumpArray2D(output);
  document.write("\n");

  return output;
}
;!(function(){
  var input1234 = "1 2 3 4";
  printShiftList(input1234);

  var input53 = "53 12 79 21 51 87";
  printShiftList(input53);

  var input51 = "5 1";
  printShiftList(input51);

  var input82 = "6 1 82313";
  printShiftList(input82);

  var inputEmpty = "";
  printShiftList(inputEmpty);

  var input5 = "5";
  printShiftList(input5);
})();
</script>

||

#

JavaScript browser version 😄 (works in IE11+)

bold ferry
#

@proper cradle i dont get it

proper cradle
bold ferry
proper cradle
# bold ferry i dont get what the question is asking for

Okay... Basically, you're getting mulitple values as input and create a series of lists, each containing a subset of the values. The lists are generated in a specific order. Each individual number from the input sequence is output as a single-element list, then pairs of consecutive numbers, then triplets and so on until you reach a list of the size of the input values.

So, if you have 4 values, you will have 4 sub-lists of 1 value, then 3 sub-lists of 2, then 2 of 3 and finally 1 of 4

#

I am unsure what to add tbh. Look at the examples if you didn't, they should help.

proper cradle
bold ferry
bold ferry
#

||```py
list_ = [1,4,3,6]
for i in range(len(list_)):
for n in range(len(list_)-i):
print(list_[n:n+i+1])

5 minute job
proper cradle
proper cradle
bold ferry
#

?

proper cradle
#

You do it like you want but yes

#

Fyi, using list_ is a really bad name.

bold ferry
#

;-; well i was gonna call it list but thats a function in python

proper cradle
#

Yea, that's because calling it list is a bad name too haha

bold ferry
#

damnm

#

lst?

proper cradle
#

That's worst

bold ferry
#

😭 i use lst all the time

proper cradle
#

Names should describe what the variable is used for or contains

#

And generally, they are plural

#

For example, a list contains user could be called users.

#

In this case, it contains what? Values? Elements? Numbers (although if well made it could handle other type than numbers), etc

bold ferry
#

lemme test rq, i promise im listening

proper cradle
bold ferry
#

also idk where i went wrong on this other problem lmao but its WRONG

proper cradle
#

I recommend reading "clean code" by Robert C Martin. This a small overview of the naming section of it

bold ferry
#
print(smallest(261235))
print(smallest(209917))
print(smallest(285365))
print(smallest(269045))
print(smallest(296837))
#

[1216121315, 2, 4]
[209090107, 1, 4]
[2825232625, 0, 4]
[206090405, 3, 4]
[2926282327, 0, 4]

proper cradle